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.

22.5 KB
Raw Blame
#!/usr/bin/env node
/**
 * Navigation & Theme Browser Tests (Flask -> FastAPI migration)
 *
 * Every route in the app was re-registered during the migration (Flask
 * blueprints -> FastAPI routers: see src/local_deep_research/web/routers/*.py
 * and their url_for()-generated hrefs in components/sidebar.html), so a
 * dead/renamed page route is a live risk. Nothing existing walked the whole
 * navigation surface in a browser off the DOM, checked active-nav state, or
 * exercised the theme selector.
 *
 * Overlap check against existing UI tests (read before writing this file):
 *   - test_full_navigation.js and test_pages_browser.js both navigate a
 *     HARDCODED list of ~6 URLs (/, /settings/, /metrics/, /history/,
 *     /benchmark/, /cost-analytics/) and assert page-specific content
 *     selectors (query input, settings form, metrics elements, etc). They do
 *     NOT: discover links from the DOM (so a link the migration silently
 *     re-pointed at a dead route wouldn't be caught — the hardcoded list
 *     would just skip it), assert HTTP status/Content-Type per page, assert
 *     active-nav state, touch the theme selector, or compare click-through
 *     vs. direct-load. Those page-specific content assertions are NOT
 *     duplicated here; the four things above are genuinely new coverage.
 *
 * What this file proves, from inside a real Chromium tab:
 *   1. Every sidebar link, enumerated live from .ldr-sidebar-nav (not a
 *      hardcoded URL list), resolves to a real rendered page: 2xx status,
 *      text/html Content-Type (a broken FastAPI route yields the JSON
 *      catch-all from _register_exception_handlers() in fastapi_app.py --
 *      {"error": "Not found"} / {"error": "Server error"} -- which this
 *      test would flag), and zero uncaught JS errors.
 *   2. After navigating to each page, exactly that page's sidebar <li> is
 *      marked .active and no other -- active_page is now threaded through
 *      per-router template context (see routers/notes.py, routers/rag.py,
 *      etc.) instead of Flask's, so a router that forgot to set it, or set
 *      the wrong slug, would silently desync the sidebar highlight.
 *   3. The header theme dropdown (#theme-dropdown) changes <html data-theme>
 *      and localStorage immediately, and the choice survives a real reload
 *      -- exercising src/.../static/js/services/theme.js end to end.
 *   4. For pages reachable via the sidebar, a genuinely fresh browser tab
 *      going straight to the same URL (no referer, no prior client state --
 *      the "typed the URL" / "opened a bookmark" case) gets the same HTTP
 *      status, <title>, and active-nav state as arriving via a sidebar
 *      click. This is the check that would catch a route that only renders
 *      correctly when reached through the app's own navigation.
 *
 * Console-error filtering rationale (same as test_streaming_realtime_ci.js):
 * "Failed to load resource" messages carry no URL/stack and fire for the
 * browser's own speculative /favicon.ico probe (base.html declares
 * favicon.png; no .ico exists) -- not attributable to a real bug. Everything
 * else counts.
 *
 * Run: CI=true node test_navigation_and_theme_ci.js
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { capture } = 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 }; // desktop width: sidebar is display:none below 767px (mobile-responsive.css)
const SCREENSHOT_PREFIX = 'nav_theme';

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)}`);
}

function isRealConsoleError(msg) {
    return msg.type() === 'error' && !msg.text().startsWith('Failed to load resource');
}

// ---------------------------------------------------------------------------
// DOM discovery helpers
// ---------------------------------------------------------------------------

/** Enumerate the sidebar link inventory straight from the DOM -- no hardcoded URL list. */
async function getSidebarLinks(page) {
    return page.evaluate(() => {
        const items = Array.from(document.querySelectorAll('.ldr-sidebar-nav li[data-page]'));
        return items.map((li) => {
            const a = li.querySelector('a');
            const textEl = a ? a.querySelector('.ldr-nav-text') : null;
            return {
                dataPage: li.getAttribute('data-page'),
                href: a ? a.getAttribute('href') : null,
                text: textEl ? textEl.textContent.trim() : (a ? a.textContent.trim() : null),
            };
        }).filter((link) => !!link.href);
    });
}

