fix(api): harden broadcast transactions and estimate fallbacks
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { SystemRole } from "@capakraken/shared";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { notificationRouter } from "../router/notification.js";
|
||||
import {
|
||||
emitBroadcastSent,
|
||||
emitNotificationCreated,
|
||||
emitTaskAssigned,
|
||||
} from "../sse/event-bus.js";
|
||||
import { createCallerFactory } from "../trpc.js";
|
||||
|
||||
const { resolveRecipientsMock } = vi.hoisted(() => ({
|
||||
@@ -10,6 +15,10 @@ const { resolveRecipientsMock } = vi.hoisted(() => ({
|
||||
// Mock the SSE event bus — we don't test real event emission here
|
||||
vi.mock("../sse/event-bus.js", () => ({
|
||||
emitNotificationCreated: vi.fn(),
|
||||
emitTaskAssigned: vi.fn(),
|
||||
emitTaskCompleted: vi.fn(),
|
||||
emitTaskStatusChanged: vi.fn(),
|
||||
emitBroadcastSent: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../lib/notification-targeting.js", () => ({
|
||||
@@ -19,6 +28,7 @@ vi.mock("../lib/notification-targeting.js", () => ({
|
||||
const createCaller = createCallerFactory(notificationRouter);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
resolveRecipientsMock.mockReset();
|
||||
});
|
||||
|
||||
@@ -315,4 +325,119 @@ describe("notification.createBroadcast", () => {
|
||||
"user_mgr",
|
||||
);
|
||||
});
|
||||
|
||||
it("does not partially persist an immediate broadcast when recipient fan-out fails", async () => {
|
||||
resolveRecipientsMock.mockResolvedValue(["user_a", "user_b"]);
|
||||
|
||||
const txCreateBroadcast = vi.fn().mockResolvedValue({
|
||||
id: "broadcast_tx_1",
|
||||
title: "Ops update",
|
||||
createdAt: new Date("2026-03-30T10:00:00Z"),
|
||||
});
|
||||
const txUpdateBroadcast = vi.fn();
|
||||
const txCreateNotification = vi.fn()
|
||||
.mockResolvedValueOnce({ id: "notif_a", userId: "user_a" })
|
||||
.mockRejectedValueOnce(new Error("fan-out failed"));
|
||||
const tx = {
|
||||
notificationBroadcast: {
|
||||
create: txCreateBroadcast,
|
||||
update: txUpdateBroadcast,
|
||||
},
|
||||
notification: {
|
||||
create: txCreateNotification,
|
||||
},
|
||||
};
|
||||
const outerCreateBroadcast = vi.fn();
|
||||
const outerUpdateBroadcast = vi.fn();
|
||||
const outerCreateNotification = vi.fn();
|
||||
const transaction = vi.fn(async (callback: (db: typeof tx) => Promise<unknown>) => callback(tx));
|
||||
const db = {
|
||||
$transaction: transaction,
|
||||
notificationBroadcast: {
|
||||
create: outerCreateBroadcast,
|
||||
update: outerUpdateBroadcast,
|
||||
},
|
||||
notification: {
|
||||
create: outerCreateNotification,
|
||||
},
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
|
||||
await expect(caller.createBroadcast({
|
||||
title: "Ops update",
|
||||
targetType: "all",
|
||||
})).rejects.toThrow("fan-out failed");
|
||||
|
||||
expect(transaction).toHaveBeenCalledTimes(1);
|
||||
expect(txCreateBroadcast).toHaveBeenCalledTimes(1);
|
||||
expect(txCreateNotification).toHaveBeenCalledTimes(2);
|
||||
expect(txUpdateBroadcast).not.toHaveBeenCalled();
|
||||
expect(outerCreateBroadcast).not.toHaveBeenCalled();
|
||||
expect(outerUpdateBroadcast).not.toHaveBeenCalled();
|
||||
expect(outerCreateNotification).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rolls back an immediate broadcast when the final broadcast update fails", async () => {
|
||||
resolveRecipientsMock.mockResolvedValue(["user_a", "user_b"]);
|
||||
|
||||
const txCreateBroadcast = vi.fn().mockResolvedValue({
|
||||
id: "broadcast_tx_2",
|
||||
title: "Ops update",
|
||||
createdAt: new Date("2026-03-30T10:00:00Z"),
|
||||
});
|
||||
const txCreateNotification = vi.fn()
|
||||
.mockResolvedValueOnce({ id: "notif_a", userId: "user_a" })
|
||||
.mockResolvedValueOnce({ id: "notif_b", userId: "user_b" });
|
||||
const txUpdateBroadcast = vi.fn().mockRejectedValue(
|
||||
Object.assign(new Error("Record to update not found"), {
|
||||
code: "P2025",
|
||||
meta: { modelName: "NotificationBroadcast" },
|
||||
}),
|
||||
);
|
||||
const tx = {
|
||||
notificationBroadcast: {
|
||||
create: txCreateBroadcast,
|
||||
update: txUpdateBroadcast,
|
||||
},
|
||||
notification: {
|
||||
create: txCreateNotification,
|
||||
},
|
||||
};
|
||||
const outerCreateBroadcast = vi.fn();
|
||||
const outerUpdateBroadcast = vi.fn();
|
||||
const outerCreateNotification = vi.fn();
|
||||
const transaction = vi.fn(async (callback: (db: typeof tx) => Promise<unknown>) => callback(tx));
|
||||
const db = {
|
||||
$transaction: transaction,
|
||||
notificationBroadcast: {
|
||||
create: outerCreateBroadcast,
|
||||
update: outerUpdateBroadcast,
|
||||
},
|
||||
notification: {
|
||||
create: outerCreateNotification,
|
||||
},
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
|
||||
await expect(caller.createBroadcast({
|
||||
title: "Ops update",
|
||||
targetType: "all",
|
||||
})).rejects.toMatchObject({
|
||||
code: "NOT_FOUND",
|
||||
message: "Notification broadcast not found",
|
||||
});
|
||||
|
||||
expect(transaction).toHaveBeenCalledTimes(1);
|
||||
expect(txCreateBroadcast).toHaveBeenCalledTimes(1);
|
||||
expect(txCreateNotification).toHaveBeenCalledTimes(2);
|
||||
expect(txUpdateBroadcast).toHaveBeenCalledTimes(1);
|
||||
expect(outerCreateBroadcast).not.toHaveBeenCalled();
|
||||
expect(outerUpdateBroadcast).not.toHaveBeenCalled();
|
||||
expect(outerCreateNotification).not.toHaveBeenCalled();
|
||||
expect(emitNotificationCreated).not.toHaveBeenCalled();
|
||||
expect(emitTaskAssigned).not.toHaveBeenCalled();
|
||||
expect(emitBroadcastSent).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user