fix: 完成消息 Outbox 投递与会话状态保护

This commit is contained in:
root
2026-09-16 13:41:59 +08:00
parent 13b8b9ded8
commit 8a12c04104
9 changed files with 621 additions and 46 deletions
+36
View File
@@ -39,6 +39,42 @@ describe("moderation worker with real PostgreSQL", () => {
},
);
it.each([
["PENDING", null, 0],
["PROCESSING", new Date(Date.now() - 60_000), 5],
] as const)(
"leaves %s MESSAGE_CREATED for the API relay",
async (status, lockedAt, attempts) => {
await prisma.account.create({
data: {
phoneCiphertext: Buffer.from("cipher"),
phoneHmac: randomUUID(),
},
});
const event = await prisma.outboxEvent.create({
data: {
aggregateType: "MESSAGE",
aggregateId: randomUUID(),
eventType: "MESSAGE_CREATED",
dedupeKey: randomUUID(),
payload: {
messageId: randomUUID(),
conversationId: randomUUID(),
},
status,
lockedAt,
lockToken: lockedAt ? randomUUID() : null,
attempts,
},
});
expect(await new ModerationWorker(prisma).runOnce()).toBe(false);
expect(
await prisma.outboxEvent.findUniqueOrThrow({ where: { id: event.id } }),
).toMatchObject({ status, attempts, lockedAt });
},
);
it("is fail-closed and exponentially reschedules failures", async () => {
const { bottle, event } = await fixture("ordinary");
const worker = new ModerationWorker(prisma, () => {