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.

32.9 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * Defensive output-encoding coverage for browser URL/storage sources.
 *
 * Source -> sink map (verified 2026-08-20):
 *
 * location.search
 *   - followup.js:52-55 reads `research_id`; the value becomes
 *     `parentResearchId`, then JSON data in the constant-URL fetches at
 *     :248-258 and :300-311. It is never rendered or persisted.
 *   - services/api.js:67 reads the whole search string (with pathname/hash),
 *     encodeURIComponent encodes it, and :72 assigns the result to the
 *     hardcoded `/auth/login?next=` navigation target.
 *   - components/library_search_ui.js:67/:90/:107/:124 preserves existing
 *     query parameters with URLSearchParams, then :74/:97/:114/:131 assigns
 *     the serialized result back to location.search. No DOM/fetch/storage sink.
 *   - components/chat.js reads `q`, removes the query string with
 *     history.replaceState and pre-fills an empty chat without taking focus.
 *     Explicit Send renders it through textContent and sends it as JSON
 *     request data. It never enters a fetch URL or submits on page load.
 *   - components/logpanel.js:481 only checks for the literal `debug=logs`;
 *     matching controls a delayed toggle click and does not render the URL.
 *
 * location.hash
 *   - services/api.js:67 follows the encoded login-return flow above.
 *   - components/logpanel.js:473/:481 compares `#logs` or checks for `debug`;
 *     matching controls a delayed toggle click and does not render the hash.
 *
 * rerunConfig
 *   - Recon correction: this is sessionStorage, not localStorage.
 *   - components/history.js:821-829 writes `{query, mode}` and navigates to `/`.
 *   - components/research.js:195-241 parses/removes it, assigns query through
 *     input.value, selects mode through CSS.escape, and shows static notice HTML.
 *     A later submit sends the query as JSON data at :3248-3283; the stored mode
 *     is not submitted unless it matches an existing mode option.
 *   - research_form.js has no rerunConfig read or write.
 *
 * Coverage note: grep of the complete static/js tree found no document.referrer or
 * window.name reader. It did find the location.hash readers above, so they are
 * exercised here rather than recorded as absent.
 */

import '@js/config/urls.js';
import '@js/utils/alert-helpers.js';
import '@js/security/xss-protection.js';
import '@js/utils/form-validation.js';
import '@js/components/custom_dropdown.js';
import '@js/services/api.js';
import { afterAll, afterEach, describe, expect, test, vi } from 'vitest';

const IMG_PAYLOAD = '<img src=x onerror=alert(1)>';
const SVG_PAYLOAD = '"><svg onload=alert(1)>';
const ATTR_PAYLOAD = 'x" onmouseover="alert(1)';
const JAVASCRIPT_URL = 'javascript:alert(1)';
const SPECIAL_VALUES = [
    IMG_PAYLOAD,
    SVG_PAYLOAD,
    ATTR_PAYLOAD,
    JAVASCRIPT_URL,
];
const BENIGN_CONTROL = 'Benign control value';
const START_RESEARCH_URL = '/api/start_research';
const DEFAULT_RESEARCH_PAYLOAD = {
    status: 'error',
    message: 'Benign stubbed response',
    settings: {},
    providers: {},
    provider_options: [{
        value: 'OLLAMA',
        label: 'Benign Ollama provider',
        is_cloud: false,
    }],
    engine_options: [],
    engines: {},
};
const nativeLocation = window.location;
const originalFetch = globalThis.fetch;
const originalApi = window.api;
const originalUrlBuilder = window.URLBuilder;
const originalUrlValidator = window.URLValidator;

function jsonResponse(payload) {
    return Promise.resolve({
        ok: true,
        status: 200,
        statusText: 'OK',
        json: () => Promise.resolve(payload),
        text: () => Promise.resolve(''),
    });
}

function installMockLocation(overrides = {}) {
    const location = {
        href: 'http://localhost/',
        protocol: 'http:',
        host: 'localhost',
        pathname: '/',
        search: '',
        hash: '',
        ...overrides,
    };
    Object.defineProperty(window, 'location', {
        configurable: true,
        writable: true,
        value: location,
    });
    return location;
}

