openclaw/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping-d.test.ts

94 lines
3.8 KiB
TypeScript
Raw Normal View History

2026-01-14 01:08:15 +00:00
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
2026-01-23 07:26:11 +00:00
import { describe, expect, it } from "vitest";
2026-01-23 18:31:47 +00:00
import "./test-helpers/fast-coding-tools.js";
import { createOpenClawCodingTools } from "./pi-tools.js";
import { createHostSandboxFsBridge } from "./test-helpers/host-sandbox-fs-bridge.js";
import { createPiToolsSandboxContext } from "./test-helpers/pi-tools-sandbox-context.js";
2026-01-14 01:08:15 +00:00
2026-01-30 03:15:10 +01:00
const defaultTools = createOpenClawCodingTools();
const tinyPngBuffer = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO2f7z8AAAAASUVORK5CYII=",
"base64",
);
2026-01-23 07:34:46 +00:00
2026-01-30 03:15:10 +01:00
describe("createOpenClawCodingTools", () => {
it("returns image metadata for images and text-only blocks for text files", async () => {
2026-01-23 07:34:46 +00:00
const readTool = defaultTools.find((tool) => tool.name === "read");
2026-01-14 01:08:15 +00:00
expect(readTool).toBeDefined();
2026-01-30 03:15:10 +01:00
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-read-"));
2026-01-14 01:08:15 +00:00
try {
const imagePath = path.join(tmpDir, "sample.png");
await fs.writeFile(imagePath, tinyPngBuffer);
2026-01-14 01:08:15 +00:00
const imageResult = await readTool?.execute("tool-1", {
2026-01-14 01:08:15 +00:00
path: imagePath,
});
expect(imageResult?.content?.some((block) => block.type === "image")).toBe(true);
const imageText = imageResult?.content?.find((block) => block.type === "text") as
2026-01-14 01:08:15 +00:00
| { text?: string }
| undefined;
expect(imageText?.text ?? "").toContain("Read image file [image/png]");
const image = imageResult?.content?.find((block) => block.type === "image") as
2026-01-14 01:08:15 +00:00
| { mimeType?: string }
| undefined;
expect(image?.mimeType).toBe("image/png");
const textPath = path.join(tmpDir, "sample.txt");
2026-01-30 03:15:10 +01:00
const contents = "Hello from openclaw read tool.";
2026-01-14 01:08:15 +00:00
await fs.writeFile(textPath, contents, "utf8");
const textResult = await readTool?.execute("tool-2", {
2026-01-14 01:08:15 +00:00
path: textPath,
});
expect(textResult?.content?.some((block) => block.type === "image")).toBe(false);
const textBlocks = textResult?.content?.filter((block) => block.type === "text") as
| Array<{ text?: string }>
| undefined;
2026-01-14 01:08:15 +00:00
expect(textBlocks?.length ?? 0).toBeGreaterThan(0);
const combinedText = textBlocks?.map((block) => block.text ?? "").join("\n");
2026-01-14 01:08:15 +00:00
expect(combinedText).toContain(contents);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it("filters tools by sandbox policy", () => {
const sandboxDir = path.join(os.tmpdir(), "moltbot-sandbox");
const sandbox = createPiToolsSandboxContext({
workspaceDir: sandboxDir,
agentWorkspaceDir: path.join(os.tmpdir(), "moltbot-workspace"),
2026-02-17 15:48:44 +09:00
workspaceAccess: "none" as const,
fsBridge: createHostSandboxFsBridge(sandboxDir),
2026-01-14 01:08:15 +00:00
tools: {
allow: ["bash"],
deny: ["browser"],
},
});
2026-01-30 03:15:10 +01:00
const tools = createOpenClawCodingTools({ sandbox });
2026-01-14 01:08:15 +00:00
expect(tools.some((tool) => tool.name === "exec")).toBe(true);
expect(tools.some((tool) => tool.name === "read")).toBe(false);
expect(tools.some((tool) => tool.name === "browser")).toBe(false);
});
it("hard-disables write/edit when sandbox workspaceAccess is ro", () => {
const sandboxDir = path.join(os.tmpdir(), "moltbot-sandbox");
const sandbox = createPiToolsSandboxContext({
workspaceDir: sandboxDir,
agentWorkspaceDir: path.join(os.tmpdir(), "moltbot-workspace"),
2026-02-17 15:48:44 +09:00
workspaceAccess: "ro" as const,
fsBridge: createHostSandboxFsBridge(sandboxDir),
2026-01-14 01:08:15 +00:00
tools: {
allow: ["read", "write", "edit"],
deny: [],
},
});
2026-01-30 03:15:10 +01:00
const tools = createOpenClawCodingTools({ sandbox });
2026-01-14 01:08:15 +00:00
expect(tools.some((tool) => tool.name === "read")).toBe(true);
expect(tools.some((tool) => tool.name === "write")).toBe(false);
expect(tools.some((tool) => tool.name === "edit")).toBe(false);
});
});