170 lines
5.8 KiB
TypeScript
170 lines
5.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import type { OutboxRequeueOutput } from "./_lib.js";
|
|
|
|
const runById = vi.fn();
|
|
const runBulk = vi.fn();
|
|
|
|
vi.mock("./_lib.js", () => ({
|
|
runOutboxRequeueById: (...args: unknown[]) => runById(...args),
|
|
runOutboxRequeueBulk: (...args: unknown[]) => runBulk(...args),
|
|
normalizeTenantId: (value: unknown) => {
|
|
const tenantId = typeof value === "string" ? value.trim() : "";
|
|
if (!tenantId) throw new Error("--tenant must be a non-empty database identifier");
|
|
return tenantId;
|
|
},
|
|
}));
|
|
vi.mock("@hivegrid/db/client.server", () => ({
|
|
getDbClient: () => ({ outboxEvent: {} }),
|
|
}));
|
|
vi.mock("consola", () => ({
|
|
default: {
|
|
log: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
success: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
import consola from "consola";
|
|
import { outboxRequeueCommand } from "./requeue.js";
|
|
|
|
const consoleMock = vi.mocked(consola);
|
|
|
|
describe("outbox requeue command wrapper", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
process.exitCode = undefined;
|
|
runById.mockResolvedValue({ exitCode: 0 });
|
|
runBulk.mockResolvedValue({ exitCode: 0 });
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.exitCode = undefined;
|
|
});
|
|
|
|
it("rejects passing both --event-id and --type", async () => {
|
|
await outboxRequeueCommand.run?.({ args: { "event-id": "evt-1", type: "a.b.c" } } as never);
|
|
|
|
expect(consoleMock.error).toHaveBeenCalledWith("Pass --event-id OR (--type + --since), not both.");
|
|
expect(process.exitCode).toBe(1);
|
|
expect(runById).not.toHaveBeenCalled();
|
|
expect(runBulk).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects when neither mode is fully specified", async () => {
|
|
await outboxRequeueCommand.run?.({ args: { type: "a.b.c" } } as never);
|
|
|
|
expect(consoleMock.error).toHaveBeenCalledWith("Either --event-id or (--type + --since) is required.");
|
|
expect(process.exitCode).toBe(1);
|
|
expect(runById).not.toHaveBeenCalled();
|
|
expect(runBulk).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("delegates single-event mode and adopts the lib outcome exit code", async () => {
|
|
runById.mockResolvedValueOnce({ exitCode: 0 });
|
|
|
|
await outboxRequeueCommand.run?.({ args: { tenant: "tenant-1", "event-id": "evt-1" } } as never);
|
|
|
|
expect(runById).toHaveBeenCalledWith(
|
|
{ tenantId: "tenant-1", eventId: "evt-1", json: false },
|
|
expect.objectContaining({ client: expect.anything(), output: expect.anything() }),
|
|
);
|
|
expect(runBulk).not.toHaveBeenCalled();
|
|
expect(process.exitCode).toBe(0);
|
|
});
|
|
|
|
it("delegates bulk mode with a parsed limit and apply flags", async () => {
|
|
runBulk.mockResolvedValueOnce({ exitCode: 0 });
|
|
|
|
await outboxRequeueCommand.run?.({
|
|
args: {
|
|
type: "billing.subscription.ended",
|
|
tenant: "tenant-1",
|
|
since: "2026-05-01T00:00:00Z",
|
|
apply: true,
|
|
"include-done": true,
|
|
limit: "42",
|
|
},
|
|
} as never);
|
|
|
|
expect(runBulk).toHaveBeenCalledWith(
|
|
{
|
|
tenantId: "tenant-1",
|
|
type: "billing.subscription.ended",
|
|
since: "2026-05-01T00:00:00Z",
|
|
apply: true,
|
|
includeDone: true,
|
|
limit: 42,
|
|
json: false,
|
|
},
|
|
expect.objectContaining({ client: expect.anything(), output: expect.anything() }),
|
|
);
|
|
expect(process.exitCode).toBe(0);
|
|
});
|
|
|
|
it("omits limit from the bulk payload when not parseable", async () => {
|
|
await outboxRequeueCommand.run?.({
|
|
args: { tenant: "tenant-1", type: "a.b.c", since: "2026-05-01T00:00:00Z", limit: "not-a-number" },
|
|
} as never);
|
|
|
|
const payload = runBulk.mock.calls[0]?.[0] as Record<string, unknown>;
|
|
expect(payload).not.toHaveProperty("limit");
|
|
expect(payload).toMatchObject({ tenantId: "tenant-1", apply: false, includeDone: false, json: false });
|
|
});
|
|
|
|
it("routes the lib output sink through consola (text mode)", async () => {
|
|
runById.mockImplementationOnce(async (_input: unknown, ctx: { output: OutboxRequeueOutput }) => {
|
|
ctx.output.log("done");
|
|
ctx.output.warn("careful");
|
|
ctx.output.error("nope");
|
|
ctx.output.json({ a: 1 });
|
|
return { exitCode: 0 };
|
|
});
|
|
|
|
await outboxRequeueCommand.run?.({ args: { tenant: "tenant-1", "event-id": "evt-1" } } as never);
|
|
|
|
expect(consoleMock.success).toHaveBeenCalledWith("done");
|
|
expect(consoleMock.warn).toHaveBeenCalledWith("careful");
|
|
expect(consoleMock.error).toHaveBeenCalledWith("nope");
|
|
expect(consoleMock.log).toHaveBeenCalledWith(JSON.stringify({ a: 1 }));
|
|
});
|
|
|
|
it("suppresses log/error decorations in JSON mode but keeps warnings", async () => {
|
|
runById.mockImplementationOnce(async (_input: unknown, ctx: { output: OutboxRequeueOutput }) => {
|
|
ctx.output.log("done");
|
|
ctx.output.warn("careful");
|
|
ctx.output.error("nope");
|
|
return { exitCode: 0 };
|
|
});
|
|
|
|
await outboxRequeueCommand.run?.({ args: { tenant: "tenant-1", "event-id": "evt-1", json: true } } as never);
|
|
|
|
expect(consoleMock.success).not.toHaveBeenCalled();
|
|
expect(consoleMock.error).not.toHaveBeenCalled();
|
|
expect(consoleMock.warn).toHaveBeenCalledWith("careful");
|
|
});
|
|
|
|
it("catches lib exceptions and reports a human error in text mode", async () => {
|
|
runById.mockRejectedValueOnce(new Error("db down"));
|
|
|
|
await outboxRequeueCommand.run?.({ args: { tenant: "tenant-1", "event-id": "evt-1" } } as never);
|
|
|
|
expect(consoleMock.error).toHaveBeenCalledWith("outbox requeue failed: db down");
|
|
expect(process.exitCode).toBe(1);
|
|
});
|
|
|
|
it("catches lib exceptions and emits a JSON failure envelope in JSON mode", async () => {
|
|
runBulk.mockRejectedValueOnce("boom");
|
|
|
|
await outboxRequeueCommand.run?.({
|
|
args: { tenant: "tenant-1", type: "a.b.c", since: "2026-05-01T00:00:00Z", json: true },
|
|
} as never);
|
|
|
|
expect(consoleMock.log).toHaveBeenCalledWith(
|
|
JSON.stringify({ ok: false, reason: "exception", message: "boom" }),
|
|
);
|
|
expect(process.exitCode).toBe(1);
|
|
});
|
|
});
|