function restoreNativeLocation() {
    Object.defineProperty(window, 'location', {
        configurable: true,
        writable: true,
        value: nativeLocation,
    });
}

function eventHandlerAttributes(root) {
    const found = [];
    for (const element of root.querySelectorAll('*')) {
        for (const name of element.getAttributeNames()) {
            if (/^on/i.test(name)) found.push(`${element.tagName}[${name}]`);
        }
    }
    return found;
}

function expectInert(root) {
    expect(root.querySelectorAll('script, img, svg')).toHaveLength(0);
    expect(eventHandlerAttributes(root)).toEqual([]);
    for (const link of root.querySelectorAll('a[href]')) {
        expect(link.getAttribute('href') ?? '').not.toMatch(/^\s*javascript:/i);
    }
}

function installHistoryPage() {
    document.body.innerHTML = `
        <p id="benign-control">Benign history item</p>
        <div id="history-items"></div>
        <input id="history-search">
        <button id="clear-history-btn" type="button">Clear</button>
        <div id="history-empty-message"></div>
    `;
}

function installResearchPage() {
    document.body.innerHTML = `
        <p id="benign-control">Benign research form</p>
        <form id="research-form">
            <div id="research-alert" role="alert" style="display:none"></div>
            <div id="research-error-alert" style="display:none"></div>
            <textarea id="query" name="query"></textarea>
            <div class="ldr-privacy-panel" data-scope="adaptive">
                <i id="ldr-privacy-panel-icon"></i>
                <select id="policy_egress_scope" name="policy_egress_scope">
                    <option value="adaptive" selected>Adaptive</option>
                </select>
                <input type="checkbox" id="llm_require_local_endpoint">
                <input type="checkbox" id="embeddings_require_local">
            </div>
            <label class="ldr-mode-option active" data-mode="quick">
                <input id="mode-quick" type="radio" name="research_mode" value="quick" checked>
                Quick
            </label>
            <button type="button" class="ldr-advanced-options-toggle ldr-open" aria-expanded="true">
                <i class="fas fa-chevron-up"></i><span class="sr-only"></span>
            </button>
            <div class="ldr-advanced-options-panel ldr-expanded" id="advanced-options-panel">
                <select id="model_provider" data-initial-value="OLLAMA">
                    <option value="OLLAMA" selected>Benign Ollama provider</option>
                </select>
                <div class="form-group">
                    <div id="model-dropdown">
                        <input type="text" id="model">
                        <div id="model-dropdown-list"></div>
                    </div>
                </div>
                <input type="hidden" id="model_hidden" value="safe-model">
                <button type="button" id="model-refresh"></button>
                <div id="search-engine-dropdown">
                    <input type="text" id="search_engine">
                    <div id="search-engine-dropdown-list"></div>
                </div>
                <input type="hidden" id="search_engine_hidden" value="searxng">
                <button type="button" id="search_engine-refresh"></button>
                <select id="strategy"><option value="source-based">Source based</option></select>
                <input id="iterations" value="2">
                <input id="questions_per_iteration" value="3">
            </div>
            <button type="submit" id="start-research-btn"><span>Start Research</span></button>
        </form>
    `;
}

function installChatPage() {
    document.head.innerHTML = '<meta name="chat-session-id" content="source-session">';
    document.body.innerHTML = `
        <div id="chat-welcome"></div>
        <textarea id="chat-input"></textarea>
        <button id="send-btn" type="button">Send</button>
        <div id="chat-title"></div>
        <div id="chat-messages" role="log"></div>
        <template id="thinking-template">
            <div class="ldr-chat-message ldr-chat-message-assistant ldr-chat-message-thinking" role="status">
                <div class="ldr-chat-message-content">
                    <div class="ldr-chat-thinking-text" hidden></div>
                    <div class="ldr-chat-thinking-dots"><span></span></div>
                </div>
            </div>
        </template>
        <template id="message-template">
            <div class="ldr-chat-message">
                <div class="ldr-chat-message-avatar"><i class="fas fa-user"></i></div>
                <div class="ldr-chat-message-content">
                    <div class="ldr-chat-message-text"></div>
                    <div class="ldr-chat-message-meta"><span class="ldr-chat-message-time"></span></div>
                </div>
            </div>
        </template>
    `;
}

