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.

10.3 KB
Raw Blame
/**
 * Direct runtime contracts for the results-page research-notes component.
 * Exercises the real endpoint builders, mutation ownership, annotation action,
 * and browser state instead of duplicating the component's implementation.
 */

let notes;
let originalLocation;

function setLocation(pathname = '/results/research-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) };
}

beforeAll(async () => {
    originalLocation = window.location;
    document.body.replaceChildren();
    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.URLBuilder = {
        extractResearchIdFromPattern: vi.fn(() => null),
    };
    globalThis.safeFetchWithAuth = vi.fn();

    await import('@js/components/research_notes.js');
    notes = window.__researchNotesTest;
});

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

beforeEach(() => {
    setLocation();
    document.body.innerHTML = `
        <section id="research-notes-section">
            <button id="research-add-note-btn"></button>
            <button id="research-save-as-note-btn"></button>
            <div id="research-notes-list"></div>
            <p id="research-notes-empty">No research notes.</p>
        </section>
        <button id="research-save-as-note-top-btn"></button>
        <h1 id="result-query">Research query</h1>
        <main id="results-content"></main>
    `;
    notes.reset();
    notes.setResearchId('research-1');
    URLBuilder.extractResearchIdFromPattern.mockReset()
        .mockReturnValue('research-1');
    window.LDRAnnotationSurface.init.mockReset();
    window.NotesShared.toast.mockReset();
    window.NotesShared.postJson.mockReset();
    window.NotesShared.renderNoteRow.mockClear();
    globalThis.safeFetchWithAuth = vi.fn()
        .mockResolvedValue(response({ success: true, notes: [] }));
    vi.spyOn(SafeLogger, 'error').mockImplementation(() => {});
});

afterEach(() => {
    vi.restoreAllMocks();
});

it('hides the feature when the results route has no research owner', () => {
    URLBuilder.extractResearchIdFromPattern.mockReturnValue(null);

    notes.initResearchNotes();

    expect(document.getElementById('research-notes-section').style.display)
        .toBe('none');
    expect(window.LDRAnnotationSurface.init).not.toHaveBeenCalled();
    expect(globalThis.safeFetchWithAuth).not.toHaveBeenCalled();
});

it('initializes encoded notes and annotation endpoints for the current run', async () => {
    URLBuilder.extractResearchIdFromPattern.mockReturnValue('run /?#');

    notes.initResearchNotes();

    await vi.waitFor(() => {
        expect(globalThis.safeFetchWithAuth).toHaveBeenCalledWith(
            '/notes/api/research/run%20%2F%3F%23/notes',
            { credentials: 'same-origin' },
        );
    });
    const config = window.LDRAnnotationSurface.init.mock.calls[0][0];
    expect(config.containerId).toBe('results-content');
    expect(config.endpoints.list)
        .toBe('/notes/api/research/run%20%2F%3F%23/annotations');
    expect(config.endpoints.create)
        .toBe('/notes/api/research/run%20%2F%3F%23/annotations');
    expect(config.endpoints.deleteFor('note /?#'))
        .toBe('/notes/api/research/run%20%2F%3F%23/annotations/note%20%2F%3F%23');
    expect(config.extraActions[0]).toMatchObject({
        icon: 'fas fa-quote-right',
        label: 'Clip to note',
    });
    expect(config.onChanged).toBe(notes.loadResearchNotes);
});

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

    await notes.loadResearchNotes();

    expect(globalThis.safeFetchWithAuth).toHaveBeenCalledWith(
        '/notes/api/research/research-1/notes',
        { credentials: 'same-origin' },
    );
    expect([...document.getElementById('research-notes-list').children]
        .map((row) => row.dataset.noteId)).toEqual(['note-1', 'note-2']);
    expect(document.getElementById('research-notes-empty').style.display)
        .toBe('none');
});

