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.

27.7 KB
Raw Blame
#!/usr/bin/env node
/**
 * Research Submission Flow — End-to-End Browser Test (Flask -> FastAPI migration)
 *
 * The research-submit path is the app's core user journey, and it crosses more
 * migration-sensitive machinery than any other single action in the app:
 *   - CSRF: the hand-rolled ASGI middleware (dependencies/csrf.py) validating
 *     the X-CSRFToken header the real frontend JS sends.
 *   - POST /api/start_research is `async def` but does its real work (four+
 *     get_user_db_session() blocks) via `run_db_sync()` on a threadpool, so
 *     the event loop isn't blocked (see the docstring on start_research()
 *     in web/routers/research.py).
 *   - The ResearchHistory + UserActiveResearch rows are written to the
 *     submitting user's own per-user ENCRYPTED SQLCipher database.
 *   - A background research thread is spawned (start_research_process) that
 *     outlives the HTTP request.
 *   - The progress page opens a python-socketio ASGI realtime channel at
 *     /ws/socket.io and subscribes to a room scoped to the real research id.
 *
 * Despite that, nothing registered in CI drives this path end to end. What
 * exists instead (read before writing this file, per the task):
 *   - NO_CI_executes_research_ajax_research_submission.js,
 *     NO_CI_executes_research_complete_workflow.js,
 *     NO_CI_executes_research_research_submission.js, etc. — all excluded
 *     from CI (the NO_CI_ prefix) specifically because they wait for a real
 *     research run to progress/complete against a real LLM, which this
 *     environment (and real CI) does not have.
 *   - test_research_submit.js — registered in run_all_tests.js but with
 *     `skipCI: true` ("Requires LLM backend").
 *   - test_research_workflow_ci.js / test_research_form_ci.js — CI-registered,
 *     but only assert the form's *structure* (query input + submit button
 *     exist, advanced options toggle, dropdowns render). Neither ever clicks
 *     submit or inspects the resulting HTTP response.
 *   - test_realtime_progress_ci.js — loads /progress/<id> using an id
 *     scraped from history (or a fake `test-id` if history is empty) and
 *     checks DOM structure. It never drives a real submission itself, so it
 *     never proves the id it's looking at was actually just created by a
 *     real POST from a real form fill.
 *   - test_streaming_realtime_ci.js — proves the Socket.IO *transport*
 *     itself works (authenticated handshake succeeds, unauthenticated one is
 *     rejected) against a synthetic `/progress/streaming-test-<ts>` id that
 *     was never created via start_research. That is deliberately NOT
 *     duplicated here; this file reuses that already-proven transport-level
 *     fact and instead checks the one thing that file can't: that the
 *     socket for a page reached via a REAL submission (real id, real
 *     UserActiveResearch/ResearchHistory rows) also connects.
 *   - test_download_and_csrf_flows_ci.js — already proves CSRF rejection
 *     (missing header -> 403) AND acceptance (header present -> success) on
 *     a *different* mutating endpoint (library collections). The "missing
 *     token is rejected" half is deliberately NOT re-proven here — this
 *     file only needs (and only asserts) the acceptance half, on the
 *     research-submission endpoint specifically, via a REAL typed-and-
 *     clicked form submit rather than a synthetic fetch().
 *
 * CRITICAL CONSTRAINT: no real LLM or network search is available here, and
 * a real research run completing is NOT required for this test to pass. The
 * research thread WILL fail shortly after spawning (no reachable LLM
 * backend) — that is an accepted, expected outcome, not a test failure. See
 * "Deliberately OUT OF SCOPE" below.
 *
 * ── What IS asserted (in scope) ──────────────────────────────────────────
 *   1. A real query is typed into #query via page.type() (not injected) and
 *      the form is submitted via a real click on #start-research-btn (not a
 *      synthetic fetch/XHR from the test).
 *   2. The resulting POST to /api/start_research carries the app's own
 *      X-CSRFToken header (read from <meta name="csrf-token">, exactly the
 *      path api.js's real client uses) and is ACCEPTED: HTTP 200 — not
 *      403/400. (Status + request headers are read off the response event
 *      directly; the JSON body is deliberately NOT read here — see the
 *      "response.json() vs navigation" comment in submitResearchViaRealForm
 *      for why that specific read is racy over CDP and how research_id/
 *      "success" are instead confirmed via the post-redirect URL plus a
 *      fresh, later GET in verifyDbAttribution().)
 *   3. The app's OWN client-side JS (not this test) redirects the browser to
 *      /progress/<research_id> after that response — i.e. the real
 *      submit-success UI transition fires, matching the real observed
 *      behaviour (confirmed by reading components/research.js's
 *      handleResearchSubmit: on success it does
 *      `window.location.href = URLBuilder.progressPage(data.research_id)`).
 *      The id is read back out of the resulting URL and checked against a
 *      UUID shape.
 *   4. GET /api/research/<id>/status (same session) confirms a DB row was
 *      actually written and stamped with the submitting username in
 *      metadata.system.user — direct proof of per-user-DB attribution, not
 *      an inference from "the query happened to return something".
 *   5. The progress page renders its real container (#research-progress)
 *      and window.socket completes a real Socket.IO handshake (connected
 *      === true, with a server-assigned sid) for THIS real research id.
 *   6. /history (the real page, not just the API) renders a
 *      `.ldr-history-item[data-id="<id>"]` whose title reflects the
 *      submitted query — proving the record surfaces through the full
 *      render pipeline, not just the JSON endpoint.
 *   7. Per-user DB isolation, the flip side of "attributed to this user": a
 *      SECOND, unrelated user's GET /api/history does not include this
 *      research id, and their GET /api/research/<id>/status 404s. Because
 *      each user's research lives in their own encrypted SQLCipher
 *      database, this is a meaningful end-to-end check of the migration's
 *      per-user-DB plumbing, not a tautology.
 *
 * ── Deliberately OUT OF SCOPE (do not fake this) ─────────────────────────
 *   - Research reaching COMPLETED. The spawned thread will almost certainly
 *     fail fast (no reachable LLM) and the DB row may show FAILED by the
 *     time /history is checked. That is accepted as a pass — see the
 *     status-text logging below, which reports whatever status is observed
 *     without requiring a specific one.
 *   - Any report/results content, citations, log panel entries, or anything
 *     else that depends on the research actually running.
 *   - CSRF *rejection* (missing/invalid token -> 403). Already covered by
 *     test_download_and_csrf_flows_ci.js against a different endpoint; the
 *     mechanism (dependencies/csrf.py) is shared, so re-proving rejection
 *     here would be pure duplication.
 *
 * ── Known test-harness gotcha: the model-field race ──────────────────────
 * model_helper.js's setupDefaultModel() calls selectProvider() then
 * immediately selectModel(), setting #model_hidden synchronously. But
 * changing #model_provider fires research.js's provider-change handler,
 * which asynchronously re-filters a client-side model catalog for the new
 * provider (see research.js: "Filtering models for provider: ... from N
 * models" / "Filtered models for provider ...: 0 models" in the console).
 * In THIS environment there is no live Ollama to discover installed models
 * from, so that filter can yield 0 matches and the async completion then
 * blanks #model_hidden — sometimes *after* our own explicit set, silently
 * clearing it before the click ever fires (verified directly: repeated runs
 * of the naive "select provider, set model, click" sequence intermittently
 * submitted with an empty model and never left the page). This is the same
 * category of flake the skipped NO_CI_executes_research_complete_workflow.js
 * alludes to in its CI-skip comment. It is a test-authoring timing issue,
 * not a product defect: a real user only picks a model from an
 * already-settled list, they don't script a value into it mid-refresh.
 * setModelFieldRobustly() below works around it by leaving #model_provider
 * untouched (default is already "ollama" from settings) and re-asserting
 * the model value for a bounded, explicitly-checked stability window
 * instead of guessing a fixed delay.
 *
 * Run: cd tests/ui_tests && CI=true node test_research_submit_flow_ci.js
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { selectPublicArxivEngine } = require('./research_form_engine_helper');
const { capture, captureOnFailure, screenshotsEnabled } = require('./screenshot_helper');

const BASE_URL = process.env.LDR_BASE_URL || 'http://127.0.0.1:5000';
const isCI = !!process.env.CI;
const NAV_TIMEOUT = isCI ? 60000 : 30000;
const VIEWPORT = { width: 1280, height: 900 };
const SCREENSHOT_PREFIX = 'research_submit_flow';
const TEST_MODEL = 'llama3.2:3b'; // matches model_helper.js's own default; value is never actually invoked

const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

let testsPassed = 0;
let testsFailed = 0;

function pass(msg) {
    testsPassed++;
    console.log(`✅ ${msg}`);
}

function fail(msg) {
    testsFailed++;
    console.error(`❌ ${msg}`);
}

function section(title) {
    console.log(`\n${'='.repeat(70)}\n${title}\n${'='.repeat(70)}`);
}

// ---------------------------------------------------------------------------
// Model-field helper — see the file-header "Known test-harness gotcha".
// ---------------------------------------------------------------------------
/**
 * Set #model_hidden/#model to `model` and hold it there. Re-applies the
 * value on a short interval (inside the page) so an in-flight async
 * provider/model-list refresh that would otherwise blank the field loses
 * the race, then waits — via explicit polling of the actual DOM value, not
 * a fixed sleep — until the value has read back correctly for
 * `requiredStableChecks` consecutive checks before declaring it safe to
 * submit.
 */
