singularity-forge/src/tests/rtk-test-utils.ts
Mikael Hugo b24f426f2b batch: snapshot of in-flight v2 work
This commit captures uncommitted modifications that accumulated in the
working tree across multiple in-progress workstreams. It is a snapshot
to clear the deck before sf v3 work begins; individual workstreams
should land separately on top of this.

Notable additions:
- trace-collector.ts, traces.ts, src/tests/trace-export.test.ts —
  trace export plumbing
- biome.json — Biome linter configuration
- .gitignore — exclude native/npm/**/*.node compiled binaries

The bulk of the diff is across src/resources/extensions/sf/ (301 files)
and src/resources/extensions/sf/tests/ (277 files), reflecting the
ongoing sf extension work. Specific feature commits should follow this
snapshot rather than being archaeology'd out of it.

The 76MB native/npm/linux-x64-gnu/forge_engine.node compiled binary
was left out of the commit — it's now gitignored and built locally.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 12:42:31 +02:00

85 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(), "sf-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 }),
};
}