function installLibraryPage() {
    document.body.innerHTML = `
        <p id="benign-control">Benign library document</p>
        <div id="search-mode-menu">
            <button class="dropdown-item" data-mode="text" type="button">Text Only</button>
        </div>
        <button id="search-mode-btn" type="button">AI Hybrid</button>
        <div id="documents-container">
            <div class="ldr-document-card" data-doc-id="benign-doc" data-domain="" data-research="">
                <div class="card-header">Benign library document</div>
                <div class="card-body"></div>
            </div>
        </div>
        <div id="semantic-results-container"></div>
        <div id="library-search-notice"></div>
        <select id="filter-collection"></select>
        <select id="filter-domain"></select>
        <select id="filter-research"></select>
        <select id="filter-date"></select>
        <input id="search-documents">
    `;
}

function installLogPanel() {
    document.body.innerHTML = `
        <p id="benign-control">Benign log panel</p>
        <div class="ldr-collapsible-log-panel">
            <div class="ldr-log-panel-header" id="log-panel-toggle">
                <i class="fas fa-chevron-right ldr-toggle-icon"></i>
            </div>
            <div class="ldr-log-panel-content collapsed" id="log-panel-content">
                <div class="ldr-log-controls">
                    <div class="ldr-log-filter">
                        <button class="ldr-small-btn ldr-selected" data-filter-type="all" type="button">All</button>
                        <button class="ldr-small-btn" data-filter-type="info" type="button">Info</button>
                    </div>
                    <button id="log-autoscroll-button" class="ldr-selected" type="button"></button>
                    <button id="log-download-button" type="button"></button>
                </div>
                <div class="ldr-console-log" id="console-log-container"></div>
            </div>
        </div>
        <template id="console-log-entry-template">
            <div class="ldr-console-log-entry">
                <span class="ldr-log-timestamp"></span>
                <span class="ldr-log-badge"></span>
                <span class="ldr-log-message"></span>
            </div>
        </template>
    `;
}

afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
    sessionStorage.clear();
    localStorage.clear();
    document.body.replaceChildren();
    document.head.querySelector('meta[name="chat-session-id"]')?.remove();
    restoreNativeLocation();
    window.history.replaceState({}, '', '/');
    globalThis.fetch = originalFetch;
    window.api = originalApi;
    window.URLBuilder = originalUrlBuilder;
    globalThis.URLBuilder = originalUrlBuilder;
    window.URLValidator = originalUrlValidator;
    globalThis.URLValidator = originalUrlValidator;
    delete window.LibrarySearch;
    delete window.socket;
    delete window.ui;
    delete window.RESEARCH_STATUS;
    delete window.currentResearchId;
    delete globalThis.ResearchStates;
});

afterAll(() => {
    restoreNativeLocation();
    globalThis.fetch = originalFetch;
});

