mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 20:44:32 +00:00
test: reclassify sandbox and web/image tool suites as unit tests
This commit is contained in:
83
src/agents/sandbox-skills.test.ts
Normal file
83
src/agents/sandbox-skills.test.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { captureFullEnv } from "../test-utils/env.js";
|
||||
import { resolveSandboxContext } from "./sandbox.js";
|
||||
import { writeSkill } from "./skills.e2e-test-helpers.js";
|
||||
|
||||
vi.mock("./sandbox/docker.js", () => ({
|
||||
ensureSandboxContainer: vi.fn(async () => "openclaw-sbx-test"),
|
||||
}));
|
||||
|
||||
vi.mock("./sandbox/browser.js", () => ({
|
||||
ensureSandboxBrowser: vi.fn(async () => null),
|
||||
}));
|
||||
|
||||
vi.mock("./sandbox/prune.js", () => ({
|
||||
maybePruneSandboxes: vi.fn(async () => undefined),
|
||||
}));
|
||||
|
||||
describe("sandbox skill mirroring", () => {
|
||||
let envSnapshot: ReturnType<typeof captureFullEnv>;
|
||||
|
||||
beforeEach(() => {
|
||||
envSnapshot = captureFullEnv();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
envSnapshot.restore();
|
||||
});
|
||||
|
||||
const runContext = async (workspaceAccess: "none" | "ro") => {
|
||||
const bundledDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-bundled-skills-"));
|
||||
await fs.mkdir(bundledDir, { recursive: true });
|
||||
|
||||
process.env.OPENCLAW_BUNDLED_SKILLS_DIR = bundledDir;
|
||||
|
||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
||||
await writeSkill({
|
||||
dir: path.join(workspaceDir, "skills", "demo-skill"),
|
||||
name: "demo-skill",
|
||||
description: "Demo skill",
|
||||
});
|
||||
|
||||
const cfg: OpenClawConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
sandbox: {
|
||||
mode: "all",
|
||||
scope: "session",
|
||||
workspaceAccess,
|
||||
workspaceRoot: path.join(bundledDir, "sandboxes"),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const context = await resolveSandboxContext({
|
||||
config: cfg,
|
||||
sessionKey: "agent:main:main",
|
||||
workspaceDir,
|
||||
});
|
||||
|
||||
return { context, workspaceDir };
|
||||
};
|
||||
|
||||
it("copies skills into the sandbox when workspaceAccess is ro", async () => {
|
||||
const { context } = await runContext("ro");
|
||||
|
||||
expect(context?.enabled).toBe(true);
|
||||
const skillPath = path.join(context?.workspaceDir ?? "", "skills", "demo-skill", "SKILL.md");
|
||||
await expect(fs.readFile(skillPath, "utf-8")).resolves.toContain("demo-skill");
|
||||
}, 20_000);
|
||||
|
||||
it("copies skills into the sandbox when workspaceAccess is none", async () => {
|
||||
const { context } = await runContext("none");
|
||||
|
||||
expect(context?.enabled).toBe(true);
|
||||
const skillPath = path.join(context?.workspaceDir ?? "", "skills", "demo-skill", "SKILL.md");
|
||||
await expect(fs.readFile(skillPath, "utf-8")).resolves.toContain("demo-skill");
|
||||
}, 20_000);
|
||||
});
|
||||
Reference in New Issue
Block a user