11import { describe , expect , test } from "bun:test"
22import path from "path"
33import { Instance } from "../../src/project/instance"
4- import { Server } from "../../src/server/server"
54import { Session } from "../../src/session"
65import { Log } from "../../src/util/log"
76
87const projectRoot = path . join ( __dirname , "../.." )
98Log . 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