Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,167 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { createDbClient } from "./client.server.js";
|
||||
import {
|
||||
OUTBOX_CLAIM_DEFAULT_LIMIT,
|
||||
OUTBOX_CLAIM_LEASE_MS,
|
||||
claimPendingOutboxEvents,
|
||||
} from "./outbox-claim.js";
|
||||
|
||||
const DATABASE_URL = process.env.DATABASE_URL?.trim();
|
||||
// Vitest setup defaults DATABASE_URL to example.invalid so unit tests do
|
||||
// not need a live Postgres. Opt into the real Postgres integration suite
|
||||
// by exporting `OUTBOX_TEST_DATABASE_URL` or by pointing DATABASE_URL at
|
||||
// a real Postgres (anything that is not the vitest example.invalid stub).
|
||||
const integrationUrl =
|
||||
process.env.OUTBOX_TEST_DATABASE_URL?.trim() ||
|
||||
(DATABASE_URL && !DATABASE_URL.includes("example.invalid") ? DATABASE_URL : undefined);
|
||||
const skipIntegration = !integrationUrl;
|
||||
|
||||
const describeIntegration = skipIntegration ? describe.skip : describe;
|
||||
|
||||
describeIntegration("claimPendingOutboxEvents (integration)", () => {
|
||||
const prisma = createDbClient({ connectionString: integrationUrl });
|
||||
|
||||
beforeAll(async () => {
|
||||
await prisma.$connect();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma.outboxEvent.deleteMany({
|
||||
where: { eventType: { startsWith: "test.claim." } },
|
||||
});
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await prisma.outboxEvent.deleteMany({
|
||||
where: { eventType: { startsWith: "test.claim." } },
|
||||
});
|
||||
});
|
||||
|
||||
it("claims up to `limit` pending events and flips them to PROCESSING", async () => {
|
||||
const now = new Date("2026-05-17T08:00:00.000Z");
|
||||
const past = new Date(now.getTime() - 60 * 1000);
|
||||
|
||||
await prisma.outboxEvent.createMany({
|
||||
data: [
|
||||
{ eventType: "test.claim.alpha", payload: { i: 1 }, scheduledAt: past, priority: 0 },
|
||||
{ eventType: "test.claim.alpha", payload: { i: 2 }, scheduledAt: past, priority: 5 },
|
||||
{ eventType: "test.claim.alpha", payload: { i: 3 }, scheduledAt: past, priority: 5 },
|
||||
],
|
||||
});
|
||||
|
||||
const claimed = await claimPendingOutboxEvents(prisma, { limit: 2, now });
|
||||
|
||||
expect(claimed).toHaveLength(2);
|
||||
for (const event of claimed) {
|
||||
expect(event.status).toBe("PROCESSING");
|
||||
expect(event.lockedUntil).not.toBeNull();
|
||||
}
|
||||
// Highest priority is dispatched first.
|
||||
expect(claimed.map((event) => event.priority)).toEqual([5, 5]);
|
||||
});
|
||||
|
||||
it("skips events whose scheduledAt is in the future", async () => {
|
||||
const now = new Date("2026-05-17T08:00:00.000Z");
|
||||
const past = new Date(now.getTime() - 60 * 1000);
|
||||
const future = new Date(now.getTime() + 60 * 1000);
|
||||
|
||||
await prisma.outboxEvent.createMany({
|
||||
data: [
|
||||
{ eventType: "test.claim.scheduled", payload: { i: 1 }, scheduledAt: past },
|
||||
{ eventType: "test.claim.scheduled", payload: { i: 2 }, scheduledAt: future },
|
||||
],
|
||||
});
|
||||
|
||||
const claimed = await claimPendingOutboxEvents(prisma, { now });
|
||||
expect(claimed).toHaveLength(1);
|
||||
expect((claimed[0]?.payload as { i: number }).i).toBe(1);
|
||||
});
|
||||
|
||||
it("never claims the same event twice when two callers race", async () => {
|
||||
const now = new Date("2026-05-17T08:00:00.000Z");
|
||||
const past = new Date(now.getTime() - 60 * 1000);
|
||||
|
||||
await prisma.outboxEvent.createMany({
|
||||
data: Array.from({ length: 20 }, (_, index) => ({
|
||||
eventType: "test.claim.race",
|
||||
payload: { i: index },
|
||||
scheduledAt: past,
|
||||
})),
|
||||
});
|
||||
|
||||
const [batchA, batchB] = await Promise.all([
|
||||
claimPendingOutboxEvents(prisma, { limit: 50, now }),
|
||||
claimPendingOutboxEvents(prisma, { limit: 50, now }),
|
||||
]);
|
||||
|
||||
const ids = new Set<string>();
|
||||
for (const event of [...batchA, ...batchB]) {
|
||||
expect(ids.has(event.id)).toBe(false);
|
||||
ids.add(event.id);
|
||||
}
|
||||
expect(ids.size).toBe(20);
|
||||
});
|
||||
|
||||
it("recovers events stuck in PROCESSING past their lease", async () => {
|
||||
const now = new Date("2026-05-17T08:00:00.000Z");
|
||||
const expired = new Date(now.getTime() - 60 * 1000);
|
||||
|
||||
const created = await prisma.outboxEvent.create({
|
||||
data: {
|
||||
eventType: "test.claim.recovery",
|
||||
payload: { i: 1 },
|
||||
status: "PROCESSING",
|
||||
lockedUntil: expired,
|
||||
},
|
||||
});
|
||||
|
||||
const claimed = await claimPendingOutboxEvents(prisma, { now });
|
||||
expect(claimed).toHaveLength(1);
|
||||
expect(claimed[0]?.id).toBe(created.id);
|
||||
});
|
||||
|
||||
it("does not touch FAILED or DONE rows", async () => {
|
||||
const now = new Date("2026-05-17T08:00:00.000Z");
|
||||
const past = new Date(now.getTime() - 60 * 1000);
|
||||
await prisma.outboxEvent.createMany({
|
||||
data: [
|
||||
{ eventType: "test.claim.terminal", payload: { i: 1 }, status: "FAILED", scheduledAt: past },
|
||||
{ eventType: "test.claim.terminal", payload: { i: 2 }, status: "DONE", scheduledAt: past },
|
||||
],
|
||||
});
|
||||
|
||||
const claimed = await claimPendingOutboxEvents(prisma, { now });
|
||||
expect(claimed).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("exposes documented constants", () => {
|
||||
expect(OUTBOX_CLAIM_DEFAULT_LIMIT).toBe(100);
|
||||
expect(OUTBOX_CLAIM_LEASE_MS).toBe(5 * 60 * 1000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("claimPendingOutboxEvents (unit fallback)", () => {
|
||||
it("returns empty array when $queryRaw returns non-array", async () => {
|
||||
const fakePrisma = {
|
||||
$queryRaw: (async () => null) as unknown as import("./outbox-claim.js").OutboxClaimRunner["$queryRaw"],
|
||||
};
|
||||
const claimed = await claimPendingOutboxEvents(fakePrisma);
|
||||
expect(claimed).toEqual([]);
|
||||
});
|
||||
|
||||
it("clamps limit to the configured bounds", async () => {
|
||||
const captured: Array<unknown> = [];
|
||||
const fakePrisma = {
|
||||
$queryRaw: (async (sql: unknown) => {
|
||||
captured.push(sql);
|
||||
return [];
|
||||
}) as unknown as import("./outbox-claim.js").OutboxClaimRunner["$queryRaw"],
|
||||
};
|
||||
await claimPendingOutboxEvents(fakePrisma, { limit: 999_999 });
|
||||
await claimPendingOutboxEvents(fakePrisma, { limit: -5 });
|
||||
await claimPendingOutboxEvents(fakePrisma, { limit: Number.NaN });
|
||||
expect(captured).toHaveLength(3);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user