155 lines
4.6 KiB
TypeScript
155 lines
4.6 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
type LeadRecord = Record<string, unknown> & {
|
|
id: string;
|
|
email: string;
|
|
firstName?: string | null;
|
|
lastName?: string | null;
|
|
company?: string | null;
|
|
status: string;
|
|
source?: string | null;
|
|
utmSource?: string | null;
|
|
createdAt: Date;
|
|
};
|
|
|
|
const dbMock = {
|
|
lead: {
|
|
findMany: vi.fn<() => Promise<LeadRecord[]>>(),
|
|
},
|
|
};
|
|
const writeFileMock = vi.fn();
|
|
|
|
vi.mock("@toolctrl/db/client.server", () => ({
|
|
getDbClient: () => dbMock,
|
|
}));
|
|
vi.mock("node:fs/promises", () => ({
|
|
default: { writeFile: (...args: unknown[]) => writeFileMock(...args) },
|
|
writeFile: (...args: unknown[]) => writeFileMock(...args),
|
|
}));
|
|
vi.mock("consola", () => ({
|
|
default: {
|
|
info: vi.fn(),
|
|
log: vi.fn(),
|
|
success: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
import consola from "consola";
|
|
import { leadsExportCommand } from "./export.js";
|
|
import { leadsListCommand } from "./list.js";
|
|
|
|
const consoleMock = vi.mocked(consola);
|
|
|
|
describe("leads CLI commands", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
dbMock.lead.findMany.mockResolvedValue([]);
|
|
writeFileMock.mockResolvedValue(undefined);
|
|
});
|
|
|
|
it("lists captured leads with bounded limit, status filter, and display fallbacks", async () => {
|
|
dbMock.lead.findMany.mockResolvedValueOnce([
|
|
{
|
|
id: "lead_1",
|
|
email: "ada@example.com",
|
|
firstName: "Ada",
|
|
lastName: "Lovelace",
|
|
company: "Analytical",
|
|
status: "new",
|
|
source: null,
|
|
utmSource: "newsletter",
|
|
createdAt: new Date("2026-05-25T12:00:00.000Z"),
|
|
},
|
|
{
|
|
id: "lead_2",
|
|
email: "grace@example.com",
|
|
firstName: null,
|
|
lastName: null,
|
|
company: null,
|
|
status: "contacted",
|
|
source: "form",
|
|
utmSource: null,
|
|
createdAt: new Date("2026-05-24T12:00:00.000Z"),
|
|
},
|
|
]);
|
|
|
|
await leadsListCommand.run?.({ args: { status: "new", limit: "999" } } as never);
|
|
|
|
expect(dbMock.lead.findMany).toHaveBeenCalledWith({
|
|
where: { status: "new" },
|
|
orderBy: { createdAt: "desc" },
|
|
take: 500,
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
company: true,
|
|
status: true,
|
|
source: true,
|
|
utmSource: true,
|
|
createdAt: true,
|
|
},
|
|
});
|
|
expect(consoleMock.log).toHaveBeenCalledWith(expect.stringContaining("utm=newsletter Ada Lovelace"));
|
|
expect(consoleMock.log).toHaveBeenCalledWith(expect.stringContaining("src=form utm=- -"));
|
|
expect(consoleMock.success).toHaveBeenCalledWith("2 leads listed.");
|
|
});
|
|
|
|
it("reports empty lead lists with and without status filters", async () => {
|
|
await leadsListCommand.run?.({ args: {} } as never);
|
|
await leadsListCommand.run?.({ args: { status: "archived" } } as never);
|
|
|
|
expect(consoleMock.info).toHaveBeenCalledWith("No leads captured yet.");
|
|
expect(consoleMock.info).toHaveBeenCalledWith("No leads with status=archived.");
|
|
expect(consoleMock.success).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("exports escaped CSV to stdout", async () => {
|
|
const stdoutSpy = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
dbMock.lead.findMany.mockResolvedValueOnce([
|
|
{
|
|
id: "lead_1",
|
|
email: "ada@example.com",
|
|
firstName: "Ada",
|
|
lastName: 'Love, "Countess"',
|
|
phone: null,
|
|
company: "Analytical\nEngines",
|
|
source: "form",
|
|
status: "new",
|
|
utmSource: "newsletter",
|
|
createdAt: new Date("2026-05-25T12:00:00.000Z"),
|
|
lastTouchAt: null,
|
|
},
|
|
]);
|
|
|
|
await leadsExportCommand.run?.({ args: { status: "new" } } as never);
|
|
|
|
expect(dbMock.lead.findMany).toHaveBeenCalledWith({
|
|
where: { status: "new" },
|
|
orderBy: { createdAt: "desc" },
|
|
});
|
|
const csv = String(stdoutSpy.mock.calls[0]?.[0]);
|
|
expect(csv).toContain("id,email,firstName,lastName");
|
|
expect(csv).toContain('"Love, ""Countess"""');
|
|
expect(csv).toContain('"Analytical\nEngines"');
|
|
expect(writeFileMock).not.toHaveBeenCalled();
|
|
expect(consoleMock.success).toHaveBeenCalledWith("Exported 1 leads.");
|
|
|
|
stdoutSpy.mockRestore();
|
|
});
|
|
|
|
it("writes CSV exports to a file when requested", async () => {
|
|
dbMock.lead.findMany.mockResolvedValueOnce([]);
|
|
|
|
await leadsExportCommand.run?.({ args: { out: "/tmp/leads.csv" } } as never);
|
|
|
|
expect(writeFileMock).toHaveBeenCalledWith(
|
|
"/tmp/leads.csv",
|
|
expect.stringMatching(/^id,email,firstName,/u),
|
|
"utf8",
|
|
);
|
|
expect(consoleMock.success).toHaveBeenCalledWith("Wrote 0 leads to /tmp/leads.csv.");
|
|
});
|
|
});
|