~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
/**
* Shipped API/URL/auth wrappers and complete history/details/library consumers.
* This is a controlled browser-runtime contract, not a live application test.
* Private callbacks are reached through the fixture's fixed lexical probes;
* no production function is copied and transport-only unit doubles are absent.
*/
import {
AUTH_CONTEXT, CHAT_ID, COLLECTION_ID, CSRF_TOKEN, RESEARCH_ID,
createRuntime, response, settle,
} from '../helpers/history-library-auth-runtime.js';
let runtime;
afterEach(() => {
runtime?.dispose(); runtime = null;
vi.restoreAllMocks();
});
const HISTORY_COLLECTION = '/library/api/research-history/collection';
const CONVERT = '/library/api/research-history/convert-all';
const INDEX_START = `/library/api/collections/${COLLECTION_ID}/index/start`;
const INDEX_STATUS = `/library/api/collections/${COLLECTION_ID}/index/status`;
const SAVE = `/library/api/research/${RESEARCH_ID}/add-to-collection`;
const EXPORT = `/api/research/${RESEARCH_ID}/logs/export`;
const CONFIRMATION = '/settings/api/research_library.confirm_deletions';
const guards = { recovery: 'function', classify: 'function', redirect: 'function', auth: 'function' };
// index/start's own conflict envelope (rag.py _indexing_conflict): no marker.
const INDEX_CONFLICT = { success: false, error: 'Indexing is already in progress for this collection', task_id: 'synthetic-running-task' };
const SIGN_IN_CHANGED = 'Your sign-in changed. Reload this page before trying again.';
// CSRFMiddleware's replaced-login 409 is { error } plus X-LDR-Auth-Changed.
// The 'marker-only' row adds success:false so only the header distinguishes
// it from INDEX_CONFLICT; the 'middleware' row is the exact server reply.
const signInChangedReplies = [
['middleware', { error: SIGN_IN_CHANGED }],
['marker-only', { success: false, error: SIGN_IN_CHANGED }],
];
// Each row reaches a different executable caller. In particular, the two chat
// DELETE rows share an endpoint but exercise single-delete vs clear-all loops,
// and the two indexing-status rows exercise polling vs startup resumption.
const callers = [
['history', 'history fallback GET', '/history/api', 'history-list-fallback', 'getResearchHistory'],
['history', 'research fallback DELETE', `/api/delete/${RESEARCH_ID}`, 'history-delete-fallback', 'deleteResearch'],
['history', 'history fallback clear', '/api/clear_history', 'history-clear-fallback', 'clearResearchHistory'],
['history', 'chat session enumeration', '/api/chat/sessions?status=all&limit=100&offset=0', 'chat-list'],
['history', 'single chat DELETE', `/api/chat/sessions/${CHAT_ID}`, 'chat-delete'],
['history', 'clear-all chat DELETE loop', `/api/chat/sessions/${CHAT_ID}`, 'chat-clear'],
['details', 'link analytics GET', `/metrics/api/metrics/research/${RESEARCH_ID}/links`, 'links'],
['details', 'primary metrics GET', `/metrics/api/metrics/research/${RESEARCH_ID}`, 'metrics'],
['details', 'timeline metrics GET', `/metrics/api/metrics/research/${RESEARCH_ID}/timeline`, 'timeline'],
['details', 'search metrics GET', `/metrics/api/metrics/research/${RESEARCH_ID}/search`, 'search-metrics'],
['details', 'context overflow GET', `/api/research/${RESEARCH_ID}/context-overflow`, 'overflow'],
['history-search', 'history indexing counts GET', HISTORY_COLLECTION, 'index-count'],
['history-search', 'convert-all POST', CONVERT, 'convert'],
['history-search', 'start indexing POST', INDEX_START, 'index-start'],
['history-search', 'indexing polling GET', INDEX_STATUS, 'index-poll'],
['history-search', 'indexing resume GET', INDEX_STATUS, 'index-resume'],
['history-search', 'semantic search POST', `/library/api/collections/${COLLECTION_ID}/search`, 'history-semantic'],
['logs', 'diagnostic logs GET', `/api/research/${RESEARCH_ID}/logs?limit=500&priority=diagnostic`, 'logs'],
['logs', 'native log export HEAD', EXPORT, 'logs-export'],
['save', 'collection list GET', '/library/api/collections', 'collections'],
['save', 'save research POST', SAVE, 'save'],
['collection', 'remove document fallback DELETE', `/library/api/collection/${COLLECTION_ID}/document/synthetic-document`, 'document-remove'],
['collection', 'delete document fallback DELETE', '/library/api/document/synthetic-document', 'document-delete'],
['pdf', 'upload limits GET', '/api/config/limits', 'pdf-limits'],
['pdf', 'PDF multipart POST', '/api/upload/pdf', 'pdf-upload'],
['confirmation', 'deletion preference GET', CONFIRMATION, 'confirmation'],
];
it.each(callers)('%s: %s observes 401 then redirects without response continuation', async (component, _label, target, kind, missingMethod) => {
let armed = false;
let rejected;
runtime = createRuntime(component, input => {
if (armed && input === target) return response({ detail: 'Authentication required' }, 401)
.then(value => { rejected = value; return value; });
return undefined;
});
await runtime.boot();
expect(runtime.guardState()).toEqual(guards);
if (typeof missingMethod === 'string') {
// The precondition is verified inside the same VM, not by deleting an
// outer copy of its globals. Real CSRF/classifier/redirect code stays.
expect(runtime.dropHighLevel(missingMethod)).toBe('undefined');
expect(runtime.guardState()).toEqual(guards);
}
if (component === 'collection') expect(runtime.sandbox.DeleteManager).toBeUndefined();
if (kind === 'logs') expect(document.getElementById('log-panel-content').dataset.loading).toBeUndefined();
if (component === 'pdf') {
expect(runtime.sandbox.pdfUploadHandler.limitsLoaded).toBe(true);
expect(runtime.sandbox.pdfUploadHandler.getUploadedPDFs()).toEqual([]);
}
runtime.fetchMock.mockClear();
runtime.sandbox.ui.showMessage.mockClear();
const queryBefore = document.getElementById('query').value;
armed = true;
let settled = false;
const operation = runtime.trigger(kind);
expect(operation?.then).toBeTypeOf('function');
operation.then(() => { settled = true; }, () => { settled = true; });
await settle();
// The receipt assertion deliberately precedes the navigation assertion so
// a broken bootstrap or unexercised branch cannot look like a valid mutant.
expect(rejected?.status).toBe(401);
const next = runtime.original.pathname + runtime.original.search + runtime.original.hash;
expect(runtime.navigations).toEqual([`/auth/login?next=${encodeURIComponent(next)}`]);
expect(settled).toBe(false);
expect(runtime.fetchMock.mock.calls.filter(([input]) => input === target)).toHaveLength(1);
expect(runtime.fetchMock.mock.calls.at(-1)[0]).toBe(target);
expect(rejected.json).not.toHaveBeenCalled();
expect(rejected.blob).not.toHaveBeenCalled();
expect(runtime.anchorClicks).toEqual([]);
expect(runtime.action).not.toHaveBeenCalled();
expect(runtime.sandbox.URL.createObjectURL).not.toHaveBeenCalled();
expect(document.getElementById('query').value).toBe(queryBefore);
expect(runtime.sandbox.ui.showMessage).not.toHaveBeenCalled();
if (kind === 'convert' || kind === 'index-start') expect(runtime.intervals.size).toBe(0);
if (kind === 'pdf-upload') expect(runtime.sandbox.pdfUploadHandler.getUploadedPDFs()).toEqual([]);
if (kind === 'save') expect(document.getElementById('collection-success').textContent).toBe('');
if (kind === 'confirmation') expect(runtime.modalInstance.show).not.toHaveBeenCalled();
});
// Positive control for the history sign-in-changed pair. Reverts caught: an
// over-tight fix that shows every index/start 409 as an error.
it('keeps conversion 500 nonfatal and resumes polling after an already-running 409', async () => {
let armed = false;
let conversionReply;
let startReply;
runtime = createRuntime('history-search', input => {
if (!armed) return undefined;
if (input === CONVERT) return response({ error: 'Optional conversion unavailable' }, 500)
.then(value => { conversionReply = value; return value; });
if (input === INDEX_START) return response(INDEX_CONFLICT, 409)
.then(value => { startReply = value; return value; });
return undefined;
});
await runtime.boot(); runtime.fetchMock.mockClear(); armed = true;
await runtime.trigger('convert');
expect(runtime.fetchMock.mock.calls.map(([input]) => input)).toEqual([CONVERT, INDEX_START]);
expect(conversionReply.json).not.toHaveBeenCalled();
expect(startReply.json).toHaveBeenCalledOnce();
expect(runtime.intervals.size).toBe(1);
await runtime.pollOnce();
expect(runtime.fetchMock.mock.calls.at(-1)[0]).toBe(INDEX_STATUS);
expect(runtime.intervals.size).toBe(0); // The controlled status is idle.
expect(runtime.navigations).toEqual([]);
});
// Reverts caught: 'middleware' fails if triggerIndexing goes back to treating
// any 409 as "already running" (the shipped bug: silent idle teardown);
// 'marker-only' fails if isIndexingConflict drops its isAuthContextChanged
// clause or api.js stops exporting isAuthContextChanged.
it.each(signInChangedReplies)('history Index All shows a sign-in-changed 409 (%s) and never polls', async (_label, body) => {
let armed = false;
let startReply;
runtime = createRuntime('history-search', input => {
if (!armed || input !== INDEX_START) return undefined;
return response(body, 409, { 'X-LDR-Auth-Changed': '1' })
.then(value => { startReply = value; return value; });
});
await runtime.boot(); runtime.fetchMock.mockClear(); armed = true;
await runtime.trigger('index-start');
expect(startReply?.status).toBe(409);
expect(runtime.fetchMock.mock.calls.map(([input]) => input)).toEqual([CONVERT, INDEX_START]);
expect(runtime.intervals.size).toBe(0);
const progress = document.getElementById('indexing-progress-text');
expect(progress.textContent).toBe(`Error: ${SIGN_IN_CHANGED}`);
expect(progress.style.color).toBe('var(--error-color)');
expect(document.getElementById('index-all-btn').disabled).toBe(false);
expect(runtime.navigations).toEqual([]);
});
// Positive control for the pair above. Reverts caught: an over-tight fix that
// shows every index/start 409 as an error (or requires the marker to poll).
it('collection Index resumes polling for the real already-running 409', async () => {
let armed = false;
let startReply;
runtime = createRuntime('collection', input => {
if (!armed || input !== INDEX_START) return undefined;
return response(INDEX_CONFLICT, 409).then(value => { startReply = value; return value; });
});
await runtime.boot(); runtime.fetchMock.mockClear(); armed = true;
await runtime.trigger('collection-index');
expect(startReply?.status).toBe(409);
expect(runtime.sandbox.alert).toHaveBeenCalledWith(`Error: ${INDEX_CONFLICT.error}`);
expect(runtime.intervals.size).toBe(1);
const before = runtime.fetchMock.mock.calls.length;
await runtime.pollOnce();
expect(runtime.fetchMock.mock.calls[before][0]).toBe(INDEX_STATUS);
expect(runtime.navigations).toEqual([]);
});
// Reverts caught: 'middleware' fails if indexCollection goes back to
// `response.status === 409` alone; 'marker-only' fails if isIndexingConflict
// drops its isAuthContextChanged clause.
it.each(signInChangedReplies)('collection Index shows a sign-in-changed 409 (%s) and never polls', async (_label, body) => {
let armed = false;
let startReply;
runtime = createRuntime('collection', input => {
if (!armed || input !== INDEX_START) return undefined;
return response(body, 409, { 'X-LDR-Auth-Changed': '1' })
.then(value => { startReply = value; return value; });
});
await runtime.boot(); runtime.fetchMock.mockClear(); armed = true;
await runtime.trigger('collection-index');
expect(startReply?.status).toBe(409);
expect(runtime.fetchMock.mock.calls.map(([input]) => input)).toEqual([INDEX_START]);
expect(runtime.sandbox.alert).toHaveBeenCalledWith(`Error: ${SIGN_IN_CHANGED}`);
expect(runtime.intervals.size).toBe(0);
expect(runtime.navigations).toEqual([]);
});
it('isAuthContextChanged keys on the marker header, not the status or prose alone', async () => {
runtime = createRuntime('save');
const { isAuthContextChanged } = runtime.sandbox.api;
expect(isAuthContextChanged(await response({ error: SIGN_IN_CHANGED }, 409, { 'X-LDR-Auth-Changed': '1' }))).toBe(true);
expect(isAuthContextChanged(await response({ error: SIGN_IN_CHANGED }, 409))).toBe(false);
expect(isAuthContextChanged(await response(INDEX_CONFLICT, 409))).toBe(false);
expect(isAuthContextChanged(await response({}, 403, { 'X-LDR-Auth-Changed': '1' }))).toBe(false);
});
it('keeps network-failed confirmation preferences conservative and executes a confirmed action once', async () => {
runtime = createRuntime('confirmation', input => (input === CONFIRMATION
? Promise.reject(new Error('Preference network unavailable')) : undefined));
await runtime.boot();
const pending = runtime.trigger('confirmation');
await settle();
expect(runtime.modalInstance.show).toHaveBeenCalledOnce();
expect(runtime.action).not.toHaveBeenCalled();
document.getElementById('deleteConfirmBtn').click();
document.getElementById('deleteConfirmBtn').click();
await expect(pending).resolves.toBe('action completed');
expect(runtime.action).toHaveBeenCalledOnce();
expect(runtime.navigations).toEqual([]);
});
it('honors an explicit disabled-confirmation preference exactly once', async () => {
runtime = createRuntime('confirmation', input => (input === CONFIRMATION ? response({ value: false }) : undefined));
await runtime.boot();
await expect(runtime.trigger('confirmation')).resolves.toBe('action completed');
expect(runtime.action).toHaveBeenCalledOnce();
expect(runtime.modalInstance.show).not.toHaveBeenCalled();
expect(runtime.navigations).toEqual([]);
});
it.each(['success', 'network-failure'])('native export preserves HEAD and direct download for %s', async outcome => {
let reply;
runtime = createRuntime('logs', input => {
if (input !== EXPORT) return undefined;
if (outcome === 'network-failure') return Promise.reject(new Error('Preflight unavailable'));
return response().then(value => { reply = value; return value; });
});
await runtime.boot(); runtime.fetchMock.mockClear();
await runtime.trigger('logs-export');
expect(runtime.fetchMock.mock.calls).toEqual([[EXPORT, { method: 'HEAD' }]]);
expect(runtime.anchorClicks).toEqual([{ href: EXPORT, download: `research_logs_${RESEARCH_ID}.jsonl`, attached: true }]);
expect(document.querySelector('a[download]')).toBeNull();
expect(runtime.sandbox.URL.createObjectURL).not.toHaveBeenCalled();
expect(runtime.sandbox.URL.revokeObjectURL).not.toHaveBeenCalled();
if (reply) {
expect(reply.json).not.toHaveBeenCalled();
expect(reply.blob).not.toHaveBeenCalled();
}
expect(runtime.navigations).toEqual([]);
});
it.each([
[404, 'Research logs not found.'],
[429, 'Log export rate limit exceeded. Please wait a moment.'],
[500, 'Failed to export logs (HTTP 500).'],
])('native export HTTP %i keeps specific feedback and does not download', async (status, message) => {
let reply;
runtime = createRuntime('logs', input => (input === EXPORT
? response({}, status).then(value => { reply = value; return value; }) : undefined));
await runtime.boot(); runtime.fetchMock.mockClear();
await runtime.trigger('logs-export');
expect(runtime.fetchMock.mock.calls).toEqual([[EXPORT, { method: 'HEAD' }]]);
expect(runtime.sandbox.ui.showAlert).toHaveBeenCalledWith(message, 'error');
expect(runtime.anchorClicks).toEqual([]);
expect(reply.json).not.toHaveBeenCalled();
expect(reply.blob).not.toHaveBeenCalled();
expect(runtime.navigations).toEqual([]);
});
it.each(['success', 'marked-csrf-rejection'])('multipart upload preserves its body and is never replayed for %s', async outcome => {
let reply;
let bodyAtSend;
runtime = createRuntime('pdf', (input, options) => {
if (input !== '/api/upload/pdf') return undefined;
bodyAtSend = options.body;
return response(outcome === 'success' ? {
status: 'success', processed_files: 1,
extracted_texts: [{ filename: 'synthetic.pdf', text: 'Controlled PDF content', size: 4, pages: 1 }],
combined_text: 'Controlled PDF content', errors: [],
} : { detail: 'Rejected multipart CSRF' }, outcome === 'success' ? 200 : 403,
outcome === 'success' ? {} : { 'X-LDR-CSRF-Rejected': '1' })
.then(value => { reply = value; return value; });
});
await runtime.boot(); runtime.fetchMock.mockClear();
// A forwarding spy records the actual service input; the original CSRF
// implementation still executes. This checks body identity across it.
const recovery = vi.spyOn(runtime.sandbox.api, 'fetchWithCsrfRecovery');
const originalQuery = document.getElementById('query').value;
await runtime.trigger('pdf-upload');
expect(runtime.fetchMock).toHaveBeenCalledOnce();
const [url, options] = runtime.fetchMock.mock.calls[0];
expect(url).toBe('/api/upload/pdf');
expect(options.method).toBe('POST');
expect(bodyAtSend).toBeInstanceOf(window.FormData);
expect(bodyAtSend).toBe(recovery.mock.calls[0][1].body);
expect(bodyAtSend.getAll('files')).toHaveLength(1);
expect(bodyAtSend.get('files').name).toBe('synthetic.pdf');
const headers = new globalThis.Headers(options.headers);
expect(headers.get('X-CSRFToken')).toBe(CSRF_TOKEN);
expect(headers.has('Content-Type')).toBe(false);
expect(headers.has('X-LDR-Auth-Context')).toBe(false); // Nonrepeatable body.
if (outcome === 'success') {
expect(reply.json).toHaveBeenCalledOnce();
expect(document.getElementById('query').value).toContain('Controlled PDF content');
expect(runtime.sandbox.pdfUploadHandler.getUploadedPDFs()).toHaveLength(1);
} else {
expect(reply.json).not.toHaveBeenCalled();
expect(document.getElementById('query').value).toBe(originalQuery);
expect(runtime.sandbox.pdfUploadHandler.getUploadedPDFs()).toEqual([]);
}
expect(runtime.navigations).toEqual([]);
});
it.each(['success', 'refresh-401', 'replacement-409', 'replacement-body'])('actual save CSRF recovery handles %s with one original login', async outcome => {
let armed = false;
let writes = 0;
let rejectedRefresh;
runtime = createRuntime('save', input => {
if (!armed) return undefined;
if (input === SAVE && ++writes === 1) return response({ detail: 'CSRF rejected' }, 403, { 'X-LDR-CSRF-Rejected': '1' });
if (input === '/auth/csrf-token') {
const status = outcome === 'refresh-401' ? 401 : outcome === 'replacement-409' ? 409 : 200;
const body = status === 200
? { auth_context: outcome === 'replacement-body' ? 'd'.repeat(64) : AUTH_CONTEXT, csrf_token: 'fresh-area3-csrf' }
: { detail: status === 401 ? 'Authentication required' : 'Sign-in changed' };
return response(body, status).then(value => { rejectedRefresh = value; return value; });
}
return undefined;
});
await runtime.boot(); runtime.fetchMock.mockClear(); armed = true;
let settled = false;
const pending = runtime.trigger('save');
pending.then(() => { settled = true; }, () => { settled = true; });
await settle();
const originals = runtime.fetchMock.mock.calls.filter(([input]) => input === SAVE);
const refreshes = runtime.fetchMock.mock.calls.filter(([input]) => input === '/auth/csrf-token');
expect(refreshes).toHaveLength(1);
expect(refreshes[0][1]).toMatchObject({ method: 'GET', credentials: 'same-origin', cache: 'no-store', redirect: 'error' });
expect(new globalThis.Headers(refreshes[0][1].headers).get('X-LDR-Auth-Context')).toBe(AUTH_CONTEXT);
expect(originals[0][1].body).toBe(JSON.stringify({ collection_id: COLLECTION_ID }));
expect(originals[0][1].method).toBe('POST');
expect(new globalThis.Headers(originals[0][1].headers).get('X-LDR-Auth-Context')).toBe(AUTH_CONTEXT);
if (outcome === 'success') {
await pending;
expect(originals).toHaveLength(2);
expect(originals[1][1].body).toBe(originals[0][1].body);
expect(originals[1][1].method).toBe(originals[0][1].method);
expect(new globalThis.Headers(originals[0][1].headers).get('X-CSRFToken')).toBe(CSRF_TOKEN);
expect(new globalThis.Headers(originals[1][1].headers).get('X-CSRFToken')).toBe('fresh-area3-csrf');
expect(new globalThis.Headers(originals[1][1].headers).get('X-LDR-Auth-Context')).toBe(AUTH_CONTEXT);
expect(document.getElementById('collection-success').textContent).toContain('2 documents added.');
expect(document.querySelector('meta[name="csrf-token"]').content).toBe('fresh-area3-csrf');
} else {
expect(originals).toHaveLength(1);
expect(document.getElementById('collection-success').textContent).toBe('');
if (outcome === 'refresh-401') {
expect(rejectedRefresh?.status).toBe(401);
expect(settled).toBe(false);
expect(rejectedRefresh.json).not.toHaveBeenCalled();
const next = runtime.original.pathname + runtime.original.search + runtime.original.hash;
expect(runtime.navigations).toEqual([`/auth/login?next=${encodeURIComponent(next)}`]);
} else {
await pending;
expect(document.getElementById('collection-error').textContent).toMatch(/[Ss]ign-in changed/);
}
}
if (outcome !== 'refresh-401') expect(runtime.navigations).toEqual([]);
});
it('leaves an auth endpoint own 401 observable without a login loop', async () => {
runtime = createRuntime('save', input => (input === '/auth/login' ? response({ detail: 'Invalid sign-in' }, 401) : undefined));
const result = await runtime.sandbox.safeFetchWithAuth('/auth/login');
expect(result.status).toBe(401);
expect(result.json).not.toHaveBeenCalled();
expect(runtime.navigations).toEqual([]);
});