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.9 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — compareVersion (semantic-diff modal).
 *
 * compareVersion opens the diff modal with a loading placeholder, fetches the
 * semantic diff of a version against current, and renders the added/removed/
 * modified sections; a failure shows the error in the modal body + a toast. A
 * single-flight guard drops a re-trigger while one diff is in flight.
 *
 * 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 ?? '');
    window.bootstrap = { Modal: { getOrCreateInstance: () => ({ show: () => {} }) } };
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

beforeEach(() => {
    document.body.innerHTML = '<div id="diffModal"></div><div id="diff-modal-body"></div>';
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [] });
    hook.resetDiffState();
});

const body = () => document.getElementById('diff-modal-body').innerHTML;

describe('compareVersion', () => {
    it('does not repaint or reopen a diff dismissed while the request is in flight', async () => {
        let resolveDiff;
        const pendingDiff = new Promise(resolve => {
            resolveDiff = resolve;
        });
        globalThis.safeFetch = vi.fn(() => pendingDiff);
        const show = vi.fn();
        window.bootstrap.Modal.getOrCreateInstance = vi.fn(() => ({ show }));
        const modalEl = document.getElementById('diffModal');

        const comparison = hook.compareVersion('ver-dismissed-3299');
        await vi.waitFor(() => expect(globalThis.safeFetch).toHaveBeenCalledOnce());
        expect(show).toHaveBeenCalledOnce();
        expect(body()).toContain('Computing semantic diff');

        modalEl.dispatchEvent(new Event('hidden.bs.modal'));
        resolveDiff({
            json: () => Promise.resolve({
                success: true,
                diff: { summary: 'Late result must stay dismissed' },
            }),
        });
        await comparison;

        expect(show).toHaveBeenCalledOnce();
        expect(body()).not.toContain('Late result must stay dismissed');
    });

    it('renders the diff sections on success', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/versions/semantic-diff');
            expect(url).toContain('version2=current');
            return Promise.resolve({ json: () => Promise.resolve({
                success: true,
                diff: { summary: 'Some changes', added: ['a new line'], removed: ['an old line'] },
            }) });
        });

        await hook.compareVersion('ver12345678');

        expect(body()).toContain('Some changes');
        expect(body()).toContain('a new line');
        expect(body()).toContain('an old line');
    });

    it('shows the error in the modal body on failure', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'diff boom' }) })
        );

        await hook.compareVersion('ver12345678');

        expect(body()).toContain('diff boom');
        expect(window.ui.showMessage).toHaveBeenCalledWith('diff boom', 'error');
    });

    it('single-flight guard: a re-trigger while in flight fetches once', async () => {
        let resolveFirst;
        const fetchMock = vi.fn(() => new Promise((r) => { resolveFirst = r; }));
        globalThis.safeFetch = fetchMock;

        hook.compareVersion('ver12345678'); // in flight
        await hook.compareVersion('ver12345678'); // guarded

        expect(fetchMock).toHaveBeenCalledTimes(1);
        resolveFirst({ json: () => Promise.resolve({ success: false }) });
    });
});