source: Klonkt/test/feed-pagination.test.js@ c581e5e

main
Last change on this file since c581e5e was 1485933, checked in by Robin <roboburr@…>, 7 weeks ago

Feature: Load more on Messages, page 72 (klonkt-demo-r9u, slice 2/4)

Messages now pages in 72s with the shared Load-more button instead of a
hard 80-item cap. getMessages gains an offset arg and pages the merged,
grouped stream by recomputing top-down and slicing [offset, offset+72]
(stable across pages); getNotifications' per-source caps scale with the
requested limit so deep paging can reach older items. The item markup
moved to partials/msg-item.ejs so the page and the append fragment
render identically. The client-side filter/search re-indexes appended
rows on htmx:afterSettle and re-applies the active chip, so search keeps
working across pages. Seen-watermark is only stamped on the first page,
not on appends.

Tests: getMessages offset paging (no overlap, PAGE+1 probe). Browser:
72 -> 144 -> 150 then the button drops; searching a term only present on
page 2 matches after append.

Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 3.0 KB
Line 
1// Load-more pagination: getTimeline honours limit + offset so the News feed can
2// page in blocks of 72 without overlap. In-memory SQLite. Run: npm test
3import { test } from 'node:test';
4import assert from 'node:assert/strict';
5
6process.env.DATABASE_PATH = ':memory:';
7process.env.PUBLIC_BASE_URL = 'https://test.example';
8
9const dbMod = await import('../src/config/database.js');
10const db = dbMod.default;
11dbMod.initializeDatabase();
12const AP = (await import('../src/services/ActivityPubService.js')).default;
13
14// Seed 150 timeline rows with strictly decreasing published times (newest first).
15const ins = db.prepare(`INSERT INTO ap_timeline (id, slug, author_uri, author_name, content, published, created_at)
16 VALUES (?,?,?,?,?,?,CURRENT_TIMESTAMP)`);
17for (let i = 0; i < 150; i++) {
18 const n = String(i).padStart(3, '0');
19 // higher i = older, so order DESC by published yields 000,001,... first
20 const pub = `2026-01-01T00:00:00Z`.replace('00:00:00', `${String(23 - Math.floor(i / 60)).padStart(2, '0')}:${String(59 - (i % 60)).padStart(2, '0')}:00`);
21 ins.run('n' + n, 'me', 'https://r.test/u/a', 'A', '<p>' + n + '</p>', pub);
22}
23
24test('getTimeline pages with limit + offset, no overlap', () => {
25 const page1 = AP.getTimeline('me', 72, 0);
26 const page2 = AP.getTimeline('me', 72, 72);
27 assert.equal(page1.length, 72, 'first page is full');
28 assert.equal(page2.length, 72, 'second page is full');
29 const ids1 = new Set(page1.map((r) => r.id));
30 const overlap = page2.filter((r) => ids1.has(r.id));
31 assert.equal(overlap.length, 0, 'pages do not overlap');
32});
33
34test('probe of PAGE+1 reveals whether more remain', () => {
35 // 150 rows: at offset 0 a probe of 73 returns 73 (more), at offset 144 it returns 6 (done).
36 assert.equal(AP.getTimeline('me', 73, 0).length, 73);
37 assert.equal(AP.getTimeline('me', 73, 144).length, 6);
38});
39
40test('offset past the end returns empty', () => {
41 assert.equal(AP.getTimeline('me', 72, 300).length, 0);
42});
43
44// getMessages pages the merged stream by offset (recompute-top-down + slice).
45const fins = db.prepare('INSERT OR IGNORE INTO ap_followers (slug, actor_uri, created_at) VALUES (?,?,?)');
46for (let i = 0; i < 150; i++) {
47 const n = String(i).padStart(3, '0');
48 const hh = String(23 - Math.floor(i / 60)).padStart(2, '0');
49 const mm = String(59 - (i % 60)).padStart(2, '0');
50 fins.run('me', 'https://r.test/u/f' + n, `2026-02-01 ${hh}:${mm}:00`);
51}
52
53test('getMessages pages the stream by offset without overlap', () => {
54 const p1 = AP.getMessages('me', 72, 0);
55 const p2 = AP.getMessages('me', 72, 72);
56 assert.equal(p1.length, 72);
57 assert.equal(p2.length, 72);
58 const k = (m) => m.type + '|' + (m.url || m.handle || m.outboxId || '');
59 const set1 = new Set(p1.map(k));
60 assert.equal(p2.filter((m) => set1.has(k(m))).length, 0, 'no overlap between pages');
61});
62
63test('getMessages probe of PAGE+1 signals the last page', () => {
64 assert.equal(AP.getMessages('me', 73, 0).length, 73); // more remain
65 assert.equal(AP.getMessages('me', 73, 144).length, 6); // 150 follows → 6 left
66});
Note: See TracBrowser for help on using the repository browser.