~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 pages/note-detail.js — setupScrollSpy observer lifecycle.
*
* Locks the perf/leak invariant in note-detail.js (~lines 1740-1775,
* documented at lines 14-17): setupScrollSpy runs on every re-render (save,
* restore), so it MUST disconnect the previous IntersectionObserver before
* creating a new one. Otherwise observers stack and every scroll fires N
* callbacks after N saves.
*
* Two guards under test:
* - The disconnect-then-null block (lines 1747-1750) tears down the prior
* observer.
* - That block runs BEFORE the `headings.length === 0` early-return (line
* 1752), so a re-render with no headings still tears the old one down and
* does not create a new (empty) observer.
*
* Exercised via the production-inert window.__noteDetailTest hook in
* note-detail.js; auto-init is skipped because the harness sets
* window.__VITEST_TEST__ before import.
*/
let hook;
let instances;
beforeAll(async () => {
window.__VITEST_TEST__ = true;
// Defensive fetch/safeFetch stubs so the import never throws.
globalThis.fetch = vi.fn(() => Promise.resolve({ ok: true, json: () => Promise.resolve({}) }));
globalThis.safeFetch = vi.fn(() => Promise.resolve({ json: () => Promise.resolve({}) }));
// Stub IntersectionObserver: each construction records itself, exposes
// observe()/disconnect() spies, and flips a `disconnected` flag.
instances = [];
globalThis.IntersectionObserver = class {
constructor(cb, opts) {
this.cb = cb;
this.opts = opts;
this.disconnected = false;
this.observe = vi.fn();
this.disconnect = vi.fn(() => { this.disconnected = true; });
instances.push(this);
}
};
await import('@js/pages/note-detail.js');
hook = window.__noteDetailTest;
});
afterAll(() => {
delete window.__VITEST_TEST__;
});
beforeEach(() => {
instances.length = 0;
// Reset the module's _tocObserver so each test starts clean. Calling
// setupScrollSpy with no headings disconnects+nulls any leftover observer,
// then early-returns without constructing a new one.
document.body.innerHTML = '<div id="note-content-rendered"></div>';
hook.setupScrollSpy();
instances.length = 0; // discard the reset (no observer was created anyway)
});
function fixtureWithHeadings() {
document.body.innerHTML = `
<ul>
<li class="ldr-toc-item"><a href="#a">A</a></li>
<li class="ldr-toc-item"><a href="#b">B</a></li>
</ul>
<div id="note-content-rendered">
<h1 id="a">A</h1>
<h2 id="b">B</h2>
</div>
`;
}
describe('setupScrollSpy — disconnect-before-recreate', () => {
it('disconnects each prior observer when re-invoked, leaving exactly one live', () => {
fixtureWithHeadings();
hook.setupScrollSpy();
hook.setupScrollSpy();
hook.setupScrollSpy();
// One observer constructed per call.
expect(instances).toHaveLength(3);
// The first two were disconnected; the last remains live.
expect(instances[0].disconnect).toHaveBeenCalled();
expect(instances[1].disconnect).toHaveBeenCalled();
expect(instances[2].disconnect).not.toHaveBeenCalled();
// Stronger invariant: disconnect calls == constructions - 1
// (each new construction tore down exactly the previous one).
const disconnects = instances.filter((io) => io.disconnected).length;
expect(disconnects).toBe(instances.length - 1);
// The live observer is the one the module is holding.
expect(hook.getTocObserver()).toBe(instances[2]);
// The latest observer watches one node per heading (2 headings).
expect(instances[2].observe).toHaveBeenCalledTimes(2);
});
it('still tears down the prior observer on a no-headings re-render and does not stack a new one', () => {
fixtureWithHeadings();
hook.setupScrollSpy();
expect(instances).toHaveLength(1);
const first = instances[0];
// Re-render with zero headings (the disconnect block at 1747-1750 runs
// BEFORE the headings.length === 0 early-return at 1752).
document.querySelector('#note-content-rendered').innerHTML = '';
hook.setupScrollSpy();
// Prior observer disconnected...
expect(first.disconnect).toHaveBeenCalled();
// ...and NO new observer was constructed on the no-headings path.
expect(instances).toHaveLength(1);
// _tocObserver was nulled (no live observer left).
expect(hook.getTocObserver()).toBeNull();
});
});