~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 collection_details.js — provider labels, document rendering,
* and indexing-failure UI.
*
* Covers:
* - getProviderLabel (pure helper, public mapping)
* - document size rendering (shared byte formatter, no local classic-script
* global shadow)
* - renderIndexingFailure (trims trailing period, uses textContent)
* - hideProgressUI (auto-hides after 5s when keepVisible=false,
* stays visible when keepVisible=true)
* - serialized ownership for collection boolean settings
* - single-flight indexing-status polling and restart ownership
*
* The 5-second auto-hide is pinned because PR #5235 review comment
* 5085604502 flagged that cancellation was leaving the UI visible
* indefinitely; only the failure path should keep it visible.
*/
import { resolve as resolvePath } from 'node:path';
import { compileTemplateHarness } from './helpers/template-harness.js';
const SOURCE_PATH = resolvePath(
__dirname,
'../../src/local_deep_research/web/static/js/collection_details.js',
);
function compileCollectionSearchRuntime({ SafeLogger = { error: vi.fn() } } = {}) {
const runtime = compileTemplateHarness({
templatePath: SOURCE_PATH,
functionNames: ['initCollectionSearch', 'searchCollection'],
dependencies: {
COLLECTION_ID: 'collection-search-3299',
SafeLogger,
escapeHtml: value => String(value),
initialDocuments: [{ id: 'indexed-document', indexed: true }],
initialNotes: [],
},
preamble: `
let documentsData = initialDocuments;
let notesData = initialNotes;
let searchDebounceTimer = null;
let searchListenersAttached = false;
let collectionSearchId = 0;
`,
returnExpression: '({ initCollectionSearch, searchCollection })',
});
return { runtime, SafeLogger };
}
let getProviderLabel;
let renderIndexingFailure;
let checkAndResumeIndexing;
let showProgressUI;
let hideProgressUI;
let startPolling;
let updateCollectionIsPublic;
let updateCollectionAgentEnabled;
beforeAll(async () => {
globalThis.fetch = vi.fn(() =>
Promise.resolve({ ok: true, json: () => Promise.resolve({}) })
);
await import('@js/utils/format-bytes.js');
// RESEARCH_STATUS / RESEARCH_TERMINAL_STATES are normally injected
// from Python via base.html. Provide stubs so the constants module's
// ``window.ResearchStates`` is fully functional.
window.RESEARCH_STATUS = {
IN_PROGRESS: 'in_progress',
COMPLETED: 'completed',
FAILED: 'failed',
SUSPENDED: 'suspended',
CANCELLED: 'cancelled',
QUEUED: 'queued',
PENDING: 'pending',
ERROR: 'error',
};
window.RESEARCH_TERMINAL_STATES = new Set([
'completed', 'suspended', 'failed', 'error', 'cancelled',
]);
await import('@js/security/xss-protection.js');
await import('@js/config/constants.js');
await import('@js/collection_details.js');
getProviderLabel = window.getProviderLabel;
renderIndexingFailure = window.renderIndexingFailure;
checkAndResumeIndexing = window.checkAndResumeIndexing;
showProgressUI = window.showProgressUI;
hideProgressUI = window.hideProgressUI;
startPolling = window.startPolling;
updateCollectionIsPublic = window.updateCollectionIsPublic;
updateCollectionAgentEnabled = window.updateCollectionAgentEnabled;
});
describe('getProviderLabel', () => {
it('maps known provider keys to their friendly labels', () => {
expect(getProviderLabel('sentence_transformers')).toBe('Sentence Transformers');
expect(getProviderLabel('ollama')).toBe('Ollama');
expect(getProviderLabel('openai')).toBe('OpenAI');
expect(getProviderLabel('anthropic')).toBe('Anthropic');
expect(getProviderLabel('cohere')).toBe('Cohere');
});
it('returns the input verbatim for unknown keys (so the UI shows the raw value)', () => {
expect(getProviderLabel('huggingface')).toBe('huggingface');
expect(getProviderLabel('local-custom-provider')).toBe('local-custom-provider');
});
it('falls back to "Not configured" for null', () => {
expect(getProviderLabel(null)).toBe('Not configured');
});
it('falls back to "Not configured" for undefined', () => {
expect(getProviderLabel(undefined)).toBe('Not configured');
});
it('falls back to "Not configured" for the empty string', () => {
expect(getProviderLabel('')).toBe('Not configured');
});
});
describe('document size rendering', () => {
afterEach(() => {
vi.unstubAllGlobals();
document.body.replaceChildren();
});
it('renders a 1 TB document with the shared formatter', async () => {
document.body.innerHTML = `
<button id="index-collection-btn"></button>
<button id="reindex-collection-btn"></button>
<button id="delete-collection-btn"></button>
<button id="cancel-indexing-btn"></button>
<input id="collection-is-public" type="checkbox">
<input id="collection-agent-enabled" type="checkbox">
<div id="collection-name"></div>
<div id="collection-description"></div>
<div id="stat-total-docs"></div>
<div id="stat-indexed-docs"></div>
<div id="stat-unindexed-docs"></div>
<div id="stat-total-chunks"></div>
<div id="collection-embedding-info"></div>
<div id="documents-list"></div>
<div id="no-documents-message"></div>
<div id="notes-section"></div>
<div id="notes-list"></div>
<div id="collection-search-section"></div>
`;
vi.stubGlobal('COLLECTION_ID', 'test-collection');
vi.stubGlobal('URLS', {
LIBRARY_API: {
COLLECTION_DOCUMENTS: '/library/api/collections/documents',
},
});
vi.stubGlobal('URLBuilder', {
build: (path, collectionId) => `${path}/${collectionId}`,
});
vi.stubGlobal('escapeHtml', value => String(value));
vi.stubGlobal('safeFetch', vi.fn(url => Promise.resolve({
ok: true,
json: () => Promise.resolve(
url.endsWith('/index/status')
? { status: 'idle' }
: {
success: true,
collection: {
name: 'Large files',
description: '',
embedding_model: null,
is_protected: false,
},
documents: [{
id: 'large-document',
filename: 'large.bin',
file_size: 1024 ** 4,
indexed: false,
chunk_count: 0,
has_pdf: false,
has_text_db: false,
in_other_collections: false,
}],
notes: [],
}
),
})));
document.dispatchEvent(new Event('DOMContentLoaded'));
await vi.waitFor(() => {
expect(document.getElementById('documents-list').textContent)
.toContain('Size: 1 TB');
});
});
});
describe('renderIndexingFailure', () => {
beforeEach(() => {
document.body.innerHTML = '';
});
function setupDom() {
const progressLog = document.createElement('div');
progressLog.id = 'progress-log';
document.body.appendChild(progressLog);
const progressText = document.createElement('div');
progressText.id = 'progress-text';
document.body.appendChild(progressText);
return { progressLog, progressText };
}
it('trims a single trailing period before appending durable text', () => {
const { progressLog, progressText } = setupDom();
renderIndexingFailure({
error_message: 'Disk full.',
result: {
durable_indexed_documents: 3,
durable_indexed_chunks: 12,
},
});
const firstEntry = progressLog.firstChild;
expect(firstEntry.textContent).toBe(
'Indexing failed: Disk full Durable vector store: 3 document(s), 12 chunk(s).'
);
expect(progressText.textContent).toBe(
'Disk full Durable vector store: 3 document(s), 12 chunk(s).'
);
});
it('trims multiple trailing periods', () => {
const { progressLog } = setupDom();
renderIndexingFailure({
error_message: 'Service unavailable..',
result: { durable_indexed_documents: 0, durable_indexed_chunks: 0 },
});
const firstEntry = progressLog.firstChild;
expect(firstEntry.textContent.startsWith(
'Indexing failed: Service unavailable'
)).toBe(true);
expect(firstEntry.textContent).not.toMatch(/\.\./);
});
it('does not append a durable sentence when durable counts are missing', () => {
const { progressLog } = setupDom();
renderIndexingFailure({
error_message: 'Disk full.',
result: {},
});
const firstEntry = progressLog.firstChild;
expect(firstEntry.textContent).toBe(
'Indexing failed: Disk full'
);
});
it('uses textContent (never innerHTML) for the progress-text update', () => {
const { progressText } = setupDom();
// XSS attempt in error_message: must render as text, not as HTML.
renderIndexingFailure({
error_message: '<img src=x onerror=alert(1)>.',
result: {},
});
// No child <img> element was created — the payload is a text node,
// not parsed HTML. happy-dom serialises innerHTML with HTML
// entity escapes (``<``) for raw text-node content; the
// load-bearing assertion is that no actual <img> element exists.
expect(progressText.querySelector('img')).toBeNull();
expect(progressText.textContent).toBe(
'<img src=x onerror=alert(1)>'
);
});
it('emits one log entry per error in result.errors', () => {
const { progressLog } = setupDom();
renderIndexingFailure({
error_message: 'Many failed.',
result: {
errors: [
{ doc_id: 'd1', title: 'Doc 1', error: 'E1' },
{ doc_id: 'd2', title: 'Doc 2', error: 'E2' },
{ doc_id: 'd3', error: 'E3' },
],
},
});
expect(progressLog.childNodes.length).toBe(4);
expect(progressLog.childNodes[0].textContent).toBe(
'Indexing failed: Many failed'
);
expect(progressLog.childNodes[1].textContent).toBe('Doc 1: E1');
expect(progressLog.childNodes[2].textContent).toBe('Doc 2: E2');
expect(progressLog.childNodes[3].textContent).toBe('d3: E3');
});
});
describe('hideProgressUI', () => {
beforeEach(() => {
vi.useFakeTimers();
// Provide the DOM elements the function touches.
const progressSection = document.createElement('div');
progressSection.id = 'indexing-progress';
progressSection.style.display = 'block';
document.body.appendChild(progressSection);
const cancelBtn = document.createElement('button');
cancelBtn.id = 'cancel-indexing-btn';
cancelBtn.style.display = 'block';
document.body.appendChild(cancelBtn);
const spinner = document.createElement('div');
spinner.id = 'indexing-spinner';
document.body.appendChild(spinner);
const indexBtn = document.createElement('button');
indexBtn.id = 'index-collection-btn';
indexBtn.disabled = true;
document.body.appendChild(indexBtn);
const reindexBtn = document.createElement('button');
reindexBtn.id = 'reindex-collection-btn';
reindexBtn.disabled = true;
document.body.appendChild(reindexBtn);
});
afterEach(() => {
vi.useRealTimers();
document.body.innerHTML = '';
});
it('hides the progress section 5s after hideProgressUI() by default', () => {
const section = document.getElementById('indexing-progress');
expect(section.style.display).toBe('block');
hideProgressUI();
// Synchronous side-effects fire immediately:
expect(section.style.display).toBe('block');
// After 5s the section is hidden:
vi.advanceTimersByTime(5000);
expect(section.style.display).toBe('none');
});
it('does NOT auto-hide when keepVisible=true (failure path)', () => {
const section = document.getElementById('indexing-progress');
expect(section.style.display).toBe('block');
hideProgressUI({ keepVisible: true });
vi.advanceTimersByTime(10000);
expect(section.style.display).toBe('block');
});
it('re-enables the index/reindex buttons and hides the cancel button synchronously', () => {
const indexBtn = document.getElementById('index-collection-btn');
const reindexBtn = document.getElementById('reindex-collection-btn');
const cancelBtn = document.getElementById('cancel-indexing-btn');
expect(indexBtn.disabled).toBe(true);
expect(reindexBtn.disabled).toBe(true);
expect(cancelBtn.style.display).toBe('block');
hideProgressUI();
expect(indexBtn.disabled).toBe(false);
expect(reindexBtn.disabled).toBe(false);
expect(cancelBtn.style.display).toBe('none');
});
});
describe('collection boolean write ownership', () => {
function deferred() {
let resolvePromise;
let rejectPromise;
const promise = new Promise((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
return {
promise,
resolve: resolvePromise,
reject: rejectPromise,
};
}
function response(payload) {
return {
ok: true,
json: vi.fn().mockResolvedValue(payload),
};
}
beforeEach(() => {
document.body.innerHTML = `
<input type="checkbox" id="collection-is-public">
<input type="checkbox" id="collection-agent-enabled" checked>
`;
vi.stubGlobal('COLLECTION_ID', 'collection-writes-3299');
vi.stubGlobal('URLS', {
LIBRARY_API: {
COLLECTION_DETAILS: '/library/api/collections/{id}',
},
});
vi.stubGlobal('URLBuilder', {
build: vi.fn((template, id) => template.replace('{id}', id)),
});
vi.stubGlobal('api', {
getCsrfToken: vi.fn(() => 'csrf-collection-write'),
});
vi.stubGlobal('alert', vi.fn());
});
afterEach(() => {
vi.unstubAllGlobals();
document.body.replaceChildren();
});
it('sends same-control intents in order and ignores an older failure', async () => {
const firstWrite = deferred();
const secondWrite = deferred();
const safeFetch = vi.fn()
.mockReturnValueOnce(firstWrite.promise)
.mockReturnValueOnce(secondWrite.promise);
vi.stubGlobal('safeFetch', safeFetch);
const publicToggle = document.getElementById('collection-is-public');
publicToggle.checked = true;
const olderIntent = updateCollectionIsPublic(true);
publicToggle.checked = false;
const latestIntent = updateCollectionIsPublic(false);
await vi.waitFor(() => expect(safeFetch).toHaveBeenCalledTimes(1));
expect(JSON.parse(safeFetch.mock.calls[0][1].body)).toEqual({
is_public: true,
});
firstWrite.reject(new Error('older write failed'));
await vi.waitFor(() => expect(safeFetch).toHaveBeenCalledTimes(2));
expect(publicToggle.checked).toBe(false);
expect(alert).not.toHaveBeenCalled();
expect(JSON.parse(safeFetch.mock.calls[1][1].body)).toEqual({
is_public: false,
});
secondWrite.resolve(response({
success: true,
collection: { is_public: false },
}));
await Promise.all([olderIntent, latestIntent]);
expect(publicToggle.checked).toBe(false);
expect(alert).toHaveBeenCalledOnce();
expect(alert).toHaveBeenCalledWith(
'Success: Collection marked private (local-only).',
);
for (const [url, options] of safeFetch.mock.calls) {
expect(url).toBe('/library/api/collections/collection-writes-3299');
expect(options).toEqual(expect.objectContaining({
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': 'csrf-collection-write',
},
}));
}
});
it('keeps controls concurrent and suppresses an older success repaint', async () => {
const firstPublicWrite = deferred();
const secondPublicWrite = deferred();
const agentWrite = deferred();
let publicCalls = 0;
const safeFetch = vi.fn((_url, options) => {
const payload = JSON.parse(options.body);
if (Object.hasOwn(payload, 'is_public')) {
publicCalls += 1;
return publicCalls === 1
? firstPublicWrite.promise
: secondPublicWrite.promise;
}
return agentWrite.promise;
});
vi.stubGlobal('safeFetch', safeFetch);
const publicToggle = document.getElementById('collection-is-public');
const agentToggle = document.getElementById('collection-agent-enabled');
publicToggle.checked = true;
const olderPublicIntent = updateCollectionIsPublic(true);
publicToggle.checked = false;
const latestPublicIntent = updateCollectionIsPublic(false);
agentToggle.checked = false;
const agentIntent = updateCollectionAgentEnabled(false);
await vi.waitFor(() => expect(safeFetch).toHaveBeenCalledTimes(2));
expect(safeFetch.mock.calls.map(([, options]) => JSON.parse(options.body)))
.toEqual([
{ is_public: true },
{ agent_enabled: false },
]);
firstPublicWrite.resolve(response({
success: true,
collection: { is_public: true },
}));
await vi.waitFor(() => expect(safeFetch).toHaveBeenCalledTimes(3));
expect(publicToggle.checked).toBe(false);
expect(agentToggle.checked).toBe(false);
expect(alert).not.toHaveBeenCalledWith(
'Success: Collection marked public.',
);
secondPublicWrite.resolve(response({
success: true,
collection: { is_public: false },
}));
agentWrite.resolve(response({
success: true,
collection: { agent_enabled: false },
}));
await Promise.all([
olderPublicIntent,
latestPublicIntent,
agentIntent,
]);
expect(publicToggle.checked).toBe(false);
expect(agentToggle.checked).toBe(false);
expect(alert).toHaveBeenCalledWith(
'Success: Collection marked private (local-only).',
);
expect(alert).toHaveBeenCalledWith(
'Success: Collection hidden from the research agent.',
);
});
});
describe('startPolling request ownership', () => {
function deferred() {
let resolvePromise;
const promise = new Promise(resolve => {
resolvePromise = resolve;
});
return { promise, resolve: resolvePromise };
}
function response(payload) {
return {
ok: true,
json: vi.fn().mockResolvedValue(payload),
};
}
function setupPollingDom() {
document.body.innerHTML = `
<section id="indexing-progress" style="display:block"></section>
<button id="cancel-indexing-btn" style="display:block"></button>
<button id="index-collection-btn" disabled></button>
<button id="reindex-collection-btn" disabled></button>
<div id="indexing-spinner"></div>
<div id="progress-fill"></div>
<div id="progress-text"></div>
<div id="progress-log"></div>
`;
}
beforeEach(() => {
vi.useFakeTimers();
setupPollingDom();
vi.stubGlobal('COLLECTION_ID', 'collection-poll-3299');
// The terminal branch fire-and-forgets the page-detail refresh. This
// focused fixture intentionally omits that page's larger DOM surface.
vi.stubGlobal('alert', vi.fn());
});
afterEach(() => {
vi.clearAllTimers();
vi.useRealTimers();
vi.unstubAllGlobals();
document.body.replaceChildren();
});
it('keeps at most one status request in flight and resumes after it settles', async () => {
const firstStatus = deferred();
let statusCalls = 0;
const safeFetch = vi.fn(url => {
if (!String(url).endsWith('/index/status')) {
return new Promise(() => {});
}
statusCalls += 1;
if (statusCalls === 1) return firstStatus.promise;
return Promise.resolve(response({
status: 'completed',
progress_current: 2,
progress_total: 2,
progress_message: 'Owned terminal status',
}));
});
vi.stubGlobal('safeFetch', safeFetch);
startPolling();
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(1);
await vi.advanceTimersByTimeAsync(8000);
expect(statusCalls).toBe(1);
firstStatus.resolve(response({
status: 'processing',
progress_current: 1,
progress_total: 2,
progress_message: 'First request settled',
}));
await vi.advanceTimersByTimeAsync(0);
expect(document.getElementById('progress-text').textContent)
.toBe('First request settled');
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(2);
expect(document.getElementById('progress-text').textContent)
.toBe('Owned terminal status');
expect(document.getElementById('index-collection-btn').disabled)
.toBe(false);
await vi.advanceTimersByTimeAsync(6000);
expect(statusCalls).toBe(2);
});
it('retries after an HTTP error frame without parsing it as terminal', async () => {
const recoveredStatus = deferred();
const errorJson = vi.fn().mockResolvedValue({ status: 'error' });
let statusCalls = 0;
const safeFetch = vi.fn(url => {
if (!String(url).endsWith('/index/status')) {
return new Promise(() => {});
}
statusCalls += 1;
if (statusCalls === 1) {
return Promise.resolve({
ok: false,
status: 500,
json: errorJson,
});
}
return recoveredStatus.promise;
});
vi.stubGlobal('safeFetch', safeFetch);
const progressSection = document.getElementById('indexing-progress');
const indexButton = document.getElementById('index-collection-btn');
showProgressUI();
startPolling();
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(1);
expect(errorJson).not.toHaveBeenCalled();
expect(progressSection.style.display).toBe('block');
expect(indexButton.disabled).toBe(true);
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(2);
await vi.advanceTimersByTimeAsync(4000);
expect(statusCalls).toBe(2);
expect(indexButton.disabled).toBe(true);
recoveredStatus.resolve(response({
status: 'completed',
progress_current: 3,
progress_total: 3,
progress_message: 'Recovered healthy completion',
}));
await vi.advanceTimersByTimeAsync(0);
expect(document.getElementById('progress-text').textContent)
.toBe('Recovered healthy completion');
expect(indexButton.disabled).toBe(false);
await vi.advanceTimersByTimeAsync(6000);
expect(statusCalls).toBe(2);
});
it('leaves resume UI untouched for a non-OK status response', async () => {
const errorJson = vi.fn().mockResolvedValue({ status: 'error' });
const safeFetch = vi.fn().mockResolvedValue({
ok: false,
status: 503,
json: errorJson,
});
vi.stubGlobal('safeFetch', safeFetch);
const progressSection = document.getElementById('indexing-progress');
const indexButton = document.getElementById('index-collection-btn');
progressSection.style.display = 'none';
indexButton.disabled = false;
await checkAndResumeIndexing();
expect(safeFetch).toHaveBeenCalledWith(
'/library/api/collections/collection-poll-3299/index/status',
);
expect(errorJson).not.toHaveBeenCalled();
expect(progressSection.style.display).toBe('none');
expect(indexButton.disabled).toBe(false);
expect(document.getElementById('progress-log').childNodes)
.toHaveLength(0);
});
it('does not let a retired poll repaint after the newer poll completes', async () => {
const retiredStatus = deferred();
let statusCalls = 0;
const safeFetch = vi.fn(url => {
if (!String(url).endsWith('/index/status')) {
return new Promise(() => {});
}
statusCalls += 1;
if (statusCalls === 1) return retiredStatus.promise;
return Promise.resolve(response({
status: 'completed',
progress_current: 4,
progress_total: 4,
progress_message: 'Newer poll completed',
}));
});
vi.stubGlobal('safeFetch', safeFetch);
startPolling();
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(1);
startPolling();
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(2);
expect(document.getElementById('progress-text').textContent)
.toBe('Newer poll completed');
retiredStatus.resolve(response({
status: 'processing',
progress_current: 1,
progress_total: 4,
progress_message: 'Retired stale repaint',
}));
await vi.advanceTimersByTimeAsync(0);
expect(document.getElementById('progress-text').textContent)
.toBe('Newer poll completed');
expect(document.getElementById('progress-fill').style.width)
.toBe('100%');
await vi.advanceTimersByTimeAsync(6000);
expect(statusCalls).toBe(2);
});
it('does not let run A delayed hide conceal newly started run B', async () => {
const runBStatus = deferred();
let statusCalls = 0;
const safeFetch = vi.fn(url => {
if (!String(url).endsWith('/index/status')) {
return new Promise(() => {});
}
statusCalls += 1;
if (statusCalls === 1) {
return Promise.resolve(response({
status: 'completed',
progress_current: 2,
progress_total: 2,
progress_message: 'Run A completed',
}));
}
return runBStatus.promise;
});
vi.stubGlobal('safeFetch', safeFetch);
const progressSection = document.getElementById('indexing-progress');
const indexButton = document.getElementById('index-collection-btn');
showProgressUI();
startPolling();
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(1);
expect(progressSection.style.display).toBe('block');
expect(indexButton.disabled).toBe(false);
// Starting B before A's five-second terminal hide expires transfers
// ownership of the visible progress section to the new run.
showProgressUI();
startPolling();
expect(indexButton.disabled).toBe(true);
await vi.advanceTimersByTimeAsync(2000);
expect(statusCalls).toBe(2);
await vi.advanceTimersByTimeAsync(4000);
expect(progressSection.style.display).toBe('block');
expect(indexButton.disabled).toBe(true);
expect(statusCalls).toBe(2);
});
});
describe('collection search request ownership', () => {
function deferred() {
let resolvePromise;
let rejectPromise;
const promise = new Promise((resolveDeferred, rejectDeferred) => {
resolvePromise = resolveDeferred;
rejectPromise = rejectDeferred;
});
return {
promise,
reject: rejectPromise,
resolve: resolvePromise,
};
}
beforeEach(() => {
document.body.innerHTML = `
<section id="collection-search-section" style="display:none">
<input id="collection-search-input">
<button id="collection-search-btn" type="button">Search</button>
<div id="collection-search-results"></div>
</section>
`;
});
afterEach(() => {
vi.clearAllTimers();
vi.useRealTimers();
delete window.LibrarySearch;
delete window.SemanticSearch;
document.body.replaceChildren();
});
it('does not let an older rejected search replace newer results', async () => {
const olderSearch = deferred();
window.LibrarySearch = {
performSemanticSearch: vi.fn()
.mockReturnValueOnce(olderSearch.promise)
.mockResolvedValueOnce({ success: true, results: [] }),
};
const { runtime, SafeLogger } = compileCollectionSearchRuntime();
const olderRun = runtime.searchCollection('older query');
await runtime.searchCollection('newer query');
const results = document.getElementById('collection-search-results');
expect(results.textContent).toContain('No matching results found.');
olderSearch.reject(new Error('late failure'));
await olderRun;
expect(results.textContent).toContain('No matching results found.');
expect(results.textContent).not.toContain('Search failed');
expect(SafeLogger.error).not.toHaveBeenCalled();
});
it('clearing the input retires a debounced search already in flight', async () => {
vi.useFakeTimers();
const pendingSearch = deferred();
const createCard = vi.fn(() => document.createElement('article'));
window.LibrarySearch = {
performSemanticSearch: vi.fn(() => pendingSearch.promise),
getLibraryCardConfig: vi.fn(() => ({ source: 'collection' })),
};
window.SemanticSearch = { createSemanticResultCard: createCard };
const { runtime } = compileCollectionSearchRuntime();
runtime.initCollectionSearch();
const input = document.getElementById('collection-search-input');
const results = document.getElementById('collection-search-results');
input.value = 'query to retire';
input.dispatchEvent(new Event('input'));
await vi.advanceTimersByTimeAsync(500);
expect(window.LibrarySearch.performSemanticSearch).toHaveBeenCalledWith(
'collection-search-3299',
'query to retire',
20,
);
input.value = '';
input.dispatchEvent(new Event('input'));
expect(results.innerHTML).toBe('');
pendingSearch.resolve({
success: true,
results: [{ id: 'stale-result' }],
});
await vi.advanceTimersByTimeAsync(0);
expect(results.innerHTML).toBe('');
expect(createCard).not.toHaveBeenCalled();
});
});
describe('displayCollectionEmbeddingSettings', () => {
let container;
beforeEach(() => {
document.body.innerHTML = '<div id="collection-embedding-info"></div>';
container = document.getElementById('collection-embedding-info');
});
it('renders chunk_overlap: 0 without collapsing to "Not set"', () => {
window.setCollectionDataForTesting({
embedding_model: 'all-MiniLM-L6-v2',
embedding_model_type: 'sentence_transformers',
chunk_size: 1000,
chunk_overlap: 0,
});
window.displayCollectionEmbeddingSettings();
expect(container.textContent).toContain('Chunk Overlap:');
expect(container.textContent).toContain('0 characters');
expect(container.textContent).not.toContain('Not set');
});
it('renders chunk_overlap: null as "Not set"', () => {
window.setCollectionDataForTesting({
embedding_model: 'all-MiniLM-L6-v2',
embedding_model_type: 'sentence_transformers',
chunk_size: 1000,
chunk_overlap: null,
});
window.displayCollectionEmbeddingSettings();
expect(container.textContent).toContain('Chunk Overlap:');
expect(container.textContent).toContain('Not set');
});
it('renders chunk_size: 0 without collapsing to "Not set"', () => {
window.setCollectionDataForTesting({
embedding_model: 'all-MiniLM-L6-v2',
embedding_model_type: 'sentence_transformers',
chunk_size: 0,
chunk_overlap: 0,
});
window.displayCollectionEmbeddingSettings();
expect(container.textContent).toContain('Chunk Size:');
expect(container.textContent).toContain('0 characters');
});
});