From 34abfa167c366bd9cc0fe5dd952b32247220bbbe Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 2 Dec 2026 13:05:22 +0800 Subject: [PATCH] test(model): share cancellation read spy (#15782) --- .../modelArtifactPartialCancellation.test.ts | 151 ++++++------------ 1 file changed, 52 insertions(+), 99 deletions(-) diff --git a/tests/boundary/model/modelArtifactPartialCancellation.test.ts b/tests/boundary/model/modelArtifactPartialCancellation.test.ts index 67b005c5aa0..1ffb564e808 100644 --- a/tests/boundary/model/modelArtifactPartialCancellation.test.ts +++ b/tests/boundary/model/modelArtifactPartialCancellation.test.ts @@ -7,6 +7,40 @@ import { projectAnalysisError } from "../../../src/domain/analysisErrorProjectio import { ggufFixture, safetensorsFixture } from "../../fixtures/model/model.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +const spyOnModelReads = async ( + path: string, + afterRead: (position: number, bytesRead: number) => void, +) => { + const sample = await open(path, "r"); + const prototype = Object.getPrototypeOf(sample) as { + read: ( + buffer: Buffer, + offset: number, + length: number, + position: number, + ) => Promise<{ bytesRead: number }>; + }; + await sample.close(); + const originalRead = prototype.read; + return vi.spyOn(prototype, "read").mockImplementation(async function ( + this: unknown, + buffer: Buffer, + offset: number, + length: number, + position: number, + ) { + const result = await originalRead.call( + this, + buffer, + offset, + length, + position, + ); + afterRead(position, result.bytesRead); + return result; + }); +}; + const cancelledArrayFixture = (alignment?: number) => { const seed = ggufFixture(); const count = 200_000; @@ -42,42 +76,15 @@ it.each([undefined, 64])( const path = join(root, "model.gguf"); const { fixture, count, arrayStart } = cancelledArrayFixture(alignment); await writeFile(path, fixture.bytes); - const sample = await open(path, "r"); - const handlePrototype = Object.getPrototypeOf(sample) as { - read: ( - buffer: Buffer, - offset: number, - length: number, - position: number, - ) => Promise<{ bytesRead: number }>; - }; - await sample.close(); - const originalRead = handlePrototype.read; const controller = new AbortController(); const reason = { marker: "caller-selected-parser-cancel" }; let arrayBytesObserved = 0; - const readSpy = vi - .spyOn(handlePrototype, "read") - .mockImplementation(async function ( - this: unknown, - buffer: Buffer, - offset: number, - length: number, - position: number, - ) { - const result = await originalRead.call( - this, - buffer, - offset, - length, - position, - ); - if (position === arrayStart && result.bytesRead > 0) { - arrayBytesObserved = result.bytesRead; - controller.abort(reason); - } - return result; - }); + const readSpy = await spyOnModelReads(path, (position, bytesRead) => { + if (position === arrayStart && bytesRead > 0) { + arrayBytesObserved = bytesRead; + controller.abort(reason); + } + }); const provider = new ModelArtifactProvider(); try { const result = await provider.inspect( @@ -152,42 +159,15 @@ it("retains GGUF tensor declarations and layout limitations before later tensor })), }); await writeFile(path, fixture.bytes); - const sample = await open(path, "r"); - const handlePrototype = Object.getPrototypeOf(sample) as { - read: ( - buffer: Buffer, - offset: number, - length: number, - position: number, - ) => Promise<{ bytesRead: number }>; - }; - await sample.close(); - const originalRead = handlePrototype.read; const controller = new AbortController(); const reason = { marker: "cancel-after-completed-tensor-declarations" }; let laterHeaderChunkSeen = false; - const readSpy = vi - .spyOn(handlePrototype, "read") - .mockImplementation(async function ( - this: unknown, - buffer: Buffer, - offset: number, - length: number, - position: number, - ) { - const result = await originalRead.call( - this, - buffer, - offset, - length, - position, - ); - if (position >= 24 + 65536 && result.bytesRead > 0) { - laterHeaderChunkSeen = true; - controller.abort(reason); - } - return result; - }); + const readSpy = await spyOnModelReads(path, (position, bytesRead) => { + if (position >= 24 + 65536 && bytesRead > 0) { + laterHeaderChunkSeen = true; + controller.abort(reason); + } + }); const provider = new ModelArtifactProvider(); try { const result = await provider.inspect( @@ -275,42 +255,15 @@ it.each([ ], }); await writeFile(path, fixture.bytes); - const sample = await open(path, "r"); - const prototype = Object.getPrototypeOf(sample) as { - read: ( - buffer: Buffer, - offset: number, - length: number, - position: number, - ) => Promise<{ bytesRead: number }>; - }; - await sample.close(); - const originalRead = prototype.read; const controller = new AbortController(); const reason = { marker: "cancel-during-full-model-hash" }; let payloadRead = false; - const readSpy = vi - .spyOn(prototype, "read") - .mockImplementation(async function ( - this: unknown, - buffer: Buffer, - offset: number, - length: number, - position: number, - ) { - const result = await originalRead.call( - this, - buffer, - offset, - length, - position, - ); - if (position >= fixture.header.length && result.bytesRead > 0) { - payloadRead = true; - controller.abort(reason); - } - return result; - }); + const readSpy = await spyOnModelReads(path, (position, bytesRead) => { + if (position >= fixture.header.length && bytesRead > 0) { + payloadRead = true; + controller.abort(reason); + } + }); const provider = new ModelArtifactProvider(); try { const result = await provider.inspect(