-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathcache.test.ts
More file actions
269 lines (208 loc) · 8.59 KB
/
Copy pathcache.test.ts
File metadata and controls
269 lines (208 loc) · 8.59 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { test, expect, beforeEach, beforeAll, afterAll, describe } from "bun:test"
import { TreeSitterClient, addDefaultParsers } from "./client"
import { tmpdir } from "os"
import { join, resolve } from "path"
import { mkdir, readdir, stat } from "fs/promises"
import type { FiletypeParserOptions } from "./types"
describe("TreeSitterClient Caching", () => {
let dataPath: string
let testServer: any
const TEST_PORT = 55231
const BASE_URL = `http://localhost:${TEST_PORT}`
beforeAll(async () => {
const assetsDir = resolve(__dirname, "assets")
testServer = Bun.serve({
port: TEST_PORT,
fetch(req) {
const url = new URL(req.url)
const filePath = join(assetsDir, url.pathname)
return new Response(Bun.file(filePath))
},
})
})
afterAll(async () => {
if (testServer) {
testServer.stop()
}
})
beforeEach(async () => {
dataPath = join(tmpdir(), "tree-sitter-cache-test-" + Math.random().toString(36).slice(2))
await mkdir(dataPath, { recursive: true })
})
test("should create storage directories on initialization", async () => {
const client = new TreeSitterClient({ dataPath })
await client.initialize()
const languagesDir = join(dataPath, "tree-sitter", "languages")
const queriesDir = join(dataPath, "tree-sitter", "queries")
const languagesStat = await stat(languagesDir)
const queriesStat = await stat(queriesDir)
expect(languagesStat.isDirectory()).toBe(true)
expect(queriesStat.isDirectory()).toBe(true)
await client.destroy()
})
test("should cache downloaded language files", async () => {
const client = new TreeSitterClient({ dataPath })
await client.initialize()
// Add URL-based parser for this test
client.addFiletypeParser({
filetype: "javascript",
queries: {
highlights: [`${BASE_URL}/javascript/highlights.scm`],
},
wasm: `${BASE_URL}/javascript/tree-sitter-javascript.wasm`,
})
const hasParser = await client.preloadParser("javascript")
expect(hasParser).toBe(true)
const languagesDir = join(dataPath, "tree-sitter", "languages")
const cachedFiles = await readdir(languagesDir)
expect(cachedFiles).toContain("tree-sitter-javascript.wasm")
await client.destroy()
})
test("should cache downloaded highlight queries", async () => {
const client = new TreeSitterClient({ dataPath })
await client.initialize()
// Add URL-based parser for this test
client.addFiletypeParser({
filetype: "javascript",
queries: {
highlights: [`${BASE_URL}/javascript/highlights.scm`],
},
wasm: `${BASE_URL}/javascript/tree-sitter-javascript.wasm`,
})
const hasParser = await client.preloadParser("javascript")
expect(hasParser).toBe(true)
const queriesDir = join(dataPath, "tree-sitter", "queries")
const cachedQueries = await readdir(queriesDir)
const scmFiles = cachedQueries.filter((file) => file.endsWith(".scm"))
expect(scmFiles.length).toBeGreaterThan(0)
await client.destroy()
})
// TODO: This is flaky, there must be a more reliable way to test this
test.skip("should reuse cached files across client instances", async () => {
const jsParser: FiletypeParserOptions = {
filetype: "javascript",
queries: {
highlights: [`${BASE_URL}/javascript/highlights.scm`],
},
wasm: `${BASE_URL}/javascript/tree-sitter-javascript.wasm`,
}
let client1 = new TreeSitterClient({ dataPath })
await client1.initialize()
client1.addFiletypeParser(jsParser)
console.log("=== First client (should download) ===")
const start1 = Date.now()
const hasParser1 = await client1.preloadParser("javascript")
const duration1 = Date.now() - start1
expect(hasParser1).toBe(true)
await client1.destroy()
let client2 = new TreeSitterClient({ dataPath })
await client2.initialize()
client2.addFiletypeParser(jsParser)
console.log("=== Second client (should use cache) ===")
const start2 = Date.now()
const hasParser2 = await client2.preloadParser("javascript")
const duration2 = Date.now() - start2
expect(hasParser2).toBe(true)
console.log(`First client: ${duration1}ms, Second client: ${duration2}ms`)
expect(duration2).toBeLessThanOrEqual(duration1)
expect(duration2).toBeLessThan(100) // Should be very fast with cache
await client2.destroy()
})
test("should handle multiple parsers with independent caching", async () => {
const client = new TreeSitterClient({ dataPath })
await client.initialize()
// Add URL-based parsers for this test
client.addFiletypeParser({
filetype: "javascript",
queries: {
highlights: [`${BASE_URL}/javascript/highlights.scm`],
},
wasm: `${BASE_URL}/javascript/tree-sitter-javascript.wasm`,
})
client.addFiletypeParser({
filetype: "typescript",
queries: {
highlights: [`${BASE_URL}/typescript/highlights.scm`],
},
wasm: `${BASE_URL}/typescript/tree-sitter-typescript.wasm`,
})
const hasJS = await client.preloadParser("javascript")
const hasTS = await client.preloadParser("typescript")
expect(hasJS).toBe(true)
expect(hasTS).toBe(true)
const languagesDir = join(dataPath, "tree-sitter", "languages")
const cachedFiles = await readdir(languagesDir)
expect(cachedFiles).toContain("tree-sitter-javascript.wasm")
expect(cachedFiles).toContain("tree-sitter-typescript.wasm")
const queriesDir = join(dataPath, "tree-sitter", "queries")
const cachedQueries = await readdir(queriesDir)
const scmFiles = cachedQueries.filter((file) => file.endsWith(".scm"))
expect(scmFiles.length).toBe(2)
await client.destroy()
})
test("should store files in dataPath subdirectories", async () => {
const client = new TreeSitterClient({ dataPath })
await client.initialize()
// Add URL-based parser for this test
client.addFiletypeParser({
filetype: "javascript",
queries: {
highlights: [`${BASE_URL}/javascript/highlights.scm`],
},
wasm: `${BASE_URL}/javascript/tree-sitter-javascript.wasm`,
})
const hasParser = await client.preloadParser("javascript")
expect(hasParser).toBe(true)
const languagesDir = join(dataPath, "tree-sitter", "languages")
const queriesDir = join(dataPath, "tree-sitter", "queries")
const languagesStat = await stat(languagesDir)
const queriesStat = await stat(queriesDir)
expect(languagesStat.isDirectory()).toBe(true)
expect(queriesStat.isDirectory()).toBe(true)
const cachedFiles = await readdir(languagesDir)
expect(cachedFiles).toContain("tree-sitter-javascript.wasm")
await client.destroy()
})
test("should handle directory creation errors gracefully", async () => {
const invalidDataPath = "/invalid\x00/path/with/null/byte"
const client = new TreeSitterClient({ dataPath: invalidDataPath })
await expect(client.initialize()).rejects.toThrow()
await client.destroy()
})
test("should handle data path changes", async () => {
const initialDataPath = join(tmpdir(), "tree-sitter-initial-" + Math.random().toString(36).slice(2))
const newDataPath = join(tmpdir(), "tree-sitter-new-" + Math.random().toString(36).slice(2))
await mkdir(initialDataPath, { recursive: true })
await mkdir(newDataPath, { recursive: true })
const client = new TreeSitterClient({ dataPath: initialDataPath })
await client.initialize()
// Add URL-based parsers for this test
client.addFiletypeParser({
filetype: "javascript",
queries: {
highlights: [`${BASE_URL}/javascript/highlights.scm`],
},
wasm: `${BASE_URL}/javascript/tree-sitter-javascript.wasm`,
})
const hasParser1 = await client.preloadParser("javascript")
expect(hasParser1).toBe(true)
const initialLanguagesDir = join(initialDataPath, "tree-sitter", "languages")
const initialFiles = await readdir(initialLanguagesDir)
expect(initialFiles).toContain("tree-sitter-javascript.wasm")
await client.setDataPath(newDataPath)
// Add typescript parser for the new data path
client.addFiletypeParser({
filetype: "typescript",
queries: {
highlights: [`${BASE_URL}/typescript/highlights.scm`],
},
wasm: `${BASE_URL}/typescript/tree-sitter-typescript.wasm`,
})
const hasParser2 = await client.preloadParser("typescript")
expect(hasParser2).toBe(true)
const newLanguagesDir = join(newDataPath, "tree-sitter", "languages")
const newFiles = await readdir(newLanguagesDir)
expect(newFiles).toContain("tree-sitter-typescript.wasm")
await client.destroy()
})
})