Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

18.6 KB
Raw Blame
/**
 * Tests for pages/notes.js — keyword-listing pagination.
 *
 * The list endpoint pages at 100 notes and returns `total` so the UI can
 * render a real pager. Pre-fix the page never sent limit/offset and never
 * read total: users with more than 100 notes simply could not reach the
 * rest from the list. The "Load more" control appends the next page in
 * keyword mode and stays hidden for the AI modes.
 *
 * Driven via the production-inert window.__notesTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
    );
    globalThis.getCSRFToken = () => 'csrf';
    globalThis.escapeHtml = (s) => String(s ?? '');
    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

afterAll(() => {
    delete window.__VITEST_TEST__;
});

beforeEach(() => {
    document.body.innerHTML =
        '<div id="ldr-notes-grid"></div><div id="ldr-notes-empty"></div>' +
        '<div id="ldr-notes-search-notice" style="display:none"></div>' +
        '<div id="ldr-notes-load-more" style="display:none">' +
        '<button data-action="load-more-notes">' +
        '<span id="ldr-notes-load-more-label">Load more</span></button></div>';
    hook.setDomRefs({
        grid: document.getElementById('ldr-notes-grid'),
        empty: document.getElementById('ldr-notes-empty'),
        notice: document.getElementById('ldr-notes-search-notice'),
    });
    hook.setSearchState({ search: '' });
    hook.setNotes([]);
    window.ui = { showMessage: vi.fn() };
});

const pager = () => document.getElementById('ldr-notes-load-more');
const pagerLabel = () =>
    document.getElementById('ldr-notes-load-more-label').textContent;

function makeNotes(start, count) {
    return Array.from({ length: count }, (_, i) => ({
        id: `n${start + i}`,
        title: `Note ${start + i}`,
        content: 'body',
        tags: [],
    }));
}

describe('keyword-listing pagination', () => {
    it('shows the pager with a showing-X-of-Y label when more pages exist', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            const params = new URL(url, 'http://x').searchParams;
            expect(params.get('limit')).toBe('100');
            return Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: makeNotes(0, 100),
                    total: 120,
                }),
            });
        });

        await hook.loadNotesKeywordSearch();

        expect(hook.getNotes()).toHaveLength(100);
        expect(pager().style.display).toBe('flex');
        expect(pagerLabel()).toBe('Load more (showing 100 of 120)');
    });

    it('load-more appends the next offset page and hides the pager at the end', async () => {
        const offsets = [];
        globalThis.safeFetch = vi.fn((url) => {
            const params = new URL(url, 'http://x').searchParams;
            const offset = Number(params.get('offset') || 0);
            offsets.push(offset);
            return Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: offset === 0 ? makeNotes(0, 100) : makeNotes(100, 20),
                    total: 120,
                }),
            });
        });

        await hook.loadNotesKeywordSearch();
        await hook.loadMoreNotes();

        expect(offsets).toEqual([0, 100]);
        expect(hook.getNotes()).toHaveLength(120);
        // Everything is shown — the pager goes away.
        expect(pager().style.display).toBe('none');
        // The appended page is actually rendered, not just stored.
        expect(
            document.getElementById('ldr-notes-grid').innerHTML
        ).toContain('Note 119');
    });

    it('the pager stays hidden when the single page holds everything', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: makeNotes(0, 3),
                    total: 3,
                }),
            })
        );

        await hook.loadNotesKeywordSearch();

        expect(pager().style.display).toBe('none');
    });

    it('discards a stale Load-more append when a fresh load supersedes it', async () => {
        hook.setMode('text'); // keep the superseding load on the keyword path
        // Page 1 on screen.
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: makeNotes(0, 100),
                    total: 250,
                }),
            })
        );
        await hook.loadNotesKeywordSearch();

        // The append fetch is deferred so we can supersede it mid-flight.
        let resolveAppend;
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('offset=100')) {
                return new Promise((res) => {
                    resolveAppend = () => res({
                        json: () => Promise.resolve({
                            success: true,
                            notes: makeNotes(100, 100),
                            total: 250,
                        }),
                    });
                });
            }
            // The superseding fresh (filtered) load.
            return Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: makeNotes(900, 3),
                    total: 3,
                }),
            });
        });

        const appendPromise = hook.loadMoreNotes();
        // A fresh load lands first, replacing the list and bumping the
        // generation the append captured.
        hook.setSearchState({ search: 'foo' });
        await hook.loadNotes();
        resolveAppend();
        await appendPromise;

        // The stale 100-note append was dropped — only the 3 fresh notes.
        expect(hook.getNotes().map((n) => n.id)).toEqual(['n900', 'n901', 'n902']);
    });

    it('re-enables the Load-more button when a fresh load supersedes an in-flight append', async () => {
        hook.setMode('text');
        // Page 1 on screen (100 of 250).
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({ success: true, notes: makeNotes(0, 100), total: 250 }),
            })
        );
        await hook.loadNotesKeywordSearch();

        // Defer the append so it stays in flight (uncancellable — no signal).
        let resolveAppend;
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('offset=100')) {
                return new Promise((res) => {
                    resolveAppend = () => res({
                        json: () => Promise.resolve({ success: true, notes: makeNotes(100, 100), total: 250 }),
                    });
                });
            }
            // The superseding fresh load ALSO has more pages, so its pager stays
            // visible (unlike the sibling test where total:3 hides it).
            return Promise.resolve({
                json: () => Promise.resolve({ success: true, notes: makeNotes(900, 100), total: 250 }),
            });
        });

        const appendPromise = hook.loadMoreNotes();
        const btn = document.querySelector('#ldr-notes-load-more button');
        expect(btn.disabled).toBe(true); // disabled while the append is out

        // A fresh load supersedes the append and resolves first, showing a
        // valid pager for the new list.
        hook.setSearchState({ search: 'foo' });
        await hook.loadNotes();

        // The freshly-shown pager's button must be clickable again — NOT stuck
        // disabled until the abandoned, uncancellable append eventually
        // resolves. Pre-fix, updateLoadMoreControl never touched btn.disabled,
        // so it stayed true here.
        expect(pager().style.display).toBe('flex');
        expect(btn.disabled).toBe(false);

        // cleanup: let the stale append resolve (it's dropped by the gen check).
        resolveAppend();
        await appendPromise;
        expect(btn.disabled).toBe(false);
    });

    it('refuses Load-more while a fresh load is in flight (no stale-offset append)', async () => {
        hook.setMode('text');
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: makeNotes(0, 100),
                    total: 300,
                }),
            })
        );
        await hook.loadNotesKeywordSearch();

        // A fresh load is in flight (deferred), so searchAbortController is
        // set. A Load-more click now must be refused — otherwise it would
        // capture the bumped generation but the pre-replacement offset and
        // slip a wrong-offset page past the guard.
        let resolveFresh;
        let sawOffsetFetch = false;
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('offset=')) sawOffsetFetch = true;
            return new Promise((res) => {
                resolveFresh = () => res({
                    json: () => Promise.resolve({
                        success: true,
                        notes: makeNotes(0, 100),
                        total: 300,
                    }),
                });
            });
        });

        const freshPromise = hook.loadNotes(); // in flight
        await hook.loadMoreNotes();            // must early-return
        expect(sawOffsetFetch).toBe(false);

        resolveFresh();
        await freshPromise;
    });

    it('a failed Load-more keeps the loaded notes on screen (does not blank the list)', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: makeNotes(0, 100),
                    total: 250,
                }),
            })
        );
        await hook.loadNotesKeywordSearch();
        const grid = document.getElementById('ldr-notes-grid');
        grid.style.display = 'grid';

        // The append fails.
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({ success: false, error: 'boom' }),
            })
        );
        await hook.loadMoreNotes();

        // The 100 already-loaded notes are untouched and still displayed;
        // the pager remains so the user can retry in place.
        expect(hook.getNotes()).toHaveLength(100);
        expect(grid.style.display).not.toBe('none');
        expect(pager().style.display).toBe('flex');
    });

    it('semantic mode hides the pager (AI results are relevance-capped, not paged)', async () => {
        // First put the pager on screen via a paged keyword listing.
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    notes: makeNotes(0, 100),
                    total: 120,
                }),
            })
        );
        await hook.loadNotesKeywordSearch();
        expect(pager().style.display).toBe('flex');

        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({ success: true, results: [] }),
            })
        );
        hook.setSearchState({ search: 'meaning of life' });
        await hook.loadNotesSemanticSearch();

        expect(pager().style.display).toBe('none');
    });
});

describe('load-more id dedup', () => {
    it('drops rows re-served by a shifted offset window instead of rendering duplicates', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            const params = new URL(url, 'http://x').searchParams;
            const offset = Number(params.get('offset') || 0);
            return Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    // Page 2 re-serves the tail of page 1 (a note edited in
                    // another tab moved to the front, shifting every offset
                    // by one) plus the genuinely new rows.
                    notes: offset === 0
                        ? makeNotes(0, 100)
                        : [...makeNotes(99, 1), ...makeNotes(100, 20)],
                    total: 120,
                }),
            });
        });

        await hook.loadNotesKeywordSearch();
        await hook.loadMoreNotes();

        const ids = hook.getNotes().map(n => n.id);
        expect(ids).toHaveLength(120);
        expect(new Set(ids).size).toBe(120);
        // The re-served row renders exactly once.
        const grid = document.getElementById('ldr-notes-grid').innerHTML;
        expect(grid.match(/Note 99\b/g)).toHaveLength(1);
    });
});

describe('load-more pager termination under offset shift', () => {
    it('closes the pager via the raw cursor even when a whole page is duplicates', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            const params = new URL(url, 'http://x').searchParams;
            const offset = Number(params.get('offset') || 0);
            return Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    // Page 2 (offset 100) re-serves page 1's rows entirely — a
                    // full-page shift where every returned row is a duplicate.
                    notes: offset === 0 ? makeNotes(0, 100) : makeNotes(0, 100),
                    total: 150,
                }),
            });
        });

        await hook.loadNotesKeywordSearch();
        // Page 1: 100 shown, pager open (100 < 150).
        expect(pager().style.display).not.toBe('none');

        await hook.loadMoreNotes();

        // Page 2 was all duplicates → 0 new rows displayed. Pre-fix the offset
        // was notes.length (frozen at 100) and hasMore was notes.length < total
        // (100 < 150 → true), wedging "Load more" open forever. With the raw
        // cursor the next offset advanced to 200 ≥ 150, so the pager closes.
        expect(hook.getNotes()).toHaveLength(100);
        expect(pager().style.display).toBe('none');
    });
});

describe('raw cursor reset across a mode switch', () => {
    it('re-pages from the NEW keyword listing (cursor reset), not the pre-semantic offset', async () => {
        // Keyword listing paged → the raw cursor advances to 100.
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                json: () => Promise.resolve({ success: true, notes: makeNotes(0, 100), total: 120 }),
            })
        );
        await hook.loadNotesKeywordSearch();
        expect(pager().style.display).toBe('flex');

        // Switch to semantic (must not touch / must not leak the keyword cursor).
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, results: [] }) })
        );
        hook.setSearchState({ search: 'meaning' });
        await hook.loadNotesSemanticSearch();

        // Return to keyword mode with a DIFFERENT, smaller listing.
        hook.setSearchState({ search: '' });
        const offsets = [];
        globalThis.safeFetch = vi.fn((url) => {
            offsets.push(Number(new URL(url, 'http://x').searchParams.get('offset') || 0));
            return Promise.resolve({
                json: () => Promise.resolve({ success: true, notes: makeNotes(500, 50), total: 200 }),
            });
        });
        await hook.loadNotesKeywordSearch(); // fresh load → cursor reset to 50
        await hook.loadMoreNotes();

        // Load-more must page from the RESET cursor (50), not the stale 100
        // carried over from the pre-semantic keyword listing.
        expect(offsets).toEqual([0, 50]);
    });
});

describe('load-more tolerates skipped rows without wedging', () => {
    it('closes the pager even when an offset shift skips a row (accepted tradeoff)', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            const offset = Number(new URL(url, 'http://x').searchParams.get('offset') || 0);
            return Promise.resolve({
                json: () => Promise.resolve({
                    success: true,
                    // Page 2 (offset 100) returns 101..149 — n100 was shifted
                    // earlier into the already-consumed window and is skipped,
                    // so the server has only 49 rows left (a SHORT page).
                    notes: offset === 0 ? makeNotes(0, 100) : makeNotes(101, 49),
                    total: 150,
                }),
            });
        });

        await hook.loadNotesKeywordSearch();
        await hook.loadMoreNotes();

        // A row was skipped, so fewer than `total` distinct notes are shown —
        // but the raw cursor reached 150, so the pager must CLOSE cleanly
        // rather than wedge open forever chasing the missing row.
        const ids = new Set(hook.getNotes().map(n => n.id));
        expect(ids.size).toBe(149);
        expect(ids.has('n100')).toBe(false); // the skipped row
        expect(pager().style.display).toBe('none');
    });
});

describe('a failed Load-more must not corrupt the cursor', () => {
    it('leaves the offset intact so the retry pages from where it left off', async () => {
        const offsets = [];
        let call = 0;
        globalThis.safeFetch = vi.fn((url) => {
            const off = Number(new URL(url, 'http://x').searchParams.get('offset') || 0);
            return Promise.resolve({
                json: () => {
                    call += 1;
                    if (call === 1) {
                        return Promise.resolve({ success: true, notes: makeNotes(0, 100), total: 300 });
                    }
                    // call 2 = the failed append; call 3 = the retry. Record
                    // both offsets — a failed append that wrongly advanced the
                    // cursor would make the retry skip page 2.
                    offsets.push(off);
                    if (call === 2) {
                        return Promise.resolve({ success: false, error: 'db hiccup' });
                    }
                    return Promise.resolve({ success: true, notes: makeNotes(100, 100), total: 300 });
                },
            });
        });

        await hook.loadNotesKeywordSearch(); // page 1 → cursor at 100
        await hook.loadMoreNotes();          // FAILS → cursor must stay 100
        await hook.loadMoreNotes();          // retry → must page from 100

        expect(offsets).toEqual([100, 100]); // failed + retry both at offset 100
        expect(hook.getNotes()).toHaveLength(200); // the retry actually appended
    });
});