Files
rea/tests/support/holdFirstFileRead.ts
T
morluto d8c76b6296 fix(io): retain cleanup ownership and honor cancellation
Keep failed JSON read handles in the existing server resource scope and fail admission until cleanup succeeds. Thread cancellation through every process CLI input read and replace timer-driven cancellation checks with a shared first-read gate.

Classify authenticated standalone Evidence as wrong-kind input while preserving tampered-record integrity failures and actionable messages across ledger, CLI, and MCP consumers. Record unrelated CS_RESTRICT process neighbors as unverified without weakening related-process ownership checks. Update stale ADB fixtures for the actual shell protocol.

Validated focused CLI, MCP, filesystem, ownership, and error projection tests; static, docs, and duplication gates pass. The host-level CS_RESTRICT load reproduction was not run. Refs #2050, #2035, #2064.
2026-10-12 14:12:44 +08:00

56 lines
1.6 KiB
TypeScript

import { open, type FileHandle } from "node:fs/promises";
import { vi } from "vitest";
/** 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;
let resolveFirstRead: (() => void) | undefined;
const firstRead = new Promise<void>((resolve) => {
resolveFirstRead = resolve;
});
let releaseRead: (() => void) | undefined;
const readGate = new Promise<void>((resolve) => {
releaseRead = resolve;
});
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) {
resolveFirstRead?.();
await readGate;
}
return result;
});
return {
firstRead,
release: () => releaseRead?.(),
get readCount() {
return readCount;
},
get totalReadBytes() {
return totalReadBytes;
},
};
};