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.

13.2 KB
Raw Blame
#!/usr/bin/env node
/** A real-browser release smoke with deterministic model/search boundaries.
 *
 * The test server pauses the real worker at its first model invocation for
 * each research id. This lets the browser subscribe, reconnect, or switch to
 * HTTP polling before the worker can finish. No research result or transport
 * event is inserted by this test.
 */

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');

const BASE_URL = process.env.LDR_BASE_URL || 'http://127.0.0.1:5000';
const SMOKE_DIR = process.env.LDR_RESEARCH_SMOKE_DIR;
const ANSWER = 'RELEASEBROWSERSYNTHESISMARKER';
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const TIMEOUT = 90000;

async function waitForFile(file) {
    const deadline = Date.now() + TIMEOUT;
    while (Date.now() < deadline) {
        if (fs.existsSync(file)) return;
        await new Promise(resolve => setTimeout(resolve, 100));
    }
    throw new Error(`Worker did not enter the gated model for ${path.basename(file)}`);
}

async function submitFromForm(page, query) {
    await page.goto(`${BASE_URL}/`, { waitUntil: 'domcontentloaded' });
    await page.waitForSelector('#start-research-btn');
    await page.waitForFunction(() => document.querySelectorAll('#model_provider option').length > 1);
    await page.type('#query', query);

    // The model and engine catalogs refresh asynchronously. Keep the test
    // choices in their actual form controls through the click, then verify
    // the production client sent those choices in the captured POST body.
    await page.evaluate(() => {
        const apply = () => {
            const model = document.querySelector('#model_hidden');
            const visibleModel = document.querySelector('#model');
            const engine = document.querySelector('#search_engine_hidden');
            if (model && model.value !== 'llama3.2:3b') {
                model.value = 'llama3.2:3b';
                if (visibleModel) visibleModel.value = 'llama3.2:3b';
                model.dispatchEvent(new Event('change', { bubbles: true }));
            }
            if (engine) engine.value = 'wikipedia';
            const strategy = document.querySelector('#strategy');
            if (strategy) strategy.value = 'source-based';
            const iterations = document.querySelector('#iterations');
            if (iterations) iterations.value = '1';
            const questions = document.querySelector('#questions_per_iteration');
            if (questions) questions.value = '1';
        };
        apply();
        window.__releaseFormGuard = setInterval(apply, 100);
    });
    await page.waitForFunction(() =>
        document.querySelector('#model_hidden')?.value === 'llama3.2:3b' &&
        document.querySelector('#search_engine_hidden')?.value === 'wikipedia' &&
        document.querySelector('#model_provider')?.value.toLowerCase() === 'ollama'
    );
    let stableModelChecks = 0;
    for (let attempt = 0; attempt < 60 && stableModelChecks < 5; attempt++) {
        const value = await page.$eval('#model_hidden', input => input.value);
        stableModelChecks = value === 'llama3.2:3b' ? stableModelChecks + 1 : 0;
        await new Promise(resolve => setTimeout(resolve, 150));
    }
    assert.equal(stableModelChecks, 5, 'the model field must settle before the real click');

    const responsePromise = page.waitForResponse(
        response => response.url().endsWith('/api/start_research'),
        { timeout: 20000 }
    );
    await page.click('#start-research-btn');
    let response;
    try {
        response = await responsePromise;
    } catch (error) {
        const state = await page.evaluate(() => ({
            url: location.href,
            query: document.querySelector('#query')?.value,
            model: document.querySelector('#model_hidden')?.value,
            provider: document.querySelector('#model_provider')?.value,
            engine: document.querySelector('#search_engine_hidden')?.value,
            mode: document.querySelector('input[name="research_mode"]:checked')?.value,
            buttonDisabled: document.querySelector('#start-research-btn')?.disabled,
            researchError: document.querySelector('#research-error-alert')?.textContent?.trim(),
            inlineErrors: [...document.querySelectorAll('.ldr-field-error, .error-message')]
                .map(node => node.textContent.trim()).filter(Boolean).slice(0, 5),
        }));
        throw new Error(`The real form sent no research POST: ${JSON.stringify(state)}`, { cause: error });
    }
    assert.equal(response.status(), 200, 'real research submit must be accepted');
    assert.ok(response.request().headers()['x-csrftoken'], 'the form must carry its CSRF token');
    const sent = JSON.parse(response.request().postData());
    assert.equal(sent.query, query);
    assert.equal(sent.model_provider.toLowerCase(), 'ollama');
    assert.equal(sent.model, 'llama3.2:3b');
    assert.equal(sent.search_engine, 'wikipedia');
    assert.equal(sent.strategy, 'source-based');
    assert.equal(sent.iterations, 1);
    assert.equal(sent.questions_per_iteration, 1);

    await page.waitForFunction(() => window.location.pathname.startsWith('/progress/'));
    const researchId = new URL(page.url()).pathname.split('/').pop();
    assert.match(researchId, UUID_RE, 'the app must redirect to the new research id');
    await waitForFile(path.join(SMOKE_DIR, 'entered', researchId));
    await page.waitForFunction(() => window.socket && window.socket.isConnected());
    return researchId;
}