describe('rerunConfig sessionStorage -> form -> request', { concurrent: false }, () => {
    test('keeps poisoned history data inert through the real writer and reader flow', async () => {
        const storedQuery = SPECIAL_VALUES.join(' | ');
        const storedMode = 'quick';
        const safeAssign = vi.fn();
        const validator = { safeAssign };
        const states = {
            formatStatus: () => 'Completed',
            isCompleted: () => true,
            isTerminal: () => true,
        };

        installHistoryPage();
        window.URLValidator = validator;
        globalThis.URLValidator = validator;
        window.ResearchStates = states;
        globalThis.ResearchStates = states;
        window.api = {
            getCsrfToken: () => 'test-csrf',
            getResearchHistory: vi.fn().mockResolvedValue({
                items: [
                    {
                        id: 'hostile-history-item',
                        query: storedQuery,
                        title: storedQuery,
                        mode: storedMode,
                        status: 'completed',
                        document_count: 0,
                        created_at: '2026-08-20T10:00:00Z',
                    },
                    {
                        id: 'benign-history-item',
                        query: BENIGN_CONTROL,
                        title: BENIGN_CONTROL,
                        mode: 'quick',
                        status: 'completed',
                        document_count: 0,
                        created_at: '2026-08-20T09:00:00Z',
                    },
                ],
            }),
        };
        globalThis.fetch = vi.fn(() => jsonResponse({ success: true, sessions: [] }));

        await import('@js/components/history.js');
        await vi.waitFor(() => {
            expect(document.querySelectorAll('.ldr-history-item')).toHaveLength(2);
        });

        const hostileItem = document.querySelector('[data-id="hostile-history-item"]');
        expect(hostileItem).not.toBeNull();
        expect(hostileItem.textContent).toContain(storedQuery);
        expect(hostileItem.textContent).toContain('Quick Summary');
        expect(document.getElementById('history-items').textContent).toContain(BENIGN_CONTROL);
        expectInert(document.body);

        hostileItem.querySelector('.ldr-rerun-btn').click();

        expect(JSON.parse(sessionStorage.getItem('rerunConfig'))).toEqual({
            query: storedQuery,
            mode: storedMode,
        });
        expect(localStorage.getItem('rerunConfig')).toBeNull();
        expect(safeAssign).toHaveBeenCalledWith(window.location, 'href', '/');

        installResearchPage();
        window.api = { getCsrfToken: () => 'test-csrf' };
        window.RESEARCH_STATUS = { QUEUED: 'queued', IN_PROGRESS: 'in_progress' };
        const researchFetch = vi.fn((url, options) => {
            if (url === START_RESEARCH_URL && options?.method === 'POST') {
                return jsonResponse({ status: 'error', message: 'Benign API rejection' });
            }
            return jsonResponse(DEFAULT_RESEARCH_PAYLOAD);
        });
        globalThis.fetch = researchFetch;

        await import('@js/components/research.js');
        document.dispatchEvent(new Event('DOMContentLoaded'));

        await vi.waitFor(() => {
            expect(document.getElementById('query').value).toBe(storedQuery);
            expect(document.getElementById('research-alert').textContent)
                .toContain('Re-running previous research');
        });

        expect(sessionStorage.getItem('rerunConfig')).toBeNull();
        expect(document.getElementById('mode-quick').checked).toBe(true);
        expect(document.getElementById('research-alert').textContent).not.toContain(storedQuery);
        expect(document.body.textContent).toContain('Benign research form');
        expect(document.body.textContent).not.toContain(storedQuery);
        expectInert(document.body);

        document.getElementById('research-form').dispatchEvent(
            new Event('submit', { bubbles: true, cancelable: true })
        );

        await vi.waitFor(() => {
            expect(researchFetch.mock.calls.some(
                ([url, options]) => url === START_RESEARCH_URL && options?.method === 'POST'
            )).toBe(true);
        });
        const startCall = researchFetch.mock.calls.find(
            ([url, options]) => url === START_RESEARCH_URL && options?.method === 'POST'
        );
        const submitted = JSON.parse(startCall[1].body);
        expect(startCall[0]).toBe(START_RESEARCH_URL);
        expect(startCall[0]).not.toContain(storedQuery);
        expect(startCall[1].body).toBe(JSON.stringify(submitted));
        expect(submitted.query).toBe(storedQuery);
        expect(submitted.mode).toBe('quick');
        await vi.waitFor(() => {
            expect(document.getElementById('research-alert').textContent)
                .toContain('Benign API rejection');
        });
        expectInert(document.body);
    });

    // Resolved: research.js matches the stored mode against the rendered options by attribute comparison, so no selector is built from the stored value.
    test('handles a quote-bearing stored mode without aborting rerun application', async () => {
        installResearchPage();
        sessionStorage.setItem('rerunConfig', JSON.stringify({
            query: BENIGN_CONTROL,
            mode: SVG_PAYLOAD,
        }));
        window.api = { getCsrfToken: () => 'test-csrf' };
        window.RESEARCH_STATUS = { QUEUED: 'queued', IN_PROGRESS: 'in_progress' };
        globalThis.fetch = vi.fn(() => jsonResponse(DEFAULT_RESEARCH_PAYLOAD));

        const realQuerySelector = document.querySelector.bind(document);
        const rejectedModeSelectors = [];
        vi.spyOn(document, 'querySelector').mockImplementation((selector) => {
            try {
                return realQuerySelector(selector);
            } catch (error) {
                if (String(selector).startsWith('.ldr-mode-option[data-mode=')) {
                    rejectedModeSelectors.push({ selector, error });
                    return null;
                }
                throw error;
            }
        });

        await import('@js/components/research.js');
        document.dispatchEvent(new Event('DOMContentLoaded'));
        await vi.waitFor(() => {
            expect(document.getElementById('query').value).toBe(BENIGN_CONTROL);
            expect(document.getElementById('research-alert').textContent)
                .toContain('Re-running previous research');
        });

        expect(sessionStorage.getItem('rerunConfig')).toBeNull();
        expect(document.body.textContent).toContain('Benign research form');
        expectInert(document.body);
        expect(rejectedModeSelectors).toEqual([]);
    });
});

