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.

3.0 KB
Raw Blame
/**
 * Tests for pages/notes.js — createNoteCard render logic.
 *
 * createNoteCard builds a note grid card: title/preview, a real <a href>, a
 * "% match" similarity badge only when similarity is shown AND present, a
 * pinned class, a select-mode checkbox + selected class, and research/indexed
 * stat badges. Content preview falls back to a truncation of `content`.
 *
 * 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.escapeHtml = (s) =>
        String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

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

beforeEach(() => {
    hook.setSelectState({ mode: false, ids: [] });
});

const BASE = { id: 'n1', title: 'My Note', content_preview: 'a preview', tags: ['x', 'y'] };

describe('createNoteCard', () => {
    it('renders a linked card with title and preview', () => {
        const html = hook.createNoteCard(BASE, false);
        expect(html).toContain('href="/notes/n1"');
        expect(html).toContain('My Note');
        expect(html).toContain('a preview');
    });

    it('shows a "% match" badge only when similarity is shown AND present', () => {
        expect(hook.createNoteCard({ ...BASE, similarity: 0.876 }, true)).toContain('88% match');
        // showSimilarity false → no badge even with a similarity value.
        expect(hook.createNoteCard({ ...BASE, similarity: 0.876 }, false)).not.toContain('% match');
        // shown but no similarity value → no badge.
        expect(hook.createNoteCard(BASE, true)).not.toContain('% match');
    });

    it('adds a pinned class for a pinned note', () => {
        expect(hook.createNoteCard({ ...BASE, pinned: true }, false)).toContain('ldr-pinned');
        expect(hook.createNoteCard(BASE, false)).not.toContain('ldr-pinned');
    });

    it('renders a checkbox and selected class in select mode', () => {
        hook.setSelectState({ mode: true, ids: ['n1'] });
        const html = hook.createNoteCard(BASE, false);
        expect(html).toContain('ldr-note-card-checkbox');
        expect(html).toContain('ldr-note-card-selected');
        expect(html).toContain('fa-check-square'); // selected glyph
    });

    it('renders research and indexed stat badges when present', () => {
        const html = hook.createNoteCard({ ...BASE, research_count: 3, is_indexed: true }, false);
        expect(html).toContain('3');
        expect(html).toContain('ldr-indexed');
    });

    it('falls back to truncating content when no preview is given', () => {
        const long = 'z'.repeat(200);
        const html = hook.createNoteCard({ id: 'n2', title: 'T', content: long, tags: [] }, false);
        expect(html).toContain('...'); // truncated past 150 chars
        expect(html).not.toContain('z'.repeat(200));
    });
});