@@ -671,17 +671,26 @@ describe('Enhanced Context Operations Integration Tests', () => {
671671 yesterdayAfternoon . setHours ( 15 , 0 , 0 , 0 ) ; // 3 PM yesterday
672672
673673 // Ensure we have items that are definitely "today" - create them at specific times today
674+ // Use UTC to match the test query
674675 const todayMorning = new Date ( ) ;
675- todayMorning . setHours ( 9 , 0 , 0 , 0 ) ; // 9 AM today
676+ todayMorning . setUTCHours ( 9 , 0 , 0 , 0 ) ; // 9 AM today UTC
676677
677678 const todayAfternoon = new Date ( ) ;
678- todayAfternoon . setHours ( 14 , 0 , 0 , 0 ) ; // 2 PM today
679+ todayAfternoon . setUTCHours ( 14 , 0 , 0 , 0 ) ; // 2 PM today UTC
680+
681+ // Create more "today" items ensuring they stay within today's boundaries
682+ const todayEarlyMorning = new Date ( ) ;
683+ todayEarlyMorning . setUTCHours ( 7 , 0 , 0 , 0 ) ; // 7 AM today UTC
684+
685+ const todayEarlyMorning2 = new Date ( ) ;
686+ todayEarlyMorning2 . setUTCHours ( 5 , 0 , 0 , 0 ) ; // 5 AM today UTC
679687
680688 const timeOffsets = [
681- { hours : - 1 , key : 'recent_1' , category : 'task' } , // 1 hour ago
682- { hours : - 2 , key : 'recent_2' , category : 'note' } , // 2 hours ago
683- { hours : - 5 , key : 'today_1' , category : 'task' } , // 5 hours ago
684- { hours : - 8 , key : 'today_2' , category : 'decision' } , // 8 hours ago
689+ // Use absolute timestamps for "today" items to ensure they're always in today
690+ { timestamp : todayMorning , key : 'recent_1' , category : 'task' } , // 9 AM today
691+ { timestamp : todayAfternoon , key : 'recent_2' , category : 'note' } , // 2 PM today
692+ { timestamp : todayEarlyMorning , key : 'today_1' , category : 'task' } , // 7 AM today
693+ { timestamp : todayEarlyMorning2 , key : 'today_2' , category : 'decision' } , // 5 AM today
685694 { timestamp : yesterday , key : 'yesterday_1' , category : 'task' } , // Yesterday 10 AM
686695 { timestamp : yesterdayAfternoon , key : 'yesterday_2' , category : 'note' } , // Yesterday 3 PM
687696 { hours : - 72 , key : 'days_ago_1' , category : 'progress' } , // 3 days ago
@@ -876,15 +885,20 @@ describe('Enhanced Context Operations Integration Tests', () => {
876885
877886 describe ( 'relativeTime parameter' , ( ) => {
878887 it ( 'should handle "today" relative time' , ( ) => {
879- const today = new Date ( ) ;
880- today . setHours ( 0 , 0 , 0 , 0 ) ;
888+ // Use UTC to ensure consistent behavior across timezones
889+ const todayUTC = new Date ( ) ;
890+ todayUTC . setUTCHours ( 0 , 0 , 0 , 0 ) ;
881891
882892 const items = db
883893 . prepare ( 'SELECT * FROM context_items WHERE session_id = ? AND created_at >= ?' )
884- . all ( testSessionId , today . toISOString ( ) ) as any [ ] ;
894+ . all ( testSessionId , todayUTC . toISOString ( ) ) as any [ ] ;
885895
886896 expect ( items . length ) . toBeGreaterThan ( 0 ) ;
887- expect ( items . some ( i => i . key . includes ( 'recent' ) ) ) . toBe ( true ) ;
897+ // Check for items we know are created "today" based on our test setup
898+ // These items use specific timestamps that should be within today
899+ const todayKeys = items . map ( i => i . key ) ;
900+ // At minimum, we should have items created with relative hours that fall within today
901+ expect ( todayKeys . some ( key => key . includes ( 'recent' ) || key . includes ( 'today' ) ) ) . toBe ( true ) ;
888902 } ) ;
889903
890904 it ( 'should handle "yesterday" relative time' , ( ) => {
@@ -905,23 +919,42 @@ describe('Enhanced Context Operations Integration Tests', () => {
905919 } ) ;
906920
907921 it ( 'should handle "X hours ago" format' , ( ) => {
908- // Use a slightly earlier time to account for millisecond differences
909- const twoHoursAgo = new Date ( ) ;
910- twoHoursAgo . setHours ( twoHoursAgo . getHours ( ) - 2 ) ;
911- twoHoursAgo . setMinutes ( twoHoursAgo . getMinutes ( ) - 1 ) ; // Go back 1 minute to ensure we catch items at exactly 2 hours
922+ // Create test items relative to current time for this specific test
923+ const now = new Date ( ) ;
924+ const oneHourAgo = new Date ( now . getTime ( ) - 1 * 3600000 ) ;
925+ const twoHoursAgo = new Date ( now . getTime ( ) - 2 * 3600000 ) ;
926+ const fiveHoursAgo = new Date ( now . getTime ( ) - 5 * 3600000 ) ;
927+
928+ // Add test items with specific relative timestamps
929+ db . prepare (
930+ `INSERT INTO context_items (id, session_id, key, value, category, priority, created_at)
931+ VALUES (?, ?, ?, ?, ?, ?, ?)`
932+ ) . run ( uuidv4 ( ) , testSessionId , 'test_1h_ago' , 'One hour ago' , 'test' , 'normal' , oneHourAgo . toISOString ( ) ) ;
933+
934+ db . prepare (
935+ `INSERT INTO context_items (id, session_id, key, value, category, priority, created_at)
936+ VALUES (?, ?, ?, ?, ?, ?, ?)`
937+ ) . run ( uuidv4 ( ) , testSessionId , 'test_2h_ago' , 'Two hours ago' , 'test' , 'normal' , twoHoursAgo . toISOString ( ) ) ;
938+
939+ db . prepare (
940+ `INSERT INTO context_items (id, session_id, key, value, category, priority, created_at)
941+ VALUES (?, ?, ?, ?, ?, ?, ?)`
942+ ) . run ( uuidv4 ( ) , testSessionId , 'test_5h_ago' , 'Five hours ago' , 'test' , 'normal' , fiveHoursAgo . toISOString ( ) ) ;
912943
944+ // Query for items created 2 hours ago or less
945+ const queryTime = new Date ( now . getTime ( ) - 2.1 * 3600000 ) ; // 2.1 hours ago to ensure we catch 2 hour old items
913946 const items = db
914947 . prepare ( 'SELECT * FROM context_items WHERE session_id = ? AND created_at >= ?' )
915- . all ( testSessionId , twoHoursAgo . toISOString ( ) ) as any [ ] ;
948+ . all ( testSessionId , queryTime . toISOString ( ) ) as any [ ] ;
916949
917950 // Check what items we actually got
918951 const itemKeys = items . map ( i => i . key ) ;
919952
920953 // Should include items created 2 hours ago or less
921- expect ( itemKeys ) . toContain ( 'recent_1 ' ) ; // 1 hour ago
922- expect ( itemKeys ) . toContain ( 'recent_2 ' ) ; // 2 hours ago
954+ expect ( itemKeys ) . toContain ( 'test_1h_ago ' ) ;
955+ expect ( itemKeys ) . toContain ( 'test_2h_ago ' ) ;
923956 // Should not include items created more than 2 hours ago
924- expect ( itemKeys ) . not . toContain ( 'today_1 ' ) ; // 5 hours ago
957+ expect ( itemKeys ) . not . toContain ( 'test_5h_ago ' ) ;
925958 } ) ;
926959
927960 it ( 'should handle "X days ago" format' , ( ) => {
0 commit comments