test(mcp): exercise real ZIP Zstandard refusals (#16200)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-03 03:03:20 +00:00
1 parent 7cfee63b43
commit 5a1515caa2
3 files changed
+42 -76

No files matched your search

+10 -38
View File
@@ -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<unknown>, 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<unknown>, 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();
}
},
+10 -38
View File
@@ -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<unknown>, 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<unknown>, 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();
}
},
+22
View File
@@ -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),
]),
});
};