Files
reverse-engineering-anything/tests/support/holdFirstFileRead.ts
T
morluto b4a2a79c55 test(lifecycle): move local gate copies, tests/process-global and tests/boundary phase gates onto the shared harness (#11412)
* test(lifecycle): replace eight local gate helper copies with the shared harness

* test(lifecycle): count an arrival announced in the turn its operation settles

* test(lifecycle): move tests/process-global phase gates onto the shared harness (#11414)

* test(lifecycle): move tests/process-global phase gates onto the shared harness

* test(lifecycle): move tests/boundary filesystem and process phase gates onto the shared harness (#11418)

* test(lifecycle): move tests/boundary provider phase gates onto the shared harness

* test(lifecycle): move tests/boundary/mcp phase gates onto the shared harness (#11433)

* test(lifecycle): give each phaseGate hold its own promise so unhandled held rejections stay visible

* test(lifecycle): move tests/boundary browser, pcap and CLI worker phase gates onto the shared harness (#11437)

* test(lifecycle): move tests/boundary browser, pcap and CLI worker phase gates onto the shared harness

* test(lifecycle): move fixture-owned and arrival-only provider gates onto the shared harness (#11441)

* test(lifecycle): move src/server and provider phase gates onto the shared harness (#11447)

* test(lifecycle): move the remaining src server, Kuna copy, Frida and Ghidra phase gates onto the shared harness (#11471)
2026-11-16 04:49:19 +08:00

49 lines
1.5 KiB
TypeScript

import { open, type FileHandle } from "node:fs/promises";
import { vi } from "vitest";
import { phaseGate } from "./lifecycleHarness.js";
/** Pause after one selected file chunk reaches the operating system. */
export const holdFirstFileRead = async (path: string) => {
const probe = await open(path, "r");
const identity = await probe.stat();
const fileHandlePrototype = Object.getPrototypeOf(probe) as {
read(
buffer: Buffer,
offset: number,
length: number,
position: number | null,
): Promise<{ readonly bytesRead: number; readonly buffer: Buffer }>;
};
await probe.close();
let totalReadBytes = 0;
let readCount = 0;
const firstRead = phaseGate();
const read = fileHandlePrototype.read;
vi.spyOn(fileHandlePrototype, "read").mockImplementation(async function (
this: FileHandle,
...args: [Buffer, number, number, number | null]
) {
const current = await this.stat();
const isTarget =
current.dev === identity.dev && current.ino === identity.ino;
const result = await read.call(this, ...args);
if (!isTarget) return result;
totalReadBytes += result.bytesRead;
readCount += 1;
if (readCount === 1) await firstRead.hold();
return result;
});
return {
/** Wait until the first read is held; fails if `operation` settles first. */
reached: firstRead.reached,
release: () => firstRead.release(),
get readCount() {
return readCount;
},
get totalReadBytes() {
return totalReadBytes;
},
};
};