mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 13:27:39 +00:00
refactor(test): dedupe agent and discord test fixtures
This commit is contained in:
@@ -7,6 +7,7 @@ import type { OpenClawConfig } from "../config/config.js";
|
||||
import { createOpenClawCodingTools } from "./pi-tools.js";
|
||||
import type { SandboxDockerConfig } from "./sandbox.js";
|
||||
import type { SandboxFsBridge } from "./sandbox/fs-bridge.js";
|
||||
import { createRestrictedAgentSandboxConfig } from "./test-helpers/sandbox-agent-config-fixtures.js";
|
||||
|
||||
type ToolWithExecute = {
|
||||
execute: (toolCallId: string, args: unknown, signal?: AbortSignal) => Promise<unknown>;
|
||||
@@ -85,28 +86,41 @@ describe("Agent-specific tool filtering", () => {
|
||||
}
|
||||
}
|
||||
|
||||
it("should apply global tool policy when no agent-specific policy exists", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
tools: {
|
||||
allow: ["read", "write"],
|
||||
deny: ["bash"],
|
||||
},
|
||||
agents: {
|
||||
list: [
|
||||
{
|
||||
id: "main",
|
||||
workspace: "~/openclaw",
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const tools = createOpenClawCodingTools({
|
||||
function createMainSessionTools(cfg: OpenClawConfig) {
|
||||
return createOpenClawCodingTools({
|
||||
config: cfg,
|
||||
sessionKey: "agent:main:main",
|
||||
workspaceDir: "/tmp/test",
|
||||
agentDir: "/tmp/agent",
|
||||
});
|
||||
}
|
||||
|
||||
function createMainAgentConfig(params: {
|
||||
tools: NonNullable<OpenClawConfig["tools"]>;
|
||||
agentTools?: NonNullable<NonNullable<OpenClawConfig["agents"]>["list"]>[number]["tools"];
|
||||
}): OpenClawConfig {
|
||||
return {
|
||||
tools: params.tools,
|
||||
agents: {
|
||||
list: [
|
||||
{
|
||||
id: "main",
|
||||
workspace: "~/openclaw",
|
||||
...(params.agentTools ? { tools: params.agentTools } : {}),
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
it("should apply global tool policy when no agent-specific policy exists", () => {
|
||||
const cfg = createMainAgentConfig({
|
||||
tools: {
|
||||
allow: ["read", "write"],
|
||||
deny: ["bash"],
|
||||
},
|
||||
});
|
||||
const tools = createMainSessionTools(cfg);
|
||||
|
||||
const toolNames = tools.map((t) => t.name);
|
||||
expect(toolNames).toContain("read");
|
||||
@@ -116,32 +130,18 @@ describe("Agent-specific tool filtering", () => {
|
||||
});
|
||||
|
||||
it("should keep global tool policy when agent only sets tools.elevated", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
const cfg = createMainAgentConfig({
|
||||
tools: {
|
||||
deny: ["write"],
|
||||
},
|
||||
agents: {
|
||||
list: [
|
||||
{
|
||||
id: "main",
|
||||
workspace: "~/openclaw",
|
||||
tools: {
|
||||
elevated: {
|
||||
enabled: true,
|
||||
allowFrom: { whatsapp: ["+15555550123"] },
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
agentTools: {
|
||||
elevated: {
|
||||
enabled: true,
|
||||
allowFrom: { whatsapp: ["+15555550123"] },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const tools = createOpenClawCodingTools({
|
||||
config: cfg,
|
||||
sessionKey: "agent:main:main",
|
||||
workspaceDir: "/tmp/test",
|
||||
agentDir: "/tmp/agent",
|
||||
});
|
||||
const tools = createMainSessionTools(cfg);
|
||||
|
||||
const toolNames = tools.map((t) => t.name);
|
||||
expect(toolNames).toContain("exec");
|
||||
@@ -524,38 +524,16 @@ describe("Agent-specific tool filtering", () => {
|
||||
});
|
||||
|
||||
it("should work with sandbox tools filtering", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
sandbox: {
|
||||
mode: "all",
|
||||
scope: "agent",
|
||||
},
|
||||
},
|
||||
list: [
|
||||
{
|
||||
id: "restricted",
|
||||
workspace: "~/openclaw-restricted",
|
||||
sandbox: {
|
||||
mode: "all",
|
||||
scope: "agent",
|
||||
},
|
||||
tools: {
|
||||
allow: ["read"], // Agent further restricts to only read
|
||||
deny: ["exec", "write"],
|
||||
},
|
||||
},
|
||||
],
|
||||
const cfg = createRestrictedAgentSandboxConfig({
|
||||
agentTools: {
|
||||
allow: ["read"], // Agent further restricts to only read
|
||||
deny: ["exec", "write"],
|
||||
},
|
||||
tools: {
|
||||
sandbox: {
|
||||
tools: {
|
||||
allow: ["read", "write", "exec"], // Sandbox allows these
|
||||
deny: [],
|
||||
},
|
||||
},
|
||||
globalSandboxTools: {
|
||||
allow: ["read", "write", "exec"], // Sandbox allows these
|
||||
deny: [],
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const tools = createOpenClawCodingTools({
|
||||
config: cfg,
|
||||
|
||||
Reference in New Issue
Block a user