~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 — loadVersionHistory deferred-refresh queue.
*
* Locks the save-vs-append concurrency contract in note-detail.js
* (loadVersionHistory, ~lines 3099-3136):
*
* - A non-append refresh (e.g. saveNote's post-save reload) requested while
* an append load ("Show older versions") is in flight must NOT be dropped
* by the re-entrancy guard. It is queued (_versionsPendingRefresh = true)
* and re-fired from the finally block once the in-flight load clears.
* - A *second append* while loading is dropped silently and does NOT queue
* (only non-append refreshes set the pending flag) — so no spurious
* re-fire happens.
*
* These exercise module-private state via the production-inert
* window.__noteDetailTest hook in note-detail.js. The auto-init is skipped
* because the harness sets window.__VITEST_TEST__ before import.
*/
let hook;
beforeAll(async () => {
// Skip the page bootstrap (DOMContentLoaded -> loadNote/fetch/Bootstrap).
window.__VITEST_TEST__ = true;
// loadVersionHistory calls a bare global safeFetch.
// Stub it so the module import (and unrelated code paths) never throw.
globalThis.safeFetch = vi.fn(() =>
Promise.resolve({ json: () => Promise.resolve({ success: true, versions: [], total: 0 }) })
);
// renderVersionHistory / renderSidebarLoadError use these bare globals
// (provided by shared <script> tags in production). Identity / no-op
// stubs keep the render paths from throwing under happy-dom.
globalThis.escapeHtml = (s) => String(s);
globalThis.formatDateTime = (s) => String(s);
await import('@js/pages/note-detail.js');
hook = window.__noteDetailTest;
});
afterAll(() => {
delete window.__VITEST_TEST__;
});
beforeEach(() => {
// Fresh module state for each test.
hook.setNote({ id: 'n1' });
hook.setVersions([]);
// renderVersionHistory queries #ldr-versions-list; provide it so the
// success path can render without throwing (innerHTML write is harmless).
document.body.innerHTML = '<div id="ldr-versions-list"></div>';
globalThis.safeFetch = vi.fn();
});
afterEach(() => {
hook.resetNote();
});
/** Resolve any pending microtasks (await-chains inside the SUT). */
function flush() {
return new Promise((r) => setTimeout(r, 0));
}
describe('auto-init guard (__VITEST_TEST__)', () => {
it('does not register/run the page bootstrap when imported under Vitest', () => {
// The module was imported with window.__VITEST_TEST__ = true, so the
// `!window.__VITEST_TEST__` guard around the DOMContentLoaded handler
// must have prevented it from being registered. If the guard were
// removed, the handler would be registered and firing DOMContentLoaded
// here would run the page bootstrap — which calls
// `new bootstrap.Modal(...)` and throws (bootstrap is not defined),
// because we never stub it. So: a no-throw dispatch proves the guard
// is intact; a removed guard makes this dispatch throw.
expect(typeof globalThis.bootstrap).toBe('undefined');
expect(() => {
document.dispatchEvent(new Event('DOMContentLoaded'));
}).not.toThrow();
});
});
describe('loadVersionHistory — deferred-refresh queue', () => {
it('queues a save-refresh behind an in-flight append and re-fires it after', async () => {
// First (append) call: a fetch that hangs until we resolve it.
let resolveFirst;
globalThis.safeFetch.mockImplementationOnce(
() => new Promise((r) => { resolveFirst = r; })
);
// Kick off the append load — it sets _versionsLoading and awaits.
const appendPromise = hook.loadVersionHistory(true);
expect(hook.getVersionsLoading()).toBe(true);
expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
// append offset == _versions.length == 0 (empty accumulator)
expect(globalThis.safeFetch.mock.calls[0][0]).toContain('offset=0');
// While in flight, the save-triggered refresh (append=false) arrives.
// It must NOT issue a second fetch; it queues instead.
await hook.loadVersionHistory(false);
expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
expect(hook.getPendingRefresh()).toBe(true);
// The deferred refresh's fetch (the 2nd safeFetch call) resolves
// immediately with one new version.
globalThis.safeFetch.mockImplementationOnce(() =>
Promise.resolve({
json: () => Promise.resolve({
success: true,
versions: [{ id: 'v', version_number: 1, change_type: 'manual_save', created_at: '2025-01-01' }],
total: 1,
}),
})
);
// Resolve the hung append fetch -> its finally block clears the guard
// and re-fires loadVersionHistory(false).
resolveFirst({
json: () => Promise.resolve({ success: true, versions: [], total: 0 }),
});
await appendPromise;
await flush();
// The queued refresh ran: a SECOND fetch was issued, with offset=0
// (refresh, not append), and the pending flag is cleared.
expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
expect(globalThis.safeFetch.mock.calls[1][0]).toContain('offset=0');
expect(hook.getPendingRefresh()).toBe(false);
expect(hook.getVersionsLoading()).toBe(false);
});
it('drops a second append while loading without queueing a re-fire', async () => {
let resolveFirst;
globalThis.safeFetch.mockImplementationOnce(
() => new Promise((r) => { resolveFirst = r; })
);
const appendPromise = hook.loadVersionHistory(true);
expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
expect(hook.getVersionsLoading()).toBe(true);
// A second append while the first is in flight: the re-entrancy guard
// returns early, and because append==true it does NOT set the pending
// flag (only non-append refreshes queue).
await hook.loadVersionHistory(true);
expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
expect(hook.getPendingRefresh()).toBe(false);
// Resolve the in-flight load. No deferred refresh should re-fire.
resolveFirst({
json: () => Promise.resolve({ success: true, versions: [], total: 0 }),
});
await appendPromise;
await flush();
// Still exactly one fetch — the dropped append was not queued.
expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
expect(hook.getPendingRefresh()).toBe(false);
expect(hook.getVersionsLoading()).toBe(false);
});
});