test(api): cover remaining timeline and broadcast fallback races

This commit is contained in:
2026-03-30 12:23:46 +02:00
parent a9a01e8df0
commit 732538857b
2 changed files with 449 additions and 0 deletions
@@ -2169,6 +2169,98 @@ describe("assistant import/export and dispo tools", () => {
});
});
it("returns stable assistant errors when broadcast fan-out loses sender or recipient rows inside the router transaction", async () => {
const senderMissingTx = {
notificationBroadcast: {
create: vi.fn().mockRejectedValue(
Object.assign(new Error("Foreign key constraint failed"), {
code: "P2003",
meta: { field_name: "NotificationBroadcast_senderId_fkey" },
}),
),
update: vi.fn(),
},
notification: {
create: vi.fn(),
},
};
const senderMissingCtx = createToolContext(
{
user: {
findMany: vi.fn().mockResolvedValue([{ id: "user_2" }]),
},
$transaction: vi.fn(async (callback: (db: typeof senderMissingTx) => Promise<unknown>) => callback(senderMissingTx)),
notificationBroadcast: {
create: vi.fn(),
update: vi.fn(),
},
notification: {
create: vi.fn(),
},
},
{ userRole: SystemRole.MANAGER },
);
const senderMissingResult = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "all",
}),
senderMissingCtx,
);
expect(JSON.parse(senderMissingResult.content)).toEqual({
error: "Sender user not found with the given criteria.",
});
const recipientMissingTx = {
notificationBroadcast: {
create: vi.fn().mockResolvedValue({
id: "broadcast_missing_recipient",
title: "Office update",
targetType: "all",
}),
update: vi.fn(),
},
notification: {
create: vi.fn().mockRejectedValue(
Object.assign(new Error("Foreign key constraint failed"), {
code: "P2003",
meta: { field_name: "Notification_userId_fkey" },
}),
),
},
};
const recipientMissingCtx = createToolContext(
{
user: {
findMany: vi.fn().mockResolvedValue([{ id: "user_2" }]),
},
$transaction: vi.fn(async (callback: (db: typeof recipientMissingTx) => Promise<unknown>) => callback(recipientMissingTx)),
notificationBroadcast: {
create: vi.fn(),
update: vi.fn(),
},
notification: {
create: vi.fn(),
},
},
{ userRole: SystemRole.MANAGER },
);
const recipientMissingResult = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "all",
}),
recipientMissingCtx,
);
expect(JSON.parse(recipientMissingResult.content)).toEqual({
error: "Broadcast recipient user not found with the given criteria.",
});
});
it("returns a stable assistant error when a broadcast target resolves to no recipients", async () => {
const create = vi.fn().mockResolvedValue({
id: "broadcast_empty",