~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
/**
* Runtime contracts for the two mutating consumers of the migrated news
* search-history endpoint. The functions are executed from the shipped page
* source so method, CSRF, credentials, and body drift fail together.
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
const NEWS_SOURCE_PATH = resolve(
__dirname,
'../../../src/local_deep_research/web/static/js/pages/news.js',
);
function extractFunction(source, name) {
const signature = new RegExp(`async\\s+function\\s+${name}\\s*\\(`);
const match = signature.exec(source);
if (!match) throw new Error(`Function ${name} not found in news.js`);
const openBrace = source.indexOf('{', match.index + match[0].length);
let depth = 0;
for (let index = openBrace; index < source.length; index += 1) {
if (source[index] === '{') depth += 1;
if (source[index] === '}') {
depth -= 1;
if (depth === 0) return source.slice(match.index, index + 1);
}
}
throw new Error(`Function ${name} has an unterminated body`);
}
function compileHarness(overrides = {}) {
const source = readFileSync(NEWS_SOURCE_PATH, 'utf8');
const functions = ['saveSearchHistory', 'clearSearchHistory', 'loadSearchHistory', 'addToSearchHistory']
.map(name => extractFunction(source, name))
.join('\n');
const factory = new Function( // eslint-disable-line no-new-func
'getCSRFToken',
'displayRecentSearches',
'showNewsAlert',
'SafeLogger',
`
let searchHistory = [{ query: 'existing search' }];
let searchHistoryMutationGeneration = 0;
let searchHistoryLoadRequestId = 0;
let searchHistoryWriteQueue = Promise.resolve();
let newsItems = [];
${functions}
return {
saveSearchHistory,
clearSearchHistory,
addToSearchHistory,
getSearchHistory: () => searchHistory,
};
`,
);
return factory(
overrides.getCSRFToken || (() => 'history-csrf'),
overrides.displayRecentSearches || vi.fn(),
overrides.showNewsAlert || vi.fn(),
overrides.SafeLogger || { log: vi.fn(), error: vi.fn() },
);
}
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
it('posts the current query, type, and count with same-origin CSRF', async () => {
const fetchMock = vi.fn().mockResolvedValue({
status: 200,
json: vi.fn().mockResolvedValue({ success: true }),
});
vi.stubGlobal('fetch', fetchMock);
const harness = compileHarness();
await harness.saveSearchHistory('FastAPI migrations', 'deep', 17);
expect(fetchMock).toHaveBeenCalledWith('/news/api/search-history', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'history-csrf',
},
credentials: 'same-origin',
body: JSON.stringify({
query: 'FastAPI migrations',
type: 'deep',
resultCount: 17,
}),
});
});
it('deletes all history and clears the rendered list only after success', async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: true });
const displayRecentSearches = vi.fn();
const showNewsAlert = vi.fn();
vi.stubGlobal('fetch', fetchMock);
vi.stubGlobal('confirm', vi.fn().mockReturnValue(true));
const harness = compileHarness({ displayRecentSearches, showNewsAlert });
await harness.clearSearchHistory();
expect(fetchMock).toHaveBeenCalledWith('/news/api/search-history', {
method: 'DELETE',
credentials: 'same-origin',
headers: { 'X-CSRFToken': 'history-csrf' },
});
expect(harness.getSearchHistory()).toEqual([]);
expect(displayRecentSearches).toHaveBeenCalledOnce();
expect(showNewsAlert).toHaveBeenCalledWith(
'Search history cleared',
'success',
);
});
it('orders old insert, clear, and a newer search by user intent', async () => {
let releaseInsert;
const insertion = new Promise(resolveInsert => { releaseInsert = resolveInsert; });
let server = [{ query: 'existing' }];
const writes = [];
vi.stubGlobal('confirm', () => true);
vi.stubGlobal('fetch', vi.fn(async (_url, options) => {
if (options.method === 'POST') {
const query = JSON.parse(options.body).query;
writes.push(`POST ${query}`);
if (query === 'older') await insertion;
server.push({ query });
} else if (options.method === 'DELETE') {
writes.push('DELETE');
server = [];
}
const snapshot = [...server];
return { ok: true, status: 200, json: async () => ({ search_history: snapshot }) };
}));
const harness = compileHarness();
const older = harness.addToSearchHistory('older');
await Promise.resolve();
const clear = harness.clearSearchHistory();
const newer = harness.addToSearchHistory('newer');
await Promise.resolve();
expect(writes).toEqual(['POST older']);
releaseInsert();
await Promise.all([older, clear, newer]);
expect(writes).toEqual(['POST older', 'DELETE', 'POST newer']);
expect(server).toEqual([{ query: 'newer' }]);
expect(harness.getSearchHistory()).toEqual(server);
});
it('releases the history queue after a failed insert or delete', async () => {
vi.stubGlobal('confirm', () => true);
const fetchMock = vi.fn()
.mockRejectedValueOnce(new Error('insert failed'))
.mockRejectedValueOnce(new Error('delete failed'))
.mockResolvedValueOnce({ status: 200, json: async () => ({ status: 'success' }) });
vi.stubGlobal('fetch', fetchMock);
const harness = compileHarness();
await Promise.all([
harness.saveSearchHistory('first', 'quick', 0),
harness.clearSearchHistory(),
harness.saveSearchHistory('last', 'quick', 0),
]);
expect(fetchMock.mock.calls.map(([, options]) => options.method)).toEqual(['POST', 'DELETE', 'POST']);
});
it('preserves visible history when deletion is declined or rejected', async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 503 });
const displayRecentSearches = vi.fn();
const showNewsAlert = vi.fn();
const confirmMock = vi.fn().mockReturnValueOnce(false).mockReturnValue(true);
vi.stubGlobal('fetch', fetchMock);
vi.stubGlobal('confirm', confirmMock);
const harness = compileHarness({ displayRecentSearches, showNewsAlert });
await harness.clearSearchHistory();
expect(fetchMock).not.toHaveBeenCalled();
await harness.clearSearchHistory();
expect(fetchMock).toHaveBeenCalledOnce();
expect(harness.getSearchHistory()).toEqual([
{ query: 'existing search' },
]);
expect(displayRecentSearches).not.toHaveBeenCalled();
expect(showNewsAlert).toHaveBeenCalledWith(
'Failed to clear search history',
'danger',
);
});