async function setModelFieldRobustly(page, model, options = {}) {
    const timeoutMs = options.timeoutMs || (isCI ? 10000 : 6000);
    const requiredStableChecks = options.requiredStableChecks || 5;
    const intervalMs = options.intervalMs || 150;

    await page.evaluate((m) => {
        const apply = () => {
            const hidden = document.querySelector('#model_hidden');
            const visible = document.querySelector('#model');
            if (hidden && hidden.value !== m) {
                hidden.value = m;
                if (visible) visible.value = m;
                hidden.dispatchEvent(new Event('change', { bubbles: true }));
            }
        };
        apply();
        window.__ldrModelGuardInterval = window.setInterval(apply, 100);
    }, model);

    let stableChecks = 0;
    const maxIterations = Math.ceil(timeoutMs / intervalMs);
    for (let i = 0; i < maxIterations && stableChecks < requiredStableChecks; i++) {
        const currentValue = await page.$eval('#model_hidden', (el) => el.value).catch(() => '');
        stableChecks = currentValue === model ? stableChecks + 1 : 0;
        await new Promise((resolve) => setTimeout(resolve, intervalMs));
    }

    await page.evaluate(() => {
        if (window.__ldrModelGuardInterval) {
            clearInterval(window.__ldrModelGuardInterval);
            window.__ldrModelGuardInterval = null;
        }
    });

    const finalValue = await page.$eval('#model_hidden', (el) => el.value).catch(() => '');
    return { stable: stableChecks >= requiredStableChecks, finalValue };
}

