Repository navigation
Expand file tree
/
Copy pathworktree.test.ts
More file actions
340 lines (327 loc) · 15.7 KB
/
Copy pathworktree.test.ts
File metadata and controls
340 lines (327 loc) · 15.7 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import fs from "node:fs/promises"
import path from "node:path"
import { $ } from "bun"
import { expect } from "bun:test"
import { Effect } from "effect"
import { tmpdir } from "../../core/test/fixture/tmpdir"
import { it } from "../../core/test/lib/effect"
import { startServer } from "./fixture/server"
import { OpenCode } from "@opencode/client"
import { initRepo } from "../../core/test/fixture/git"
it.live("list reads saved inventory even when its checkout is missing, without booting a location", () =>
Effect.gen(function* () {
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-list-")))
const directory = path.join(tmp.path, "repo")
yield* Effect.promise(async () => {
await fs.mkdir(directory)
await initRepo(directory)
})
const server = yield* startServer(path.join(tmp.path, "config"))
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
yield* Effect.promise(async () => {
const session = await api.session.create({ location: { directory } })
const loaded = await api.debug.location.list()
await fs.rm(directory, { recursive: true })
expect(await api.worktree.list({ projectID: session.projectID })).toEqual([{ directory }])
await expect(api.worktree.create({ projectID: session.projectID })).rejects.toMatchObject({
name: "WorktreeError",
data: { message: `Worktree directory unavailable: ${directory}` },
})
await expect(api.worktree.refresh({ projectID: session.projectID })).rejects.toMatchObject({
name: "WorktreeError",
data: { message: `Worktree directory unavailable: ${directory}` },
})
expect(await api.debug.location.list()).toEqual(loaded)
const missing = await fetch(`${server.base}/api/worktree?projectID=unknown`, { headers: server.headers })
expect(missing.status).toBe(404)
const legacy = await fetch(`${server.base}/api/worktree?location[directory]=${directory}`, {
headers: server.headers,
})
expect(legacy.status).toBe(400)
})
}),
)
it.live("refresh discovers both clones while list alone never discovers external worktrees", () =>
Effect.gen(function* () {
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-clones-")))
const first = path.join(tmp.path, "first")
const second = path.join(tmp.path, "second")
const a = path.join(tmp.path, "a")
const b = path.join(tmp.path, "b")
yield* Effect.promise(async () => {
await fs.mkdir(first)
await initRepo(first)
await $`git remote add origin https://github.com/example/clones.git`.cwd(first).quiet()
await $`git clone --no-hardlinks ${first} ${second}`.quiet()
await $`git remote set-url origin https://github.com/example/clones.git`.cwd(second).quiet()
})
const server = yield* startServer(path.join(tmp.path, "config"))
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
yield* Effect.promise(async () => {
const session = await api.session.create({ location: { directory: first } })
const other = await api.session.create({ location: { directory: second } })
const projectID = session.projectID
expect(other.projectID).toBe(projectID)
await $`git worktree add --detach ${a} HEAD`.cwd(first).quiet()
await $`git worktree add --detach ${b} HEAD`.cwd(second).quiet()
expect(await api.worktree.list({ projectID })).toHaveLength(2)
await api.worktree.refresh({ projectID })
expect(await api.worktree.list({ projectID })).toEqual(
expect.arrayContaining([
{ directory: first },
{ directory: second },
{ directory: a, strategy: "git" },
{ directory: b, strategy: "git" },
]),
)
await fs.rm(second, { recursive: true })
await $`git worktree remove ${a}`.cwd(first).quiet()
await api.worktree.refresh({ projectID })
const rows = await api.worktree.list({ projectID })
expect(rows).not.toContainEqual({ directory: a, strategy: "git" })
expect(rows).not.toContainEqual({ directory: second })
expect(rows).toContainEqual({ directory: b, strategy: "git" })
})
}),
)
it.live("remove uses bundled Git without loading configuration and enforces project ownership", () =>
Effect.gen(function* () {
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-remove-")))
const directory = path.join(tmp.path, "repo")
const other = path.join(tmp.path, "other")
const linked = path.join(tmp.path, "linked")
yield* Effect.promise(async () => {
await fs.mkdir(directory)
await fs.mkdir(other)
await initRepo(directory)
await initRepo(other)
await $`git remote add origin https://github.com/example/remove.git`.cwd(directory).quiet()
await $`git remote add origin https://github.com/example/other.git`.cwd(other).quiet()
await $`git worktree add --detach ${linked} HEAD`.cwd(directory).quiet()
})
const server = yield* startServer(path.join(tmp.path, "config"))
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
yield* Effect.promise(async () => {
const session = await api.session.create({ location: { directory: linked } })
const foreign = await api.session.create({ location: { directory: other } })
const loaded = await api.debug.location.list()
await expect(
api.worktree.remove({ projectID: foreign.projectID, directory: linked, force: true }),
).rejects.toMatchObject({ name: "WorktreeError" })
expect(await fs.stat(linked).then((stat) => stat.isDirectory())).toBe(true)
await api.worktree.remove({ projectID: session.projectID, directory: linked, force: false })
expect(await api.debug.location.list()).toEqual(loaded)
expect(await api.worktree.list({ projectID: session.projectID })).toEqual([{ directory }])
})
}),
)
it.live(
"lists, creates, and removes worktrees by project ID",
() =>
Effect.gen(function* () {
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-endpoint-")))
const project = path.join(tmp.path, "project")
const destination = path.join(tmp.path, "worktrees")
yield* Effect.promise(() => fs.mkdir(project, { recursive: true }))
yield* Effect.promise(() => $`git init`.cwd(project).quiet())
yield* Effect.promise(() => $`git config user.email [email protected]`.cwd(project).quiet())
yield* Effect.promise(() => $`git config user.name Test`.cwd(project).quiet())
yield* Effect.promise(() => $`git commit --allow-empty -m root`.cwd(project).quiet())
const server = yield* startServer(path.join(tmp.path, "config"))
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
const session = yield* Effect.promise(() => api.session.create({ location: { directory: project } }))
const url = new URL("/api/worktree", server.base)
url.searchParams.set("projectID", session.projectID)
const initial = yield* Effect.promise(() =>
fetch(url, { headers: server.headers }).then((response) => response.json()),
)
expect(initial).toEqual([{ directory: project }])
const created = yield* Effect.promise(() =>
fetch(url, {
method: "POST",
headers: { ...server.headers, "content-type": "application/json" },
body: JSON.stringify({ projectID: session.projectID, directory: destination, name: "api" }),
}).then((response) => response.json()),
)
expect(created).toEqual({ directory: path.join(destination, "api") })
const listed = yield* Effect.promise(() =>
fetch(url, { headers: server.headers }).then((response) => response.json()),
)
expect(listed).toContainEqual({
directory: path.join(destination, "api"),
strategy: "git",
})
const removed = yield* Effect.promise(() =>
fetch(url, {
method: "DELETE",
headers: { ...server.headers, "content-type": "application/json" },
body: JSON.stringify({
projectID: session.projectID,
directory: path.join(destination, "api"),
force: false,
}),
}),
)
expect(removed.status).toBe(204)
}),
30_000,
)
it.live(
"uses project configuration independently of default location headers",
() =>
Effect.gen(function* () {
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-default-location-")))
const project = path.join(tmp.path, "project")
const config = path.join(tmp.path, "config")
const destination = path.join(tmp.path, "copies")
yield* Effect.promise(async () => {
await fs.mkdir(project)
await initRepo(project)
await fs.mkdir(config)
await Bun.write(path.join(config, "opencode.json"), JSON.stringify({ worktree: { directory: destination } }))
})
const server = yield* startServer(config)
const api = OpenCode.make({
baseUrl: server.base,
headers: { ...server.headers, "x-opencode-directory": encodeURIComponent("/unrelated-missing-directory") },
})
yield* Effect.promise(async () => {
const session = await api.session.create({ location: { directory: project } })
const projectID = session.projectID
const created = await api.worktree.create({ projectID })
expect(path.dirname(created.directory)).toBe(destination)
await api.worktree.refresh({ projectID })
expect(await api.worktree.list({ projectID })).toContainEqual({
directory: created.directory,
strategy: "git",
})
await api.worktree.remove({ projectID, directory: created.directory, force: false })
expect(await api.worktree.list({ projectID })).toEqual([{ directory: project }])
})
}),
30_000,
)
it.live(
"uses canonical plugins for shared clones and remove only uses already-loaded strategies",
() =>
Effect.gen(function* () {
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-plugins-")))
const first = path.join(tmp.path, "first")
const second = path.join(tmp.path, "second")
const nested = path.join(first, "nested")
const config = path.join(tmp.path, "config")
const destination = path.join(tmp.path, "worktrees")
yield* Effect.promise(async () => {
await fs.mkdir(first)
await initRepo(first)
await $`git remote add origin [email protected]:example/worktree-fixture.git`.cwd(first).quiet()
await $`git clone --no-hardlinks ${first} ${second}`.quiet()
await $`git remote set-url origin https://github.com/example/worktree-fixture.git`.cwd(second).quiet()
await fs.mkdir(nested)
await fs.mkdir(config)
await Bun.write(path.join(config, "opencode.json"), JSON.stringify({ worktree: { directory: destination } }))
await Bun.write(
path.join(first, "opencode.json"),
JSON.stringify({
plugins: [
{ package: path.join(import.meta.dir, "fixture/worktree-plugin"), options: { strategy: "test-copy" } },
],
}),
)
})
const server = yield* startServer(config)
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
yield* Effect.promise(async () => {
const a = await api.session.create({ location: { directory: nested } })
const b = await api.session.create({ location: { directory: second } })
expect(a.projectID).toBe(b.projectID)
const projectID = a.projectID
const custom = await api.worktree.create({ projectID, name: "custom" })
const builtin = await api.worktree.create({ projectID, from: second, name: "other-clone" })
expect(custom.directory).toBe(path.join(destination, "custom"))
expect(builtin.directory).toBe(path.join(destination, "other-clone"))
const otherRows = await api.worktree.list({ projectID: b.projectID })
expect(otherRows).toContainEqual({ directory: custom.directory, strategy: "test-copy" })
const rows = await api.worktree.list({ projectID })
expect(rows).toContainEqual({
directory: custom.directory,
strategy: "test-copy",
})
expect(rows).toContainEqual({ directory: builtin.directory, strategy: "test-copy" })
await Bun.write(path.join(custom.directory, "dirty.txt"), "keep me")
await api.debug.location.evict({ location: { directory: first } })
const loaded = await api.debug.location.list()
const remove = new URL("/api/worktree", server.base)
const unavailable = await fetch(remove, {
method: "DELETE",
headers: { ...server.headers, "content-type": "application/json" },
body: JSON.stringify({ projectID, directory: custom.directory, force: true }),
})
expect(unavailable.status).toBe(400)
expect(await unavailable.json()).toMatchObject({
data: { message: "Worktree strategy unavailable: test-copy" },
})
expect(await Bun.file(path.join(custom.directory, "dirty.txt")).text()).toBe("keep me")
expect(await api.worktree.list({ projectID })).toEqual(rows)
expect(await api.debug.location.list()).toEqual(loaded)
await api.worktree.refresh({ projectID })
const failure = await fetch(remove, {
method: "DELETE",
headers: { ...server.headers, "content-type": "application/json" },
body: JSON.stringify({ projectID, directory: custom.directory, force: false }),
})
expect(failure.status).toBe(400)
expect(await failure.json()).toMatchObject({ data: { forceRequired: true } })
expect(await Bun.file(path.join(custom.directory, "dirty.txt")).text()).toBe("keep me")
await api.worktree.remove({
projectID,
directory: custom.directory,
force: true,
})
await api.worktree.remove({
projectID,
directory: builtin.directory,
force: false,
})
expect((await api.worktree.list({ projectID })).filter((row) => row.strategy)).toEqual([])
})
}),
30_000,
)
it.live(
"canonical plugin setup can create using registrations made so far",
() =>
Effect.gen(function* () {
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-delegate-")))
const source = path.join(tmp.path, "source")
const destination = path.join(tmp.path, "copies")
yield* Effect.promise(async () => {
for (const directory of [source]) {
await fs.mkdir(directory)
await initRepo(directory)
await $`git remote add origin [email protected]:example/delegate-fixture.git`.cwd(directory).quiet()
}
await Bun.write(
path.join(source, "opencode.json"),
JSON.stringify({
worktree: { directory: destination },
plugins: [
{ package: path.join(import.meta.dir, "fixture/worktree-plugin"), options: { strategy: "target-copy" } },
{ package: path.join(import.meta.dir, "fixture/worktree-delegate") },
],
}),
)
})
const server = yield* startServer(path.join(tmp.path, "config"))
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
yield* Effect.promise(async () => {
const session = await api.session.create({ location: { directory: source } })
await api.worktree.refresh({ projectID: session.projectID })
expect(await api.worktree.list({ projectID: session.projectID })).toContainEqual({
directory: path.join(destination, "delegated"),
strategy: "target-copy",
})
})
}),
30_000,
)