forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.test.ts
More file actions
239 lines (200 loc) · 7.76 KB
/
instance.test.ts
File metadata and controls
239 lines (200 loc) · 7.76 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
import { afterEach, describe, expect } from "bun:test"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { Effect, Fiber, Layer } from "effect"
import { InstanceRef } from "../../src/effect/instance-ref"
import { registerDisposer } from "../../src/effect/instance-registry"
import { InstanceBootstrap } from "../../src/project/bootstrap-service"
import { Instance } from "../../src/project/instance"
import { WithInstance } from "../../src/project/with-instance"
import { InstanceStore } from "../../src/project/instance-store"
import { disposeAllInstances, tmpdirScoped } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
let bootstrapRun: Effect.Effect<void> = Effect.void
const noopBootstrap = Layer.succeed(
InstanceBootstrap.Service,
InstanceBootstrap.Service.of({ run: Effect.suspend(() => bootstrapRun) }),
)
const it = testEffect(
Layer.mergeAll(InstanceStore.defaultLayer, CrossSpawnSpawner.defaultLayer).pipe(Layer.provide(noopBootstrap)),
)
afterEach(async () => {
bootstrapRun = Effect.void
await disposeAllInstances()
})
describe("InstanceStore", () => {
it.live("loads instance context without installing ALS for the caller", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
const ctx = yield* store.load({ directory: dir })
expect(ctx.directory).toBe(dir)
expect(ctx.worktree).toBe(dir)
expect(() => Instance.current).toThrow()
}),
)
it.live("runs bootstrap with InstanceRef provided", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
let initializedDirectory: string | undefined
bootstrapRun = Effect.gen(function* () {
initializedDirectory = (yield* InstanceRef)?.directory
})
yield* store.load({ directory: dir })
expect(initializedDirectory).toBe(dir)
expect(() => Instance.current).toThrow()
}),
)
it.live("caches loaded instance context by directory", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
let initialized = 0
bootstrapRun = Effect.sync(() => {
initialized++
})
const first = yield* store.load({ directory: dir })
const second = yield* store.load({ directory: dir })
expect(second).toBe(first)
expect(initialized).toBe(1)
}),
)
it.live("dedupes concurrent loads while init is in flight", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
const started = Promise.withResolvers<void>()
const release = Promise.withResolvers<void>()
let initialized = 0
bootstrapRun = Effect.promise(async () => {
initialized++
started.resolve()
await release.promise
})
const first = yield* store.load({ directory: dir }).pipe(Effect.forkScoped)
yield* Effect.promise(() => started.promise)
bootstrapRun = Effect.sync(() => {
initialized++
})
const second = yield* store.load({ directory: dir }).pipe(Effect.forkScoped)
expect(initialized).toBe(1)
release.resolve()
const [firstCtx, secondCtx] = yield* Effect.all([Fiber.join(first), Fiber.join(second)])
expect(secondCtx).toBe(firstCtx)
expect(initialized).toBe(1)
}),
)
it.live("removes failed loads from the cache", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
let attempts = 0
bootstrapRun = Effect.sync(() => {
attempts++
throw new Error("init failed")
})
const failed = yield* store.load({ directory: dir }).pipe(
Effect.as(false),
Effect.catchCause(() => Effect.succeed(true)),
)
expect(failed).toBe(true)
bootstrapRun = Effect.sync(() => {
attempts++
})
const ctx = yield* store.load({ directory: dir })
expect(ctx.directory).toBe(dir)
expect(attempts).toBe(2)
}),
)
it.live("reload replaces the cached context", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
const first = yield* store.load({ directory: dir })
const second = yield* store.reload({ directory: dir })
const cached = yield* store.load({ directory: dir })
expect(second).not.toBe(first)
expect(cached).toBe(second)
}),
)
it.live("stale dispose does not delete an in-flight reload", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
const reloading = Promise.withResolvers<void>()
const releaseReload = Promise.withResolvers<void>()
const disposed: Array<string> = []
const off = registerDisposer(async (directory) => {
disposed.push(directory)
})
yield* Effect.addFinalizer(() => Effect.sync(off))
const first = yield* store.load({ directory: dir })
bootstrapRun = Effect.promise(async () => {
reloading.resolve()
await releaseReload.promise
})
const reload = yield* store.reload({ directory: dir }).pipe(Effect.forkScoped)
yield* Effect.promise(() => reloading.promise)
const staleDispose = yield* store.dispose(first).pipe(Effect.forkScoped)
releaseReload.resolve()
const second = yield* Fiber.join(reload)
yield* Fiber.join(staleDispose)
expect(disposed).toEqual([dir])
expect(yield* store.load({ directory: dir })).toBe(second)
}),
)
it.live("dedupes concurrent disposeAll calls", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
const disposing = Promise.withResolvers<void>()
const releaseDispose = Promise.withResolvers<void>()
const disposed: Array<string> = []
const off = registerDisposer(async (directory) => {
disposed.push(directory)
disposing.resolve()
await releaseDispose.promise
})
yield* Effect.addFinalizer(() => Effect.sync(off))
yield* store.load({ directory: dir })
const first = yield* store.disposeAll().pipe(Effect.forkScoped)
yield* Effect.promise(() => disposing.promise)
const second = yield* store.disposeAll().pipe(Effect.forkScoped)
expect(disposed).toEqual([dir])
releaseDispose.resolve()
yield* Effect.all([Fiber.join(first), Fiber.join(second)])
expect(disposed).toEqual([dir])
}),
)
it.live("re-arms disposeAll after completion", () =>
Effect.gen(function* () {
const dir1 = yield* tmpdirScoped({ git: true })
const dir2 = yield* tmpdirScoped({ git: true })
const store = yield* InstanceStore.Service
const disposed: Array<string> = []
const off = registerDisposer(async (directory) => {
disposed.push(directory)
})
yield* Effect.addFinalizer(() => Effect.sync(off))
yield* store.load({ directory: dir1 })
yield* store.disposeAll()
expect(disposed).toEqual([dir1])
yield* store.load({ directory: dir2 })
yield* store.disposeAll()
expect(disposed).toEqual([dir1, dir2])
}),
)
it.live("provides legacy Promise callers with instance ALS", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const directory = yield* Effect.promise(() =>
WithInstance.provide({
directory: dir,
fn: () => Instance.directory,
}),
)
expect(directory).toBe(dir)
expect(() => Instance.current).toThrow()
}),
)
})