describe('components/chat.js location.search q flow', { concurrent: false }, () => {
    test.each(SPECIAL_VALUES)(
        'drafts q and explicitly submits it as inert data, never as URL structure: %s',
        async (payload) => {
            vi.resetModules();
            installChatPage();
            window.history.replaceState(
                {},
                '',
                `/chat/source-session?${new URLSearchParams({ q: payload, control: 'benign' })}`
            );

            const socketInstance = { on: vi.fn(), off: vi.fn() };
            window.api = { getCsrfToken: () => 'test-csrf' };
            window.ui = { renderMarkdown: (text) => text };
            window.socket = {
                getSocketInstance: () => socketInstance,
                subscribeToResearch: vi.fn(),
                unsubscribeFromResearch: vi.fn(),
            };
            const chatFetch = vi.fn((url, options = {}) => {
                if (url === '/api/chat/sessions/source-session/messages' && options.method === 'POST') {
                    return jsonResponse({ success: true });
                }
                if (url === '/api/chat/sessions/source-session/messages') {
                    return jsonResponse({
                        success: true,
                        messages: [],
                        has_more: false,
                        in_progress_research_id: null,
                    });
                }
                if (url === '/api/chat/sessions/source-session') {
                    return jsonResponse({
                        success: true,
                        session: { id: 'source-session', title: 'Benign chat session' },
                    });
                }
                throw new Error(`Unexpected chat request: ${url}`);
            });
            globalThis.fetch = chatFetch;

            await import('@js/components/chat.js');
            await vi.waitFor(() => {
                expect(document.getElementById('chat-input').dataset.initComplete).toBe('true');
            });

            const input = document.getElementById('chat-input');
            expect(input.value).toBe(payload);
            expect(document.activeElement).not.toBe(input);
            expect(document.getElementById('send-btn').disabled).toBe(false);
            expect(chatFetch.mock.calls.some(([, options]) => options?.method === 'POST')).toBe(false);
            expect(document.querySelector('.ldr-chat-message-user')).toBeNull();
            expect(window.location.search).toBe('');
            expectInert(document.body);

            document.getElementById('send-btn').click();
            await vi.waitFor(() => {
                expect(chatFetch.mock.calls.some(
                    ([url, options]) => url === '/api/chat/sessions/source-session/messages'
                        && options?.method === 'POST'
                )).toBe(true);
            });

            const userMessages = document.querySelectorAll('.ldr-chat-message-user .ldr-chat-message-text');
            const renderedPayload = userMessages[userMessages.length - 1];
            const sendCalls = chatFetch.mock.calls.filter(
                ([url, options]) => url === '/api/chat/sessions/source-session/messages'
                    && options?.method === 'POST'
            );
            expect(sendCalls).toHaveLength(1);
            const [sendCall] = sendCalls;
            expect(userMessages).toHaveLength(1);
            expect(renderedPayload.textContent).toBe(payload);
            expect(document.getElementById('chat-title').textContent).toBe('Benign chat session');
            expect(sendCall[0]).toBe('/api/chat/sessions/source-session/messages');
            expect(sendCall[0]).not.toContain(payload);
            expect(sendCall[1].headers['X-CSRFToken']).toBe('test-csrf');
            expect(sendCall[1].body).toBe(JSON.stringify({
                content: payload,
                trigger_research: true,
            }));
            expect(window.location.search).toBe('');
            expectInert(document.body);
        }
    );
});

