refactor: consolidate schema scrub + test harness

This commit is contained in:
Peter Steinberger
2026-01-09 16:27:24 +01:00
parent 7957196924
commit f436808735
10 changed files with 332 additions and 411 deletions

View File

@@ -1,49 +1,4 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { installTestEnv } from "./test-env";
import { installWindowsCIOutputSanitizer } from "./windows-ci-output-sanitizer";
installWindowsCIOutputSanitizer();
const originalHome = process.env.HOME;
const originalUserProfile = process.env.USERPROFILE;
const originalXdgConfigHome = process.env.XDG_CONFIG_HOME;
const originalXdgDataHome = process.env.XDG_DATA_HOME;
const originalXdgStateHome = process.env.XDG_STATE_HOME;
const originalXdgCacheHome = process.env.XDG_CACHE_HOME;
const originalStateDir = process.env.CLAWDBOT_STATE_DIR;
const originalTestHome = process.env.CLAWDBOT_TEST_HOME;
const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-test-home-"));
process.env.HOME = tempHome;
process.env.USERPROFILE = tempHome;
process.env.CLAWDBOT_TEST_HOME = tempHome;
if (process.platform === "win32") {
process.env.CLAWDBOT_STATE_DIR = path.join(tempHome, ".clawdbot");
}
process.env.XDG_CONFIG_HOME = path.join(tempHome, ".config");
process.env.XDG_DATA_HOME = path.join(tempHome, ".local", "share");
process.env.XDG_STATE_HOME = path.join(tempHome, ".local", "state");
process.env.XDG_CACHE_HOME = path.join(tempHome, ".cache");
const restoreEnv = (key: string, value: string | undefined) => {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
};
process.on("exit", () => {
restoreEnv("HOME", originalHome);
restoreEnv("USERPROFILE", originalUserProfile);
restoreEnv("XDG_CONFIG_HOME", originalXdgConfigHome);
restoreEnv("XDG_DATA_HOME", originalXdgDataHome);
restoreEnv("XDG_STATE_HOME", originalXdgStateHome);
restoreEnv("XDG_CACHE_HOME", originalXdgCacheHome);
restoreEnv("CLAWDBOT_STATE_DIR", originalStateDir);
restoreEnv("CLAWDBOT_TEST_HOME", originalTestHome);
try {
fs.rmSync(tempHome, { recursive: true, force: true });
} catch {
// ignore cleanup errors
}
});
const { cleanup } = installTestEnv();
process.on("exit", cleanup);

54
test/test-env.ts Normal file
View File

@@ -0,0 +1,54 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
type RestoreEntry = { key: string; value: string | undefined };
function restoreEnv(entries: RestoreEntry[]): void {
for (const { key, value } of entries) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
}
export function installTestEnv(): { cleanup: () => void; tempHome: string } {
const restore: RestoreEntry[] = [
{ key: "HOME", value: process.env.HOME },
{ key: "USERPROFILE", value: process.env.USERPROFILE },
{ key: "XDG_CONFIG_HOME", value: process.env.XDG_CONFIG_HOME },
{ key: "XDG_DATA_HOME", value: process.env.XDG_DATA_HOME },
{ key: "XDG_STATE_HOME", value: process.env.XDG_STATE_HOME },
{ key: "XDG_CACHE_HOME", value: process.env.XDG_CACHE_HOME },
{ key: "CLAWDBOT_STATE_DIR", value: process.env.CLAWDBOT_STATE_DIR },
{ key: "CLAWDBOT_TEST_HOME", value: process.env.CLAWDBOT_TEST_HOME },
];
const tempHome = fs.mkdtempSync(
path.join(os.tmpdir(), "clawdbot-test-home-"),
);
process.env.HOME = tempHome;
process.env.USERPROFILE = tempHome;
process.env.CLAWDBOT_TEST_HOME = tempHome;
// Windows: prefer the legacy default state dir so auth/profile tests match real paths.
if (process.platform === "win32") {
process.env.CLAWDBOT_STATE_DIR = path.join(tempHome, ".clawdbot");
}
process.env.XDG_CONFIG_HOME = path.join(tempHome, ".config");
process.env.XDG_DATA_HOME = path.join(tempHome, ".local", "share");
process.env.XDG_STATE_HOME = path.join(tempHome, ".local", "state");
process.env.XDG_CACHE_HOME = path.join(tempHome, ".cache");
const cleanup = () => {
restoreEnv(restore);
try {
fs.rmSync(tempHome, { recursive: true, force: true });
} catch {
// ignore cleanup errors
}
};
return { cleanup, tempHome };
}

View File

@@ -1,5 +0,0 @@
import { installWindowsCIOutputSanitizer } from "./windows-ci-output-sanitizer";
export default function globalSetup() {
installWindowsCIOutputSanitizer();
}

View File

@@ -1,59 +0,0 @@
import fs from "node:fs";
function sanitizeWindowsCIOutput(text: string): string {
return text
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "?")
.replace(/[\uD800-\uDFFF]/g, "?");
}
function decodeUtf8Text(chunk: unknown): string | null {
if (typeof chunk === "string") return chunk;
if (Buffer.isBuffer(chunk)) return chunk.toString("utf-8");
if (chunk instanceof Uint8Array) return Buffer.from(chunk).toString("utf-8");
if (chunk instanceof ArrayBuffer) return Buffer.from(chunk).toString("utf-8");
if (ArrayBuffer.isView(chunk)) {
return Buffer.from(
chunk.buffer,
chunk.byteOffset,
chunk.byteLength,
).toString("utf-8");
}
return null;
}
export function installWindowsCIOutputSanitizer(): void {
if (process.platform !== "win32") return;
if (process.env.GITHUB_ACTIONS !== "true") return;
const globalKey = "__clawdbotWindowsCIOutputSanitizerInstalled";
if ((globalThis as Record<string, unknown>)[globalKey] === true) return;
(globalThis as Record<string, unknown>)[globalKey] = true;
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
const originalStderrWrite = process.stderr.write.bind(process.stderr);
process.stdout.write = ((chunk: unknown, ...args: unknown[]) => {
const text = decodeUtf8Text(chunk);
if (text !== null)
return originalStdoutWrite(sanitizeWindowsCIOutput(text), ...args);
return originalStdoutWrite(chunk as never, ...args); // passthrough
}) as typeof process.stdout.write;
process.stderr.write = ((chunk: unknown, ...args: unknown[]) => {
const text = decodeUtf8Text(chunk);
if (text !== null)
return originalStderrWrite(sanitizeWindowsCIOutput(text), ...args);
return originalStderrWrite(chunk as never, ...args); // passthrough
}) as typeof process.stderr.write;
const originalWriteSync = fs.writeSync.bind(fs);
fs.writeSync = ((fd: number, data: unknown, ...args: unknown[]) => {
if (fd === 1 || fd === 2) {
const text = decodeUtf8Text(data);
if (text !== null) {
return originalWriteSync(fd, sanitizeWindowsCIOutput(text), ...args);
}
}
return originalWriteSync(fd, data as never, ...(args as never[]));
}) as typeof fs.writeSync;
}