import { describe, it, expect, vi, beforeEach } from "vitest"; // Override the global setup.ts mock for keys — we need the REAL implementation vi.unmock("../services/keys.js"); vi.mock("../services/db.js", () => ({ default: { query: vi.fn(), connect: vi.fn(), on: vi.fn(), end: vi.fn() }, pool: { query: vi.fn(), connect: vi.fn(), on: vi.fn(), end: vi.fn() }, queryWithRetry: vi.fn().mockResolvedValue({ rows: [], rowCount: 0 }), connectWithRetry: vi.fn().mockResolvedValue(undefined), initDatabase: vi.fn().mockResolvedValue(undefined), cleanupStaleData: vi.fn(), isTransientError: vi.fn(), })); vi.mock("../services/logger.js", () => ({ default: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() }, })); import { queryWithRetry } from "../services/db.js"; import { updateEmailByCustomer, updateKeyEmail } from "../services/keys.js"; const mockQuery = vi.mocked(queryWithRetry); describe("updateEmailByCustomer DB fallback (BUG-108)", () => { beforeEach(() => { vi.clearAllMocks(); }); it("returns true and updates DB when key is NOT in cache but IS in DB", async () => { mockQuery .mockResolvedValueOnce({ rows: [{ key: "df_pro_abc123", tier: "pro", email: "old@example.com", created_at: "2026-01-01T00:00:00.000Z", stripe_customer_id: "cus_456", }], rowCount: 1, } as any) .mockResolvedValueOnce({ rows: [], rowCount: 1 } as any); // UPDATE const result = await updateEmailByCustomer("cus_456", "new@example.com"); expect(result).toBe(true); expect(mockQuery).toHaveBeenCalledWith( expect.stringContaining("SELECT"), expect.arrayContaining(["cus_456"]) ); expect(mockQuery).toHaveBeenCalledWith( expect.stringContaining("UPDATE"), expect.arrayContaining(["new@example.com", "cus_456"]) ); }); it("returns false when key is NOT in cache AND NOT in DB", async () => { mockQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 } as any); const result = await updateEmailByCustomer("cus_nonexistent", "new@example.com"); expect(result).toBe(false); const updateCalls = mockQuery.mock.calls.filter(c => (c[0] as string).includes("UPDATE")); expect(updateCalls).toHaveLength(0); }); }); describe("updateKeyEmail DB fallback (BUG-109)", () => { beforeEach(() => { vi.clearAllMocks(); }); it("returns true and updates DB when key is NOT in cache but IS in DB", async () => { mockQuery .mockResolvedValueOnce({ rows: [{ key: "df_pro_xyz789", tier: "pro", email: "old@example.com", created_at: "2026-01-01T00:00:00.000Z", stripe_customer_id: "cus_789", }], rowCount: 1, } as any) .mockResolvedValueOnce({ rows: [], rowCount: 1 } as any); // UPDATE const result = await updateKeyEmail("df_pro_xyz789", "new@example.com"); expect(result).toBe(true); expect(mockQuery).toHaveBeenCalledWith( expect.stringContaining("SELECT"), expect.arrayContaining(["df_pro_xyz789"]) ); expect(mockQuery).toHaveBeenCalledWith( expect.stringContaining("UPDATE"), expect.arrayContaining(["new@example.com", "df_pro_xyz789"]) ); }); it("returns false when key is NOT in cache AND NOT in DB", async () => { mockQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 } as any); const result = await updateKeyEmail("df_pro_nonexistent", "new@example.com"); expect(result).toBe(false); const updateCalls = mockQuery.mock.calls.filter(c => (c[0] as string).includes("UPDATE")); expect(updateCalls).toHaveLength(0); }); });