fix(reconstruction): reject contradictory function comparison status (#2798)
* fix(reconstruction): reject contradictory function comparison status * test(reconstruction): cover imported function match precedence --------- Co-authored-by: maxazure <405711+maxazure@users.noreply.github.com>
This commit is contained in:
@@ -999,3 +999,51 @@ const expectFunctionDimensionAlgebra = (
|
||||
}).success,
|
||||
).toBe(false);
|
||||
};
|
||||
|
||||
it.each([
|
||||
{ match: "mismatched", changedDimensions: false, expected: "changed" },
|
||||
{ match: "ambiguous", changedDimensions: false, expected: "unknown" },
|
||||
{ match: "ambiguous", changedDimensions: true, expected: "unknown" },
|
||||
] as const)(
|
||||
"validates $match match precedence with changed dimensions=$changedDimensions",
|
||||
({ match, changedDimensions, expected }) => {
|
||||
const result = compareFunctions(
|
||||
FUNCTION_COMPARISON_EXAMPLE.left,
|
||||
changedDimensions
|
||||
? FUNCTION_COMPARISON_EXAMPLE.right
|
||||
: FUNCTION_COMPARISON_EXAMPLE.left,
|
||||
);
|
||||
const candidate = {
|
||||
...result,
|
||||
function_match: {
|
||||
status: match,
|
||||
method: "explicit",
|
||||
left_name: "main",
|
||||
right_name: "other",
|
||||
},
|
||||
};
|
||||
expect(
|
||||
functionComparisonResultSchema.safeParse({
|
||||
...candidate,
|
||||
status: expected,
|
||||
}).success,
|
||||
).toBe(true);
|
||||
for (const status of ["unchanged", "changed", "unknown"] as const) {
|
||||
if (status === expected) continue;
|
||||
const parsed = functionComparisonResultSchema.safeParse({
|
||||
...candidate,
|
||||
status,
|
||||
});
|
||||
expect(parsed.success).toBe(false);
|
||||
if (!parsed.success)
|
||||
expect(parsed.error.issues).toMatchObject([
|
||||
{
|
||||
path: ["status"],
|
||||
code: "custom",
|
||||
message:
|
||||
"Function comparison status contradicts its dimensions or function match.",
|
||||
},
|
||||
]);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { z } from "zod";
|
||||
import { overallStatus } from "./functionComparisonResults.js";
|
||||
import { evidenceSchema } from "./evidence.js";
|
||||
import { digestSchema } from "./../domain/digests.js";
|
||||
import { prefixedDigestSchema } from "./../domain/digests.js";
|
||||
@@ -79,20 +80,33 @@ const functionMatchSchema = z.union([
|
||||
]);
|
||||
|
||||
/** Deterministic dimension-classified function comparison Evidence payload. */
|
||||
export const functionComparisonResultSchema = z.object({
|
||||
status: comparisonStatusSchema,
|
||||
function_match: functionMatchSchema,
|
||||
left_subject_sha256: digestSchema,
|
||||
right_subject_sha256: digestSchema,
|
||||
summary: z.object({
|
||||
unchanged: z.number().int().min(0),
|
||||
changed: z.number().int().min(0),
|
||||
unknown: z.number().int().min(0),
|
||||
}),
|
||||
dimensions: z.array(functionDimensionSchema).length(8),
|
||||
changes: z.array(functionDimensionSchema),
|
||||
limitations: z.array(z.string()),
|
||||
});
|
||||
export const functionComparisonResultSchema = z
|
||||
.object({
|
||||
status: comparisonStatusSchema,
|
||||
function_match: functionMatchSchema,
|
||||
left_subject_sha256: digestSchema,
|
||||
right_subject_sha256: digestSchema,
|
||||
summary: z.object({
|
||||
unchanged: z.number().int().min(0),
|
||||
changed: z.number().int().min(0),
|
||||
unknown: z.number().int().min(0),
|
||||
}),
|
||||
dimensions: z.array(functionDimensionSchema).length(8),
|
||||
changes: z.array(functionDimensionSchema),
|
||||
limitations: z.array(z.string()),
|
||||
})
|
||||
.superRefine((result, context) => {
|
||||
if (
|
||||
result.status !==
|
||||
overallStatus(result.dimensions, result.function_match.status)
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["status"],
|
||||
message:
|
||||
"Function comparison status contradicts its dimensions or function match.",
|
||||
});
|
||||
});
|
||||
|
||||
export type FunctionComparisonResult = z.infer<
|
||||
typeof functionComparisonResultSchema
|
||||
|
||||
Reference in New Issue
Block a user