mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 11:33:10 +00:00
* test: fix false-pass assertions, consolidate suites, and close coverage gaps * test: finish signal-0, unchecked close_binary, and vacuous .every() audit * test(registration): run shared capture inside test so child inherits scaled deadline * test(artifacts): bind final verification fault to reader source and post-read phase
173 lines
5.8 KiB
TypeScript
173 lines
5.8 KiB
TypeScript
import { lstat, writeFile } from "node:fs/promises";
|
|
import type { FileHandle } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect, onTestFinished } from "vitest";
|
|
import type { AnalysisError } from "../../src/domain/analysisErrorBase.js";
|
|
import type { Result } from "../../src/domain/result.js";
|
|
import { PrivateRuntimeRoot } from "../../src/process/PrivateRuntimeRoot.js";
|
|
import { spawnOwnedProviderProcess } from "../../src/process/ProviderProcess.js";
|
|
import {
|
|
descriptorFailurePath,
|
|
expectRetainedDescriptor,
|
|
} from "./descriptorCloseFailure.js";
|
|
import { createTestTempDirectory } from "./temporaryDirectory.js";
|
|
|
|
export interface DescriptorCleanupFixtureState {
|
|
path: string;
|
|
denied: boolean;
|
|
handles: FileHandle[];
|
|
}
|
|
|
|
export const DESCRIPTOR_CLEANUP_MEMBERS = [
|
|
"capture",
|
|
"tool",
|
|
"limiter",
|
|
"kernel",
|
|
"field-tool",
|
|
"field-limiter",
|
|
"field-kernel",
|
|
] as const;
|
|
|
|
export type DescriptorCleanupMember =
|
|
(typeof DESCRIPTOR_CLEANUP_MEMBERS)[number];
|
|
|
|
export interface CliToolDescriptorCleanupOptions {
|
|
readonly cleanup: DescriptorCleanupFixtureState;
|
|
readonly member: DescriptorCleanupMember;
|
|
readonly tempPrefix: string;
|
|
readonly recordingFileName: string;
|
|
readonly commandFileName: string;
|
|
readonly commandSource: string;
|
|
readonly commandEnvKey: string;
|
|
readonly limiterEnvKey: string;
|
|
readonly primaryOperation: string;
|
|
readonly discoveryOperation: string;
|
|
readonly createFixture: (options: {
|
|
readonly env: NodeJS.ProcessEnv;
|
|
readonly spawn: (
|
|
input: Parameters<typeof spawnOwnedProviderProcess>[0],
|
|
) => ReturnType<typeof spawnOwnedProviderProcess>;
|
|
readonly createRuntimeRoot: () => Promise<PrivateRuntimeRoot>;
|
|
readonly recordingPath: string;
|
|
}) => {
|
|
readonly close: () => Promise<void>;
|
|
readonly runPrimary: () => Promise<Result<unknown, AnalysisError>>;
|
|
readonly runDiscovery: () => Promise<Result<unknown, AnalysisError>>;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Shared Linux descriptor-close-denial regression for CLI-backed capture and
|
|
* signal providers: verifies failed close retains the exact OS descriptor and
|
|
* runtime root, blocks replacement launches until released, and clears on retry.
|
|
*/
|
|
export const runCliToolDescriptorCleanupScenario = async (
|
|
options: CliToolDescriptorCleanupOptions,
|
|
): Promise<void> => {
|
|
const { cleanup, member } = options;
|
|
const directory = await createTestTempDirectory(options.tempPrefix);
|
|
const path = join(directory, options.recordingFileName);
|
|
const command = join(directory, options.commandFileName);
|
|
const limiter = join(directory, "limiter");
|
|
await writeFile(path, "recording bytes");
|
|
await writeFile(limiter, "limiter", { mode: 0o700 });
|
|
await writeFile(command, options.commandSource, { mode: 0o700 });
|
|
let launches = 0;
|
|
let runtimeRoot: PrivateRuntimeRoot | undefined;
|
|
const fixture = options.createFixture({
|
|
env: {
|
|
...process.env,
|
|
[options.commandEnvKey]: command,
|
|
[options.limiterEnvKey]: limiter,
|
|
},
|
|
spawn: (input) => {
|
|
launches += 1;
|
|
return spawnOwnedProviderProcess({
|
|
...input,
|
|
command: process.execPath,
|
|
arguments: [command, ...input.arguments],
|
|
expectedCommand: process.execPath,
|
|
});
|
|
},
|
|
createRuntimeRoot: async () => {
|
|
runtimeRoot = await PrivateRuntimeRoot.create({ parent: directory });
|
|
return runtimeRoot;
|
|
},
|
|
recordingPath: path,
|
|
});
|
|
|
|
const discovery = member.startsWith("field-");
|
|
const resource = discovery ? member.slice(6) : member;
|
|
const operation = discovery
|
|
? options.discoveryOperation
|
|
: options.primaryOperation;
|
|
cleanup.path = descriptorFailurePath(resource, {
|
|
capture: path,
|
|
tool: command,
|
|
limiter,
|
|
});
|
|
cleanup.denied = true;
|
|
cleanup.handles = [];
|
|
onTestFinished(async () => {
|
|
cleanup.denied = false;
|
|
await fixture.close();
|
|
for (const handle of cleanup.handles) await handle.close();
|
|
cleanup.path = "";
|
|
});
|
|
|
|
const first = discovery
|
|
? await fixture.runDiscovery()
|
|
: await fixture.runPrimary();
|
|
expectRetainedDescriptor(
|
|
first,
|
|
cleanup,
|
|
operation,
|
|
runtimeRoot === undefined ? [] : [runtimeRoot.path],
|
|
);
|
|
if (first.ok) throw new Error("Expected retained descriptor cleanup");
|
|
const originatingCause = first.error.cause;
|
|
if (resource === "capture") {
|
|
expect(runtimeRoot).toBeDefined();
|
|
await expect(lstat(runtimeRoot!.path)).resolves.toBeDefined();
|
|
}
|
|
const launchCount = launches;
|
|
expect(await fixture.runPrimary()).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: true, operation },
|
|
});
|
|
expect(cleanup.handles).toHaveLength(1);
|
|
expect(launches).toBe(launchCount);
|
|
|
|
let closingOperation = operation;
|
|
if (discovery) {
|
|
cleanup.denied = false;
|
|
const recovered = await fixture.runPrimary();
|
|
expect(recovered.ok).toBe(true);
|
|
expect(cleanup.handles.length).toBeGreaterThan(0);
|
|
expect(cleanup.handles.every((handle) => handle.fd === -1)).toBe(true);
|
|
expect(launches).toBeGreaterThan(launchCount);
|
|
cleanup.denied = true;
|
|
expect(await fixture.runPrimary()).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: true, operation: options.primaryOperation },
|
|
});
|
|
expect(cleanup.handles.at(-1)?.fd).toBeGreaterThanOrEqual(0);
|
|
closingOperation = options.primaryOperation;
|
|
}
|
|
|
|
await expect(fixture.close()).rejects.toMatchObject({
|
|
cleanupIncomplete: true,
|
|
operation: closingOperation,
|
|
});
|
|
cleanup.denied = false;
|
|
await fixture.close();
|
|
expect(first.error.cleanupIncomplete).toBe(false);
|
|
expect(first.error.cause).toBe(originatingCause);
|
|
if (runtimeRoot !== undefined)
|
|
await expect(lstat(runtimeRoot.path)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
expect(cleanup.handles.length).toBeGreaterThan(0);
|
|
expect(cleanup.handles.every((handle) => handle.fd === -1)).toBe(true);
|
|
};
|