forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcs.test.ts
More file actions
335 lines (283 loc) · 10.7 KB
/
Copy pathvcs.test.ts
File metadata and controls
335 lines (283 loc) · 10.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
import { afterEach, describe, expect } from "bun:test"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { FSUtil } from "@opencode-ai/core/fs-util"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { parsePatch } from "diff"
import { Deferred, Effect, Layer } from "effect"
import fs from "fs/promises"
import path from "path"
import {
disposeAllInstances,
provideInstance,
testInstanceStoreLayer,
TestInstance,
tmpdirScoped,
} from "../fixture/fixture"
import { EventV2Bridge } from "../../src/event-v2-bridge"
import { Watcher } from "@opencode-ai/core/filesystem/watcher"
import { Git } from "../../src/git"
import { Vcs } from "@/project/vcs"
import { testEffect } from "../lib/effect"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const weird = process.platform === "win32" ? "space file.txt" : "tab\tfile.txt"
const layer = LayerNode.compile(
LayerNode.group([Vcs.node, Git.node, EventV2Bridge.node, FSUtil.node, CrossSpawnSpawner.node]),
)
const it = testEffect(layer)
const worktreeIt = testEffect(Layer.mergeAll(layer, testInstanceStoreLayer))
const git = Effect.fn("VcsTest.git")(function* (cwd: string, args: string[]) {
const result = yield* Git.Service.use((git) => git.run(args, { cwd }))
if (result.exitCode !== 0) throw new Error(`git ${args.join(" ")} failed: ${result.stderr.toString("utf8")}`)
})
const write = Effect.fn("VcsTest.write")(function* (file: string, content: string) {
yield* FSUtil.Service.use((fs) => fs.writeWithDirs(file, content))
})
const remove = Effect.fn("VcsTest.remove")(function* (file: string) {
yield* FSUtil.Service.use((fs) => fs.remove(file))
})
const symlink = (target: string, file: string) => Effect.promise(() => fs.symlink(target, file))
const init = Effect.fn("VcsTest.init")(function* () {
const vcs = yield* Vcs.Service
yield* vcs.init()
return vcs
})
const nextBranchUpdate = Effect.fn("VcsTest.nextBranchUpdate")(function* () {
const events = yield* EventV2Bridge.Service
const updated = yield* Deferred.make<string | undefined>()
const off = yield* events.listen((event) => {
if (event.type === Vcs.Event.BranchUpdated.type)
Deferred.doneUnsafe(updated, Effect.succeed((event.data as typeof Vcs.Event.BranchUpdated.data.Type).branch))
return Effect.void
})
yield* Effect.addFinalizer(() => off)
return updated
})
const publishHeadChangeUntil = Effect.fn("VcsTest.publishHeadChangeUntil")(function* (
pending: Deferred.Deferred<string | undefined>,
head: string,
) {
const events = yield* EventV2Bridge.Service
for (let i = 0; i < 50; i++) {
yield* events.publish(Watcher.Event.Updated, { file: head, event: "change" })
if (yield* Deferred.isDone(pending)) return
yield* Effect.sleep("10 millis")
}
})
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("Vcs", () => {
afterEach(async () => {
await disposeAllInstances()
})
it.instance(
"branch() returns current branch name",
() =>
Effect.gen(function* () {
const vcs = yield* init()
const branch = yield* vcs.branch()
expect(branch).toBeDefined()
expect(typeof branch).toBe("string")
}),
{ git: true },
)
it.instance("branch() returns undefined for non-git directories", () =>
Effect.gen(function* () {
const vcs = yield* init()
const branch = yield* vcs.branch()
expect(branch).toBeUndefined()
}),
)
it.instance(
"publishes BranchUpdated when .git/HEAD changes",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const branch = `test-${Math.random().toString(36).slice(2)}`
yield* git(test.directory, ["branch", branch])
const vcs = yield* init()
yield* vcs.branch()
const pending = yield* nextBranchUpdate()
const head = path.join(test.directory, ".git", "HEAD")
yield* write(head, `ref: refs/heads/${branch}\n`)
yield* publishHeadChangeUntil(pending, head)
const updated = yield* Deferred.await(pending).pipe(Effect.timeout("2 seconds"))
expect(updated).toBe(branch)
}),
{ git: true },
)
it.instance(
"branch() reflects the new branch after HEAD change",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
const branch = `test-${Math.random().toString(36).slice(2)}`
yield* git(test.directory, ["branch", branch])
const vcs = yield* init()
yield* vcs.branch()
const pending = yield* nextBranchUpdate()
const head = path.join(test.directory, ".git", "HEAD")
yield* write(head, `ref: refs/heads/${branch}\n`)
yield* publishHeadChangeUntil(pending, head)
yield* Deferred.await(pending).pipe(Effect.timeout("2 seconds"))
const current = yield* vcs.branch()
expect(current).toBe(branch)
}),
{ git: true },
)
})
describe("Vcs diff", () => {
afterEach(async () => {
await disposeAllInstances()
})
it.instance(
"defaultBranch() falls back to main",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* git(test.directory, ["branch", "-M", "main"])
const vcs = yield* init()
const branch = yield* vcs.defaultBranch()
expect(branch).toBe("main")
}),
{ git: true },
)
it.instance(
"defaultBranch() uses init.defaultBranch when available",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* git(test.directory, ["branch", "-M", "trunk"])
yield* git(test.directory, ["config", "init.defaultBranch", "trunk"])
const vcs = yield* init()
const branch = yield* vcs.defaultBranch()
expect(branch).toBe("trunk")
}),
{ git: true },
)
worktreeIt.live("detects current branch from the active worktree", () =>
Effect.gen(function* () {
const tmp = yield* tmpdirScoped({ git: true })
const wt = yield* tmpdirScoped()
yield* git(tmp, ["branch", "-M", "main"])
const dir = path.join(wt, "feature")
yield* git(tmp, ["worktree", "add", "-b", "feature/test", dir, "HEAD"])
const [branch, base] = yield* Effect.gen(function* () {
const vcs = yield* init()
return yield* Effect.all([vcs.branch(), vcs.defaultBranch()], { concurrency: 2 })
}).pipe(provideInstance(dir))
expect(branch).toBeDefined()
expect(branch).toBe("feature/test")
expect(base).toBe("main")
}),
)
it.instance(
"diff('git') returns uncommitted changes",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* write(path.join(test.directory, "file.txt"), "original\n")
yield* git(test.directory, ["add", "."])
yield* git(test.directory, ["commit", "--no-gpg-sign", "-m", "add file"])
yield* write(path.join(test.directory, "file.txt"), "changed\n")
const vcs = yield* init()
const diff = yield* vcs.diff("git")
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: "file.txt",
status: "modified",
}),
]),
)
expect(diff.find((item) => item.file === "file.txt")?.patch).toContain("diff --git")
}),
{ git: true },
)
it.instance(
"diff('git') handles special filenames",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* write(path.join(test.directory, weird), "hello\n")
const vcs = yield* init()
const diff = yield* vcs.diff("git")
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: weird,
status: "added",
}),
]),
)
}),
{ git: true },
)
it.instance(
"diff('git') keeps batched patches aligned for type changes",
() =>
Effect.gen(function* () {
if (process.platform === "win32") return
const test = yield* TestInstance
yield* write(path.join(test.directory, "a.txt"), "old\n")
yield* write(path.join(test.directory, "b.txt"), "old\n")
yield* git(test.directory, ["add", "."])
yield* git(test.directory, ["commit", "--no-gpg-sign", "-m", "add files"])
yield* remove(path.join(test.directory, "a.txt"))
yield* symlink("target", path.join(test.directory, "a.txt"))
yield* write(path.join(test.directory, "b.txt"), "new\n")
const vcs = yield* init()
const diff = yield* vcs.diff("git")
const a = diff.find((item) => item.file === "a.txt")
const b = diff.find((item) => item.file === "b.txt")
expect(a?.patch).toContain("deleted file mode")
expect(a?.patch).toContain("new file mode")
expect(b?.patch).toContain("+new")
}),
{ git: true },
)
it.instance(
"diff('git') keeps carriage returns inside patch hunks",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* write(path.join(test.directory, "file.txt"), "keep\nsame\rdiff --git inside\ndelete\n")
yield* git(test.directory, ["add", "."])
yield* git(test.directory, ["commit", "--no-gpg-sign", "-m", "add file"])
yield* write(path.join(test.directory, "file.txt"), "keep\nadd\nsame\rdiff --git inside\n")
const vcs = yield* init()
const diff = yield* vcs.diff("git")
const file = diff.find((item) => item.file === "file.txt")
expect(file?.patch).toContain(" same\rdiff --git inside")
expect(file?.patch).toContain("-delete")
expect(() => parsePatch(file?.patch ?? "")).not.toThrow()
}),
{ git: true },
20_000,
)
it.instance(
"diff('branch') returns changes against default branch",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* git(test.directory, ["branch", "-M", "main"])
yield* git(test.directory, ["checkout", "-b", "feature/test"])
yield* write(path.join(test.directory, "branch.txt"), "hello\n")
yield* git(test.directory, ["add", "."])
yield* git(test.directory, ["commit", "--no-gpg-sign", "-m", "branch file"])
const vcs = yield* init()
const diff = yield* vcs.diff("branch")
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: "branch.txt",
status: "added",
}),
]),
)
}),
{ git: true },
)
})