describe('services/api.js location.search/hash return target', { concurrent: false }, () => {
    test.each(SPECIAL_VALUES)(
        'percent-encodes the full URL state under the fixed login target: %s',
        (payload) => {
            document.body.innerHTML = '<p id="benign-control">Benign current page</p>';
            const search = `?${new URLSearchParams({ payload, control: 'benign-control' })}`;
            const hash = `#${new URLSearchParams({ fragment: payload })}`;
            const location = installMockLocation({
                href: `http://localhost/results/benign${search}${hash}`,
                pathname: '/results/benign',
                search,
                hash,
            });
            const sourceValue = location.pathname + location.search + location.hash;

            originalApi.redirectToLogin();

            expect(location.href).toBe(`/auth/login?next=${encodeURIComponent(sourceValue)}`);
            expect(new URL(location.href, 'http://localhost').searchParams.get('next'))
                .toBe(sourceValue);
            expect(location.href).toContain('benign-control');
            expect(location.href).not.toMatch(/^\s*javascript:/i);
            expect(document.getElementById('benign-control').textContent).toBe('Benign current page');
            expect(document.body.textContent).not.toContain(payload);
            expectInert(document.body);
        }
    );
});

describe('components/library_search_ui.js URL serialization', { concurrent: false }, () => {
    test('preserves every existing special-character value with URLSearchParams across all four filters', async () => {
        installLibraryPage();
        const location = installMockLocation({ pathname: '/library' });
        const fetchMock = vi.fn();
        globalThis.fetch = fetchMock;
        window.LibrarySearch = null;

        await import('@js/components/library_search_ui.js');
        document.querySelector('[data-mode="text"]').click();

        const filterCases = [
            ['filter-collection', 'collection'],
            ['filter-domain', 'domain'],
            ['filter-research', 'research'],
            ['filter-date', 'date'],
        ];
        for (const payload of SPECIAL_VALUES) {
            for (const [elementId, parameter] of filterCases) {
                const selection = `benign-${parameter}`;
                const select = document.getElementById(elementId);
                const option = document.createElement('option');
                option.value = selection;
                option.textContent = `Benign ${parameter}`;
                select.replaceChildren(option);
                select.value = selection;

                const sourceParams = new URLSearchParams({
                    source: payload,
                    control: 'benign-control',
                    page: '9',
                });
                location.search = `?${sourceParams.toString()}`;
                select.dispatchEvent(new Event('change', { bubbles: true }));

                const expected = new URLSearchParams(sourceParams);
                expected.set(parameter, selection);
                expected.delete('page');
                expect(location.search).toBe(expected.toString());
                const serialized = new URLSearchParams(location.search);
                expect(serialized.get('source')).toBe(payload);
                expect(serialized.get('control')).toBe('benign-control');
                expect(serialized.get(parameter)).toBe(selection);
                expect(serialized.has('page')).toBe(false);
                expect(document.body.textContent).toContain('Benign library document');
                expectInert(document.body);
            }
        }
        expect(fetchMock).not.toHaveBeenCalled();
    });
});

let followUpResearchPromise;

function getFollowUpResearch() {
    if (!followUpResearchPromise) {
        followUpResearchPromise = import('@js/followup.js')
            .then(() => window.FollowUpResearch);
    }
    return followUpResearchPromise;
}

