test: classify filesystem and process workflows by boundary

Move artifact inventory, application projection, runtime reconciliation, and TODO scanner suites into their real boundary lanes. Extract Hopper process/socket cleanup cases while keeping injected session cleanup cases in composition.

Validation: all 56 focused tests passed; discovery finds all 876 test files exactly once. Test assertions and provider verification scope are unchanged.
This commit is contained in:
morluto
2026-10-12 04:44:13 +08:00
parent 12fa8fd9f9
commit c2b56e3e1c
8 changed files with 193 additions and 193 deletions
@@ -0,0 +1,166 @@
import { access, mkdtemp, rm } from "node:fs/promises";
import { expect, it, onTestFinished } from "vitest";
import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js";
import { HopperStartError } from "../../../src/domain/hopperErrors.js";
import { err, ok as resultOk } from "../../../src/domain/result.js";
import type {
BridgeLauncher,
BridgeSession,
} from "../../../src/hopper/BridgeLauncher.js";
import { HopperClient } from "../../../src/hopper/HopperClient.js";
import {
acquireHopperTargetLease,
type HopperTargetLease,
} from "../../../src/hopper/HopperTargetLease.js";
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
import { hopperFixturePath } from "../providers/hopper/hopperClient.fixture.js";
import {
createBinarySessionTargets,
createTestBinarySession,
} from "../../fixtures/binarySession.js";
class RetryingHopperLauncher implements BridgeLauncher {
cleanupAllowed = false;
cleanupAttempts = 0;
directory: string | undefined;
lease: HopperTargetLease | undefined;
constructor(
readonly leaseDirectory: string,
readonly targetPath: string,
readonly cleanupRequired = false,
) {}
acquire(runId: string) {
return acquireHopperTargetLease({
targetPath: this.targetPath,
targetKind: "database",
loaderArgs: [],
runId,
directory: this.leaseDirectory,
});
}
async launch(session: BridgeSession) {
const lease = await this.acquire(session.runId);
if (!lease.acquired)
return err(
new HopperStartError({ userMessage: "fixture lease is held" }),
);
this.lease = lease.lease;
this.directory = session.directory;
const started = await spawnOwnedProviderProcess({
command: process.execPath,
arguments: [
hopperFixturePath,
session.socketPath,
session.token,
session.runId,
this.cleanupRequired ? "cleanup-required" : "acknowledge",
],
runId: session.runId,
expectedCommand: null,
});
return resultOk({
...started,
ownsProcessLifetime: true as const,
providerLifetime: "launcher-process" as const,
shutdownMode: "process-cleanup" as const,
cleanup: () => {
this.cleanupAttempts += 1;
return this.cleanupAllowed
? cleanupOwnedProcessGroup(started.ownership)
: Promise.resolve({
cleaned: false as const,
reason: "fixture process cleanup not confirmed",
});
},
releaseLease: () => lease.lease.release(),
});
}
}
it("retains Hopper process handles, private files, and lease until an actual session cleanup retry succeeds", async () => {
const leaseDirectory = await mkdtemp("/tmp/rea-hl-");
onTestFinished(() => rm(leaseDirectory, { recursive: true, force: true }));
const [first, second] = await createBinarySessionTargets();
const launcher = new RetryingHopperLauncher(leaseDirectory, first);
const hopper = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
let clientsCreated = 0;
const session = createTestBinarySession(() => {
clientsCreated += 1;
return {
execute: async () => {
const started = await hopper.start();
return started.ok
? resultOk(
createAnalysisExecution(null, {
id: "hopper",
name: "fixture Hopper",
version: null,
}),
)
: started;
},
close: (options) => hopper.close(options),
};
});
onTestFinished(async () => {
launcher.cleanupAllowed = true;
await session.close();
await hopper.close();
await launcher.lease?.release();
});
expect(await session.open(first)).toMatchObject({ ok: true });
expect(await session.close()).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
});
const runtime = launcher.directory;
if (runtime === undefined)
throw new Error("Fixture did not capture its runtime");
await access(runtime);
expect(launcher.cleanupAttempts).toBe(1);
expect(await launcher.acquire("other-owner")).toMatchObject({
acquired: false,
});
expect(await session.open(second)).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
});
expect(clientsCreated).toBe(1);
expect(launcher.cleanupAttempts).toBe(2);
launcher.cleanupAllowed = true;
expect(await session.close()).toEqual(resultOk(null));
expect(launcher.cleanupAttempts).toBe(3);
await expect(access(runtime)).rejects.toMatchObject({ code: "ENOENT" });
const replacement = await launcher.acquire("replacement-owner");
expect(replacement.acquired).toBe(true);
if (replacement.acquired) await replacement.lease.release();
expect((await session.open(second)).ok).toBe(true);
expect(clientsCreated).toBe(2);
});
it("reports a direct Hopper close failure and retries the retained owned process", async () => {
const leaseDirectory = await mkdtemp("/tmp/rea-hl-");
onTestFinished(() => rm(leaseDirectory, { recursive: true, force: true }));
const [target] = await createBinarySessionTargets();
const launcher = new RetryingHopperLauncher(leaseDirectory, target, true);
const hopper = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
onTestFinished(async () => {
launcher.cleanupAllowed = true;
await hopper.close();
await launcher.lease?.release();
});
expect(await hopper.start()).toMatchObject({ ok: true });
expect(await hopper.close()).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
});
expect(launcher.cleanupAttempts).toBe(1);
launcher.cleanupAllowed = true;
expect(await hopper.close()).toEqual(resultOk(null));
expect(launcher.cleanupAttempts).toBe(2);
});
+33
View File
@@ -0,0 +1,33 @@
import { execFile } from "node:child_process";
import { copyFile, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const execute = promisify(execFile);
describe("tech-debt marker scan", () => {
it("passes a clean tree and rejects a tree containing a marker", async () => {
const root = await createTestTempDirectory("rea-scan-todos-");
const scripts = join(root, "scripts");
const source = join(root, "src");
await Promise.all([mkdir(scripts), mkdir(source)]);
const scanner = join(scripts, "scan-todos.mjs");
await copyFile(join(process.cwd(), "scripts/scan-todos.mjs"), scanner);
await writeFile(join(source, "clean.ts"), "export const clean = true;\n");
await expect(execute(process.execPath, [scanner])).resolves.toMatchObject({
stdout: "No tech-debt markers found.\n",
});
const marker = `TO${"DO"}`;
await writeFile(join(scripts, "debt.mts"), `// ${marker}: close the gap\n`);
await expect(execute(process.execPath, [scanner])).rejects.toMatchObject({
code: 1,
stdout: expect.stringContaining(`${marker} - close the gap`),
});
});
});