import { beforeEach, describe, expect, it, vi } from 'vitest'; const { query } = vi.hoisted(() => ({ query: vi.fn() })); vi.mock('../src/db', () => ({ pool: { query }, })); import { attachCategoryPhotoInspectionRequirements, isCategoryPhotoInspectionRequired, } from '../src/services/inspectionPhotoService'; describe('category photo inspection rule', () => { beforeEach(() => { query.mockReset(); }); it('uses the category setting within the requested branch', async () => { query.mockResolvedValueOnce({ rows: [{ requires_photo_inspection: false }] }); await expect( isCategoryPhotoInspectionRequired({ branchCode: 'BR002', categoryCode: 'VEG001', }) ).resolves.toBe(false); expect(query).toHaveBeenCalledWith( expect.stringContaining('branch_code = $2'), ['VEG001', 'BR002'] ); }); it('defaults to requiring photos when category configuration is missing', async () => { query.mockResolvedValueOnce({ rows: [] }); await expect( isCategoryPhotoInspectionRequired({ branchCode: 'BR002', categoryCode: 'UNKNOWN', }) ).resolves.toBe(true); }); it('resolves a missing category from the sku and caches identical task rules', async () => { query .mockResolvedValueOnce({ rows: [{ category_code: 'MEAT001' }] }) .mockResolvedValueOnce({ rows: [{ requires_photo_inspection: true }] }); const rows = await attachCategoryPhotoInspectionRequirements([ { branch_code: 'BR002', sku_code: 'SKU001' }, { branch_code: 'BR002', sku_code: 'SKU001' }, ]); expect(rows.map((row) => row.requires_photo_inspection)).toEqual([true, true]); expect(query).toHaveBeenCalledTimes(2); }); });