/** data-page values of every currently .active sidebar <li>. */
function getActiveDataPages(page) {
    return page.evaluate(() =>
        Array.from(document.querySelectorAll('.ldr-sidebar-nav li.active'))
            .map((li) => li.getAttribute('data-page'))
    );
}

// ---------------------------------------------------------------------------
// Tests 1 + 2: walk every discovered sidebar link
// ---------------------------------------------------------------------------
async function walkSidebarLinks(page) {
    await page.goto(`${BASE_URL}/`, { waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });
    await AuthHelper.waitForStable(page, '.ldr-sidebar-nav', { timeout: 5000 }).catch(() => {});

    const links = await getSidebarLinks(page);

    if (links.length === 0) {
        fail('Sidebar link discovery: found 0 links under .ldr-sidebar-nav li[data-page] -- selector may be stale after the migration');
        return [];
    }

    console.log(`\n📋 Discovered ${links.length} sidebar nav links:`);
    links.forEach((l, i) => console.log(`   ${i + 1}. [${l.dataPage}] "${l.text}" -> ${l.href}`));

    for (const link of links) {
        const targetUrl = new URL(link.href, BASE_URL).toString();
        const targetPath = new URL(targetUrl).pathname;

        const consoleErrors = [];
        const pageErrors = [];
        const onConsole = (msg) => { if (isRealConsoleError(msg)) consoleErrors.push(msg.text()); };
        const onPageError = (err) => pageErrors.push(err.message);
        page.on('console', onConsole);
        page.on('pageerror', onPageError);

        let response = null;
        let navError = null;
        try {
            const currentPath = new URL(page.url()).pathname;
            if (currentPath === targetPath) {
                // Same-URL click fires no navigation event in Chrome (only true
                // for the entry link -- login lands on "/", the Home link's own
                // target). Force a real reload instead of silently skipping it.
                response = await page.goto(targetUrl, { waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });
            } else {
                const linkHandle = await page.$(`.ldr-sidebar-nav li[data-page="${link.dataPage}"] a`);
                if (!linkHandle) {
                    throw new Error('link element vanished from the DOM before it could be clicked');
                }
                [response] = await Promise.all([
                    page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT }),
                    linkHandle.click(),
                ]);
            }
            await AuthHelper.waitForStable(page, 'body', { timeout: 3000, idleMs: 200 }).catch(() => {});
        } catch (e) {
            navError = e;
        }

        page.off('console', onConsole);
        page.off('pageerror', onPageError);

        const status = response ? response.status() : null;
        const contentType = response ? (response.headers()['content-type'] || '') : '';
        const title = await page.title().catch(() => '');

        // Capture each page as it's visited -- opt-in only, see screenshot_helper.js.
        await capture(page, SCREENSHOT_PREFIX, link.dataPage || 'unknown-page');

        // --- Test 1: the link resolves to a working page ---
        if (navError) {
            fail(`Nav link [${link.dataPage}] "${link.text}" (${link.href}): navigation error: ${navError.message}`);
        } else if (status === null) {
            fail(`Nav link [${link.dataPage}] "${link.text}" (${link.href}): no HTTP response captured`);
        } else if (status < 200 || status >= 400) {
            fail(`GENUINE DEFECT: Nav link [${link.dataPage}] "${link.text}" (${link.href}) returned HTTP ${status} -- a route the migration left broken`);
        } else if (!contentType.startsWith('text/html')) {
            fail(`GENUINE DEFECT: Nav link [${link.dataPage}] "${link.text}" (${link.href}) did not return HTML (Content-Type: "${contentType}") -- looks like the FastAPI JSON error handler, not a rendered page`);
        } else if (!title) {
            fail(`Nav link [${link.dataPage}] "${link.text}" (${link.href}): page has no <title>`);
        } else if (consoleErrors.length > 0 || pageErrors.length > 0) {
            fail(`Nav link [${link.dataPage}] "${link.text}" (${link.href}): JS error(s): ${[...pageErrors, ...consoleErrors].join(' | ')}`);
        } else {
            pass(`Nav link [${link.dataPage}] "${link.text}": HTTP ${status}, HTML, title="${title}", no JS errors`);
        }

        // --- Test 2: active-nav state matches the current page ---
        const activePages = await getActiveDataPages(page);
        if (activePages.length === 1 && activePages[0] === link.dataPage) {
            pass(`Active-nav state: [${link.dataPage}] correctly (and solely) marked .active after navigating there`);
        } else {
            fail(`GENUINE DEFECT: active-nav state after navigating to [${link.dataPage}] (${link.href}) -- expected exactly ["${link.dataPage}"] marked .active, found ${JSON.stringify(activePages)}`);
        }
    }

    return links;
}

