Files
seal-use/src/domains/seal/capabilities/validate.test.ts
T

97 lines
4.0 KiB
TypeScript

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, unknown>): 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 normalized values without mutating caller input", () => {
const target = tool("sealUserCreate");
const params = { name: "张三", phone: "13800001111", departmentIds: ["d1"] };
expect(parseCapabilityParams(target, params)).toEqual(params);
});
test("names the missing required field instead of letting the handler crash on undefined", () => {
const message = rejectsWith("sealUserCreate", { phone: "13800001111" });
expect(message).toContain("sealUserCreate");
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("sealApprovalRunsSearch", { limit: 9999 });
expect(message).toContain("limit");
});
test("coerces query-string values and returns the coerced types without mutating the original", () => {
const target = tool("sealApprovalRunsSearch");
const params = { limit: "50", includeBridge: "true" } as Record<string, unknown>;
// /v1/* passes url.searchParams through: everything arrives as a string,
// but the schema declares z.number() / z.boolean(). Returning the coerced
// values ensures the handler receives the exact types the schema validated.
expect(parseCapabilityParams(target, params)).toEqual({ limit: 50, includeBridge: true });
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("sealApprovalRunsSearch", { limit: "abc" });
expect(message).toContain("limit");
});
test("coerces a comma-separated query value into the array the schema and handler want", () => {
// `?fields=id,code` on /v1/approval/rules: validation splits the string and
// hands the array to the handler so pickRuleFields iterates field names,
// not individual characters.
expect(parseCapabilityParams(tool("sealApprovalRulesList"), { fields: "id,code" })).toEqual({
fields: ["id", "code"]
});
expect(rejectsWith("sealApprovalRulesList", { fields: "id,not-a-field" })).toContain("fields");
});
test("preserves undeclared compatibility keys alongside normalized schema fields", () => {
// z.object is non-strict: an extra HTTP body key or legacy alias is kept
// while declared fields are coerced and defaulted.
const target = tool("sealApprovalRunsSearch");
expect(parseCapabilityParams(target, { status: "TERMINATED", limit: "10", corp: "corp-a", stray: 1 })).toEqual({
status: "TERMINATED",
limit: 10,
corp: "corp-a",
stray: 1
});
});
test("lets the legacy credential aliases through to the handler and populates schema defaults", () => {
expect(
parseCapabilityParams(tool("sealDeployHosecloudConfigure"), {
appKey: "key-1",
appSecurity: "secret-1",
mode: "assisted"
})
).toEqual({
appKey: "key-1",
appSecurity: "secret-1",
mode: "assisted",
apiOrigin: "production"
});
});
});