~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
/**
* Tests for followup.js — FollowUpResearch.getResearchIdFromPage.
*
* Extracts the parent research ID from one of four fallback sources:
* 1. URL path segment (/results/<id>)
* 2. URL query param (?research_id=<id>)
* 3. DOM data-research-id attribute
* 4. window.currentResearchId
*
* Each test isolates exactly one source so the precedence order doesn't
* have to be re-derived from the test setup.
*/
import { readFileSync } from 'node:fs';
import { resolve as resolvePath } from 'node:path';
const FOLLOWUP_TEMPLATE = readFileSync(resolvePath(
__dirname,
'../../src/local_deep_research/web/static/templates/followup_modal.html',
), 'utf8');
let FollowUpResearch;
beforeAll(async () => {
// followup.js auto-constructs an instance and binds a DOMContentLoaded
// listener that fetches /static/templates/followup_modal.html. The
// DOMContentLoaded event has already fired in happy-dom by the time
// import settles, so the listener never runs — but stub fetch
// defensively in case ordering changes.
globalThis.fetch = vi.fn(() =>
Promise.resolve({ ok: false, status: 404, text: () => Promise.resolve('') })
);
await import('@js/followup.js');
FollowUpResearch = window.FollowUpResearch;
});
function setLocation(pathname, search = '') {
Object.defineProperty(window, 'location', {
configurable: true,
writable: true,
value: {
pathname,
search,
hash: '',
href: pathname + search,
host: 'localhost',
protocol: 'http:',
},
});
}
describe('FollowUpResearch.getResearchIdFromPage', () => {
afterEach(() => {
document.body.innerHTML = '';
delete window.currentResearchId;
});
it('extracts the id from /results/<id> in the URL path', () => {
setLocation('/results/abc-123-def');
const fr = new FollowUpResearch();
expect(fr.getResearchIdFromPage()).toBe('abc-123-def');
});
it('falls through to the query string when the path does not match', () => {
setLocation('/somewhere-else', '?research_id=xyz-789');
const fr = new FollowUpResearch();
expect(fr.getResearchIdFromPage()).toBe('xyz-789');
});
it('falls through to a [data-research-id] DOM attribute', () => {
setLocation('/somewhere-else', '');
const el = document.createElement('div');
el.dataset.researchId = 'data-id-456';
document.body.appendChild(el);
const fr = new FollowUpResearch();
expect(fr.getResearchIdFromPage()).toBe('data-id-456');
});
it('falls through to window.currentResearchId as the last resort', () => {
setLocation('/somewhere-else', '');
window.currentResearchId = 'window-id-999';
const fr = new FollowUpResearch();
expect(fr.getResearchIdFromPage()).toBe('window-id-999');
});
it('returns null when none of the four sources have a value', () => {
setLocation('/somewhere-else', '');
// No DOM element, no window.currentResearchId.
const fr = new FollowUpResearch();
expect(fr.getResearchIdFromPage()).toBeNull();
});
it('prefers the URL path over the query string (precedence smoke test)', () => {
setLocation('/results/from-path', '?research_id=from-query');
const fr = new FollowUpResearch();
expect(fr.getResearchIdFromPage()).toBe('from-path');
});
});
describe('FollowUpResearch FastAPI contracts', () => {
beforeEach(() => {
setLocation('/results/parent-123');
window.api = { getCsrfToken: vi.fn(() => 'csrf-followup') };
});
afterEach(() => {
vi.restoreAllMocks();
delete window.api;
delete window.ui;
delete window.bootstrap;
document.body.innerHTML = '';
});
it('prepares and renders the parent context from the migrated response envelope', async () => {
document.body.innerHTML = `
<section id="parentContext" style="display: none"></section>
<div id="parentSummary"></div>
<div id="parentSources"></div>
`;
globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({
success: true,
parent_summary: 'The original migration report',
available_sources: 7,
}), { status: 200 }));
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
await followup.loadParentContext();
expect(globalThis.fetch).toHaveBeenCalledWith(
'/api/followup/prepare',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-followup',
},
body: JSON.stringify({
parent_research_id: 'parent-123',
question: 'test',
}),
},
);
expect(document.getElementById('parentContext').style.display)
.toBe('block');
expect(document.getElementById('parentSummary').textContent)
.toBe('The original migration report');
expect(document.getElementById('parentSources').textContent).toBe('7');
});
it('starts a follow-up with CSRF and consumes the success/research_id envelope', async () => {
document.body.innerHTML = `
<textarea id="followUpQuestion">What changed?</textarea>
<div id="followup-error-container"></div>
<div id="followUpModal"></div>
`;
const modal = { hide: vi.fn() };
window.bootstrap = {
Modal: { getInstance: vi.fn(() => modal) },
};
window.ui = {
clearInlineError: vi.fn(),
showInlineError: vi.fn(),
};
globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({
success: true,
research_id: 'followup-456',
}), { status: 200 }));
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
followup.modalElement = document.getElementById('followUpModal');
await followup.submitFollowUp();
expect(globalThis.fetch).toHaveBeenCalledWith(
'/api/followup/start',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-followup',
},
body: JSON.stringify({
parent_research_id: 'parent-123',
question: 'What changed?',
}),
},
);
expect(modal.hide).toHaveBeenCalledOnce();
expect(window.location.href).toBe('/progress/followup-456');
expect(window.ui.showInlineError).not.toHaveBeenCalled();
});
});
describe('FollowUpResearch browser lifecycle and failure recovery', () => {
beforeEach(() => {
setLocation('/results/parent-123');
window.api = { getCsrfToken: vi.fn(() => 'csrf-followup') };
window.ui = {
clearInlineError: vi.fn(),
showInlineError: vi.fn(),
};
window.alert = vi.fn();
globalThis.fetch = vi.fn();
});
afterEach(() => {
vi.restoreAllMocks();
document.body.innerHTML = '';
document.head.querySelector('#followup-modal-styles')?.remove();
delete window.api;
delete window.ui;
delete window.bootstrap;
delete window.alert;
});
it('disables the entry point when no research ID owns the page', () => {
setLocation('/library');
document.body.innerHTML = '<button id="ask-followup-btn"></button>';
const followup = new FollowUpResearch();
followup.createModal = vi.fn();
followup.init();
const button = document.getElementById('ask-followup-btn');
expect(button.disabled).toBe(true);
expect(button.title).toBe('No research ID available');
expect(followup.createModal).toHaveBeenCalledOnce();
});
it('wires the results-page entry point to the owning research instance', () => {
document.body.innerHTML = '<button id="ask-followup-btn"></button>';
const followup = new FollowUpResearch();
followup.createModal = vi.fn();
followup.showFollowUpModal = vi.fn();
followup.init();
document.getElementById('ask-followup-btn').click();
expect(followup.parentResearchId).toBe('parent-123');
expect(followup.showFollowUpModal).toHaveBeenCalledOnce();
expect(followup.createModal).toHaveBeenCalledOnce();
});
it('loads the checked-in modal template and owns its start and close controls', async () => {
document.body.innerHTML = '<button id="ask-followup-btn"></button>';
globalThis.fetch.mockResolvedValue({
ok: true,
text: () => Promise.resolve(FOLLOWUP_TEMPLATE),
});
const followup = new FollowUpResearch();
await followup.createModal();
followup.modalElement.dispatchEvent(new Event('hidden.bs.modal'));
followup.addModalStyles();
expect(globalThis.fetch)
.toHaveBeenCalledWith('/static/templates/followup_modal.html');
expect(followup.modalElement.id).toBe('followUpModal');
expect(followup.getStartButton())
.toBe(followup.modalElement.querySelector('.modal-footer .btn-primary'));
expect(followup.getStartButton().hasAttribute('onclick')).toBe(false);
followup.submitFollowUp = vi.fn();
followup.getStartButton().click();
expect(followup.submitFollowUp).toHaveBeenCalledOnce();
expect(window.ui.clearInlineError)
.toHaveBeenCalledWith('followup-error-container');
expect(document.querySelectorAll('#followup-modal-styles')).toHaveLength(1);
});
it('aborts showing after a visible template-load failure', async () => {
document.body.innerHTML = '<button id="ask-followup-btn"></button>';
globalThis.fetch.mockResolvedValue({ ok: false, status: 503 });
const errorSpy = vi.spyOn(SafeLogger, 'error');
const followup = new FollowUpResearch();
await followup.showFollowUpModal();
const button = document.getElementById('ask-followup-btn');
expect(button.disabled).toBe(true);
expect(button.title).toBe('Failed to load follow-up modal template');
expect(window.alert).toHaveBeenCalledWith(
'Unable to load follow-up interface. Please refresh the page and try again.',
);
expect(errorSpy).toHaveBeenCalledWith(
'Error loading follow-up modal template:',
expect.objectContaining({ message: 'Failed to load modal template: 503' }),
);
expect(followup.modalElement).toBeNull();
expect(followup.modalLoadPromise).toBeNull();
});
it('adopts an already-rendered modal without fetching a second copy', async () => {
document.body.innerHTML = '<div id="followUpModal"></div>';
const existing = document.getElementById('followUpModal');
const followup = new FollowUpResearch();
await followup.createModal();
expect(followup.modalElement).toBe(existing);
expect(globalThis.fetch).not.toHaveBeenCalled();
});
it('loads parent context before displaying the Bootstrap modal', async () => {
document.body.innerHTML = '<div id="followUpModal"></div>';
const show = vi.fn();
window.bootstrap = {
Modal: vi.fn(function Modal() {
return { show };
}),
};
const followup = new FollowUpResearch();
followup.modalElement = document.getElementById('followUpModal');
followup.loadParentContext = vi.fn().mockResolvedValue(undefined);
await followup.showFollowUpModal();
expect(followup.loadParentContext).toHaveBeenCalledOnce();
expect(window.bootstrap.Modal)
.toHaveBeenCalledWith(followup.modalElement);
expect(show).toHaveBeenCalledOnce();
});
it('contains a failed parent-context request so the modal remains usable', async () => {
document.body.innerHTML = `
<section id="parentContext" style="display: none"></section>
<div id="parentSummary"></div>
<div id="parentSources"></div>
`;
globalThis.fetch.mockRejectedValue(new Error('context unavailable'));
const errorSpy = vi.spyOn(SafeLogger, 'error');
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
await expect(followup.loadParentContext()).resolves.toBeUndefined();
expect(document.getElementById('parentContext').style.display)
.toBe('none');
expect(errorSpy).toHaveBeenCalledWith(
'Error loading parent context:',
expect.objectContaining({ message: 'context unavailable' }),
);
});
it('shares an eager modal load with an immediate show request', async () => {
let finishFetch;
globalThis.fetch.mockImplementation(() => new Promise((resolve) => {
finishFetch = resolve;
}));
const show = vi.fn();
window.bootstrap = {
Modal: vi.fn(function Modal() {
return { show };
}),
};
const followup = new FollowUpResearch();
followup.loadParentContext = vi.fn().mockResolvedValue(undefined);
const eagerLoad = followup.createModal();
const immediateShow = followup.showFollowUpModal();
expect(globalThis.fetch).toHaveBeenCalledOnce();
finishFetch({
ok: true,
text: () => Promise.resolve(`
<div id="followUpModal">
<div class="modal-footer">
<button class="btn btn-primary"></button>
</div>
<div id="followup-error-container"></div>
</div>
`),
});
await Promise.all([eagerLoad, immediateShow]);
expect(document.querySelectorAll('#followUpModal')).toHaveLength(1);
expect(followup.modalElement)
.toBe(document.getElementById('followUpModal'));
expect(followup.loadParentContext).toHaveBeenCalledOnce();
expect(show).toHaveBeenCalledOnce();
expect(followup.modalLoadPromise).toBeNull();
});
it('rejects empty questions and missing ownership before calling FastAPI', async () => {
document.body.innerHTML = `
<textarea id="followUpQuestion"> </textarea>
<div id="followup-error-container"></div>
`;
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
await followup.submitFollowUp();
document.getElementById('followUpQuestion').value = 'What next?';
followup.parentResearchId = null;
await followup.submitFollowUp();
expect(globalThis.fetch).not.toHaveBeenCalled();
expect(window.ui.showInlineError.mock.calls).toEqual([
['followup-error-container', 'Please enter a follow-up question'],
['followup-error-container', 'No parent research ID available'],
]);
});
it('surfaces a non-2xx response and re-enables the start action for retry', async () => {
document.body.innerHTML = `
<div id="followUpModal">
<textarea id="followUpQuestion">What next?</textarea>
<div class="modal-footer">
<button class="btn btn-primary"></button>
</div>
<div id="followup-error-container"></div>
</div>
`;
globalThis.fetch.mockResolvedValue(new Response('backend unavailable', {
status: 503,
statusText: 'Unavailable',
}));
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
followup.modalElement = document.getElementById('followUpModal');
await followup.submitFollowUp();
expect(window.ui.showInlineError).toHaveBeenCalledWith(
'followup-error-container',
'Failed to start follow-up research: HTTP error! status: 503',
);
expect(document.querySelector('.modal-footer .btn-primary').disabled)
.toBe(false);
expect(followup.submitInProgress).toBe(false);
});
it('surfaces a non-2xx FastAPI error body and keeps the action retryable', async () => {
document.body.innerHTML = `
<div id="followUpModal">
<textarea id="followUpQuestion">What next?</textarea>
<div class="modal-footer">
<button class="btn btn-primary"></button>
</div>
<div id="followup-error-container"></div>
</div>
`;
globalThis.fetch.mockResolvedValue(new Response(JSON.stringify({
success: false,
error: 'Server is at research capacity. Please retry shortly.',
}), {
status: 429,
statusText: 'Too Many Requests',
}));
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
followup.modalElement = document.getElementById('followUpModal');
await followup.submitFollowUp();
expect(window.ui.showInlineError).toHaveBeenCalledWith(
'followup-error-container',
'Failed to start follow-up research: ' +
'Server is at research capacity. Please retry shortly.',
);
expect(document.querySelector('.modal-footer .btn-primary').disabled)
.toBe(false);
expect(followup.submitInProgress).toBe(false);
});
it.each([
['missing', undefined],
['blank', ' '],
['non-finite', Number.POSITIVE_INFINITY],
])('keeps a malformed success with a %s research ID retryable', async (
_label,
researchId,
) => {
document.body.innerHTML = `
<div id="followUpModal">
<textarea id="followUpQuestion">What next?</textarea>
<div class="modal-footer">
<button class="btn btn-primary"></button>
</div>
<div id="followup-error-container"></div>
</div>
`;
const modal = { hide: vi.fn() };
window.bootstrap = {
Modal: { getInstance: vi.fn(() => modal) },
};
globalThis.fetch.mockResolvedValue({
ok: true,
status: 200,
json: vi.fn().mockResolvedValue({
success: true,
research_id: researchId,
}),
});
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
followup.modalElement = document.getElementById('followUpModal');
const originalLocation = window.location.href;
await followup.submitFollowUp();
expect(modal.hide).not.toHaveBeenCalled();
expect(window.location.href).toBe(originalLocation);
expect(window.ui.showInlineError).toHaveBeenCalledWith(
'followup-error-container',
'Error starting follow-up research: ' +
'Response did not include a valid research ID',
);
expect(document.querySelector('.modal-footer .btn-primary').disabled)
.toBe(false);
expect(followup.submitInProgress).toBe(false);
});
it('encodes a valid returned research ID as one progress path segment', async () => {
document.body.innerHTML = `
<div id="followUpModal">
<textarea id="followUpQuestion">What next?</textarea>
<div class="modal-footer">
<button class="btn btn-primary"></button>
</div>
<div id="followup-error-container"></div>
</div>
`;
const modal = { hide: vi.fn() };
window.bootstrap = {
Modal: { getInstance: vi.fn(() => modal) },
};
globalThis.fetch.mockResolvedValue({
ok: true,
status: 200,
json: vi.fn().mockResolvedValue({
success: true,
research_id: 'followup/3299?source=parent',
}),
});
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
followup.modalElement = document.getElementById('followUpModal');
await followup.submitFollowUp();
expect(modal.hide).toHaveBeenCalledOnce();
expect(window.location.href)
.toBe('/progress/followup%2F3299%3Fsource%3Dparent');
expect(window.ui.showInlineError).not.toHaveBeenCalled();
});
it('owns an in-flight start request so a double-click cannot create two child researches', async () => {
document.body.innerHTML = `
<div id="followUpModal">
<textarea id="followUpQuestion">What next?</textarea>
<div class="modal-footer">
<button class="btn btn-primary"></button>
</div>
<div id="followup-error-container"></div>
</div>
`;
let finishRequest;
globalThis.fetch.mockImplementationOnce(() => new Promise((resolve) => {
finishRequest = resolve;
}));
const followup = new FollowUpResearch();
followup.parentResearchId = 'parent-123';
followup.modalElement = document.getElementById('followUpModal');
const first = followup.submitFollowUp();
const duplicate = followup.submitFollowUp();
expect(globalThis.fetch).toHaveBeenCalledOnce();
expect(document.querySelector('.modal-footer .btn-primary').disabled)
.toBe(true);
finishRequest(new Response(JSON.stringify({
success: false,
error: 'parent is no longer available',
}), { status: 200 }));
await Promise.all([first, duplicate]);
expect(window.ui.showInlineError).toHaveBeenCalledWith(
'followup-error-container',
'Error starting follow-up research: parent is no longer available',
);
expect(document.querySelector('.modal-footer .btn-primary').disabled)
.toBe(false);
expect(followup.submitInProgress).toBe(false);
globalThis.fetch.mockResolvedValueOnce(new Response(JSON.stringify({
success: false,
}), { status: 200 }));
await followup.submitFollowUp();
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
expect(window.ui.showInlineError).toHaveBeenLastCalledWith(
'followup-error-container',
'Error starting follow-up research: Unknown error',
);
});
});