refactor(test): centralize temp home + polling

This commit is contained in:
Peter Steinberger
2026-01-09 16:48:24 +01:00
parent eb73b4e58e
commit c8b15af979
5 changed files with 98 additions and 94 deletions

25
test/helpers/poll.ts Normal file
View File

@@ -0,0 +1,25 @@
export type PollOptions = {
timeoutMs?: number;
intervalMs?: number;
};
function sleep(ms: number) {
return new Promise<void>((resolve) => setTimeout(resolve, ms));
}
export async function pollUntil<T>(
fn: () => Promise<T | null | undefined>,
opts: PollOptions = {},
): Promise<T | undefined> {
const timeoutMs = opts.timeoutMs ?? 2000;
const intervalMs = opts.intervalMs ?? 25;
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const value = await fn();
if (value !== null && value !== undefined) return value;
await sleep(intervalMs);
}
return undefined;
}

View File

@@ -7,6 +7,8 @@ type EnvSnapshot = {
userProfile: string | undefined;
homeDrive: string | undefined;
homePath: string | undefined;
stateDir: string | undefined;
legacyStateDir: string | undefined;
};
function snapshotEnv(): EnvSnapshot {
@@ -15,6 +17,8 @@ function snapshotEnv(): EnvSnapshot {
userProfile: process.env.USERPROFILE,
homeDrive: process.env.HOMEDRIVE,
homePath: process.env.HOMEPATH,
stateDir: process.env.CLAWDBOT_STATE_DIR,
legacyStateDir: process.env.CLAWDIS_STATE_DIR,
};
}
@@ -27,11 +31,15 @@ function restoreEnv(snapshot: EnvSnapshot) {
restoreKey("USERPROFILE", snapshot.userProfile);
restoreKey("HOMEDRIVE", snapshot.homeDrive);
restoreKey("HOMEPATH", snapshot.homePath);
restoreKey("CLAWDBOT_STATE_DIR", snapshot.stateDir);
restoreKey("CLAWDIS_STATE_DIR", snapshot.legacyStateDir);
}
function setTempHome(base: string) {
process.env.HOME = base;
process.env.USERPROFILE = base;
process.env.CLAWDBOT_STATE_DIR = path.join(base, ".clawdbot");
process.env.CLAWDIS_STATE_DIR = path.join(base, ".clawdbot");
if (process.platform !== "win32") return;
const match = base.match(/^([A-Za-z]:)(.*)$/);