~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
#!/usr/bin/env node
/**
* Streaming / Realtime Browser Tests (Flask -> FastAPI migration)
*
* The migration rewrote the two "push data to the browser without a page
* reload" mechanisms wholesale:
* - Flask streamed responses -> Starlette StreamingResponse (SSE)
* - flask-socketio (WSGI, threading) -> python-socketio AsyncServer,
* mounted as an ASGI sub-app at /ws
*
* Every existing UI test that touches these paths only checks that DOM
* elements/routes exist; none of them prove a browser actually RECEIVES
* streamed or pushed data over the new transport. Unit tests can't fill
* that gap either — they stub the transport (the socketio AsyncServer /
* StreamingResponse) rather than driving it end-to-end.
*
* This file proves, from inside a real Chromium tab:
* 1. The app's real Socket.IO client (window.socket, backed by the
* bundled socket.io-client — see static/js/services/socket.js, which
* hardcodes path '/ws/socket.io') completes a real ASGI WebSocket
* handshake when authenticated, AND that an unauthenticated handshake
* against the exact same endpoint is rejected by the server
* (socketio_asgi.py's `connect` handler returns False for sessions
* with no username — see the code comment there for the security
* rationale: accepting it would leak roomless broadcasts, e.g.
* parallel_search_started, to anyone).
* 2. A real SSE endpoint (POST /library/api/rag/index-all, StreamingResponse
* + media_type="text/event-stream") delivers its body to the browser
* as multiple discrete chunks over chunked transfer-encoding, carrying
* the anti-buffering headers the frontend depends on, and that the
* chunks parse as the "data: {...}" SSE event(s) collection_details.js
* and download_manager.html actually consume via
* `response.body.getReader()` (this codebase does not use the
* EventSource API anywhere — every real streaming consumer is
* fetch + ReadableStream reader, so that's what this test drives too).
* 3. The pages that most depend on these two subsystems load with no
* uncaught JS errors post-migration.
*
* Endpoint choice for (2): /library/api/rag/index-all was picked over the
* other three SSE endpoints in the codebase (POST /library/api/download-all-text,
* POST /library/api/download-research/<id>, POST /library/api/collections/<id>/index)
* because it needs no request body/seed data, needs no LLM, and —
* critically — every freshly authenticated user already has an empty
* default "Library" collection (ensure_default_library_collection() runs at
* login), so the generator's "no documents to index" branch fires in well
* under a second while still emitting >1 real SSE event over the wire
* (verified manually against the running dev server: 'start' then
* 'complete', delivered as 2 separate ReadableStream reads).
*
* Run: CI=true node test_streaming_realtime_ci.js
*/
const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { captureOnFailure } = require('./screenshot_helper');
const BASE_URL = process.env.LDR_BASE_URL || 'http://127.0.0.1:5000';
const isCI = !!process.env.CI;
const SCREENSHOT_PREFIX = 'streaming_realtime';
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)}`);
}
// Console "Failed to load resource" messages carry no URL and cannot be
// attributed to anything real (they also fire for the browser's own
// speculative /favicon.ico probe — the app ships favicon.png, declared by
// base.html, and no .ico). Same filtering rationale as
// test_frontend_bundle_integrity_ci.js: only real JS errors count here.
function isRealConsoleError(msg) {
return msg.type() === 'error' && !msg.text().startsWith('Failed to load resource');
}
// ---------------------------------------------------------------------------
// Test 1a: authenticated Socket.IO handshake via the app's real client
// ---------------------------------------------------------------------------
async function testAuthenticatedSocketConnects(page) {
// /progress/<id> is one of the paths socket.js's isResearchPage() check
// matches, so the app auto-initializes window.socket on load (see
// autoInitSocket() in socket.js). The route renders pages/progress.html
// unconditionally for ANY research_id (no 404), so a made-up id is fine
// for exercising the *transport* — this test does not need real research
// data, only a real authenticated handshake.
await page.goto(`${BASE_URL}/progress/streaming-test-${Date.now()}`, {
waitUntil: 'domcontentloaded',
timeout: isCI ? 60000 : 30000,
});
try {
await page.waitForFunction(
() => !!(window.socket && window.socket.isConnected && window.socket.isConnected()),
{ timeout: isCI ? 20000 : 10000 }
);
} catch {
fail('Authenticated Socket.IO: window.socket never reached isConnected()===true');
await captureOnFailure(page, SCREENSHOT_PREFIX, 'authenticated_socket', false);
return;
}
// Confirm it's a real engine.io/socket.io handshake, not just the
// wrapper's boolean flag: the underlying socket.io-client instance must
// report connected=true and carry a session id assigned by the server.
const details = await page.evaluate(() => {
const inst = window.socket.getSocketInstance && window.socket.getSocketInstance();
return {
hasInstance: !!inst,
connected: inst ? inst.connected : false,
id: inst ? inst.id : null,
usingPolling: window.socket.isUsingPolling ? window.socket.isUsingPolling() : null,
};
});
if (details.hasInstance && details.connected && details.id) {
pass(
`Authenticated Socket.IO: real handshake completed (sid=${details.id}, ` +
`fallback-polling=${details.usingPolling})`
);
} else {
fail(`Authenticated Socket.IO: instance/connected/id incomplete: ${JSON.stringify(details)}`);
await captureOnFailure(page, SCREENSHOT_PREFIX, 'authenticated_socket', false);
}
}
// ---------------------------------------------------------------------------
// Test 1b: unauthenticated handshake against the SAME endpoint is rejected
// ---------------------------------------------------------------------------
async function testUnauthenticatedSocketRejected(browser) {
// A fresh incognito browser context has no cookies at all — distinct
// from just navigating a second tab in the same context (which would
// inherit the already-authenticated session and silently pass).
const incognitoContext = await browser.createBrowserContext();
try {
const page = await incognitoContext.newPage();
const cookiesBefore = await page.cookies(BASE_URL);
if (cookiesBefore.length !== 0) {
fail(`Unauthenticated Socket.IO: incognito context was not clean (had ${cookiesBefore.length} cookie(s))`);
await captureOnFailure(page, SCREENSHOT_PREFIX, 'unauthenticated_socket', false);
return;
}
// Any page that loads the app bundle exposes window.io (socket.io-client
// is bundled into app.js and loaded on every page — see app.js's
// `window.io = io`). /auth/login is public and pre-auth, so this proves
// the client library itself is available without a session, and lets us
// drive a handshake with the exact same path the real app uses.
await page.goto(`${BASE_URL}/auth/login`, { waitUntil: 'domcontentloaded', timeout: 30000 });
const result = await page.evaluate((wsPath) => {
return new Promise((resolve) => {
if (typeof window.io !== 'function') {
resolve({ outcome: 'no-io-client' });
return;
}
const s = window.io(window.location.origin, {
path: wsPath,
reconnection: false,
transports: ['websocket', 'polling'],
timeout: 8000,
});
const finish = (outcome, extra) => {
clearTimeout(timer);
try { s.disconnect(); } catch { /* already gone */ }
resolve({ outcome, ...extra });
};
const timer = setTimeout(() => finish('no-event-within-deadline'), 9000);
s.on('connect', () => finish('connected', { id: s.id }));
s.on('connect_error', (err) => finish('connect_error', { message: err && err.message }));
});
}, '/ws/socket.io');
if (result.outcome === 'connect_error') {
pass(`Unauthenticated Socket.IO: handshake correctly rejected (${result.message})`);
} else if (result.outcome === 'connected') {
fail(
'GENUINE DEFECT: unauthenticated client completed a Socket.IO handshake ' +
`against /ws/socket.io (sid=${result.id}). socketio_asgi.py's connect() ` +
'handler is supposed to return False when request.session has no username.'
);
await captureOnFailure(page, SCREENSHOT_PREFIX, 'unauthenticated_socket', false);
} else {
fail(`Unauthenticated Socket.IO: unexpected outcome "${result.outcome}"`);
await captureOnFailure(page, SCREENSHOT_PREFIX, 'unauthenticated_socket', false);
}
} finally {
await incognitoContext.close();
}
}
// ---------------------------------------------------------------------------
// Test 2: SSE endpoint actually streams to the browser
// ---------------------------------------------------------------------------
async function testSseEndpointStreams(page) {
// Light-touch: one screenshot for this whole test, only if any of its
// assertions below failed -- not one capture per assertion.
const failedBefore = testsFailed;
// Make sure we're on an authenticated, same-origin page before issuing
// the fetch (require_auth on the route needs the session cookie).
if (!page.url().startsWith(BASE_URL)) {
await page.goto(`${BASE_URL}/`, { waitUntil: 'domcontentloaded', timeout: 30000 });
}
// The route mutates (it runs the embedding/index pipeline), so it is a
// POST and CSRFMiddleware challenges it like any other mutation. Same
// token source the shipped frontend uses: the csrf-token meta tag
// base.html renders on every page (see getCsrf() in
// test_download_and_csrf_flows_ci.js).
const csrfToken = await page.evaluate(() => {
const m = document.querySelector('meta[name="csrf-token"]');
return m ? m.content : '';
});
if (!csrfToken) {
fail('SSE endpoint: no csrf-token meta tag on the authenticated page');
await captureOnFailure(page, SCREENSHOT_PREFIX, 'sse_endpoint', false);
return;
}
const result = await page.evaluate(async (csrf) => {
const resp = await fetch('/library/api/rag/index-all', {
method: 'POST',
credentials: 'same-origin',
headers: { 'X-CSRFToken': csrf },
});
const headers = {};
for (const [k, v] of resp.headers.entries()) headers[k] = v;
const reader = resp.body.getReader();
let chunkReads = 0;
let buffer = '';
const events = [];
const deadline = Date.now() + 20000;
while (Date.now() < deadline) {
const { done, value } = await reader.read();
if (done) break;
chunkReads++;
// Decode via Response(...).text() rather than TextDecoder — this
// repo's SSE payloads are ASCII-safe JSON with frame boundaries
// on "\n\n", so per-chunk decoding needs no streaming decoder
// state, and Response is already a recognised browser global here.
buffer += await new Response(value).text();
// Parse "data: {...}\n\n" frames exactly like the real consumers
// do (download_manager.html's downloadAllText()).
const parts = buffer.split('\n\n');
buffer = parts.pop(); // last part may be incomplete, keep for next read
for (const part of parts) {
for (const line of part.split('\n')) {
if (line.startsWith('data: ')) {
try {
events.push(JSON.parse(line.slice(6)));
} catch {
events.push({ __unparsed: line });
}
}
}
}
if (events.some((e) => e && e.type === 'complete')) break;
}
return { status: resp.status, ok: resp.ok, headers, chunkReads, events };
}, csrfToken);
// --- Contract assertions ---
if (result.status === 200) {
pass(`SSE endpoint: HTTP ${result.status}`);
} else {
fail(`SSE endpoint: expected HTTP 200, got ${result.status}`);
}
const contentType = result.headers['content-type'] || '';
if (contentType.startsWith('text/event-stream')) {
pass(`SSE endpoint: Content-Type is "${contentType}"`);
} else {
fail(`SSE endpoint: expected Content-Type text/event-stream, got "${contentType}"`);
}
const cacheControl = result.headers['cache-control'] || '';
if (cacheControl.includes('no-cache')) {
pass(`SSE endpoint: Cache-Control carries no-cache ("${cacheControl}")`);
} else {
fail(`SSE endpoint: Cache-Control missing no-cache directive: "${cacheControl}"`);
}
const accelBuffering = result.headers['x-accel-buffering'];
if (accelBuffering === 'no') {
pass('SSE endpoint: X-Accel-Buffering: no (proxy buffering disabled)');
} else {
fail(`SSE endpoint: expected X-Accel-Buffering: no, got "${accelBuffering}"`);
}
// The real proof of streaming, not just a well-formed single response:
// the body must have arrived across more than one ReadableStream read.
if (result.chunkReads > 1) {
pass(`SSE endpoint: body arrived across ${result.chunkReads} separate stream reads (incremental delivery, not buffered)`);
} else {
fail(`SSE endpoint: body arrived in a single read (chunkReads=${result.chunkReads}) — looks buffered, not streamed`);
}
if (result.events.length >= 1 && result.events.some((e) => e && typeof e.type === 'string')) {
const types = result.events.map((e) => e.type).join(' -> ');
pass(`SSE endpoint: browser parsed ${result.events.length} real event(s) from the stream (${types})`);
} else {
fail(`SSE endpoint: no parseable SSE "data:" events received: ${JSON.stringify(result.events)}`);
}
await captureOnFailure(page, SCREENSHOT_PREFIX, 'sse_endpoint', testsFailed === failedBefore);
}
// ---------------------------------------------------------------------------
// Test 3: no console errors on the pages that depend on these subsystems
// ---------------------------------------------------------------------------
async function testNoConsoleErrorsOnPage(browser, path) {
const page = await browser.newPage();
const errors = [];
page.on('console', (msg) => {
if (isRealConsoleError(msg)) errors.push(msg.text());
});
page.on('pageerror', (err) => errors.push(`pageerror: ${err.message}`));
try {
await page.goto(`${BASE_URL}${path}`, { waitUntil: 'networkidle2', timeout: 30000 });
// Let deferred scripts (socket.js autoInitSocket has a 100ms delay)
// and any async init settle before judging — condition-based, not a
// fixed guess: wait for layout to stop moving (see waitForStable in
// auth_helper.js) rather than a bare sleep.
await AuthHelper.waitForStable(page, 'body', { timeout: 5000, idleMs: 300 }).catch(() => {});
if (errors.length === 0) {
pass(`No console errors: ${path}`);
} else {
fail(`Console errors on ${path}: ${errors.join(' | ')}`);
await captureOnFailure(page, SCREENSHOT_PREFIX, `console_errors_${path.replace(/\//g, '_')}`, false);
}
} finally {
await page.close();
}
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
console.log(`🧪 Streaming/Realtime UI tests (CI mode: ${isCI}) against ${BASE_URL}`);
const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
const page = await browser.newPage();
if (isCI) {
page.setDefaultTimeout(60000);
page.setDefaultNavigationTimeout(60000);
}
try {
const authHelper = new AuthHelper(page, BASE_URL);
await authHelper.ensureAuthenticatedWithTimeout();
const authedPage = authHelper.getPage();
section('Socket.IO — real ASGI handshake at /ws/socket.io');
await testAuthenticatedSocketConnects(authedPage);
await testUnauthenticatedSocketRejected(browser);
section('SSE — POST /library/api/rag/index-all streams to the browser');
await testSseEndpointStreams(authedPage);
section('No console errors on realtime-dependent authenticated pages');
for (const path of ['/', '/history', '/library/', '/chat/']) {
await testNoConsoleErrorsOnPage(browser, path);
}
} catch (error) {
fail(`Fatal test-harness error: ${error.message}`);
console.error(error.stack);
} finally {
await browser.close();
}
console.log('\n' + '='.repeat(70));
console.log(`📊 Streaming/Realtime 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);
});