aboutsummaryrefslogtreecommitdiffstats
path: root/frontend-vanilla/src/regression.test.ts
blob: 7f72ed58bd07c778180db95eb82eb197cc7de3da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { store } from './store';
import { apiFetch } from './api';
import { renderItems, renderLayout } from './main';
import { createFeedItem } from './components/FeedItem';

// Mock api
vi.mock('./api', () => ({
    apiFetch: vi.fn()
}));

// Mock main module functions that we aren't testing directly but are imported/used
// Note: We need to use vi.importActual to get the real implementations we want to test
// But main.ts has side effects and complex DOM manipulations, so we might want to mock parts of it.
// Mock IntersectionObserver
class MockIntersectionObserver {
    observe = vi.fn();
    unobserve = vi.fn();
    disconnect = vi.fn();
}
vi.stubGlobal('IntersectionObserver', MockIntersectionObserver);

describe('Scroll-to-Read Regression Tests', () => {
    beforeEach(() => {
        document.body.innerHTML = '<div id="app"><div id="main-content"><div id="content-area"></div></div></div>';
        // Mock scrollIntoView
        Element.prototype.scrollIntoView = vi.fn();
        vi.clearAllMocks();
        store.setItems([]);

        // Setup default auth response
        vi.mocked(apiFetch).mockResolvedValue({
            ok: true,
            status: 200,
            json: async () => []
        } as Response);
    });

    afterEach(() => {
        vi.useRealTimers();
    });

    it('should mark item as read when existing in store and fully scrolled past (bottom < container top)', async () => {
        vi.useRealTimers();
        const mockItem = {
            _id: 999,
            title: 'Regression Test Item',
            read: false,
            url: 'http://example.com/regression',
            publish_date: '2023-01-01'
        } as any;

        store.setItems([mockItem]);

        // Manual setup
        const mainContent = document.getElementById('main-content');
        expect(mainContent).not.toBeNull();
        renderItems();

        // Mock getBoundingClientRect for container
        if (mainContent) {
            mainContent.getBoundingClientRect = vi.fn(() => ({
                top: 0, bottom: 800, height: 800, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        // Mock getBoundingClientRect for item (fully scrolled past: bottom < 0)
        // Item top: -150, Item bottom: -50. Container Top: 0.
        // -50 < 0, so should mark as read.
        const itemEl = document.querySelector(`.feed-item[data-id="999"]`);
        expect(itemEl).not.toBeNull();

        if (itemEl) {
            itemEl.getBoundingClientRect = vi.fn(() => ({
                top: -150, bottom: -50, height: 100, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        vi.mocked(apiFetch).mockResolvedValue({ ok: true } as Response);

        // Trigger scroll event
        mainContent?.dispatchEvent(new Event('scroll'));

        // Wait for throttle (250ms) + buffer
        await new Promise(resolve => setTimeout(resolve, 300));

        // Verify API call
        expect(apiFetch).toHaveBeenCalledWith(expect.stringContaining('/api/item/999'), expect.objectContaining({
            method: 'PUT',
            body: expect.stringContaining('"read":true')
        }));
    });

    it('should NOT mark item as read if only partially scrolled past (top < container top but bottom > container top)', async () => {
        vi.useRealTimers();
        const mockItem = {
            _id: 777,
            title: 'Partial Test Item',
            read: false,
            url: 'http://example.com/partial',
            publish_date: '2023-01-01'
        } as any;

        store.setItems([mockItem]);
        renderItems();

        const mainContent = document.getElementById('main-content');
        if (mainContent) {
            mainContent.getBoundingClientRect = vi.fn(() => ({
                top: 0, bottom: 800, height: 800, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        // Mock getBoundingClientRect for item (partially scrolled past: top < 0, bottom > 0)
        // Item top: -50, Item bottom: 50. Container Top: 0.
        // 50 is NOT < 0, so should NOT mark as read.
        const itemEl = document.querySelector(`.feed-item[data-id="777"]`);
        if (itemEl) {
            itemEl.getBoundingClientRect = vi.fn(() => ({
                top: -50, bottom: 50, height: 100, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        vi.mocked(apiFetch).mockResolvedValue({ ok: true } as Response);

        mainContent?.dispatchEvent(new Event('scroll'));
        await new Promise(resolve => setTimeout(resolve, 300));

        expect(apiFetch).not.toHaveBeenCalledWith(expect.stringContaining('/api/item/777'), expect.anything());
    });

    it('should NOT mark item as read if NOT scrolled past (item below container top)', async () => {
        vi.useRealTimers();
        const mockItem = {
            _id: 888,
            title: 'Visible Test Item',
            read: false,
            url: 'http://example.com/visible',
            publish_date: '2023-01-01'
        } as any;

        store.setItems([mockItem]);
        renderItems();

        const mainContent = document.getElementById('main-content');

        // Container
        if (mainContent) {
            mainContent.getBoundingClientRect = vi.fn(() => ({
                top: 0, bottom: 800, height: 800, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        // Item is still visible (top: 100)
        const itemEl = document.querySelector(`.feed-item[data-id="888"]`);
        if (itemEl) {
            itemEl.getBoundingClientRect = vi.fn(() => ({
                top: 100, bottom: 200, height: 100, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        vi.mocked(apiFetch).mockResolvedValue({ ok: true } as Response);

        mainContent?.dispatchEvent(new Event('scroll'));
        await new Promise(resolve => setTimeout(resolve, 300));

        // API should NOT be called
        expect(apiFetch).not.toHaveBeenCalledWith(expect.stringContaining('/api/item/888'), expect.anything());
    });

    it('should mark item as read when WINDOW scrolls (robustness fallback)', async () => {
        vi.useRealTimers();
        const mockItem = {
            _id: 12345,
            title: 'Window Scroll Item',
            read: false,
            url: 'http://example.com/window',
            publish_date: '2023-01-01'
        } as any;

        store.setItems([mockItem]);
        renderItems();

        // Setup successful detection scenario
        const mainContent = document.getElementById('main-content');
        if (mainContent) {
            mainContent.getBoundingClientRect = vi.fn(() => ({
                top: 0, bottom: 800, height: 800, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        const itemEl = document.querySelector(`.feed-item[data-id="12345"]`);
        if (itemEl) {
            // Fully scrolled past
            itemEl.getBoundingClientRect = vi.fn(() => ({
                top: -150, bottom: -50, height: 100, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => { }
            }));
        }

        vi.mocked(apiFetch).mockResolvedValue({ ok: true } as Response);

        // Dispatch scroll on WINDOW, not mainContent
        window.dispatchEvent(new Event('scroll'));

        // Wait for potential debounce/poll (3000ms interval + buffer)
        await new Promise(resolve => setTimeout(resolve, 3100));

        // Expect it to handle it
        expect(apiFetch).toHaveBeenCalledWith(expect.stringContaining('/api/item/12345'), expect.objectContaining({
            method: 'PUT',
            body: expect.stringContaining('"read":true')
        }));
    });
});

// NK-t8qnrh: Links in feed item descriptions should have no underlines (match v1 style)
describe('NK-t8qnrh: Feed item description links have no underlines', () => {
    it('item-description should be rendered inside feed items', () => {
        const item = {
            _id: 1,
            title: 'Test',
            url: 'http://example.com',
            description: '<p>Text with <a href="http://example.com">a link</a></p>',
            read: false,
            starred: false,
            publish_date: '2024-01-01',
        } as any;
        const html = createFeedItem(item);
        expect(html).toContain('class="item-description"');
        expect(html).toContain('<a href="http://example.com">a link</a>');
    });
});

// NK-mcl01m: Sidebar order should be filters → search → "+ new" → Feeds
describe('NK-mcl01m: Sidebar section order', () => {
    beforeEach(() => {
        document.body.innerHTML = '<div id="app"></div>';
        vi.mocked(apiFetch).mockResolvedValue({ ok: true, status: 200, json: async () => [] } as Response);
        renderLayout();
    });

    it('filter-list appears before section-feeds in the sidebar', () => {
        const sidebar = document.getElementById('sidebar');
        expect(sidebar).not.toBeNull();
        const filterList = sidebar!.querySelector('#filter-list');
        const sectionFeeds = sidebar!.querySelector('#section-feeds');
        expect(filterList).not.toBeNull();
        expect(sectionFeeds).not.toBeNull();
        // filter-list should come before section-feeds in DOM order
        const position = filterList!.compareDocumentPosition(sectionFeeds!);
        expect(position & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
    });

    /* FIXME: Tags feature soft-deprecated
    it('section-feeds appears before section-tags in the sidebar', () => {
        const sidebar = document.getElementById('sidebar');
        const sectionFeeds = sidebar!.querySelector('#section-feeds');
        const sectionTags = sidebar!.querySelector('#section-tags');
        expect(sectionFeeds).not.toBeNull();
        expect(sectionTags).not.toBeNull();
        // feeds should come before tags
        const position = sectionFeeds!.compareDocumentPosition(sectionTags!);
        expect(position & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
    });
    */

    it('search input appears after filter-list and before section-feeds', () => {
        const sidebar = document.getElementById('sidebar');
        const filterList = sidebar!.querySelector('#filter-list');
        const searchInput = sidebar!.querySelector('#search-input');
        const sectionFeeds = sidebar!.querySelector('#section-feeds');
        expect(searchInput).not.toBeNull();
        // search after filters
        const pos1 = filterList!.compareDocumentPosition(searchInput!);
        expect(pos1 & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
        // search before feeds
        const pos2 = searchInput!.compareDocumentPosition(sectionFeeds!);
        expect(pos2 & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
    });

    it('sidebar has a "+ new" link pointing to settings', () => {
        const newLink = document.querySelector('.new-feed-link');
        expect(newLink).not.toBeNull();
        expect(newLink!.textContent?.trim()).toBe('+ new');
    });
});

// NK-z1czaq: Main content should fill full width (sidebar overlays, never shifts content)
describe('NK-z1czaq: Sidebar overlays content, does not shift layout', () => {
    beforeEach(() => {
        document.body.innerHTML = '<div id="app"></div>';
        vi.mocked(apiFetch).mockResolvedValue({ ok: true, status: 200, json: async () => [] } as Response);
    });

    it('sidebar is a sibling of main-content inside .layout (not flex-shifting)', () => {
        renderLayout();
        const sidebar = document.querySelector('.sidebar');
        const mainContent = document.querySelector('.main-content');
        expect(sidebar).not.toBeNull();
        expect(mainContent).not.toBeNull();
        // Both should be children of .layout
        expect(sidebar!.parentElement?.classList.contains('layout')).toBe(true);
        expect(mainContent!.parentElement?.classList.contains('layout')).toBe(true);
    });
});

// Infinite scroll: uses scroll-position check (like v1) instead of IntersectionObserver.
// When the user scrolls within 200px of the bottom of #main-content, loadMore fires.
describe('Infinite scroll: scroll near bottom triggers loadMore', () => {
    beforeEach(() => {
        document.body.innerHTML = '<div id="app"><div id="main-content"><div id="content-area"></div></div></div>';
        Element.prototype.scrollIntoView = vi.fn();
        vi.clearAllMocks();
        store.setItems([]);
        store.setHasMore(true);

        vi.mocked(apiFetch).mockResolvedValue({
            ok: true,
            status: 200,
            json: async () => [],
        } as Response);
    });

    function simulateScrollNearBottom(mainContent: HTMLElement) {
        // Simulate: scrollHeight=2000, clientHeight=800, scrollTop=1050
        // remaining = 2000 - 1050 - 800 = 150 < 200 → should trigger loadMore
        Object.defineProperty(mainContent, 'scrollHeight', { value: 2000, configurable: true });
        Object.defineProperty(mainContent, 'clientHeight', { value: 800, configurable: true });
        mainContent.scrollTop = 1050;
        mainContent.dispatchEvent(new Event('scroll'));
    }

    function simulateScrollFarFromBottom(mainContent: HTMLElement) {
        // remaining = 2000 - 200 - 800 = 1000 > 200 → should NOT trigger
        Object.defineProperty(mainContent, 'scrollHeight', { value: 2000, configurable: true });
        Object.defineProperty(mainContent, 'clientHeight', { value: 800, configurable: true });
        mainContent.scrollTop = 200;
        mainContent.dispatchEvent(new Event('scroll'));
    }

    it('should call loadMore (apiFetch /api/stream) when scrolled near the bottom', () => {
        const items = Array.from({ length: 50 }, (_, i) => ({
            _id: i + 1,
            title: `Item ${i + 1}`,
            url: `http://example.com/${i + 1}`,
            read: false,
            publish_date: '2024-01-01',
        }));
        store.setItems(items as any);
        vi.clearAllMocks();

        const mainContent = document.getElementById('main-content')!;
        // renderItems sets onscroll on main-content; fire the scroll event
        simulateScrollNearBottom(mainContent);

        expect(apiFetch).toHaveBeenCalledWith(
            expect.stringContaining('/api/stream'),
        );
    });

    it('should NOT call loadMore when scrolled far from the bottom', () => {
        const items = Array.from({ length: 50 }, (_, i) => ({
            _id: i + 1,
            title: `Item ${i + 1}`,
            url: `http://example.com/${i + 1}`,
            read: false,
            publish_date: '2024-01-01',
        }));
        store.setItems(items as any);
        vi.clearAllMocks();

        const mainContent = document.getElementById('main-content')!;
        simulateScrollFarFromBottom(mainContent);

        expect(apiFetch).not.toHaveBeenCalledWith(
            expect.stringContaining('/api/stream'),
        );
    });

    it('should NOT call loadMore when store.loading is true', () => {
        const items = Array.from({ length: 50 }, (_, i) => ({
            _id: i + 1,
            title: `Item ${i + 1}`,
            url: `http://example.com/${i + 1}`,
            read: false,
            publish_date: '2024-01-01',
        }));
        store.setItems(items as any);
        vi.clearAllMocks();
        store.loading = true;

        const mainContent = document.getElementById('main-content')!;
        simulateScrollNearBottom(mainContent);

        expect(apiFetch).not.toHaveBeenCalledWith(
            expect.stringContaining('/api/stream'),
        );
    });

    it('should NOT render sentinel (or call loadMore) when hasMore is false', () => {
        const items = Array.from({ length: 10 }, (_, i) => ({
            _id: i + 1,
            title: `Item ${i + 1}`,
            url: `http://example.com/${i + 1}`,
            read: false,
            publish_date: '2024-01-01',
        }));
        store.setHasMore(false);
        store.setItems(items as any);

        expect(document.getElementById('load-more-sentinel')).toBeNull();
    });
});