// ---------------------------------------------------------------------------
// Test 3: theme switching + persistence
// ---------------------------------------------------------------------------
async function testThemeSwitchingAndPersistence(page) {
    section('Theme switching + persistence');

    await page.goto(`${BASE_URL}/`, { waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });
    await page.waitForSelector('#theme-dropdown', { timeout: 10000 });
    await page.waitForFunction(
        () => document.querySelectorAll('#theme-dropdown option').length > 0,
        { timeout: 5000 }
    );

    const originalTheme = await page.$eval('#theme-dropdown', (el) => el.value);
    const availableThemes = await page.$$eval('#theme-dropdown option', (opts) => opts.map((o) => o.value));
    const targetTheme = availableThemes.find((t) => t !== originalTheme);

    if (!targetTheme) {
        fail(`Theme switching: only one theme option available (${JSON.stringify(availableThemes)}), cannot exercise switching`);
        return;
    }

    console.log(`   Available themes: ${availableThemes.join(', ')}`);
    console.log(`   Original theme: "${originalTheme}" -> switching to: "${targetTheme}"`);

    // Reads <html data-theme> + the user-scoped localStorage key theme.js
    // writes (STORAGE_KEY_PREFIX = 'ldr-theme', see services/theme.js). We
    // scan for the key by prefix (same technique as theme.js's own
    // clearAllThemes()) instead of hardcoding the logged-in username.
    async function readThemeState() {
        return page.evaluate((prefix) => {
            let storageKey = null;
            for (let i = 0; i < window.localStorage.length; i++) {
                const key = window.localStorage.key(i);
                if (key && key.startsWith(prefix)) {
                    storageKey = key;
                    break;
                }
            }
            const dropdown = document.getElementById('theme-dropdown');
            return {
                dataTheme: document.documentElement.getAttribute('data-theme'),
                storedRaw: storageKey ? window.localStorage.getItem(storageKey) : null,
                storageKey,
                dropdownValue: dropdown ? dropdown.value : null,
            };
        }, 'ldr-theme-');
    }

    async function selectTheme(themeValue) {
        await page.select('#theme-dropdown', themeValue);
        // setTheme() -> applyTheme() writes the data-theme attribute and
        // localStorage synchronously on the 'change' handler; wait for the
        // attribute to actually reflect the new selection rather than
        // assuming the microtask has flushed.
        await page.waitForFunction(
            (expectedRaw, expectedStoredPrefix) => {
                const dropdown = document.getElementById('theme-dropdown');
                if (!dropdown || dropdown.value !== expectedRaw) return false;
                for (let i = 0; i < window.localStorage.length; i++) {
                    const key = window.localStorage.key(i);
                    if (key && key.startsWith(expectedStoredPrefix) && window.localStorage.getItem(key) === expectedRaw) {
                        return true;
                    }
                }
                return false;
            },
            { timeout: 5000 },
            themeValue,
            'ldr-theme-'
        );
    }

    // --- Switch theme ---
    await selectTheme(targetTheme);
    const afterSwitch = await readThemeState();

    if (afterSwitch.storedRaw === targetTheme) {
        pass(`Theme switching: localStorage["${afterSwitch.storageKey}"] = "${targetTheme}" immediately after selecting it`);
    } else {
        fail(`GENUINE DEFECT: theme switch did not persist to localStorage -- expected "${targetTheme}", got "${afterSwitch.storedRaw}" (key: ${afterSwitch.storageKey})`);
    }

    // 'system' resolves to 'hashed' or 'light' depending on OS color-scheme
    // preference (getEffectiveTheme() in theme.js); every other theme's
    // data-theme equals the raw value 1:1.
    if (targetTheme === 'system') {
        const expectedSystemTheme = await page.evaluate(() => (
            window.matchMedia('(prefers-color-scheme: dark)').matches ? 'hashed' : 'light'
        ));
        if (afterSwitch.dataTheme === expectedSystemTheme) {
            pass(`Theme switching: <html data-theme> resolved "system" to "${afterSwitch.dataTheme}"`);
        } else {
            fail(`GENUINE DEFECT: "system" theme resolved to unexpected data-theme="${afterSwitch.dataTheme}"`);
        }
    } else if (afterSwitch.dataTheme === targetTheme) {
        pass(`Theme switching: <html data-theme="${afterSwitch.dataTheme}"> matches the selected theme`);
    } else {
        fail(`GENUINE DEFECT: <html data-theme> is "${afterSwitch.dataTheme}", expected "${targetTheme}"`);
    }

    // --- Persist across a real reload ---
    await page.reload({ waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });
    await page.waitForSelector('#theme-dropdown', { timeout: 10000 });
    await page.waitForFunction(
        () => document.querySelectorAll('#theme-dropdown option').length > 0,
        { timeout: 5000 }
    );
    const afterReload = await readThemeState();

    if (afterReload.storedRaw === targetTheme && afterReload.dropdownValue === targetTheme) {
        pass(`Theme persistence: survived reload -- localStorage and dropdown both still "${targetTheme}"`);
    } else {
        fail(`GENUINE DEFECT: theme did not persist across reload -- localStorage="${afterReload.storedRaw}", dropdown="${afterReload.dropdownValue}" (expected "${targetTheme}")`);
    }

    if (afterReload.dataTheme === afterSwitch.dataTheme) {
        pass(`Theme persistence: <html data-theme="${afterReload.dataTheme}"> unchanged across reload`);
    } else {
        fail(`GENUINE DEFECT: <html data-theme> changed across reload: "${afterSwitch.dataTheme}" -> "${afterReload.dataTheme}"`);
    }

    // --- Restore original theme so the test is idempotent ---
    await selectTheme(originalTheme);
    const restored = await readThemeState();
    if (restored.dropdownValue === originalTheme && restored.storedRaw === originalTheme) {
        pass(`Theme restore: original theme "${originalTheme}" restored -- test left no residue`);
    } else {
        fail(`Theme restore FAILED -- left theme as dropdown="${restored.dropdownValue}"/storage="${restored.storedRaw}" instead of original "${originalTheme}". Manual cleanup may be needed.`);
    }
}

