import { describe, expect, test } from "bun:test"; import { CliError } from "../../../core/errors.js"; import { findSealCapability, parseCapabilityParams, type SealCapability } from "./index.js"; function tool(name: string): SealCapability { const found = findSealCapability(name); if (!found) throw new Error(`test fixture missing tool: ${name}`); return found; } /** Asserts rejection and returns the message so tests can check its wording. */ function rejectsWith(toolName: string, params: Record): string { try { parseCapabilityParams(tool(toolName), params); } catch (error) { expect(error).toBeInstanceOf(CliError); expect((error as CliError).exitCode).toBe(13); return (error as CliError).message; } throw new Error(`expected ${toolName} to reject ${JSON.stringify(params)}`); } describe("parseCapabilityParams", () => { test("accepts valid params and returns the exact object it was given", () => { const target = tool("seal_user_create"); const params = { name: "张三", phone: "13800001111", departmentIds: ["d1"] }; // Identity matters: handlers read keys the schema does not declare, so the // gate must pass its input through, never zod's (stripped) parse output. expect(parseCapabilityParams(target, params)).toBe(params); }); test("names the missing required field instead of letting the handler crash on undefined", () => { const message = rejectsWith("seal_user_create", { phone: "13800001111" }); expect(message).toContain("seal_user_create"); expect(message).toContain("name"); }); test("enforces the value bound a handler would otherwise silently accept", () => { // limit is capped at 100 in the schema; nothing in the handler checks it. const message = rejectsWith("seal_approval_runs_search", { limit: 9999 }); expect(message).toContain("limit"); }); test("coerces query-string values before checking, without touching the original", () => { const target = tool("seal_approval_runs_search"); const params = { limit: "50", includeBridge: "true" } as Record; // /v1/* passes url.searchParams through: everything arrives as a string, // but the schema declares z.number() / z.boolean(). Without coercion every // REST route would 400 on its own documented query params. expect(parseCapabilityParams(target, params)).toBe(params); expect(params.limit).toBe("50"); expect(params.includeBridge).toBe("true"); }); test("still rejects a string that merely looks like a wire value", () => { const message = rejectsWith("seal_approval_runs_search", { limit: "abc" }); expect(message).toContain("limit"); }); test("coerces a comma-separated query value into the array the schema wants", () => { // `?fields=id,code` on /v1/approval/rules: today the handler receives a // string and pickRuleFields iterates its characters, returning empty // objects. Validation splits it so the documented form actually works. expect(parseCapabilityParams(tool("seal_approval_rules_list"), { fields: "id,code" })).toEqual({ fields: "id,code" }); expect(rejectsWith("seal_approval_rules_list", { fields: "id,not-a-field" })).toContain("fields"); }); test("ignores undeclared keys rather than rejecting the request", () => { // z.object is non-strict: an extra HTTP body key is a compatibility // non-event, not an error. This gate checks declared params only. const target = tool("seal_approval_runs_search"); expect(parseCapabilityParams(target, { status: "TERMINATED", corp: "corp-a", stray: 1 })).toBeTypeOf("object"); }); test("lets the legacy credential aliases through to the handler that supports them", () => { // seal_deploy_hosecloud_configure has always accepted appKey/appSecurity // alongside apiKey/apiSecret; a schema that declared only apiKey as // required would have blocked that documented path. expect(() => parseCapabilityParams(tool("seal_deploy_hosecloud_configure"), { appKey: "key-1", appSecurity: "secret-1", mode: "assisted" }) ).not.toThrow(); }); });