test(api): cover assistant broadcast sends

This commit is contained in:
2026-04-01 00:22:24 +02:00
parent 8bac169a5e
commit 1e569a9855
7 changed files with 733 additions and 0 deletions
@@ -0,0 +1,98 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import { executeTool } from "./assistant-tools-broadcast-send-test-helpers.js";
import { createToolContext } from "./assistant-tools-notification-test-helpers.js";
describe("assistant broadcast send recipient fan-out errors", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns a stable assistant error when broadcast recipient user is missing", async () => {
const ctx = createToolContext(
{
notificationBroadcast: {
create: vi.fn().mockResolvedValue({
id: "broadcast_missing_user",
title: "Office update",
targetType: "user",
}),
},
notification: {
create: vi.fn().mockRejectedValue({
code: "P2003",
message: "Foreign key constraint failed",
meta: { field_name: "Notification_userId_fkey" },
}),
},
},
SystemRole.MANAGER,
);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "user",
targetValue: "user_missing",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Broadcast recipient user not found with the given criteria.",
});
});
it("returns a stable assistant error when broadcast fan-out loses recipient rows inside the router transaction", async () => {
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(),
},
},
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.",
});
});
});
@@ -0,0 +1,163 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import { executeTool } from "./assistant-tools-broadcast-send-test-helpers.js";
import { createToolContext } from "./assistant-tools-notification-test-helpers.js";
describe("assistant broadcast send sender fan-out errors", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns a stable assistant error when broadcast sender user is missing", async () => {
const ctx = createToolContext(
{
notificationBroadcast: {
create: vi.fn().mockResolvedValue({
id: "broadcast_missing_sender",
title: "Office update",
targetType: "user",
}),
},
notification: {
create: vi.fn().mockRejectedValue({
code: "P2003",
message: "Foreign key constraint failed",
meta: { field_name: "Notification_senderId_fkey" },
}),
},
},
SystemRole.MANAGER,
);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "user",
targetValue: "user_2",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Sender user not found with the given criteria.",
});
});
it("returns a stable assistant error when broadcast fan-out loses sender 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(),
},
},
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.",
});
});
it("returns a stable assistant error when broadcast creation fails because the sender user is missing", async () => {
const ctx = createToolContext(
{
user: {
findMany: vi.fn().mockResolvedValue([{ id: "user_2" }]),
},
notificationBroadcast: {
create: vi.fn().mockRejectedValue(
Object.assign(new Error("Foreign key constraint failed"), {
code: "P2003",
meta: { field_name: "NotificationBroadcast_senderId_fkey" },
}),
),
},
},
SystemRole.MANAGER,
);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "all",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Sender user not found with the given criteria.",
});
});
it("returns a stable assistant error when scheduled broadcast creation loses the sender user", async () => {
const ctx = createToolContext(
{
user: {
findMany: vi.fn().mockResolvedValue([{ id: "user_2" }]),
},
notificationBroadcast: {
create: vi.fn().mockRejectedValue(
Object.assign(new Error("Foreign key constraint failed"), {
code: "P2003",
meta: { field_name: "NotificationBroadcast_senderId_fkey" },
}),
),
},
notification: {
create: vi.fn(),
},
},
SystemRole.MANAGER,
);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "all",
scheduledAt: "2099-04-10T08:00:00.000Z",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Sender user not found with the given criteria.",
});
expect(ctx.db.notification.create).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,113 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import { executeTool } from "./assistant-tools-broadcast-send-test-helpers.js";
import { createToolContext } from "./assistant-tools-notification-test-helpers.js";
describe("assistant broadcast send finalization errors", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns a stable assistant error when broadcast finalization loses the broadcast row", async () => {
const txCreateBroadcast = vi.fn().mockResolvedValue({
id: "broadcast_missing_after_create",
title: "Office update",
targetType: "all",
});
const txCreateNotification = vi.fn().mockResolvedValue({ id: "notification_2", userId: "user_2" });
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 ctx = createToolContext(
{
user: {
findMany: vi.fn().mockResolvedValue([{ id: "user_2" }]),
},
$transaction: vi.fn(async (callback: (db: typeof tx) => Promise<unknown>) => callback(tx)),
notificationBroadcast: {
create: vi.fn(),
update: vi.fn(),
},
notification: {
create: vi.fn(),
},
},
SystemRole.MANAGER,
);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "all",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Broadcast not found with the given criteria.",
});
});
it("returns a stable assistant error when broadcast finalization loses the row without prisma metadata", async () => {
const tx = {
notificationBroadcast: {
create: vi.fn().mockResolvedValue({
id: "broadcast_missing_after_create_generic",
title: "Office update",
targetType: "all",
}),
update: vi.fn().mockRejectedValue(
Object.assign(new Error("Record to update not found"), {
code: "P2025",
}),
),
},
notification: {
create: vi.fn().mockResolvedValue({ id: "notification_2", userId: "user_2" }),
},
};
const ctx = createToolContext(
{
user: {
findMany: vi.fn().mockResolvedValue([{ id: "user_2" }]),
},
$transaction: vi.fn(async (callback: (db: typeof tx) => Promise<unknown>) => callback(tx)),
notificationBroadcast: {
create: vi.fn(),
update: vi.fn(),
},
notification: {
create: vi.fn(),
},
},
SystemRole.MANAGER,
);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "all",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Broadcast not found with the given criteria.",
});
});
});
@@ -0,0 +1,137 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
vi.mock("@capakraken/application", async (importOriginal) => {
const actual = await importOriginal<typeof import("@capakraken/application")>();
return {
...actual,
approveEstimateVersion: vi.fn(),
cloneEstimate: vi.fn(),
commitDispoImportBatch: vi.fn(),
countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
createEstimateExport: vi.fn(),
createEstimatePlanningHandoff: vi.fn(),
createEstimateRevision: vi.fn(),
assessDispoImportReadiness: vi.fn(),
loadResourceDailyAvailabilityContexts: vi.fn().mockResolvedValue(new Map()),
getDashboardDemand: vi.fn().mockResolvedValue([]),
getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
getDashboardOverview: vi.fn(),
getDashboardSkillGapSummary: vi.fn().mockResolvedValue({
roleGaps: [],
totalOpenPositions: 0,
skillSupplyTop10: [],
resourcesByRole: [],
}),
getDashboardProjectHealth: vi.fn().mockResolvedValue([]),
getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
getDashboardTopValueResources: vi.fn().mockResolvedValue([]),
getEstimateById: vi.fn(),
listAssignmentBookings: vi.fn().mockResolvedValue([]),
stageDispoImportBatch: vi.fn(),
submitEstimateVersion: vi.fn(),
updateEstimateDraft: vi.fn(),
};
});
import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-notification-test-helpers.js";
describe("assistant broadcast send tools", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("creates and sends a broadcast through the notification router", async () => {
const tx = {
notificationBroadcast: {
create: vi.fn().mockResolvedValue({
id: "broadcast_1",
title: "Office update",
targetType: "user",
createdAt: new Date("2026-03-30T09:00:00.000Z"),
}),
update: vi.fn().mockResolvedValue({
id: "broadcast_1",
recipientCount: 1,
}),
},
notification: {
create: vi.fn().mockResolvedValue({ id: "notification_2", userId: "user_2" }),
},
};
const db = {
notificationBroadcast: {
create: vi.fn(),
update: vi.fn(),
},
notification: {
create: vi.fn(),
},
$transaction: vi.fn(async (callback: (db: typeof tx) => Promise<unknown>) => callback(tx)),
};
const ctx = createToolContext(db, SystemRole.ADMIN);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
body: "New schedule",
targetType: "user",
targetValue: "user_2",
category: "TASK",
priority: "HIGH",
channel: "in_app",
dueDate: "2026-04-04T10:00:00.000Z",
}),
ctx,
);
expect(tx.notificationBroadcast.create).toHaveBeenCalledWith({
data: expect.objectContaining({
senderId: "user_1",
title: "Office update",
body: "New schedule",
category: "TASK",
priority: "HIGH",
channel: "in_app",
targetType: "user",
targetValue: "user_2",
}),
});
expect(tx.notification.create).toHaveBeenCalledWith({
data: expect.objectContaining({
userId: "user_2",
type: "BROADCAST_TASK",
title: "Office update",
body: "New schedule",
category: "TASK",
priority: "HIGH",
channel: "in_app",
sourceId: "broadcast_1",
senderId: "user_1",
taskStatus: "OPEN",
dueDate: new Date("2026-04-04T10:00:00.000Z"),
}),
});
expect(tx.notificationBroadcast.update).toHaveBeenCalledWith({
where: { id: "broadcast_1" },
data: expect.objectContaining({
sentAt: expect.any(Date),
recipientCount: 1,
}),
});
expect(JSON.parse(result.content)).toEqual(
expect.objectContaining({
success: true,
broadcastId: "broadcast_1",
recipientCount: 1,
message: 'Broadcast "Office update" created.',
}),
);
expect(result.action).toEqual({
type: "invalidate",
scope: ["notification"],
});
});
});
@@ -0,0 +1,38 @@
import { vi } from "vitest";
vi.mock("@capakraken/application", async (importOriginal) => {
const actual = await importOriginal<typeof import("@capakraken/application")>();
return {
...actual,
approveEstimateVersion: vi.fn(),
cloneEstimate: vi.fn(),
commitDispoImportBatch: vi.fn(),
countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
createEstimateExport: vi.fn(),
createEstimatePlanningHandoff: vi.fn(),
createEstimateRevision: vi.fn(),
assessDispoImportReadiness: vi.fn(),
loadResourceDailyAvailabilityContexts: vi.fn().mockResolvedValue(new Map()),
getDashboardDemand: vi.fn().mockResolvedValue([]),
getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
getDashboardOverview: vi.fn(),
getDashboardSkillGapSummary: vi.fn().mockResolvedValue({
roleGaps: [],
totalOpenPositions: 0,
skillSupplyTop10: [],
resourcesByRole: [],
}),
getDashboardProjectHealth: vi.fn().mockResolvedValue([]),
getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
getDashboardTopValueResources: vi.fn().mockResolvedValue([]),
getEstimateById: vi.fn(),
listAssignmentBookings: vi.fn().mockResolvedValue([]),
stageDispoImportBatch: vi.fn(),
submitEstimateVersion: vi.fn(),
updateEstimateDraft: vi.fn(),
};
});
import { executeTool as executeAssistantTool } from "../router/assistant-tools.js";
export const executeTool = executeAssistantTool;
@@ -0,0 +1,80 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import { executeTool } from "./assistant-tools-broadcast-send-test-helpers.js";
import { createToolContext } from "./assistant-tools-notification-test-helpers.js";
describe("assistant broadcast send validation errors", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns a stable assistant error when broadcast scheduledAt is invalid", async () => {
const ctx = createToolContext({}, SystemRole.MANAGER);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "all",
scheduledAt: "not-a-datetime",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Invalid scheduledAt: not-a-datetime",
});
});
it("returns a stable assistant error when a broadcast target resolves to no recipients", async () => {
const create = vi.fn().mockResolvedValue({
id: "broadcast_empty",
title: "Office update",
targetType: "user",
});
const ctx = createToolContext(
{
notificationBroadcast: {
create,
},
},
SystemRole.MANAGER,
);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Office update",
targetType: "user",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "No recipients matched the broadcast target.",
});
expect(create).not.toHaveBeenCalled();
});
it("returns a stable assistant error for scheduled broadcasts with task metadata", async () => {
const ctx = createToolContext({}, SystemRole.MANAGER);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Approve later",
targetType: "all",
category: "TASK",
taskAction: "approve_vacation:vac_1",
dueDate: "2099-04-11T08:00:00.000Z",
scheduledAt: "2099-04-10T08:00:00.000Z",
}),
ctx,
);
expect(JSON.parse(result.content)).toEqual({
error: "Scheduled broadcasts with task metadata are not supported yet.",
});
});
});
@@ -0,0 +1,104 @@
import { describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
vi.mock("@capakraken/application", async (importOriginal) => {
const actual = await importOriginal<typeof import("@capakraken/application")>();
return {
...actual,
getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
listAssignmentBookings: vi.fn().mockResolvedValue([]),
};
});
import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-notification-test-helpers.js";
describe("assistant broadcast send tool", () => {
it("returns a stable assistant error when a scheduled broadcast resolves to no recipients", async () => {
const create = vi.fn();
const db = {
user: {
findMany: vi.fn().mockResolvedValue([]),
},
notificationBroadcast: {
create,
},
};
const ctx = createToolContext(db, SystemRole.MANAGER);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Holiday planning update",
targetType: "all",
scheduledAt: "2099-04-10T08:00:00.000Z",
}),
ctx,
);
expect(db.user.findMany).toHaveBeenCalledWith({
select: { id: true },
});
expect(create).not.toHaveBeenCalled();
expect(JSON.parse(result.content)).toEqual({
error: "No recipients matched the broadcast target.",
});
});
it("returns invalidate metadata and broadcast identifiers for scheduled broadcasts", async () => {
const scheduledAt = "2099-04-10T08:00:00.000Z";
const create = vi.fn().mockResolvedValue({
id: "broadcast_future",
title: "Planned update",
targetType: "all",
scheduledAt: new Date(scheduledAt),
});
const db = {
user: {
findMany: vi.fn().mockResolvedValue([{ id: "user_2" }, { id: "user_3" }]),
},
notificationBroadcast: {
create,
update: vi.fn(),
},
notification: {
create: vi.fn(),
},
};
const ctx = createToolContext(db, SystemRole.ADMIN);
const result = await executeTool(
"send_broadcast",
JSON.stringify({
title: "Planned update",
targetType: "all",
scheduledAt,
}),
ctx,
);
expect(db.user.findMany).toHaveBeenCalledWith({
select: { id: true },
});
expect(create).toHaveBeenCalledWith({
data: expect.objectContaining({
senderId: "user_1",
title: "Planned update",
targetType: "all",
scheduledAt: new Date(scheduledAt),
}),
});
expect(db.notification.create).not.toHaveBeenCalled();
expect(db.notificationBroadcast.update).not.toHaveBeenCalled();
expect(result.action).toEqual({ type: "invalidate", scope: ["notification"] });
expect(JSON.parse(result.content)).toEqual(
expect.objectContaining({
success: true,
broadcastId: "broadcast_future",
recipientCount: 0,
message: 'Broadcast "Planned update" created.',
}),
);
});
});