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.

3.2 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — read-mode save re-syncs from `note`.
 *
 * saveNote serializes its PUT from the DOM editor (titleInput/contentEditor),
 * but read-mode features (accept-suggested-link, etc.) mutate the `note`
 * singleton WITHOUT touching those hidden editor fields. So every read-mode
 * save (togglePin, addSuggestedTag) first re-syncs the editor from `note`:
 *     titleInput.value = note.title; contentEditor.value = note.content;
 * Without it, pinning right after accepting a link would PUT the pre-link
 * editor content and silently strip the just-added [[link]] server-side.
 *
 * This characterization test LOCKS that invariant (the prerequisite for any
 * future single-source-of-truth refactor — finding A5). The PUT is answered
 * with success:false so saveNote short-circuits before its read-mode
 * re-render, keeping the harness focused on the request body.
 *
 * Driven via the production-inert window.__noteDetailTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
    );
    globalThis.getCSRFToken = () => 'csrf';

    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

afterAll(() => {
    delete window.__VITEST_TEST__;
});

describe('read-mode save re-syncs the editor from `note`', () => {
    it('togglePin PUTs note.content (re-synced), not the stale editor buffer', async () => {
        document.body.innerHTML =
            '<input id="ldr-note-title" /><textarea id="note-content"></textarea>';
        hook.bindEditorRefs();
        window.ui = { showMessage: vi.fn() };

        // A read-mode feature mutated `note` (e.g. accept-suggested-link
        // appended a wiki-link) WITHOUT updating the hidden editor fields.
        hook.setNote({
            id: 'note-1',
            title: 'Real Title',
            content: 'real body with [[Target]]',
            tags: ['t'],
            pinned: false,
        });
        // The editor still holds the STALE pre-mutation values.
        document.getElementById('ldr-note-title').value = 'stale title';
        document.getElementById('note-content').value = 'stale body';

        let putBody = null;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'PUT') {
                putBody = JSON.parse(opts.body);
                // success:false → saveNote returns 'failed' before re-render.
                return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: false, error: 'x' }) });
            }
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
        });

        await hook.togglePin();

        // The PUT carries note.* (re-synced), NOT the stale editor buffer —
        // so the just-added [[Target]] link survives the read-mode save.
        expect(putBody).not.toBeNull();
        expect(putBody.content).toBe('real body with [[Target]]');
        expect(putBody.title).toBe('Real Title');
        // togglePin flips pinned before saving.
        expect(putBody.pinned).toBe(true);
    });
});