|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { Value } from "typebox/value"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 6 | +import { createOpenClawReadTool } from "./agent-tools.read.js"; |
| 7 | +import type { AnyAgentTool } from "./agent-tools.types.js"; |
| 8 | +import { createApplyPatchTool } from "./apply-patch.js"; |
| 9 | +import { createEditTool, createReadTool } from "./sessions/index.js"; |
| 10 | +import { DEFAULT_MAX_BYTES } from "./sessions/tools/truncate.js"; |
| 11 | +import { compactToolOutputHint } from "./tool-schema-hints.js"; |
| 12 | + |
| 13 | +const ONE_PIXEL_PNG_BASE64 = |
| 14 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII="; |
| 15 | + |
| 16 | +function expectContract(tool: AnyAgentTool, details: unknown): void { |
| 17 | + expect(tool.outputSchema).toBeDefined(); |
| 18 | + expect(Value.Check(tool.outputSchema!, details)).toBe(true); |
| 19 | +} |
| 20 | + |
| 21 | +describe("filesystem tool output contracts", () => { |
| 22 | + let tmpDir = ""; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-filesystem-contract-")); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(async () => { |
| 29 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("validates read text, image, truncation, and optional-not-found results", async () => { |
| 33 | + await fs.writeFile(path.join(tmpDir, "notes.txt"), "ordinary text\n", "utf8"); |
| 34 | + await fs.writeFile(path.join(tmpDir, "pixel.png"), Buffer.from(ONE_PIXEL_PNG_BASE64, "base64")); |
| 35 | + await fs.writeFile(path.join(tmpDir, "long.txt"), "x".repeat(DEFAULT_MAX_BYTES + 1), "utf8"); |
| 36 | + |
| 37 | + const tool = createOpenClawReadTool( |
| 38 | + createReadTool(tmpDir, { autoResizeImages: false }) as unknown as AnyAgentTool, |
| 39 | + ); |
| 40 | + const text = await tool.execute("read-text", { path: "notes.txt", limit: 10 }); |
| 41 | + const image = await tool.execute("read-image", { path: "pixel.png", limit: 10 }); |
| 42 | + const truncated = await tool.execute("read-truncated", { path: "long.txt", limit: 10 }); |
| 43 | + const notFound = await tool.execute("read-not-found", { path: "memory/2026-07-17.md" }); |
| 44 | + |
| 45 | + for (const result of [text, image, truncated, notFound]) { |
| 46 | + expectContract(tool, result.details); |
| 47 | + } |
| 48 | + expect(text.details).toEqual({ kind: "text", content: "ordinary text\n" }); |
| 49 | + expect(image.details).toMatchObject({ kind: "image", mimeType: "image/png" }); |
| 50 | + expect(truncated.details).toMatchObject({ kind: "truncated" }); |
| 51 | + expect(notFound.details).toEqual({ |
| 52 | + kind: "not_found", |
| 53 | + status: "not_found", |
| 54 | + path: "memory/2026-07-17.md", |
| 55 | + optional: true, |
| 56 | + }); |
| 57 | + expect(compactToolOutputHint(tool.outputSchema)).toBe( |
| 58 | + '{ content: string; kind: "text" } | { content: string; kind: "image"; mimeType: string } | { content: string; kind: "truncated"; truncation: { firstLineExceedsLimit: boolean; lastLinePartial: boolean; maxBytes: number; maxLines: number; outputBytes: number; outputLines: number; totalBytes: number; totalLines: number; truncated: true; truncatedBy: "lines" | "bytes" } } | { kind: "not_found"; optional: true; path: string; status: "not_found" }', |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it("validates edit changed and no-op results", async () => { |
| 63 | + const filePath = path.join(tmpDir, "edit.txt"); |
| 64 | + await fs.writeFile(filePath, "before\n", "utf8"); |
| 65 | + const tool = createEditTool(tmpDir) as unknown as AnyAgentTool; |
| 66 | + const changed = await tool.execute("edit-changed", { |
| 67 | + path: filePath, |
| 68 | + edits: [{ oldText: "before", newText: "after" }], |
| 69 | + }); |
| 70 | + const noOp = await tool.execute("edit-no-op", { |
| 71 | + path: filePath, |
| 72 | + edits: [{ oldText: "after", newText: "after" }], |
| 73 | + }); |
| 74 | + |
| 75 | + expectContract(tool, changed.details); |
| 76 | + expectContract(tool, noOp.details); |
| 77 | + expect(changed.details).toMatchObject({ changed: true }); |
| 78 | + expect(noOp.details).toEqual({ changed: false }); |
| 79 | + expect(compactToolOutputHint(tool.outputSchema)).toBe( |
| 80 | + "{ changed: false } | { changed: true; diff: string; patch: string; firstChangedLine?: number }", |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("validates apply_patch path summaries", async () => { |
| 85 | + const tool = createApplyPatchTool({ cwd: tmpDir }) as unknown as AnyAgentTool; |
| 86 | + const result = await tool.execute("patch-add", { |
| 87 | + input: "*** Begin Patch\n*** Add File: added.txt\n+added\n*** End Patch", |
| 88 | + }); |
| 89 | + |
| 90 | + expectContract(tool, result.details); |
| 91 | + expect(result.details).toEqual({ |
| 92 | + summary: { added: ["added.txt"], modified: [], deleted: [] }, |
| 93 | + }); |
| 94 | + expect(compactToolOutputHint(tool.outputSchema)).toBe( |
| 95 | + "{ summary: { added: Array<string>; deleted: Array<string>; modified: Array<string> } }", |
| 96 | + ); |
| 97 | + }); |
| 98 | +}); |
0 commit comments