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.3 KB
Raw Blame
/** Runtime FastAPI contract for the shared storage-mode modal. */

import { resolve } from 'node:path';
import { compileTemplateHarness } from '../helpers/template-harness.js';

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/components/storage_mode_modal.html',
);

function renderModal({ selected = true } = {}) {
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-storage">';
    document.body.innerHTML = `
        <div id="storage-mode-modal" style="display: flex">
            <input name="storage_mode" type="radio" value="database">
            <button id="storage-mode-confirm-btn"><i class="fas fa-save"></i> Save</button>
        </div>
    `;
    document.querySelector('input[name="storage_mode"]').checked = selected;
}

function compileModal(locationStub = { reload: vi.fn() }) {
    return compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: [
            'showStorageModeModal',
            'closeStorageModeModal',
            'confirmStorageModeChange',
        ],
        dependencies: { location: locationStub },
        returnExpression: `({
            showStorageModeModal,
            closeStorageModeModal,
            confirmStorageModeChange,
        })`,
    });
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.head.replaceChildren();
    document.body.replaceChildren();
});

it('requires a storage mode before sending a settings mutation', async () => {
    renderModal({ selected: false });
    const alertMock = vi.fn();
    const fetchMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    vi.stubGlobal('fetch', fetchMock);

    await compileModal().confirmStorageModeChange();

    expect(alertMock).toHaveBeenCalledWith('Please select a storage mode');
    expect(fetchMock).not.toHaveBeenCalled();
});

it('POSTs the selected storage setting with CSRF and reloads on success', async () => {
    renderModal();
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({ status: 'success' }),
    });
    vi.stubGlobal('fetch', fetchMock);
    const locationStub = { reload: vi.fn() };

    await compileModal(locationStub).confirmStorageModeChange();

    expect(fetchMock).toHaveBeenCalledWith('/settings/save_all_settings', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-storage',
        },
        body: JSON.stringify({
            'research_library.pdf_storage_mode': 'database',
        }),
    });
    expect(document.getElementById('storage-mode-modal').style.display)
        .toBe('none');
    expect(locationStub.reload).toHaveBeenCalledOnce();
});

it('restores the storage action after an HTTP failure', async () => {
    renderModal();
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        ok: false,
        status: 403,
    }));
    const error = vi.spyOn(console, 'error').mockImplementation(() => {});
    const button = document.getElementById('storage-mode-confirm-btn');
    const originalContent = button.innerHTML;

    await compileModal().confirmStorageModeChange();

    expect(error).toHaveBeenCalled();
    expect(alertMock).toHaveBeenCalledWith(
        'Failed to update PDF storage mode. Please try again or use the Settings page.',
    );
    expect(button.disabled).toBe(false);
    expect(button.innerHTML).toBe(originalContent);
});

it('treats an HTTP-200 rejected settings envelope as a failed mutation', async () => {
    renderModal();
    const alertMock = vi.fn();
    vi.stubGlobal('alert', alertMock);
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        ok: true,
        json: vi.fn().mockResolvedValue({
            status: 'error',
            message: 'storage backend unavailable',
        }),
    }));
    vi.spyOn(console, 'error').mockImplementation(() => {});

    await compileModal().confirmStorageModeChange();

    expect(alertMock).toHaveBeenCalledWith(
        'Failed to update PDF storage mode. Please try again or use the Settings page.',
    );
    expect(document.getElementById('storage-mode-modal').style.display)
        .toBe('flex');
    expect(document.getElementById('storage-mode-confirm-btn').disabled)
        .toBe(false);
});