'use strict'; /** * Integration tests – pilot controller (controllers/pilot.js) */ const { connectDB, disconnectDB, clearCollection } = require('./jest.setup'); const { mockApplicator, mockPilot, mockReq, mockRes, newId } = require('./mock_data'); let Pilot, Customer; beforeAll(async () => { await connectDB(); Customer = require('../../model/customer'); Pilot = require('../../model/pilot'); }); afterAll(async () => { await disconnectDB(); }); const pilotCtl = require('../../controllers/pilot'); describe('pilot controller – data methods', () => { let applicator; beforeAll(async () => { await clearCollection(Pilot); applicator = await Customer.create(mockApplicator()); }); afterAll(async () => { await clearCollection(Pilot); await Customer.deleteMany({ _id: applicator._id }); }); const makeReq = (extra = {}) => mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra }); // ------------------------------------------------------------------------- describe('create_post', () => { it('creates a pilot and returns it', async () => { const req = makeReq({ body: mockPilot(applicator._id) }); const res = mockRes(); await pilotCtl.createPilot_post(req, res); expect(res.json).toHaveBeenCalled(); expect(res._data._id).toBeDefined(); expect(res._data.name).toBe('Test Pilot'); }); it('throws when body is missing', async () => { const req = makeReq({ body: null }); const res = mockRes(); await expect(pilotCtl.createPilot_post(req, res)).rejects.toThrow(); }); }); // ------------------------------------------------------------------------- describe('getById_get', () => { let pilotId; beforeAll(async () => { const doc = await Pilot.create(mockPilot(applicator._id)); pilotId = String(doc._id); }); it('returns pilot by id', async () => { const req = makeReq({ params: { id: pilotId } }); const res = mockRes(); await pilotCtl.getPilot_get(req, res); expect(res.json).toHaveBeenCalled(); expect(String(res._data._id)).toBe(pilotId); }); }); // ------------------------------------------------------------------------- describe('search_post', () => { it('returns matching pilots', async () => { const req = makeReq({ body: { byUserId: applicator._id } }); const res = mockRes(); await pilotCtl.search_post(req, res); expect(res.json).toHaveBeenCalled(); expect(Array.isArray(res._data)).toBe(true); }); }); // ------------------------------------------------------------------------- describe('delete_delete', () => { let pilotId; beforeAll(async () => { const doc = await Pilot.create(mockPilot(applicator._id)); pilotId = String(doc._id); }); it.skip('soft-deletes a pilot (skipped: requires MongoDB replica-set for transactions)', async () => { const req = makeReq({ params: { id: pilotId } }); const res = mockRes(); await pilotCtl.deletePilot(req, res); expect(res.json).toHaveBeenCalled(); const found = await Pilot.findById(pilotId); expect(!found || found.markedDelete).toBeTruthy(); }); }); });