import { expect, it } from "vitest"; import { createEvidence } from "../evidence.js"; import { freezeOwnedJsonTreeSnapshot } from "../immutableJson.js"; import { gpuCodeObjectsSchema } from "../gpu/gpuCodeObjects.js"; import type { AnalysisViewParent } from "./analysisViewParent.js"; import { gpuCodeObjectsItemsSchema, gpuCodeObjectsViewRequestSchema, projectGpuCodeObjectsView, } from "./gpuCodeObjectsView.js"; const report = () => gpuCodeObjectsSchema.parse({ artifact: { path: "/gpu", file_size: 0, sha256: "a".repeat(64), bytes_read: 0, }, format: "elf", analysis: "inventory", objects: [], kernels: [], ptx_artifacts: [], coverage: { discovery_scope: "supported-section-names-and-standalone-formats", discovery: "partial", payloads: "not-requested", examined_sections: 0, unknown_section_names: 0, }, limitations: ["No sections examined."], }); const parent = (normalizedResult: unknown): AnalysisViewParent => ({ predicateType: "rea.analysis", evidenceId: `ev_${"b".repeat(64)}`, operation: "inspect_gpu_code_objects", normalizedResult, sealedResult: true, limitations: [], }); it("checks each subject despite reusable GPU summary indexes", () => { const source = freezeOwnedJsonTreeSnapshot(report()); const input = parent(source); expect(projectGpuCodeObjectsView(input, { kind: "summary" }).ok).toBe(true); const subject = createEvidence( { path: "/other", sha256: "b".repeat(64), format: "file" }, { id: "fixture", name: "Fixture", version: "1" }, { operation: input.operation, parameters: {}, result: source, confidence: "observed", }, ).subject; expect( projectGpuCodeObjectsView({ ...input, subject }, { kind: "summary" }), ).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } }); }); it("revalidates mutable GPU facts and preserves incomplete source coverage", () => { const source = report(); Object.freeze(source); const input = parent(source); const first = projectGpuCodeObjectsView(input, { kind: "summary" }); expect(first).toMatchObject({ ok: true, value: { summary: { coverage: { discovery: "partial", payloads: "not-requested" }, }, }, }); Object.assign(source.artifact, { sha256: "invalid" }); expect(projectGpuCodeObjectsView(input, { kind: "summary" })).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" }, }); }); const objectLocation = (size: number) => ({ container_id: null, offset: 0, size, section_index: null, section_name: null, }); it("preserves omitted AMD metadata columns instead of inventing null metadata", () => { const items = [ { id: "object", kind: "amdgpu", target: "gfx900", payload_target: "gfx900", target_consistency: "single-source", inspection: "complete", location: objectLocation(64), metadata_location: objectLocation(64), }, ]; expect(gpuCodeObjectsItemsSchema.parse(items)).toEqual(items); }); it("pages GPU rows at the byte allowance in filtered order", () => { const location = objectLocation(0); const source = gpuCodeObjectsSchema.parse({ ...report(), objects: ["first", "excluded", "second", "oversized"].map((id) => ({ id, parent_id: null, kind: "ptx", target: null, payload_target: null, cuda_target_header: null, amd_target_metadata: null, amd_pal_metadata: null, target_consistency: "unexamined", bundle_id: null, compression: "none", compression_id: null, encoded_size: 0, decoded_size: 0, sha256: null, raw_flags: null, location, metadata_location: location, inspection: "inventory", limitations: ["x".repeat(id === "oversized" ? 1_100_000 : 550_000)], })), }); const input = parent(source); const view = { kind: "gpu-code-objects", collection: "objects", ids: ["second", "first"], offset: 0, limit: 2, } as const; const page = (request: unknown) => projectGpuCodeObjectsView( input, gpuCodeObjectsViewRequestSchema.parse(request), ); const first = page(view); expect(first).toMatchObject({ ok: true, value: { items: [{ id: "first" }], coverage: { examined: 1, total: 2, next_offset: 1 }, }, }); if (!first.ok) throw first.error; const second = page({ ...view, offset: first.value.coverage.next_offset!, }); expect(second).toMatchObject({ ok: true, value: { items: [{ id: "second" }], coverage: { examined: 1, total: 2, next_offset: null }, }, }); const oversized = { ...view, ids: ["oversized"] }; expect(page(oversized)).toMatchObject({ ok: false, error: { _tag: "AnalysisResourceConstraintError" }, }); expect(page({ ...oversized, fields: ["id"] })).toMatchObject({ ok: true, value: { items: [{ id: "oversized" }], coverage: { examined: 1, total: 1, next_offset: null }, }, }); });