'use strict'; /** * Integration tests – geoutil controller (controllers/geoutil.js) */ const { connectDB, disconnectDB, clearCollection } = require('./jest.setup'); const { mockApplicator, mockClient, mockJob, mockReq, mockRes, newId } = require('./mock_data'); let Customer, Client, Job; beforeAll(async () => { await connectDB(); Customer = require('../../model/customer'); Client = require('../../model/client'); Job = require('../../model/job'); }); afterAll(async () => { await disconnectDB(); }); const geoutilCtl = require('../../controllers/geoutil'); describe('geoutil controller – generateLines_post', () => { let applicator, client; beforeAll(async () => { applicator = await Customer.create(mockApplicator()); client = await Client.create(mockClient(applicator._id)); }); afterAll(async () => { await Customer.deleteMany({ _id: { $in: [applicator._id, client._id] } }); await clearCollection(Job); }); const makeReq = (extra = {}) => mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra }); it('calls res.end() when body is null', async () => { const req = makeReq({ body: {} }); req.body = null; const res = mockRes(); await geoutilCtl.generateLines_post(req, res); expect(res.end).toHaveBeenCalled(); }); it('throws when jobId is missing from body', async () => { const req = makeReq({ body: { swath: 60 } }); const res = mockRes(); await expect(geoutilCtl.generateLines_post(req, res)).rejects.toThrow(); }); it('throws when swath is missing from body', async () => { const req = makeReq({ body: { jobId: 9999 } }); const res = mockRes(); await expect(geoutilCtl.generateLines_post(req, res)).rejects.toThrow(); }); it('returns empty array when no job matches the given jobId', async () => { const req = makeReq({ body: { jobId: 9999999, swath: 60 } }); const res = mockRes(); await geoutilCtl.generateLines_post(req, res); expect(res.json).toHaveBeenCalledWith([]); }); it('returns empty array when job exists but has no spray areas and generate is false', async () => { const job = await Job.create({ ...mockJob(applicator._id), client: client._id }); const req = makeReq({ body: { jobId: job._id, swath: 60, generate: false } }); const res = mockRes(); await geoutilCtl.generateLines_post(req, res); // sprayAreas is empty → $unwind eliminates the doc → no lines found expect(res.json).toHaveBeenCalled(); expect(Array.isArray(res._data)).toBe(true); }); });