forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.test.ts
More file actions
293 lines (249 loc) · 10.3 KB
/
storage.test.ts
File metadata and controls
293 lines (249 loc) · 10.3 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
import { describe, expect } from "bun:test"
import path from "path"
import { Effect, Exit, Layer } from "effect"
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
import { Git } from "../../src/git"
import { Global } from "../../src/global"
import { Storage } from "../../src/storage"
import { tmpdirScoped } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
const dir = path.join(Global.Path.data, "storage")
const it = testEffect(Layer.mergeAll(Storage.defaultLayer, AppFileSystem.defaultLayer, CrossSpawnSpawner.defaultLayer))
const scope = Effect.fnUntraced(function* () {
const root = ["storage_test", crypto.randomUUID()]
const fs = yield* AppFileSystem.Service
const svc = yield* Storage.Service
yield* Effect.addFinalizer(() =>
fs.remove(path.join(dir, ...root), { recursive: true, force: true }).pipe(Effect.ignore),
)
return { root, svc }
})
// remap(root) rewrites any path under Global.Path.data to live under `root` instead.
// Used by remappedFs to build an AppFileSystem that Storage thinks is the real global
// data dir but actually targets a tmp dir — letting migration tests stage legacy layouts.
// NOTE: only the 6 methods below are intercepted. If Storage starts using a different
// AppFileSystem method that touches Global.Path.data, add it here.
function remap(root: string, file: string) {
if (file === Global.Path.data) return root
if (file.startsWith(Global.Path.data + path.sep)) return path.join(root, path.relative(Global.Path.data, file))
return file
}
function remappedFs(root: string) {
return Layer.effect(
AppFileSystem.Service,
Effect.gen(function* () {
const fs = yield* AppFileSystem.Service
return AppFileSystem.Service.of({
...fs,
isDir: (file) => fs.isDir(remap(root, file)),
readJson: (file) => fs.readJson(remap(root, file)),
writeWithDirs: (file, content, mode) => fs.writeWithDirs(remap(root, file), content, mode),
readFileString: (file) => fs.readFileString(remap(root, file)),
remove: (file) => fs.remove(remap(root, file)),
glob: (pattern, options) =>
fs.glob(pattern, options?.cwd ? { ...options, cwd: remap(root, options.cwd) } : options),
})
}),
).pipe(Layer.provide(AppFileSystem.defaultLayer))
}
// Layer.fresh forces a new Storage instance — without it, Effect's in-test layer cache
// returns the outer testEffect's Storage (which uses the real AppFileSystem), not a new
// one built on top of remappedFs.
const remappedStorage = (root: string) =>
Layer.fresh(Storage.layer.pipe(Layer.provide(remappedFs(root)), Layer.provide(Git.defaultLayer)))
describe("Storage", () => {
it.live("round-trips JSON content", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const key = [...root, "session_diff", "roundtrip"]
const value = [{ file: "a.ts", additions: 2, deletions: 1 }]
yield* svc.write(key, value)
expect(yield* svc.read<typeof value>(key)).toEqual(value)
}),
)
it.live("maps missing reads to NotFoundError", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const exit = yield* svc.read([...root, "missing", "value"]).pipe(Effect.exit)
expect(Exit.isFailure(exit)).toBe(true)
}),
)
it.live("update on missing key throws NotFoundError", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const exit = yield* svc
.update<{ value: number }>([...root, "missing", "key"], (draft) => {
draft.value += 1
})
.pipe(Effect.exit)
expect(Exit.isFailure(exit)).toBe(true)
}),
)
it.live("write overwrites existing value", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const key = [...root, "overwrite", "test"]
yield* svc.write<{ v: number }>(key, { v: 1 })
yield* svc.write<{ v: number }>(key, { v: 2 })
expect(yield* svc.read<{ v: number }>(key)).toEqual({ v: 2 })
}),
)
it.live("remove on missing key is a no-op", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
yield* svc.remove([...root, "nonexistent", "key"])
}),
)
it.live("list on missing prefix returns empty", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
expect(yield* svc.list([...root, "nonexistent"])).toEqual([])
}),
)
it.live("serializes concurrent updates for the same key", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const key = [...root, "counter", "shared"]
yield* svc.write(key, { value: 0 })
yield* Effect.all(
Array.from({ length: 25 }, () =>
svc.update<{ value: number }>(key, (draft) => {
draft.value += 1
}),
),
{ concurrency: "unbounded" },
)
expect(yield* svc.read<{ value: number }>(key)).toEqual({ value: 25 })
}),
)
it.live("concurrent reads do not block each other", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const key = [...root, "concurrent", "reads"]
yield* svc.write(key, { ok: true })
const results = yield* Effect.all(
Array.from({ length: 10 }, () => svc.read(key)),
{ concurrency: "unbounded" },
)
expect(results).toHaveLength(10)
for (const r of results) expect(r).toEqual({ ok: true })
}),
)
it.live("nested keys create deep paths", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const key = [...root, "a", "b", "c", "deep"]
yield* svc.write<{ nested: boolean }>(key, { nested: true })
expect(yield* svc.read<{ nested: boolean }>(key)).toEqual({ nested: true })
expect(yield* svc.list([...root, "a"])).toEqual([key])
}),
)
it.live("lists and removes stored entries", () =>
Effect.gen(function* () {
const { root, svc } = yield* scope()
const a = [...root, "list", "a"]
const b = [...root, "list", "b"]
const prefix = [...root, "list"]
yield* svc.write(b, { value: 2 })
yield* svc.write(a, { value: 1 })
expect(yield* svc.list(prefix)).toEqual([a, b])
yield* svc.remove(a)
expect(yield* svc.list(prefix)).toEqual([b])
const exit = yield* svc.read(a).pipe(Effect.exit)
expect(Exit.isFailure(exit)).toBe(true)
}),
)
it.live("migration 2 runs when marker contents are invalid", () =>
Effect.gen(function* () {
const fs = yield* AppFileSystem.Service
const tmp = yield* tmpdirScoped()
const storage = path.join(tmp, "storage")
const diffs = [
{ additions: 2, deletions: 1 },
{ additions: 3, deletions: 4 },
]
yield* fs.writeWithDirs(path.join(storage, "migration"), "wat")
yield* fs.writeWithDirs(
path.join(storage, "session", "proj_test", "ses_test.json"),
JSON.stringify({
id: "ses_test",
projectID: "proj_test",
title: "legacy",
summary: { diffs },
}),
)
yield* Effect.gen(function* () {
const svc = yield* Storage.Service
expect(yield* svc.list(["session_diff"])).toEqual([["session_diff", "ses_test"]])
expect(yield* svc.read<typeof diffs>(["session_diff", "ses_test"])).toEqual(diffs)
expect(
yield* svc.read<{
id: string
projectID: string
title: string
summary: { additions: number; deletions: number }
}>(["session", "proj_test", "ses_test"]),
).toEqual({
id: "ses_test",
projectID: "proj_test",
title: "legacy",
summary: { additions: 5, deletions: 5 },
})
}).pipe(Effect.provide(remappedStorage(tmp)))
expect(yield* fs.readFileString(path.join(storage, "migration"))).toBe("2")
}),
)
it.live("migration 1 tolerates malformed legacy records", () =>
Effect.gen(function* () {
const fs = yield* AppFileSystem.Service
const tmp = yield* tmpdirScoped({ git: true })
const storage = path.join(tmp, "storage")
const legacy = path.join(tmp, "project", "legacy")
yield* fs.writeWithDirs(path.join(legacy, "storage", "session", "message", "probe", "0.json"), "[]")
yield* fs.writeWithDirs(
path.join(legacy, "storage", "session", "message", "probe", "1.json"),
JSON.stringify({ path: { root: tmp } }),
)
yield* fs.writeWithDirs(
path.join(legacy, "storage", "session", "info", "ses_legacy.json"),
JSON.stringify({ id: "ses_legacy", title: "legacy" }),
)
yield* fs.writeWithDirs(
path.join(legacy, "storage", "session", "message", "ses_legacy", "msg_legacy.json"),
JSON.stringify({ role: "user", text: "hello" }),
)
yield* Effect.gen(function* () {
const svc = yield* Storage.Service
const projects = yield* svc.list(["project"])
expect(projects).toHaveLength(1)
const project = projects[0]![1]
expect(yield* svc.list(["session", project])).toEqual([["session", project, "ses_legacy"]])
expect(yield* svc.read<{ id: string; title: string }>(["session", project, "ses_legacy"])).toEqual({
id: "ses_legacy",
title: "legacy",
})
expect(yield* svc.read<{ role: string; text: string }>(["message", "ses_legacy", "msg_legacy"])).toEqual({
role: "user",
text: "hello",
})
}).pipe(Effect.provide(remappedStorage(tmp)))
expect(yield* fs.readFileString(path.join(storage, "migration"))).toBe("2")
}),
)
it.live("failed migrations do not advance the marker", () =>
Effect.gen(function* () {
const fs = yield* AppFileSystem.Service
const tmp = yield* tmpdirScoped()
const storage = path.join(tmp, "storage")
const legacy = path.join(tmp, "project", "legacy")
yield* fs.writeWithDirs(path.join(legacy, "storage", "session", "message", "probe", "0.json"), "{")
yield* Effect.gen(function* () {
const svc = yield* Storage.Service
expect(yield* svc.list(["project"])).toEqual([])
}).pipe(Effect.provide(remappedStorage(tmp)))
const exit = yield* fs.access(path.join(storage, "migration")).pipe(Effect.exit)
expect(Exit.isFailure(exit)).toBe(true)
}),
)
})