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.3 KB
Raw Blame
/**
 * Tests for pages/notes.js pure-logic helpers — mergeTieredNotes / formatNoteDate.
 *
 * mergeTieredNotes flattens the hybrid-search tiers into one list: tier1 merges
 * the semantic similarity + snippet onto the history item; tier2 passes the
 * history item through; tier3 passes the semantic result through — in that
 * order. formatNoteDate renders a relative date (Today / Yesterday / Nd ago)
 * or an absolute date, and '' for empty/invalid input.
 *
 * 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: [] }) })
    );
    // mergeTieredNotes delegates to the shared component's flatten.
    await import('@js/components/semantic_search.js');
    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

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

describe('mergeTieredNotes', () => {
    it('merges similarity + snippet for tier1, passes tier2/tier3 through, in order', () => {
        const tiered = {
            tier1: [{
                historyItem: { id: 'a', title: 'A' },
                semanticMatch: { similarity: 0.91, snippet: 'snip-a' },
            }],
            tier2: [{ historyItem: { id: 'b', title: 'B' } }],
            tier3: [{ semanticResult: { id: 'c', title: 'C', similarity: 0.4 } }],
        };

        const out = hook.mergeTieredNotes(tiered);

        expect(out.map((n) => n.id)).toEqual(['a', 'b', 'c']); // tier order preserved
        // tier1: similarity + snippet merged onto a COPY of the history item.
        expect(out[0]).toMatchObject({ id: 'a', title: 'A', similarity: 0.91, content_preview: 'snip-a' });
        expect(tiered.tier1[0].historyItem).not.toHaveProperty('similarity'); // didn't mutate source
        // tier2 / tier3 pass through unchanged.
        expect(out[1]).toBe(tiered.tier2[0].historyItem);
        expect(out[2]).toBe(tiered.tier3[0].semanticResult);
    });

    it('omits content_preview when the tier1 snippet is absent', () => {
        const out = hook.mergeTieredNotes({
            tier1: [{ historyItem: { id: 'a' }, semanticMatch: { similarity: 0.5 } }],
            tier2: [],
            tier3: [],
        });
        expect(out[0].similarity).toBe(0.5);
        expect(out[0]).not.toHaveProperty('content_preview');
    });
});

describe('formatNoteDate', () => {
    const daysAgo = (n) => new Date(Date.now() - n * 86400000).toISOString();

    it('returns "" for empty or invalid input', () => {
        expect(hook.formatNoteDate('')).toBe('');
        expect(hook.formatNoteDate(null)).toBe('');
        expect(hook.formatNoteDate(undefined)).toBe('');
    });

    it('renders relative dates within the last week', () => {
        expect(hook.formatNoteDate(daysAgo(0))).toBe('Today');
        expect(hook.formatNoteDate(daysAgo(1))).toBe('Yesterday');
        expect(hook.formatNoteDate(daysAgo(3))).toBe('3d ago');
    });

    it('falls back to an absolute date beyond a week', () => {
        const result = hook.formatNoteDate(daysAgo(30));
        expect(result).not.toBe('');
        expect(['Today', 'Yesterday']).not.toContain(result);
        expect(result).not.toMatch(/\dd ago/);
    });
});