Files
reverse-engineering-anything/tests/boundary/process/desktopBuildOwnedCommand.test.ts
T
gensokyo-devandgensokyo-dev 07e4b700ac test: let the deadline policy own test waits
Vitest's expect.poll and vi.waitFor stop after one second by default, a
host-speed guess that the project's deadline policy does not see. Under
hosted-CI load, kunaProjectConcurrency failed that way twice on unrelated
PRs. Another 256 waits relied on the default, and 97 passed literals
picked to fit some host.

Waits now take as long as their test may run, less the reporting reserve
that children already keep. Each Vitest project sets expect.poll's
default, and the new waitFor in tests/support/testWait.ts replaces
vi.waitFor. Fixture readiness waits (awaitAcknowledgement and the provider
process waits) default to the same deadline, and their host-speed literal
arguments are removed. One remainingTestDeadline owns the arithmetic for
both children and waits.

Ten waits whose short timeout was the real check now assert it directly
(Kuna version probe, Cutter discovery, doctor and Inspector cancellation,
apktool fixture teardown, JADX disconnect): settlement before the
production deadline that would otherwise also satisfy the wait, or a
deadline pinned beyond every test deadline.

The rea/project-wait-deadline lint rule rejects vi.waitFor and explicit
expect.poll timeouts. The testing guide and the CI tests audit skill now
document the policy.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-14 19:28:38 +08:00

101 lines
4.4 KiB
TypeScript

import { mkdir, readFile, stat } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { ArtifactResourceScope } from "../../../src/artifacts/ArtifactResourceScope.js";
import { runDesktopBuildOwnedCommand } from "../../../src/desktop/DesktopBuildOwnedCommand.js";
import { prepareProcessOwnershipInspection } from "../../../src/process/ProcessOwnershipObservation.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { waitFor } from "../../support/testWait.js";
it.skipIf(process.platform === "win32")(
"stops the packing process family before removing its partial archive on timeout",
async () => {
await prepareProcessOwnershipInspection();
const directory = await createTestTempDirectory("rea-owned-pack-");
const partial = join(directory, "partial.tar.xz");
const workspace = join(directory, "work");
await mkdir(workspace);
const pidFile = join(directory, "child.pid");
const child = `require('node:fs').writeFileSync(process.argv[1], String(process.pid)); require('node:fs').writeFileSync(require('node:path').join(require('node:os').tmpdir(), 'child-work'), 'temporary'); setInterval(() => {}, 1000);`;
const parent = `const {spawn} = require('node:child_process'); require('node:fs').writeFileSync(process.argv[1], 'partial'); spawn(process.execPath, ['-e', process.argv[3], process.argv[2]], {stdio:'inherit'}); setInterval(() => {}, 1000);`;
const resources = new ArtifactResourceScope();
try {
await expect(
runDesktopBuildOwnedCommand({
command: process.execPath,
arguments: ["-e", parent, partial, pidFile, child],
environment: {
...process.env,
TMPDIR: workspace,
TMP: workspace,
TEMP: workspace,
},
timeoutMs: 2000,
maxOutputBytes: 4096,
resources,
partialPath: partial,
workspace,
}),
).rejects.toMatchObject({
_tag: "AnalysisOutputError",
operation: "setup",
diagnostics: { command_failure: "timeout" },
});
const pid = Number(await readFile(pidFile, "utf8"));
expect(Number.isInteger(pid)).toBe(true);
expect(() => process.kill(pid, 0)).toThrow();
await expect(stat(partial)).rejects.toMatchObject({ code: "ENOENT" });
await expect(stat(workspace)).rejects.toMatchObject({ code: "ENOENT" });
expect(resources.hasPending).toBe(false);
} finally {
await resources.close();
}
},
);
it.skipIf(process.platform === "win32")(
"cancels a running setup process family before removing its partial output",
async () => {
await prepareProcessOwnershipInspection();
const directory = await createTestTempDirectory("rea-owned-pack-cancel-");
const partial = join(directory, "partial.tar.xz");
const pidFile = join(directory, "child.pid");
const child = `require('node:fs').writeFileSync(process.argv[1], String(process.pid)); setInterval(() => {}, 1000);`;
const parent = `const {spawn} = require('node:child_process'); require('node:fs').writeFileSync(process.argv[1], 'partial'); spawn(process.execPath, ['-e', process.argv[3], process.argv[2]], {stdio:'inherit'}); setInterval(() => {}, 1000);`;
const resources = new ArtifactResourceScope();
const controller = new AbortController();
const reason = new Error("caller stopped setup");
const running = runDesktopBuildOwnedCommand({
command: process.execPath,
arguments: ["-e", parent, partial, pidFile, child],
environment: process.env,
timeoutMs: 10_000,
maxOutputBytes: 4096,
resources,
partialPath: partial,
signal: controller.signal,
});
void running.catch(() => undefined);
try {
await waitFor(async () => {
expect(Number(await readFile(pidFile, "utf8"))).toBeGreaterThan(0);
});
const pid = Number(await readFile(pidFile, "utf8"));
controller.abort(reason);
await expect(running).rejects.toMatchObject({
_tag: "AnalysisCancelledError",
operation: "setup",
cause: reason,
diagnostics: { command_failure: "cancelled" },
});
expect(() => process.kill(pid, 0)).toThrow();
await expect(stat(partial)).rejects.toMatchObject({ code: "ENOENT" });
} finally {
controller.abort(reason);
await running.catch(() => undefined);
await resources.close();
}
},
);