mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 01:51:24 +00:00
fix(ci): stabilize state-dir dependent tests
This commit is contained in:
@@ -1,26 +1,31 @@
|
|||||||
|
import path from "node:path";
|
||||||
import { describe, expect, test } from "vitest";
|
import { describe, expect, test } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { buildCleanupPlan } from "./cleanup-utils.js";
|
import { buildCleanupPlan } from "./cleanup-utils.js";
|
||||||
|
|
||||||
describe("buildCleanupPlan", () => {
|
describe("buildCleanupPlan", () => {
|
||||||
test("resolves inside-state flags and workspace dirs", () => {
|
test("resolves inside-state flags and workspace dirs", () => {
|
||||||
|
const tmpRoot = path.join(path.parse(process.cwd()).root, "tmp");
|
||||||
const cfg = {
|
const cfg = {
|
||||||
agents: {
|
agents: {
|
||||||
defaults: { workspace: "/tmp/openclaw-workspace-1" },
|
defaults: { workspace: path.join(tmpRoot, "openclaw-workspace-1") },
|
||||||
list: [{ workspace: "/tmp/openclaw-workspace-2" }],
|
list: [{ workspace: path.join(tmpRoot, "openclaw-workspace-2") }],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const plan = buildCleanupPlan({
|
const plan = buildCleanupPlan({
|
||||||
cfg: cfg as unknown as OpenClawConfig,
|
cfg: cfg as unknown as OpenClawConfig,
|
||||||
stateDir: "/tmp/openclaw-state",
|
stateDir: path.join(tmpRoot, "openclaw-state"),
|
||||||
configPath: "/tmp/openclaw-state/openclaw.json",
|
configPath: path.join(tmpRoot, "openclaw-state", "openclaw.json"),
|
||||||
oauthDir: "/tmp/openclaw-oauth",
|
oauthDir: path.join(tmpRoot, "openclaw-oauth"),
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(plan.configInsideState).toBe(true);
|
expect(plan.configInsideState).toBe(true);
|
||||||
expect(plan.oauthInsideState).toBe(false);
|
expect(plan.oauthInsideState).toBe(false);
|
||||||
expect(new Set(plan.workspaceDirs)).toEqual(
|
expect(new Set(plan.workspaceDirs)).toEqual(
|
||||||
new Set(["/tmp/openclaw-workspace-1", "/tmp/openclaw-workspace-2"]),
|
new Set([
|
||||||
|
path.join(tmpRoot, "openclaw-workspace-1"),
|
||||||
|
path.join(tmpRoot, "openclaw-workspace-2"),
|
||||||
|
]),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import type { ButtonInteraction, ComponentData } from "@buape/carbon";
|
import type { ButtonInteraction, ComponentData } from "@buape/carbon";
|
||||||
import { Routes } from "discord-api-types/v10";
|
import { Routes } from "discord-api-types/v10";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import type { DiscordExecApprovalConfig } from "../../config/types.discord.js";
|
import type { DiscordExecApprovalConfig } from "../../config/types.discord.js";
|
||||||
import {
|
import {
|
||||||
@@ -13,7 +15,7 @@ import {
|
|||||||
type ExecApprovalButtonContext,
|
type ExecApprovalButtonContext,
|
||||||
} from "./exec-approvals.js";
|
} from "./exec-approvals.js";
|
||||||
|
|
||||||
const STORE_PATH = "/tmp/openclaw-exec-approvals-test.json";
|
const STORE_PATH = path.join(os.tmpdir(), "openclaw-exec-approvals-test.json");
|
||||||
|
|
||||||
const writeStore = (store: Record<string, unknown>) => {
|
const writeStore = (store: Record<string, unknown>) => {
|
||||||
fs.writeFileSync(STORE_PATH, `${JSON.stringify(store, null, 2)}\n`, "utf8");
|
fs.writeFileSync(STORE_PATH, `${JSON.stringify(store, null, 2)}\n`, "utf8");
|
||||||
|
|||||||
@@ -2,25 +2,28 @@ import os from "node:os";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { resolveAgentWorkspaceDir } from "../agents/agent-scope.js";
|
import { resolveAgentWorkspaceDir } from "../agents/agent-scope.js";
|
||||||
import { STATE_DIR } from "../config/paths.js";
|
import { resolveStateDir } from "../config/paths.js";
|
||||||
|
|
||||||
const STATIC_LOCAL_ROOTS = [
|
function buildMediaLocalRoots(stateDir: string): string[] {
|
||||||
os.tmpdir(),
|
const resolvedStateDir = path.resolve(stateDir);
|
||||||
path.join(STATE_DIR, "media"),
|
return [
|
||||||
path.join(STATE_DIR, "agents"),
|
os.tmpdir(),
|
||||||
path.join(STATE_DIR, "workspace"),
|
path.join(resolvedStateDir, "media"),
|
||||||
path.join(STATE_DIR, "sandboxes"),
|
path.join(resolvedStateDir, "agents"),
|
||||||
] as const;
|
path.join(resolvedStateDir, "workspace"),
|
||||||
|
path.join(resolvedStateDir, "sandboxes"),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
export function getDefaultMediaLocalRoots(): readonly string[] {
|
export function getDefaultMediaLocalRoots(): readonly string[] {
|
||||||
return STATIC_LOCAL_ROOTS;
|
return buildMediaLocalRoots(resolveStateDir());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAgentScopedMediaLocalRoots(
|
export function getAgentScopedMediaLocalRoots(
|
||||||
cfg: OpenClawConfig,
|
cfg: OpenClawConfig,
|
||||||
agentId?: string,
|
agentId?: string,
|
||||||
): readonly string[] {
|
): readonly string[] {
|
||||||
const roots = [...STATIC_LOCAL_ROOTS];
|
const roots = buildMediaLocalRoots(resolveStateDir());
|
||||||
if (!agentId?.trim()) {
|
if (!agentId?.trim()) {
|
||||||
return roots;
|
return roots;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ let alphaPngFile = "";
|
|||||||
let fallbackPngBuffer: Buffer;
|
let fallbackPngBuffer: Buffer;
|
||||||
let fallbackPngFile = "";
|
let fallbackPngFile = "";
|
||||||
let fallbackPngCap = 0;
|
let fallbackPngCap = 0;
|
||||||
|
let previousStateDir: string | undefined;
|
||||||
|
|
||||||
async function writeTempFile(buffer: Buffer, ext: string): Promise<string> {
|
async function writeTempFile(buffer: Buffer, ext: string): Promise<string> {
|
||||||
const file = path.join(fixtureRoot, `media-${fixtureFileCount++}${ext}`);
|
const file = path.join(fixtureRoot, `media-${fixtureFileCount++}${ext}`);
|
||||||
@@ -96,6 +97,21 @@ afterEach(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("web media loading", () => {
|
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(() => {
|
beforeAll(() => {
|
||||||
vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation(async (hostname) => {
|
vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation(async (hostname) => {
|
||||||
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
|
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 () => {
|
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"));
|
const readFile = vi.fn(async () => Buffer.from("generated-media"));
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
loadWebMedia(path.join(STATE_DIR, "workspace", "tmp", "render.bin"), {
|
loadWebMedia(path.join(stateDir, "workspace", "tmp", "render.bin"), {
|
||||||
maxBytes: 1024 * 1024,
|
maxBytes: 1024 * 1024,
|
||||||
readFile,
|
readFile,
|
||||||
}),
|
}),
|
||||||
@@ -361,7 +378,7 @@ describe("local media root guard", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
loadWebMedia(path.join(STATE_DIR, "sandboxes", "session-1", "frame.bin"), {
|
loadWebMedia(path.join(stateDir, "sandboxes", "session-1", "frame.bin"), {
|
||||||
maxBytes: 1024 * 1024,
|
maxBytes: 1024 * 1024,
|
||||||
readFile,
|
readFile,
|
||||||
}),
|
}),
|
||||||
@@ -373,11 +390,12 @@ describe("local media root guard", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("rejects default OpenClaw state per-agent workspace-* roots without explicit local roots", async () => {
|
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"));
|
const readFile = vi.fn(async () => Buffer.from("generated-media"));
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
loadWebMedia(path.join(STATE_DIR, "workspace-clawdy", "tmp", "render.bin"), {
|
loadWebMedia(path.join(stateDir, "workspace-clawdy", "tmp", "render.bin"), {
|
||||||
maxBytes: 1024 * 1024,
|
maxBytes: 1024 * 1024,
|
||||||
readFile,
|
readFile,
|
||||||
}),
|
}),
|
||||||
@@ -385,9 +403,10 @@ describe("local media root guard", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("allows per-agent workspace-* paths with explicit local roots", async () => {
|
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 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(
|
await expect(
|
||||||
loadWebMedia(path.join(agentWorkspaceDir, "tmp", "render.bin"), {
|
loadWebMedia(path.join(agentWorkspaceDir, "tmp", "render.bin"), {
|
||||||
|
|||||||
Reference in New Issue
Block a user