fix(ci): stabilize state-dir dependent tests

This commit is contained in:
Peter Steinberger
2026-02-15 17:57:13 +00:00
parent 813b96a804
commit be9b5cefbd
4 changed files with 53 additions and 24 deletions

View File

@@ -19,6 +19,7 @@ let alphaPngFile = "";
let fallbackPngBuffer: Buffer;
let fallbackPngFile = "";
let fallbackPngCap = 0;
let previousStateDir: string | undefined;
async function writeTempFile(buffer: Buffer, ext: string): Promise<string> {
const file = path.join(fixtureRoot, `media-${fixtureFileCount++}${ext}`);
@@ -96,6 +97,21 @@ afterEach(() => {
});
describe("web media loading", () => {
beforeAll(() => {
// Ensure state dir is stable and not influenced by other tests that stub OPENCLAW_STATE_DIR.
// Also keep it outside os.tmpdir() so tmpdir localRoots doesn't accidentally make all state readable.
previousStateDir = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = path.join(os.homedir(), ".openclaw-media-state-test");
});
afterAll(() => {
if (previousStateDir === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = previousStateDir;
}
});
beforeAll(() => {
vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation(async (hostname) => {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
@@ -346,11 +362,12 @@ describe("local media root guard", () => {
});
it("allows default OpenClaw state workspace and sandbox roots", async () => {
const { STATE_DIR } = await import("../config/paths.js");
const { resolveStateDir } = await import("../config/paths.js");
const stateDir = resolveStateDir();
const readFile = vi.fn(async () => Buffer.from("generated-media"));
await expect(
loadWebMedia(path.join(STATE_DIR, "workspace", "tmp", "render.bin"), {
loadWebMedia(path.join(stateDir, "workspace", "tmp", "render.bin"), {
maxBytes: 1024 * 1024,
readFile,
}),
@@ -361,7 +378,7 @@ describe("local media root guard", () => {
);
await expect(
loadWebMedia(path.join(STATE_DIR, "sandboxes", "session-1", "frame.bin"), {
loadWebMedia(path.join(stateDir, "sandboxes", "session-1", "frame.bin"), {
maxBytes: 1024 * 1024,
readFile,
}),
@@ -373,11 +390,12 @@ describe("local media root guard", () => {
});
it("rejects default OpenClaw state per-agent workspace-* roots without explicit local roots", async () => {
const { STATE_DIR } = await import("../config/paths.js");
const { resolveStateDir } = await import("../config/paths.js");
const stateDir = resolveStateDir();
const readFile = vi.fn(async () => Buffer.from("generated-media"));
await expect(
loadWebMedia(path.join(STATE_DIR, "workspace-clawdy", "tmp", "render.bin"), {
loadWebMedia(path.join(stateDir, "workspace-clawdy", "tmp", "render.bin"), {
maxBytes: 1024 * 1024,
readFile,
}),
@@ -385,9 +403,10 @@ describe("local media root guard", () => {
});
it("allows per-agent workspace-* paths with explicit local roots", async () => {
const { STATE_DIR } = await import("../config/paths.js");
const { resolveStateDir } = await import("../config/paths.js");
const stateDir = resolveStateDir();
const readFile = vi.fn(async () => Buffer.from("generated-media"));
const agentWorkspaceDir = path.join(STATE_DIR, "workspace-clawdy");
const agentWorkspaceDir = path.join(stateDir, "workspace-clawdy");
await expect(
loadWebMedia(path.join(agentWorkspaceDir, "tmp", "render.bin"), {