async function waitForCompletionUI(page) {
    await page.waitForFunction(() => {
        const button = document.querySelector('#view-results-btn');
        const bar = document.querySelector('.ldr-progress-bar[role="progressbar"]');
        return button && getComputedStyle(button).display !== 'none'
            && bar && bar.getAttribute('aria-valuenow') === '100';
    }, { timeout: TIMEOUT });
}

async function assertCompleted(page, researchId) {
    await waitForCompletionUI(page);
    const status = await page.evaluate(async id => {
        const response = await fetch(`/api/research/${id}/status`, { credentials: 'same-origin' });
        return { code: response.status, data: await response.json() };
    }, researchId);
    assert.equal(status.code, 200);
    assert.equal(status.data.status, 'completed', JSON.stringify(status.data));
    assert.equal(status.data.progress, 100);
    const resultUrl = await page.$eval('#view-results-btn', button => button.href);
    assert.ok(resultUrl.endsWith(`/results/${researchId}`), resultUrl);
    const response = await page.goto(resultUrl, { waitUntil: 'domcontentloaded' });
    assert.equal(response.status(), 200);
    await page.waitForFunction(marker => document.body.textContent.includes(marker), {}, ANSWER);
}

async function run() {
    assert.ok(SMOKE_DIR, 'LDR_RESEARCH_SMOKE_DIR is required');
    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    page.on('pageerror', error => console.error('PAGE ERROR:', error.message));
    page.setDefaultTimeout(TIMEOUT);
    page.setDefaultNavigationTimeout(TIMEOUT);

    try {
        const auth = new AuthHelper(page, BASE_URL);
        await auth.ensureAuthenticatedWithTimeout();
        const authed = auth.getPage();
        authed.setDefaultTimeout(TIMEOUT);

        const socketId = await submitFromForm(authed, `Release socket ${Date.now()}`);
        await authed.evaluate(id => {
            window.__releaseSocketFrames = [];
            window.__releaseSubscriptionAck = false;
            window.__releaseSubscriptionError = null;
            const instance = window.socket.getSocketInstance();
            instance.on('subscribe_error', data => { window.__releaseSubscriptionError = data; });
            const emit = instance.emit.bind(instance);
            instance.emit = (event, ...args) => {
                if (event === 'subscribe_to_research' && args[0]?.research_id === id) {
                    // Add an ACK to the production client's own resubscribe
                    // emit. Do not submit an extra event from the test: the
                    // ACK proves the server finished installing the room.
                    return emit(event, ...args, () => { window.__releaseSubscriptionAck = true; });
                }
                return emit(event, ...args);
            };
            instance.disconnect();
        }, socketId);
        await authed.waitForFunction(() => window.socket.isUsingPolling());
        await authed.evaluate(() => window.socket.getSocketInstance().connect());
        await authed.waitForFunction(() => window.socket.isConnected() && !window.socket.isUsingPolling());
        await authed.waitForFunction(() => window.__releaseSubscriptionAck || window.__releaseSubscriptionError);
        const subscriptionError = await authed.evaluate(() => window.__releaseSubscriptionError);
        assert.equal(subscriptionError, null, JSON.stringify(subscriptionError));
        // Production resubscribe calls socket.off(event) before installing
        // its handler, so attach this passive observer only after that path
        // has finished and its server-side ACK has arrived.
        await authed.evaluate(id => {
            window.socket.getSocketInstance().on(
                `research_progress_${id}`,
                data => window.__releaseSocketFrames.push(data)
            );
        }, socketId);
        fs.writeFileSync(path.join(SMOKE_DIR, 'release', socketId), 'release\n');
        try {
            await authed.waitForFunction(() => window.__releaseSocketFrames.some(frame => frame.phase === 'complete'),
                { timeout: 15000 });
        } catch (error) {
            const observed = await authed.evaluate(async id => {
                const response = await fetch(`/api/research/${id}/status`);
                const status = await response.json();
                return {
                    frames: window.__releaseSocketFrames.map(frame => ({
                        phase: frame.phase, status: frame.status, progress: frame.progress,
                    })),
                    connected: window.socket.isConnected(),
                    polling: window.socket.isUsingPolling(),
                    subscriptionAck: window.__releaseSubscriptionAck,
                    subscriptionError: window.__releaseSubscriptionError,
                    status: status.status,
                    progress: status.progress,
                    buttonDisplay: getComputedStyle(document.querySelector('#view-results-btn')).display,
                };
            }, socketId);
            throw new Error(`No terminal Socket.IO frame after reconnect: ${JSON.stringify(observed)}`, { cause: error });
        }
        const frames = await authed.evaluate(() => window.__releaseSocketFrames);
        assert.ok(frames.some(frame => frame.phase === 'complete' && frame.progress === 100),
            `no terminal Socket.IO frame after reconnect: ${JSON.stringify(frames)}`);
        assert.ok(frames.some(frame => frame.progress != null && frame.progress < 100),
            `no nonterminal Socket.IO progress frame after reconnect: ${JSON.stringify(frames)}`);
        assert.ok(frames.every(frame => frame.progress == null ||
            (typeof frame.progress === 'number' && Number.isFinite(frame.progress) &&
                frame.progress >= 0 && frame.progress <= 100)), JSON.stringify(frames));
        await assertCompleted(authed, socketId);
        console.log('PASS: real form + CSRF, reconnect, Socket.IO completion, report');

        const pollingId = await submitFromForm(authed, `Release polling ${Date.now()}`);
        await authed.evaluate(() => window.socket.getSocketInstance().disconnect());
        await authed.waitForFunction(() => window.socket.isUsingPolling() && !window.socket.isConnected());
        // Keep the model gate closed until a real fallback GET has completed.
        // This proves polling started; the later terminal UI must come from a
        // subsequent GET because the Socket.IO transport stays disconnected.
        const firstPoll = await authed.waitForResponse(
            response => response.url().endsWith(`/api/research/${pollingId}/status`)
                && response.request().method() === 'GET',
            { timeout: 10000 }
        );
        assert.equal((await firstPoll.json()).status, 'in_progress');
        fs.writeFileSync(path.join(SMOKE_DIR, 'release', pollingId), 'release\n');
        await waitForCompletionUI(authed);
        await assertCompleted(authed, pollingId);
        console.log('PASS: disconnected Socket.IO falls back to HTTP polling and completes');
    } finally {
        // A browser assertion failure must not leave the worker waiting on a
        // gate while the test server shuts down.
        const enteredDir = path.join(SMOKE_DIR, 'entered');
        if (fs.existsSync(enteredDir)) {
            for (const id of fs.readdirSync(enteredDir)) {
                if (UUID_RE.test(id)) fs.writeFileSync(path.join(SMOKE_DIR, 'release', id), 'release\n');
            }
        }
        await browser.close();
    }
}

run().catch(error => {
    console.error(error);
    process.exitCode = 1;
});