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.2 KB
Raw Blame
/** Browser bootstrap contract for distinguishing missing notes from outages. */

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/note_detail.html',
);
const NOTE_ID = 'note-load-state-3299';

class ModalStub {
    show = vi.fn();
    hide = vi.fn();

    static getOrCreateInstance() {
        return new ModalStub();
    }

    static getInstance() {
        return new ModalStub();
    }
}

class IntersectionObserverStub {
    observe = vi.fn();
    disconnect = vi.fn();
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
    delete window.noteId;
    delete window.api;
    delete window.ui;
    delete window.bootstrap;
    delete window.__VITEST_TEST__;
});

it('shows a retryable state for HTTP 500 and not-found only for HTTP 404', async () => {
    vi.resetModules();
    delete window.__VITEST_TEST__;
    // eslint-disable-next-line no-unsanitized/property -- checked-in template is the browser fixture.
    document.body.innerHTML = readFileSync(TEMPLATE_PATH, 'utf8');
    window.noteId = NOTE_ID;
    window.api = { getCsrfToken: vi.fn(() => 'csrf-load-state') };
    window.ui = { showMessage: vi.fn() };
    window.bootstrap = { Modal: ModalStub };
    globalThis.bootstrap = window.bootstrap;
    vi.stubGlobal('IntersectionObserver', IntersectionObserverStub);
    vi.stubGlobal('URLValidator', { safeAssign: vi.fn() });

    let noteLoads = 0;
    const safeFetchWithAuth = vi.fn((input) => {
        const url = String(input);
        if (url === `/notes/api/notes/${NOTE_ID}`) {
            noteLoads += 1;
            return Promise.resolve(new Response(JSON.stringify({
                detail: noteLoads === 1
                    ? 'Temporary database outage'
                    : 'Note not found',
            }), {
                status: noteLoads === 1 ? 500 : 404,
            }));
        }
        if (url === '/library/api/collections') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                collections: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected note bootstrap request: ${url}`);
    });
    vi.stubGlobal('safeFetchWithAuth', safeFetchWithAuth);

    await import('@js/pages/note-detail.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(document.getElementById('note-load-error').style.display)
            .toBe('block');
    });
    expect(document.getElementById('note-not-found').style.display).toBe('none');
    expect(document.getElementById('note-content-wrapper').style.display)
        .toBe('none');

    document.querySelector('[data-action="retry-load-note"]').click();

    await vi.waitFor(() => {
        expect(noteLoads).toBe(2);
        expect(document.getElementById('note-not-found').style.display)
            .toBe('block');
    });
    expect(document.getElementById('note-load-error').style.display).toBe('none');
    expect(document.getElementById('note-content-wrapper').style.display)
        .toBe('none');
});