history-pagination.test.ts (3427B)
1 import { test, expect } from 'vitest'; 2 import { 3 initialOuterCursor, 4 advanceOuterPage 5 } from '../../src/lib/messaging/inbox-state.ts'; 6 const source = 'wss://archive.example.org'; 7 const fact = (n: number, time: number) => ({ 8 outerId: n.toString(16).padStart(64, '0'), 9 outerTime: time 10 }); 11 test('oldest retained signed outer time anchors inclusive boundary, independent of inner ordering', () => { 12 const c = initialOuterCursor(source, [fact(1, 100), fact(2, 200)]); 13 expect(c.until).toBe(100); 14 expect(c.historyComplete).toBe(false); 15 }); 16 test('full equal-time bucket stays inclusive and reports capped without skipping one second', () => { 17 const rows = Array.from({ length: 200 }, (_, i) => fact(i + 1, 100)); 18 const c = advanceOuterPage( 19 initialOuterCursor(source, []), 20 rows, 21 200, 22 'complete' 23 ); 24 expect(c).toMatchObject({ 25 until: 100, 26 state: 'capped', 27 historyComplete: false 28 }); 29 }); 30 test('repeated full identical page becomes saturated instead of unbounded automatic traversal', () => { 31 const rows = Array.from({ length: 200 }, (_, i) => fact(i + 1, 100)); 32 const c = initialOuterCursor(source, rows); 33 expect(advanceOuterPage(c, rows, 200, 'complete')).toMatchObject({ 34 until: 100, 35 state: 'saturated', 36 historyComplete: false 37 }); 38 }); 39 test('new older retained ID progresses using outer time while preserving boundary IDs', () => { 40 const c = advanceOuterPage( 41 initialOuterCursor(source, [fact(1, 100)]), 42 [fact(1, 100), fact(2, 99)], 43 2, 44 'complete' 45 ); 46 expect(c).toMatchObject({ until: 99, state: 'older_available' }); 47 expect(c.seen).toEqual([fact(1, 100).outerId, fact(2, 99).outerId]); 48 }); 49 test('underfilled repeated EOSE describes a bounded exhausted window never a global complete inbox', () => 50 expect( 51 advanceOuterPage( 52 initialOuterCursor(source, [fact(1, 100)]), 53 [fact(1, 100)], 54 1, 55 'complete' 56 ) 57 ).toMatchObject({ state: 'window_exhausted', historyComplete: false })); 58 test('failed source retains confirmed metadata without mutating independent peer state', () => { 59 const left = initialOuterCursor(source, [fact(1, 100)]), 60 right = initialOuterCursor('wss://peer.example.org', [fact(2, 50)]); 61 expect(advanceOuterPage(left, [fact(3, 90)], 1, 'closed')).toMatchObject({ 62 state: 'partial', 63 until: 90 64 }); 65 expect(right.until).toBe(50); 66 expect(left.until).toBe(100); 67 }); 68 test('aggregate cutoff is capped partial evidence and never resets original confirmed IDs', () => { 69 const c = advanceOuterPage( 70 initialOuterCursor(source, [fact(1, 100)]), 71 [], 72 500, 73 'budget' 74 ); 75 expect(c).toMatchObject({ 76 state: 'capped', 77 partial: true, 78 until: 100, 79 historyComplete: false 80 }); 81 expect(c.seen).toEqual([fact(1, 100).outerId]); 82 }); 83 test('invalid negative noninteger or noncanonical outer metadata cannot become a cursor', () => { 84 for (const row of [ 85 { outerId: 'bad', outerTime: 100 }, 86 fact(1, -1), 87 fact(1, 1.5) 88 ]) 89 expect(() => initialOuterCursor(source, [row])).toThrow(); 90 }); 91 test('metadata quota refuses the next distinct ID without evicting unresolved history state', () => { 92 const rows = Array.from({ length: 2000 }, (_, i) => fact(i + 1, 100)); 93 const c = initialOuterCursor(source, rows); 94 expect(() => advanceOuterPage(c, [fact(2001, 99)], 1, 'complete')).toThrow(); 95 expect(c.seen).toHaveLength(2000); 96 expect(c.until).toBe(100); 97 });