// ---------------------------------------------------------------------------
// Test 4: deep-link / direct-navigation parity
// ---------------------------------------------------------------------------
async function testDeepLinkParity(page, browser, links) {
    section('Deep-link / direct-navigation parity');

    // Pick two pages from different sidebar sections so the check isn't
    // trivially narrow to one router module.
    const candidateDataPages = ['history', 'library'];
    const candidates = candidateDataPages
        .map((dp) => links.find((l) => l.dataPage === dp))
        .filter(Boolean);

    if (candidates.length === 0) {
        fail(`Deep-link parity: none of ${JSON.stringify(candidateDataPages)} were found among discovered sidebar links -- cannot test`);
        return;
    }

    for (const link of candidates) {
        const targetUrl = new URL(link.href, BASE_URL).toString();

        // --- Path A: reach it by clicking through the nav from Home ---
        await page.goto(`${BASE_URL}/`, { waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });
        const navLink = await page.$(`.ldr-sidebar-nav li[data-page="${link.dataPage}"] a`);
        if (!navLink) {
            fail(`Deep-link parity [${link.dataPage}]: nav link disappeared on second pass`);
            continue;
        }
        const [clickResponse] = await Promise.all([
            page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT }),
            navLink.click(),
        ]);
        await AuthHelper.waitForStable(page, 'body', { timeout: 3000, idleMs: 200 }).catch(() => {});
        const viaClick = {
            status: clickResponse ? clickResponse.status() : null,
            title: await page.title(),
            activePages: await getActiveDataPages(page),
        };

        // --- Path B: a genuinely fresh tab going straight to the URL, the
        // way a bookmark or a shared link would (no prior client state, no
        // referer from the sidebar). Same browser context, so the session
        // cookie carries over -- this isolates "does the route itself work
        // standalone" from "am I authenticated". ---
        const freshPage = await browser.newPage();
        let viaDirect;
        try {
            const directResponse = await freshPage.goto(targetUrl, { waitUntil: 'domcontentloaded', timeout: NAV_TIMEOUT });
            await AuthHelper.waitForStable(freshPage, 'body', { timeout: 3000, idleMs: 200 }).catch(() => {});
            viaDirect = {
                status: directResponse ? directResponse.status() : null,
                title: await freshPage.title(),
                activePages: await getActiveDataPages(freshPage),
            };
        } finally {
            await freshPage.close();
        }

        console.log(`   [${link.dataPage}] via click:       HTTP ${viaClick.status}, title="${viaClick.title}", active=${JSON.stringify(viaClick.activePages)}`);
        console.log(`   [${link.dataPage}] via direct load: HTTP ${viaDirect.status}, title="${viaDirect.title}", active=${JSON.stringify(viaDirect.activePages)}`);

        if (viaDirect.status === null || viaDirect.status < 200 || viaDirect.status >= 400) {
            fail(`GENUINE DEFECT: [${link.dataPage}] (${targetUrl}) fails on a direct fresh-tab load (HTTP ${viaDirect.status}) despite working via sidebar click -- classic client-routing-only regression`);
        } else {
            pass(`Deep-link parity [${link.dataPage}]: direct fresh-tab load returns HTTP ${viaDirect.status}`);
        }

        if (viaClick.title && viaClick.title === viaDirect.title) {
            pass(`Deep-link parity [${link.dataPage}]: <title> matches between click-through and direct load ("${viaClick.title}")`);
        } else {
            fail(`GENUINE DEFECT: [${link.dataPage}] <title> differs -- click-through="${viaClick.title}" vs direct-load="${viaDirect.title}"`);
        }

        const clickActive = JSON.stringify(viaClick.activePages);
        const directActive = JSON.stringify(viaDirect.activePages);
        if (clickActive === directActive && viaClick.activePages.includes(link.dataPage)) {
            pass(`Deep-link parity [${link.dataPage}]: active-nav state matches between click-through and direct load (${clickActive})`);
        } else {
            fail(`GENUINE DEFECT: [${link.dataPage}] active-nav state differs -- click-through=${clickActive} vs direct-load=${directActive}`);
        }
    }
}

// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
    console.log(`🧪 Navigation & Theme UI tests (CI mode: ${isCI}) against ${BASE_URL}`);

    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    await page.setViewport(VIEWPORT);
    if (isCI) {
        page.setDefaultTimeout(60000);
        page.setDefaultNavigationTimeout(60000);
    }

    try {
        const authHelper = new AuthHelper(page, BASE_URL);
        await authHelper.ensureAuthenticatedWithTimeout();
        const authedPage = authHelper.getPage();
        // ensureAuthenticated() may have swapped in a fresh Page after a
        // detached-frame recovery -- re-apply the desktop viewport to it.
        await authedPage.setViewport(VIEWPORT);

        section('Sidebar link inventory + per-link health + active-nav state');
        const links = await walkSidebarLinks(authedPage);

        await testThemeSwitchingAndPersistence(authedPage);

        if (links.length > 0) {
            await testDeepLinkParity(authedPage, browser, links);
        } else {
            fail('Deep-link parity: skipped -- sidebar link discovery found nothing to test');
        }
    } catch (error) {
        fail(`Fatal test-harness error: ${error.message}`);
        console.error(error.stack);
    } finally {
        await browser.close();
    }

    console.log('\n' + '='.repeat(70));
    console.log(`📊 Navigation & Theme 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);
});