singularity-forge/src/tests/rtk-test-utils.ts
mastertyko d7755e596c test(gsd): harden suite-level stability for RTK, worktree, and git bootstrap (#2786)
* test: harden web runtime auth token and lock retry tests

Teach the packaged web runtime harness to recover the auth token from the launcher stderr when the browser-open stub log is absent. Also widen the transient session-lock retry tests so they stay stable under full-suite CPU contention.

* test: harden suite-level RTK and worktree stability

Stabilize the RTK seam tests under full-suite load by using a faster fake RTK binary on Unix and allowing the tests to raise the rewrite timeout without changing the production default. Also widen the transient session-lock retry budget and give the heavy auto-worktree milestone merge suite an explicit timeout so it can complete under CI-level contention.

* test: harden git-service repo bootstrap under suite load

Switch repo bootstrap steps in git-service.test.ts to runGit(...) where the setup only needs direct git invocations.

This removes avoidable shell wrappers from the highest-churn repo setup paths, which makes the full unit suite less prone to child-process flake under load while keeping the test behavior unchanged.
2026-03-26 20:02:41 -06:00

75 lines
2.4 KiB
TypeScript

import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
export type FakeRtkResponse = string | { status?: number; stdout?: string };
function shellQuote(value: string): string {
return `'${value.replace(/'/g, `'\"'\"'`)}'`;
}
export function createFakeRtk(mapping: Record<string, FakeRtkResponse>): { path: string; cleanup: () => void } {
const dir = mkdtempSync(join(tmpdir(), "gsd-fake-rtk-"));
const payload = JSON.stringify(mapping);
const jsSource = `#!/usr/bin/env node
const mapping = ${payload};
const args = process.argv.slice(2);
const fullInput = args.join(' ');
const rewriteInput = args[0] === 'rewrite' ? args.slice(1).join(' ') : null;
const match = mapping[fullInput] ?? (rewriteInput !== null ? mapping[rewriteInput] : undefined);
if (match === undefined) process.exit(1);
if (typeof match === 'string') {
process.stdout.write(match);
process.exit(0);
}
if (match.stdout) process.stdout.write(match.stdout);
process.exit(match.status ?? 0);
`;
if (process.platform === "win32") {
const jsPath = join(dir, "fake-rtk.js");
const cmdPath = join(dir, "rtk.cmd");
writeFileSync(jsPath, jsSource, "utf-8");
// Use the absolute jsPath so the .cmd works even when copied to another directory.
writeFileSync(cmdPath, `@echo off\r\n"${process.execPath}" "${jsPath}" %*\r\n`, "utf-8");
return {
path: cmdPath,
cleanup: () => rmSync(dir, { recursive: true, force: true }),
};
}
const binaryPath = join(dir, "rtk");
const cases = Object.entries(mapping).map(([key, response], index) => {
const output = typeof response === "string" ? response : (response.stdout ?? "");
const status = typeof response === "string" ? 0 : (response.status ?? 0);
return `
if [ "$full_input" = ${shellQuote(key)} ]; then
printf '%s' ${shellQuote(output)}
exit ${status}
fi
if [ -n "$rewrite_input" ] && [ "$rewrite_input" = ${shellQuote(key)} ]; then
printf '%s' ${shellQuote(output)}
exit ${status}
fi`.trimStart();
}).join("\n\n");
const shellSource = `#!/bin/sh
full_input="$*"
rewrite_input=""
if [ "$1" = "rewrite" ]; then
shift
rewrite_input="$*"
fi
${cases}
exit 1
`;
writeFileSync(binaryPath, shellSource, "utf-8");
chmodSync(binaryPath, 0o755);
return {
path: binaryPath,
cleanup: () => rmSync(dir, { recursive: true, force: true }),
};
}