MCP: пакетный acc_review_batch, компактная таблица очереди, интерактивное приложение (ui://) с кнопками Done/Delegate/Snooze, подсказки readOnly/destructive
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -217,6 +217,28 @@ d("MCP бухгалтерии (временная БД)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("пакетные действия", () => {
|
||||
it("валидация: пусто, больше 50, неоднозначный/короткий префикс; роли не обходятся", async () => {
|
||||
expect(await errOf(call(accountant, "acc_review_batch", { actions: [] }))).toContain("1..50");
|
||||
expect(await errOf(call(accountant, "acc_review_batch", { actions: Array.from({ length: 51 }, () => ({ id: "x", action: "done" })) }))).toContain("1..50");
|
||||
expect(await errOf(call(stranger, "acc_review_batch", { actions: [{ id: "abcdef12", action: "done" }] }))).toContain("Доступ запрещён");
|
||||
const t = await addCat("p1");
|
||||
const res = (await call(accountant, "acc_review_batch", { actions: [{ id: "abc", action: "done" }, { id: t, action: "handoff", role: "cfo" }, { id: t, action: "done" }] })) as { results: Array<Record<string, any>> };
|
||||
expect(res.results[0].ok).toBe(false);
|
||||
expect(res.results[1].ok).toBe(true);
|
||||
// после передачи cfo задача уже не закрывается accountant-ом
|
||||
expect(res.results[2].ok).toBe(false);
|
||||
expect(res.results[2].error).toContain("cfo");
|
||||
});
|
||||
|
||||
it("update в пакете меняет поля, как отдельный вызов", async () => {
|
||||
const t = await addCat("u1");
|
||||
const res = (await call(accountant, "acc_review_batch", { actions: [{ id: t, action: "update", fields: { confidence: 55 } }] })) as { ok_count: number };
|
||||
expect(res.ok_count).toBe(1);
|
||||
expect((await db.pool.query("select confidence from acc_categorization_review where feed_txn_id = 'u1'")).rows[0].confidence).toBe(55);
|
||||
});
|
||||
});
|
||||
|
||||
describe("HTTP", () => {
|
||||
const post = async (headers: Record<string, string>, payload: unknown, deps: Partial<McpDeps> = {}) => {
|
||||
const app = Fastify();
|
||||
@@ -246,8 +268,13 @@ d("MCP бухгалтерии (временная БД)", () => {
|
||||
expect(((await post(bearer(TOKEN_A), rpc("ping"))).json()).result).toEqual({});
|
||||
const l = (await post(bearer(TOKEN_A), rpc("tools/list"))).json();
|
||||
expect(l.result.tools.map((t: { name: string }) => t.name)).toEqual([
|
||||
"acc_review_list", "acc_review_get", "acc_review_update", "acc_review_done", "acc_review_handoff", "acc_review_return", "acc_review_snooze",
|
||||
"acc_review_list", "acc_review_get", "acc_review_update", "acc_review_done", "acc_review_handoff", "acc_review_return", "acc_review_batch", "acc_review_snooze",
|
||||
]);
|
||||
const tl = l.result.tools as Array<{ name: string; annotations?: { readOnlyHint?: boolean }; _meta?: { ui?: { resourceUri?: string } } }>;
|
||||
expect(tl.find((t) => t.name === "acc_review_list")?.annotations?.readOnlyHint).toBe(true);
|
||||
expect(tl.find((t) => t.name === "acc_review_done")?.annotations?.readOnlyHint).toBe(false);
|
||||
expect(tl.find((t) => t.name === "acc_review_list")?._meta?.ui?.resourceUri).toBe("ui://accounting_automation/review-table");
|
||||
expect(i.json().result.capabilities.resources).toBeDefined();
|
||||
expect((await post(bearer(TOKEN_A), rpc("nope"))).json().error.code).toBe(-32601);
|
||||
});
|
||||
|
||||
@@ -255,7 +282,9 @@ d("MCP бухгалтерии (временная БД)", () => {
|
||||
await addInvoice();
|
||||
const ok = (await post(bearer(TOKEN_A), rpc("tools/call", { name: "acc_review_list", arguments: {} }))).json();
|
||||
expect(ok.result.isError).toBeUndefined();
|
||||
expect(JSON.parse(ok.result.content[0].text).total).toBe(1);
|
||||
expect(ok.result.structuredContent.total).toBe(1);
|
||||
expect(ok.result.structuredContent.rows[0]).toMatchObject({ kind: "invoice", who: "Vendor A", role: "accountant" });
|
||||
expect(ok.result.content[0].text).toContain("| id | вид |");
|
||||
const denied = (await post(bearer("token-stranger-0123456789"), rpc("tools/call", { name: "acc_review_list", arguments: {} }))).json();
|
||||
expect(denied.result.isError).toBe(true);
|
||||
expect(denied.result.content[0].text).toContain("Доступ запрещён");
|
||||
@@ -263,13 +292,53 @@ d("MCP бухгалтерии (временная БД)", () => {
|
||||
expect(bad.result.isError).toBe(true);
|
||||
});
|
||||
|
||||
it("resources: интерактивная таблица очереди отдаётся как ui:// ресурс", async () => {
|
||||
const list = (await post(bearer(TOKEN_A), rpc("resources/list"))).json();
|
||||
expect(list.result.resources[0]).toMatchObject({ uri: "ui://accounting_automation/review-table", mimeType: "text/html;profile=mcp-app" });
|
||||
const read = (await post(bearer(TOKEN_A), rpc("resources/read", { uri: "ui://accounting_automation/review-table" }))).json();
|
||||
expect(read.result.contents[0].mimeType).toBe("text/html;profile=mcp-app");
|
||||
expect(read.result.contents[0].text).toContain("acc_review_batch");
|
||||
expect((await post(bearer(TOKEN_A), rpc("resources/read", { uri: "ui://nope" }))).json().error.code).toBe(-32002);
|
||||
expect((await post({}, rpc("resources/list"))).statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it("acc_review_batch через HTTP: пакет с частичной ошибкой, id по префиксу", async () => {
|
||||
const a = await addCat("b1");
|
||||
const b = await addCat("b2");
|
||||
const c = await addCat("b3");
|
||||
const r = (
|
||||
await post(
|
||||
bearer(TOKEN_A),
|
||||
rpc("tools/call", {
|
||||
name: "acc_review_batch",
|
||||
arguments: {
|
||||
actions: [
|
||||
{ id: a.slice(0, 8), action: "done" },
|
||||
{ id: b, action: "snooze", until: "2026-12-01" },
|
||||
{ id: c, action: "handoff", role: "cfo", comment: "сумма" },
|
||||
{ id: "00000000-0000-4000-8000-000000000000", action: "done" },
|
||||
{ id: b, action: "bogus" },
|
||||
],
|
||||
},
|
||||
}),
|
||||
)
|
||||
).json();
|
||||
expect(r.result.isError).toBeUndefined();
|
||||
expect(r.result.structuredContent.ok_count).toBe(3);
|
||||
expect(r.result.structuredContent.error_count).toBe(2);
|
||||
expect(r.result.content[0].text).toContain("ОШИБКА");
|
||||
expect((await db.pool.query("select done from acc_categorization_review where feed_txn_id = 'b1'")).rows[0].done).toBe(true);
|
||||
expect((await db.pool.query("select snoozed_until from acc_manual_review where id = $1", [b])).rows[0].snoozed_until).not.toBeNull();
|
||||
expect((await db.pool.query("select assignee_role from acc_manual_review where id = $1", [c])).rows[0].assignee_role).toBe("cfo");
|
||||
});
|
||||
|
||||
it("JWT Keycloak: person по основной и дополнительной почте; чужой email - 403; плохие токены - 401", async () => {
|
||||
await addInvoice();
|
||||
const call = { name: "acc_review_list", arguments: {} };
|
||||
for (const email of ["kc@khbuild.pro", "KC.Alias@gmail.com", "other@gmail.com"]) {
|
||||
const r = await post(bearer(await jwt({ email, email_verified: true })), rpc("tools/call", call));
|
||||
expect(r.statusCode, email).toBe(200);
|
||||
expect(JSON.parse(r.json().result.content[0].text).total).toBe(1);
|
||||
expect(r.json().result.structuredContent.total).toBe(1);
|
||||
}
|
||||
expect((await post(bearer(await jwt({ email: "nobody@x.com" })), rpc("tools/call", call))).statusCode).toBe(403);
|
||||
expect((await post(bearer(await jwt({ email: "off@khbuild.pro" })), rpc("tools/call", call))).statusCode).toBe(403);
|
||||
|
||||
Reference in New Issue
Block a user