opencode_offline/packages/opencode/test/tool/tool.test.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-06-01 02:41:00 +08:00
import { describe, expect, test } from "bun:test"
import { App } from "../../src/app/app"
2025-06-01 05:12:16 +08:00
import { GlobTool } from "../../src/tool/glob"
import { ListTool } from "../../src/tool/ls"
import path from "path"
2025-05-21 22:30:39 +08:00
2025-06-19 01:40:21 +08:00
const ctx = {
sessionID: "test",
messageID: "",
2025-07-31 23:10:34 +08:00
toolCallID: "",
2025-06-19 01:40:21 +08:00
abort: AbortSignal.any([]),
metadata: () => {},
}
const glob = await GlobTool.init()
const list = await ListTool.init()
const projectRoot = path.join(__dirname, "../..")
const fixturePath = path.join(__dirname, "../fixtures/example")
2025-05-21 22:30:39 +08:00
describe("tool.glob", () => {
test("truncate", async () => {
await App.provide({ cwd: projectRoot }, async () => {
let result = await glob.execute(
{
pattern: "**/*",
path: "../../node_modules",
},
2025-06-19 01:40:21 +08:00
ctx,
)
2025-06-01 02:41:00 +08:00
expect(result.metadata.truncated).toBe(true)
})
})
2025-05-21 22:30:39 +08:00
test("basic", async () => {
await App.provide({ cwd: projectRoot }, async () => {
let result = await glob.execute(
{
pattern: "*.json",
2025-06-17 23:27:07 +08:00
path: undefined,
},
2025-06-19 01:40:21 +08:00
ctx,
)
2025-05-21 22:30:39 +08:00
expect(result.metadata).toMatchObject({
truncated: false,
count: 2,
2025-06-01 02:41:00 +08:00
})
})
})
})
2025-05-27 02:09:17 +08:00
describe("tool.ls", () => {
test("basic", async () => {
const result = await App.provide({ cwd: projectRoot }, async () => {
return await list.execute({ path: fixturePath, ignore: [".git"] }, ctx)
})
// Normalize absolute path to relative for consistent snapshots
const normalizedOutput = result.output.replace(fixturePath, "packages/opencode/test/fixtures/example")
expect(normalizedOutput).toMatchSnapshot()
2025-06-01 02:41:00 +08:00
})
})