~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 services/formatting.js
*
* Tests the formatting utility functions used for displaying
* dates, durations, modes, and numbers throughout the UI.
*/
// Stub ResearchStates (used by formatStatus)
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',
]);
// Load constants first (provides ResearchStates), then formatting
import '@js/config/constants.js';
import '@js/services/formatting.js';
const fmt = window.formatting;
describe('formatting', () => {
describe('formatMode', () => {
it('maps quick to Quick Summary', () => {
expect(fmt.formatMode('quick')).toBe('Quick Summary');
});
it('maps detailed to Detailed Report', () => {
expect(fmt.formatMode('detailed')).toBe('Detailed Report');
});
it('capitalizes unknown modes', () => {
expect(fmt.formatMode('custom')).toBe('Custom');
});
});
describe('formatDate', () => {
it('returns Unknown for falsy input', () => {
expect(fmt.formatDate(null)).toBe('Unknown');
expect(fmt.formatDate('')).toBe('Unknown');
expect(fmt.formatDate(undefined)).toBe('Unknown');
});
it('formats a valid ISO date', () => {
const result = fmt.formatDate('2025-01-15T10:30:00Z');
// Should contain month, day, and year
expect(result).toMatch(/Jan/);
expect(result).toMatch(/15/);
expect(result).toMatch(/2025/);
});
it('appends duration in minutes and seconds', () => {
const result = fmt.formatDate('2025-01-15T10:30:00Z', 125);
expect(result).toContain('2m 5s');
});
it('appends duration in seconds only when < 60s', () => {
const result = fmt.formatDate('2025-01-15T10:30:00Z', 45);
expect(result).toContain('45s');
expect(result).not.toContain('m');
});
it('does not append duration when null', () => {
const result = fmt.formatDate('2025-01-15T10:30:00Z', null);
expect(result).not.toContain('s)');
});
});
describe('formatDuration', () => {
it('returns Unknown for falsy input', () => {
expect(fmt.formatDuration(null)).toBe('Unknown');
expect(fmt.formatDuration(undefined)).toBe('Unknown');
expect(fmt.formatDuration(0)).toBe('Unknown');
});
it('returns Unknown for NaN', () => {
expect(fmt.formatDuration(NaN)).toBe('Unknown');
});
it('formats seconds only when < 60', () => {
expect(fmt.formatDuration(45)).toBe('45s');
});
it('formats minutes and seconds', () => {
expect(fmt.formatDuration(125)).toBe('2m 5s');
});
it('handles exact minutes', () => {
expect(fmt.formatDuration(120)).toBe('2m 0s');
});
it('floors fractional seconds', () => {
expect(fmt.formatDuration(45.7)).toBe('45s');
});
});
describe('capitalizeFirstLetter', () => {
it('capitalizes lowercase string', () => {
expect(fmt.capitalizeFirstLetter('hello')).toBe('Hello');
});
it('returns empty string for falsy input', () => {
expect(fmt.capitalizeFirstLetter('')).toBe('');
expect(fmt.capitalizeFirstLetter(null)).toBe('');
expect(fmt.capitalizeFirstLetter(undefined)).toBe('');
});
it('preserves already capitalized strings', () => {
expect(fmt.capitalizeFirstLetter('Hello')).toBe('Hello');
});
it('handles single character', () => {
expect(fmt.capitalizeFirstLetter('a')).toBe('A');
});
});
describe('formatNumber', () => {
it('returns "0" for null/undefined', () => {
expect(fmt.formatNumber(null)).toBe('0');
expect(fmt.formatNumber(undefined)).toBe('0');
});
it('returns number as-is when < 1000', () => {
expect(fmt.formatNumber(999)).toBe('999');
});
it('adds comma separators for thousands', () => {
expect(fmt.formatNumber(1000)).toBe('1,000');
expect(fmt.formatNumber(1234567)).toBe('1,234,567');
});
it('handles zero', () => {
expect(fmt.formatNumber(0)).toBe('0');
});
it('coerces API values before they can become markup in callers', () => {
expect(fmt.formatNumber('1234')).toBe('1,234');
expect(fmt.formatNumber('<img src=x onerror=alert(1)>')).toBe('0');
expect(fmt.formatNumber(Infinity)).toBe('0');
});
it('returns "0" for values that are neither a number nor a string', () => {
// JSON can give an object its own toString and valueOf keys. With
// neither callable, Number() throws on it instead of returning NaN.
const unconvertible = JSON.parse('{"toString":null,"valueOf":null}');
expect(fmt.formatNumber(unconvertible)).toBe('0');
expect(fmt.formatNumber(true)).toBe('0');
expect(fmt.formatNumber([1234])).toBe('0');
});
});
describe('formatStatus', () => {
it('delegates to ResearchStates.formatStatus', () => {
expect(fmt.formatStatus('completed')).toBe('Completed');
expect(fmt.formatStatus('in_progress')).toBe('In Progress');
});
});
describe('formatCurrency', () => {
it('uses 6 decimal places for tiny amounts (< $0.01)', () => {
expect(fmt.formatCurrency(0.000123)).toBe('$0.000123');
expect(fmt.formatCurrency(0.005)).toBe('$0.005000');
});
it('uses 4 decimal places for amounts < $1 (boundary: 0.01)', () => {
expect(fmt.formatCurrency(0.01)).toBe('$0.0100');
expect(fmt.formatCurrency(0.5)).toBe('$0.5000');
expect(fmt.formatCurrency(0.9999)).toBe('$0.9999');
});
it('uses 2 decimal places for amounts >= $1', () => {
expect(fmt.formatCurrency(1)).toBe('$1.00');
expect(fmt.formatCurrency(12.345)).toBe('$12.35');
expect(fmt.formatCurrency(1000)).toBe('$1000.00');
});
it('zero uses the tiny-amount branch', () => {
expect(fmt.formatCurrency(0)).toBe('$0.000000');
});
});
describe('generateChartColors', () => {
it('returns empty arrays for count=0', () => {
const r = fmt.generateChartColors(0);
expect(r.background).toEqual([]);
expect(r.border).toEqual([]);
});
it('returns `count` colors', () => {
const r = fmt.generateChartColors(3);
expect(r.background).toHaveLength(3);
expect(r.border).toHaveLength(3);
});
it('cycles through the palette when count exceeds base length', () => {
const r = fmt.generateChartColors(15);
expect(r.background).toHaveLength(15);
// First and 11th entries should be identical (palette of 10)
expect(r.background[0]).toBe(r.background[10]);
});
it('border colors are opaque versions of background colors', () => {
const r = fmt.generateChartColors(2);
for (let i = 0; i < 2; i++) {
// background should use alpha 0.8, border alpha 1
expect(r.background[i]).toContain('0.8');
expect(r.border[i]).not.toContain('0.8');
expect(r.border[i]).toContain(', 1)');
}
});
});
describe('basicMarkdownRender — link safety', () => {
it('renders a safe http(s) link as an anchor', () => {
const html = fmt.basicMarkdownRender('[site](https://example.com)');
expect(html).toContain('<a href="https://example.com"');
expect(html).toContain('>site</a>');
});
it('neutralizes a plain javascript: link (no anchor)', () => {
const html = fmt.basicMarkdownRender('[x](javascript:alert(1))');
expect(html).not.toContain('<a ');
expect(html.toLowerCase()).not.toContain('href="javascript');
});
it('neutralizes javascript: hidden by an embedded tab (control-char bypass)', () => {
// The browser strips the tab before resolving the scheme, so a literal
// scheme blocklist would let this through and execute on click.
const html = fmt.basicMarkdownRender('[x](java\tscript:alert(1))');
expect(html).not.toContain('<a ');
});
it('neutralizes javascript: hidden by a leading control char', () => {
const html = fmt.basicMarkdownRender('[x](javascript:alert(1))');
expect(html).not.toContain('<a ');
});
it('neutralizes a data: URI link', () => {
const html = fmt.basicMarkdownRender('[x](data:text/html,<script>alert(1)</script>)');
expect(html).not.toContain('<a ');
});
it('neutralizes scheme-case and whitespace variants', () => {
for (const url of [
'JAVASCRIPT:alert(1)',
'jAvAsCrIpT:alert(1)',
' javascript:alert(1)',
'java\nscript:alert(1)',
'vbscript:msgbox(1)',
'DATA:text/html,x',
]) {
const html = fmt.basicMarkdownRender(`[x](${url})`);
expect(html, url).not.toContain('<a ');
}
});
it('still renders ordinary relative + anchor links', () => {
expect(fmt.basicMarkdownRender('[a](/notes/1)')).toContain('<a href="/notes/1"');
expect(fmt.basicMarkdownRender('[b](#section)')).toContain('<a href="#section"');
});
it('escapes raw HTML in the body', () => {
const html = fmt.basicMarkdownRender('<img src=x onerror=alert(1)>');
expect(html).not.toContain('<img');
expect(html).toContain('<img');
});
});
describe('basicMarkdownRender — emphasis vs link constructs', () => {
it('does not inject <em> into a link URL containing paired underscores', () => {
const html = fmt.basicMarkdownRender('[wiki](https://en.wikipedia.org/wiki/Foo_bar_baz)');
expect(html).toContain('<a href="https://en.wikipedia.org/wiki/Foo_bar_baz"');
expect(html).not.toContain('<em>');
});
it('does not inject <em> into a link URL containing paired asterisks', () => {
const html = fmt.basicMarkdownRender('[x](https://example.com/a*b*c)');
expect(html).toContain('<a href="https://example.com/a*b*c"');
expect(html).not.toContain('<em>');
});
it('does not inject <strong> into a link URL containing double underscores', () => {
const html = fmt.basicMarkdownRender('[x](https://example.com/a__b__c)');
expect(html).toContain('<a href="https://example.com/a__b__c"');
expect(html).not.toContain('<strong>');
});
it('keeps a snake_case [[wiki-link]] intact for caller post-processing', () => {
const html = fmt.basicMarkdownRender('see [[My_Note_Title]] here');
expect(html).toContain('[[My_Note_Title]]');
expect(html).not.toContain('<em>');
});
it('keeps an asterisk-containing [[wiki-link]] intact', () => {
const html = fmt.basicMarkdownRender('see [[Note *draft* v2]] here');
expect(html).toContain('[[Note *draft* v2]]');
expect(html).not.toContain('<em>');
});
it('still applies emphasis adjacent to links and wiki-links', () => {
const html = fmt.basicMarkdownRender('_italic_ [a](https://x.com/p_q_r) **bold** [[a_b_c]]');
expect(html).toContain('<em>italic</em>');
expect(html).toContain('<strong>bold</strong>');
expect(html).toContain('<a href="https://x.com/p_q_r"');
expect(html).toContain('[[a_b_c]]');
});
it('still applies emphasis inside a link label', () => {
const html = fmt.basicMarkdownRender('[**bold** and _it_](https://example.com)');
expect(html).toContain('<a href="https://example.com"');
expect(html).toContain('<strong>bold</strong>');
expect(html).toContain('<em>it</em>');
});
it('restores a [[wiki-link]] nested inside a link label', () => {
const html = fmt.basicMarkdownRender('[see [[a_b]]](https://example.com)');
expect(html).toContain('<a href="https://example.com"');
expect(html).toContain('[[a_b]]');
expect(html).not.toContain('LDRTOK');
});
it('does not single-escape forged placeholder tokens into real ones', () => {
// A literal token in note content must render as inert escaped
// text, not be substituted during the restore pass.
const html = fmt.basicMarkdownRender('<!--LDRTOK0--> [a](https://example.com)');
expect(html).toContain('<!--LDRTOK0-->');
expect(html).toContain('<a href="https://example.com"');
});
it('leaves no unrestored placeholder tokens in the output', () => {
const html = fmt.basicMarkdownRender('[a](https://x.com) [[B]] [c](javascript:alert(1))');
expect(html).not.toContain('LDRTOK');
});
});
describe('basicMarkdownRender — inline code precedence', () => {
it('does not turn underscores inside a code span into <em> (snake_case)', () => {
const html = fmt.basicMarkdownRender('use `snake_case_name` here');
expect(html).toContain('<code');
expect(html).toContain('snake_case_name');
expect(html).not.toContain('<em>');
});
it('does not turn double-underscores inside a code span into <strong>', () => {
const html = fmt.basicMarkdownRender('call `__init__` first');
expect(html).toContain('<code');
expect(html).toContain('__init__');
expect(html).not.toContain('<strong>');
});
it('does not turn asterisks inside a code span into emphasis (globs/pointers)', () => {
const html = fmt.basicMarkdownRender('the `a*b*c` pattern');
expect(html).toContain('<code');
expect(html).toContain('a*b*c');
expect(html).not.toContain('<em>');
});
it('does not turn a [label](url) inside a code span into a live anchor', () => {
const html = fmt.basicMarkdownRender('literal `[x](https://example.com)` here');
expect(html).toContain('<code');
expect(html).toContain('[x](https://example.com)');
// No anchor created from the code-span contents.
expect(html).not.toContain('<a href="https://example.com"');
});
it('still applies emphasis OUTSIDE a code span on the same line', () => {
const html = fmt.basicMarkdownRender('_italic_ and `code_span` and **bold**');
expect(html).toContain('<em>italic</em>');
expect(html).toContain('<strong>bold</strong>');
expect(html).toContain('code_span');
expect(html).not.toContain('<em>span</em>');
});
it('leaves no unrestored placeholder tokens when code spans are present', () => {
const html = fmt.basicMarkdownRender('`a_b` and [[Note]] and [c](https://x.com)');
expect(html).not.toContain('LDRTOK');
});
});
describe('basicMarkdownRender — rejected links display + safety', () => {
it('shows a rejected unsafe link as single-escaped literal text (no double-escaping)', () => {
const html = fmt.basicMarkdownRender('[Tom & Jerry](javascript:alert(1))');
expect(html).not.toContain('<a ');
expect(html).toContain('[Tom & Jerry](javascript:alert(1))');
expect(html).not.toContain('&amp;');
});
it('keeps HTML-injection labels of rejected links inert', () => {
const html = fmt.basicMarkdownRender('[<img src=x onerror=alert(1)>](javascript:alert(1))');
expect(html).not.toContain('<a ');
expect(html).not.toContain('<img');
expect(html).toContain('<img');
});
it('does not corrupt a rejected link containing underscores with emphasis tags', () => {
const html = fmt.basicMarkdownRender('[a_b_c](javascript:a_l_ert(1))');
expect(html).not.toContain('<a ');
expect(html).not.toContain('<em>');
expect(html).toContain('[a_b_c](javascript:a_l_ert(1))');
});
it('still blocks control-char scheme bypass on underscore-laden links', () => {
for (const input of [
'[x_y](java\tscript:alert(1))',
'[x_y](java\nscript:alert(1))',
'[x_y](javascript:alert(1))',
'[x_y](JAVASCRIPT:alert(1))',
'[x_y](vbscript:msgbox(1))',
]) {
const html = fmt.basicMarkdownRender(input);
expect(html, input).not.toContain('<a ');
expect(html.toLowerCase(), input).not.toContain('href="java');
}
});
it('escape-first pipeline still neutralizes markup around safe links', () => {
const html = fmt.basicMarkdownRender('<script>x</script> [a](https://example.com)');
expect(html).not.toContain('<script');
expect(html).toContain('<script>');
expect(html).toContain('<a href="https://example.com"');
});
});
describe('stripMarkdownToText — shared note-preview strip', () => {
it('reduces markdown syntax soup to plain text', () => {
const raw = '# Research kickoff\n\nSome **bold** thoughts, with a [[Second note]] wiki-link.\n\n## Open questions\n- How does the cache behave?';
expect(fmt.stripMarkdownToText(raw)).toBe(
'Research kickoff Some bold thoughts, with a Second note wiki-link. Open questions How does the cache behave?'
);
});
it('resolves wiki-link aliases and markdown links to their text', () => {
expect(fmt.stripMarkdownToText('See [[Real|the alias]] and [docs](https://x/d).'))
.toBe('See the alias and docs.');
});
it('handles the superset the components previously lacked (fenced code, HR, strikethrough)', () => {
expect(fmt.stripMarkdownToText('```js\ncode\n```\n\n---\n\n~~gone~~ text'))
.toBe('code gone text');
});
it('is null/empty safe', () => {
expect(fmt.stripMarkdownToText('')).toBe('');
expect(fmt.stripMarkdownToText(null)).toBe('');
expect(fmt.stripMarkdownToText(undefined)).toBe('');
});
});
describe('similarityBadge — shared "% match" badge', () => {
it('renders the rounded percentage with the tooltip', () => {
const html = fmt.similarityBadge(0.912);
expect(html).toContain('91% match');
expect(html).toContain('title="Semantic similarity"');
expect(html).toContain('ldr-similarity-badge');
});
it('returns empty string for null/undefined (so callers can drop it in)', () => {
expect(fmt.similarityBadge(null)).toBe('');
expect(fmt.similarityBadge(undefined)).toBe('');
});
it('renders 0% for a zero similarity (not treated as absent)', () => {
expect(fmt.similarityBadge(0)).toContain('0% match');
});
});
});