test(api): cover notification and user edge cases

This commit is contained in:
2026-03-30 11:51:26 +02:00
parent 4c542d0015
commit 4ce8577824
2 changed files with 180 additions and 1 deletions
@@ -1,15 +1,27 @@
import { SystemRole } from "@capakraken/shared";
import { describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { notificationRouter } from "../router/notification.js";
import { createCallerFactory } from "../trpc.js";
const { resolveRecipientsMock } = vi.hoisted(() => ({
resolveRecipientsMock: vi.fn(),
}));
// Mock the SSE event bus — we don't test real event emission here
vi.mock("../sse/event-bus.js", () => ({
emitNotificationCreated: vi.fn(),
}));
vi.mock("../lib/notification-targeting.js", () => ({
resolveRecipients: resolveRecipientsMock,
}));
const createCaller = createCallerFactory(notificationRouter);
beforeEach(() => {
resolveRecipientsMock.mockReset();
});
// ── Caller factories ─────────────────────────────────────────────────────────
function createProtectedCaller(db: Record<string, unknown>) {
@@ -264,3 +276,49 @@ describe("notification.create", () => {
).rejects.toThrow();
});
});
// ─── createBroadcast ────────────────────────────────────────────────────────
describe("notification.createBroadcast", () => {
it("rejects broadcasts when no recipients match the target", async () => {
resolveRecipientsMock.mockResolvedValue([]);
const create = vi.fn().mockResolvedValue({
id: "broadcast_1",
title: "Ops update",
createdAt: new Date("2026-03-30T10:00:00Z"),
});
const update = vi.fn();
const db = {
notificationBroadcast: {
create,
update,
},
};
const caller = createManagerCaller(db);
await expect(caller.createBroadcast({
title: "Ops update",
targetType: "all",
})).rejects.toMatchObject({
code: "BAD_REQUEST",
message: "No recipients matched the broadcast target.",
});
expect(create).toHaveBeenCalledWith(expect.objectContaining({
data: expect.objectContaining({
senderId: "user_mgr",
title: "Ops update",
targetType: "all",
}),
}));
expect(update).not.toHaveBeenCalled();
expect(resolveRecipientsMock).toHaveBeenCalledWith(
"all",
undefined,
db,
"user_mgr",
);
});
});