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.

2.6 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — countWords + generateTableOfContents.
 *
 * countWords counts whitespace-separated words (0 for empty/falsy).
 * generateTableOfContents scans the rendered note for h1/h2/h3, assigns each
 * an id, and builds a leveled TOC list with anchor links — or a "No headings
 * found" placeholder when there are none.
 *
 * Driven via the production-inert window.__noteDetailTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
    );
    globalThis.escapeHtml = (s) => String(s ?? '');
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

describe('countWords', () => {
    it('counts whitespace-separated words', () => {
        expect(hook.countWords('one two three')).toBe(3);
        expect(hook.countWords('  spaced   out  ')).toBe(2);
        expect(hook.countWords('single')).toBe(1);
    });

    it('returns 0 for empty or falsy input', () => {
        expect(hook.countWords('')).toBe(0);
        expect(hook.countWords(null)).toBe(0);
        expect(hook.countWords('   ')).toBe(0);
    });
});

describe('generateTableOfContents', () => {
    it('builds a leveled TOC from the rendered headings and assigns ids', () => {
        document.body.innerHTML =
            '<div id="note-content-rendered"><h1>Intro</h1><p>x</p><h2>Details</h2><h3>Sub</h3></div>' +
            '<ul id="toc-list"></ul>';

        hook.generateTableOfContents();

        const items = document.querySelectorAll('#toc-list .ldr-toc-item');
        expect(items.length).toBe(3);
        expect(items[0].className).toContain('ldr-level-1');
        expect(items[1].className).toContain('ldr-level-2');
        expect(items[2].className).toContain('ldr-level-3');
        expect(document.querySelector('#toc-list a').textContent).toBe('Intro');
        // Each heading got an id so the anchor links resolve.
        const headings = document.querySelectorAll('#note-content-rendered h1, #note-content-rendered h2, #note-content-rendered h3');
        headings.forEach((h) => expect(h.id).toMatch(/^heading-\d+$/));
    });

    it('shows a placeholder when there are no headings', () => {
        document.body.innerHTML =
            '<div id="note-content-rendered"><p>no headings here</p></div><ul id="toc-list"></ul>';

        hook.generateTableOfContents();

        expect(document.getElementById('toc-list').innerHTML.toLowerCase()).toContain('no headings found');
    });
});