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.

19.6 KB
Raw Blame
/**
 * Complete shipped consumers and authentication transports in an isolated VM.
 * The DOM, server, widgets and clock are controlled. Fixed lexical probes expose
 * private callbacks without copying or rewriting any production function body.
 * No real timers, application server, database or provider is started here.
 */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import vm from 'node:vm';

const JS_ROOT = resolve(__dirname, '../../../src/local_deep_research/web/static/js');
export const AUTH_CONTEXT = 'c'.repeat(64);
export const RESEARCH_ID = 'research-area3';
export const COLLECTION_ID = 'collection-area3';
export const CHAT_ID = 'chat-area3';
export const CSRF_TOKEN = 'area3-csrf';

export function response(body = {}, status = 200, headers = {}) {
    return Promise.resolve({
        ok: status >= 200 && status < 300,
        status, statusText: String(status), redirected: false,
        headers: new globalThis.Headers(headers),
        json: vi.fn().mockResolvedValue(body),
        blob: vi.fn().mockResolvedValue(new Blob(['synthetic export'])),
    });
}

export async function settle() {
    for (let turn = 0; turn < 80; turn++) await Promise.resolve();
}

const FIXTURE = `
    <div id="history-items"></div><input id="history-search">
    <button id="clear-history-btn"></button><div id="history-empty-message"></div>
    <div id="semantic-panel-header"></div><i id="semantic-panel-toggle"></i>
    <div id="semantic-panel-content"></div><span id="indexed-count"></span><span id="total-count"></span>
    <button id="index-all-btn">Index All</button><div id="indexing-progress">
        <div id="indexing-progress-bar"></div><div id="indexing-progress-text"></div>
        <div id="progress-fill"></div><div id="progress-text"></div><div id="progress-log"></div>
        <div id="indexing-progress-title"></div><div id="indexing-spinner"></div>
    </div>
    <div id="loading"></div><div id="error"></div><main id="details-content"></main>
    <div id="total-tokens"></div><div id="prompt-tokens"></div><div id="completion-tokens"></div>
    <div id="llm-calls"></div><div id="avg-response-time"></div><div id="model-used"></div>
    <div id="research-query"></div><div id="research-mode"></div><div id="research-date"></div>
    <div id="research-strategy"></div><div id="total-cost"></div><div id="phase-breakdown"></div>
    <div id="search-engine-breakdown"></div><div id="search-engine-performance"></div><div id="search-timeline"></div>
    <div id="token-metrics-section"></div><div id="search-metrics-section"></div><div id="token-usage-top-chart"></div>
    <canvas id="timeline-chart"></canvas><canvas id="search-chart"></canvas>
    <div id="source-distribution-section"><div class="ldr-card-content"><canvas id="source-type-chart"></canvas></div></div>
    <div id="link-analytics-section"></div><div id="total-links"></div><div id="unique-domains"></div>
    <label>Academic</label><span id="academic-sources"></span><label>News</label><span id="news-sources"></span>
    <div id="domain-list"></div><div id="resource-sample"></div>
    <section id="context-overflow-section"><div id="co-total-tokens"></div><div id="co-context-limit"></div>
        <div id="co-max-tokens"></div><div id="co-truncation-status"></div><div id="co-phase-breakdown"></div>
        <table><tbody id="co-requests-table"></tbody></table><div id="co-performance-warning"></div>
    </section>
    <div class="ldr-collapsible-log-panel"><div id="log-panel-toggle"><i class="ldr-toggle-icon"></i>
        <span class="ldr-log-indicator"></span></div><div id="log-panel-content" class="collapsed">
        <div id="console-log-container"></div><button id="log-download-button"></button>
    </div></div>
    <button id="save-to-collection-btn"></button><div id="saveToCollectionModal"></div>
    <div id="collection-list-loading"></div><div id="collection-list"><div id="collection-items"></div></div>
    <div id="collection-error"></div><div id="collection-success"></div>
    <button id="index-collection-btn"></button><button id="reindex-collection-btn"></button>
    <button id="delete-collection-btn"></button><button id="cancel-indexing-btn"></button>
    <input id="collection-is-public" type="checkbox"><input id="collection-agent-enabled" type="checkbox">
    <h1 id="collection-name"></h1><p id="collection-description"></p><div id="collection-rebuild-notice"></div>
    <span id="stat-total-docs"></span><span id="stat-indexed-docs"></span>
    <span id="stat-unindexed-docs"></span><span id="stat-total-chunks"></span>
    <div id="collection-embedding-info"></div><div id="documents-list"></div><div id="no-documents-message"></div>
    <section id="notes-section"><div id="notes-list"></div></section><div id="collection-search-section"></div>
    <div><textarea id="query">Existing research question</textarea>
        <div class="ldr-search-hints"><div class="ldr-hint-row"></div></div></div>
    <div id="deleteConfirmModal"><h2 id="deleteModalTitle"></h2><p id="deleteModalMessage"></p>
        <div id="deleteModalDetails"><ul id="deleteDetailsList"></ul></div>
        <div id="deleteModalWarning"><span id="deleteWarningText"></span></div>
        <button id="deleteConfirmBtn"><span id="deleteConfirmBtnText"></span></button>
    </div>`;

