Files
reverse-engineering-anything/tests/boundary/cli/contractJsonInputChecks.test.ts
T
morluto 26d6cf39e6 test(cli): consolidate JSON admission and status checks (#11898)
* test(cli): consolidate JSON admission and status checks

* test(cli): retain delimiter-led paths in browser validation
2026-11-18 07:45:34 +08:00

65 lines
2.3 KiB
TypeScript

import { cliCommand } from "../../../src/cli/commandRegistry.js";
import { createCommandCli } from "../../support/cli/commandCli.js";
import { expect, it } from "vitest";
import { z } from "zod";
import { registerContractCommand } from "../../../src/cli/contractCommand.js";
import type { CliInstance } from "../../../src/cli/types.js";
import { ok } from "../../../src/domain/result.js";
import { silentLogger } from "../../../src/logger.js";
it("admits a checked JSON input through its check only, and the rest through the contract", async () => {
const contract = {
name: "compare_checked_fixture",
inputSchema: z.strictObject({
document: z.strictObject({ checked: z.literal(true) }),
max_age_ms: z.int().nonnegative().optional(),
}),
};
const received: unknown[] = [];
const cli = createCommandCli();
registerContractCommand(
cli as unknown as CliInstance,
silentLogger,
contract,
{
command: "compare-checked-fixture",
description: "Checked fixture",
positionals: ["document"],
jsonInputs: { document: "document must be JSON" },
jsonInputChecks: {
document: (value) => ok({ checked: value }),
},
},
async (_input, { checked, parsed }) => {
received.push({ checked, parsed });
return {};
},
);
const command = cliCommand(cli, "compare-checked-fixture")?.definition;
if (command === undefined) throw new Error("command is not registered");
const parse = (key: "args" | "options", value: unknown) =>
(Reflect.get(command, key) as { parse(v: unknown): unknown }).parse(value);
const run = (options: unknown) =>
(Reflect.get(command, "run") as (value: unknown) => Promise<unknown>)({
args: parse("args", { document: '{"records":[1,2,3]}' }),
options,
});
// The command accepts this document through its own check. The base field
// schema would reject the raw document.
await run(parse("options", { maxAgeMs: 5 }));
expect(received).toEqual([
{
checked: { document: { checked: { records: [1, 2, 3] } } },
parsed: { document: { records: [1, 2, 3] }, max_age_ms: 5 },
},
]);
// The contract still admits the remaining fields, past argv parsing.
expect(await run({ maxAgeMs: -1 })).toMatchObject({
code: "invalid_request",
details: { issues: [{ path: ["maxAgeMs"] }] },
});
});