forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-list.test.ts
More file actions
231 lines (206 loc) · 8.61 KB
/
session-list.test.ts
File metadata and controls
231 lines (206 loc) · 8.61 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
import { afterEach, describe, expect } from "bun:test"
import { Effect, Layer } from "effect"
import { Session as SessionNs } from "@/session/session"
import * as Log from "@opencode-ai/core/util/log"
import { disposeAllInstances, provideInstance, TestInstance } from "../fixture/fixture"
import { mkdir } from "fs/promises"
import path from "path"
import { Database } from "@/storage/db"
import { SessionTable } from "@/session/session.sql"
import { eq } from "drizzle-orm"
import { testEffect } from "../lib/effect"
import { Bus } from "@/bus"
import { Storage } from "@/storage/storage"
import { SyncEvent } from "@/sync"
import { RuntimeFlags } from "@/effect/runtime-flags"
import { BackgroundJob } from "@/background/job"
void Log.init({ print: false })
const it = testEffect(
SessionNs.layer.pipe(
Layer.provide(Bus.layer),
Layer.provide(Storage.defaultLayer),
Layer.provide(SyncEvent.defaultLayer),
Layer.provide(RuntimeFlags.layer({ experimentalWorkspaces: false })),
Layer.provide(BackgroundJob.defaultLayer),
),
)
const withSession = (input?: Parameters<SessionNs.Interface["create"]>[0]) =>
Effect.acquireRelease(
SessionNs.Service.use((session) => session.create(input)),
(created) => SessionNs.Service.use((session) => session.remove(created.id).pipe(Effect.ignore)),
)
afterEach(async () => {
await disposeAllInstances()
})
describe("session.list", () => {
it.instance(
"does not filter by directory when directory is omitted",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* Effect.promise(() => mkdir(path.join(test.directory, "packages", "opencode"), { recursive: true }))
yield* Effect.promise(() => mkdir(path.join(test.directory, "packages", "app"), { recursive: true }))
const root = yield* withSession({ title: "root" })
const parent = yield* withSession({ title: "parent" }).pipe(
provideInstance(path.join(test.directory, "packages")),
)
const current = yield* withSession({ title: "current" }).pipe(
provideInstance(path.join(test.directory, "packages", "opencode")),
)
const sibling = yield* withSession({ title: "sibling" }).pipe(
provideInstance(path.join(test.directory, "packages", "app")),
)
const ids = (yield* SessionNs.Service.use((session) => session.list())).map((session) => session.id)
expect(ids).toContain(root.id)
expect(ids).toContain(parent.id)
expect(ids).toContain(current.id)
expect(ids).toContain(sibling.id)
}),
{ git: true },
)
it.instance(
"filters by directory when directory is provided",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* Effect.promise(() => mkdir(path.join(test.directory, "packages", "opencode"), { recursive: true }))
yield* Effect.promise(() => mkdir(path.join(test.directory, "packages", "app"), { recursive: true }))
const root = yield* withSession({ title: "root" })
const parent = yield* withSession({ title: "parent" }).pipe(
provideInstance(path.join(test.directory, "packages")),
)
const current = yield* withSession({ title: "current" }).pipe(
provideInstance(path.join(test.directory, "packages", "opencode")),
)
const sibling = yield* withSession({ title: "sibling" }).pipe(
provideInstance(path.join(test.directory, "packages", "app")),
)
const ids = (yield* SessionNs.Service.use((session) =>
session.list({ directory: path.join(test.directory, "packages", "opencode") }),
)).map((session) => session.id)
expect(ids).not.toContain(root.id)
expect(ids).not.toContain(parent.id)
expect(ids).toContain(current.id)
expect(ids).not.toContain(sibling.id)
}),
{ git: true },
)
it.instance(
"filters by path and ignores directory when path is provided",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* Effect.promise(() =>
mkdir(path.join(test.directory, "packages", "opencode", "src", "deep"), { recursive: true }),
)
yield* Effect.promise(() => mkdir(path.join(test.directory, "packages", "app"), { recursive: true }))
const parent = yield* withSession({ title: "parent" }).pipe(
provideInstance(path.join(test.directory, "packages", "opencode")),
)
const current = yield* withSession({ title: "current" }).pipe(
provideInstance(path.join(test.directory, "packages", "opencode", "src")),
)
const deeper = yield* withSession({ title: "deeper" }).pipe(
provideInstance(path.join(test.directory, "packages", "opencode", "src", "deep")),
)
const sibling = yield* withSession({ title: "sibling" }).pipe(
provideInstance(path.join(test.directory, "packages", "app")),
)
const pathIDs = (yield* SessionNs.Service.use((session) =>
session.list({
directory: path.join(test.directory, "packages", "app"),
path: "packages/opencode/src",
}),
)).map((session) => session.id)
expect(pathIDs).not.toContain(parent.id)
expect(pathIDs).toContain(current.id)
expect(pathIDs).toContain(deeper.id)
expect(pathIDs).not.toContain(sibling.id)
}),
{ git: true },
)
it.instance(
"falls back to directory when filtering legacy sessions without path",
() =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* Effect.promise(() =>
mkdir(path.join(test.directory, "packages", "opencode", "src"), { recursive: true }),
)
yield* Effect.promise(() => mkdir(path.join(test.directory, "packages", "app"), { recursive: true }))
const current = yield* withSession({ title: "legacy-current" }).pipe(
provideInstance(path.join(test.directory, "packages", "opencode", "src")),
)
const sibling = yield* withSession({ title: "legacy-sibling" }).pipe(
provideInstance(path.join(test.directory, "packages", "app")),
)
yield* Effect.sync(() =>
Database.use((db) =>
db.update(SessionTable).set({ path: null }).where(eq(SessionTable.id, current.id)).run(),
),
)
yield* Effect.sync(() =>
Database.use((db) =>
db.update(SessionTable).set({ path: null }).where(eq(SessionTable.id, sibling.id)).run(),
),
)
const pathIDs = (yield* SessionNs.Service.use((session) =>
session.list({
directory: path.join(test.directory, "packages", "opencode", "src"),
path: "packages/opencode/src",
}),
)).map((session) => session.id)
expect(pathIDs).toContain(current.id)
expect(pathIDs).not.toContain(sibling.id)
}),
{ git: true },
)
it.instance(
"filters root sessions",
() =>
Effect.gen(function* () {
const root = yield* withSession({ title: "root-session" })
const child = yield* withSession({ title: "child-session", parentID: root.id })
const sessions = yield* SessionNs.Service.use((session) => session.list({ roots: true }))
const ids = sessions.map((session) => session.id)
expect(ids).toContain(root.id)
expect(ids).not.toContain(child.id)
}),
{ git: true },
)
it.instance(
"filters by start time",
() =>
Effect.gen(function* () {
yield* withSession({ title: "new-session" })
const sessions = yield* SessionNs.Service.use((session) => session.list({ start: Date.now() + 86400000 }))
expect(sessions.length).toBe(0)
}),
{ git: true },
)
it.instance(
"filters by search term",
() =>
Effect.gen(function* () {
yield* withSession({ title: "unique-search-term-abc" })
yield* withSession({ title: "other-session-xyz" })
const sessions = yield* SessionNs.Service.use((session) => session.list({ search: "unique-search" }))
const titles = sessions.map((session) => session.title)
expect(titles).toContain("unique-search-term-abc")
expect(titles).not.toContain("other-session-xyz")
}),
{ git: true },
)
it.instance(
"respects limit parameter",
() =>
Effect.gen(function* () {
yield* withSession({ title: "session-1" })
yield* withSession({ title: "session-2" })
yield* withSession({ title: "session-3" })
const sessions = yield* SessionNs.Service.use((session) => session.list({ limit: 2 }))
expect(sessions.length).toBe(2)
}),
{ git: true },
)
})