Files
CapaKraken/packages/api/src/__tests__/assistant-tools-comments-create-resolve.test.ts
T

116 lines
3.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import { createToolContext, executeTool } from "./assistant-tools-comments-test-helpers.js";
describe("assistant comment mutation tools", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("routes comment creation and resolution through the real comment router path", async () => {
const commentFindUnique = vi.fn().mockResolvedValue({
id: "comment_1",
authorId: "user_1",
entityType: "estimate",
entityId: "est_1",
});
const db = {
estimate: {
findUnique: vi.fn().mockResolvedValue({ id: "est_1" }),
},
comment: {
findUnique: commentFindUnique,
create: vi.fn().mockResolvedValue({
id: "comment_created",
body: "Please review this estimate.",
resolved: false,
createdAt: new Date("2026-03-29T11:00:00.000Z"),
author: { id: "user_1", name: "Assistant User", email: "assistant@example.com", image: null },
}),
update: vi.fn().mockResolvedValue({
id: "comment_1",
body: "Initial note",
resolved: true,
createdAt: new Date("2026-03-29T09:00:00.000Z"),
author: { id: "user_1", name: "Assistant User", email: "assistant@example.com", image: null },
}),
},
notification: {
create: vi.fn(),
},
auditLog: {
create: vi.fn().mockResolvedValue({ id: "audit_1" }),
},
};
const ctx = createToolContext(db, { userRole: SystemRole.CONTROLLER });
const createResult = await executeTool(
"create_comment",
JSON.stringify({
entityType: "estimate",
entityId: "est_1",
body: "Please review this estimate.",
}),
ctx,
);
const resolveResult = await executeTool(
"resolve_comment",
JSON.stringify({ commentId: "comment_1", resolved: true }),
ctx,
);
expect(db.estimate.findUnique).toHaveBeenNthCalledWith(1, {
where: { id: "est_1" },
select: { id: true },
});
expect(db.estimate.findUnique).toHaveBeenNthCalledWith(2, {
where: { id: "est_1" },
select: { id: true },
});
expect(db.comment.create).toHaveBeenCalledWith({
data: {
entityType: "estimate",
entityId: "est_1",
authorId: "user_1",
body: "Please review this estimate.",
mentions: [],
},
include: {
author: { select: { id: true, name: true, email: true, image: true } },
},
});
expect(commentFindUnique).toHaveBeenCalledWith({
where: { id: "comment_1" },
select: { id: true, authorId: true, entityType: true, entityId: true },
});
expect(db.comment.update).toHaveBeenCalledWith({
where: { id: "comment_1" },
data: { resolved: true },
include: {
author: { select: { id: true, name: true, email: true, image: true } },
},
});
expect(JSON.parse(createResult.content)).toEqual({
id: "comment_created",
author: "Assistant User",
body: "Please review this estimate.",
createdAt: "2026-03-29T11:00:00.000Z",
});
expect(createResult.action).toEqual({
type: "invalidate",
scope: ["comment"],
});
expect(JSON.parse(resolveResult.content)).toEqual({
id: "comment_1",
resolved: true,
author: "Assistant User",
body: "Initial note",
});
expect(resolveResult.action).toEqual({
type: "invalidate",
scope: ["comment"],
});
});
});