~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 components/semantic_search.js
*
* Covers the shared data-shaping helpers plus the actual snippet/card DOM
* runtime consumed by history, library, notes, and collection search.
*/
import '@js/components/semantic_search.js';
const SS = window.SemanticSearch;
describe('SemanticSearch.buildTieredResults', () => {
it('returns empty tiers for empty inputs', () => {
const r = SS.buildTieredResults([], []);
expect(r.tier1).toEqual([]);
expect(r.tier2).toEqual([]);
expect(r.tier3).toEqual([]);
});
it('puts text-only matches in tier2 (preserving original order)', () => {
const text = [{ id: 'a' }, { id: 'b' }, { id: 'c' }];
const r = SS.buildTieredResults(text, []);
expect(r.tier1).toEqual([]);
expect(r.tier3).toEqual([]);
expect(r.tier2.map(x => x.historyItem.id)).toEqual(['a', 'b', 'c']);
});
it('puts semantic-only matches in tier3', () => {
const sem = [
{ research_id: 'x', similarity: 0.5 },
{ research_id: 'y', similarity: 0.8 },
];
const r = SS.buildTieredResults([], sem);
expect(r.tier1).toEqual([]);
expect(r.tier2).toEqual([]);
expect(r.tier3).toHaveLength(2);
});
it('places items appearing in both tiers in tier1 with semanticMatch populated', () => {
const text = [{ id: 'a' }, { id: 'b' }];
const sem = [
{ research_id: 'a', similarity: 0.7, snippet: 'hi' },
{ research_id: 'c', similarity: 0.9 },
];
const r = SS.buildTieredResults(text, sem);
expect(r.tier1).toHaveLength(1);
expect(r.tier1[0].historyItem.id).toBe('a');
expect(r.tier1[0].semanticMatch.similarity).toBe(0.7);
expect(r.tier1[0].semanticMatch.snippet).toBe('hi');
// 'b' was text-only → tier2
expect(r.tier2.map(x => x.historyItem.id)).toEqual(['b']);
// 'c' was semantic-only → tier3
expect(r.tier3.map(x => x.semanticResult.research_id)).toEqual(['c']);
});
it('sorts tier1 by similarity DESC', () => {
const text = [{ id: 'a' }, { id: 'b' }, { id: 'c' }];
const sem = [
{ research_id: 'a', similarity: 0.3 },
{ research_id: 'b', similarity: 0.9 },
{ research_id: 'c', similarity: 0.6 },
];
const r = SS.buildTieredResults(text, sem);
expect(r.tier1.map(x => x.historyItem.id)).toEqual(['b', 'c', 'a']);
});
it('sorts tier3 by similarity DESC', () => {
const sem = [
{ research_id: 'a', similarity: 0.2 },
{ research_id: 'b', similarity: 0.95 },
{ research_id: 'c', similarity: 0.5 },
];
const r = SS.buildTieredResults([], sem);
expect(r.tier3.map(x => x.semanticResult.research_id)).toEqual(['b', 'c', 'a']);
});
it('dedups semantic results by ID, keeping the highest similarity', () => {
const sem = [
{ research_id: 'dup', similarity: 0.3 },
{ research_id: 'dup', similarity: 0.8 },
{ research_id: 'dup', similarity: 0.5 },
];
const r = SS.buildTieredResults([], sem);
expect(r.tier3).toHaveLength(1);
expect(r.tier3[0].semanticResult.similarity).toBe(0.8);
});
it('skips semantic results missing the configured ID key', () => {
const sem = [
{ research_id: 'a', similarity: 0.5 },
{ similarity: 0.9 }, // missing research_id
{ research_id: '', similarity: 0.7 }, // empty falsy
];
const r = SS.buildTieredResults([], sem);
expect(r.tier3).toHaveLength(1);
expect(r.tier3[0].semanticResult.research_id).toBe('a');
});
it('honors custom textIdKey and semanticIdKey', () => {
const text = [{ doc_id: 'x' }];
const sem = [{ id: 'x', similarity: 0.5 }];
const r = SS.buildTieredResults(text, sem, {
textIdKey: 'doc_id',
semanticIdKey: 'id',
});
expect(r.tier1).toHaveLength(1);
expect(r.tier1[0].historyItem.doc_id).toBe('x');
});
it('uses default keys when options is undefined', () => {
const text = [{ id: 'k' }];
const sem = [{ research_id: 'k', similarity: 0.5 }];
const r = SS.buildTieredResults(text, sem);
expect(r.tier1).toHaveLength(1);
});
it('coerces IDs to strings for matching (number vs string)', () => {
const text = [{ id: 42 }];
const sem = [{ research_id: '42', similarity: 0.5 }];
const r = SS.buildTieredResults(text, sem);
expect(r.tier1).toHaveLength(1);
});
it('defaults snippet to empty string when missing', () => {
const text = [{ id: 'a' }];
const sem = [{ research_id: 'a', similarity: 0.5 }];
const r = SS.buildTieredResults(text, sem);
expect(r.tier1[0].semanticMatch.snippet).toBe('');
});
});
describe('SemanticSearch rendering helpers', () => {
let originalMarked;
let originalDOMPurify;
let originalURLBuilder;
let originalURLValidator;
beforeEach(() => {
originalMarked = window.marked;
originalDOMPurify = window.DOMPurify;
originalURLBuilder = window.URLBuilder;
originalURLValidator = window.URLValidator;
});
afterEach(() => {
if (originalMarked === undefined) delete window.marked;
else window.marked = originalMarked;
if (originalDOMPurify === undefined) delete window.DOMPurify;
else window.DOMPurify = originalDOMPurify;
if (originalURLBuilder === undefined) delete window.URLBuilder;
else window.URLBuilder = originalURLBuilder;
if (originalURLValidator === undefined) delete window.URLValidator;
else window.URLValidator = originalURLValidator;
document.body.replaceChildren();
delete window.__semanticXss;
});
it('escapes snippets when markdown dependencies are unavailable', () => {
delete window.marked;
delete window.DOMPurify;
const rendered = SS.renderSnippet(
'<img src=x onerror="window.__semanticXss=true"> migration',
'migration',
);
expect(rendered).toContain('<img');
expect(rendered).not.toContain('<img');
expect(rendered).not.toContain('<mark');
expect(window.__semanticXss).toBeUndefined();
});
it('sanitizes markdown and highlights only text, including regex terms', () => {
window.marked = {
parseInline: vi.fn(() => (
'<a title="migration a+b">migration</a> plus a+b'
)),
};
window.DOMPurify = {
isSupported: true,
sanitize: vi.fn(html => html),
};
const rendered = SS.renderSnippet('source', 'migration a+b');
expect(window.marked.parseInline).toHaveBeenCalledWith('source');
expect(window.DOMPurify.sanitize).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
ALLOW_DATA_ATTR: false,
ALLOWED_ATTR: ['href', 'title', 'class'],
}),
);
expect(rendered).toContain('title="migration a+b"');
expect(rendered).toContain(
'<mark class="ldr-search-highlight">migration</mark>',
);
expect(rendered).toContain(
'<mark class="ldr-search-highlight">a+b</mark>',
);
});
it('flattens all tiers without mutating the caller\'s text result', () => {
const matched = { id: 'both', title: 'Matched' };
const tiered = {
tier1: [{
historyItem: matched,
semanticMatch: { similarity: 0.91, snippet: 'Best excerpt' },
}],
tier2: [{ historyItem: { id: 'text-only' } }],
tier3: [{ semanticResult: { research_id: 'semantic-only' } }],
};
expect(SS.flattenTieredResults(tiered)).toEqual([
{
id: 'both',
title: 'Matched',
similarity: 0.91,
content_preview: 'Best excerpt',
},
{ id: 'text-only' },
{ research_id: 'semantic-only' },
]);
expect(matched).toEqual({ id: 'both', title: 'Matched' });
});
it('creates an inert escaped card and delegates safe card navigation', () => {
delete window.marked;
delete window.DOMPurify;
window.URLBuilder = {
resultsPage: vi.fn(id => `/results/${id}`),
};
window.URLValidator = {
isSafeUrl: vi.fn(() => false),
safeAssign: vi.fn(),
};
const card = SS.createSemanticResultCard({
research_id: 'research-3299',
research_title: '<img src=x onerror="window.__semanticXss=true">',
similarity: 87,
snippet: '<script>window.__semanticXss=true</script>',
url: 'javascript:window.__semanticXss=true',
type: 'report',
});
document.body.appendChild(card);
expect(card.textContent).toContain('<img src=x');
expect(card.querySelector('img')).toBeNull();
expect(card.querySelector('script')).toBeNull();
expect(card.querySelector('.ldr-semantic-result-source')).toBeNull();
expect(card.querySelector('a').getAttribute('href'))
.toBe('/results/research-3299');
expect(window.__semanticXss).toBeUndefined();
card.querySelector('a').dispatchEvent(
new MouseEvent('click', { bubbles: true }),
);
expect(window.URLValidator.safeAssign).not.toHaveBeenCalled();
card.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(
window.location,
'href',
'/results/research-3299',
);
window.URLValidator.isSafeUrl.mockReturnValue(true);
const safeSourceCard = SS.createSemanticResultCard({
research_id: 'safe-source',
title: 'Safe source',
url: 'https://example.test/source?q=3299',
});
const sourceLink = safeSourceCard.querySelector(
'.ldr-semantic-result-source a',
);
expect(sourceLink.getAttribute('href'))
.toBe('https://example.test/source?q=3299');
expect(sourceLink.getAttribute('target')).toBe('_blank');
expect(sourceLink.getAttribute('rel')).toBe('noopener noreferrer');
});
});
describe('SemanticSearch.isSafeExternalUrl', () => {
let savedValidator;
beforeEach(() => {
// Force the fallback path so we test the inline scheme list,
// not URLValidator's behavior (covered by url-validator tests).
savedValidator = window.URLValidator;
delete window.URLValidator;
});
afterEach(() => {
if (savedValidator !== undefined) window.URLValidator = savedValidator;
});
it('accepts http and https URLs', () => {
expect(SS.isSafeExternalUrl('http://example.com')).toBe(true);
expect(SS.isSafeExternalUrl('https://example.com/path?q=1')).toBe(true);
});
it('rejects javascript: URLs', () => {
expect(SS.isSafeExternalUrl('javascript:alert(1)')).toBe(false);
});
it('rejects data: URLs', () => {
expect(SS.isSafeExternalUrl('data:text/html,<script>alert(1)</script>')).toBe(false);
});
it('rejects vbscript: URLs', () => {
expect(SS.isSafeExternalUrl('vbscript:msgbox(1)')).toBe(false);
});
it('rejects about:, blob:, file: schemes', () => {
expect(SS.isSafeExternalUrl('about:blank')).toBe(false);
expect(SS.isSafeExternalUrl('blob:https://example.com/abc')).toBe(false);
expect(SS.isSafeExternalUrl('file:///etc/passwd')).toBe(false);
});
it('rejects non-string input', () => {
expect(SS.isSafeExternalUrl(null)).toBe(false);
expect(SS.isSafeExternalUrl(undefined)).toBe(false);
expect(SS.isSafeExternalUrl(42)).toBe(false);
expect(SS.isSafeExternalUrl({})).toBe(false);
});
it('rejects empty string', () => {
expect(SS.isSafeExternalUrl('')).toBe(false);
});
it('is case-insensitive against scheme obfuscation', () => {
expect(SS.isSafeExternalUrl('JaVaScRiPt:alert(1)')).toBe(false);
});
it('rejects relative URLs (no scheme)', () => {
expect(SS.isSafeExternalUrl('/relative/path')).toBe(false);
expect(SS.isSafeExternalUrl('example.com')).toBe(false);
});
it('delegates to URLValidator when present', () => {
window.URLValidator = {
isSafeUrl: vi.fn().mockReturnValue(true),
};
expect(SS.isSafeExternalUrl('https://example.com')).toBe(true);
expect(window.URLValidator.isSafeUrl).toHaveBeenCalledWith(
'https://example.com',
{ allowMailto: false }
);
});
});