~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 — searchNotesForLinking stale-response guard.
*
* Locks the last-writer-wins contract for the wiki-link [[ autocomplete
* (note-detail.js, ~lines 3539-3583): when two searches overlap, a slower
* EARLIER response must not overwrite the results of a newer query.
*
* The guard is the identity bail at line 3564
* `if (wikiLinkSearchController !== controller) return;`
* plus the abort-prior-controller at line 3542 and the AbortError swallow at
* line 3576. If the stale earlier writer were allowed through, pressing
* Enter/Tab (which only gates on wikiLinkResults.length) would insert the
* WRONG [[link]].
*
* 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;
beforeAll(async () => {
window.__VITEST_TEST__ = true;
// Defensive global stub so the import never throws; replaced per-test.
globalThis.safeFetch = vi.fn(() =>
Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
);
globalThis.escapeHtml = (value) => String(value ?? '');
await import('@js/pages/note-detail.js');
hook = window.__noteDetailTest;
});
afterAll(() => {
delete window.__VITEST_TEST__;
});
beforeEach(() => {
hook.resetWikiLink();
hook.setNote({ id: 'n1' });
// wikiLinkDropdown stays null in tests, so renderWikiLinkItems /
// renderWikiLinkPlaceholder early-return — no DOM scaffold needed.
});
afterEach(() => {
hook.resetWikiLink();
hook.resetNote();
});
function flush() {
return new Promise((r) => setTimeout(r, 0));
}
describe('searchNotesForLinking — stale-response last-writer-wins', () => {
it('does not let a slow earlier response overwrite a newer query, and aborts the prior controller', async () => {
// First (older) query 'qu': resolves LATE — captured resolver.
// Second (newer) query 'que': resolves IMMEDIATELY.
let resolveOld;
const signals = [];
globalThis.safeFetch = vi.fn((url, opts) => {
signals.push(opts.signal);
if (signals.length === 1) {
// Older query — hangs until we resolve it explicitly.
return new Promise((r) => { resolveOld = r; });
}
// Newer query — settles now with the fresh results.
return Promise.resolve({
json: () => Promise.resolve({ success: true, notes: [{ id: 'new', title: 'NewResult' }] }),
});
});
// Fire the older search (in flight), then the newer one.
const p1 = hook.searchNotesForLinking('qu');
const p2 = hook.searchNotesForLinking('que');
// Starting the 2nd search aborts the 1st controller (line 3542).
expect(signals[0].aborted).toBe(true);
// Newer response settles and writes the fresh results.
await p2;
await flush();
expect(hook.getWikiLinkResults()).toEqual([{ id: 'new', title: 'NewResult' }]);
// Now let the STALE older response settle LAST.
resolveOld({
json: () => Promise.resolve({ success: true, notes: [{ id: 'old', title: 'OldResult' }] }),
});
// Must not throw, must not overwrite the newer results.
await expect(p1).resolves.toBeUndefined();
await flush();
expect(hook.getWikiLinkResults()).toEqual([{ id: 'new', title: 'NewResult' }]);
// Two fetches were issued, each given an AbortSignal.
expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
expect(globalThis.safeFetch.mock.calls[0][1]).toHaveProperty('signal');
expect(globalThis.safeFetch.mock.calls[1][1]).toHaveProperty('signal');
});
it('swallows an AbortError from a superseded fetch without throwing', async () => {
// First call rejects with an AbortError (as a real aborted fetch would);
// second call resolves normally.
globalThis.safeFetch = vi
.fn()
.mockImplementationOnce(() => {
const err = new Error('aborted');
err.name = 'AbortError';
return Promise.reject(err);
})
.mockImplementationOnce(() =>
Promise.resolve({
json: () => Promise.resolve({ success: true, notes: [{ id: 'new', title: 'NewResult' }] }),
})
);
const p1 = hook.searchNotesForLinking('qu');
const p2 = hook.searchNotesForLinking('que');
// The AbortError from the first search is swallowed (line 3576) —
// it does not reject out of searchNotesForLinking.
await expect(p1).resolves.toBeUndefined();
await p2;
await flush();
expect(hook.getWikiLinkResults()).toEqual([{ id: 'new', title: 'NewResult' }]);
});
it('clears stale results when a later search returns success:false', async () => {
// A first search populates the dropdown results.
globalThis.safeFetch = vi.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ success: true, notes: [{ id: 'a', title: 'Alpha' }] }),
})
);
await hook.searchNotesForLinking('al');
await flush();
expect(hook.getWikiLinkResults()).toEqual([{ id: 'a', title: 'Alpha' }]);
// A later search gets a 200 with success:false. Pre-fix the
// `if (data.success && data.notes)` had no else, so wikiLinkResults
// kept the stale ['Alpha'] — and Enter/Tab (gated only on length)
// would insert it. The fix clears them.
globalThis.safeFetch = vi.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ success: false, error: 'boom' }),
})
);
await hook.searchNotesForLinking('xyz');
await flush();
expect(hook.getWikiLinkResults()).toEqual([]);
});
it('invalidates an in-flight search immediately on non-empty input changes', async () => {
vi.useFakeTimers();
try {
let resolveOld;
const signals = [];
globalThis.safeFetch = vi
.fn()
.mockImplementationOnce((url, opts) => {
signals.push(opts.signal);
return new Promise((resolve) => { resolveOld = resolve; });
})
.mockImplementationOnce((url, opts) => {
signals.push(opts.signal);
return Promise.resolve({
json: () => Promise.resolve({
success: true,
notes: [{ id: 'new', title: 'New result' }],
}),
});
});
const oldSearch = hook.searchNotesForLinking('old');
hook.setWikiLinkState({
active: true,
results: [{ id: 'old', title: 'Old result' }],
index: 0,
start: 0,
end: 5,
});
document.body.innerHTML =
'<textarea id="note-content">[[new</textarea>';
const textarea = document.getElementById('note-content');
textarea.setSelectionRange(5, 5);
hook.handleWikiLinkInput(textarea);
// Input state changes immediately; replacement network work stays
// debounced. The old response cannot remain selectable meanwhile.
expect(signals[0].aborted).toBe(true);
expect(hook.getWikiLinkResults()).toEqual([]);
expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
resolveOld({
json: () => Promise.resolve({
success: true,
notes: [{ id: 'old', title: 'Old result' }],
}),
});
await oldSearch;
expect(hook.getWikiLinkResults()).toEqual([]);
await vi.advanceTimersByTimeAsync(299);
expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(1);
expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
} finally {
vi.clearAllTimers();
vi.useRealTimers();
}
});
it('clears selectable results after a current non-abort failure', async () => {
hook.setWikiLinkState({
active: true,
results: [{ id: 'old', title: 'Old result' }],
index: 0,
start: 0,
end: 5,
});
globalThis.safeFetch = vi.fn(() =>
Promise.reject(new Error('network unavailable'))
);
await expect(hook.searchNotesForLinking('new'))
.resolves.toBeUndefined();
expect(hook.getWikiLinkResults()).toEqual([]);
});
});