const CONSUMERS = {
    history: ['components/history.js', ['apiUtils', 'fetchChatSessions', 'handleDeleteItem', 'handleClearHistory']],
    details: ['components/details.js', ['loadLinkAnalytics', 'loadResearchMetrics', 'loadContextOverflowData']],
    'history-search': ['components/history_search.js', ['loadIndexingStatus', 'triggerIndexing', 'startPolling', 'checkAndResumeIndexing']],
    logs: ['components/logpanel.js', ['downloadLogs']],
    save: ['components/save_to_collection.js', ['loadCollections', 'saveToCollection']],
    collection: ['collection_details.js', []],
    pdf: ['pdf_upload_handler.js', []],
    confirmation: ['deletion/confirmation_modal.js', []],
};

export function createRuntime(component, override = null) {
    if (!Object.hasOwn(CONSUMERS, component)) throw new Error('Unknown consumer fixture');
    const page = component === 'details' ? `/details/${RESEARCH_ID}`
        : component === 'history' || component === 'history-search' ? '/history/'
        : component === 'collection' ? `/library/collections/${COLLECTION_ID}`
        : component === 'pdf' ? '/' : `/results/${RESEARCH_ID}`;
    const original = new URL(`${page}?tab=retained#logs`, 'https://app.test');
    let current = original;
    const navigations = [];
    const location = {
        get href() { return current.href; },
        set href(value) { navigations.push(value); current = new URL(value, current); },
        get origin() { return current.origin; },
        get pathname() { return current.pathname; },
        get search() { return current.search; },
        get hash() { return current.hash; },
    };
    // eslint-disable-next-line no-unsanitized/property -- Repository-owned fixture with a fixed non-secret authentication fingerprint.
    document.head.innerHTML = `<meta name="csrf-token" content="${CSRF_TOKEN}"><meta name="auth-context" content="${AUTH_CONTEXT}">`;
    // eslint-disable-next-line no-unsanitized/property -- Entire markup is the repository-owned constant above.
    document.body.innerHTML = FIXTURE;
    const documentListeners = [];
    const windowListeners = [];
    const bodyListeners = [];
    const addBodyListener = document.body.addEventListener.bind(document.body);
    const bodyListenerSpy = vi.spyOn(document.body, 'addEventListener').mockImplementation((type, listener, options) => {
        bodyListeners.push([type, listener, options]);
        addBodyListener(type, listener, options);
    });
    let readyState = 'loading';
    const fixtureDocument = new Proxy(document, {
        get(target, key) {
            if (key === 'readyState') return readyState;
            if (key === 'addEventListener') return (type, listener, options) => {
                documentListeners.push([type, listener, options]);
                if (type !== 'DOMContentLoaded') target.addEventListener(type, listener, options);
            };
            const value = Reflect.get(target, key, target);
            return typeof value === 'function' ? value.bind(target) : value;
        },
    });
    let nextTimer = 1;
    const timeouts = new Map();
    const intervals = new Map();
    const anchorClicks = [];
    const anchorClick = vi.spyOn(window.HTMLAnchorElement.prototype, 'click').mockImplementation(function() {
        anchorClicks.push({ href: this.getAttribute('href'), download: this.download, attached: document.body.contains(this) });
    });
    class FixtureURL extends URL {}
    FixtureURL.createObjectURL = vi.fn(() => 'blob:unexpected-area3-buffer');
    FixtureURL.revokeObjectURL = vi.fn();
    const modalInstance = {
        show: vi.fn(),
        hide: vi.fn(() => document.getElementById('deleteConfirmModal').dispatchEvent(new Event('hidden.bs.modal'))),
    };
    function Modal() { return modalInstance; }
    Modal.getInstance = () => modalInstance;
    Modal.getOrCreateInstance = () => modalInstance;
    const action = vi.fn(async () => 'action completed');
    const sandbox = {
        document: fixtureDocument, location, URL: FixtureURL, URLSearchParams,
        Headers: globalThis.Headers, Blob, File: window.File, FormData: window.FormData,
        AbortController, DOMException: globalThis.DOMException, Element: window.Element, Event: window.Event,
        localStorage: window.localStorage, sessionStorage: window.sessionStorage,
        setTimeout: (callback, delay) => { const id = nextTimer++; timeouts.set(id, { callback, delay }); return id; },
        clearTimeout: id => timeouts.delete(id),
        setInterval: (callback, delay) => { const id = nextTimer++; intervals.set(id, { callback, delay }); return id; },
        clearInterval: id => intervals.delete(id),
        requestAnimationFrame: vi.fn(),
        addEventListener: (type, listener) => windowListeners.push([type, listener]),
        getComputedStyle: window.getComputedStyle.bind(window),
        SafeLogger: { log: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), info: vi.fn() },
        formatting: {
            formatNumber: value => Number(value).toLocaleString('en-US'),
            formatCurrency: value => String(value), generateChartColors: () => ({ background: [], border: [] }),
        },
        ui: { showMessage: vi.fn(), showAlert: vi.fn() },
        bootstrap: { Modal }, alert: vi.fn(), confirm: vi.fn(() => true),
        COLLECTION_ID,
        RESEARCH_STATUS: { IN_PROGRESS: 'in_progress', COMPLETED: 'completed', FAILED: 'failed', ERROR: 'error', CANCELLED: 'cancelled', SUSPENDED: 'suspended', QUEUED: 'queued', PENDING: 'pending' },
        RESEARCH_TERMINAL_STATES: new Set(['completed', 'failed', 'error', 'cancelled', 'suspended']),
        formatBytes: value => `${value} bytes`,
    };
    sandbox.window = sandbox;
    sandbox.globalThis = sandbox;
    const baseRoute = (input, options = {}) => {
        const url = new URL(input, original);
        const path = url.pathname;
        if (path === '/history/api') return response({ items: [{ id: RESEARCH_ID, title: 'Synthetic research', query: 'Synthetic query', mode: 'quick', status: 'completed', created_at: '2026-10-01T12:00:00Z' }] });
        if (path === '/api/chat/sessions' && !options.method) return response({ success: true, sessions: [{ id: CHAT_ID, title: 'Synthetic chat', status: 'active', created_at: '2026-10-01T12:00:00Z' }] });
        if (path.startsWith('/api/chat/sessions/') || path.startsWith('/api/delete/') || path === '/api/clear_history') return response({ status: 'success', success: true });
        if (path.startsWith('/history/details/')) return response({ query: 'Synthetic query', mode: 'quick', strategy: 'source-based' });
        if (path.endsWith('/log_count/' + RESEARCH_ID)) return response({ status: 'success', total_logs: 0 });
        if (path.endsWith('/logs/export')) return response();
        if (path.endsWith('/logs')) return response([]);
        if (path.startsWith('/metrics/api/metrics/research/')) return path.endsWith('/links')
            ? response({ status: 'success', data: { total_links: 0, unique_domains: 0, domain_categories: {}, domains: [], resources: [] } })
            : response({ status: 'success', metrics: {} });
        if (path.endsWith('/context-overflow')) return response({ status: 'success', data: { overview: { total_tokens: 0, context_limit: 4096, max_tokens_used: 0, truncation_occurred: false }, phase_stats: {}, requests: [] } });
        if (path === '/library/api/research-history/collection') return response({ success: true, collection_id: COLLECTION_ID, indexed_documents: 0, total_documents: 2 });
        if (path.endsWith('/convert-all')) return response({ success: true, converted: 2 });
        if (path.endsWith('/index/status')) return response({ status: 'idle' });
        if (path.endsWith('/index/start')) return response({ success: true, task_id: 'synthetic-index-task' });
        if (path.endsWith('/search')) return response({ success: true, results: [] });
        if (path === '/library/api/collections') return response({ success: true, collections: [{ id: COLLECTION_ID, name: 'Synthetic collection', description: 'Synthetic fixture' }] });
        if (path.endsWith('/add-to-collection')) return response({ success: true, documents_added: 2 });
        if (path.endsWith('/documents')) return response({ success: true, collection: { name: 'Synthetic collection', is_public: false, agent_enabled: true }, documents: [], notes: [] });
        if (path.startsWith('/library/api/collection/') || path.startsWith('/library/api/document/')) return response({ success: true, document_deleted: true });
        if (path === '/api/config/limits') return response({ max_file_size: 1048576, max_files: 3 });
        if (path === '/api/upload/pdf') return response({ status: 'success', processed_files: 1, extracted_texts: [{ filename: 'synthetic.pdf', text: 'Synthetic extracted text', size: 4, pages: 1 }], combined_text: 'Synthetic extracted text', errors: [] });
        if (path === '/settings/api/research_library.confirm_deletions') return response({ value: true });
        throw new Error(`Unexpected fixture request: ${input}`);
    };
    const fetchMock = vi.fn((input, options) => {
        const controlled = override?.(input, options || {}, sandbox);
        return controlled === undefined ? baseRoute(input, options) : controlled;
    });
    sandbox.fetch = fetchMock;
    const context = vm.createContext(sandbox);
    const run = (path, probes = []) => {
        let source = readFileSync(resolve(JS_ROOT, path), 'utf8');
        if (probes.length) {
            const close = source.lastIndexOf('\n})();');
            if (close < 0 || source.slice(close).trim() !== '})();') throw new Error('Expected complete consumer IIFE');
            source = `${source.slice(0, close)}\nwindow.__historyLibraryProbe = { ${probes.join(', ')} };${source.slice(close)}`;
        }
        vm.runInContext(source, context, { filename: path, timeout: 1000 });
    };
    run('config/urls.js');
    run('config/constants.js');
    run('services/api.js');
    run('security/url-validator.js');
    run('security/safe-fetch.js');
    run('utils/log-helpers.js');
    run('components/context-overflow-shared.js');
    run(...CONSUMERS[component]);
    const probe = sandbox.__historyLibraryProbe;
    return {
        sandbox, probe, action, fetchMock, original, navigations, anchorClicks, modalInstance, timeouts, intervals,
        async boot() {
            readyState = 'interactive';
            for (const [type, listener] of [...documentListeners]) if (type === 'DOMContentLoaded') listener(new Event(type));
            await settle();
        },
        guardState() {
            return vm.runInContext('({ recovery: typeof window.api.fetchWithCsrfRecovery, classify: typeof window.api.shouldRedirectToLoginOn401, redirect: typeof window.api.redirectToLogin, auth: typeof window.safeFetchWithAuth })', context, { timeout: 1000 });
        },
        dropHighLevel(method) {
            const operations = {
                getResearchHistory: 'delete window.api.getResearchHistory; typeof window.api.getResearchHistory;',
                deleteResearch: 'delete window.api.deleteResearch; typeof window.api.deleteResearch;',
                clearResearchHistory: 'delete window.api.clearResearchHistory; typeof window.api.clearResearchHistory;',
            };
            if (!Object.hasOwn(operations, method)) throw new Error('Unknown high-level API fixture');
            return vm.runInContext(operations[method], context, { timeout: 1000 });
        },
        pollOnce() {
            const callbacks = [...intervals.values()];
            if (callbacks.length !== 1 || callbacks[0].delay !== 2000) throw new Error('Expected one actual two-second indexing poll');
            return callbacks[0].callback();
        },
        trigger(kind) {
            switch (kind) {
                case 'history-list-fallback': return probe.apiUtils.getResearchHistory();
                case 'history-delete-fallback': return probe.apiUtils.deleteResearch(RESEARCH_ID);
                case 'history-clear-fallback': return probe.handleClearHistory();
                case 'chat-list': return probe.fetchChatSessions({ all: true, throwOnError: true });
                case 'chat-delete': return probe.handleDeleteItem(CHAT_ID, true);
                case 'chat-clear': return probe.handleClearHistory();
                case 'links': return probe.loadLinkAnalytics();
                case 'metrics': case 'timeline': case 'search-metrics': return probe.loadResearchMetrics();
                case 'overflow': return probe.loadContextOverflowData();
                case 'index-count': return probe.loadIndexingStatus();
                case 'convert': case 'index-start': return probe.triggerIndexing();
                case 'index-poll': probe.startPolling(); return this.pollOnce();
                case 'index-resume': return probe.checkAndResumeIndexing();
                case 'history-semantic': return sandbox.HistorySearch.semanticSearchHistory('Synthetic query');
                case 'logs': return sandbox.logPanel.loadLogs(RESEARCH_ID);
                case 'logs-export': return probe.downloadLogs();
                case 'collections': return probe.loadCollections();
                case 'save': return probe.saveToCollection(COLLECTION_ID, 'Synthetic collection');
                case 'collection-index': return sandbox.indexCollection(false);
                case 'document-remove': return sandbox.removeDocumentFromCollection('synthetic-document');
                case 'document-delete': return sandbox.deleteDocumentCompletely('synthetic-document');
                case 'pdf-limits': return sandbox.pdfUploadHandler.fetchUploadLimits();
                case 'pdf-upload': return sandbox.pdfUploadHandler.uploadAndExtractPDFs([new window.File(['%PDF'], 'synthetic.pdf', { type: 'application/pdf' })]);
                case 'confirmation': return sandbox.DeleteConfirmation.confirmAndRun({ action: 'deleteDocument' }, action);
                default: throw new Error('Unknown consumer callback fixture');
            }
        },
        dispose() {
            for (const [type, listener, options] of documentListeners) if (type !== 'DOMContentLoaded') document.removeEventListener(type, listener, options);
            for (const [type, listener, options] of bodyListeners) document.body.removeEventListener(type, listener, options);
            for (const [type, listener] of windowListeners) if (type === 'beforeunload') listener(new Event(type));
            timeouts.clear(); intervals.clear(); anchorClick.mockRestore(); bodyListenerSpy.mockRestore();
            document.head.replaceChildren(); document.body.replaceChildren();
        },
    };
}