refactor(domain): map validation taxonomies and add inventory Result seam (#833)
Document the three disjoint failure taxonomies (filesystem reader codes, session reference reasons, inventory TypeErrors) with a test pinning their disjointness. Export ParsedInventorySet and add tryAssembleInventorySet covering the empty-pages leaf as Result; the throwing entry contract is unchanged until callers migrate.
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
import { canonicalDigest, canonicalJson } from "./comparisonSemantics.js";
|
||||
import { parseEvidence, type Evidence } from "./evidence.js";
|
||||
import { artifactInspectionResultSchema } from "./artifactInspection.js";
|
||||
import { err, ok, type Result } from "./result.js";
|
||||
|
||||
/** Complete or partially assembled inventory used by comparison workflows. */
|
||||
export interface InventorySet {
|
||||
@@ -20,7 +21,7 @@ export interface InventorySet {
|
||||
readonly complete: boolean;
|
||||
}
|
||||
|
||||
interface ParsedInventorySet {
|
||||
export interface ParsedInventorySet {
|
||||
readonly evidence: readonly Evidence[];
|
||||
readonly inventory: InventorySet;
|
||||
}
|
||||
@@ -85,14 +86,36 @@ const validateInventoryPage = (inventory: ArtifactInventoryResult): void => {
|
||||
throw new TypeError("Artifact node ID is not content-addressed");
|
||||
};
|
||||
|
||||
/** Empty-inventory failure, distinct from malformed/unsupported/unavailable taxonomy codes. */
|
||||
export interface InventoryAssemblyEmpty {
|
||||
readonly code: "empty";
|
||||
readonly message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result-returning seam for the empty-pages leaf. Other assembly failures
|
||||
* still throw TypeError (migration in progress); parseArtifactInventoryEvidence
|
||||
* keeps its throwing contract until callers switch.
|
||||
*/
|
||||
export const tryAssembleInventorySet = (
|
||||
pages: readonly {
|
||||
readonly evidence: Evidence;
|
||||
readonly inventory: ArtifactInventoryResult;
|
||||
}[],
|
||||
): Result<ParsedInventorySet, InventoryAssemblyEmpty> =>
|
||||
pages.length === 0
|
||||
? err({
|
||||
code: "empty",
|
||||
message: "Artifact inventory requires Evidence pages",
|
||||
})
|
||||
: ok(assembleInventorySet(pages));
|
||||
|
||||
const assembleInventorySet = (
|
||||
pages: readonly {
|
||||
readonly evidence: Evidence;
|
||||
readonly inventory: ArtifactInventoryResult;
|
||||
}[],
|
||||
): ParsedInventorySet => {
|
||||
if (pages.length === 0)
|
||||
throw new TypeError("Artifact inventory requires Evidence pages");
|
||||
const first = pages[0];
|
||||
if (first === undefined)
|
||||
throw new TypeError("Artifact inventory requires Evidence pages");
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
parseArtifactInventoryEvidence,
|
||||
tryAssembleInventorySet,
|
||||
} from "./artifactInventoryEvidence.js";
|
||||
import { EvidenceReferenceError } from "./evidenceErrors.js";
|
||||
import type { ReferenceSourceReaderError } from "../reference/ReferenceSourceReaderTypes.js";
|
||||
|
||||
/**
|
||||
* The three validation taxonomies and their disjoint codes:
|
||||
* - Filesystem reader: cancelled | invalid-root | io | unsupported
|
||||
* (per-entry: cancelled | changed | io | symlink | unsupported).
|
||||
* - Session evidence references: missing | wrong_operation |
|
||||
* wrong_predicate | identity_mismatch.
|
||||
* - Inventory assembly: TypeError strings, migrating to Result leaves
|
||||
* starting with the empty-pages code below.
|
||||
*/
|
||||
const readerCodes: readonly ReferenceSourceReaderError["code"][] = [
|
||||
"cancelled",
|
||||
"invalid-root",
|
||||
"io",
|
||||
"unsupported",
|
||||
];
|
||||
|
||||
const referenceReasons = (
|
||||
[
|
||||
"missing",
|
||||
"wrong_operation",
|
||||
"wrong_predicate",
|
||||
"identity_mismatch",
|
||||
] as const
|
||||
).map(
|
||||
(reason) =>
|
||||
new EvidenceReferenceError("ev_test", reason, "expected", null).reason,
|
||||
);
|
||||
|
||||
describe("validation taxonomy mapping", () => {
|
||||
it("keeps session reference reasons disjoint from reader codes", () => {
|
||||
for (const reason of referenceReasons)
|
||||
expect(readerCodes).not.toContain(reason);
|
||||
});
|
||||
|
||||
it("returns the empty-pages leaf as Result without throwing", () => {
|
||||
const result = tryAssembleInventorySet([]);
|
||||
expect(result).toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
code: "empty",
|
||||
message: "Artifact inventory requires Evidence pages",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the throwing entry contract for empty input", () => {
|
||||
expect(() => parseArtifactInventoryEvidence([])).toThrowError(
|
||||
"Artifact inventory requires Evidence pages",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user