From a07fe4574a864442c7fa7bd4ab7bf8e5d6d4da8b Mon Sep 17 00:00:00 2001 From: lorentz Date: Fri, 10 Jul 2026 17:33:52 -0400 Subject: [PATCH] test(10-01): add failing tests for pax8-factory config check + singleton --- lib/services/pax8-factory.test.ts | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/services/pax8-factory.test.ts diff --git a/lib/services/pax8-factory.test.ts b/lib/services/pax8-factory.test.ts new file mode 100644 index 0000000..dd2d6d9 --- /dev/null +++ b/lib/services/pax8-factory.test.ts @@ -0,0 +1,61 @@ +import { describe, it, expect, beforeEach } from 'vitest'; +import { isPax8Configured, getPax8Client, _resetPax8Client } from './pax8-factory'; + +beforeEach(() => { + delete process.env.PAX8_CLIENT_ID; + delete process.env.PAX8_CLIENT_SECRET; + _resetPax8Client(); +}); + +describe('isPax8Configured', () => { + it('returns false when neither env var is set', () => { + expect(isPax8Configured()).toBe(false); + }); + + it('returns false when only PAX8_CLIENT_ID is set', () => { + process.env.PAX8_CLIENT_ID = 'id1'; + expect(isPax8Configured()).toBe(false); + }); + + it('returns false when only PAX8_CLIENT_SECRET is set', () => { + process.env.PAX8_CLIENT_SECRET = 'secret1'; + expect(isPax8Configured()).toBe(false); + }); + + it('returns true when both env vars are set', () => { + process.env.PAX8_CLIENT_ID = 'id1'; + process.env.PAX8_CLIENT_SECRET = 'secret1'; + expect(isPax8Configured()).toBe(true); + }); +}); + +describe('getPax8Client', () => { + it('throws the exact configuration error when not configured', () => { + expect(() => getPax8Client()).toThrow( + 'PAX8 is not configured — set PAX8_CLIENT_ID and PAX8_CLIENT_SECRET' + ); + }); + + it('returns a client instance when both env vars are set', () => { + process.env.PAX8_CLIENT_ID = 'id1'; + process.env.PAX8_CLIENT_SECRET = 'secret1'; + expect(getPax8Client()).toBeDefined(); + }); + + it('returns the same cached instance on repeated calls', () => { + process.env.PAX8_CLIENT_ID = 'id1'; + process.env.PAX8_CLIENT_SECRET = 'secret1'; + const first = getPax8Client(); + const second = getPax8Client(); + expect(second).toBe(first); + }); + + it('rebuilds a new instance after _resetPax8Client()', () => { + process.env.PAX8_CLIENT_ID = 'id1'; + process.env.PAX8_CLIENT_SECRET = 'secret1'; + const first = getPax8Client(); + _resetPax8Client(); + const second = getPax8Client(); + expect(second).not.toBe(first); + }); +});