refactor(test): temp home env + normalize status

This commit is contained in:
Peter Steinberger
2026-01-09 16:49:01 +01:00
parent e8d75a39bc
commit 8341b662af
7 changed files with 190 additions and 132 deletions

View File

@@ -0,0 +1,32 @@
function stripAnsi(input: string): string {
let out = "";
for (let i = 0; i < input.length; i++) {
const code = input.charCodeAt(i);
if (code !== 27) {
out += input[i];
continue;
}
const next = input[i + 1];
if (next !== "[") continue;
i += 1;
while (i + 1 < input.length) {
i += 1;
const c = input[i];
if (!c) break;
const isLetter =
(c >= "A" && c <= "Z") || (c >= "a" && c <= "z") || c === "~";
if (isLetter) break;
}
}
return out;
}
export function normalizeTestText(input: string): string {
return stripAnsi(input)
.replaceAll("\r\n", "\n")
.replaceAll("…", "...")
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "?")
.replace(/[\uD800-\uDFFF]/g, "?");
}

View File

@@ -2,6 +2,8 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
type EnvValue = string | undefined | ((home: string) => string | undefined);
type EnvSnapshot = {
home: string | undefined;
userProfile: string | undefined;
@@ -35,6 +37,19 @@ function restoreEnv(snapshot: EnvSnapshot) {
restoreKey("CLAWDIS_STATE_DIR", snapshot.legacyStateDir);
}
function snapshotExtraEnv(keys: string[]): Record<string, string | undefined> {
const snapshot: Record<string, string | undefined> = {};
for (const key of keys) snapshot[key] = process.env[key];
return snapshot;
}
function restoreExtraEnv(snapshot: Record<string, string | undefined>) {
for (const [key, value] of Object.entries(snapshot)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
}
function setTempHome(base: string) {
process.env.HOME = base;
process.env.USERPROFILE = base;
@@ -50,17 +65,38 @@ function setTempHome(base: string) {
export async function withTempHome<T>(
fn: (home: string) => Promise<T>,
opts: { prefix?: string } = {},
opts: { env?: Record<string, EnvValue>; prefix?: string } = {},
): Promise<T> {
const base = await fs.mkdtemp(
path.join(os.tmpdir(), opts.prefix ?? "clawdbot-test-home-"),
);
const snapshot = snapshotEnv();
const envKeys = Object.keys(opts.env ?? {});
for (const key of envKeys) {
if (
key === "HOME" ||
key === "USERPROFILE" ||
key === "HOMEDRIVE" ||
key === "HOMEPATH"
) {
throw new Error(`withTempHome: use built-in home env (got ${key})`);
}
}
const envSnapshot = snapshotExtraEnv(envKeys);
setTempHome(base);
if (opts.env) {
for (const [key, raw] of Object.entries(opts.env)) {
const value = typeof raw === "function" ? raw(base) : raw;
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
}
try {
return await fn(base);
} finally {
restoreExtraEnv(envSnapshot);
restoreEnv(snapshot);
try {
await fs.rm(base, {