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.

6.3 KB
Raw Blame
/**
 * Direct browser contracts for the library document-notes panel.
 *
 * These tests load the real component so its document ownership, encoded
 * FastAPI routes, annotation bridge, rendering, and mutation recovery remain
 * pinned through the migration.
 */

let originalLocation;

function setLocation(pathname = '/library/document/doc-1') {
    Object.defineProperty(window, 'location', {
        configurable: true,
        writable: true,
        value: {
            pathname,
            search: '',
            hash: '',
            href: pathname,
            host: 'localhost',
            protocol: 'http:',
        },
    });
}

function response(data) {
    return { json: () => Promise.resolve(data) };
}

function buildDocumentNotesDom(documentId = 'doc-1') {
    document.body.innerHTML = `
        <section id="document-notes-section">
            <button id="document-add-note-btn">Add note</button>
            <div id="document-notes-list"></div>
            <p id="document-notes-empty">No document notes.</p>
        </section>
        <article id="ldr-document-text-content"></article>
    `;
    document.getElementById('document-notes-section').dataset.documentId =
        documentId;
    document.getElementById('ldr-document-text-content').dataset.documentId =
        documentId;
}

async function loadComponent() {
    await import('@js/components/document_notes.js');
    return window.__documentNotesTest;
}

beforeAll(() => {
    originalLocation = window.location;
});

beforeEach(() => {
    vi.resetModules();
    setLocation();
    buildDocumentNotesDom();
    window.__VITEST_TEST__ = true;
    window.NotesShared = {
        toast: vi.fn(),
        postJson: vi.fn(),
        renderNoteRow: vi.fn((note) => {
            const row = document.createElement('article');
            row.dataset.noteId = note.id;
            row.textContent = note.title;
            return row;
        }),
    };
    window.LDRAnnotationSurface = { init: vi.fn() };
    globalThis.safeFetchWithAuth = vi.fn().mockResolvedValue(response({
        success: true,
        notes: [],
    }));
    vi.spyOn(SafeLogger, 'error').mockImplementation(() => {});
});

afterEach(() => {
    vi.restoreAllMocks();
    delete window.__documentNotesTest;
    document.body.replaceChildren();
});

afterAll(() => {
    Object.defineProperty(window, 'location', {
        configurable: true,
        writable: true,
        value: originalLocation,
    });
    delete window.__VITEST_TEST__;
});

it('initializes encoded note and annotation routes for the owned document', async () => {
    buildDocumentNotesDom('doc /?#');

    const notes = await loadComponent();

    await vi.waitFor(() => {
        expect(globalThis.safeFetchWithAuth).toHaveBeenCalledWith(
            '/notes/api/documents/doc%20%2F%3F%23/notes',
            { credentials: 'same-origin' },
        );
    });
    const config = window.LDRAnnotationSurface.init.mock.calls[0][0];
    expect(config.containerId).toBe('ldr-document-text-content');
    expect(config.endpoints.list)
        .toBe('/notes/api/documents/doc%20%2F%3F%23/annotations');
    expect(config.endpoints.create)
        .toBe('/notes/api/documents/doc%20%2F%3F%23/annotations');
    expect(config.endpoints.deleteFor('note /?#'))
        .toBe('/notes/api/documents/doc%20%2F%3F%23/annotations/note%20%2F%3F%23');
    expect(config.onChanged).toBe(notes.loadDocumentNotes);
});

it('renders the FastAPI notes envelope and owns the empty state', async () => {
    globalThis.safeFetchWithAuth.mockResolvedValue(response({
        success: true,
        notes: [
            { id: 'note-1', title: 'Migration note' },
            { id: 'note-2', title: 'Follow-up' },
        ],
    }));

    await loadComponent();

    await vi.waitFor(() => {
        expect([...document.getElementById('document-notes-list').children]
            .map((row) => row.dataset.noteId)).toEqual(['note-1', 'note-2']);
    });
    expect(document.getElementById('document-notes-empty').style.display)
        .toBe('none');
});

it('renders a fixed load failure without injecting the server message', async () => {
    globalThis.safeFetchWithAuth.mockResolvedValue(response({
        success: false,
        error: '<img src=x onerror="window.pwned=true">',
    }));

    await loadComponent();

    await vi.waitFor(() => {
        expect(document.getElementById('document-notes-empty').textContent)
            .toBe("Couldn't load notes for this document.");
    });
    expect(document.getElementById('document-notes-empty').querySelector('img'))
        .toBeNull();
    expect(window.pwned).toBeUndefined();
    expect(SafeLogger.error).toHaveBeenCalledWith(
        'Error loading document notes:',
        expect.objectContaining({
            message: '<img src=x onerror="window.pwned=true">',
        }),
    );
});

it('creates a linked note once and encodes the returned navigation ID', async () => {
    window.NotesShared.postJson.mockResolvedValue({ note_id: 'note /?#' });
    await loadComponent();

    document.getElementById('document-add-note-btn').click();

    await vi.waitFor(() => {
        expect(window.NotesShared.postJson).toHaveBeenCalledWith(
            '/notes/api/documents/doc-1/notes',
            {},
        );
    });
    expect(document.getElementById('document-add-note-btn').disabled).toBe(true);
    expect(window.location.href).toBe('/notes/note%20%2F%3F%23');
});

it('re-enables note creation and surfaces a failed mutation', async () => {
    window.NotesShared.postJson.mockRejectedValue(new Error('quota reached'));
    await loadComponent();

    document.getElementById('document-add-note-btn').click();

    await vi.waitFor(() => {
        expect(window.NotesShared.toast)
            .toHaveBeenCalledWith('quota reached', 'error');
    });
    expect(document.getElementById('document-add-note-btn').disabled)
        .toBe(false);
    expect(SafeLogger.error).toHaveBeenCalledWith(
        'Error creating note for document:',
        expect.objectContaining({ message: 'quota reached' }),
    );
});

it('does nothing when neither document surface owns an ID', async () => {
    document.body.innerHTML = `
        <section id="document-notes-section"></section>
        <article id="ldr-document-text-content"></article>
    `;

    await loadComponent();

    expect(globalThis.safeFetchWithAuth).not.toHaveBeenCalled();
    expect(window.LDRAnnotationSurface.init).not.toHaveBeenCalled();
});