forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree.test.ts
More file actions
324 lines (279 loc) · 11.1 KB
/
Copy pathworktree.test.ts
File metadata and controls
324 lines (279 loc) · 11.1 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
import { afterEach, describe, expect } from "bun:test"
import path from "path"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { FSUtil } from "@opencode-ai/core/fs-util"
import { Cause, Deferred, Effect, Exit, Fiber } from "effect"
import { GlobalBus, type GlobalEvent } from "../../src/bus/global"
import { Git } from "../../src/git"
import { InstanceBootstrap } from "../../src/project/bootstrap"
import { InstanceStore } from "../../src/project/instance-store"
import { Worktree } from "../../src/worktree"
import { disposeAllInstances, provideInstance, TestInstance } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
const it = testEffect(
LayerNode.compile(LayerNode.group([Worktree.node, FSUtil.node, Git.node]), [
[InstanceStore.bootstrapNode, InstanceBootstrap.node],
]),
)
const wintest = process.platform !== "win32" ? it.instance : it.instance.skip
function normalize(input: string) {
return input.replace(/\\/g, "/").toLowerCase()
}
const waitReady = Effect.fn("WorktreeTest.waitReady")(function* () {
const ready = yield* Deferred.make<{ name: string; branch?: string }>()
const on = (evt: GlobalEvent) => {
if (evt.payload.type !== Worktree.Event.Ready.type) return
Deferred.doneUnsafe(ready, Effect.succeed(evt.payload.properties))
}
GlobalBus.on("event", on)
yield* Effect.addFinalizer(() => Effect.sync(() => GlobalBus.off("event", on)))
return yield* Deferred.await(ready).pipe(
Effect.timeoutOrElse({
duration: "10 seconds",
orElse: () => Effect.fail(new Error("timed out waiting for worktree.ready")),
}),
)
})
const removeCreatedWorktree = (directory: string) =>
Effect.gen(function* () {
const svc = yield* Worktree.Service
const ok = yield* svc.remove({ directory })
if (!ok) return yield* Effect.fail(new Error(`failed to remove worktree ${directory}`))
})
const withCreatedWorktree = <A, E, R>(
input: Parameters<Worktree.Interface["create"]>[0],
use: (created: { info: Worktree.Info; ready: { name: string; branch?: string } }) => Effect.Effect<A, E, R>,
) =>
Effect.acquireUseRelease(
Effect.gen(function* () {
const svc = yield* Worktree.Service
const ready = yield* waitReady().pipe(Effect.forkScoped)
const info = yield* svc.create(input)
const props = yield* Fiber.join(ready)
return { info, ready: props }
}),
use,
({ info }) => removeCreatedWorktree(info.directory),
)
const git = Effect.fn("WorktreeTest.git")(function* (cwd: string, args: string[]) {
const service = yield* Git.Service
const result = yield* service.run(args, { cwd })
if (result.exitCode !== 0) throw new Error(`git ${args.join(" ")} failed: ${result.stderr.toString("utf8")}`)
return result.text()
})
const gitResult = Effect.fn("WorktreeTest.gitResult")(function* (cwd: string, args: string[]) {
const service = yield* Git.Service
return yield* service.run(args, { cwd })
})
describe("Worktree", () => {
afterEach(() => disposeAllInstances())
describe("makeWorktreeInfo", () => {
it.instance(
"returns info with name, branch, and directory",
() =>
Effect.gen(function* () {
const svc = yield* Worktree.Service
const info = yield* svc.makeWorktreeInfo()
expect(info.name).toBeDefined()
expect(typeof info.name).toBe("string")
expect(info.branch).toBe(`opencode/${info.name}`)
expect(info.directory).toContain(info.name)
}),
{ git: true },
)
it.instance(
"uses provided name as base",
() =>
Effect.gen(function* () {
const svc = yield* Worktree.Service
const info = yield* svc.makeWorktreeInfo({ name: "my-feature" })
expect(info.name).toBe("my-feature")
expect(info.branch).toBe("opencode/my-feature")
}),
{ git: true },
)
it.instance(
"slugifies the provided name",
() =>
Effect.gen(function* () {
const svc = yield* Worktree.Service
const info = yield* svc.makeWorktreeInfo({ name: "My Feature Branch!" })
expect(info.name).toBe("my-feature-branch")
}),
{ git: true },
)
it.instance(
"omits branch for detached info",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const svc = yield* Worktree.Service
yield* git(test.directory, ["branch", "opencode/my-feature"])
const info = yield* svc.makeWorktreeInfo({ name: "my-feature", detached: true })
expect(info.name).toBe("my-feature")
expect(info.branch).toBeUndefined()
}),
{ git: true },
)
it.instance("fails with NotGitError for non-git directories", () =>
Effect.gen(function* () {
const svc = yield* Worktree.Service
const exit = yield* Effect.exit(svc.makeWorktreeInfo())
expect(Exit.isFailure(exit)).toBe(true)
if (Exit.isFailure(exit)) {
const error = Cause.squash(exit.cause)
expect(error).toBeInstanceOf(Worktree.NotGitError)
if (error instanceof Worktree.NotGitError) expect(error._tag).toBe("WorktreeNotGitError")
}
}),
)
wintest(
"creates detached git worktree when info has no branch",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const svc = yield* Worktree.Service
const info = yield* svc.makeWorktreeInfo({ name: "detached-test", detached: true })
const ready = yield* waitReady().pipe(Effect.forkScoped)
yield* svc.createFromInfo(info)
const list = yield* git(test.directory, ["worktree", "list", "--porcelain"])
const normalizedList = normalize(list)
const normalizedDir = normalize(info.directory)
expect(normalizedList).toContain(normalizedDir)
const branch = yield* gitResult(info.directory, ["symbolic-ref", "-q", "--short", "HEAD"])
expect(branch.exitCode).not.toBe(0)
const props = yield* Fiber.join(ready)
expect(props.name).toBe(info.name)
expect(props.branch).toBeUndefined()
yield* svc.remove({ directory: info.directory })
}),
{ git: true },
)
})
describe("create + remove lifecycle", () => {
it.instance(
"create returns worktree info and remove cleans up",
() =>
withCreatedWorktree(undefined, ({ info }) =>
Effect.gen(function* () {
expect(info.name).toBeDefined()
expect(info.branch ?? "").toStartWith("opencode/")
expect(info.directory).toBeDefined()
}),
),
{ git: true },
)
it.instance(
"create returns after setup and fires Event.Ready after bootstrap",
() =>
withCreatedWorktree(undefined, ({ info, ready }) =>
Effect.gen(function* () {
const svc = yield* Worktree.Service
expect(info.name).toBeDefined()
expect(info.branch ?? "").toStartWith("opencode/")
expect(ready.name).toBe(info.name)
expect(ready.branch).toBe(info.branch)
const list = yield* svc.list()
expect(list).toContainEqual(expect.objectContaining({ name: info.name, branch: info.branch }))
}),
),
{ git: true },
)
it.instance(
"lists the active linked worktree but not the project checkout",
() =>
withCreatedWorktree(undefined, ({ info }) =>
Effect.gen(function* () {
const test = yield* TestInstance
const svc = yield* Worktree.Service
const list = yield* svc.list().pipe(provideInstance(info.directory))
expect(list.map((item) => item.name)).toContain(info.name)
expect(list.map((item) => item.name)).not.toContain(path.basename(test.directory).toLowerCase())
}),
),
{ git: true },
)
it.instance(
"create with custom name",
() =>
withCreatedWorktree({ name: "test-workspace" }, ({ info }) =>
Effect.gen(function* () {
expect(info.name).toBe("test-workspace")
expect(info.branch).toBe("opencode/test-workspace")
}),
),
{ git: true },
)
})
describe("createFromInfo", () => {
wintest(
"creates git worktree and boots asynchronously",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const svc = yield* Worktree.Service
const info = yield* svc.makeWorktreeInfo({ name: "from-info-test" })
const ready = yield* waitReady().pipe(Effect.forkScoped)
yield* svc.createFromInfo(info)
const list = yield* git(test.directory, ["worktree", "list", "--porcelain"])
const normalizedList = list.replace(/\\/g, "/")
const normalizedDir = info.directory.replace(/\\/g, "/")
expect(normalizedList).toContain(normalizedDir)
yield* Fiber.join(ready)
yield* removeCreatedWorktree(info.directory)
}),
{ git: true },
)
})
describe("list", () => {
it.instance(
"uses parent folder name when worktree basename matches the primary worktree",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const fs = yield* FSUtil.Service
const svc = yield* Worktree.Service
const parent = path.join(path.dirname(test.directory), `${path.basename(test.directory)}-parent`)
const target = path.join(parent, path.basename(test.directory))
const branch = `same-basename-list-${Date.now()}`
yield* fs.ensureDir(parent)
yield* git(test.directory, ["worktree", "add", "-b", branch, target])
const list = yield* svc.list()
const directory = yield* fs.realPath(target).pipe(Effect.catch(() => Effect.succeed(target)))
expect(list.map((item) => ({ ...item, directory: normalize(item.directory) }))).toContainEqual({
name: path.basename(parent),
branch,
directory: normalize(directory),
})
yield* svc.remove({ directory: target })
}),
{ git: true },
)
})
describe("remove edge cases", () => {
it.instance(
"remove non-existent directory succeeds silently",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const svc = yield* Worktree.Service
const ok = yield* svc.remove({ directory: path.join(test.directory, "does-not-exist") })
expect(ok).toBe(true)
}),
{ git: true },
)
it.instance("fails with NotGitError for non-git directories", () =>
Effect.gen(function* () {
const test = yield* TestInstance
const svc = yield* Worktree.Service
const exit = yield* Effect.exit(svc.remove({ directory: path.join(test.directory, "fake") }))
expect(Exit.isFailure(exit)).toBe(true)
if (Exit.isFailure(exit)) {
const error = Cause.squash(exit.cause)
expect(error).toBeInstanceOf(Worktree.NotGitError)
if (error instanceof Worktree.NotGitError) expect(error._tag).toBe("WorktreeNotGitError")
}
}),
)
})
})