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.

4.7 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — restoreVersion unsaved-edits confirm.
 *
 * Restoring a version reloads the note from the server's snapshot of the
 * chosen version; it does NOT save the live editor buffer first. The old
 * confirm copy falsely promised "your current content will be saved as a new
 * version", so a mid-edit restore silently discarded unsaved edits. The fix
 * tells the truth and explicitly warns when there are unsaved edits to lose.
 *
 * Driven via the production-inert window.__noteDetailTest hook; confirm is
 * declined so the network/reload path is never reached.
 */

let hook;

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

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

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

beforeEach(() => {
    hook.setNote({ id: 'note-1' });
    window.confirm = vi.fn(() => false); // decline → no restore fetch/reload
});

describe('restoreVersion — unsaved-edits confirm copy', () => {
    it('warns that unsaved edits will be discarded when editing with unsaved changes', async () => {
        hook.setEditState({ inEdit: true, unsaved: true });

        await hook.restoreVersion('v1');

        expect(window.confirm).toHaveBeenCalledTimes(1);
        const msg = window.confirm.mock.calls[0][0].toLowerCase();
        expect(msg).toContain('unsaved edits will be discarded');
        // The old, false promise must be gone.
        expect(msg).not.toContain('saved as a new version');
    });

    it('uses an accurate copy (no false "will be saved") when there are no unsaved edits', async () => {
        hook.setEditState({ inEdit: false, unsaved: false });

        await hook.restoreVersion('v1');

        const msg = window.confirm.mock.calls[0][0].toLowerCase();
        expect(msg).toContain('replaces the current note content');
        expect(msg).not.toContain('saved as a new version');
    });
});

describe('restoreVersion — blocked while a save is in flight', () => {
    it('refuses (no confirm, no restore POST) while the save PUT is pending', async () => {
        hook.setNote({ id: 'note-1', title: 'T', content: 'body', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false });
        window.ui = { showMessage: vi.fn() };
        // Even an accepting user must not reach the restore POST.
        window.confirm = vi.fn(() => true);

        let resolvePut;
        globalThis.safeFetch = vi.fn(() => new Promise((resolve) => { resolvePut = resolve; }));

        // Read-mode save: PUT dispatched with note's content, left hanging.
        const savePromise = hook.saveNote();

        await hook.restoreVersion('v1');

        expect(window.confirm).not.toHaveBeenCalled();
        // Only the save PUT hit the network — no /restore POST raced it.
        expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
        expect(globalThis.safeFetch.mock.calls[0][0]).not.toContain('/restore');
        expect(window.ui.showMessage).toHaveBeenCalledWith('Still saving — please wait.', 'info');

        // Settle the hanging PUT (failure branch keeps DOM needs minimal).
        resolvePut({ json: () => Promise.resolve({ success: false, error: 'nope' }) });
        await savePromise;
    });
});

describe('saveNote — blocked while a restore is in flight (mirror guard)', () => {
    it("refuses (no PUT) during the restore's round-trip", async () => {
        hook.setNote({ id: 'note-1', title: 'T', content: 'body', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false });
        window.ui = { showMessage: vi.fn() };
        window.confirm = vi.fn(() => true);

        let resolveRestore;
        globalThis.safeFetch = vi.fn(() => new Promise((resolve) => { resolveRestore = resolve; }));

        // Restore POST dispatched, left hanging.
        const restorePromise = hook.restoreVersion('v1');

        const outcome = await hook.saveNote();

        // The save must refuse outright — not queue: a queued body would
        // still be pre-restore state and would overwrite the restore.
        expect(outcome).toBe('failed');
        expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
        expect(globalThis.safeFetch.mock.calls[0][0]).toContain('/restore');
        expect(window.ui.showMessage).toHaveBeenCalledWith('Restore in progress — please wait.', 'info');

        // Settle the hanging restore (failure branch keeps DOM needs minimal).
        resolveRestore({ json: () => Promise.resolve({ success: false, error: 'nope' }) });
        await restorePromise;
    });
});