mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 14:35:45 +00:00
test(cli): consolidate numeric and flag admission checks (#12933)
This commit is contained in:
1 parent
82cb3d040b
commit
48e1af5990
3 files changed
+27
-29
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { strictEqual } from "node:assert/strict";
|
||||
import { Ajv2020 } from "ajv/dist/2020.js";
|
||||
import { z } from "zod";
|
||||
import { expect, it } from "vitest";
|
||||
@@ -21,6 +22,8 @@ it("keeps advertised decimal bounds aligned across decimal-width transitions", (
|
||||
const validate = ajv.compile(z.toJSONSchema(field));
|
||||
const candidates = new Set([
|
||||
...Array.from({ length: 1001 }, (_, index) => index),
|
||||
9_999,
|
||||
10_000,
|
||||
minimum - 1,
|
||||
minimum,
|
||||
minimum + 1,
|
||||
@@ -30,14 +33,25 @@ it("keeps advertised decimal bounds aligned across decimal-width transitions", (
|
||||
]);
|
||||
for (const value of candidates) {
|
||||
if (value < 0) continue;
|
||||
const admitted = minimum <= value && value <= maximum;
|
||||
for (const token of [String(value), `000${value}`]) {
|
||||
expect(validate(token), `${minimum}..${maximum}: ${token}`).toBe(
|
||||
const context = `${minimum}..${maximum}: ${token}`;
|
||||
strictEqual(validate(token), admitted, `Schema ${context}`);
|
||||
strictEqual(
|
||||
field.safeParse(token).success,
|
||||
admitted,
|
||||
`Parser ${context}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const token of ["", "1e2", "0x10", "+1", "1.0", "1\n", " 1"])
|
||||
expect(validate(token), token).toBe(false);
|
||||
}
|
||||
// Keep every numeric spelling within the chosen range, so its syntax is
|
||||
// what causes refusal.
|
||||
const decimal = cliDecimalField(z.number().int().min(0).max(256));
|
||||
const validate = ajv.compile(z.toJSONSchema(decimal));
|
||||
for (const token of ["", "1e2", "0x10", "+1", "1.0", "1\n", " 1"]) {
|
||||
expect(validate(token), token).toBe(false);
|
||||
expect(decimal.safeParse(token).success, token).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -68,11 +82,10 @@ it("retains exclusive bounds, wrappers, and parser-only refinements", () => {
|
||||
});
|
||||
|
||||
it("keeps a positive floor when the domain has no ceiling", () => {
|
||||
const ajv = new Ajv2020({ strict: false });
|
||||
for (const minimum of [1, 5, 10, 256]) {
|
||||
const field = cliDecimalField(z.number().min(minimum));
|
||||
const validate = new Ajv2020({ strict: false }).compile(
|
||||
z.toJSONSchema(field),
|
||||
);
|
||||
const validate = ajv.compile(z.toJSONSchema(field));
|
||||
for (const value of [
|
||||
minimum - 1,
|
||||
minimum,
|
||||
|
||||
@@ -467,6 +467,9 @@ it("keeps every argv fixture on a contract-built command", () => {
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
const commandsWithBooleanFlags = each.filter(([, { parameters }]) =>
|
||||
parameters.some(({ positional, booleanFlag }) => !positional && booleanFlag),
|
||||
);
|
||||
describe("CLI commands projected from tool contracts", () => {
|
||||
it.each(each)(
|
||||
"%s exposes every contract field or declares its omission",
|
||||
@@ -535,8 +538,6 @@ describe("CLI commands projected from tool contracts", () => {
|
||||
for (const parameter of description.parameters) {
|
||||
const cliField = registeredField(description, parameter);
|
||||
const contractField = suppliedField(description, parameter);
|
||||
// Incur's help reads flag and choice types only through `optional`.
|
||||
expect(cliField, parameter.key).not.toBeInstanceOf(z.ZodExactOptional);
|
||||
if (parameter.callerPath) {
|
||||
// Only the absolute-path rule is relaxed, and only by resolution.
|
||||
const spelled = (path: string) =>
|
||||
@@ -578,7 +579,7 @@ describe("CLI commands projected from tool contracts", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.each(each)(
|
||||
it.each(commandsWithBooleanFlags)(
|
||||
"%s takes each boolean contract field as a bare flag",
|
||||
(_command, description) => {
|
||||
for (const parameter of description.parameters) {
|
||||
|
||||
@@ -164,17 +164,6 @@ const contractShape = (
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/** One CLI token as incur passes it: a single argv string or the value. */
|
||||
const cliAdmittedValue = (
|
||||
field: z.ZodType,
|
||||
value: unknown,
|
||||
): { readonly admitted: boolean; readonly value?: unknown } => {
|
||||
const parsed = field.safeParse(value);
|
||||
return parsed.success
|
||||
? { admitted: true, value: parsed.data }
|
||||
: { admitted: false };
|
||||
};
|
||||
|
||||
const collectParity = () => {
|
||||
const cli = createCli();
|
||||
const commands = cliCommands(cli);
|
||||
@@ -185,7 +174,6 @@ const collectParity = () => {
|
||||
const uncomparable: string[] = [];
|
||||
let compared = 0;
|
||||
let commandsCompared = 0;
|
||||
let unmatchedFields = 0;
|
||||
for (const command of commands) {
|
||||
const { name, definition: entry } = command;
|
||||
const ownOperations = commandOwnOperations(command);
|
||||
@@ -213,17 +201,14 @@ const collectParity = () => {
|
||||
: {};
|
||||
for (const [fieldName, cliField] of Object.entries(fields)) {
|
||||
const contractField = shape[snake(fieldName)];
|
||||
if (contractField === undefined) {
|
||||
unmatchedFields += 1;
|
||||
continue;
|
||||
}
|
||||
if (contractField === undefined) continue;
|
||||
for (const probe of probesFor(contractField)) {
|
||||
compared += 1;
|
||||
const cliValue = cliAdmittedValue(cliField, probe.value);
|
||||
const cliValue = cliField.safeParse(probe.value);
|
||||
const mismatch =
|
||||
probe.expect === "resolved"
|
||||
? !cliValue.admitted
|
||||
: cliValue.admitted && !admits(contractField, cliValue.value);
|
||||
? !cliValue.success
|
||||
: cliValue.success && !admits(contractField, cliValue.data);
|
||||
if (mismatch)
|
||||
mismatches.push(`${name} ${group}.${fieldName} ${probe.id}`);
|
||||
}
|
||||
@@ -235,7 +220,6 @@ const collectParity = () => {
|
||||
mismatches: [...new Set(mismatches)].sort(),
|
||||
compared,
|
||||
commandsCompared,
|
||||
unmatchedFields,
|
||||
uncomparable,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user