describe('followup.js location.search research_id flow', { concurrent: false }, () => {
    test.each(SPECIAL_VALUES)(
        'keeps research_id out of DOM and fetch URLs while preserving JSON data: %s',
        async (payload) => {
            const search = `?${new URLSearchParams({
                research_id: payload,
                control: 'benign-control',
            })}`;
            installMockLocation({
                href: `http://localhost/followup${search}`,
                pathname: '/followup',
                search,
            });
            document.body.innerHTML = `
                <p id="benign-control">Benign follow-up page</p>
                <button id="ask-followup-btn" type="button">Ask</button>
                <div id="followUpModal"></div>
                <section id="parentContext" style="display:none">
                    <div id="parentSummary"></div>
                    <div id="parentSources"></div>
                </section>
                <textarea id="followUpQuestion">Benign follow-up question</textarea>
                <div id="followup-error-container"></div>
            `;
            window.api = { getCsrfToken: () => 'test-csrf' };
            window.ui = {
                clearInlineError: vi.fn(),
                showInlineError: vi.fn(),
            };
            const followupFetch = vi.fn((url) => {
                if (url === '/api/followup/prepare') {
                    return jsonResponse({
                        success: true,
                        parent_summary: 'Benign parent summary',
                        available_sources: '7 benign sources',
                    });
                }
                if (url === '/api/followup/start') {
                    return jsonResponse({ success: false, error: 'Benign rejected start' });
                }
                throw new Error(`Unexpected follow-up request: ${url}`);
            });
            globalThis.fetch = followupFetch;
            const FollowUpClass = await getFollowUpResearch();
            const followUp = new FollowUpClass();

            followUp.init();
            expect(followUp.parentResearchId).toBe(payload);
            expect(document.getElementById('ask-followup-btn').onclick).toBeTypeOf('function');
            await followUp.loadParentContext();
            await followUp.submitFollowUp();

            const prepareCall = followupFetch.mock.calls.find(
                ([url]) => url === '/api/followup/prepare'
            );
            const startCall = followupFetch.mock.calls.find(
                ([url]) => url === '/api/followup/start'
            );
            expect(prepareCall).toBeDefined();
            expect(startCall).toBeDefined();
            expect(prepareCall[0]).toBe('/api/followup/prepare');
            expect(startCall[0]).toBe('/api/followup/start');
            expect(prepareCall[0]).not.toContain(payload);
            expect(startCall[0]).not.toContain(payload);
            expect(prepareCall[1].body).toBe(JSON.stringify({
                parent_research_id: payload,
                question: 'test',
            }));
            expect(startCall[1].body).toBe(JSON.stringify({
                parent_research_id: payload,
                question: 'Benign follow-up question',
            }));
            expect(document.getElementById('parentSummary').textContent)
                .toBe('Benign parent summary');
            expect(document.getElementById('parentSources').textContent)
                .toBe('7 benign sources');
            expect(document.getElementById('parentContext').style.display).toBe('block');
            expect(document.body.textContent).toContain('Benign follow-up page');
            expect(document.body.textContent).not.toContain(payload);
            expectInert(document.body);
        }
    );
});

describe('components/logpanel.js location.search/hash control flow', { concurrent: false }, () => {
    test('uses benign and hostile URL state only as toggle conditions', async () => {
        vi.useFakeTimers();
        const location = installMockLocation({ pathname: '/not-a-research-page' });
        await import('@js/utils/log-helpers.js');
        window.URLBuilder = {
            researchLogs: (id, limit) => `/api/research/${id}/logs${limit ? `?limit=${limit}` : ''}`,
            historyLogCount: (id) => `/api/research/${id}/log_count`,
            researchLogsExport: (id) => `/api/research/${id}/logs/export`,
        };
        globalThis.URLBuilder = window.URLBuilder;
        globalThis.fetch = vi.fn((url) => {
            if (String(url).includes('log_count')) {
                return jsonResponse({ total_logs: 0 });
            }
            return jsonResponse([]);
        });
        await import('@js/components/logpanel.js');

        let sequence = 0;
        const exerciseToggle = async ({ search = '', hash = '', payload = null }) => {
            installLogPanel();
            location.pathname = '/results/url-source-test';
            location.search = search;
            location.hash = hash;
            const state = window._logPanelState;
            state.initialized = false;
            state.connectedResearchId = null;
            state.expanded = false;
            state.renderedIds = new Set();
            state.counts = window.LdrLogHelpers.emptyCounts();
            const toggle = document.getElementById('log-panel-toggle');
            const clickSpy = vi.spyOn(toggle, 'click');

            window.logPanel.initialize(`url-source-${sequence++}`);
            await vi.advanceTimersByTimeAsync(900);

            expect(clickSpy).toHaveBeenCalled();
            expect(document.body.textContent).toContain('Benign log panel');
            if (payload !== null) expect(document.body.textContent).not.toContain(payload);
            expectInert(document.body);
        };

        await exerciseToggle({ hash: '#logs' });
        for (const payload of SPECIAL_VALUES) {
            await exerciseToggle({
                hash: `#debug-${encodeURIComponent(payload)}`,
                payload,
            });
            await exerciseToggle({
                search: `?${new URLSearchParams({
                    debug: 'logs',
                    payload,
                    control: 'benign-control',
                })}`,
                payload,
            });
        }
    });
});