-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathglob.test.ts
More file actions
135 lines (127 loc) · 4.21 KB
/
glob.test.ts
File metadata and controls
135 lines (127 loc) · 4.21 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { describe, expect, test } from "bun:test"
import path from "path"
import fs from "fs/promises"
import { GlobTool } from "../../src/tool/glob"
import { Instance } from "../../src/project/instance"
import { tmpdir } from "../fixture/fixture"
import { SessionID, MessageID } from "../../src/session/schema"
const ctx = {
sessionID: SessionID.make("ses_test"),
messageID: MessageID.make(""),
callID: "",
agent: "build",
abort: AbortSignal.any([]),
messages: [],
metadata: () => {},
ask: async () => {},
}
describe("tool.glob", () => {
test("finds files matching pattern", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "a.txt"), "hello")
await Bun.write(path.join(dir, "b.txt"), "world")
await Bun.write(path.join(dir, "c.md"), "readme")
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const glob = await GlobTool.init()
const result = await glob.execute({ pattern: "*.txt" }, ctx)
expect(result.metadata.count).toBe(2)
expect(result.output).toContain("a.txt")
expect(result.output).toContain("b.txt")
expect(result.output).not.toContain("c.md")
},
})
})
test("returns 'No files found' when no matches", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const glob = await GlobTool.init()
const result = await glob.execute({ pattern: "*.nonexistent" }, ctx)
expect(result.metadata.count).toBe(0)
expect(result.output).toBe("No files found")
},
})
})
test("truncates results at 100 files", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
for (let i = 0; i < 110; i++) {
await Bun.write(path.join(dir, `file${String(i).padStart(3, "0")}.txt`), "")
}
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const glob = await GlobTool.init()
const result = await glob.execute({ pattern: "*.txt" }, ctx)
expect(result.metadata.count).toBe(100)
expect(result.metadata.truncated).toBe(true)
expect(result.output).toContain("Results are truncated")
},
})
})
test("respects user abort signal", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "a.txt"), "hello")
},
})
const controller = new AbortController()
controller.abort()
const abortCtx = { ...ctx, abort: controller.signal }
await Instance.provide({
directory: tmp.path,
fn: async () => {
const glob = await GlobTool.init()
await expect(glob.execute({ pattern: "*.txt" }, abortCtx)).rejects.toThrow()
},
})
})
test("finds nested files with ** pattern", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await fs.mkdir(path.join(dir, "sub", "deep"), { recursive: true })
await Bun.write(path.join(dir, "sub", "deep", "target.yml"), "")
await Bun.write(path.join(dir, "other.txt"), "")
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const glob = await GlobTool.init()
const result = await glob.execute({ pattern: "**/target.yml" }, ctx)
expect(result.metadata.count).toBe(1)
expect(result.output).toContain("target.yml")
},
})
})
test("uses custom path parameter", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await fs.mkdir(path.join(dir, "subdir"))
await Bun.write(path.join(dir, "subdir", "inner.txt"), "")
await Bun.write(path.join(dir, "outer.txt"), "")
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const glob = await GlobTool.init()
const result = await glob.execute(
{ pattern: "*.txt", path: path.join(tmp.path, "subdir") },
ctx,
)
expect(result.metadata.count).toBe(1)
expect(result.output).toContain("inner.txt")
expect(result.output).not.toContain("outer.txt")
},
})
})
})