1+ const Database = require ( 'better-sqlite3' ) ;
2+ const path = require ( 'path' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const { v4 : uuidv4 } = require ( 'uuid' ) ;
5+
6+ // Create temporary database
7+ const tempDbPath = path . join ( __dirname , `test-timeline-${ Date . now ( ) } .db` ) ;
8+ const db = new Database ( tempDbPath ) ;
9+
10+ // Initialize schema
11+ const schema = fs . readFileSync ( path . join ( __dirname , 'src/database/schema.sql' ) , 'utf8' ) ;
12+ db . exec ( schema ) ;
13+
14+ // Create test session
15+ const testSessionId = uuidv4 ( ) ;
16+ db . prepare ( 'INSERT INTO sessions (id, name) VALUES (?, ?)' ) . run ( testSessionId , 'Test Session' ) ;
17+
18+ // Create test data
19+ const now = new Date ( ) ;
20+ const items = [
21+ // Today - 6 items
22+ { time : new Date ( now . getTime ( ) - 1 * 60 * 60 * 1000 ) , category : 'task' , priority : 'high' } ,
23+ { time : new Date ( now . getTime ( ) - 2 * 60 * 60 * 1000 ) , category : 'task' , priority : 'normal' } ,
24+ { time : new Date ( now . getTime ( ) - 3 * 60 * 60 * 1000 ) , category : 'note' , priority : 'normal' } ,
25+ { time : new Date ( now . getTime ( ) - 4 * 60 * 60 * 1000 ) , category : 'decision' , priority : 'high' } ,
26+ { time : new Date ( now . getTime ( ) - 5 * 60 * 60 * 1000 ) , category : 'progress' , priority : 'normal' } ,
27+ { time : new Date ( now . getTime ( ) - 6 * 60 * 60 * 1000 ) , category : 'task' , priority : 'low' } ,
28+ // Yesterday - 3 items
29+ { time : new Date ( now . getTime ( ) - 26 * 60 * 60 * 1000 ) , category : 'task' , priority : 'high' } ,
30+ { time : new Date ( now . getTime ( ) - 28 * 60 * 60 * 1000 ) , category : 'note' , priority : 'normal' } ,
31+ { time : new Date ( now . getTime ( ) - 30 * 60 * 60 * 1000 ) , category : 'progress' , priority : 'low' } ,
32+ // 3 days ago - 1 item
33+ { time : new Date ( now . getTime ( ) - 72 * 60 * 60 * 1000 ) , category : 'decision' , priority : 'high' } ,
34+ // 5 days ago - 2 items
35+ { time : new Date ( now . getTime ( ) - 120 * 60 * 60 * 1000 ) , category : 'task' , priority : 'normal' } ,
36+ { time : new Date ( now . getTime ( ) - 121 * 60 * 60 * 1000 ) , category : 'note' , priority : 'normal' } ,
37+ // 7 days ago - 4 items
38+ { time : new Date ( now . getTime ( ) - 168 * 60 * 60 * 1000 ) , category : 'progress' , priority : 'high' } ,
39+ { time : new Date ( now . getTime ( ) - 169 * 60 * 60 * 1000 ) , category : 'task' , priority : 'normal' } ,
40+ { time : new Date ( now . getTime ( ) - 170 * 60 * 60 * 1000 ) , category : 'decision' , priority : 'low' } ,
41+ { time : new Date ( now . getTime ( ) - 171 * 60 * 60 * 1000 ) , category : 'note' , priority : 'normal' } ,
42+ ] ;
43+
44+ // Insert items
45+ const stmt = db . prepare ( `
46+ INSERT INTO context_items (
47+ id, session_id, key, value, category, priority, created_at, updated_at, size
48+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
49+ ` ) ;
50+
51+ items . forEach ( ( item , index ) => {
52+ const key = `item.${ item . time . toISOString ( ) . split ( 'T' ) [ 0 ] } .${ index } ` ;
53+ const value = `Test item created at ${ item . time . toISOString ( ) } ` ;
54+ stmt . run (
55+ uuidv4 ( ) ,
56+ testSessionId ,
57+ key ,
58+ value ,
59+ item . category ,
60+ item . priority ,
61+ item . time . toISOString ( ) ,
62+ item . time . toISOString ( ) ,
63+ value . length
64+ ) ;
65+ } ) ;
66+
67+ // Query timeline data
68+ const sql = `
69+ SELECT
70+ strftime('%Y-%m-%d', created_at) as period,
71+ COUNT(*) as count
72+ FROM context_items
73+ WHERE session_id = ?
74+ GROUP BY period
75+ ORDER BY period DESC
76+ ` ;
77+
78+ const timeline = db . prepare ( sql ) . all ( testSessionId ) ;
79+
80+ console . log ( 'Timeline results:' ) ;
81+ console . log ( 'Total periods:' , timeline . length ) ;
82+ console . log ( 'Periods:' ) ;
83+ timeline . forEach ( period => {
84+ console . log ( ` ${ period . period } : ${ period . count } items` ) ;
85+ } ) ;
86+
87+ // Cleanup
88+ db . close ( ) ;
89+ fs . unlinkSync ( tempDbPath ) ;
90+ try { fs . unlinkSync ( `${ tempDbPath } -wal` ) ; } catch ( e ) { }
91+ try { fs . unlinkSync ( `${ tempDbPath } -shm` ) ; } catch ( e ) { }
0 commit comments