diff --git a/src/application/nuget/NugetNupkgService.test.ts b/src/application/nuget/NugetNupkgService.test.ts index 278f400385..f1828fc52b 100644 --- a/src/application/nuget/NugetNupkgService.test.ts +++ b/src/application/nuget/NugetNupkgService.test.ts @@ -113,21 +113,3 @@ it("retains completed Evidence and caller selections on late cancellation", asyn }); } }); - -it.each([undefined, 67108864, 128])( - "preserves the caller's selected file budget %s", - async (maximum) => { - const input = { - path: report.artifact.path, - ...(maximum === undefined ? {} : { max_file_bytes: maximum }), - }; - const service = new NugetNupkgService({ - close: async () => {}, - identity, - inspect: async () => ok(report), - }); - const result = await service.inspect(input); - if (!result.ok) throw result.error; - expect(result.value.parameters).toEqual(input); - }, -); diff --git a/src/domain/analysisView/nugetNupkgView.test.ts b/src/domain/analysisView/nugetNupkgView.test.ts index af99d1b8ad..dee2bb1427 100644 --- a/src/domain/analysisView/nugetNupkgView.test.ts +++ b/src/domain/analysisView/nugetNupkgView.test.ts @@ -22,29 +22,25 @@ const report = { dependency_groups: [ { target_framework: "net8.0", - dependencies: [ - { - id: "Newtonsoft.Json", - version: "13.0.1", - include: null, - exclude: null, - development_dependency: null, - }, - ], - }, - ], - contents: [ - { - name: "./Demo.Package.nuspec", - path: "Demo.Package.nuspec", - uncompressed_size: 12, - compressed_size: 12, - role: "nuspec", - encrypted: false, + dependencies: ["Newtonsoft.Json", "Demo.Support"].map((id) => ({ + id, + version: "13.0.1", + include: null, + exclude: null, + development_dependency: null, + })), }, ], + contents: ["Demo.Package.nuspec", "README.md", "build.props"].map((path) => ({ + name: path, + path, + uncompressed_size: 12, + compressed_size: 12, + role: path.endsWith(".nuspec") ? "nuspec" : "unknown", + encrypted: false, + })), nuspec: { path: "Demo.Package.nuspec", bytes: 12, source: "root-nuspec" }, - coverage: { status: "complete", entries: 1, examined_nuspec_bytes: 12 }, + coverage: { status: "unknown", entries: 3, examined_nuspec_bytes: 12 }, limitations: ["Package condition"], }; @@ -60,7 +56,7 @@ const parent = (): AnalysisViewParent => { operation: "inspect_nuget_nupkg", parameters: {}, result: report, - limitations: ["Parent condition"], + limitations: ["Parent condition", "Selection condition"], }, ); return { @@ -73,9 +69,10 @@ const parent = (): AnalysisViewParent => { }; }; -it("summarizes NuGet package Evidence with identity and counts", () => { +it("keeps source coverage and both limitation owners in the retained summary", () => { + const input = parent(); const result = projectAnalysisView( - parent(), + input, analysisViewRequestSchema.parse({ kind: "summary" }), ); if (!result.ok) throw result.error; @@ -83,11 +80,19 @@ it("summarizes NuGet package Evidence with identity and counts", () => { expect(view.summary).toMatchObject({ identity: { id: "Demo.Package", version: "1.2.3" }, dependency_group_count: 1, - dependency_count: 1, - content_count: 1, + dependency_count: 2, + content_count: 3, limitation_count: 1, + coverage: { status: "unknown", entries: 3, examined_nuspec_bytes: 12 }, }); - expect(view.limitations).toEqual( - expect.arrayContaining(["Parent condition", "Package condition"]), - ); + expect(view).toMatchObject({ + parent_evidence_id: input.evidenceId, + parent_digest: input.evidenceId.slice(3), + coverage: { status: "complete-within-view", examined: 3, total: 3 }, + }); + expect(view.limitations).toEqual([ + "Package condition", + "Parent condition", + "Selection condition", + ]); }); diff --git a/src/nuget/NugetNupkg.fixture.ts b/src/nuget/NugetNupkg.fixture.ts index c25afc688a..f041ca0e08 100644 --- a/src/nuget/NugetNupkg.fixture.ts +++ b/src/nuget/NugetNupkg.fixture.ts @@ -7,67 +7,64 @@ import { import { crc32 } from "node:zlib"; /** Real ZIP filename carriers and their independent, format-defined readings. */ -export const NUPKG_NAME_ENCODING_CASES = [ +const nupkgNameEncodingCases = [ { encoding: "legacy", rawName: Buffer.from("6c69622f6e6574382e302f822e646c6c", "hex"), utf8: false, unicodePath: null, - name: "lib/net8.0/é.dll", - uncertain: true, }, { encoding: "ascii", rawName: Buffer.from("lib/net8.0/demo.dll"), utf8: false, unicodePath: null, - name: "lib/net8.0/demo.dll", - uncertain: false, }, { encoding: "utf8", - rawName: Buffer.from("lib/net8.0/é.dll"), + rawName: Buffer.from("lib/net8.0/π.dll"), utf8: true, unicodePath: null, - name: "lib/net8.0/é.dll", - uncertain: false, }, { encoding: "unicode-path", rawName: Buffer.from("6c69622f6e6574382e302f822e646c6c", "hex"), utf8: false, unicodePath: "lib/net8.0/café.dll", - name: "lib/net8.0/café.dll", - uncertain: false, }, ] as const; -/** Inert package with an independently selected raw filename, EFS flag and Unicode Path claim. */ -export const nugetNupkgNameEncodingFixture = async ( - input: (typeof NUPKG_NAME_ENCODING_CASES)[number], -): Promise => { +/** Inert ZIP members use independently selected names, EFS flags and Unicode Path extras. */ +export const nugetNupkgNameEncodingFixture = async (): Promise => { const writer = new ZipWriter(new Uint8ArrayWriter(), { encodeText: (text, type) => - type === "filename" && text === "asset" ? input.rawName : undefined, + type === "filename" + ? nupkgNameEncodingCases.find(({ encoding }) => encoding === text) + ?.rawName + : undefined, }); - await writer.add("Demo.Package.nuspec", new TextReader(defaultNuspec), { - level: 0, - }); - let unicodeField: Buffer | undefined; - if (input.unicodePath !== null) { - const name = Buffer.from(input.unicodePath); - unicodeField = Buffer.alloc(5 + name.length); - unicodeField[0] = 1; - unicodeField.writeUInt32LE(crc32(input.rawName), 1); - name.copy(unicodeField, 5); + await addText(writer, "Demo.Package.nuspec", defaultNuspec); + for (const { + encoding, + rawName, + utf8, + unicodePath, + } of nupkgNameEncodingCases) { + let extraField: Map | undefined; + if (unicodePath !== null) { + const name = Buffer.from(unicodePath); + const unicodeField = Buffer.alloc(5 + name.length); + unicodeField[0] = 1; + unicodeField.writeUInt32LE(crc32(rawName), 1); + name.copy(unicodeField, 5); + extraField = new Map([[0x7075, unicodeField]]); + } + await writer.add(encoding, new TextReader("benign asset bytes"), { + level: 0, + useUnicodeFileNames: utf8, + ...(extraField === undefined ? {} : { extraField }), + }); } - await writer.add("asset", new TextReader("benign asset bytes"), { - level: 0, - useUnicodeFileNames: input.utf8, - ...(unicodeField === undefined - ? {} - : { extraField: new Map([[0x7075, unicodeField]]) }), - }); return writer.close(); }; diff --git a/tests/acceptance/nugetNupkgCli.test.ts b/tests/acceptance/nugetNupkgCli.test.ts index e696c40429..76f5cac54a 100644 --- a/tests/acceptance/nugetNupkgCli.test.ts +++ b/tests/acceptance/nugetNupkgCli.test.ts @@ -1,14 +1,16 @@ import { nugetNupkgSchema } from "../../src/domain/nuget/nupkg.js"; -import { writeFile } from "node:fs/promises"; +import { createHash } from "node:crypto"; +import { unlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect } from "vitest"; +import { ANALYSIS_VIEW_TOOL_CONTRACTS } from "../../src/contracts/analysisViewToolContracts.js"; import { NUGET_NUPKG_TOOL_CONTRACTS } from "../../src/contracts/nuget/nupkgToolContracts.js"; import { toolContract } from "../../src/contracts/toolContracts.js"; +import { nugetNupkgSummaryViewResultSchema } from "../../src/domain/analysisView/analysisViewResult.js"; import { parseEvidenceBundle } from "../../src/domain/evidenceBundle.js"; import { contractNamed } from "../../src/cli/contractCliDescription.js"; import { nugetNupkgFixture, - NUPKG_NAME_ENCODING_CASES, nugetNupkgNameEncodingFixture, } from "../../src/nuget/NugetNupkg.fixture.js"; import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; @@ -19,6 +21,10 @@ const nupkgContract = contractNamed( NUGET_NUPKG_TOOL_CONTRACTS, "inspect_nuget_nupkg", ); +const analysisViewContract = contractNamed( + ANALYSIS_VIEW_TOOL_CONTRACTS, + "inspect_analysis_view", +); const completeNupkgEvidenceSchema = nupkgContract.outputSchema.safeExtend({ normalized_result: nugetNupkgSchema, }); @@ -27,104 +33,101 @@ cliTest( "preserves ZIP filename bytes and encoding uncertainty through CLI, MCP and retained views", async ({ cli }) => { const root = await createTestTempDirectory("rea-nupkg-name-provenance-"); - const { call } = await connectLocalToolsMcp(); - for (const input of NUPKG_NAME_ENCODING_CASES) { - const path = join(root, `${input.encoding}.nupkg`); - await writeFile(path, await nugetNupkgNameEncodingFixture(input)); - const output = await cli.run({ - arguments: ["inspect-nuget-nupkg", path, "--json"], - }); - expect(output.exitCode, output.stderr).toBe(0); - const cliEvidence = completeNupkgEvidenceSchema.parse(output.json); - const response = await call("inspect_nuget_nupkg", { path }); - expect(response.isError, JSON.stringify(response)).not.toBe(true); - const evidence = completeNupkgEvidenceSchema.parse( - response.structuredContent, - ); - expect(evidence.normalized_result).toEqual(cliEvidence.normalized_result); - expect(evidence.normalized_result.contents).toContainEqual( - expect.objectContaining({ - name: input.name, - path: input.name, - role: "lib", - }), - ); - const warnings = evidence.normalized_result.limitations.filter((value) => - value.includes("CP437"), - ); - if (input.uncertain) { - expect(warnings).toEqual([ - expect.stringContaining( - `CP437 fallback; source encoding unverified; raw hex: ${input.rawName.toString("hex")}`, - ), - ]); - expect(warnings[0]).toContain(JSON.stringify(input.name)); - expect(evidence.limitations).toContain(warnings[0]); - } else expect(warnings).toEqual([]); - const view = await call("inspect_analysis_view", { - source: { - kind: "retained-evidence", - evidence_id: evidence.evidence_id, - }, - view: { kind: "summary" }, - }); - expect(view.isError, JSON.stringify(view)).not.toBe(true); - if (input.uncertain) - expect(JSON.stringify(view.structuredContent)).toContain( - input.rawName.toString("hex"), - ); - else - expect(JSON.stringify(view.structuredContent)).not.toContain("CP437"); - const retained = await call("get_evidence_bundle", { - detail: "complete", - }); - expect(retained.isError, JSON.stringify(retained)).not.toBe(true); - const bundle = parseEvidenceBundle( - toolContract("get_evidence_bundle").outputSchema.parse( - retained.structuredContent, - ).result, - ); - expect( - bundle.records.find( - (record) => record.evidence_id === evidence.evidence_id, - ), - ).toEqual(evidence); - } - }, -); - -cliTest( - "reports NuGet package identity and dependencies through the public CLI", - async ({ cli }) => { - const root = await createTestTempDirectory("rea-nupkg-cli-"); - const path = join(root, "Demo.Package.1.2.3.nupkg"); - await writeFile(path, await nugetNupkgFixture()); - const result = await cli.run({ + const path = join(root, "mixed-names.nupkg"); + const bytes = await nugetNupkgNameEncodingFixture(); + await writeFile(path, bytes); + const artifact = { + path, + sha256: createHash("sha256").update(bytes).digest("hex"), + }; + const output = await cli.run({ arguments: ["inspect-nuget-nupkg", path, "--json"], }); - expect(result.exitCode, result.stderr).toBe(0); - const evidence = completeNupkgEvidenceSchema.parse(result.json); - expect(evidence).toMatchObject({ + expect(output.exitCode, output.stderr).toBe(0); + const cliEvidence = completeNupkgEvidenceSchema.parse(output.json); + expect(cliEvidence).toMatchObject({ confidence: "observed", subject: { local_path: path, identity: { format: "file" } }, + normalized_result: { + artifact: { ...artifact, bytes: bytes.length }, + identity: { + id: "Demo.Package", + version: "1.2.3", + authors: "Alice, Bob", + description: "A tiny fixture package.", + }, + dependency_groups: [ + { + target_framework: "net8.0", + dependencies: [{ id: "Newtonsoft.Json", version: "13.0.1" }], + }, + { + target_framework: "netstandard2.0", + dependencies: [ + { id: "Newtonsoft.Json", version: "[12.0.3,13.0.0)" }, + ], + }, + ], + coverage: { status: "complete", entries: 5 }, + }, }); - expect(evidence.normalized_result.identity).toEqual({ - id: "Demo.Package", - version: "1.2.3", - authors: "Alice, Bob", - description: "A tiny fixture package.", + const { call } = await connectLocalToolsMcp(); + const response = await call("inspect_nuget_nupkg", { path }); + expect(response.isError, JSON.stringify(response)).not.toBe(true); + const evidence = completeNupkgEvidenceSchema.parse( + response.structuredContent, + ); + expect(evidence.normalized_result).toEqual(cliEvidence.normalized_result); + expect( + evidence.normalized_result.contents.filter(({ role }) => role === "lib"), + ).toEqual( + ["é.dll", "demo.dll", "π.dll", "café.dll"].map((file) => + expect.objectContaining({ + name: `lib/net8.0/${file}`, + path: `lib/net8.0/${file}`, + }), + ), + ); + const warnings = evidence.normalized_result.limitations.filter((message) => + message.includes("CP437"), + ); + expect(warnings).toEqual([ + expect.stringMatching( + /ZIP member "lib\/net8\.0\/é\.dll".*CP437 fallback; source encoding unverified; raw hex: 6c69622f6e6574382e302f822e646c6c/u, + ), + ]); + const [warning] = warnings; + expect(evidence.limitations).toContain(warning); + + await unlink(path); + const view = await call("inspect_analysis_view", { + source: { kind: "retained-evidence", evidence_id: evidence.evidence_id }, + view: { kind: "summary" }, }); - expect(evidence.normalized_result.dependency_groups).toEqual( - expect.arrayContaining([ - expect.objectContaining({ target_framework: "net8.0" }), - ]), + expect(view.isError, JSON.stringify(view)).not.toBe(true); + const summary = nugetNupkgSummaryViewResultSchema.parse( + analysisViewContract.outputSchema.parse(view.structuredContent) + .normalized_result, + ); + expect(summary).toMatchObject({ + parent_evidence_id: evidence.evidence_id, + artifact, + }); + expect( + summary.limitations.filter((message) => message.includes("CP437")), + ).toEqual([warning]); + const retained = await call("get_evidence_bundle", { detail: "complete" }); + expect(retained.isError, JSON.stringify(retained)).not.toBe(true); + const bundle = parseEvidenceBundle( + toolContract("get_evidence_bundle").outputSchema.parse( + retained.structuredContent, + ).result, ); expect( - evidence.normalized_result.contents.some( - (entry) => entry.role === "lib" && entry.name === "lib/net8.0/Demo.dll", + bundle.records.find( + ({ evidence_id }) => evidence_id === evidence.evidence_id, ), - ).toBe(true); - expect(evidence.normalized_result.coverage.status).toBe("complete"); + ).toEqual(evidence); }, ); @@ -261,6 +264,8 @@ cliTest( expect(evidence.normalized_result.limitations.join(" ")).toContain( "//feed.invalid/missing.nuspec?token=visible", ); + if (maximum !== 8192) continue; + await unlink(path); const view = await call("inspect_analysis_view", { source: { kind: "retained-evidence",