From caf4550b42a2adaa9bbede7ff9938a94e292e89a Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 2 Dec 2026 06:38:11 +0800 Subject: [PATCH] test(python): share pyc code fixture construction (#15652) --- src/python/PythonPyc.fixture.ts | 7 + src/python/PythonPycParser.test.ts | 306 +++++++----------- .../applications/pythonPycViews.test.ts | 33 +- tests/acceptance/pythonPycCli.test.ts | 70 ++-- tests/boundary/mcp/pythonPycMcp.test.ts | 37 +-- 5 files changed, 184 insertions(+), 269 deletions(-) diff --git a/src/python/PythonPyc.fixture.ts b/src/python/PythonPyc.fixture.ts index 72686de88e..392b9412f2 100644 --- a/src/python/PythonPyc.fixture.ts +++ b/src/python/PythonPyc.fixture.ts @@ -146,6 +146,13 @@ export const marshalCode = ( return concat(...parts); }; +/** A timestamp-header pyc with independently selected magic and code layout. */ +export const pythonPycWithCode = ( + magic: number, + layout: PythonPycCodeLayout, + fields: FixtureCodeFields = {}, +): Buffer => concat(pythonPycHeader(magic), marshalCode(layout, fields)); + /** Complete pyc with a module code object and optional nested function. */ export const pythonPycFixture = ( layout: PythonPycCodeLayout = "py311", diff --git a/src/python/PythonPycParser.test.ts b/src/python/PythonPycParser.test.ts index 83e39ec343..56c356a77c 100644 --- a/src/python/PythonPycParser.test.ts +++ b/src/python/PythonPycParser.test.ts @@ -13,6 +13,7 @@ import { marshalCode, pythonPycFixture, pythonPycHeader, + pythonPycWithCode, } from "./PythonPyc.fixture.js"; import { parsePythonPyc } from "./PythonPycParser.js"; import { @@ -143,10 +144,7 @@ it.each([ ].map((code) => marshalCode(layout, { bytecode: code })), ]); const report = parse( - Buffer.concat([ - pythonPycHeader(magic), - marshalCode(layout, { consts: [frozenSet] }), - ]), + pythonPycWithCode(magic, layout, { consts: [frozenSet] }), ); expect(root(report).consts).toEqual([ { kind: "frozenset", length: distinct }, @@ -178,10 +176,7 @@ it.each([ Buffer.from([0x54, 0x30]), ]); const report = parse( - Buffer.concat([ - pythonPycHeader(magic), - marshalCode(layout, { consts: [set, dictionary] }), - ]), + pythonPycWithCode(magic, layout, { consts: [set, dictionary] }), ); expect(root(report).consts).toEqual([ { kind: "frozenset", length: expected }, @@ -215,13 +210,10 @@ it("preserves signed co_firstlineno in code objects and code constants", () => { name: "inner", firstlineno: -7, }); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - firstlineno: -1, - consts: [nested], - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + firstlineno: -1, + consts: [nested], + }); const report = parse(bytes); expect(root(report).firstlineno).toBe(-1); expect(root(report).consts).toContainEqual({ @@ -305,9 +297,7 @@ it.each([ ] as const)( "decodes supported standalone magic $magic with its own marshal layout", ({ magic, layout, version }) => { - const result = parse( - Buffer.concat([pythonPycHeader(magic), marshalCode(layout)]), - ); + const result = parse(pythonPycWithCode(magic, layout)); expect(result.header).toMatchObject({ magic, python_version: version, @@ -341,7 +331,7 @@ it("does not qualify a magic outside the pinned identity table", () => { }); const withSliceConsts = (magic: number, consts: Buffer[]): Buffer => - Buffer.concat([pythonPycHeader(magic), marshalCode("py311", { consts })]); + pythonPycWithCode(magic, "py311", { consts }); const marshalSlice = ( start: Buffer, stop: Buffer, @@ -508,7 +498,7 @@ const marshalLong = (digits: readonly number[], negative = false): Buffer => const marshalUnicode = (bytes: Buffer): Buffer => Buffer.concat([Buffer.from([0x75]), u32(bytes.length), bytes]); const withConsts = (consts: Buffer[]): Buffer => - Buffer.concat([pythonPycHeader(3531), marshalCode("py311", { consts })]); + pythonPycWithCode(3531, "py311", { consts }); const CO_VARARGS = 0x04; /** Each level keeps one flagged child and two TYPE_REF copies of it. */ @@ -557,10 +547,7 @@ it("bounds names repeated through TYPE_REF by what the report carries", () => { name, ...Array.from({ length: 100 }, () => marshalRef(0)), ]); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { namesObject }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { namesObject }); expect(() => parsePythonPyc( bytes, @@ -591,12 +578,9 @@ it("charges shared empty name tables against the object budget", () => { flagged: true, namesObject: index === 0 ? namesObject : marshalRef(1), }); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - consts: Array.from({ length: owners }, (_, index) => owner(index)), - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + consts: Array.from({ length: owners }, (_, index) => owner(index)), + }); const report = parseWith(bytes, { max_objects: 65_536, max_file_bytes: bytes.length, @@ -727,12 +711,7 @@ it("keeps signed zero and non-finite float and complex constants JSON-safe", () }); it("accepts an empty co_filename", () => { - const report = parse( - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { filename: "" }), - ]), - ); + const report = parse(pythonPycWithCode(3531, "py311", { filename: "" })); expect(root(report).filename).toBe(""); expect(pythonPycSchema.safeParse(report).success).toBe(true); }); @@ -740,10 +719,9 @@ it("accepts an empty co_filename", () => { it("rejects a code object whose location tables are not bytes", () => { expect(() => parse( - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { exceptiontableObject: Buffer.from([0x53]) }), - ]), + pythonPycWithCode(3531, "py311", { + exceptiontableObject: Buffer.from([0x53]), + }), ), ).toThrow(ParseFailureError); }); @@ -751,19 +729,16 @@ it("rejects a code object whose location tables are not bytes", () => { it("hashes one shared byte string no matter how many times it is referenced", () => { const payload = Buffer.alloc(1024 * 1024, 0x61); const refs = 1200; - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - constsObject: Buffer.concat([ - Buffer.from([0x28]), - u32(refs + 1), - Buffer.from([0xf3]), - u32(payload.length), - payload, - ...Array.from({ length: refs }, () => marshalRef(0)), - ]), - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + constsObject: Buffer.concat([ + Buffer.from([0x28]), + u32(refs + 1), + Buffer.from([0xf3]), + u32(payload.length), + payload, + ...Array.from({ length: refs }, () => marshalRef(0)), + ]), + }); const { report, work } = measure(bytes); expect(work.constSummaries).toBe(1); expect(work.digestedBytes).toBeLessThan(2 * payload.length); @@ -782,17 +757,14 @@ it("rejects constant tables whose copies exceed max_objects", () => { ]); const child = (constsObject: Buffer, name: string) => marshalCode("py311", { constsObject, name }); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - consts: [ - child(shared, "a"), - ...Array.from({ length: 5 }, (_, index) => - child(marshalRef(0), `b${index}`), - ), - ], - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + consts: [ + child(shared, "a"), + ...Array.from({ length: 5 }, (_, index) => + child(marshalRef(0), `b${index}`), + ), + ], + }); const artifact = { path: defaults.path, sha256: createHash("sha256").update(bytes).digest("hex"), @@ -824,23 +796,17 @@ it("rejects NULL tuple items and mismatched local kinds", () => { }); expect(() => parse( - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - localsplusnames: ["v"], - localspluskinds: Buffer.alloc(0), - }), - ]), + pythonPycWithCode(3531, "py311", { + localsplusnames: ["v"], + localspluskinds: Buffer.alloc(0), + }), ), ).toThrow(ParseFailureError); const matched = parse( - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - localsplusnames: ["v"], - localspluskinds: Buffer.from([0x20]), - }), - ]), + pythonPycWithCode(3531, "py311", { + localsplusnames: ["v"], + localspluskinds: Buffer.from([0x20]), + }), ); expect(root(matched).localsplusnames).toEqual(["v"]); expect(root(matched).localspluskinds_hex).toBe("20"); @@ -893,17 +859,14 @@ it("charges every reported code-constant name against the file budget", () => { flagged: true, }); const file = (copies: number): Buffer => - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - constsObject: Buffer.concat([ - Buffer.from([0x28]), - u32(copies), - child, - ...Array.from({ length: copies - 1 }, () => marshalRef(0)), - ]), - }), - ]); + pythonPycWithCode(3531, "py311", { + constsObject: Buffer.concat([ + Buffer.from([0x28]), + u32(copies), + child, + ...Array.from({ length: copies - 1 }, () => marshalRef(0)), + ]), + }); const once = parseLimited(file(1), 20_000); expect(root(once).consts[0]).toMatchObject({ kind: "code", @@ -923,17 +886,14 @@ it("reuses a shared integer's bit length instead of converting every reference", long.subarray(1), ]); const refs = 400; - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - constsObject: Buffer.concat([ - Buffer.from([0x28]), - u32(refs + 1), - flagged, - ...Array.from({ length: refs }, () => marshalRef(0)), - ]), - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + constsObject: Buffer.concat([ + Buffer.from([0x28]), + u32(refs + 1), + flagged, + ...Array.from({ length: refs }, () => marshalRef(0)), + ]), + }); const { report, work } = measure(bytes); // One summary carries the bit length for the integer and all its references. expect(work.constSummaries).toBe(1); @@ -1045,14 +1005,11 @@ it("reads marshal version 0 text floats inside a code object", () => { it("accepts empty code names", () => { const child = marshalCode("py311", { name: "", qualname: "" }); const report = parse( - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - name: "", - qualname: "", - consts: [child], - }), - ]), + pythonPycWithCode(3531, "py311", { + name: "", + qualname: "", + consts: [child], + }), ); expect(root(report).name).toBe(""); expect(root(report).qualname).toBe(""); @@ -1197,12 +1154,9 @@ it("lists a shared container's code objects once per owner and charges each copy child, ...Array.from({ length: copies - 1 }, () => marshalRef(1)), ]); - const repeated = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - consts: [container, ...Array.from({ length: 7 }, () => marshalRef(0))], - }), - ]); + const repeated = pythonPycWithCode(3531, "py311", { + consts: [container, ...Array.from({ length: 7 }, () => marshalRef(0))], + }); const { report: shared, work } = measure(repeated); // Eight owners share one walk of the container and its children. expect(work.listedChildren).toBe(copies); @@ -1226,20 +1180,17 @@ it("lists a shared container's code objects once per owner and charges each copy flagged: true, constsObject: ownedContainer, }); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - consts: [ - first, - ...Array.from({ length: owners - 1 }, (_, index) => - marshalCode("py311", { - name: `owner${index + 1}`, - constsObject: marshalRef(1), - }), - ), - ], - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + consts: [ + first, + ...Array.from({ length: owners - 1 }, (_, index) => + marshalCode("py311", { + name: `owner${index + 1}`, + constsObject: marshalRef(1), + }), + ), + ], + }); const report = parse(bytes); expect(report.code_objects.map((code) => code.name)).toEqual([ "", @@ -1504,10 +1455,9 @@ it("keeps one descendant list when many wrappers share a container", () => { const shared = marshalRef(0); for (let index = 0; index < wrappers; index += 1) pieces.push(Buffer.from([0x29 | 0x80, 1]), shared); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { constsObject: Buffer.concat(pieces) }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + constsObject: Buffer.concat(pieces), + }); const before = process.memoryUsage().heapUsed; const report = parseWith(bytes, { max_objects: 65_536 }); const retained = process.memoryUsage().heapUsed - before; @@ -1645,10 +1595,9 @@ it("follows a long TYPE_REF chain without a recursive walk", () => { pieces.push(index === 0 ? hidden : marshalRef(index - 1)); } pieces.push(later); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { constsObject: Buffer.concat(pieces) }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + constsObject: Buffer.concat(pieces), + }); const { report, work } = measure(bytes); expect(work.listedChildren).toBe(links); expect(work.nestedSteps).toBe(2 * links); @@ -1673,11 +1622,7 @@ it("rejects negative CPython code counts and keeps them out of the schema", () = nlocals?: number; stacksize?: number; }, - ) => - Buffer.concat([ - pythonPycHeader(layout === "py37" ? 3394 : 3531), - marshalCode(layout, fields), - ]); + ) => pythonPycWithCode(layout === "py37" ? 3394 : 3531, layout, fields); for (const field of [ "argcount", "posonlyargcount", @@ -1751,18 +1696,15 @@ it("keeps code objects whose name tables differ only by boundaries", () => { expect(pythonPycSchema.safeParse(split).success).toBe(true); const tables = parse( - Buffer.concat([ - pythonPycHeader(3439), - marshalCode("py38", { - consts: [ - frozen( - code("py38", { varnames: ["a"], cellvars: ["b"] }), - code("py38", { varnames: ["a", "b"] }), - code("py38", { varnames: ["a"], freevars: ["b"] }), - ), - ], - }), - ]), + pythonPycWithCode(3439, "py38", { + consts: [ + frozen( + code("py38", { varnames: ["a"], cellvars: ["b"] }), + code("py38", { varnames: ["a", "b"] }), + code("py38", { varnames: ["a"], freevars: ["b"] }), + ), + ], + }), ); expect(root(tables).consts[0]).toEqual({ kind: "frozenset", length: 3 }); expect(tables.code_objects).toHaveLength(4); @@ -1787,10 +1729,9 @@ it("hashes a long TYPE_REF tuple chain without recursing", () => { marshalRef(links), marshalRef(links + 1), ); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { constsObject: Buffer.concat(pieces) }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + constsObject: Buffer.concat(pieces), + }); expect(bytes.length).toBeLessThan(64_000); const { report, work } = measure(bytes); // Two key steps per link: each tuple and its reference to the previous one. @@ -1810,19 +1751,16 @@ it("hashes shallow TYPE_REF code chains without using the JavaScript stack", () consts: index === 0 ? [Buffer.from([0x4e])] : [marshalRef(index - 1)], }), ); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - constsObject: Buffer.concat([ - Buffer.from([0x28]), - u32(links + 1), - ...codes, - Buffer.from([0x3c]), - u32(1), - marshalRef(links - 1), - ]), - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + constsObject: Buffer.concat([ + Buffer.from([0x28]), + u32(links + 1), + ...codes, + Buffer.from([0x3c]), + u32(1), + marshalRef(links - 1), + ]), + }); const report = parseWith(bytes, { max_objects: 65_536 }); expect(root(report).consts.at(-1)).toEqual({ kind: "set", length: 1 }); expect(report.coverage).toMatchObject({ @@ -1851,19 +1789,16 @@ it("hashes slice-wrapped TYPE_REF code chains without using the JavaScript stack ], }), ); - const bytes = Buffer.concat([ - pythonPycHeader(3627), - marshalCode("py311", { - constsObject: Buffer.concat([ - Buffer.from([0x28]), - u32(links + 1), - ...codes, - Buffer.from([0x3c]), - u32(1), - marshalRef(links - 1), - ]), - }), - ]); + const bytes = pythonPycWithCode(3627, "py311", { + constsObject: Buffer.concat([ + Buffer.from([0x28]), + u32(links + 1), + ...codes, + Buffer.from([0x3c]), + u32(1), + marshalRef(links - 1), + ]), + }); const report = parseWith(bytes, { max_objects: 65_536 }); expect(root(report).consts.at(-1)).toEqual({ kind: "set", length: 1 }); expect(report.coverage).toMatchObject({ @@ -1877,8 +1812,7 @@ it("rejects code CPython treats as corrupt instead of complete coverage", () => layout: "py37" | "py38" | "py311", magic: number, fields: Parameters[1], - ): Buffer => - Buffer.concat([pythonPycHeader(magic), marshalCode(layout, fields)]); + ): Buffer => pythonPycWithCode(magic, layout, fields); expect(() => parse(file("py311", 3531, { bytecode: Buffer.from([0x53]) })), ).toThrow(/co_code is malformed/); @@ -1975,7 +1909,7 @@ it("rejects code CPython treats as corrupt instead of complete coverage", () => it("leaves 3.11 alpha magics with other code layouts unsupported", () => { for (const magic of [3452, 3459, 3460, 3493]) { - const bytes = Buffer.concat([pythonPycHeader(magic), marshalCode("py311")]); + const bytes = pythonPycWithCode(magic, "py311"); expect(() => parse(bytes)).toThrow( expect.objectContaining({ kind: "unsupported" }), ); diff --git a/tests/acceptance/applications/pythonPycViews.test.ts b/tests/acceptance/applications/pythonPycViews.test.ts index b35c48878b..de2262037a 100644 --- a/tests/acceptance/applications/pythonPycViews.test.ts +++ b/tests/acceptance/applications/pythonPycViews.test.ts @@ -17,7 +17,7 @@ import { digestCanonicalValue } from "../../../src/domain/canonicalDigest.js"; import { analysisViewResultSchema } from "../../../src/domain/analysisView/analysisViewResult.js"; import { marshalCode, - pythonPycHeader, + pythonPycWithCode, } from "../../../src/python/PythonPyc.fixture.js"; import { fileSubjectTarget } from "../../fixtures/evidenceSubject.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; @@ -30,23 +30,20 @@ const viewContract = toolContract("inspect_analysis_view"); const writeSameNamePyc = async (path: string): Promise => { await writeFile( path, - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - consts: [ - marshalCode("py311", { - name: "same", - qualname: "First.same", - firstlineno: 11, - }), - marshalCode("py311", { - name: "same", - qualname: "Second.same", - firstlineno: 22, - }), - ], - }), - ]), + pythonPycWithCode(3531, "py311", { + consts: [ + marshalCode("py311", { + name: "same", + qualname: "First.same", + firstlineno: 11, + }), + marshalCode("py311", { + name: "same", + qualname: "Second.same", + firstlineno: 22, + }), + ], + }), ); }; diff --git a/tests/acceptance/pythonPycCli.test.ts b/tests/acceptance/pythonPycCli.test.ts index 32b32768b4..913f841eee 100644 --- a/tests/acceptance/pythonPycCli.test.ts +++ b/tests/acceptance/pythonPycCli.test.ts @@ -8,6 +8,7 @@ import { marshalCode, pythonPycFixture, pythonPycHeader, + pythonPycWithCode, } from "../../src/python/PythonPyc.fixture.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest } from "../support/cli/cliFixture.js"; @@ -317,13 +318,10 @@ cliTest( name: "inner", firstlineno: -7, }); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - firstlineno: -1, - consts: [nested], - }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + firstlineno: -1, + consts: [nested], + }); await writeFile(path, bytes); const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], @@ -369,12 +367,9 @@ cliTest( const path = join(root, `${testCase.name}.pyc`); await writeFile( path, - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - constsObject: Buffer.from(testCase.consts, "hex"), - }), - ]), + pythonPycWithCode(3531, "py311", { + constsObject: Buffer.from(testCase.consts, "hex"), + }), ); const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], @@ -448,10 +443,9 @@ cliTest( const path = join(root, `${testCase.name}.pyc`); await writeFile( path, - Buffer.concat([ - pythonPycHeader(testCase.magic), - marshalCode(testCase.layout, { consts: [frozenSet, dict] }), - ]), + pythonPycWithCode(testCase.magic, testCase.layout, { + consts: [frozenSet, dict], + }), ); const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], @@ -474,15 +468,12 @@ cliTest( const rawPath = join(root, `raw-${testCase.name}.pyc`); await writeFile( rawPath, - Buffer.concat([ - pythonPycHeader(testCase.magic), - marshalCode(testCase.layout, { - name: "f", - flags: 0x40, - freevars: testCase.freevars, - cellvars: testCase.cellvars, - }), - ]), + pythonPycWithCode(testCase.magic, testCase.layout, { + name: "f", + flags: 0x40, + freevars: testCase.freevars, + cellvars: testCase.cellvars, + }), ); const rawResult = await cli.run({ arguments: ["inspect-python-pyc", rawPath, "--json"], @@ -499,16 +490,13 @@ cliTest( async ({ cli }) => { const root = await createTestTempDirectory("rea-python-pyc-cli-rel-"); const path = join(root, "sample.pyc"); - const bytes = Buffer.concat([ - pythonPycHeader(3394), - marshalCode("py37", { - flags: 0, - consts: [ - ...[42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map(marshalInt64), - Buffer.from([0x69, 0xd6, 0xff, 0xff, 0xff]), - ], - }), - ]); + const bytes = pythonPycWithCode(3394, "py37", { + flags: 0, + consts: [ + ...[42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map(marshalInt64), + Buffer.from([0x69, 0xd6, 0xff, 0xff, 0xff]), + ], + }); await writeFile(path, bytes); const result = await cli.run({ arguments: ["inspect-python-pyc", "sample.pyc", "--json"], @@ -624,10 +612,7 @@ cliTest( scalar, separate, ].map(marshalUtf8); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { consts }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { consts }); const root = await createTestTempDirectory("rea-python-pyc-preview-"); const path = join(root, "unicode.pyc"); await writeFile(path, bytes); @@ -673,10 +658,7 @@ cliTest( name, ...Array.from({ length: 100 }, () => Buffer.from([0x72, 0, 0, 0, 0])), ]); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { namesObject }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { namesObject }); await writeFile(path, bytes); for (const [flag, field, boundary, maximum, resource] of [ diff --git a/tests/boundary/mcp/pythonPycMcp.test.ts b/tests/boundary/mcp/pythonPycMcp.test.ts index 009843bac1..dd73a32f6e 100644 --- a/tests/boundary/mcp/pythonPycMcp.test.ts +++ b/tests/boundary/mcp/pythonPycMcp.test.ts @@ -15,6 +15,7 @@ import { marshalCode, pythonPycFixture, pythonPycHeader, + pythonPycWithCode, } from "../../../src/python/PythonPyc.fixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js"; @@ -59,15 +60,12 @@ it("reads signed TYPE_INT64 constants and rejects a truncated payload", async () const path = join(root, "signed.pyc"); await writeFile( path, - Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { - consts: [ - ...[42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map(marshalInt64), - marshalInt32(-42), - ], - }), - ]), + pythonPycWithCode(3531, "py311", { + consts: [ + ...[42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map(marshalInt64), + marshalInt32(-42), + ], + }), ); const response = await client.callTool({ name: "inspect_python_pyc", @@ -135,10 +133,10 @@ it("advertises inspect_python_pyc and reads nested code objects", async () => { name: "inner", firstlineno: -7, }); - const bytes = Buffer.concat([ - pythonPycHeader(3531), - marshalCode("py311", { firstlineno: -1, consts: [nested] }), - ]); + const bytes = pythonPycWithCode(3531, "py311", { + firstlineno: -1, + consts: [nested], + }); await writeFile(path, bytes); const response = await client.callTool( { name: "inspect_python_pyc", arguments: { path } }, @@ -187,10 +185,10 @@ it("advertised pyc output schema preserves historical signed fields", async () = const path = join(root, "ambiguous-3410.pyc"); await writeFile( path, - Buffer.concat([ - pythonPycHeader(3410), - marshalCode("py38", { stacksize: 0xffffffff, flags: 0x80000000 }), - ]), + pythonPycWithCode(3410, "py38", { + stacksize: 0xffffffff, + flags: 0x80000000, + }), ); const response = await client.callTool( { name: "inspect_python_pyc", arguments: { path } }, @@ -219,10 +217,7 @@ it("rejects negative code fields for magics after constructor validation was add const path = join(root, "negative-flags.pyc"); await writeFile( path, - Buffer.concat([ - pythonPycHeader(3411), - marshalCode("py38", { flags: 0x80000000 }), - ]), + pythonPycWithCode(3411, "py38", { flags: 0x80000000 }), ); const response = await client.callTool({ name: "inspect_python_pyc",