// @vitest-environment node import { describe, it, expect, vi, beforeEach } from 'vitest' const { selectQueue, setSpy, insertSpy, audit, txError } = vi.hoisted(() => ({ selectQueue: [] as unknown[][], setSpy: vi.fn(), insertSpy: vi.fn(), audit: vi.fn(), txError: { code: null as string | null }, })) vi.mock('@/lib/auth/require-admin', () => ({ requireAdmin: async () => ({ session: { sub: 'admin-1', role: 'admin' } }), })) vi.mock('@/lib/db/audit', () => ({ writeAuditLog: audit })) vi.mock('@/lib/db/with-user', () => { const tx = { execute: async () => undefined, insert: () => ({ values: (v: unknown) => { insertSpy(v) return { returning: async () => [{ id: 'truck-new' }] } }, }), update: () => ({ set: (v: unknown) => { setSpy(v) return { where: async () => { if (txError.code) throw Object.assign(new Error('db'), { code: txError.code }) }, } }, }), } const db = { select: () => ({ from: () => ({ where: () => ({ limit: async () => selectQueue.shift() ?? [] }) }) }), transaction: async (fn: (t: unknown) => unknown) => fn(tx), } return { asAdmin: async (fn: (d: unknown) => unknown) => fn(db) } }) import { POST, PATCH } from '@/app/api/admin/trucks/route' const SITE = '3f2b8c1e-9d4a-4e6f-8a1b-2c3d4e5f6a7b' const req = (method: string, body: unknown) => new Request('http://localhost/api/admin/trucks', { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) as never beforeEach(() => { selectQueue.length = 0 setSpy.mockClear() insertSpy.mockClear() audit.mockClear() txError.code = null }) describe('POST /api/admin/trucks', () => { it('creates a truck with no site', async () => { const res = await POST(req('POST', { truck_no: 'WXY 1' })) expect(res.status).toBe(201) expect(insertSpy).toHaveBeenCalledWith({ truckNo: 'WXY 1', carrier: null, siteId: null }) }) it('creates a truck linked to an active site and audits it', async () => { selectQueue.push([{ id: SITE }]) const res = await POST(req('POST', { truck_no: 'WXY 2', site_id: SITE })) expect(res.status).toBe(201) expect(insertSpy).toHaveBeenCalledWith({ truckNo: 'WXY 2', carrier: null, siteId: SITE }) expect(audit).toHaveBeenCalledTimes(1) expect(audit.mock.calls[0][4]).toMatchObject({ site_id: SITE }) }) it('422 for a malformed site_id', async () => { const res = await POST(req('POST', { truck_no: 'WXY 3', site_id: 'abc' })) expect(res.status).toBe(422) expect(insertSpy).not.toHaveBeenCalled() }) it('422 for an unknown or inactive site', async () => { selectQueue.push([]) const res = await POST(req('POST', { truck_no: 'WXY 4', site_id: SITE })) expect(res.status).toBe(422) expect(insertSpy).not.toHaveBeenCalled() }) }) describe('PATCH /api/admin/trucks', () => { it('sets site_id and audits with the previous row', async () => { selectQueue.push([{ id: 't1', siteId: null }], [{ id: SITE }]) const res = await PATCH(req('PATCH', { id: 't1', site_id: SITE })) expect(res.status).toBe(200) expect(setSpy).toHaveBeenCalledWith({ siteId: SITE }) expect(audit).toHaveBeenCalledTimes(1) expect(audit.mock.calls[0][4]).toEqual({ siteId: SITE }) expect(audit.mock.calls[0][5]).toEqual({ id: 't1', siteId: null }) }) it('clears the site with null', async () => { selectQueue.push([{ id: 't1', siteId: SITE }]) const res = await PATCH(req('PATCH', { id: 't1', site_id: null })) expect(res.status).toBe(200) expect(setSpy).toHaveBeenCalledWith({ siteId: null }) }) it('edits truck_no and carrier together', async () => { selectQueue.push([{ id: 't1' }]) const res = await PATCH(req('PATCH', { id: 't1', truck_no: ' ABC 9 ', carrier: ' ' })) expect(res.status).toBe(200) expect(setSpy).toHaveBeenCalledWith({ truckNo: 'ABC 9', carrier: null }) }) it('still toggles active', async () => { selectQueue.push([{ id: 't1' }]) const res = await PATCH(req('PATCH', { id: 't1', active: false })) expect(res.status).toBe(200) expect(setSpy).toHaveBeenCalledWith({ active: false }) }) it('422 when nothing to change', async () => { const res = await PATCH(req('PATCH', { id: 't1' })) expect(res.status).toBe(422) }) it('422 for an empty truck_no', async () => { const res = await PATCH(req('PATCH', { id: 't1', truck_no: ' ' })) expect(res.status).toBe(422) expect(setSpy).not.toHaveBeenCalled() }) it('422 for a malformed or unknown site', async () => { expect((await PATCH(req('PATCH', { id: 't1', site_id: 'abc' }))).status).toBe(422) selectQueue.push([{ id: 't1' }], []) expect((await PATCH(req('PATCH', { id: 't1', site_id: SITE }))).status).toBe(422) expect(setSpy).not.toHaveBeenCalled() }) it('skips the active-site check when the site is unchanged', async () => { selectQueue.push([{ id: 't1', siteId: SITE }]) const res = await PATCH(req('PATCH', { id: 't1', truck_no: 'NEW 1', site_id: SITE })) expect(res.status).toBe(200) expect(setSpy).toHaveBeenCalledWith({ truckNo: 'NEW 1', siteId: SITE }) }) it('422 when moving a site-less truck to an inactive site', async () => { selectQueue.push([{ id: 't1', siteId: null }], []) const res = await PATCH(req('PATCH', { id: 't1', site_id: SITE })) expect(res.status).toBe(422) expect(setSpy).not.toHaveBeenCalled() }) it('409 for a duplicate truck number', async () => { selectQueue.push([{ id: 't1' }]) txError.code = '23505' const res = await PATCH(req('PATCH', { id: 't1', truck_no: 'DUP 1' })) expect(res.status).toBe(409) }) })