From 577d6c3e9b7f6ddd86ed3fef9a7179921b334cb8 Mon Sep 17 00:00:00 2001 From: 64579525 <17373302683@163.com> Date: Tue, 13 Oct 2026 15:46:10 +0800 Subject: [PATCH] test(artifacts): share file read setup and satisfy CI checks --- .../boundary/filesystem/inesArtifact.test.ts | 52 ++++++++----------- .../process/processCaptureLifecycle.test.ts | 6 +-- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/tests/boundary/filesystem/inesArtifact.test.ts b/tests/boundary/filesystem/inesArtifact.test.ts index a9c93945f..427951f26 100644 --- a/tests/boundary/filesystem/inesArtifact.test.ts +++ b/tests/boundary/filesystem/inesArtifact.test.ts @@ -8,6 +8,7 @@ import { readFile, unlink, writeFile, + type FileHandle, } from "node:fs/promises"; import { join } from "node:path"; import { expect, it, vi } from "vitest"; @@ -30,21 +31,27 @@ import { const sha256 = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex"); +const fileReadPrototype = async ( + path: string, +): Promise> => { + const handle = await open(path, "r"); + try { + return Object.getPrototypeOf(handle) as Pick; + } finally { + await handle.close(); + } +}; + it("reuses scanned iNES region bytes for reconstruction without another full read", async () => { const root = await createTestTempDirectory("rea-ines-read-budget-"); const path = join(root, "large-tail.nes"); const bytes = inesFixture({ trailing: Buffer.alloc(2 * 1024 * 1024, 0x5a) }); await writeFile(path, bytes); - const sample = await open(path, "r"); - const prototype = Object.getPrototypeOf(sample) as Pick< - typeof sample, - "read" - >; - await sample.close(); + const prototype = await fileReadPrototype(path); const originalRead = prototype.read; let bytesRead = 0; const read = vi.spyOn(prototype, "read").mockImplementation(async function ( - this: typeof sample, + this: FileHandle, ...args ) { const result = await originalRead.call(this, ...args); @@ -70,16 +77,11 @@ it("rejects region bytes that disagree with stable whole-file hashes", async () const path = join(root, "source.nes"); const bytes = inesFixture(); await writeFile(path, bytes); - const sample = await open(path, "r"); - const prototype = Object.getPrototypeOf(sample) as Pick< - typeof sample, - "read" - >; - await sample.close(); + const prototype = await fileReadPrototype(path); const originalRead = prototype.read; let changed = false; const read = vi.spyOn(prototype, "read").mockImplementation(async function ( - this: typeof sample, + this: FileHandle, ...args ) { const result = await originalRead.call(this, ...args); @@ -384,16 +386,11 @@ it.each(["inventory", "target"])( for (const change of ["truncated", "counts", "signature"] as const) { const bytes = inesFixture(); await writeFile(path, bytes); - const sample = await open(path, "r"); - const prototype = Object.getPrototypeOf(sample) as Pick< - typeof sample, - "read" - >; - await sample.close(); + const prototype = await fileReadPrototype(path); const originalRead = prototype.read; const read = vi .spyOn(prototype, "read") - .mockImplementationOnce(async function (this: typeof sample, ...args) { + .mockImplementationOnce(async function (this: FileHandle, ...args) { read.mockRestore(); const changed = change === "truncated" ? bytes.subarray(0, 16) : Buffer.from(bytes); @@ -430,16 +427,11 @@ it.each([ const root = await createTestTempDirectory("rea-ines-hash-loss-"); const path = join(root, "source.nes"); await writeFile(path, inesFixture()); - const sample = await open(path, "r"); - const prototype = Object.getPrototypeOf(sample) as Pick< - typeof sample, - "read" - >; - await sample.close(); + const prototype = await fileReadPrototype(path); const originalRead = prototype.read; let observedPasses = 0; const read = vi.spyOn(prototype, "read").mockImplementation(async function ( - this: typeof sample, + this: FileHandle, ...args ) { const result = await originalRead.call(this, ...args); @@ -521,7 +513,7 @@ it.each(["EISDIR", "ENOTFILE", "EACCES", "cancelled"])( const controller = new AbortController(); const stat = vi .spyOn(prototype, "stat") - .mockImplementationOnce(async function (this: typeof sample, ...args) { + .mockImplementationOnce(async function (this: FileHandle, ...args) { if (code !== "cancelled") throw Object.assign(new Error("injected admission failure"), { code, @@ -635,7 +627,7 @@ it.each(["target", "inventory", "reader"])( }; const originalRead = prototype.read; const read = vi.spyOn(prototype, "read").mockImplementation(async function ( - this: typeof sample, + this: FileHandle, ...args ) { const [buffer, offset, length, position] = args; diff --git a/tests/boundary/process/processCaptureLifecycle.test.ts b/tests/boundary/process/processCaptureLifecycle.test.ts index 2a483845b..01d8eba13 100644 --- a/tests/boundary/process/processCaptureLifecycle.test.ts +++ b/tests/boundary/process/processCaptureLifecycle.test.ts @@ -196,9 +196,9 @@ itWithCaptureCapability( result.value.frames.map(({ at_ms }) => at_ms), "capture-relative frame times stay ordered and nonnegative", ).toEqual( - result.value.frames.map(({ at_ms }) => at_ms).sort( - (left, right) => left - right, - ), + result.value.frames + .map(({ at_ms }) => at_ms) + .sort((left, right) => left - right), ); expect(result.value.frames.every(({ at_ms }) => at_ms >= 0)).toBe(true); expect(result.value.exit.reason).toBe("exited");