// ---------------------------------------------------------------------------
// Step 1: real typing + real click submission, CSRF + acceptance + redirect
// ---------------------------------------------------------------------------
async function submitResearchViaRealForm(page, query) {
    await page.goto(`${BASE_URL}/`, { waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });
    await page.waitForSelector('#query', { timeout: NAV_TIMEOUT });
    await page.waitForFunction(
        () => document.querySelectorAll('#model_provider option').length > 1,
        { timeout: NAV_TIMEOUT }
    );

    await page.type('#query', query);
    await selectPublicArxivEngine(page);

    const modelResult = await setModelFieldRobustly(page, TEST_MODEL);
    if (!modelResult.stable) {
        fail(
            `Form setup: #model_hidden did not settle on "${TEST_MODEL}" ` +
            `(last observed: "${modelResult.finalValue}") — cannot submit meaningfully`
        );
        await capture(page, SCREENSHOT_PREFIX, 'model-field-unstable', { fullPage: true });
        return { ok: false };
    }

    await capture(page, SCREENSHOT_PREFIX, 'filled-form-before-submit', { fullPage: true });

    // Capture the /api/start_research response's status + request headers —
    // NOT its JSON body. Reading the body via CDP (response.json()) races
    // the app's own success handler, which calls window.location.href =
    // ... within the same microtask the fetch promise resolves in; even
    // reading eagerly inside this 'response' handler intermittently loses
    // that race ("Could not load response body for this request"), because
    // response.json() is itself an async CDP round-trip that can still be
    // in flight when the navigation begins. Status code and request headers
    // come with the response event itself (no extra round-trip) and are not
    // subject to this race — verified by running this suite repeatedly:
    // status/headers were reliable every time, body reads were not.
    // research_id and the "success" contract are instead confirmed via the
    // post-redirect URL (deterministic, already proven reliable) and via
    // GET /api/research/<id>/status in verifyDbAttribution() below, which
    // is a fresh, later, non-racing request.
    let startResearchStatus = null;
    let csrfHeaderSent = false;
    const onResponse = (response) => {
        if (response.url().endsWith('/api/start_research')) {
            startResearchStatus = response.status();
            csrfHeaderSent = 'x-csrftoken' in response.request().headers();
        }
    };
    page.on('response', onResponse);

    const submitBtn = await page.$('#start-research-btn');
    if (!submitBtn) {
        page.off('response', onResponse);
        fail('Form setup: #start-research-btn not found on the page');
        return { ok: false };
    }
    await submitBtn.click();

    let redirected = true;
    try {
        await page.waitForFunction(
            () => window.location.pathname.startsWith('/progress/'),
            { timeout: NAV_TIMEOUT }
        );
    } catch {
        redirected = false;
    }
    page.off('response', onResponse);

    if (startResearchStatus === null) {
        fail('POST /api/start_research: no response captured for the real form submission');
        await captureOnFailure(page, SCREENSHOT_PREFIX, 'no-start-research-response', false);
        return { ok: false };
    }

    const accepted = startResearchStatus === 200 && csrfHeaderSent;
    if (accepted) {
        pass(
            `POST /api/start_research: real form submit ACCEPTED (HTTP ${startResearchStatus}) ` +
            `carrying the app's X-CSRFToken header (not 403/400)`
        );
    } else {
        fail(
            `GENUINE DEFECT candidate: real form submit expected HTTP 200 with an ` +
            `X-CSRFToken header, got status=${startResearchStatus}, csrfHeaderSent=${csrfHeaderSent}`
        );
    }
    await captureOnFailure(page, SCREENSHOT_PREFIX, 'start-research-not-accepted', accepted);

    // The app's own client JS (components/research.js's handleResearchSubmit)
    // redirects via URLBuilder.progressPage(data.research_id) on success —
    // extracting the id from the resulting URL is proof the server returned
    // a real id AND that the client parsed {status:"success", research_id}
    // out of the response body correctly (a malformed/missing id would leave
    // the redirect never firing, which `redirected` below would catch).
    const currentUrl = page.url();
    const progressMatch = currentUrl.match(/\/progress\/([^/?#]+)$/);
    const researchId = progressMatch ? progressMatch[1] : null;
    const idLooksValid = !!researchId && UUID_RE.test(researchId);

    if (redirected && idLooksValid) {
        pass(
            `UI transition: the app's own client JS redirected to /progress/${researchId} ` +
            `after the accepted response (not driven by this test)`
        );
    } else {
        fail(
            `GENUINE DEFECT candidate: expected a client-side redirect to /progress/<uuid>, ` +
            `got redirected=${redirected}, url=${currentUrl}`
        );
    }
    await captureOnFailure(page, SCREENSHOT_PREFIX, 'no-progress-redirect', redirected && idLooksValid);

    return { ok: accepted && redirected && idLooksValid, researchId, query };
}

// ---------------------------------------------------------------------------
// Step 2: confirm the DB row exists and is stamped with this username
// ---------------------------------------------------------------------------
async function verifyDbAttribution(page, researchId, username) {
    const result = await page.evaluate(async (id) => {
        const resp = await fetch(`/api/research/${id}/status`, { credentials: 'same-origin' });
        let json = null;
        try {
            json = await resp.json();
        } catch {
            // leave json as null
        }
        return { status: resp.status, json };
    }, researchId);

    const attributedUser = result.json && result.json.metadata && result.json.metadata.system
        ? result.json.metadata.system.user
        : undefined;

    if (result.status === 200 && attributedUser === username) {
        pass(
            `GET /api/research/${researchId}/status: DB row exists and ` +
            `metadata.system.user === "${username}" (per-user-DB attribution confirmed at write time)`
        );
    } else {
        fail(
            `GENUINE DEFECT candidate: expected HTTP 200 with metadata.system.user="${username}", ` +
            `got status=${result.status}, attributedUser="${attributedUser}"`
        );
    }

    return { status: result.json ? result.json.status : null };
}

// ---------------------------------------------------------------------------
// Step 3: progress page renders + realtime channel opens for the REAL id
// ---------------------------------------------------------------------------
async function verifyProgressPageAndRealtimeChannel(page, researchId) {
    const hasContainer = await page.$('#research-progress.ldr-page');
    if (hasContainer) {
        pass(`Progress page: #research-progress container rendered for /progress/${researchId}`);
    } else {
        fail(`GENUINE DEFECT candidate: /progress/${researchId} did not render #research-progress`);
    }

    let socketConnected;
    try {
        await page.waitForFunction(
            () => !!(window.socket && window.socket.isConnected && window.socket.isConnected()),
            { timeout: isCI ? 20000 : 10000 }
        );
        socketConnected = true;
    } catch {
        socketConnected = false;
    }

    if (socketConnected) {
        const details = await page.evaluate(() => {
            const inst = window.socket.getSocketInstance && window.socket.getSocketInstance();
            return { hasInstance: !!inst, id: inst ? inst.id : null };
        });
        if (details.hasInstance && details.id) {
            pass(
                `Progress page: window.socket completed a real Socket.IO handshake ` +
                `(sid=${details.id}) for the real research id`
            );
        } else {
            fail(`GENUINE DEFECT candidate: isConnected()===true but no socket instance/sid: ${JSON.stringify(details)}`);
        }
    } else {
        fail(
            `GENUINE DEFECT candidate: window.socket never reached isConnected()===true ` +
            `on the progress page for a real just-created research id`
        );
    }
    await capture(page, SCREENSHOT_PREFIX, 'progress-page-after-transition', { fullPage: true });
    await captureOnFailure(page, SCREENSHOT_PREFIX, 'progress-page-no-realtime', socketConnected);
}

// ---------------------------------------------------------------------------
// Step 4: /history (the real page) shows the record, attributed to this user
// ---------------------------------------------------------------------------
async function verifyHistoryPageShowsRecord(page, researchId, query) {
    await page.goto(`${BASE_URL}/history`, { waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });

    const itemSelector = `#history-items .ldr-history-item[data-id="${researchId}"]`;
    let found = true;
    try {
        await page.waitForSelector(itemSelector, { timeout: NAV_TIMEOUT });
    } catch {
        found = false;
    }

    if (!found) {
        fail(`GENUINE DEFECT candidate: /history never rendered a row for research id ${researchId}`);
        await capture(page, SCREENSHOT_PREFIX, 'history-missing-record', { fullPage: true });
        return;
    }

    const title = await page.$eval(`${itemSelector} .ldr-history-item-title`, (el) => el.textContent.trim());
    const statusText = await page.$eval(`${itemSelector} .ldr-history-item-status`, (el) => el.textContent.trim())
        .catch(() => '(no status element)');

    if (title.includes(query)) {
        pass(
            `/history: rendered row for research id ${researchId} with title matching the ` +
            `submitted query, status shown as "${statusText}" (any status is acceptable — ` +
            `see file header: completion is out of scope, FAILED is a fine outcome here)`
        );
    } else {
        fail(`GENUINE DEFECT candidate: /history row title "${title}" does not contain the submitted query "${query}"`);
    }
}

// ---------------------------------------------------------------------------
// Step 5: per-user DB isolation — a different user cannot see this record
// ---------------------------------------------------------------------------
async function verifyCrossUserIsolation(auth, researchId) {
    await auth.logout();

    const isolationUsername = `research_submit_isolation_${Date.now()}`;
    const isolationPassword = 'T3st!Secure#2024$LDR'; // pragma: allowlist secret
    await auth.ensureAuthenticated(isolationUsername, isolationPassword);
    const page = auth.getPage();

    const historyResult = await page.evaluate(async (id) => {
        const resp = await fetch('/api/history', { credentials: 'same-origin' });
        const json = await resp.json();
        const match = (json.items || []).find((item) => item.id === id) || null;
        return { status: resp.status, match };
    }, researchId);

    if (historyResult.status === 200 && historyResult.match === null) {
        pass(
            `Cross-user isolation: a second, unrelated user's /api/history does NOT ` +
            `include research id ${researchId} (per-user encrypted DB boundary holds)`
        );
    } else {
        fail(
            `GENUINE DEFECT candidate: SECURITY — a second user's /api/history leaked ` +
            `another user's research: ${JSON.stringify(historyResult)}`
        );
    }

    const statusResult = await page.evaluate(async (id) => {
        const resp = await fetch(`/api/research/${id}/status`, { credentials: 'same-origin' });
        return { status: resp.status };
    }, researchId);

    if (statusResult.status === 404) {
        pass(
            `Cross-user isolation: a second, unrelated user's GET /api/research/${researchId}/status ` +
            `returns 404 (not leaked, not a 500)`
        );
    } else {
        fail(
            `GENUINE DEFECT candidate: SECURITY — a second user's GET /api/research/${researchId}/status ` +
            `returned ${statusResult.status} instead of 404`
        );
    }

    return page;
}

// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
    console.log(`🧪 Research Submission Flow UI test (CI mode: ${isCI}) against ${BASE_URL}`);
    console.log(`   Screenshots: ${screenshotsEnabled() ? 'ENABLED (LDR_UI_SCREENSHOTS set)' : 'disabled (default)'}`);

    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    await page.setViewport(VIEWPORT);
    if (isCI) {
        page.setDefaultTimeout(60000);
        page.setDefaultNavigationTimeout(60000);
    }
    page.on('pageerror', (err) => console.log('  PAGE ERROR:', err.message));

    try {
        const authHelper = new AuthHelper(page, BASE_URL);
        await authHelper.ensureAuthenticatedWithTimeout();
        const authedPage = authHelper.getPage();
        await authedPage.setViewport(VIEWPORT);

        // Idempotent + collision-proof across reruns/parallel shards: a
        // timestamp+random-suffixed query, scoped to this file's own prefix.
        // Kept under 57 chars deliberately: history.js's formatTitleFromQuery()
        // truncates (query.length > 60 -> first 57 chars + "...") before
        // rendering the /history row title, and this exact string is what
        // verifyHistoryPageShowsRecord() below matches against — a longer
        // "realistic" query would get silently truncated and fail that check
        // for a reason that has nothing to do with the migration.
        const uniqueQuery = `LDR_SUBMIT_${Date.now()}_${Math.floor(Math.random() * 1e6)} test query`;

        // base.html renders the logged-in username into <meta name="user-id">
        // on every authenticated page — read it directly rather than
        // hardcoding the username this run happened to register with.
        const submittingUsername = await authedPage.evaluate(() => {
            const meta = document.querySelector('meta[name="user-id"]');
            return meta ? meta.content : null;
        });

        section('Real form submission: typing, CSRF header, HTTP acceptance, UI redirect');
        const submitResult = await submitResearchViaRealForm(authedPage, uniqueQuery);

        if (submitResult.ok && submitResult.researchId) {
            section('DB write attribution (per-user encrypted database)');
            await verifyDbAttribution(authedPage, submitResult.researchId, submittingUsername);

            section('Progress page + realtime (Socket.IO) channel');
            await verifyProgressPageAndRealtimeChannel(authedPage, submitResult.researchId);

            section('History page shows the record');
            await verifyHistoryPageShowsRecord(authedPage, submitResult.researchId, uniqueQuery);

            section('Cross-user isolation (per-user encrypted DB boundary)');
            await verifyCrossUserIsolation(authHelper, submitResult.researchId);
        } else {
            fail('Submission step failed — skipping downstream progress/history/isolation checks (no valid research id)');
        }
    } catch (error) {
        fail(`Fatal test-harness error: ${error.message}`);
        console.error(error.stack);
    } finally {
        await browser.close();
    }

    console.log('\n' + '='.repeat(70));
    console.log(`📊 Research Submission Flow tests: ${testsPassed} passed, ${testsFailed} failed`);
    console.log('='.repeat(70));

    process.exit(testsFailed === 0 ? 0 : 1);
}

main().catch((error) => {
    console.error('Fatal error:', error);
    process.exit(1);
});