~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
/**
* E2E Tests for the chat page's `?q=` pre-fill and main-page message POST
*
* The session cookie is SameSite=Lax, so a link from another site opens
* the chat page signed in, and that link chooses the URL. chat.js therefore
* never sends a URL parameter by itself: `?q=` only pre-fills the composer
* of a chat without messages, and the user has to press Send. The server
* also drops `q` from links the browser marks as cross-site; page.goto() is
* a browser-initiated navigation, so it keeps `q` here. Chat mode on the
* main page (research.js) creates the session and posts its first message
* before opening the clean chat URL. The chat page hydrates saved messages;
* it never restores a startup query from browser storage.
*
* Contract verified here:
* - Main page chat mode: one submit opens /chat/<session_id> (no `?q=`)
* and sends the query exactly once, also across a reload
* - Visiting /chat/<session_id>?q=<query> for a session without messages
* sends nothing; it pre-fills the composer without focusing it, cleans
* the URL, and one press of Send posts the query
* - Visiting /chat/?q=<query> opens a new draft, never the most recent
* conversation
* - Empty ?q= is a no-op
* - A legacy query left in the same tab by another account is discarded
* without being read, displayed or sent
*
* No LLM required — we stop at the user-side bubble.
*
* Prerequisites: Web server on http://127.0.0.1:5000 (override BASE_URL).
*/
const puppeteer = require('puppeteer');
const AuthHelper = require('../auth_helper');
const { getPuppeteerLaunchOptions } = require('../puppeteer_config');
const fs = require('fs');
const path = require('path');
const BASE_URL = process.env.BASE_URL || 'http://127.0.0.1:5000';
const isCI = !!process.env.CI;
const TIMEOUTS = {
navigation: isCI ? 60000 : 30000,
selector: isCI ? 30000 : 10000,
element: isCI ? 10000 : 5000,
};
const SCREENSHOTS_DIR = path.join(__dirname, '..', 'screenshots');
async function snap(page, name) {
if (!fs.existsSync(SCREENSHOTS_DIR)) {
fs.mkdirSync(SCREENSHOTS_DIR, { recursive: true });
}
const file = path.join(SCREENSHOTS_DIR, `chat_qparam_${name}_${Date.now()}.png`);
try {
await page.screenshot({ path: file, fullPage: true });
} catch (_) {}
}
// Create a chat session via API and return its id, the way research.js
// creates one before it opens the chat page.
async function createSessionViaApi(page) {
await page.goto(`${BASE_URL}/chat/`, {
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForSelector('.ldr-chat-container', { timeout: TIMEOUTS.selector });
const token = await page.evaluate(() => {
const m = document.querySelector('meta[name="csrf-token"]');
return m ? m.content : '';
});
const result = await page.evaluate(
async (csrf) => {
const r = await fetch('/api/chat/sessions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrf,
},
body: JSON.stringify({ initial_query: 'qparam-test session' }),
});
const data = await r.json().catch(() => ({}));
return { ok: r.ok, status: r.status, data };
},
token
);
if (!result.ok || !result.data.success || !result.data.session_id) {
throw new Error(
`Session creation failed: status=${result.status} body=${JSON.stringify(result.data)}`
);
}
return result.data.session_id;
}
async function run() {
console.log(`Running chat ?q= URL param tests (CI mode: ${isCI})`);
const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
if (isCI) {
page.setDefaultTimeout(60000);
page.setDefaultNavigationTimeout(60000);
}
page.on('pageerror', (e) => console.log('PAGE ERROR:', e.message));
page.on('console', (m) => {
if (m.type() === 'error') console.log('BROWSER ERROR:', m.text());
});
const auth = new AuthHelper(page, BASE_URL);
let passed = 0;
let failed = 0;
try {
await auth.ensureAuthenticated();
// Every chat message POST the page makes, across navigations.
const messagePosts = [];
const messagePostPages = [];
page.on('request', (request) => {
if (request.method() === 'POST'
&& /\/api\/chat\/sessions\/[^/]+\/messages$/.test(new URL(request.url()).pathname)) {
messagePosts.push(request.url());
messagePostPages.push(page.url());
}
});
// Test 1: the main page accepts the message before opening chat.
console.log('Test 1: main-page chat mode sends its query once, without ?q=');
try {
const probe = `direct-post probe ${Date.now()}`;
const postsBefore = messagePosts.length;
await page.goto(`${BASE_URL}/`, {
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForSelector('#query', { timeout: TIMEOUTS.selector });
await page.evaluate((query) => {
document.querySelector('.ldr-mode-option[data-mode="chat"]').click();
document.getElementById('query').value = query;
}, probe);
const mode = await page.$eval(
'input[name="research_mode"]:checked',
(el) => el.value
);
if (mode !== 'chat') throw new Error(`Chat mode not selected: ${mode}`);
await Promise.all([
page.waitForNavigation({
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
}),
page.evaluate(() => document.getElementById('start-research-btn').click()),
]);
const landed = new URL(page.url());
if (!/^\/chat\/[^/]+$/.test(landed.pathname) || landed.search || landed.hash) {
throw new Error(`Expected /chat/<session_id> without a query, got ${page.url()}`);
}
await page.waitForFunction(
(needle) => {
const nodes = document.querySelectorAll(
'.ldr-chat-message-user .ldr-chat-message-text'
);
return Array.from(nodes).some((n) =>
(n.textContent || '').includes(needle)
);
},
{ timeout: TIMEOUTS.selector },
probe
);
await page
.waitForNetworkIdle({ idleTime: 500, timeout: TIMEOUTS.element })
.catch(() => {});
if (messagePosts.length - postsBefore !== 1) {
throw new Error(
`Expected one message POST, got ${messagePosts.length - postsBefore}`
);
}
if (new URL(messagePostPages[postsBefore]).pathname !== '/') {
throw new Error('The first message was sent after leaving the main page');
}
const leftover = await page.evaluate(
() => sessionStorage.getItem('pendingChatQuery')
);
if (leftover !== null) {
throw new Error('A startup query remained in browser storage');
}
// A reload must not send the query again.
await page.reload({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation });
await page.waitForFunction(
() => document.getElementById('chat-input')?.dataset.initComplete === 'true',
{ timeout: TIMEOUTS.selector }
);
await page
.waitForNetworkIdle({ idleTime: 500, timeout: TIMEOUTS.element })
.catch(() => {});
if (messagePosts.length - postsBefore !== 1) {
throw new Error('Reloading the chat page sent the query again');
}
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'direct_post');
failed++;
}
// Test 2: ?q= in the URL only pre-fills the composer
console.log('Test 2: /chat/<session>?q=<query> pre-fills without sending');
try {
const sessionId = await createSessionViaApi(page);
const probe = `qparam probe ${Date.now()}`;
const target = `${BASE_URL}/chat/${sessionId}?q=${encodeURIComponent(probe)}`;
const postsBefore = messagePosts.length;
await page.goto(target, {
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForFunction(
() => document.getElementById('chat-input')?.dataset.initComplete === 'true',
{ timeout: TIMEOUTS.selector }
);
await page
.waitForNetworkIdle({ idleTime: 500, timeout: TIMEOUTS.element })
.catch(() => {});
const state = await page.evaluate(() => ({
composer: document.getElementById('chat-input').value,
focused: document.activeElement === document.getElementById('chat-input'),
sendDisabled: document.getElementById('send-btn').disabled,
userBubbles: document.querySelectorAll('.ldr-chat-message-user').length,
search: window.location.search,
}));
if (messagePosts.length !== postsBefore) {
throw new Error('Opening a ?q= link sent a chat message');
}
if (state.userBubbles !== 0) {
throw new Error(`Expected 0 user bubbles, got ${state.userBubbles}`);
}
if (state.composer !== probe) {
throw new Error(`Composer not pre-filled: ${JSON.stringify(state.composer)}`);
}
// Text from a URL must not sit in a focused box one Enter away.
if (state.focused) throw new Error('The pre-filled composer took focus');
if (state.sendDisabled) throw new Error('Send stayed disabled after the pre-fill');
if (state.search.includes('q=')) throw new Error(`URL still has q=: ${page.url()}`);
// One press of Send posts the pre-filled query.
await Promise.all([
page.waitForRequest(
(request) => request.method() === 'POST'
&& new URL(request.url()).pathname
=== `/api/chat/sessions/${sessionId}/messages`,
{ timeout: TIMEOUTS.selector }
),
page.click('#send-btn'),
]);
await page.waitForFunction(
(needle) => {
const nodes = document.querySelectorAll(
'.ldr-chat-message-user .ldr-chat-message-text'
);
return Array.from(nodes).some((n) =>
(n.textContent || '').includes(needle)
);
},
{ timeout: TIMEOUTS.selector },
probe
);
if (messagePosts.length - postsBefore !== 1) {
throw new Error(
`Expected one message POST after Send, got ${messagePosts.length - postsBefore}`
);
}
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'prefill');
failed++;
}
// Test 2b: a bare /chat/?q= opens a new draft. Tests 1 and 2 left this
// user with chats, and the most recent one must not receive the text.
console.log('Test 2b: /chat/?q=<query> opens a new draft, not the latest chat');
try {
const probe = `new draft probe ${Date.now()}`;
const postsBefore = messagePosts.length;
await page.goto(`${BASE_URL}/chat/?q=${encodeURIComponent(probe)}`, {
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForFunction(
() => document.getElementById('chat-input')?.dataset.initComplete === 'true',
{ timeout: TIMEOUTS.selector }
);
await page
.waitForNetworkIdle({ idleTime: 500, timeout: TIMEOUTS.element })
.catch(() => {});
const state = await page.evaluate(() => ({
composer: document.getElementById('chat-input').value,
focused: document.activeElement === document.getElementById('chat-input'),
bubbles: document.querySelectorAll('.ldr-chat-message').length,
path: window.location.pathname + window.location.search,
}));
if (messagePosts.length !== postsBefore) {
throw new Error('Opening a ?q= link sent a chat message');
}
if (state.path !== '/chat/') {
throw new Error(`Expected a new draft at /chat/, got ${state.path}`);
}
if (state.bubbles !== 0) {
throw new Error(`A restored conversation is showing (${state.bubbles} messages)`);
}
if (state.composer !== probe) {
throw new Error(`Composer not pre-filled: ${JSON.stringify(state.composer)}`);
}
if (state.focused) throw new Error('The pre-filled composer took focus');
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'new_draft');
failed++;
}
// Test 3: Empty ?q= changes nothing: chat.js ignores a falsy query.
console.log('Test 3: Empty ?q= leaves chat state unchanged');
try {
const sessionId = await createSessionViaApi(page);
await page.goto(`${BASE_URL}/chat/${sessionId}?q=`, {
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForSelector('.ldr-chat-container', {
timeout: TIMEOUTS.selector,
});
// Wait for the network to settle. The chat init runs on
// load and may fetch session/messages; once those quiet
// down, any spurious POST that an empty ?q= MIGHT have
// triggered would also have fired. Deterministic substitute
// for a wall-clock setTimeout.
await page
.waitForNetworkIdle({ idleTime: 500, timeout: TIMEOUTS.element })
.catch(() => {});
// A fresh session with empty ?q= should have zero user bubbles
// (the session was just created via API and has no messages).
const userBubbleCount = await page.$$eval(
'.ldr-chat-message-user .ldr-chat-message-text',
(els) => els.length
);
if (userBubbleCount !== 0) {
throw new Error(`Expected 0 user bubbles, got ${userBubbleCount}`);
}
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'empty_q');
failed++;
}
console.log('Test 4: switching accounts discards a legacy startup query without reading it');
try {
// Register B first, then A, so the boundary under test is an
// actual A logout followed by B login in this very same tab.
const suffix = `${Date.now()}_${process.pid}`;
const userA = `chat_legacy_a_${suffix}`;
const userB = `chat_legacy_b_${suffix}`;
const password = 'T3st!ChatPrivacy#2026';
await auth.logout();
await auth.register(userB, password);
await auth.logout();
await auth.register(userA, password);
if (auth.getPage() !== page) {
throw new Error('Authentication replaced the tab used for the storage regression');
}
const owner = await page.$eval('.ldr-user-info', el => el.textContent.trim());
if (owner !== userA) throw new Error('The source account was not authenticated');
const sessionId = await createSessionViaApi(page);
const ownerRead = await page.evaluate(async (id) => {
const response = await fetch(`/api/chat/sessions/${encodeURIComponent(id)}`);
const data = await response.json();
return { status: response.status, id: data.session?.id };
}, sessionId);
if (ownerRead.status !== 200 || ownerRead.id !== sessionId) {
throw new Error('The source account could not read its own real chat session');
}
// Model an interrupted navigation from an older revision. This
// synthetic marker must survive auth, but must never become B's
// draft even though B cannot hydrate A's session.
const privateQuery = `legacy account A query ${suffix}`;
const postsBefore = messagePosts.length;
await page.evaluate(({ id, query }) => {
sessionStorage.setItem('pendingChatQuery', JSON.stringify({ sessionId: id, query }));
}, { id: sessionId, query: privateQuery });
await auth.logout();
await auth.login(userB, password);
if (auth.getPage() !== page) throw new Error('The account switch did not keep the same tab');
const replacement = await page.$eval('.ldr-user-info', el => el.textContent.trim());
if (replacement !== userB) throw new Error('The replacement account was not authenticated');
const foreignRead = await page.evaluate(async (id) => {
const response = await fetch(`/api/chat/sessions/${encodeURIComponent(id)}`);
const data = await response.json();
return { status: response.status, success: data.success };
}, sessionId);
if (foreignRead.status !== 404 || foreignRead.success !== false) {
throw new Error('The replacement account could access the source account chat');
}
const legacyStillPresent = await page.evaluate(
() => Object.keys(sessionStorage).includes('pendingChatQuery')
);
if (!legacyStillPresent) {
throw new Error('The legacy-state premise disappeared before chat initialization');
}
// Observe actual production access without substituting its
// return value: an old get-and-restore implementation still
// reads the real marker and fails the privacy assertions below.
await page.evaluateOnNewDocument(() => {
window.__chatLegacyStorageAccess = { reads: 0, removals: 0 };
const read = globalThis.Storage.prototype.getItem;
const remove = globalThis.Storage.prototype.removeItem;
globalThis.Storage.prototype.getItem = function(key) {
if (this === window.sessionStorage && key === 'pendingChatQuery') {
window.__chatLegacyStorageAccess.reads++;
}
return read.call(this, key);
};
globalThis.Storage.prototype.removeItem = function(key) {
if (this === window.sessionStorage && key === 'pendingChatQuery') {
window.__chatLegacyStorageAccess.removals++;
}
return remove.call(this, key);
};
});
await page.goto(`${BASE_URL}/chat/${encodeURIComponent(sessionId)}`, {
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForFunction(
() => document.getElementById('chat-input')?.dataset.initComplete === 'true',
{ timeout: TIMEOUTS.selector }
);
const state = await page.evaluate((marker) => ({
composer: document.getElementById('chat-input').value,
markerVisible: document.body.textContent.includes(marker)
|| document.title.includes(marker),
hasLegacyKey: Object.keys(sessionStorage).includes('pendingChatQuery'),
reads: window.__chatLegacyStorageAccess.reads,
removals: window.__chatLegacyStorageAccess.removals,
path: window.location.pathname + window.location.search + window.location.hash,
}), privateQuery);
if (state.composer !== '' || state.markerVisible) {
throw new Error('The source account query was displayed to the replacement account');
}
if (messagePosts.length !== postsBefore) {
throw new Error('Switching accounts or opening the unavailable chat sent a message');
}
if (state.hasLegacyKey || state.reads !== 0 || state.removals < 1) {
throw new Error('The legacy query was not removed without reading its value');
}
if (state.path !== `/chat/${encodeURIComponent(sessionId)}`) {
throw new Error('The source account query affected the replacement account URL');
}
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'account_switch');
failed++;
}
} catch (e) {
console.log(`Test suite error: ${e.message}`);
failed++;
} finally {
await browser.close();
}
console.log('-'.repeat(50));
console.log(`Chat ?q= URL Tests — passed: ${passed}, failed: ${failed}`);
console.log('-'.repeat(50));
if (failed > 0) process.exit(1);
}
run().catch((e) => {
console.error('Test runner error:', e);
process.exit(1);
});