-
Notifications
You must be signed in to change notification settings - Fork 17.6k
Expand file tree
/
Copy pathexperimental-tools.test.ts
More file actions
110 lines (92 loc) · 3.54 KB
/
experimental-tools.test.ts
File metadata and controls
110 lines (92 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { describe, expect, test } from "bun:test"
import { Hono } from "hono"
import { ExperimentalRoutes } from "../../src/server/routes/experimental"
import { Instance } from "../../src/project/instance"
import { Session } from "../../src/session"
import { Log } from "../../src/util/log"
import { tmpdir } from "../fixture/fixture"
Log.init({ print: false })
function route() {
return new Hono().route("/experimental", ExperimentalRoutes())
}
async function listTools(query: Record<string, string>) {
const qs = new URLSearchParams(query)
const res = await route().request(`/experimental/tool?${qs.toString()}`)
expect(res.status).toBe(200)
return (await res.json()) as Array<{ id: string }>
}
async function listToolIDs(query: Record<string, string>) {
const qs = new URLSearchParams(query)
const suffix = qs.toString()
const res = await route().request(`/experimental/tool/ids${suffix ? `?${suffix}` : ""}`)
expect(res.status).toBe(200)
return (await res.json()) as string[]
}
describe("ExperimentalRoutes /experimental/tool", () => {
test("applies agent permissions when listing visible tools", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const buildTools = await listTools({ provider: "openai", model: "qwen-plus", agent: "build" })
const exploreTools = await listTools({ provider: "openai", model: "qwen-plus", agent: "explore" })
expect(buildTools.some((tool) => tool.id === "edit")).toBe(true)
expect(exploreTools.some((tool) => tool.id === "edit")).toBe(false)
},
})
})
test("applies session permissions when sessionID is provided", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const session = await Session.create({})
await Session.setPermission({
sessionID: session.id,
permission: [{ permission: "bash", pattern: "*", action: "deny" }],
})
const withSession = await listTools({
provider: "openai",
model: "qwen-plus",
agent: "build",
sessionID: session.id,
})
expect(withSession.some((tool) => tool.id === "bash")).toBe(false)
},
})
})
})
describe("ExperimentalRoutes /experimental/tool/ids", () => {
test("applies agent permissions when listing visible tool IDs", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const buildToolIDs = await listToolIDs({ provider: "openai", model: "qwen-plus", agent: "build" })
const exploreToolIDs = await listToolIDs({ provider: "openai", model: "qwen-plus", agent: "explore" })
expect(buildToolIDs.includes("edit")).toBe(true)
expect(exploreToolIDs.includes("edit")).toBe(false)
},
})
})
test("applies session permissions to tool IDs when sessionID is provided", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const session = await Session.create({})
await Session.setPermission({
sessionID: session.id,
permission: [{ permission: "bash", pattern: "*", action: "deny" }],
})
const withSession = await listToolIDs({
provider: "openai",
model: "qwen-plus",
agent: "build",
sessionID: session.id,
})
expect(withSession.includes("bash")).toBe(false)
},
})
})
})