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.

7.2 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — AI panel reopen-after-dismiss guard.
 *
 * Every AI action opens the shared results panel in a "Working…" state and
 * renders into it when the response lands. Pre-fix, only the fact-check
 * flow guarded dismissal (_factCheckGeneration): for every other action
 * (Summarize, Key Concepts, Similar Notes, Version Details, …), closing
 * the panel while the multi-second request was in flight was undone the
 * moment the response arrived — showAIResults unconditionally set
 * display:block, popping the dismissed panel back open over the page.
 *
 * Post-fix, closeAIResults bumps _aiPanelGeneration and the action's
 * success path drops the render when the generation moved.
 *
 * 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.getCSRFToken = () => 'csrf';
    globalThis.escapeHtml = (s) =>
        String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');

    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

beforeEach(() => {
    document.body.innerHTML =
        '<div id="ldr-ai-results-panel" style="display:none"><span id="ldr-ai-results-title"></span>' +
        '<div id="ldr-ai-results-content"></div></div>';
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'note-1', title: 'T', content: 'body', tags: [] });
    hook.resetAiActionGuards();
});

function panelDisplay() {
    return document.getElementById('ldr-ai-results-panel').style.display;
}

describe('AI panel dismissed while a request is in flight', () => {
    it('does not reopen when the response lands after the user closed it', async () => {
        let resolveRequest;
        globalThis.safeFetch = vi.fn(() => new Promise((r) => { resolveRequest = r; }));

        const action = hook.summarizeNote();

        // The action opened the panel in its "Working…" state.
        expect(panelDisplay()).toBe('block');

        // The user dismisses it while the LLM call is still running.
        hook.closeAIResults();
        expect(panelDisplay()).toBe('none');

        // The response lands — pre-fix this popped the panel back open.
        resolveRequest({ json: () => Promise.resolve({ success: true, summary: 'late result' }) });
        await action;

        expect(panelDisplay()).toBe('none');
        expect(document.getElementById('ldr-ai-results-content').innerHTML)
            .not.toContain('late result');
    });

    it('still renders normally when the panel was not dismissed', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, summary: 'fresh result' }) })
        );

        await hook.summarizeNote();

        expect(panelDisplay()).toBe('block');
        expect(document.getElementById('ldr-ai-results-content').innerHTML)
            .toContain('fresh result');
    });

    it('a dismissed panel can be reopened by a NEW action afterwards', async () => {
        // First action dismissed mid-flight...
        let resolveFirst;
        globalThis.safeFetch = vi.fn(() => new Promise((r) => { resolveFirst = r; }));
        const first = hook.summarizeNote();
        hook.closeAIResults();
        resolveFirst({ json: () => Promise.resolve({ success: true, summary: 'stale' }) });
        await first;
        expect(panelDisplay()).toBe('none');

        // ...must not poison the next explicit action.
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, summary: 'second run' }) })
        );
        await hook.summarizeNote();

        expect(panelDisplay()).toBe('block');
        expect(document.getElementById('ldr-ai-results-content').innerHTML)
            .toContain('second run');
    });
});

describe("one AI action's failure must not eat a concurrent action's result", () => {
    it('Key Concepts still renders when a concurrent Summarize fails', async () => {
        // Summarize: held, then fails. Key Concepts: starts while Summarize
        // is in flight (different buttonId → allowed), then succeeds.
        let rejectSummary;
        let resolveConcepts;
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/summarize')) {
                return new Promise((_, rej) => { rejectSummary = rej; });
            }
            if (url.includes('/key-concepts')) {
                return new Promise((res) => { resolveConcepts = res; });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true }) });
        });

        const summary = hook.summarizeNote();
        expect(panelDisplay()).toBe('block'); // Summarize claimed the panel

        const concepts = hook.extractKeyConcepts();
        // Key Concepts now owns the panel (its "Working…" is showing).
        expect(panelDisplay()).toBe('block');

        // Summarize fails: pre-fix this closed the panel and bumped the
        // generation, dropping Key Concepts' result.
        rejectSummary(new Error('summary boom'));
        await summary;
        // Panel must still be open (Key Concepts owns it).
        expect(panelDisplay()).toBe('block');

        resolveConcepts({
            json: () => Promise.resolve({
                success: true,
                concepts: { entities: [], concepts: ['transformers'], themes: [] },
            }),
        });
        await concepts;

        expect(panelDisplay()).toBe('block');
        expect(document.getElementById('ldr-ai-results-content').innerHTML)
            .toContain('transformers');
    });
});

describe('verifyNote claims the shared panel from an in-flight action', () => {
    it("drops the slower action's late render instead of clobbering the fact-check panel", async () => {
        hook.resetVerifyState();
        let resolveSummarize;
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/summarize')) {
                return new Promise((r) => { resolveSummarize = r; });
            }
            // fact-check POST: left pending — only the first paint matters.
            return new Promise(() => {});
        });

        const summarize = hook.summarizeNote();
        expect(panelDisplay()).toBe('block');

        // Fact-check takes over the shared panel while Summarize is in flight.
        hook.verifyNote();
        expect(document.getElementById('ldr-ai-results-title').textContent)
            .toBe('Fact-check');

        // Summarize lands late — pre-fix it repainted the panel mid-fact-check.
        resolveSummarize({ json: () => Promise.resolve({ success: true, summary: 'late summary' }) });
        await summarize;

        expect(document.getElementById('ldr-ai-results-title').textContent)
            .toBe('Fact-check');
        expect(document.getElementById('ldr-ai-results-content').innerHTML)
            .toContain('Extracting checkable claims');
        expect(document.getElementById('ldr-ai-results-content').innerHTML)
            .not.toContain('late summary');

        hook.resetVerifyState();
    });
});