~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
/**
* Error Handling UI Tests
*
* Tests for 404, 401, 429 error handling, and form validation errors.
*
* Run: node test_error_handling_ci.js
*/
const { setupTest, teardownTest, TestResults, log, delay, navigateTo, withTimeout, withFreshBrowserContext } = require('./test_lib');
/**
* Navigate with a single retry on timeout.
*
* Used for deliberately-bad URLs (404 probes) where a slow CI server can
* push the first goto past its 60s timeout. Without the retry, a single
* stuck navigation consumes the suite's wall-clock budget and triggers the
* SIGTERM that detaches the page for every subsequent sub-test.
*/
async function navigateToWithRetry(page, url) {
try {
return await navigateTo(page, url);
} catch (firstError) {
await delay(2000);
return await navigateTo(page, url);
}
}
// ============================================================================
// 404 Error Handling Tests
// ============================================================================
const Error404Tests = {
async nonExistentPageShows404(page, baseUrl) {
const response = await navigateToWithRetry(page, `${baseUrl}/nonexistent-page-12345`);
const result = await page.evaluate(() => {
const bodyText = document.body.textContent?.toLowerCase() || '';
return {
has404Text: bodyText.includes('404') || bodyText.includes('not found'),
hasErrorPage: !!document.querySelector('.error-page, .not-found, [class*="404"]'),
hasHomeLink: !!document.querySelector('a[href="/"], a[href*="home"]'),
pageTitle: document.title
};
});
const statusCode = response?.status();
const passed = statusCode === 404 || result.has404Text || result.hasErrorPage;
return {
passed,
message: passed
? `404 handled (status: ${statusCode}, has404Text: ${result.has404Text})`
: `Unexpected response for non-existent page (status: ${statusCode})`
};
},
async invalidResearchIdHandled(page, baseUrl) {
// The /results/<id> route always renders pages/results.html (status 200);
// not-found handling is client-side: results.js fetches /api/report/<id>,
// gets a 404, and calls showError() which injects an
// `.alert-danger` (with "Error loading research results: HTTP error 404")
// into #results-content. Assert the SPECIFIC outcome:
// 1. The results page actually rendered (#research-results container) —
// proves we did not land on login/a generic error page.
// 2. A targeted error element appeared inside #results-content
// (not the bare substring "error" anywhere in page chrome).
await navigateToWithRetry(page, `${baseUrl}/results/invalid-research-id-12345`);
// Confirm the results page chrome rendered before waiting on the async error.
await page.waitForSelector('#research-results #results-content', { timeout: 15000 });
// results.js renders the error asynchronously after the failed /api/report
// fetch. Wait for the targeted alert rather than reading body text once.
try {
await page.waitForFunction(() => {
const alert = document.querySelector('#results-content .alert-danger');
return !!alert && (alert.textContent || '').trim().length > 0;
}, { timeout: 15000 });
} catch {
// Targeted alert never appeared within 15s. Don't fail here — the DOM
// re-read below makes the final pass/fail decision — but log a hint so a
// slow render is distinguishable from "no error element rendered" during
// triage.
log('invalidResearchIdHandled: timed out waiting for #results-content .alert-danger (slow render?)');
}
const result = await page.evaluate(() => {
const onResultsPage = !!document.querySelector('#research-results');
const alert = document.querySelector('#results-content .alert-danger');
return {
onResultsPage,
hasTargetedError: !!alert,
errorText: alert?.textContent?.trim().substring(0, 120) || '',
currentPath: window.location.pathname
};
});
const passed = result.onResultsPage && result.hasTargetedError;
return {
passed,
message: passed
? `Invalid research ID surfaced targeted error on results page (path: ${result.currentPath}, error: "${result.errorText}")`
: `Invalid research ID not handled (onResultsPage: ${result.onResultsPage}, targetedError: ${result.hasTargetedError}, path: ${result.currentPath})`
};
},
async invalidDocumentIdHandled(page, baseUrl) {
// Use fetch instead of page navigation to avoid flaky domcontentloaded timeouts
// The Flask route returns a simple text "Document not found" with status 404
const result = await page.evaluate(async (url) => {
try {
const response = await fetch(`${url}/library/document/invalid-doc-id-12345`);
const text = await response.text();
const bodyText = text.toLowerCase();
return {
status: response.status,
hasErrorText: bodyText.includes('not found') || bodyText.includes('error'),
redirected: response.redirected,
finalUrl: response.url
};
} catch (e) {
return { error: e.message };
}
}, baseUrl);
if (result.error) {
return { passed: null, skipped: true, message: `Fetch failed: ${result.error}` };
}
const passed = result.status === 404 || result.hasErrorText || result.redirected;
return {
passed,
message: passed
? `Invalid document ID handled (status: ${result.status})`
: 'Invalid document ID not handled gracefully'
};
}
};
// ============================================================================
// 401 Authentication Error Tests
// ============================================================================
// Both checks send their requests from a new browser context
// (withFreshBrowserContext) and leave the signed-in page alone. They used to
// clear the signed-in page's cookies over CDP, which was flaky: the server
// re-sends the session cookie on authenticated responses, so a response to a
// request the page had sent before the clear could put the cookie back. In
// CI this showed up as the protected-route check failing right after the
// clear, and as the re-login after it finding no login form, because
// /auth/login redirects a browser that is still signed in.
const Error401Tests = {
async unauthenticatedRedirectsToLogin(page, baseUrl) {
return withFreshBrowserContext(page, async (freshPage) => {
const response = await freshPage.goto(`${baseUrl}/settings/`, { waitUntil: 'domcontentloaded' });
if (!response) {
return { passed: false, message: 'No response for /settings/' };
}
// Judge the server's own answer. page.goto() follows redirects and
// returns the last response, so a server-side redirect to the
// login page shows up as a non-empty redirect chain ending on
// /auth/login. A 200 /settings/ page whose scripts later move the
// browser to /auth/login does not count.
const redirects = response.request().redirectChain()
.map((request) => `${request.response()?.status()} ${new URL(request.url()).pathname}`);
const status = response.status();
const finalPath = new URL(response.url()).pathname;
const redirectedToLogin = redirects.length > 0 && finalPath === '/auth/login';
const passed = status === 401 || status === 403 || redirectedToLogin;
const details = `redirects: [${redirects.join(', ')}], final: ${status} ${finalPath}`;
return {
passed,
message: passed
? `Unauthenticated /settings/ request denied (${details})`
: `Protected route accessible without authentication (${details})`
};
});
},
async apiUnauthorizedReturns401(page, baseUrl) {
return withFreshBrowserContext(page, async (freshPage) => {
// fetch() needs a same-origin document; the login page is public.
await freshPage.goto(`${baseUrl}/auth/login`, { waitUntil: 'domcontentloaded' });
const result = await freshPage.evaluate(async (url) => {
try {
const response = await fetch(`${url}/api/history`);
return {
status: response.status,
redirected: response.redirected,
finalPath: new URL(response.url).pathname
};
} catch (e) {
return { error: e.message };
}
}, baseUrl);
if (result.error) {
return { passed: null, skipped: true, message: `API call failed: ${result.error}` };
}
const passed = result.status === 401 || result.status === 403
|| (result.redirected && result.finalPath === '/auth/login');
return {
passed,
message: passed
? `Unauthenticated API request denied (status: ${result.status}, path: ${result.finalPath})`
: `API accessible without auth (status: ${result.status}, path: ${result.finalPath})`
};
});
}
};
// ============================================================================
// API Error Response Tests
// ============================================================================
const ApiErrorTests = {
async apiMissingParamsReturns400(page, baseUrl) {
await navigateTo(page, `${baseUrl}/`);
const result = await page.evaluate(async (url) => {
try {
// Try to start research without required params
const response = await fetch(`${url}/api/start_research`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}) // Empty body, missing required 'query'
});
const data = await response.json().catch(() => ({}));
return {
status: response.status,
is400: response.status === 400,
hasError: 'error' in data || 'message' in data,
errorMessage: data.error || data.message
};
} catch (e) {
return { error: e.message };
}
}, baseUrl);
if (result.error) {
return { passed: null, skipped: true, message: `API call failed: ${result.error}` };
}
// 400 or 422 are both acceptable for validation errors
const passed = result.status === 400 || result.status === 422 || result.hasError;
return {
passed,
message: passed
? `Missing params returns ${result.status} with error message`
: `Unexpected response for missing params (status: ${result.status})`
};
},
async apiInvalidIdReturns404(page, baseUrl) {
await navigateTo(page, `${baseUrl}/`);
const result = await page.evaluate(async (url) => {
try {
const response = await fetch(`${url}/api/research/nonexistent-id-12345`);
return {
status: response.status,
is404: response.status === 404
};
} catch (e) {
return { error: e.message };
}
}, baseUrl);
if (result.error) {
return { passed: null, skipped: true, message: `API call failed: ${result.error}` };
}
// Only 404 counts. The 401 sub-tests leave this page signed in, so a
// 401 here means the session was lost, not that the ID was handled.
const passed = result.is404;
return {
passed,
message: passed
? `Invalid research ID returns ${result.status}`
: result.status === 401
? 'Invalid ID returns 401 (expected 404): the signed-in session was lost'
: `Invalid ID returns ${result.status} (expected 404)`
};
}
};
// ============================================================================
// Rate Limiting Tests
// ============================================================================
const RateLimitTests = {
async rateLimitingSectionRenders(page, baseUrl) {
// The metrics dashboard (pages/metrics.html) renders a server-side
// "Rate Limiting Analytics" section that is present regardless of LLM or
// collected data — unavailable values render as dashes. This is the page
// that surfaces rate-limit info to the user, so assert the SPECIFIC
// server-rendered container + the stable rate-limiting element IDs,
// instead of skipping on a loose "rate limit" body-text substring (which
// matched the section heading and made the test never really fail).
await navigateTo(page, `${baseUrl}/metrics/`);
// The metrics route requires login: an unauthenticated request 302s to
// /auth/login. The 401 sub-tests earlier in this suite run in their own
// browser context and leave this page signed in, so landing anywhere
// but the metrics page is a real failure, not an environmental skip.
const landingPath = page.url();
if (/\/auth\/login/.test(landingPath) || !(await page.$('#metrics'))) {
return { passed: false, message: `Metrics dashboard not reachable (session lost / redirected): ${landingPath}` };
}
// Page-specific container present — now require the server-rendered
// rate-limiting section. A timeout here is a REAL failure.
await page.waitForSelector('#metrics #rate-limit-success-rate', { timeout: 15000 });
const result = await page.evaluate(() => {
const onMetricsPage = !!document.querySelector('#metrics');
// Stable, server-rendered rate-limiting elements from metrics.html.
const successRate = document.querySelector('#rate-limit-success-rate');
const learnedWait = document.querySelector('#avg-wait-time');
const unsupportedEvents = document.querySelector('#rate-limit-events');
const enginesTracked = document.querySelector('#engines-tracked');
const engineStatusGrid = document.querySelector('#engine-status-grid');
const chart = document.querySelector('#rate-limiting-chart');
// The section heading text confirms this is the rate-limiting block,
// not some other metric card reusing a similar id.
const heading = Array.from(document.querySelectorAll('h2'))
.find(h => /rate limiting analytics/i.test(h.textContent || ''));
return {
onMetricsPage,
hasSuccessRate: !!successRate,
hasLearnedWait: !!learnedWait,
hasUnsupportedEvents: !!unsupportedEvents,
learnedWaitLabel: learnedWait?.closest('.ldr-metric-card')?.textContent || '',
hasEnginesTracked: !!enginesTracked,
hasEngineStatusGrid: !!engineStatusGrid,
hasChart: !!chart,
hasHeading: !!heading,
successRateText: successRate?.textContent?.trim().substring(0, 20) || '',
learnedWaitText: learnedWait?.textContent?.trim().substring(0, 20) || ''
};
});
const passed = result.onMetricsPage
&& result.hasHeading
&& result.hasSuccessRate
&& result.hasLearnedWait
&& !result.hasUnsupportedEvents
&& /Learned Base Wait/i.test(result.learnedWaitLabel)
&& result.hasEnginesTracked
&& result.hasEngineStatusGrid
&& result.hasChart;
return {
passed,
message: passed
? `Rate Limiting Analytics section rendered on metrics dashboard (success-rate value: "${result.successRateText}")`
: `Rate limiting section incomplete (metricsPage: ${result.onMetricsPage}, heading: ${result.hasHeading}, successRate: ${result.hasSuccessRate}, learnedWait: ${result.hasLearnedWait}, unsupportedEvents: ${result.hasUnsupportedEvents}, engines: ${result.hasEnginesTracked}, grid: ${result.hasEngineStatusGrid}, chart: ${result.hasChart})`
};
},
async rateLimitingStatusEndpoint(page, baseUrl) {
await navigateTo(page, `${baseUrl}/`);
const result = await page.evaluate(async (url) => {
try {
const response = await fetch(`${url}/metrics/api/rate-limiting/current`);
if (!response.ok) return { ok: false, status: response.status };
// Guard against HTML responses (e.g. login page redirects)
const contentType = response.headers.get('content-type') || '';
if (!contentType.includes('application/json')) {
return { ok: false, status: response.status, error: `Non-JSON content-type: ${contentType}` };
}
const data = await response.json();
return {
ok: true,
status: response.status,
hasLimits: Object.keys(data).length > 0
};
} catch (e) {
return { ok: false, error: e.message };
}
}, baseUrl);
if (!result.ok && result.status === 404) {
return { passed: null, skipped: true, message: 'Rate limiting status endpoint not found' };
}
// A 401/403 or a non-JSON answer (such as a login page) means this
// signed-in page lost its session. The 401 sub-tests leave it signed
// in, so that is a failure here, not a skip.
return {
passed: result.ok,
message: result.ok
? 'Rate limiting status endpoint responds'
: `Rate limiting endpoint failed: ${result.error || 'status ' + result.status}`
};
}
};
// ============================================================================
// Form Validation Error Tests
// ============================================================================
const FormValidationTests = {
async emptyQueryShowsError(page, baseUrl) {
await navigateTo(page, `${baseUrl}/`);
// Submit the research form with an empty query and wait for an error
// about the query to become visible. research.js's submit handler runs
// FormValidator, which sets aria-invalid on #query and writes the
// message into #query-error. Without FormValidator it shows the
// message as an alert in #research-alert, and if the request reaches
// the server, its "Query is required" answer is shown the same way.
//
// Only a visible, non-empty message counts, and it must appear after
// the click. The page always contains hidden, empty error containers
// (#research-error-alert has class ldr-settings-error-container), so
// checking that an element matching [class*="error"] exists passed
// whatever the form did.
const result = await page.evaluate(() => {
const form = document.querySelector('#research-form');
const queryInput = form?.querySelector('#query');
const submitBtn = form?.querySelector('#start-research-btn');
if (!form || !queryInput || !submitBtn) {
return { hasForm: false, path: window.location.pathname };
}
const shownText = (el) => {
if (!el || el.getClientRects().length === 0) return '';
if (getComputedStyle(el).visibility === 'hidden') return '';
return (el.textContent || '').trim();
};
const queryErrorText = () => {
if (queryInput.getAttribute('aria-invalid') === 'true') {
const text = shownText(document.getElementById(`${queryInput.id}-error`));
if (text) return text;
}
for (const id of ['research-alert', 'research-error-alert']) {
const text = shownText(document.getElementById(id)?.querySelector('.alert'));
if (/query/i.test(text)) return text;
}
return '';
};
const textBeforeSubmit = queryErrorText();
queryInput.value = '';
submitBtn.click();
return new Promise(resolve => {
let attempts = 0;
const check = () => {
const errorText = queryErrorText();
if (errorText || ++attempts >= 15) {
resolve({ hasForm: true, textBeforeSubmit, errorText });
} else {
setTimeout(check, 200);
}
};
setTimeout(check, 200);
});
});
// The 401 sub-tests leave this page signed in, so "/" must render the
// research form. A missing form is a failure, not a skip.
if (!result.hasForm) {
return { passed: false, message: `No research form found (path: ${result.path})` };
}
if (result.textBeforeSubmit) {
return { passed: false, message: `A query error was already shown before submitting: "${result.textBeforeSubmit}"` };
}
const passed = !!result.errorText;
return {
passed,
message: passed
? `Empty query validation works (error: "${result.errorText}")`
: 'Empty query did not show a visible error about the query'
};
},
async invalidSettingsShowsError(page, baseUrl) {
await navigateTo(page, `${baseUrl}/settings/`);
const result = await page.evaluate(() => {
// Look for any numeric input and try to set invalid value
const numericInput = document.querySelector(
'input[type="number"], ' +
'input[name*="temperature"], ' +
'input[name*="iterations"]'
);
if (!numericInput) return { hasInput: false };
// Count only what changes after the bad value: a visible,
// non-empty error message that was not shown before, or this
// input becoming invalid. Settings pages ship empty error
// containers (embedding_settings.html always renders a
// .ldr-field-error), which a bare selector match finds whether
// or not anything was validated.
const shownErrors = () => Array.from(
document.querySelectorAll('.error, .invalid-feedback, .form-error, [class*="error"]')
)
.filter((el) => el.getClientRects().length > 0 && getComputedStyle(el).visibility !== 'hidden')
.map((el) => (el.textContent || '').trim())
.filter(Boolean);
const inputFlagged = () =>
numericInput.matches(':invalid') || numericInput.getAttribute('aria-invalid') === 'true';
const errorsBefore = new Set(shownErrors());
const flaggedBefore = inputFlagged();
// Set invalid value
numericInput.value = '-999';
numericInput.dispatchEvent(new Event('change', { bubbles: true }));
numericInput.dispatchEvent(new Event('input', { bubbles: true }));
return new Promise(resolve => {
let attempts = 0;
const check = () => {
const newErrors = shownErrors().filter((text) => !errorsBefore.has(text));
const inputMarkedInvalid = !flaggedBefore && inputFlagged();
if (newErrors.length > 0 || inputMarkedInvalid || ++attempts >= 15) {
resolve({
hasInput: true,
newErrors,
inputMarkedInvalid
});
} else {
setTimeout(check, 200);
}
};
setTimeout(check, 200);
});
});
if (!result.hasInput) {
return { passed: null, skipped: true, message: 'No numeric input found to test validation' };
}
const passed = result.newErrors.length > 0 || result.inputMarkedInvalid;
return {
passed,
message: passed
? `Invalid settings value shows validation error (${result.inputMarkedInvalid ? 'input marked invalid' : `"${result.newErrors[0]}"`})`
: 'Invalid settings value did not trigger validation'
};
},
async requiredFieldsMarked(page, baseUrl) {
await navigateTo(page, `${baseUrl}/`);
const result = await page.evaluate(() => {
const requiredInputs = document.querySelectorAll('[required], .required');
const requiredLabels = document.querySelectorAll('label.required, label:has(+ [required])');
// Note: :contains() is not valid CSS - check for asterisks differently
const asteriskIndicators = document.querySelectorAll('.required-indicator, .asterisk');
// Check for labels that contain asterisks in their text content
const labelsWithAsterisk = Array.from(document.querySelectorAll('label')).filter(label => label.textContent.includes('*'));
return {
requiredInputCount: requiredInputs.length,
requiredLabelCount: requiredLabels.length,
hasAsterisks: asteriskIndicators.length > 0 ||
labelsWithAsterisk.length > 0 ||
document.body.innerHTML.includes('*</label>') ||
document.body.innerHTML.includes('required')
};
});
if (result.requiredInputCount === 0 && result.requiredLabelCount === 0) {
return { passed: null, skipped: true, message: 'No required field indicators found' };
}
return {
passed: true,
message: `Required fields marked (${result.requiredInputCount} inputs, ${result.requiredLabelCount} labels)`
};
}
};
// ============================================================================
// Main Test Runner
// ============================================================================
async function main() {
log.section('Error Handling Tests');
const ctx = await setupTest({ authenticate: true });
const results = new TestResults('Error Handling Tests');
const { page } = ctx;
const { baseUrl } = ctx.config;
// Per-sub-test timeout + about:blank recovery on failure.
//
// The suite has a wall-clock budget enforced externally (300s in CI). If
// any single sub-test hangs past that budget the runner SIGTERMs the
// process and every remaining sub-test cascades into "detached frame"
// errors. A per-sub-test timeout caps each call well below the suite
// budget; resetting to about:blank on failure prevents a half-loaded
// page from breaking the next test.
const subTestTimeout = ctx.config.isCI ? 60000 : 30000;
async function run(category, name, testFn) {
try {
const result = await withTimeout(testFn(), subTestTimeout, `${category}/${name}`);
if (result && result.skipped) {
results.skip(category, name, result.message);
} else {
results.add(category, name, result.passed, result.message || '');
}
} catch (error) {
results.add(category, name, false, `Error: ${error.message}`);
try {
await page.goto('about:blank', { timeout: 5000 });
} catch {
// Best-effort recovery — don't mask the original failure.
}
}
}
try {
// 404 Error Tests
log.section('404 Errors');
await run('404', 'Non-existent Page Shows 404', () => Error404Tests.nonExistentPageShows404(page, baseUrl));
await run('404', 'Invalid Research ID Handled', () => Error404Tests.invalidResearchIdHandled(page, baseUrl));
await run('404', 'Invalid Document ID Handled', () => Error404Tests.invalidDocumentIdHandled(page, baseUrl));
// 401 Authentication Tests. Both run in their own browser context, so
// the signed-in page used by the rest of the suite keeps its session
// and needs no re-login.
log.section('401 Authentication');
await run('401', 'Unauthenticated Redirects To Login', () => Error401Tests.unauthenticatedRedirectsToLogin(page, baseUrl));
await run('401', 'API Unauthorized Returns 401', () => Error401Tests.apiUnauthorizedReturns401(page, baseUrl));
// API Error Tests (require authenticated session)
log.section('API Errors');
await run('API', 'API Missing Params Returns 400', () => ApiErrorTests.apiMissingParamsReturns400(page, baseUrl));
await run('API', 'API Invalid ID Returns 404', () => ApiErrorTests.apiInvalidIdReturns404(page, baseUrl));
// Rate Limiting Tests (require authenticated session)
log.section('Rate Limiting');
await run('RateLimit', 'Rate Limiting Section Renders', () => RateLimitTests.rateLimitingSectionRenders(page, baseUrl));
await run('RateLimit', 'Rate Limiting Status Endpoint', () => RateLimitTests.rateLimitingStatusEndpoint(page, baseUrl));
// Form Validation Tests
log.section('Form Validation');
await run('Validation', 'Empty Query Shows Error', () => FormValidationTests.emptyQueryShowsError(page, baseUrl));
await run('Validation', 'Invalid Settings Shows Error', () => FormValidationTests.invalidSettingsShowsError(page, baseUrl));
await run('Validation', 'Required Fields Marked', () => FormValidationTests.requiredFieldsMarked(page, baseUrl));
} catch (error) {
log.error(`Fatal error: ${error.message}`);
console.error(error.stack);
} finally {
results.print();
results.save();
await teardownTest(ctx);
process.exit(results.exitCode());
}
}
// Run if executed directly
if (require.main === module) {
main().catch(error => {
console.error('Test runner failed:', error);
process.exit(1);
});
}
module.exports = { Error404Tests, Error401Tests, ApiErrorTests, RateLimitTests, FormValidationTests };