Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -2,6 +2,7 @@ import { describe, it, expect, vi } from "vitest";
import {
normalizeStatus,
normalizeTenantId,
runOutboxRequeueById,
runOutboxRequeueBulk,
type OutboxRequeueClient,
@@ -16,29 +17,58 @@ describe("normalizeStatus", () => {
});
});
describe("normalizeTenantId", () => {
it("trims the tenant identifier shared with the CLI RLS context", () => {
expect(normalizeTenantId(" tenant-1 ")).toBe("tenant-1");
});
it("rejects empty and NUL-containing tenant identifiers", () => {
expect(() => normalizeTenantId(" ")).toThrow(/--tenant/);
expect(() => normalizeTenantId("tenant\u0000other")).toThrow(/--tenant/);
});
});
interface FakeState {
byId: Map<string, OutboxRequeueEventRow>;
updates: Array<{ id: string; data: Record<string, unknown> }>;
updateMany: Array<{ ids: string[]; data: Record<string, unknown> }>;
updateMany: Array<{
ids: string[];
tenantId: string;
status?: OutboxRequeueStatus[];
deliveryGeneration?: number;
data: Record<string, unknown>;
}>;
}
function fakeClient(state: FakeState): OutboxRequeueClient {
return {
outboxEvent: {
async findUnique({ where }) {
return state.byId.get(where.id) ?? null;
async findFirst({ where }) {
const row = state.byId.get(where.id);
return row?.tenantId === where.tenantId ? row : null;
},
async update({ where, data }) {
const row = state.byId.get(where.id);
if (!row) throw new Error(`no row ${where.id}`);
const next: OutboxRequeueEventRow = { ...row, status: data.status, retries: data.retries };
const next: OutboxRequeueEventRow = {
...row,
status: data.status,
retries: data.retries,
deliveryGeneration: row.deliveryGeneration + data.deliveryGeneration.increment,
};
state.byId.set(where.id, next);
state.updates.push({ id: where.id, data: data as unknown as Record<string, unknown> });
return next;
},
async findMany({ where, take }) {
const all = Array.from(state.byId.values())
.filter((r) => r.eventType === where.eventType && r.createdAt >= where.createdAt.gte && where.status.in.includes(r.status))
.filter(
(r) =>
r.tenantId === where.tenantId &&
r.eventType === where.eventType &&
r.createdAt >= where.createdAt.gte &&
where.status.in.includes(r.status)
)
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
.slice(0, take);
return all;
@@ -48,11 +78,25 @@ function fakeClient(state: FakeState): OutboxRequeueClient {
for (const id of where.id.in) {
const row = state.byId.get(id);
if (row) {
state.byId.set(id, { ...row, status: data.status, retries: data.retries });
if (row.tenantId !== where.tenantId) continue;
if (where.status && !where.status.in.includes(row.status)) continue;
if (where.deliveryGeneration !== undefined && row.deliveryGeneration !== where.deliveryGeneration) continue;
state.byId.set(id, {
...row,
status: data.status,
retries: data.retries,
deliveryGeneration: row.deliveryGeneration + data.deliveryGeneration.increment,
});
count += 1;
}
}
state.updateMany.push({ ids: where.id.in, data: data as unknown as Record<string, unknown> });
state.updateMany.push({
ids: where.id.in,
tenantId: where.tenantId,
...(where.status ? { status: where.status.in } : {}),
...(where.deliveryGeneration !== undefined ? { deliveryGeneration: where.deliveryGeneration } : {}),
data: data as unknown as Record<string, unknown>,
});
return { count };
},
},
@@ -88,6 +132,7 @@ function row(id: string, overrides: Partial<OutboxRequeueEventRow> = {}): Outbox
status: "FAILED",
tenantId: "tenant-1",
retries: 3,
deliveryGeneration: 0,
createdAt: new Date("2026-05-20T08:00:00Z"),
...overrides,
};
@@ -101,13 +146,24 @@ describe("runOutboxRequeueById", () => {
updateMany: [],
};
const output = makeOutput();
const outcome = await runOutboxRequeueById({ eventId: "evt-1", now: new Date("2026-05-25T00:00:00Z") }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueById(
{ tenantId: " tenant-1 ", eventId: "evt-1", now: new Date("2026-05-25T00:00:00Z") },
{ client: fakeClient(state), output }
);
expect(outcome.exitCode).toBe(0);
expect(outcome.status).toBe("requeued");
expect(state.updates).toHaveLength(1);
const update = state.updates[0]!.data as { status: OutboxRequeueStatus; retries: number; lastError: string | null };
expect(state.updateMany).toHaveLength(1);
const update = state.updateMany[0]!.data as {
status: OutboxRequeueStatus;
retries: number;
deliveryGeneration: { increment: number };
lastError: string | null;
};
expect(update.status).toBe("PENDING");
expect(update.retries).toBe(0);
expect(update.deliveryGeneration).toEqual({ increment: 1 });
expect(state.byId.get("evt-1")?.deliveryGeneration).toBe(1);
expect(state.updateMany[0]).toMatchObject({ tenantId: "tenant-1", status: ["FAILED"], deliveryGeneration: 0 });
expect(update.lastError).toBeNull();
expect(output.logs.some((m) => m.includes("re-queued"))).toBe(true);
});
@@ -119,7 +175,7 @@ describe("runOutboxRequeueById", () => {
updateMany: [],
};
const output = makeOutput();
await runOutboxRequeueById({ eventId: "evt-2", json: true }, { client: fakeClient(state), output });
await runOutboxRequeueById({ tenantId: "tenant-1", eventId: "evt-2", json: true }, { client: fakeClient(state), output });
expect(output.jsons[0]).toEqual({ ok: true, eventId: "evt-2", previousStatus: "FAILED" });
expect(output.logs).toEqual([]);
});
@@ -131,7 +187,7 @@ describe("runOutboxRequeueById", () => {
updateMany: [],
};
const output = makeOutput();
const outcome = await runOutboxRequeueById({ eventId: "evt-3" }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueById({ tenantId: "tenant-1", eventId: "evt-3" }, { client: fakeClient(state), output });
expect(outcome.exitCode).toBe(1);
expect(outcome.status).toBe("refused");
expect(state.updates).toEqual([]);
@@ -145,28 +201,29 @@ describe("runOutboxRequeueById", () => {
updateMany: [],
};
const output = makeOutput();
const outcome = await runOutboxRequeueById({ eventId: "evt-4" }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueById({ tenantId: "tenant-1", eventId: "evt-4" }, { client: fakeClient(state), output });
expect(outcome.exitCode).toBe(1);
expect(state.updates).toEqual([]);
});
it("re-queues DONE events but warns the operator", async () => {
it("refuses DONE events because enqueue does not prove consumer completion", async () => {
const state: FakeState = {
byId: new Map([["evt-5", row("evt-5", { status: "DONE", retries: 0 })]]),
updates: [],
updateMany: [],
};
const output = makeOutput();
const outcome = await runOutboxRequeueById({ eventId: "evt-5" }, { client: fakeClient(state), output });
expect(outcome.exitCode).toBe(0);
expect(state.updates).toHaveLength(1);
expect(output.warns[0]).toMatch(/will replay/);
const outcome = await runOutboxRequeueById({ tenantId: "tenant-1", eventId: "evt-5" }, { client: fakeClient(state), output });
expect(outcome.exitCode).toBe(1);
expect(outcome.status).toBe("refused");
expect(state.updateMany).toHaveLength(0);
expect(output.errors[0]).toMatch(/double-deliver/);
});
it("returns not_found when the event does not exist", async () => {
const state: FakeState = { byId: new Map(), updates: [], updateMany: [] };
const output = makeOutput();
const outcome = await runOutboxRequeueById({ eventId: "ghost" }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueById({ tenantId: "tenant-1", eventId: "ghost" }, { client: fakeClient(state), output });
expect(outcome.exitCode).toBe(1);
expect(outcome.status).toBe("not_found");
expect(output.errors[0]).toMatch(/not found/);
@@ -175,11 +232,29 @@ describe("runOutboxRequeueById", () => {
it("emits JSON for not_found lookups when --json is set", async () => {
const state: FakeState = { byId: new Map(), updates: [], updateMany: [] };
const output = makeOutput();
const outcome = await runOutboxRequeueById({ eventId: "ghost", json: true }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueById({ tenantId: "tenant-1", eventId: "ghost", json: true }, { client: fakeClient(state), output });
expect(outcome.status).toBe("not_found");
expect(output.jsons[0]).toEqual({ ok: false, reason: "not_found", eventId: "ghost" });
expect(output.errors).toEqual([]);
});
it("does not reveal or update an event owned by another tenant", async () => {
const state: FakeState = {
byId: new Map([["evt-cross-tenant", row("evt-cross-tenant", { tenantId: "tenant-2" })]]),
updates: [],
updateMany: [],
};
const output = makeOutput();
const outcome = await runOutboxRequeueById(
{ tenantId: "tenant-1", eventId: "evt-cross-tenant" },
{ client: fakeClient(state), output }
);
expect(outcome.status).toBe("not_found");
expect(state.updateMany).toEqual([]);
expect(state.byId.get("evt-cross-tenant")?.status).toBe("FAILED");
});
});
describe("runOutboxRequeueBulk", () => {
@@ -192,6 +267,7 @@ describe("runOutboxRequeueBulk", () => {
["d", row("d", { status: "PENDING", createdAt: new Date("2026-05-22T00:00:00Z") })],
["e", row("e", { status: "FAILED", createdAt: new Date("2026-05-19T00:00:00Z") })],
["f", row("f", { eventType: "billing.subscription.cancelled", status: "FAILED", createdAt: new Date("2026-05-22T00:00:00Z") })],
["g", row("g", { tenantId: "tenant-2", status: "FAILED", createdAt: new Date("2026-05-22T00:00:00Z") })],
]),
updates: [],
updateMany: [],
@@ -201,7 +277,10 @@ describe("runOutboxRequeueBulk", () => {
it("previews matched count without writing when --apply is omitted", async () => {
const state = bulkState();
const output = makeOutput();
const outcome = await runOutboxRequeueBulk({ type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z" }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueBulk(
{ tenantId: " tenant-1 ", type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z" },
{ client: fakeClient(state), output }
);
expect(outcome.exitCode).toBe(0);
expect(outcome.status).toBe("previewed");
expect(outcome.matched).toBe(2);
@@ -214,35 +293,47 @@ describe("runOutboxRequeueBulk", () => {
const state = bulkState();
const output = makeOutput();
const outcome = await runOutboxRequeueBulk(
{ type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z", apply: true, now: new Date("2026-05-25T00:00:00Z") },
{ tenantId: "tenant-1", type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z", apply: true, now: new Date("2026-05-25T00:00:00Z") },
{ client: fakeClient(state), output }
);
expect(outcome.exitCode).toBe(0);
expect(outcome.status).toBe("requeued");
expect(outcome.applied).toBe(2);
expect(state.updateMany).toHaveLength(1);
expect(state.updateMany).toHaveLength(2);
expect(state.updateMany).toEqual(
expect.arrayContaining([
expect.objectContaining({ tenantId: "tenant-1", status: ["FAILED"], deliveryGeneration: 0 }),
])
);
// FAILED rows a + b should have flipped to PENDING.
expect(state.byId.get("a")?.status).toBe("PENDING");
expect(state.byId.get("b")?.status).toBe("PENDING");
expect(state.byId.get("a")?.deliveryGeneration).toBe(1);
expect(state.byId.get("b")?.deliveryGeneration).toBe(1);
expect(state.byId.get("c")?.status).toBe("DONE");
expect(state.byId.get("d")?.status).toBe("PENDING"); // already pending, untouched
expect(state.byId.get("g")?.status).toBe("FAILED"); // same event type, different tenant
});
it("includes DONE events when --include-done is set", async () => {
it("rejects --include-done until consumer completion receipts exist", async () => {
const state = bulkState();
const output = makeOutput();
const outcome = await runOutboxRequeueBulk(
{ type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z", apply: true, includeDone: true },
{ tenantId: "tenant-1", type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z", apply: true, includeDone: true },
{ client: fakeClient(state), output }
);
expect(outcome.applied).toBe(3);
expect(state.byId.get("c")?.status).toBe("PENDING");
expect(outcome.exitCode).toBe(1);
expect(outcome.status).toBe("invalid_args");
expect(state.byId.get("c")?.status).toBe("DONE");
});
it("rejects malformed event type", async () => {
const state = bulkState();
const output = makeOutput();
const outcome = await runOutboxRequeueBulk({ type: "notavalidtype", since: "2026-05-20T00:00:00Z" }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueBulk(
{ tenantId: "tenant-1", type: "notavalidtype", since: "2026-05-20T00:00:00Z" },
{ client: fakeClient(state), output }
);
expect(outcome.exitCode).toBe(1);
expect(outcome.status).toBe("invalid_args");
expect(output.errors[0]).toMatch(/must match/);
@@ -251,7 +342,10 @@ describe("runOutboxRequeueBulk", () => {
it("rejects malformed --since", async () => {
const state = bulkState();
const output = makeOutput();
const outcome = await runOutboxRequeueBulk({ type: "billing.subscription.ended", since: "not-a-date" }, { client: fakeClient(state), output });
const outcome = await runOutboxRequeueBulk(
{ tenantId: "tenant-1", type: "billing.subscription.ended", since: "not-a-date" },
{ client: fakeClient(state), output }
);
expect(outcome.exitCode).toBe(1);
expect(outcome.status).toBe("invalid_args");
expect(output.errors[0]).toMatch(/ISO-8601/);
@@ -261,7 +355,7 @@ describe("runOutboxRequeueBulk", () => {
const state = bulkState();
const output = makeOutput();
await runOutboxRequeueBulk(
{ type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z", apply: true, json: true },
{ tenantId: "tenant-1", type: "billing.subscription.ended", since: "2026-05-20T00:00:00Z", apply: true, json: true },
{ client: fakeClient(state), output }
);
expect(output.jsons[0]).toMatchObject({ ok: true, applied: 2, matched: 2, eventType: "billing.subscription.ended" });
@@ -280,7 +374,10 @@ describe("runOutboxRequeueBulk", () => {
};
const output = makeOutput();
await runOutboxRequeueBulk({ type: "billing.subscription.ended", since: "2026-05-01T00:00:00Z", json: true }, { client: fakeClient(state), output });
await runOutboxRequeueBulk(
{ tenantId: "tenant-1", type: "billing.subscription.ended", since: "2026-05-01T00:00:00Z", json: true },
{ client: fakeClient(state), output }
);
expect(output.jsons[0]).toMatchObject({ ok: true, applied: 0, matched: 12 });
expect((output.jsons[0] as { preview: unknown[] }).preview).toHaveLength(10);
@@ -291,7 +388,7 @@ describe("runOutboxRequeueBulk", () => {
const state = bulkState();
const output = makeOutput();
const outcome = await runOutboxRequeueBulk(
{ type: "billing.subscription.ended", since: "2030-01-01T00:00:00Z", apply: true },
{ tenantId: "tenant-1", type: "billing.subscription.ended", since: "2030-01-01T00:00:00Z", apply: true },
{ client: fakeClient(state), output }
);
expect(outcome.exitCode).toBe(0);
@@ -316,7 +413,7 @@ describe("OutboxRequeueClient narrowness", () => {
// adjacent; we keep one runtime assertion to keep vitest happy.
const minimal: OutboxRequeueClient = {
outboxEvent: {
findUnique: vi.fn(async () => null),
findFirst: vi.fn(async () => null),
update: vi.fn(
async (): Promise<OutboxRequeueEventRow> => ({
id: "x",
@@ -324,6 +421,7 @@ describe("OutboxRequeueClient narrowness", () => {
status: "PENDING" as OutboxRequeueStatus,
tenantId: null,
retries: 0,
deliveryGeneration: 0,
createdAt: new Date(),
})
),