Include full contents of all nested repositories
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
14
openclaw/src/test-helpers/ssrf.ts
Normal file
14
openclaw/src/test-helpers/ssrf.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { vi } from "vitest";
|
||||
import * as ssrf from "../infra/net/ssrf.js";
|
||||
|
||||
export function mockPinnedHostnameResolution(addresses: string[] = ["93.184.216.34"]) {
|
||||
return vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation(async (hostname) => {
|
||||
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
|
||||
const pinnedAddresses = [...addresses];
|
||||
return {
|
||||
hostname: normalized,
|
||||
addresses: pinnedAddresses,
|
||||
lookup: ssrf.createPinnedLookup({ hostname: normalized, addresses: pinnedAddresses }),
|
||||
};
|
||||
});
|
||||
}
|
||||
108
openclaw/src/test-helpers/state-dir-env.test.ts
Normal file
108
openclaw/src/test-helpers/state-dir-env.test.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
restoreStateDirEnv,
|
||||
setStateDirEnv,
|
||||
snapshotStateDirEnv,
|
||||
withStateDirEnv,
|
||||
} from "./state-dir-env.js";
|
||||
|
||||
type EnvSnapshot = {
|
||||
openclaw?: string;
|
||||
legacy?: string;
|
||||
};
|
||||
|
||||
function snapshotCurrentStateDirVars(): EnvSnapshot {
|
||||
return {
|
||||
openclaw: process.env.OPENCLAW_STATE_DIR,
|
||||
legacy: process.env.CLAWDBOT_STATE_DIR,
|
||||
};
|
||||
}
|
||||
|
||||
function expectStateDirVars(snapshot: EnvSnapshot) {
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe(snapshot.openclaw);
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBe(snapshot.legacy);
|
||||
}
|
||||
|
||||
async function expectPathMissing(filePath: string) {
|
||||
await expect(fs.stat(filePath)).rejects.toThrow();
|
||||
}
|
||||
|
||||
async function expectStateDirEnvRestored(params: {
|
||||
prev: EnvSnapshot;
|
||||
capturedStateDir: string;
|
||||
capturedTempRoot: string;
|
||||
}) {
|
||||
expectStateDirVars(params.prev);
|
||||
await expectPathMissing(params.capturedStateDir);
|
||||
await expectPathMissing(params.capturedTempRoot);
|
||||
}
|
||||
|
||||
describe("state-dir-env helpers", () => {
|
||||
it("set/snapshot/restore round-trips OPENCLAW_STATE_DIR", () => {
|
||||
const prev = snapshotCurrentStateDirVars();
|
||||
const snapshot = snapshotStateDirEnv();
|
||||
|
||||
setStateDirEnv("/tmp/openclaw-state-dir-test");
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe("/tmp/openclaw-state-dir-test");
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBeUndefined();
|
||||
|
||||
restoreStateDirEnv(snapshot);
|
||||
expectStateDirVars(prev);
|
||||
});
|
||||
|
||||
it("withStateDirEnv sets env for callback and cleans up temp root", async () => {
|
||||
const prev = snapshotCurrentStateDirVars();
|
||||
|
||||
let capturedTempRoot = "";
|
||||
let capturedStateDir = "";
|
||||
await withStateDirEnv("openclaw-state-dir-env-", async ({ tempRoot, stateDir }) => {
|
||||
capturedTempRoot = tempRoot;
|
||||
capturedStateDir = stateDir;
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe(stateDir);
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBeUndefined();
|
||||
await fs.writeFile(path.join(stateDir, "probe.txt"), "ok", "utf8");
|
||||
});
|
||||
|
||||
await expectStateDirEnvRestored({ prev, capturedStateDir, capturedTempRoot });
|
||||
});
|
||||
|
||||
it("withStateDirEnv restores env and cleans temp root when callback throws", async () => {
|
||||
const prev = snapshotCurrentStateDirVars();
|
||||
|
||||
let capturedTempRoot = "";
|
||||
let capturedStateDir = "";
|
||||
await expect(
|
||||
withStateDirEnv("openclaw-state-dir-env-", async ({ tempRoot, stateDir }) => {
|
||||
capturedTempRoot = tempRoot;
|
||||
capturedStateDir = stateDir;
|
||||
throw new Error("boom");
|
||||
}),
|
||||
).rejects.toThrow("boom");
|
||||
|
||||
await expectStateDirEnvRestored({ prev, capturedStateDir, capturedTempRoot });
|
||||
});
|
||||
|
||||
it("withStateDirEnv restores both env vars when legacy var was previously set", async () => {
|
||||
const testSnapshot = snapshotStateDirEnv();
|
||||
process.env.OPENCLAW_STATE_DIR = "/tmp/original-openclaw";
|
||||
process.env.CLAWDBOT_STATE_DIR = "/tmp/original-legacy";
|
||||
const prev = snapshotCurrentStateDirVars();
|
||||
|
||||
let capturedTempRoot = "";
|
||||
let capturedStateDir = "";
|
||||
try {
|
||||
await withStateDirEnv("openclaw-state-dir-env-", async ({ tempRoot, stateDir }) => {
|
||||
capturedTempRoot = tempRoot;
|
||||
capturedStateDir = stateDir;
|
||||
expect(process.env.OPENCLAW_STATE_DIR).toBe(stateDir);
|
||||
expect(process.env.CLAWDBOT_STATE_DIR).toBeUndefined();
|
||||
});
|
||||
|
||||
await expectStateDirEnvRestored({ prev, capturedStateDir, capturedTempRoot });
|
||||
} finally {
|
||||
restoreStateDirEnv(testSnapshot);
|
||||
}
|
||||
});
|
||||
});
|
||||
34
openclaw/src/test-helpers/state-dir-env.ts
Normal file
34
openclaw/src/test-helpers/state-dir-env.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
|
||||
export function snapshotStateDirEnv() {
|
||||
return captureEnv(["OPENCLAW_STATE_DIR", "CLAWDBOT_STATE_DIR"]);
|
||||
}
|
||||
|
||||
export function restoreStateDirEnv(snapshot: ReturnType<typeof snapshotStateDirEnv>): void {
|
||||
snapshot.restore();
|
||||
}
|
||||
|
||||
export function setStateDirEnv(stateDir: string): void {
|
||||
process.env.OPENCLAW_STATE_DIR = stateDir;
|
||||
delete process.env.CLAWDBOT_STATE_DIR;
|
||||
}
|
||||
|
||||
export async function withStateDirEnv<T>(
|
||||
prefix: string,
|
||||
fn: (ctx: { tempRoot: string; stateDir: string }) => Promise<T>,
|
||||
): Promise<T> {
|
||||
const snapshot = snapshotStateDirEnv();
|
||||
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||
const stateDir = path.join(tempRoot, "state");
|
||||
await fs.mkdir(stateDir, { recursive: true });
|
||||
setStateDirEnv(stateDir);
|
||||
try {
|
||||
return await fn({ tempRoot, stateDir });
|
||||
} finally {
|
||||
restoreStateDirEnv(snapshot);
|
||||
await fs.rm(tempRoot, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
17
openclaw/src/test-helpers/workspace.ts
Normal file
17
openclaw/src/test-helpers/workspace.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
export async function makeTempWorkspace(prefix = "openclaw-workspace-"): Promise<string> {
|
||||
return fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||
}
|
||||
|
||||
export async function writeWorkspaceFile(params: {
|
||||
dir: string;
|
||||
name: string;
|
||||
content: string;
|
||||
}): Promise<string> {
|
||||
const filePath = path.join(params.dir, params.name);
|
||||
await fs.writeFile(filePath, params.content, "utf-8");
|
||||
return filePath;
|
||||
}
|
||||
Reference in New Issue
Block a user