Skip to content

Commit 8631d6c

Browse files
committed
core: add comprehensive test coverage for Session.list() filters
Adds test cases for filtering sessions by directory, root sessions only, start time, search terms, and result limits to ensure the listing functionality works correctly for all filter combinations.
1 parent 68bb8ce commit 8631d6c

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed
Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import { describe, expect, test } from "bun:test"
22
import path from "path"
33
import { Instance } from "../../src/project/instance"
4-
import { Server } from "../../src/server/server"
54
import { Session } from "../../src/session"
65
import { Log } from "../../src/util/log"
76

87
const projectRoot = path.join(__dirname, "../..")
98
Log.init({ print: false })
109

11-
describe("session.list", () => {
10+
describe("Session.list", () => {
1211
test("filters by directory", async () => {
1312
await Instance.provide({
1413
directory: projectRoot,
1514
fn: async () => {
16-
const app = Server.App()
17-
1815
const first = await Session.create({})
1916

2017
const otherDir = path.join(projectRoot, "..", "__session_list_other")
@@ -23,17 +20,71 @@ describe("session.list", () => {
2320
fn: async () => Session.create({}),
2421
})
2522

26-
const response = await app.request(`/session?directory=${encodeURIComponent(projectRoot)}`)
27-
expect(response.status).toBe(200)
28-
29-
const body = (await response.json()) as unknown[]
30-
const ids = body
31-
.map((s) => (typeof s === "object" && s && "id" in s ? (s as { id: string }).id : undefined))
32-
.filter((x): x is string => typeof x === "string")
23+
const sessions = [...Session.list({ directory: projectRoot })]
24+
const ids = sessions.map((s) => s.id)
3325

3426
expect(ids).toContain(first.id)
3527
expect(ids).not.toContain(second.id)
3628
},
3729
})
3830
})
31+
32+
test("filters root sessions", async () => {
33+
await Instance.provide({
34+
directory: projectRoot,
35+
fn: async () => {
36+
const root = await Session.create({ title: "root-session" })
37+
const child = await Session.create({ title: "child-session", parentID: root.id })
38+
39+
const sessions = [...Session.list({ roots: true })]
40+
const ids = sessions.map((s) => s.id)
41+
42+
expect(ids).toContain(root.id)
43+
expect(ids).not.toContain(child.id)
44+
},
45+
})
46+
})
47+
48+
test("filters by start time", async () => {
49+
await Instance.provide({
50+
directory: projectRoot,
51+
fn: async () => {
52+
const session = await Session.create({ title: "new-session" })
53+
const futureStart = Date.now() + 86400000
54+
55+
const sessions = [...Session.list({ start: futureStart })]
56+
expect(sessions.length).toBe(0)
57+
},
58+
})
59+
})
60+
61+
test("filters by search term", async () => {
62+
await Instance.provide({
63+
directory: projectRoot,
64+
fn: async () => {
65+
await Session.create({ title: "unique-search-term-abc" })
66+
await Session.create({ title: "other-session-xyz" })
67+
68+
const sessions = [...Session.list({ search: "unique-search" })]
69+
const titles = sessions.map((s) => s.title)
70+
71+
expect(titles).toContain("unique-search-term-abc")
72+
expect(titles).not.toContain("other-session-xyz")
73+
},
74+
})
75+
})
76+
77+
test("respects limit parameter", async () => {
78+
await Instance.provide({
79+
directory: projectRoot,
80+
fn: async () => {
81+
await Session.create({ title: "session-1" })
82+
await Session.create({ title: "session-2" })
83+
await Session.create({ title: "session-3" })
84+
85+
const sessions = [...Session.list({ limit: 2 })]
86+
expect(sessions.length).toBe(2)
87+
},
88+
})
89+
})
3990
})

0 commit comments

Comments
 (0)