~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
/** Direct browser-runtime coverage for feed state and user actions in news.js. */
import DOMPurify from 'dompurify';
const NOW = new Date('2026-09-01T12:00:00.000Z');
function response(payload, status = 200) {
return {
ok: status >= 200 && status < 300,
status,
headers: {},
json: vi.fn().mockResolvedValue(payload),
text: vi.fn().mockResolvedValue(JSON.stringify(payload)),
};
}
function deferred() {
let resolvePromise;
let rejectPromise;
const promise = new Promise((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
return { promise, resolve: resolvePromise, reject: rejectPromise };
}
function installDom() {
document.body.innerHTML = `
<header class="ldr-feed-header"><h2>News</h2></header>
<section id="news-feed-content"></section>
<input id="table-view-toggle" type="checkbox">
<input id="news-search" type="search">
<button id="search-btn"></button>
<button id="create-subscription-btn"></button>
<input id="impact-filter" type="range" min="0" max="10" value="0">
<span class="ldr-impact-value">0+</span>
<span class="ldr-slider-value">0</span>
<section id="news-semantic-results" style="display: none"></section>
<input id="news-query">
<form id="news-search-form"></form>
<div id="newsSubscriptionModal">
<form id="news-subscription-form">
<textarea id="news-subscription-query"></textarea>
<input id="news-subscription-name">
<select id="news-subscription-frequency">
<option value="4">Every 4 hours</option>
</select>
<select id="news-subscription-folder">
<option value="">Uncategorized</option>
</select>
<input id="news-subscription-active" type="checkbox" checked>
<input id="news-subscription-run-now" type="checkbox">
<input id="news-subscription-model">
<select id="news-subscription-strategy">
<option value="source-based">Source based</option>
</select>
<button id="run-template-btn" type="button"></button>
<button type="submit">Create</button>
</form>
</div>
<div class="ldr-time-filter-group">
<button class="ldr-filter-btn active" data-time="all"></button>
<button class="ldr-filter-btn" data-time="today"></button>
<button class="ldr-filter-btn" data-time="week"></button>
</div>
<input id="auto-refresh" type="checkbox">
<label for="auto-refresh">Auto-refresh</label>
<button id="refresh-feed-btn"><i></i></button>
<div id="recent-searches"></div>
<div id="trending-topics"></div>
<div id="priority-status" style="display: none"><span id="priority-message"></span></div>
<div id="news-alert" style="display: none"></div>
<button id="news-search-mode-btn"></button>
<div id="news-search-mode-menu">
<a class="dropdown-item active" data-mode="hybrid"></a>
<a class="dropdown-item" data-mode="text"></a>
<a class="dropdown-item" data-mode="semantic"></a>
</div>
<section id="query-template" style="display: none">
<pre class="ldr-template-content"></pre>
</section>
`;
}
let fetchMock;
let feedItems;
let searchHistory;
let clipboardWrite;
let createdBlob;
let downloadedFilename;
let bootstrapSnapshot;
function setReadState(newsId, shouldBeRead) {
const card = document.querySelector(`[data-news-id="${newsId}"]`);
const isRead = card?.classList.contains('ldr-is-read') ?? false;
if (isRead !== shouldBeRead) window.toggleReadStatus(newsId);
}
function setSavedState(newsId, shouldBeSaved) {
const savedIds = JSON.parse(
localStorage.getItem('saved_news_ids') || '[]',
);
if (savedIds.includes(newsId) !== shouldBeSaved) {
window.toggleSaveItem(newsId);
}
}
async function restoreAllFeed() {
history.replaceState({}, '', '/');
document.getElementById('news-search').value = '';
const tableToggle = document.getElementById('table-view-toggle');
if (tableToggle.checked) {
tableToggle.checked = false;
tableToggle.dispatchEvent(new Event('change'));
}
window.clearAllFilters();
await window.selectSubscription('all');
await vi.waitFor(() => {
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
});
}
async function renderSecondNewsLinks(sourceUrl, linkUrl = sourceUrl) {
const originalFetch = fetchMock.getMockImplementation();
fetchMock.mockImplementation((input, options = {}) => {
if (String(input).startsWith('/news/api/feed?')) {
return Promise.resolve(response({
news_items: feedItems.map(item => (
item.id === 'news-two'
? {
...item,
source_url: sourceUrl,
links: item.links.map(link => ({
...link,
url: linkUrl,
})),
}
: { ...item }
)),
}));
}
return originalFetch(input, options);
});
try {
await window.selectSubscription('all');
const card = document.querySelector('[data-news-id="news-two"]');
return {
primaryHref: card.querySelector('.btn-primary').getAttribute('href'),
sourceHref: card.querySelector('.ldr-source-link').getAttribute('href'),
};
} finally {
fetchMock.mockImplementation(originalFetch);
}
}
beforeAll(async () => {
vi.useFakeTimers();
vi.setSystemTime(NOW);
localStorage.clear();
sessionStorage.clear();
installDom();
// DOMPurify's DOM traversal is not supported by happy-dom. This harness
// mirrors the production policy relevant to the page: event attributes
// and unsafe URLs are removed, while declarative data attributes survive.
const purify = {
isSupported: true,
addHook: vi.fn(),
sanitize: vi.fn(dirty => {
const template = document.createElement('template');
// eslint-disable-next-line no-unsanitized/property -- static test sanitizer for controlled repo fixtures.
template.innerHTML = String(dirty);
template.content.querySelectorAll('script, iframe, object')
.forEach(node => node.remove());
template.content.querySelectorAll('*').forEach(node => {
for (const attr of [...node.attributes]) {
if (attr.name.startsWith('on')) node.removeAttribute(attr.name);
if (
attr.name === 'href' &&
/^(?:javascript|data|vbscript):/i.test(attr.value.trim())
) node.setAttribute('href', '#');
}
});
return template.content.cloneNode(true);
}),
};
globalThis.DOMPurify = purify;
window.DOMPurify = purify;
window.RESEARCH_STATUS = {
IN_PROGRESS: 'in_progress',
QUEUED: 'queued',
PENDING: 'pending',
COMPLETED: 'completed',
FAILED: 'failed',
ERROR: 'error',
CANCELLED: 'cancelled',
SUSPENDED: 'suspended',
};
window.RESEARCH_TERMINAL_STATES = new Set([
'completed', 'failed', 'error', 'cancelled', 'suspended',
]);
window.api = { getCsrfToken: vi.fn(() => 'csrf-news-direct') };
window.safeUpdateButton = vi.fn((button, icon, label) => {
button.dataset.icon = icon;
button.textContent = label;
});
clipboardWrite = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, 'clipboard', {
configurable: true,
value: { writeText: clipboardWrite },
});
Object.defineProperty(navigator, 'share', {
configurable: true,
value: undefined,
});
Object.defineProperty(URL, 'createObjectURL', {
configurable: true,
value: vi.fn(blob => {
createdBlob = blob;
return 'blob:news-export';
}),
});
vi.spyOn(window.HTMLAnchorElement.prototype, 'click').mockImplementation(function() {
downloadedFilename = this.download;
});
const modalInstances = new WeakMap();
class Modal {
constructor(element) {
this.element = element;
modalInstances.set(element, this);
}
show() {
this.element.dataset.shown = 'true';
}
hide() {
this.element.dataset.hidden = 'true';
}
static getInstance(element) {
return modalInstances.get(element) || null;
}
}
globalThis.bootstrap = { Modal };
window.bootstrap = globalThis.bootstrap;
feedItems = [{
id: 'news-one',
research_id: 'research-one',
headline: 'FastAPI migration reaches production',
category: 'Technology',
impact_score: 9,
upvotes: 1,
downvotes: 0,
created_at: '2026-09-01T11:30:00.000Z',
findings: '**Deployment complete**\n\nNo regressions observed.',
summary: 'Migration deployed. Services are healthy.',
topics: ['FastAPI', 'Migration'],
source_url: 'https://publisher.example/article?id=3299',
links: [{
title: 'Publisher report with a deliberately long descriptive title',
url: 'https://publisher.example/source?id=3299',
}],
}, {
id: 'news-two',
research_id: 'research/two',
headline: 'Older economy briefing',
category: 'Economy',
impact_score: 3,
upvotes: 0,
downvotes: 2,
created_at: '2026-08-10T10:00:00.000Z',
summary: 'Markets were quiet. Analysis remains stable.',
topics: ['Economy', 'Migration'],
source_url: 'javascript:window.__newsActionXss=true',
links: [{
title: 'Unsafe source',
url: 'javascript:window.__newsActionXss=true',
}],
}];
searchHistory = [{
id: 'search-1',
query: 'FastAPI migration',
type: 'table',
timestamp: '2026-09-01T11:55:00.000Z',
resultCount: 2,
}];
fetchMock = vi.fn(async (input, options = {}) => {
const url = String(input);
if (url === '/news/api/search-history' && options.method === 'GET') {
return response({ search_history: [...searchHistory] });
}
if (url === '/news/api/search-history' && options.method === 'POST') {
const payload = JSON.parse(options.body);
searchHistory.unshift({
id: `search-${searchHistory.length + 1}`,
timestamp: NOW.toISOString(),
...payload,
});
return response({ status: 'success' });
}
if (url === '/library/api/research-history/collection') {
return response({ success: true, collection_id: 'news-collection-3299' });
}
if (url === '/news/api/subscriptions/current') {
return response({
subscriptions: [{ id: 'subscription-3299', query: 'Migration' }],
});
}
if (url === '/news/api/subscription/folders') {
return response([{
id: 'folder-3299',
icon: '🚀',
name: '<img src=x onerror="window.__newsActionXss=true"> Migration',
}]);
}
if (url === '/api/start_research' && options.method === 'POST') {
return response({
status: 'queued',
research_id: 'advanced-news-3299',
});
}
if (url === '/news/api/subscribe' && options.method === 'POST') {
return response({ subscription_id: 'subscription-created' });
}
if (url.startsWith('/news/api/feed?')) {
return response({ news_items: feedItems.map(item => ({ ...item })) });
}
if (url === '/news/api/feedback/batch') {
return response({
votes: {
'news-one': { upvotes: 7, downvotes: 1, user_vote: 'up' },
'news-two': { upvotes: 0, downvotes: 4, user_vote: null },
},
});
}
if (url === '/news/api/feedback/news-one' && options.method === 'POST') {
return response({ upvotes: 12, downvotes: 3 });
}
if (url === '/history/api') return response({ items: [] });
throw new Error(`Unexpected request: ${url} ${options.method || 'GET'}`);
});
vi.stubGlobal('fetch', fetchMock);
await import('@js/config/constants.js');
await import('@js/config/urls.js');
await import('@js/security/url-validator.js');
await import('@js/security/xss-protection.js');
await import('@js/pages/news.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
await vi.waitFor(() => {
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
expect(document.querySelector('[data-news-id="news-one"] .ldr-vote-btn')
.textContent).toContain('7');
expect(document.getElementById('recent-searches').textContent)
.toContain('FastAPI migration');
});
const unsafeCard = document.querySelector('[data-news-id="news-two"]');
bootstrapSnapshot = {
trendingText: document.getElementById('trending-topics').textContent,
bulkActionsText: document.querySelector('.ldr-bulk-actions-bar').textContent,
primaryHref: unsafeCard.querySelector('.btn-primary').getAttribute('href'),
sourceHref: unsafeCard.querySelector('.ldr-source-link').getAttribute('href'),
scriptCount: unsafeCard.querySelectorAll('script').length,
xssExecuted: window.__newsActionXss,
};
});
afterAll(() => {
window.dispatchEvent(new Event('pagehide'));
vi.clearAllTimers();
vi.useRealTimers();
vi.restoreAllMocks();
vi.unstubAllGlobals();
localStorage.clear();
sessionStorage.clear();
document.body.replaceChildren();
delete window.__newsActionXss;
delete window.api;
delete window.bootstrap;
delete window.DOMPurify;
delete window.safeUpdateButton;
});
beforeEach(async () => {
await restoreAllFeed();
});
it('boots through migrated endpoints and renders feed, votes, history, and safe links', () => {
expect(fetchMock).toHaveBeenCalledWith('/news/api/subscriptions/current', {
credentials: 'same-origin',
});
expect(fetchMock).toHaveBeenCalledWith(
'/library/api/research-history/collection',
{ headers: { 'X-CSRFToken': 'csrf-news-direct' } },
);
expect(fetchMock).toHaveBeenCalledWith(
'/news/api/feed?limit=20&use_cache=true',
);
expect(fetchMock).toHaveBeenCalledWith('/news/api/feedback/batch', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-news-direct',
},
body: JSON.stringify({ card_ids: ['news-one', 'news-two'] }),
});
expect(bootstrapSnapshot).toMatchObject({
primaryHref: '#',
sourceHref: '#',
scriptCount: 0,
xssExecuted: undefined,
});
expect(bootstrapSnapshot.trendingText).toContain('Migration 2');
expect(bootstrapSnapshot.bulkActionsText).toContain('2 unread');
});
it('uses action attributes accepted by the installed DOMPurify defaults', () => {
expect(DOMPurify.isValidAttribute(
'button', 'data-news-action', 'toggle-read',
)).toBe(true);
expect(DOMPurify.isValidAttribute(
'button', 'data-news-page-action', 'clear-search',
)).toBe(true);
expect(DOMPurify.isValidAttribute(
'button', 'data-vote', 'up',
)).toBe(true);
expect(DOMPurify.isValidAttribute(
'button', 'onclick', "vote('news-one', 'up')",
)).toBe(false);
});
it.each([
['protocol-relative paths', '//outside.example/report'],
['slash-backslash paths', '/\\outside.example/report'],
['C0-prefixed paths', `${String.fromCharCode(0)}//outside.example/report`],
['control-obscured schemes', `h${String.fromCharCode(9)}ttps://outside.example/report`],
])('blocks rendered report links with %s', async (_case, unsafeUrl) => {
await expect(renderSecondNewsLinks(unsafeUrl)).resolves.toEqual({
primaryHref: '#',
sourceHref: '#',
});
});
it('preserves valid internal report and HTTPS source links', async () => {
await expect(renderSecondNewsLinks(
'/results/research-two?view=full#sources',
'https://publisher.example/report?id=3299',
)).resolves.toEqual({
primaryHref: '/results/research-two?view=full#sources',
sourceHref: 'https://publisher.example/report?id=3299',
});
});
it('preserves fragment, same-origin protocol-relative, and FTP report links', async () => {
const sameOriginReport = `//${window.location.host}/results/research-two`;
await expect(renderSecondNewsLinks(
'#report-details',
sameOriginReport,
)).resolves.toEqual({
primaryHref: '#report-details',
sourceHref: sameOriginReport,
});
await expect(renderSecondNewsLinks(
'ftp://publisher.example/report',
'ftps://publisher.example/source',
)).resolves.toEqual({
primaryHref: 'ftp://publisher.example/report',
sourceHref: 'ftps://publisher.example/source',
});
});
it('uses the legacy isSafeUrl validator when safeAssign is unavailable', async () => {
const validatorDescriptor = Object.getOwnPropertyDescriptor(
window,
'URLValidator',
);
const isSafeUrl = vi.fn(url => url.includes('allowed.example'));
Object.defineProperty(window, 'URLValidator', {
configurable: true,
value: { isSafeUrl },
});
try {
await expect(renderSecondNewsLinks(
'https://allowed.example/report',
'https://blocked.example/source',
)).resolves.toEqual({
primaryHref: 'https://allowed.example/report',
sourceHref: '#',
});
expect(isSafeUrl).toHaveBeenCalledWith(
'https://allowed.example/report',
);
expect(isSafeUrl).toHaveBeenCalledWith(
'https://blocked.example/source',
);
} finally {
Object.defineProperty(window, 'URLValidator', validatorDescriptor);
}
});
it('canonically validates rendered report links without URLValidator', async () => {
const validatorDescriptor = Object.getOwnPropertyDescriptor(
window,
'URLValidator',
);
Object.defineProperty(window, 'URLValidator', {
configurable: true,
value: undefined,
});
try {
await expect(renderSecondNewsLinks(
'/\\outside.example/report',
`${String.fromCharCode(0)}//outside.example/source`,
)).resolves.toEqual({
primaryHref: '#',
sourceHref: '#',
});
await expect(renderSecondNewsLinks(
'/results/research-two',
'https://publisher.example/source',
)).resolves.toEqual({
primaryHref: '/results/research-two',
sourceHref: 'https://publisher.example/source',
});
} finally {
Object.defineProperty(window, 'URLValidator', validatorDescriptor);
}
});
it.each([
['active data URL', 'data:text/html,<script>window.__newsActionXss=true</script>'],
['blob URL', 'blob:http://localhost/untrusted-news-source'],
])('blocks news-card navigation to an untrusted %s', async (
_case,
unsafeUrl,
) => {
const originalFetch = fetchMock.getMockImplementation();
fetchMock.mockImplementation((input, options = {}) => {
if (String(input).startsWith('/news/api/feed?')) {
return Promise.resolve(response({
news_items: feedItems.map(item => (
item.id === 'news-two'
? { ...item, source_url: unsafeUrl }
: { ...item }
)),
}));
}
return originalFetch(input, options);
});
const safeAssign = vi.spyOn(window.URLValidator, 'safeAssign');
try {
await window.selectSubscription('all');
const hrefBefore = window.location.href;
document.querySelector('[data-news-id="news-two"] .ldr-news-summary')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(safeAssign).toHaveBeenCalledWith(
window.location,
'href',
unsafeUrl,
);
expect(safeAssign.mock.results.at(-1).value).toBe(false);
expect(window.location.href).toBe(hrefBefore);
expect(window.__newsActionXss).toBeUndefined();
} finally {
safeAssign.mockRestore();
fetchMock.mockImplementation(originalFetch);
}
});
it('creates a table view that retains only valid HTTP sources', () => {
const toggle = document.getElementById('table-view-toggle');
toggle.checked = true;
toggle.dispatchEvent(new Event('change'));
const table = document.getElementById('news-table-view');
expect(table.style.display).toBe('block');
expect(document.getElementById('news-feed-content').style.display).toBe('none');
expect(table.querySelectorAll('tbody tr')).toHaveLength(2);
expect(table.querySelectorAll('tbody a')).toHaveLength(1);
expect(table.querySelector('tbody a').getAttribute('href'))
.toBe('https://publisher.example/article?id=3299');
toggle.checked = false;
toggle.dispatchEvent(new Event('change'));
expect(document.getElementById('news-feed-content').style.display).toBe('block');
expect(table.style.display).toBe('none');
});
it('votes through the exported page action with CSRF and authoritative counts', async () => {
await window.vote('news-one', 'down');
expect(fetchMock).toHaveBeenCalledWith('/news/api/feedback/news-one', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-news-direct',
},
body: JSON.stringify({ vote: 'down' }),
});
const card = document.querySelector('[data-news-id="news-one"]');
const [up, down] = card.querySelectorAll('.ldr-vote-btn');
expect(up.textContent).toContain('12');
expect(up.classList.contains('ldr-voted')).toBe(false);
expect(down.textContent).toContain('3');
expect(down.classList.contains('ldr-voted')).toBe(true);
});
it('keeps every rendered card action clickable through the page sanitizer contract', async () => {
setReadState('news-one', false);
setSavedState('news-one', false);
clipboardWrite.mockClear();
createdBlob = null;
downloadedFilename = null;
const card = document.querySelector('[data-news-id="news-one"]');
const actionControls = [...card.querySelectorAll('[data-news-action]')];
expect(actionControls.map(control => control.dataset.newsAction)).toEqual([
'toggle-read',
'share',
'copy-link',
'export-markdown',
'hide',
'vote',
'vote',
'mark-read-on-click',
'toggle-save',
]);
expect(card.querySelector('[onclick]')).toBeNull();
// Click the nested icons to prove the delegated listener owns descendants,
// not just clicks that happen directly on the action element.
card.querySelector('[data-news-action="toggle-read"] i')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(JSON.parse(localStorage.getItem('news_read_ids'))).toContain('news-one');
card.querySelector('[data-news-action="toggle-save"] i')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(JSON.parse(localStorage.getItem('saved_news_ids'))).toContain('news-one');
expect(card.querySelector('.ldr-save-btn i').className)
.toBe('bi bi-bookmark-fill');
const downvoteCallsBefore = fetchMock.mock.calls.filter(([url, options = {}]) => (
url === '/news/api/feedback/news-one' && options.method === 'POST'
)).length;
card.querySelector('[data-news-action="vote"][data-vote="down"] i')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await vi.waitFor(() => {
expect(fetchMock.mock.calls.filter(([url, options = {}]) => (
url === '/news/api/feedback/news-one' && options.method === 'POST'
))).toHaveLength(downvoteCallsBefore + 1);
expect(card.querySelectorAll('.ldr-vote-btn')[1].textContent)
.toContain('3');
});
const cardNavigation = vi.spyOn(window.URLValidator, 'safeAssign');
const shareEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
});
card.querySelector('[data-news-action="share"] i').dispatchEvent(shareEvent);
const copyEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
});
card.querySelector('[data-news-action="copy-link"] i').dispatchEvent(copyEvent);
await vi.waitFor(() => expect(clipboardWrite).toHaveBeenCalledTimes(2));
expect(clipboardWrite).toHaveBeenCalledWith(
'FastAPI migration reaches production\n\nhttps://publisher.example/article?id=3299',
);
expect(clipboardWrite).toHaveBeenCalledWith(
'https://publisher.example/article?id=3299',
);
expect(shareEvent.defaultPrevented).toBe(true);
expect(copyEvent.defaultPrevented).toBe(true);
expect(cardNavigation).not.toHaveBeenCalled();
cardNavigation.mockRestore();
history.replaceState({}, '', '/');
const exportEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
});
card.querySelector('[data-news-action="export-markdown"] i')
.dispatchEvent(exportEvent);
expect(exportEvent.defaultPrevented).toBe(true);
expect(createdBlob.type).toBe('text/markdown');
expect(downloadedFilename).toBe('fastapi_migration_reaches_production.md');
URL.createObjectURL.mockClear();
setReadState('news-one', false);
card.querySelector('[data-news-action="mark-read-on-click"] i')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(JSON.parse(localStorage.getItem('news_read_ids'))).toContain('news-one');
card.querySelector('[data-news-action="hide"] i')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await vi.advanceTimersByTimeAsync(300);
expect(document.querySelector('[data-news-id="news-one"]')).toBeNull();
});
it('opens the subscription form from its sanitized rendered card control', async () => {
feedItems[0].query = 'FastAPI migration releases';
try {
await window.selectSubscription('all');
const subscribeControl = document.querySelector(
'[data-news-id="news-one"] [data-news-action="create-subscription"]',
);
expect(subscribeControl).not.toBeNull();
expect(subscribeControl.getAttribute('onclick')).toBeNull();
subscribeControl.querySelector('i')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
const target = new URL(window.location.href);
expect(target.pathname).toBe('/news/subscriptions/new');
expect(target.searchParams.get('query')).toBe('FastAPI migration releases');
expect(target.searchParams.get('research_id')).toBe('research-one');
} finally {
delete feedItems[0].query;
history.replaceState({}, '', '/');
}
});
it('combines impact, time, and topic filters and clears their DOM state', () => {
const visibleIds = () => [...document.querySelectorAll('.ldr-news-item')]
.map(item => item.dataset.newsId);
const impact = document.getElementById('impact-filter');
impact.value = '8';
impact.dispatchEvent(new Event('input'));
expect(visibleIds()).toEqual(['news-one']);
expect(document.querySelector('.ldr-impact-value').textContent).toBe('8+');
impact.value = '0';
impact.dispatchEvent(new Event('input'));
document.querySelector('[data-time="week"]')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(visibleIds()).toEqual(['news-one']);
window.filterByTopic('Economy');
expect(visibleIds()).toEqual([]);
expect(document.querySelector('.ldr-filter-status-bar').textContent)
.toContain('Topic: Economy');
window.clearAllFilters();
expect(visibleIds()).toEqual(['news-one', 'news-two']);
expect(document.querySelector('[data-time="all"]').classList.contains('active'))
.toBe(true);
expect(impact.value).toBe('0');
expect(document.querySelector('.ldr-slider-value').textContent).toBe('0');
expect(document.querySelector('.ldr-filter-status-bar')).toBeNull();
});
it('persists read and saved ownership and restores the server feed after saved view', async () => {
setReadState('news-one', false);
setReadState('news-two', false);
setSavedState('news-one', false);
window.toggleReadStatus('news-one');
expect(JSON.parse(localStorage.getItem('news_read_ids'))).toContain('news-one');
expect(document.querySelector('[data-news-id="news-one"]')
.classList.contains('ldr-is-read')).toBe(true);
window.markAllAsRead();
expect(new Set(JSON.parse(localStorage.getItem('news_read_ids'))))
.toEqual(new Set(['news-one', 'news-two']));
window.toggleSaveItem('news-one');
expect(JSON.parse(localStorage.getItem('saved_news_ids'))).toEqual(['news-one']);
expect(JSON.parse(localStorage.getItem('saved_news_items'))['news-one'])
.toMatchObject({ headline: 'FastAPI migration reaches production' });
expect(document.querySelector('[data-news-id="news-one"] .ldr-save-btn i').className)
.toBe('bi bi-bookmark-fill');
const feedCallsBeforeSaved = fetchMock.mock.calls.filter(([url]) => (
String(url).startsWith('/news/api/feed?')
)).length;
await window.selectSubscription('saved');
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(1);
expect(document.querySelector('.ldr-feed-header h2').textContent).toBe('Saved Items');
expect(fetchMock.mock.calls.filter(([url]) => (
String(url).startsWith('/news/api/feed?')
))).toHaveLength(feedCallsBeforeSaved);
window.toggleSaveItem('news-one');
await window.selectSubscription('saved');
expect(document.getElementById('news-feed-content').textContent)
.toContain('No saved items');
await window.selectSubscription('all');
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
expect(document.querySelector('.ldr-feed-header h2').textContent).toBe('All News');
});
it('expands, collapses, and hides owned cards while keeping bulk counts current', async () => {
window.expandAll();
expect([...document.querySelectorAll('.ldr-news-item')].every(item => (
item.classList.contains('ldr-is-expanded')
))).toBe(true);
window.collapseAll();
expect([...document.querySelectorAll('.ldr-news-item')].every(item => (
!item.classList.contains('ldr-is-expanded')
))).toBe(true);
window.hideNewsItem('news-two');
expect(document.querySelector('[data-news-id="news-two"]').style.opacity)
.toBe('0');
await vi.advanceTimersByTimeAsync(300);
expect(document.querySelector('[data-news-id="news-two"]')).toBeNull();
expect(document.querySelector('.ldr-bulk-actions-bar').textContent)
.toContain('1 total');
await window.selectSubscription('all');
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
});
it('copies and exports canonical absolute URLs, falling back from unsafe sources', async () => {
clipboardWrite.mockClear();
window.copyNewsLink('news-one');
window.shareNews('news-two');
await Promise.resolve();
expect(clipboardWrite).toHaveBeenCalledWith(
'https://publisher.example/article?id=3299',
);
expect(clipboardWrite).toHaveBeenCalledWith(
`Older economy briefing\n\n${window.location.origin}/results/research%2Ftwo`,
);
createdBlob = null;
downloadedFilename = null;
window.exportToMarkdown('news-one');
expect(URL.createObjectURL).toHaveBeenCalledOnce();
expect(createdBlob.type).toBe('text/markdown');
expect(downloadedFilename)
.toBe('fastapi_migration_reaches_production.md');
expect(await createdBlob.text()).toContain(
'[View Full Report](https://publisher.example/article?id=3299)',
);
});
it('runs text search from the real mode controls and posts history with CSRF', async () => {
document.querySelector('[data-mode="text"]')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(document.getElementById('news-search-mode-btn').textContent)
.toContain('Text Only');
expect(document.getElementById('news-search').placeholder)
.toContain('Text:');
document.getElementById('news-search').value = 'FastAPI';
document.getElementById('search-btn').click();
await vi.waitFor(() => {
expect(fetchMock.mock.calls.some(([url]) => (
String(url).includes('focus=FastAPI')
))).toBe(true);
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(1);
});
const historyPost = fetchMock.mock.calls.find(([url, options]) => {
if (url !== '/news/api/search-history' || options.method !== 'POST') {
return false;
}
return JSON.parse(options.body).query === 'FastAPI';
});
expect(historyPost[1].credentials).toBe('same-origin');
expect(historyPost[1].headers['X-CSRFToken']).toBe('csrf-news-direct');
expect(JSON.parse(historyPost[1].body)).toMatchObject({
query: 'FastAPI',
type: 'filter',
resultCount: 2,
});
const clearSearchControl = document.querySelector(
'[data-news-page-action="clear-search"]',
);
expect(clearSearchControl).not.toBeNull();
expect(clearSearchControl.getAttribute('onclick')).toBeNull();
clearSearchControl.querySelector('i')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await vi.waitFor(() => {
expect(document.getElementById('news-search').value).toBe('');
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
});
});
it('does not let a history body captured before Clear All restore deleted rows', async () => {
const staleBody = deferred();
const originalFetch = fetchMock.getMockImplementation();
const originalConfirm = Object.getOwnPropertyDescriptor(window, 'confirm');
Object.defineProperty(window, 'confirm', {
configurable: true,
value: vi.fn(() => true),
});
const staleResponse = {
ok: true,
status: 200,
headers: {},
json: vi.fn(() => staleBody.promise),
text: vi.fn().mockResolvedValue(''),
};
fetchMock.mockImplementation((input, options = {}) => {
const url = String(input);
if (url === '/news/api/search-history' && options.method === 'GET') {
return Promise.resolve(staleResponse);
}
if (url === '/news/api/search-history' && options.method === 'DELETE') {
return Promise.resolve(response({ status: 'success' }));
}
return originalFetch(input, options);
});
try {
// This models the unawaited bootstrap load: its response has arrived,
// but decoding the pre-delete snapshot is still in flight.
const staleLoad = window.loadSearchHistory();
await vi.waitFor(() => expect(staleResponse.json).toHaveBeenCalledOnce());
await window.clearSearchHistory();
expect(document.getElementById('recent-searches').textContent)
.toContain('Your recent news searches will appear here');
staleBody.resolve({
search_history: [{
id: 'deleted-search',
query: 'This row was deleted',
type: 'quick',
timestamp: NOW.toISOString(),
resultCount: 1,
}],
});
await staleLoad;
expect(document.getElementById('recent-searches').textContent)
.not.toContain('This row was deleted');
expect(fetchMock).toHaveBeenCalledWith('/news/api/search-history', {
method: 'DELETE',
credentials: 'same-origin',
headers: { 'X-CSRFToken': 'csrf-news-direct' },
});
} finally {
fetchMock.mockImplementation(originalFetch);
if (originalConfirm) {
Object.defineProperty(window, 'confirm', originalConfirm);
} else {
delete window.confirm;
}
await window.loadSearchHistory();
}
});
it('drives the report-link, topic-clear, and recent-search public actions', async () => {
setReadState('news-one', true);
window.toggleReadStatus('news-one');
expect(document.querySelector('[data-news-id="news-one"]')
.classList.contains('ldr-is-unread')).toBe(true);
expect(window.markAsReadOnClick('news-one')).toBe(true);
expect(document.querySelector('[data-news-id="news-one"]')
.classList.contains('ldr-is-read')).toBe(true);
expect(JSON.parse(localStorage.getItem('news_read_ids'))).toContain('news-one');
window.filterByTopic('Economy');
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(1);
window.clearTopicFilter();
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
expect(document.querySelector('.ldr-active-filter-bar')).toBeNull();
const recentSearch = [...document.querySelectorAll('.ldr-recent-search-item')]
.find(item => item.querySelector('.ldr-search-query')?.textContent === (
'FastAPI migration'
));
expect(recentSearch).toBeDefined();
expect(recentSearch.getAttribute('onclick')).toBeNull();
expect(recentSearch.dataset).toMatchObject({
newsPageAction: 'rerun-search',
query: 'FastAPI migration',
searchType: 'table',
});
recentSearch.querySelector('.bi-arrow-repeat')
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await vi.waitFor(() => {
expect(document.getElementById('news-search').value)
.toBe('FastAPI migration');
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(1);
expect(document.querySelector('[data-news-id="news-one"]')).not.toBeNull();
});
document.getElementById('news-search').value = '';
document.getElementById('search-btn').click();
await vi.waitFor(() => {
expect(document.querySelectorAll('.ldr-news-item')).toHaveLength(2);
});
});
it('falls back to the temporary-textarea copy path after clipboard rejection', async () => {
clipboardWrite.mockRejectedValueOnce(new Error('clipboard denied'));
const execCommand = vi.fn(() => true);
Object.defineProperty(document, 'execCommand', {
configurable: true,
value: execCommand,
});
window.copyNewsLink('news-one');
await Promise.resolve();
await Promise.resolve();
expect(execCommand).toHaveBeenCalledWith('copy');
expect(document.querySelector('textarea[style*="position: fixed"]')).toBeNull();
expect(document.getElementById('news-alert').textContent)
.toBe('Copied to clipboard');
});
it('loads and copies the checked-in query template through public controls', async () => {
window.loadNewsTableQuery();
const template = document.getElementById('query-template');
expect(template.style.display).toBe('block');
expect(template.textContent).toContain('DIVERSITY IS MANDATORY');
window.useQueryTemplate();
expect(document.getElementById('news-query').value)
.toContain('START YOUR RESPONSE DIRECTLY WITH THE TABLE');
expect(template.style.display).toBe('none');
expect(document.getElementById('table-view-toggle').checked).toBe(true);
clipboardWrite.mockClear();
await window.copyQueryTemplate();
expect(clipboardWrite).toHaveBeenCalledWith(
expect.stringContaining('PRIORITIZE BY REAL-WORLD IMPACT'),
);
});
it('loads safe folder options and submits the template modal through the migrated create contract', async () => {
document.getElementById('create-subscription-btn').click();
await vi.waitFor(() => {
expect(document.querySelectorAll('#news-subscription-folder option'))
.toHaveLength(2);
});
expect(document.getElementById('newsSubscriptionModal').dataset.shown)
.toBe('true');
const folderOption = document.querySelector(
'#news-subscription-folder option[value="folder-3299"]',
);
expect(folderOption.textContent).toContain('Migration');
expect(folderOption.querySelector('img')).toBeNull();
expect(window.__newsActionXss).toBeUndefined();
document.getElementById('news-subscription-query').value = 'Migration releases';
document.getElementById('news-subscription-name').value = 'Release watch';
document.getElementById('news-subscription-folder').value = 'folder-3299';
document.getElementById('news-subscription-model').value = 'OLLAMA:llama3';
document.getElementById('news-subscription-strategy').value = 'source-based';
await window.handleNewsSubscriptionSubmit(new Event('submit'));
const createCall = fetchMock.mock.calls.find(([url, options = {}]) => {
if (url !== '/news/api/subscribe' || options.method !== 'POST') {
return false;
}
return JSON.parse(options.body).name === 'Release watch';
});
expect(createCall).toBeDefined();
expect(createCall[1].headers['X-CSRFToken']).toBe('csrf-news-direct');
expect(JSON.parse(createCall[1].body)).toEqual({
query: 'Migration releases',
name: 'Release watch',
subscription_type: 'search',
refresh_minutes: 240,
folder_id: 'folder-3299',
is_active: true,
model_provider: 'OLLAMA',
model: 'llama3',
search_strategy: 'source-based',
});
expect(document.getElementById('newsSubscriptionModal').dataset.hidden)
.toBe('true');
expect(document.getElementById('news-alert').textContent)
.toBe('Subscription created successfully!');
});
it('builds an encoded migrated subscription-form URL from a rendered news item', () => {
window.createSubscriptionFromItem('news-one');
const target = new URL(window.location.href);
expect(target.pathname).toBe('/news/subscriptions/new');
expect(target.searchParams.get('query'))
.toBe('FastAPI migration reaches production');
expect(target.searchParams.get('name'))
.toBe('Subscription: FastAPI migration reaches production...');
expect(target.searchParams.get('research_id')).toBe('research-one');
history.replaceState({}, '', '/');
window.createSubscriptionFromItem('missing-news');
expect(document.getElementById('news-alert').textContent)
.toBe('News item not found');
});
it('routes checked-in and prompted templates to the encoded subscription form', () => {
window.useNewsTemplate('breaking-news');
let target = new URL(window.location.href);
expect(target.pathname).toBe('/news/subscriptions/new');
expect(target.searchParams.get('template')).toBe('breaking-news');
expect(target.searchParams.get('name')).toContain('dynamic dates');
expect(target.searchParams.get('query')).toContain('YYYY-MM-DD');
history.replaceState({}, '', '/');
vi.stubGlobal('prompt', vi.fn(() => '<script>Quantum policy</script>'));
window.useNewsTemplate('topic-news');
target = new URL(window.location.href);
expect(target.pathname).toBe('/news/subscriptions/new');
expect(target.searchParams.get('template')).toBe('topic-news');
expect(target.searchParams.get('query'))
.toContain('<script>Quantum policy</script>');
expect(window.__newsActionXss).toBeUndefined();
history.replaceState({}, '', '/');
window.useNewsTemplate('custom');
expect(window.location.pathname).toBe('/news/subscriptions/new');
expect(window.location.search).toBe('');
history.replaceState({}, '', '/');
});
it('starts and stops auto-refresh without replacing an active text search', async () => {
document.getElementById('table-view-toggle').checked = false;
const toggle = document.getElementById('auto-refresh');
const feedCallCount = () => fetchMock.mock.calls.filter(([url]) => (
String(url).startsWith('/news/api/feed?')
)).length;
const beforeRefresh = feedCallCount();
toggle.checked = true;
toggle.dispatchEvent(new Event('change'));
await vi.advanceTimersByTimeAsync(5 * 60 * 1000);
await vi.waitFor(() => expect(feedCallCount()).toBeGreaterThan(beforeRefresh));
expect(document.querySelector('label[for="auto-refresh"]').textContent)
.toContain('Auto-refresh');
document.getElementById('news-search').value = 'do not replace this search';
const withSearch = feedCallCount();
await vi.advanceTimersByTimeAsync(5 * 60 * 1000);
expect(feedCallCount()).toBe(withSearch);
toggle.checked = false;
toggle.dispatchEvent(new Event('change'));
document.getElementById('news-search').value = '';
});
it('refreshes from the real template button without relying on inline JavaScript', async () => {
document.getElementById('news-search').value = '';
const feedCallCount = () => fetchMock.mock.calls.filter(([url]) => (
String(url).startsWith('/news/api/feed?')
)).length;
const beforeRefresh = feedCallCount();
document.getElementById('refresh-feed-btn').click();
await vi.waitFor(() => expect(feedCallCount()).toBeGreaterThan(beforeRefresh));
expect(document.getElementById('news-alert').textContent).toBe('Feed refreshed');
});
it.each([
['OPENAI:gpt-4.1-mini', 'OPENAI', 'gpt-4.1-mini'],
['QWEN:qwen3.8-max', 'QWEN', 'qwen3.8-max'],
['OLLAMA:llama3.1:8b', 'OLLAMA', 'llama3.1:8b'],
// The field is free text: only an uppercase key (the form the dropdown's
// options use) is taken as a provider; a lowercase one is part of a tag.
['ollama:llama3.1:8b', undefined, 'ollama:llama3.1:8b'],
['qwen:7b', undefined, 'qwen:7b'],
['llama3.1:8b', undefined, 'llama3.1:8b'],
['custom:model:tag', undefined, 'custom:model:tag'],
['OLLAMA:OPENAI:model:tag', 'OLLAMA', 'OPENAI:model:tag'],
['gpt-4.1-mini', undefined, 'gpt-4.1-mini'],
['', undefined, undefined],
])('sends the native model for selection %s', async (selected, provider, model) => {
const query = `Run Once ${selected}`;
document.getElementById('news-subscription-query').value = query;
document.getElementById('news-subscription-model').value = selected;
document.getElementById('run-template-btn').click();
await vi.waitFor(() => {
const request = fetchMock.mock.calls.find(([url, options = {}]) => (
url === '/api/start_research' && JSON.parse(options.body).query === query
));
expect(request).toBeDefined();
const payload = JSON.parse(request[1].body);
expect(payload.model_provider).toBe(provider);
expect(payload.model).toBe(model);
});
});
it('runs advanced news and creates its follow-on subscription through migrated contracts', async () => {
const query = 'FastAPI follow-on subscriptions';
const model = 'OPENAI:gpt-4.1-mini';
const currentSubscriptionsBefore = fetchMock.mock.calls.filter(([url]) => (
url === '/news/api/subscriptions/current'
)).length;
document.getElementById('news-subscription-query').value = query;
document.getElementById('news-subscription-model').value = model;
document.getElementById('run-template-btn').click();
await vi.waitFor(() => {
const subscribeCall = fetchMock.mock.calls.find(([url, options = {}]) => {
if (url !== '/news/api/subscribe' || options.method !== 'POST') return false;
return JSON.parse(options.body).metadata?.is_advanced_query === true;
});
expect(subscribeCall).toBeDefined();
expect(fetchMock.mock.calls.filter(([url]) => (
url === '/news/api/subscriptions/current'
))).toHaveLength(currentSubscriptionsBefore + 1);
});
expect(fetchMock).toHaveBeenCalledWith('/api/start_research', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-news-direct',
},
body: JSON.stringify({
query,
mode: 'quick',
strategy: 'source-based',
metadata: {
is_news_search: true,
search_type: 'news_analysis',
display_in: 'news_feed',
triggered_by: 'test_run',
},
model_provider: 'OPENAI',
model: 'gpt-4.1-mini',
}),
});
expect(fetchMock).toHaveBeenCalledWith('/news/api/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-news-direct',
},
body: JSON.stringify({
query,
subscription_type: 'search',
refresh_minutes: 60,
metadata: {
research_id: 'advanced-news-3299',
is_advanced_query: true,
},
}),
});
expect(document.getElementById('news-alert').textContent)
.toBe('Advanced news subscription created!');
});
it('coalesces rapid Run Once clicks into one advanced research request', async () => {
const startRequest = deferred();
const originalFetch = fetchMock.getMockImplementation();
const startCallCount = () => fetchMock.mock.calls.filter(([url, options = {}]) => (
url === '/api/start_research' && options.method === 'POST'
)).length;
const subscriptionCallCount = () => fetchMock.mock.calls.filter(([url, options = {}]) => (
url === '/news/api/subscribe' && options.method === 'POST' &&
JSON.parse(options.body).metadata?.is_advanced_query === true
)).length;
const startsBefore = startCallCount();
const subscriptionsBefore = subscriptionCallCount();
fetchMock.mockImplementation((input, options = {}) => {
if (String(input) === '/api/start_research' && options.method === 'POST') {
return startRequest.promise;
}
return originalFetch(input, options);
});
try {
document.getElementById('news-subscription-query').value = (
'One owned advanced migration search'
);
const runButton = document.getElementById('run-template-btn');
runButton.click();
runButton.click();
await vi.waitFor(() => expect(startCallCount()).toBe(startsBefore + 1));
expect(subscriptionCallCount()).toBe(subscriptionsBefore);
startRequest.resolve(response({
status: 'queued',
research_id: 'single-flight-news-3299',
}));
await vi.waitFor(() => {
expect(subscriptionCallCount()).toBe(subscriptionsBefore + 1);
});
expect(startCallCount()).toBe(startsBefore + 1);
const newSubscriptionCalls = fetchMock.mock.calls.filter(([url, options = {}]) => (
url === '/news/api/subscribe' && options.method === 'POST' &&
JSON.parse(options.body).metadata?.research_id === 'single-flight-news-3299'
));
expect(newSubscriptionCalls).toHaveLength(1);
} finally {
fetchMock.mockImplementation(originalFetch);
}
});
it('keeps distinct Run Once queries concurrent', async () => {
const firstStart = deferred();
const secondStart = deferred();
const originalFetch = fetchMock.getMockImplementation();
const firstQuery = 'First concurrent migration query';
const secondQuery = 'Second concurrent migration query';
const startsBefore = fetchMock.mock.calls.filter(([url, options = {}]) => (
url === '/api/start_research' && options.method === 'POST'
)).length;
fetchMock.mockImplementation((input, options = {}) => {
if (String(input) === '/api/start_research' && options.method === 'POST') {
const { query } = JSON.parse(options.body);
if (query === firstQuery) return firstStart.promise;
if (query === secondQuery) return secondStart.promise;
}
return originalFetch(input, options);
});
try {
const queryInput = document.getElementById('news-subscription-query');
const runButton = document.getElementById('run-template-btn');
queryInput.value = firstQuery;
runButton.click();
queryInput.value = secondQuery;
runButton.click();
await vi.waitFor(() => {
const newStartCalls = fetchMock.mock.calls
.filter(([url, options = {}]) => (
url === '/api/start_research' && options.method === 'POST'
))
.slice(startsBefore);
expect(newStartCalls).toHaveLength(2);
expect(newStartCalls.map(([, options]) => (
JSON.parse(options.body).query
))).toEqual([firstQuery, secondQuery]);
});
secondStart.resolve(response({
status: 'queued',
research_id: 'second-concurrent-news-3299',
}));
firstStart.resolve(response({
status: 'queued',
research_id: 'first-concurrent-news-3299',
}));
await vi.waitFor(() => {
const researchIds = fetchMock.mock.calls
.filter(([url, options = {}]) => (
url === '/news/api/subscribe' && options.method === 'POST'
))
.map(([, options]) => JSON.parse(options.body).metadata?.research_id);
expect(researchIds).toContain('first-concurrent-news-3299');
expect(researchIds).toContain('second-concurrent-news-3299');
});
} finally {
fetchMock.mockImplementation(originalFetch);
}
});