| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- import { resolve } from "path"
- import { describe, expect, test } from "bun:test"
- import {
- completedToolContent,
- completedToolUpdate,
- completedToolRawOutput,
- extractImageAttachments,
- imageContents,
- pendingToolCall,
- shellOutputSnapshot,
- toLocations,
- toToolKind,
- } from "../../src/acp/tool"
- describe("acp tool conversion", () => {
- test("maps KirinCode tool ids to ACP tool kinds", () => {
- expect(toToolKind("bash")).toBe("execute")
- expect(toToolKind("shell")).toBe("execute")
- expect(toToolKind("webfetch")).toBe("fetch")
- expect(toToolKind("edit")).toBe("edit")
- expect(toToolKind("apply_patch")).toBe("edit")
- expect(toToolKind("patch")).toBe("edit")
- expect(toToolKind("write")).toBe("edit")
- expect(toToolKind("grep")).toBe("search")
- expect(toToolKind("glob")).toBe("search")
- expect(toToolKind("context7_resolve_library_id")).toBe("search")
- expect(toToolKind("context7_get_library_docs")).toBe("search")
- expect(toToolKind("read")).toBe("read")
- expect(toToolKind("task")).toBe("think")
- expect(toToolKind("custom_tool")).toBe("other")
- })
- test("extracts file locations from tool input", () => {
- expect(toLocations("read", { filePath: "/tmp/a.ts" })).toEqual([{ path: "/tmp/a.ts" }])
- expect(toLocations("edit", { filePath: "/tmp/b.ts" })).toEqual([{ path: "/tmp/b.ts" }])
- expect(toLocations("write", { filePath: "/tmp/c.ts" })).toEqual([{ path: "/tmp/c.ts" }])
- expect(toLocations("grep", { path: "/repo/src" })).toEqual([{ path: "/repo/src" }])
- expect(toLocations("glob", { path: "/repo/test" })).toEqual([{ path: "/repo/test" }])
- expect(toLocations("context7_get_library_docs", { path: "/docs" })).toEqual([{ path: "/docs" }])
- expect(toLocations("external_directory", { directories: ["/tmp/outside"], patterns: ["/tmp/outside/*"] })).toEqual([
- { path: "/tmp/outside" },
- ])
- expect(toLocations("bash", { cmd: "pwd" }, "/workspace")).toEqual([{ path: "/workspace" }])
- // Relative workdir resolves against cwd via the platform path resolver (backslashes on Windows).
- expect(toLocations("bash", { command: "pwd", workdir: "subdir" }, "/workspace")).toEqual([
- { path: resolve("/workspace", "subdir") },
- ])
- expect(toLocations("bash", { command: "pwd", workdir: "/abs/dir" }, "/workspace")).toEqual([{ path: "/abs/dir" }])
- expect(toLocations("bash", { command: "printf hello" })).toEqual([])
- expect(toLocations("read", { path: "/tmp/missing-file-path.ts" })).toEqual([])
- })
- test("builds completed content with text, edit diffs, and image attachments", () => {
- const image = Buffer.from("image-data").toString("base64")
- expect(
- completedToolContent("edit", {
- status: "completed",
- input: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- output: "edited /tmp/file.ts",
- attachments: [
- {
- type: "file",
- mime: "image/png",
- filename: "image.png",
- url: `data:image/png;base64,${image}`,
- },
- {
- type: "file",
- mime: "text/plain",
- filename: "note.txt",
- url: "data:text/plain;base64,bm90ZQ==",
- },
- ],
- }),
- ).toEqual([
- {
- type: "content",
- content: { type: "text", text: "edited /tmp/file.ts" },
- },
- {
- type: "diff",
- path: "/tmp/file.ts",
- oldText: "before",
- newText: "after",
- },
- {
- type: "content",
- content: { type: "image", mimeType: "image/png", data: image },
- },
- ])
- })
- test("omits edit diffs until old and new text fields exist", () => {
- expect(
- completedToolContent("write", {
- status: "completed",
- input: {
- filePath: "/tmp/file.ts",
- content: "created",
- },
- output: "wrote /tmp/file.ts",
- }),
- ).toEqual([
- {
- type: "content",
- content: { type: "text", text: "wrote /tmp/file.ts" },
- },
- ])
- })
- test("sends completed tool calls as partial updates", () => {
- expect(
- pendingToolCall({
- toolCallId: "tool-1",
- toolName: "edit",
- state: {
- input: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- },
- }),
- ).toMatchObject({
- kind: "edit",
- locations: [{ path: "/tmp/file.ts" }],
- rawInput: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- })
- expect(
- completedToolUpdate({
- toolCallId: "tool-1",
- toolName: "edit",
- state: {
- status: "completed",
- input: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- output: "Edit applied successfully.",
- },
- }),
- ).toEqual({
- toolCallId: "tool-1",
- status: "completed",
- content: [
- {
- type: "content",
- content: { type: "text", text: "Edit applied successfully." },
- },
- {
- type: "diff",
- path: "/tmp/file.ts",
- oldText: "before",
- newText: "after",
- },
- ],
- rawOutput: {
- output: "Edit applied successfully.",
- },
- })
- expect(
- completedToolUpdate({
- toolCallId: "tool-1",
- toolName: "edit",
- state: {
- status: "completed",
- input: {
- filePath: "/tmp/file.ts",
- oldString: "before",
- newString: "after",
- },
- title: "file.ts",
- output: "Edit applied successfully.",
- },
- }),
- ).toMatchObject({
- toolCallId: "tool-1",
- status: "completed",
- title: "file.ts",
- })
- })
- test("uses clean read display text for completed content", () => {
- const output = [
- "<path>/tmp/file.ts</path>",
- "<type>file</type>",
- "<content>",
- "7: first",
- "8: second",
- "",
- "(End of file - total 8 lines)",
- "</content>",
- ].join("\n")
- const state = {
- status: "completed" as const,
- input: { filePath: "/tmp/file.ts" },
- output,
- metadata: {
- display: {
- type: "file",
- path: "/tmp/file.ts",
- text: "first\nsecond",
- lineStart: 7,
- lineEnd: 8,
- totalLines: 8,
- truncated: false,
- },
- },
- }
- expect(completedToolContent("read", state)).toEqual([
- {
- type: "content",
- content: { type: "text", text: "first\nsecond" },
- },
- ])
- expect(completedToolRawOutput(state)).toEqual({
- output,
- metadata: state.metadata,
- })
- })
- test("builds completed raw output with optional metadata and attachments", () => {
- const attachments = [
- {
- type: "file",
- mime: "image/jpeg",
- filename: "photo.jpg",
- url: "data:image/jpeg;base64,AAAA",
- },
- ]
- expect(
- completedToolRawOutput({
- status: "completed",
- input: {},
- output: "done",
- metadata: { exit: 0 },
- attachments,
- }),
- ).toEqual({
- output: "done",
- metadata: { exit: 0 },
- attachments,
- })
- expect(
- completedToolRawOutput({
- status: "completed",
- input: {},
- output: "done",
- }),
- ).toEqual({ output: "done" })
- })
- test("extracts image attachments only from data URLs", () => {
- const attachments = [
- {
- mime: "image/webp",
- url: "data:image/webp;charset=utf-8;base64,AAAA",
- },
- {
- mime: "image/png",
- url: "https://example.com/image.png",
- },
- {
- mime: "text/plain",
- url: "data:text/plain;base64,BBBB",
- },
- ]
- expect(extractImageAttachments(attachments)).toEqual([{ mimeType: "image/webp", data: "AAAA" }])
- expect(imageContents(attachments)).toEqual([
- {
- type: "content",
- content: { type: "image", mimeType: "image/webp", data: "AAAA" },
- },
- ])
- })
- test("reads shell output snapshot from string metadata output", () => {
- expect(shellOutputSnapshot({ metadata: { output: "line 1\nline 2" } })).toBe("line 1\nline 2")
- expect(shellOutputSnapshot({ metadata: { output: 42 } })).toBeUndefined()
- expect(shellOutputSnapshot({ metadata: undefined })).toBeUndefined()
- })
- })
|