Skip to content

~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.

test(zotero): cover the inline collection picker behaviour (#7886)

dodoshell1e47ccc46c94
diff --git a/tests/js/pages/zotero-runtime.test.js b/tests/js/pages/zotero-runtime.test.js
index f88c46c9cd..306dda07fb 100644
--- a/tests/js/pages/zotero-runtime.test.js
+++ b/tests/js/pages/zotero-runtime.test.js
@@ -44,6 +44,85 @@ function jsonResponse(payload) {
return new Response(JSON.stringify(payload), { status: 200 });
}
+const COLLECTIONS = [
+ { key: 'AAAA1111', name: 'Thesis' },
+ { key: 'BBBB2222', name: 'Reviews' },
+ { key: 'CCCC3333', name: 'Archive' },
+];
+
+// A configured library whose collections load into the inline picker. Every
+// autosave payload is recorded in `saves`, oldest first.
+function pickerFetch(currentConfig, saves) {
+ return vi.fn((url, options = {}) => {
+ if (url === '/library/api/zotero/config') {
+ return Promise.resolve(jsonResponse(currentConfig));
+ }
+ if (url === '/library/api/zotero/status') {
+ return Promise.resolve(jsonResponse({
+ success: true,
+ collections: [],
+ progress: null,
+ }));
+ }
+ if (url === '/library/api/zotero/collections') {
+ return Promise.resolve(jsonResponse({
+ success: true,
+ collections: COLLECTIONS,
+ }));
+ }
+ if (url === '/library/api/zotero/groups') {
+ return Promise.resolve(jsonResponse({ success: true, groups: [] }));
+ }
+ if (
+ url === '/settings/save_all_settings'
+ && options.method === 'POST'
+ ) {
+ saves.push(JSON.parse(options.body));
+ return Promise.resolve(jsonResponse({ status: 'success' }));
+ }
+ throw new Error(`Unexpected request: ${url}`);
+ });
+}
+
+function choice(key) {
+ return document.querySelector(
+ `.ldr-zotero-collection-choice[data-key="${key}"]`,
+ );
+}
+
+function checkedChoices() {
+ return Array.from(
+ document.querySelectorAll('.ldr-zotero-collection-choice:checked'),
+ ).map(input => input.dataset.key);
+}
+
+function toggle(input, checked) {
+ input.checked = checked;
+ input.dispatchEvent(new Event('change'));
+}
+
+// Waits until the last autosave carries `keys` and its config refresh has gone
+// out. A save queued behind an earlier one would otherwise still be running
+// after afterEach has restored the real fetch.
+async function waitForLastSave(fetchMock, saves, keys) {
+ await vi.waitFor(() => {
+ expect(saves.at(-1)?.['zotero.collection_keys']).toBe(keys);
+ const refreshes = fetchMock.mock.calls.filter(
+ ([url]) => url === '/library/api/zotero/config',
+ ).length - 1;
+ expect(refreshes).toBe(saves.length);
+ });
+}
+
+async function loadPicker(fetchMock) {
+ loadPage(fetchMock);
+ await vi.waitFor(() => {
+ expect(document.getElementById('zotero-collection-picker').hidden)
+ .toBe(false);
+ expect(choice('CCCC3333')).toBeTruthy();
+ });
+}
+
function loadPage(fetchMock) {
expect(scriptMatch, 'Zotero inline runtime not found').toBeTruthy();
document.head.innerHTML = '<meta name="csrf-token" content="csrf-zotero">';
@@ -470,3 +549,134 @@ it('preserves the last good status when refresh returns an invalid envelope', as
expect(progress.querySelector('.ldr-zotero-progress-fill').style.width)
.toBe('50%');
});
+
+it.each([
+ [['BBBB2222', 'CCCC3333'], ['BBBB2222', 'CCCC3333'], false],
+ [[], [], true],
+])('restores saved collection keys %j on the picker once collections load', async (savedKeys, checked, entireLibrary) => {
+ const saves = [];
+ await loadPicker(pickerFetch(config({
+ configured: true,
+ collection_keys: savedKeys,
+ }), saves));
+
+ expect(checkedChoices()).toEqual(checked);
+ expect(document.getElementById('zt-entire-library').checked)
+ .toBe(entireLibrary);
+ // Restoring the saved state is not a change to save.
+ expect(saves).toEqual([]);
+});
+
+it('writes picker choices to the raw field and autosaves them', async () => {
+ const saves = [];
+ const fetchMock = pickerFetch(config({ configured: true }), saves);
+ await loadPicker(fetchMock);
+ const entireLibrary = document.getElementById('zt-entire-library');
+ const field = document.getElementById('zt-collections');
+
+ toggle(choice('AAAA1111'), true);
+ expect(field.value).toBe('AAAA1111');
+ expect(entireLibrary.checked).toBe(false);
+ await waitForLastSave(fetchMock, saves, 'AAAA1111');
+
+ toggle(choice('CCCC3333'), true);
+ expect(field.value).toBe('AAAA1111, CCCC3333');
+ await waitForLastSave(fetchMock, saves, 'AAAA1111, CCCC3333');
+
+ toggle(choice('AAAA1111'), false);
+ toggle(choice('CCCC3333'), false);
+ expect(field.value).toBe('');
+ expect(entireLibrary.checked).toBe(true);
+ await waitForLastSave(fetchMock, saves, '');
+});
+
+it('clears every collection and saves an empty value when Entire library is selected', async () => {
+ const saves = [];
+ const fetchMock = pickerFetch(config({
+ configured: true,
+ collection_keys: ['AAAA1111', 'BBBB2222'],
+ }), saves);
+ await loadPicker(fetchMock);
+
+ toggle(document.getElementById('zt-entire-library'), true);
+
+ expect(checkedChoices()).toEqual([]);
+ expect(document.getElementById('zt-collections').value).toBe('');
+ await waitForLastSave(fetchMock, saves, '');
+ expect(saves).toHaveLength(1);
+});
+
+it('resyncs the picker when the raw field is edited by hand', async () => {
+ const saves = [];
+ const fetchMock = pickerFetch(config({
+ configured: true,
+ collection_keys: ['AAAA1111'],
+ }), saves);
+ await loadPicker(fetchMock);
+ const entireLibrary = document.getElementById('zt-entire-library');
+ const field = document.getElementById('zt-collections');
+
+ // ZZZZ9999 has no checkbox: it stays in the field and in the payload.
+ field.value = 'BBBB2222, ZZZZ9999';
+ field.dispatchEvent(new Event('change'));
+ expect(checkedChoices()).toEqual(['BBBB2222']);
+ expect(entireLibrary.checked).toBe(false);
+ await waitForLastSave(fetchMock, saves, 'BBBB2222, ZZZZ9999');
+
+ field.value = 'ZZZZ9999';
+ field.dispatchEvent(new Event('change'));
+ expect(checkedChoices()).toEqual([]);
+ expect(entireLibrary.checked).toBe(false);
+ await waitForLastSave(fetchMock, saves, 'ZZZZ9999');
+
+ field.value = '';
+ field.dispatchEvent(new Event('change'));
+ expect(entireLibrary.checked).toBe(true);
+ await waitForLastSave(fetchMock, saves, '');
+});
+
+it('does not autosave collection changes before the saved config has loaded', async () => {
+ const pendingConfig = deferred();
+ const fetchMock = vi.fn(url => {
+ if (url === '/library/api/zotero/config') return pendingConfig.promise;
+ if (url === '/library/api/zotero/status') {
+ return Promise.resolve(jsonResponse({
+ success: true,
+ collections: [],
+ progress: null,
+ }));
+ }
+ throw new Error(`Unexpected request: ${url}`);
+ });
+
+ loadPage(fetchMock);
+ const field = document.getElementById('zt-collections');
+ field.value = 'AAAA1111';
+ field.dispatchEvent(new Event('change'));
+ toggle(document.getElementById('zt-entire-library'), true);
+
+ expect(fetchMock).not.toHaveBeenCalledWith(
+ '/settings/save_all_settings',
+ expect.anything(),
+ );
+ expect(window.ui.showMessage).toHaveBeenCalledWith(
+ expect.stringContaining('Still loading your saved settings'),
+ 'error',
+ 6000,
+ );
+ expect(window.ui.showMessage).toHaveBeenCalledTimes(2);
+
+ pendingConfig.resolve(jsonResponse(config({
+ collection_keys: ['BBBB2222'],
+ })));
+ await vi.waitFor(() => {
+ expect(document.getElementById('zotero-config-form').dataset.configLoaded)
+ .toBe('1');
+ });
+ // The saved keys replace what was typed during loading, unsaved.
+ expect(field.value).toBe('BBBB2222');
+ expect(fetchMock).not.toHaveBeenCalledWith(
+ '/settings/save_all_settings',
+ expect.anything(),
+ );
+});