~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
/** Browser endpoint contracts for the checked-in news vote consumers. */
import { readFileSync } from 'node:fs';
import { resolve as resolvePath } from 'node:path';
import { compileTemplateHarness } from '../helpers/template-harness.js';
const NEWS_SOURCE_PATH = resolvePath(
__dirname,
'../../../src/local_deep_research/web/static/js/pages/news.js',
);
function response(payload, ok = true, status = ok ? 200 : 500) {
return {
ok,
status,
json: vi.fn().mockResolvedValue(payload),
};
}
function deferred() {
let resolvePromise;
let rejectPromise;
const promise = new Promise((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
return { promise, reject: rejectPromise, resolve: resolvePromise };
}
function compileVotesRuntime(items, csrfToken = 'csrf-news-votes') {
const showNewsAlert = vi.fn();
const runtime = compileTemplateHarness({
templatePath: NEWS_SOURCE_PATH,
functionNames: ['loadVotesForNewsItems', 'vote'],
dependencies: { items, csrfToken, showNewsAlert },
preamble: `
let newsItems = items;
let voteLoadRequestId = 0;
const activeVoteRequests = new Map();
const voteRequestTails = new Map();
const getCSRFToken = () => csrfToken;
`,
returnExpression: `({
loadVotesForNewsItems,
vote,
setNewsItems: value => { newsItems = value; },
})`,
});
return { ...runtime, showNewsAlert };
}
function addNewsCard(id) {
const card = document.createElement('article');
card.dataset.newsId = id;
card.innerHTML = `
<button class="ldr-vote-btn ldr-voted">old up</button>
<button class="ldr-vote-btn ldr-voted">old down</button>
`;
document.body.appendChild(card);
return card;
}
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
document.body.replaceChildren();
});
it('posts card IDs with CSRF and renders numeric, inert vote state', async () => {
const firstCard = addNewsCard('news-1');
const secondCard = addNewsCard('news-2');
const fetchMock = vi.fn().mockResolvedValue(response({
votes: {
'news-1': { upvotes: '6', downvotes: 2, user_vote: 'up' },
'news-2': {
upvotes: '<img src=x onerror=alert(1)>',
downvotes: '<script>alert(1)</script>',
user_vote: null,
},
},
}));
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([
{ id: 'news-1' },
{ id: 'news-2' },
]);
await runtime.loadVotesForNewsItems();
expect(fetchMock).toHaveBeenCalledOnce();
expect(fetchMock).toHaveBeenCalledWith('/news/api/feedback/batch', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-news-votes',
},
body: JSON.stringify({ card_ids: ['news-1', 'news-2'] }),
});
const firstButtons = firstCard.querySelectorAll('.ldr-vote-btn');
expect(firstButtons[0].textContent).toContain('6');
expect(firstButtons[1].textContent).toContain('2');
expect(firstButtons[0].classList.contains('ldr-voted')).toBe(true);
expect(firstButtons[1].classList.contains('ldr-voted')).toBe(false);
const secondButtons = secondCard.querySelectorAll('.ldr-vote-btn');
expect(secondButtons[0].textContent).toContain('0');
expect(secondButtons[1].textContent).toContain('0');
expect(secondButtons[0].classList.contains('ldr-voted')).toBe(false);
expect(secondButtons[1].classList.contains('ldr-voted')).toBe(false);
expect(secondCard.querySelector('img, script')).toBeNull();
expect(secondCard.innerHTML).not.toContain('onerror');
});
it('posts an individual vote with CSRF and keeps response counts inert', async () => {
const card = addNewsCard('news-vote');
const fetchMock = vi.fn().mockResolvedValue(response({
upvotes: '<img src=x onerror=alert(1)>',
downvotes: '12',
}));
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([{ id: 'news-vote' }]);
expect(readFileSync(NEWS_SOURCE_PATH, 'utf8')).toContain('window.vote = vote;');
await runtime.vote('news-vote', 'down');
expect(fetchMock).toHaveBeenCalledOnce();
expect(fetchMock).toHaveBeenCalledWith('/news/api/feedback/news-vote', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-news-votes',
},
body: JSON.stringify({ vote: 'down' }),
});
const buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('0');
expect(buttons[1].textContent).toContain('12');
expect(buttons[0].classList.contains('ldr-voted')).toBe(false);
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
expect(card.querySelector('img, script')).toBeNull();
expect(card.innerHTML).not.toContain('onerror');
});
it('ignores a stale batch response after a newer vote load finishes', async () => {
const card = addNewsCard('shared-news');
const olderResponse = deferred();
const fetchMock = vi.fn()
.mockImplementationOnce(() => olderResponse.promise)
.mockResolvedValueOnce(response({
votes: {
'shared-news': { upvotes: 9, downvotes: 4, user_vote: 'down' },
},
}));
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([{ id: 'shared-news' }]);
const olderLoad = runtime.loadVotesForNewsItems();
await runtime.loadVotesForNewsItems();
let buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('9');
expect(buttons[1].textContent).toContain('4');
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
olderResponse.resolve(response({
votes: {
'shared-news': { upvotes: 1, downvotes: 0, user_vote: 'up' },
},
}));
await olderLoad;
buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('9');
expect(buttons[1].textContent).toContain('4');
expect(buttons[0].classList.contains('ldr-voted')).toBe(false);
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
});
it('serializes same-news votes and renders only the latest intent', async () => {
const card = addNewsCard('rapid-news');
const olderResponse = deferred();
const newerResponse = deferred();
const fetchMock = vi.fn()
.mockImplementationOnce(() => olderResponse.promise)
.mockImplementationOnce(() => newerResponse.promise);
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([{ id: 'rapid-news' }]);
const olderVote = runtime.vote('rapid-news', 'up');
const newerVote = runtime.vote('rapid-news', 'down');
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledOnce();
});
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ vote: 'up' });
olderResponse.resolve(response({ upvotes: 6, downvotes: 7 }));
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(2);
});
expect(JSON.parse(fetchMock.mock.calls[1][1].body))
.toEqual({ vote: 'down' });
expect(card.querySelectorAll('.ldr-vote-btn')[0].textContent)
.toBe('old up');
newerResponse.resolve(response({ upvotes: 5, downvotes: 8 }));
await Promise.all([olderVote, newerVote]);
const buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('5');
expect(buttons[1].textContent).toContain('8');
expect(buttons[0].classList.contains('ldr-voted')).toBe(false);
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
});
it.each([
[
'non-OK',
() => response({ error: 'rejected' }, false, 503),
{ upvotes: 6, downvotes: 7, user_vote: 'up' },
],
[
'malformed',
() => ({
ok: true,
status: 200,
json: vi.fn().mockRejectedValue(new SyntaxError('invalid JSON')),
}),
{ upvotes: 5, downvotes: 8, user_vote: 'down' },
],
])('reconciles after the latest queued vote returns %s', async (
_failureType,
failedResponse,
authoritativeVote,
) => {
const card = addNewsCard('reconcile-news');
const olderResponse = deferred();
const latestResponse = deferred();
const reconciliationResponse = deferred();
const fetchMock = vi.fn()
.mockImplementationOnce(() => olderResponse.promise)
.mockImplementationOnce(() => latestResponse.promise)
.mockImplementationOnce(() => reconciliationResponse.promise);
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([{ id: 'reconcile-news' }]);
const olderVote = runtime.vote('reconcile-news', 'up');
const latestVote = runtime.vote('reconcile-news', 'down');
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledOnce();
});
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ vote: 'up' });
olderResponse.resolve(response({ upvotes: 6, downvotes: 7 }));
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(2);
});
expect(JSON.parse(fetchMock.mock.calls[1][1].body))
.toEqual({ vote: 'down' });
latestResponse.resolve(failedResponse());
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(3);
});
expect(fetchMock.mock.calls[2]).toEqual([
'/news/api/feedback/batch',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-news-votes',
},
body: JSON.stringify({ card_ids: ['reconcile-news'] }),
},
]);
expect(runtime.showNewsAlert).toHaveBeenCalledWith(
'Failed to save vote. Restoring the latest vote state.',
'error',
);
reconciliationResponse.resolve(response({
votes: { 'reconcile-news': authoritativeVote },
}));
await Promise.all([olderVote, latestVote]);
const buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain(String(authoritativeVote.upvotes));
expect(buttons[1].textContent)
.toContain(String(authoritativeVote.downvotes));
expect(buttons[0].classList.contains('ldr-voted'))
.toBe(authoritativeVote.user_vote === 'up');
expect(buttons[1].classList.contains('ldr-voted'))
.toBe(authoritativeVote.user_vote === 'down');
});
it('starts a same-news retry before stale reconciliation settles', async () => {
const card = addNewsCard('retry-during-reconcile');
const olderResponse = deferred();
const failedResponse = deferred();
const reconciliationResponse = deferred();
const retryResponse = deferred();
const fetchMock = vi.fn()
.mockImplementationOnce(() => olderResponse.promise)
.mockImplementationOnce(() => failedResponse.promise)
.mockImplementationOnce(() => reconciliationResponse.promise)
.mockImplementationOnce(() => retryResponse.promise);
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([
{ id: 'retry-during-reconcile' },
]);
const olderVote = runtime.vote('retry-during-reconcile', 'up');
const failedVote = runtime.vote('retry-during-reconcile', 'down');
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledOnce();
});
olderResponse.resolve(response({ upvotes: 4, downvotes: 1 }));
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(2);
});
failedResponse.resolve(response({ error: 'temporary' }, false, 503));
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(3);
});
expect(fetchMock.mock.calls[2][0]).toBe('/news/api/feedback/batch');
const retryVote = runtime.vote('retry-during-reconcile', 'down');
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(4);
});
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'/news/api/feedback/retry-during-reconcile',
'/news/api/feedback/retry-during-reconcile',
'/news/api/feedback/batch',
'/news/api/feedback/retry-during-reconcile',
]);
expect(JSON.parse(fetchMock.mock.calls[3][1].body))
.toEqual({ vote: 'down' });
reconciliationResponse.resolve(response({
votes: {
'retry-during-reconcile': {
upvotes: 4,
downvotes: 1,
user_vote: 'up',
},
},
}));
await Promise.all([olderVote, failedVote]);
let buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toBe('old up');
expect(buttons[1].textContent).toBe('old down');
retryResponse.resolve(response({ upvotes: 4, downvotes: 2 }));
await retryVote;
buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('4');
expect(buttons[1].textContent).toContain('2');
expect(buttons[0].classList.contains('ldr-voted')).toBe(false);
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
});
it('continues a same-news vote queue after the older request rejects', async () => {
const card = addNewsCard('retry-news');
const olderResponse = deferred();
const latestResponse = deferred();
const fetchMock = vi.fn()
.mockImplementationOnce(() => olderResponse.promise)
.mockImplementationOnce(() => latestResponse.promise);
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([{ id: 'retry-news' }]);
const olderVote = runtime.vote('retry-news', 'up');
const latestVote = runtime.vote('retry-news', 'down');
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledOnce();
});
olderResponse.reject(new Error('older vote failed'));
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(2);
});
expect(JSON.parse(fetchMock.mock.calls[1][1].body))
.toEqual({ vote: 'down' });
latestResponse.resolve(response({ upvotes: 2, downvotes: 9 }));
await Promise.all([olderVote, latestVote]);
const buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('2');
expect(buttons[1].textContent).toContain('9');
expect(buttons[0].classList.contains('ldr-voted')).toBe(false);
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
});
it('starts votes for different news IDs concurrently', async () => {
const firstCard = addNewsCard('parallel-a');
const secondCard = addNewsCard('parallel-b');
const firstResponse = deferred();
const secondResponse = deferred();
const fetchMock = vi.fn(url => (
url.endsWith('/parallel-a')
? firstResponse.promise
: secondResponse.promise
));
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([
{ id: 'parallel-a' },
{ id: 'parallel-b' },
]);
const firstVote = runtime.vote('parallel-a', 'up');
const secondVote = runtime.vote('parallel-b', 'down');
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledTimes(2);
});
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'/news/api/feedback/parallel-a',
'/news/api/feedback/parallel-b',
]);
secondResponse.resolve(response({ upvotes: 1, downvotes: 4 }));
await secondVote;
expect(secondCard.querySelectorAll('.ldr-vote-btn')[1].textContent)
.toContain('4');
expect(firstCard.querySelectorAll('.ldr-vote-btn')[0].textContent)
.toBe('old up');
firstResponse.resolve(response({ upvotes: 3, downvotes: 0 }));
await firstVote;
expect(firstCard.querySelectorAll('.ldr-vote-btn')[0].textContent)
.toContain('3');
});
it('does not let an older batch overwrite a completed individual vote', async () => {
const card = addNewsCard('batch-then-vote');
const olderBatch = deferred();
const fetchMock = vi.fn()
.mockImplementationOnce(() => olderBatch.promise)
.mockResolvedValueOnce(response({ upvotes: 10, downvotes: 3 }));
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([{ id: 'batch-then-vote' }]);
const batchLoad = runtime.loadVotesForNewsItems();
await runtime.vote('batch-then-vote', 'down');
olderBatch.resolve(response({
votes: {
'batch-then-vote': {
upvotes: 9,
downvotes: 2,
user_vote: 'up',
},
},
}));
await batchLoad;
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'/news/api/feedback/batch',
'/news/api/feedback/batch-then-vote',
]);
const buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('10');
expect(buttons[1].textContent).toContain('3');
expect(buttons[0].classList.contains('ldr-voted')).toBe(false);
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
});
it('keeps a completed mutation authoritative over a batch started mid-write', async () => {
const card = addNewsCard('vote-then-batch');
const individualResponse = deferred();
const batchResponse = deferred();
const fetchMock = vi.fn()
.mockImplementationOnce(() => individualResponse.promise)
.mockImplementationOnce(() => batchResponse.promise);
vi.stubGlobal('fetch', fetchMock);
const runtime = compileVotesRuntime([{ id: 'vote-then-batch' }]);
const individualVote = runtime.vote('vote-then-batch', 'down');
const batchLoad = runtime.loadVotesForNewsItems();
individualResponse.resolve(response({ upvotes: 10, downvotes: 3 }));
await individualVote;
batchResponse.resolve(response({
votes: {
'vote-then-batch': {
upvotes: 9,
downvotes: 2,
user_vote: 'up',
},
},
}));
await batchLoad;
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
'/news/api/feedback/vote-then-batch',
'/news/api/feedback/batch',
]);
const buttons = card.querySelectorAll('.ldr-vote-btn');
expect(buttons[0].textContent).toContain('10');
expect(buttons[1].textContent).toContain('3');
expect(buttons[0].classList.contains('ldr-voted')).toBe(false);
expect(buttons[1].classList.contains('ldr-voted')).toBe(true);
});