it('keeps the report usable and does not render an untrusted load error', async () => {
    globalThis.safeFetchWithAuth.mockResolvedValue(response({
        success: false,
        error: '<img src=x onerror="window.pwned=true">',
    }));

    await notes.loadResearchNotes();

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

it('does not let an older transport failure replace a newer notes refresh', async () => {
    let rejectOlder;
    globalThis.safeFetchWithAuth
        .mockImplementationOnce(() => new Promise((resolve, reject) => {
            rejectOlder = reject;
        }))
        .mockResolvedValueOnce(response({
            success: true,
            notes: [{ id: 'fresh', title: 'Fresh note' }],
        }));

    const olderLoad = notes.loadResearchNotes();
    const newerLoad = notes.loadResearchNotes();
    await newerLoad;
    rejectOlder(new Error('stale network failure'));
    await olderLoad;

    expect(document.getElementById('research-notes-list').textContent)
        .toBe('Fresh note');
    expect(document.getElementById('research-notes-empty').style.display)
        .toBe('none');
    expect(SafeLogger.error).not.toHaveBeenCalled();
});

it('creates an owned starter note and encodes the returned navigation ID', async () => {
    window.NotesShared.postJson.mockResolvedValue({ note_id: 'note /?#' });

    await notes.addNoteForResearch();

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

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

    await notes.addNoteForResearch();

    expect(document.getElementById('research-add-note-btn').disabled)
        .toBe(false);
    expect(window.NotesShared.toast)
        .toHaveBeenCalledWith('quota reached', 'error');
});

it('locks both save-as-note entry points around one in-flight mutation', async () => {
    let rejectSave;
    window.NotesShared.postJson.mockImplementationOnce(() => new Promise((resolve, reject) => {
        rejectSave = reject;
    }));

    const first = notes.saveReportAsNote();
    const duplicate = notes.saveReportAsNote();

    expect(window.NotesShared.postJson).toHaveBeenCalledOnce();
    expect(document.getElementById('research-save-as-note-btn').disabled)
        .toBe(true);
    expect(document.getElementById('research-save-as-note-top-btn').disabled)
        .toBe(true);

    rejectSave(new Error('save failed'));
    await Promise.all([first, duplicate]);

    expect(document.getElementById('research-save-as-note-btn').disabled)
        .toBe(false);
    expect(document.getElementById('research-save-as-note-top-btn').disabled)
        .toBe(false);
    expect(window.NotesShared.toast)
        .toHaveBeenCalledWith('save failed', 'error');
});

it('saves one linked report copy and encodes its returned note ID', async () => {
    window.NotesShared.postJson.mockResolvedValue({ note_id: 'copy/id' });

    await notes.saveReportAsNote();

    expect(window.NotesShared.postJson).toHaveBeenCalledWith(
        '/notes/api/research/research-1/save-as-note',
        {},
    );
    expect(window.location.href).toBe('/notes/copy%2Fid');
});

it('clips a bounded quote with an escaped markdown provenance label and refreshes', async () => {
    const query = 'Report ](javascript:alert(1))\n[more]\\';
    document.getElementById('result-query').textContent = query;
    const quote = 'x'.repeat(4010);
    window.NotesShared.postJson.mockResolvedValue({ note_id: 'clip-1' });

    await notes.clipSelectionToNote({ text: quote });

    expect(window.NotesShared.postJson).toHaveBeenCalledOnce();
    const [url, body] = window.NotesShared.postJson.mock.calls[0];
    expect(url).toBe('/notes/api/research/research-1/notes');
    expect(body.title).toBe(`Clip: ${'x'.repeat(60)}`);
    expect(body.content).toContain(`> ${'x'.repeat(4000)}…`);
    expect(body.content).toContain('\\]\\(javascript:alert\\(1\\)\\)');
    expect(body.content).toContain('\\[more\\]\\\\](/results/research-1)');
    expect(body.content).not.toContain('](javascript:');
    expect(window.NotesShared.toast)
        .toHaveBeenCalledWith('Clipped to note', 'success');
    await vi.waitFor(() => {
        expect(globalThis.safeFetchWithAuth).toHaveBeenCalledWith(
            '/notes/api/research/research-1/notes',
            { credentials: 'same-origin' },
        );
    });
});

it('does not refresh after a failed clip mutation', async () => {
    window.NotesShared.postJson.mockRejectedValue(new Error('clip rejected'));

    await notes.clipSelectionToNote({ text: 'A useful excerpt' });

    expect(window.NotesShared.toast)
        .toHaveBeenCalledWith('clip rejected', 'error');
    expect(globalThis.safeFetchWithAuth).not.toHaveBeenCalled();
});