From 5a1515caa2608bb4fd187267cf81b8bfbbed03e2 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 3 Dec 2026 11:03:20 +0800 Subject: [PATCH] test(mcp): exercise real ZIP Zstandard refusals (#16200) --- tests/boundary/mcp/nugetNupkgMcp.test.ts | 48 +++++------------------ tests/boundary/mcp/vsixPackageMcp.test.ts | 48 +++++------------------ tests/fixtures/zipZstandard.ts | 22 +++++++++++ 3 files changed, 42 insertions(+), 76 deletions(-) diff --git a/tests/boundary/mcp/nugetNupkgMcp.test.ts b/tests/boundary/mcp/nugetNupkgMcp.test.ts index 49526ccc476..c9df4c05a4d 100644 --- a/tests/boundary/mcp/nugetNupkgMcp.test.ts +++ b/tests/boundary/mcp/nugetNupkgMcp.test.ts @@ -2,8 +2,7 @@ import { nugetNupkgSchema } from "../../../src/domain/nuget/nupkg.js"; import { budgetedSummaryOf } from "../../fixtures/mcpClientProjection.js"; import { writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { ZipReader } from "@zip.js/zip.js"; -import { expect, vi } from "vitest"; +import { expect } from "vitest"; import { NUGET_NUPKG_TOOL_CONTRACTS } from "../../../src/contracts/nuget/nupkgToolContracts.js"; import { contractNamed } from "../../../src/cli/contractCliDescription.js"; import { createServer } from "../../../src/server/createServer.js"; @@ -12,6 +11,7 @@ import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { nugetNupkgFixture } from "../../../src/nuget/NugetNupkg.fixture.js"; import { ZIP_ZSTANDARD_WINDOW_CEILING } from "../../../src/resources/ZipArtifactReader.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { zstandardZipRefusalFixture } from "../../fixtures/zipZstandard.js"; import { mcpTest, type TestMcp } from "../../support/mcp/mcpFixture.js"; import { testDeliveryEnvironment } from "../../support/completeEvidenceByDefault.js"; @@ -66,26 +66,13 @@ mcpTest( "advises a fixed allowance, not more memory, for a Zstandard window refusal", async ({ mcp }) => { const { client, session } = await connect(mcp); - const refusal = Object.assign(new Error("Frame requires too much memory"), { - code: "ZSTD_error_frameParameter_windowTooLarge", - }); - const original = ZipReader.prototype.getEntriesGenerator; - const enumeration = vi - .spyOn(ZipReader.prototype, "getEntriesGenerator") - .mockImplementation(async function* (this: ZipReader, options) { - for await (const entry of original.call(this, options)) { - if (entry.filename === "Demo.Package.nuspec" && !entry.directory) { - entry.compressionMethod = 93; - entry.getData = () => Promise.reject(refusal); - } - yield entry; - } - return true; - }); try { const root = await createTestTempDirectory("rea-nupkg-mcp-"); const path = join(root, "Demo.Package.nupkg"); - await writeFile(path, await nugetNupkgFixture()); + await writeFile( + path, + zstandardZipRefusalFixture("Demo.Package.nuspec", "window"), + ); const response = await client.callTool({ name: "inspect_nuget_nupkg", arguments: { path }, @@ -104,7 +91,6 @@ mcpTest( }, }); } finally { - enumeration.mockRestore(); await session.close(); } }, @@ -114,26 +100,13 @@ mcpTest( "reports a Zstandard dictionary member as an unsupported target, not a malformed package", async ({ mcp }) => { const { client, session } = await connect(mcp); - const refusal = Object.assign(new Error("Dictionary mismatch"), { - code: "ZSTD_error_dictionary_wrong", - }); - const original = ZipReader.prototype.getEntriesGenerator; - const enumeration = vi - .spyOn(ZipReader.prototype, "getEntriesGenerator") - .mockImplementation(async function* (this: ZipReader, options) { - for await (const entry of original.call(this, options)) { - if (entry.filename === "Demo.Package.nuspec" && !entry.directory) { - entry.compressionMethod = 93; - entry.getData = () => Promise.reject(refusal); - } - yield entry; - } - return true; - }); try { const root = await createTestTempDirectory("rea-nupkg-mcp-"); const path = join(root, "Demo.Package.nupkg"); - await writeFile(path, await nugetNupkgFixture()); + await writeFile( + path, + zstandardZipRefusalFixture("Demo.Package.nuspec", "dictionary"), + ); const response = await client.callTool({ name: "inspect_nuget_nupkg", arguments: { path }, @@ -142,7 +115,6 @@ mcpTest( expect(error.code).toBe("unsupported_target"); expect(error.message).toMatch(/Zstandard dictionary/u); } finally { - enumeration.mockRestore(); await session.close(); } }, diff --git a/tests/boundary/mcp/vsixPackageMcp.test.ts b/tests/boundary/mcp/vsixPackageMcp.test.ts index fda84eac52e..38db601a346 100644 --- a/tests/boundary/mcp/vsixPackageMcp.test.ts +++ b/tests/boundary/mcp/vsixPackageMcp.test.ts @@ -2,8 +2,7 @@ import { vsixPackageSchema } from "../../../src/domain/vsix/vsixPackage.js"; import { budgetedSummaryOf } from "../../fixtures/mcpClientProjection.js"; import { writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { ZipReader } from "@zip.js/zip.js"; -import { expect, vi } from "vitest"; +import { expect } from "vitest"; import { VSIX_PACKAGE_TOOL_CONTRACTS } from "../../../src/contracts/vsix/vsixPackageToolContracts.js"; import { contractNamed } from "../../../src/cli/contractCliDescription.js"; import { createServer } from "../../../src/server/createServer.js"; @@ -11,6 +10,7 @@ import { createTestBinarySession } from "../../fixtures/binarySession.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { vsixPackageFixture } from "../../../src/vsix/VsixPackage.fixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { zstandardZipRefusalFixture } from "../../fixtures/zipZstandard.js"; import { mcpTest, type TestMcp } from "../../support/mcp/mcpFixture.js"; import { testDeliveryEnvironment, @@ -298,26 +298,13 @@ mcpTest( "advises a fixed allowance, not more memory, for a Zstandard window refusal", async ({ mcp }) => { const { client, session } = await connect(mcp); - const refusal = Object.assign(new Error("Frame requires too much memory"), { - code: "ZSTD_error_frameParameter_windowTooLarge", - }); - const original = ZipReader.prototype.getEntriesGenerator; - const enumeration = vi - .spyOn(ZipReader.prototype, "getEntriesGenerator") - .mockImplementation(async function* (this: ZipReader, options) { - for await (const entry of original.call(this, options)) { - if (entry.filename === "extension.vsixmanifest" && !entry.directory) { - entry.compressionMethod = 93; - entry.getData = () => Promise.reject(refusal); - } - yield entry; - } - return true; - }); try { const root = await createTestTempDirectory("rea-vsix-mcp-"); const path = join(root, "demo.hello.vsix"); - await writeFile(path, await vsixPackageFixture()); + await writeFile( + path, + zstandardZipRefusalFixture("extension.vsixmanifest", "window"), + ); const response = await client.callTool({ name: "inspect_vsix_package", arguments: { path }, @@ -336,7 +323,6 @@ mcpTest( unit: "bytes", }); } finally { - enumeration.mockRestore(); await session.close(); } }, @@ -346,26 +332,13 @@ mcpTest( "reports a Zstandard dictionary member as an unsupported target, not a malformed package", async ({ mcp }) => { const { client, session } = await connect(mcp); - const refusal = Object.assign(new Error("Dictionary mismatch"), { - code: "ZSTD_error_dictionary_wrong", - }); - const original = ZipReader.prototype.getEntriesGenerator; - const enumeration = vi - .spyOn(ZipReader.prototype, "getEntriesGenerator") - .mockImplementation(async function* (this: ZipReader, options) { - for await (const entry of original.call(this, options)) { - if (entry.filename === "extension.vsixmanifest" && !entry.directory) { - entry.compressionMethod = 93; - entry.getData = () => Promise.reject(refusal); - } - yield entry; - } - return true; - }); try { const root = await createTestTempDirectory("rea-vsix-mcp-"); const path = join(root, "demo.hello.vsix"); - await writeFile(path, await vsixPackageFixture()); + await writeFile( + path, + zstandardZipRefusalFixture("extension.vsixmanifest", "dictionary"), + ); const response = await client.callTool({ name: "inspect_vsix_package", arguments: { path }, @@ -374,7 +347,6 @@ mcpTest( expect(error.code).toBe("unsupported_target"); expect(error.message).toMatch(/Zstandard dictionary/u); } finally { - enumeration.mockRestore(); await session.close(); } }, diff --git a/tests/fixtures/zipZstandard.ts b/tests/fixtures/zipZstandard.ts index daf195a0e18..5d76845885f 100644 --- a/tests/fixtures/zipZstandard.ts +++ b/tests/fixtures/zipZstandard.ts @@ -54,3 +54,25 @@ export const zstandardZipFixture = ( end.writeUInt32LE(local.length + name.length + compressed.length, 16); return Buffer.concat([local, name, compressed, central, name, end]); }; + +/** One required package member that needs an unsupported Zstandard feature. */ +export const zstandardZipRefusalFixture = ( + name: string, + kind: "window" | "dictionary", +): Buffer => { + const { content, compressed } = zstandardWindowFrameFixture( + kind === "window" ? 0x90 : 0x50, + ); + if (kind === "window") + return zstandardZipFixture(content, { name, compressed }); + // The frame descriptor now announces a one-byte dictionary ID after the window byte. + compressed[4] = 1; + return zstandardZipFixture(content, { + name, + compressed: Buffer.concat([ + compressed.subarray(0, 6), + Buffer.of(1), + compressed.subarray(6), + ]), + }); +};