From 0d7974cdd9d2425a6bec311260c7a7bbf666bb91 Mon Sep 17 00:00:00 2001 From: lorentz Date: Thu, 16 Jul 2026 19:45:04 -0400 Subject: [PATCH] test(23-05): add failing tests for getCompanyAutomationGate reader - covers absent-row, present-row mapping, null/NaN companyId short-circuit --- lib/services/phishing-automation-gate.test.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 lib/services/phishing-automation-gate.test.ts diff --git a/lib/services/phishing-automation-gate.test.ts b/lib/services/phishing-automation-gate.test.ts new file mode 100644 index 0000000..05b9d8c --- /dev/null +++ b/lib/services/phishing-automation-gate.test.ts @@ -0,0 +1,52 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +// Mock postgresClient BEFORE importing the module under test — mirrors +// campaign-classifier.test.ts's vi.mock() factory-mocking discipline. +const queryMock = vi.fn(); +vi.mock('./postgres-client', () => ({ + postgresClient: { + query: (...args: unknown[]) => queryMock(...args), + }, +})); + +// eslint-disable-next-line import/first -- imported after vi.mock hoisting +import { getCompanyAutomationGate } from './phishing-automation-gate'; + +describe('getCompanyAutomationGate', () => { + beforeEach(() => { + queryMock.mockReset(); + }); + + it('resolves to all-false when no row exists for the company', async () => { + queryMock.mockResolvedValueOnce({ rows: [] }); + + const gate = await getCompanyAutomationGate(123); + + expect(gate).toEqual({ autoParse: false, autoClassify: false, autoReport: false }); + expect(queryMock).toHaveBeenCalledTimes(1); + }); + + it('resolves to the mapped camelCase values when a row exists', async () => { + queryMock.mockResolvedValueOnce({ + rows: [{ auto_parse: true, auto_classify: false, auto_report: true }], + }); + + const gate = await getCompanyAutomationGate(456); + + expect(gate).toEqual({ autoParse: true, autoClassify: false, autoReport: true }); + }); + + it('resolves to all-false without querying when companyId is null', async () => { + const gate = await getCompanyAutomationGate(null); + + expect(gate).toEqual({ autoParse: false, autoClassify: false, autoReport: false }); + expect(queryMock).not.toHaveBeenCalled(); + }); + + it('resolves to all-false without querying when companyId is NaN', async () => { + const gate = await getCompanyAutomationGate(NaN); + + expect(gate).toEqual({ autoParse: false, autoClassify: false, autoReport: false }); + expect(queryMock).not.toHaveBeenCalled(); + }); +});