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
/**
 * Browser contract for the inline subscription-folder loader.
 *
 * The generic frontend route census only scans static/js. Extracting and
 * executing the checked-in function keeps this test bound to the code the
 * browser gets instead of copying its implementation into the fixture.
 */

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

import { extractTemplateFunction } from '../helpers/template-harness.js';

const TEMPLATE_ROOT = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages',
);

function readTemplate(name) {
    return readFileSync(resolve(TEMPLATE_ROOT, name), 'utf8');
}

function compileSubscriptionFolderLoader(subscriptionFolderId) {
    const source = extractTemplateFunction(
        readTemplate('news-subscription-form.html'),
        'loadSubscriptionFolders',
    )
        .replace(
            /\{\{\s*subscription\.folder_id\s*\}\}/g,
            String(subscriptionFolderId),
        )
        .replace(/\{%[\s\S]*?%\}/g, '');
    // The extracted source is repository-owned production code from the
    // template above, not user-controlled input.
    return new Function(`return (${source});`)(); // eslint-disable-line no-new-func
}

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

describe('news-subscription-form.html folder bootstrap', () => {
    it('loads the FastAPI array response and restores the edited folder', async () => {
        const loadSubscriptionFolders =
            compileSubscriptionFolderLoader('folder-2');
        document.body.innerHTML = `
            <select id="subscription-folder">
                <option value="">No folder</option>
                <option value="stale">Stale option</option>
            </select>
        `;
        const fetchMock = vi.fn().mockResolvedValue({
            ok: true,
            json: vi.fn().mockResolvedValue([
                { id: 'folder-1', name: 'Daily briefings' },
                { id: 'folder-2', name: 'Migration watch' },
            ]),
        });
        vi.stubGlobal('fetch', fetchMock);

        await loadSubscriptionFolders();

        expect(fetchMock).toHaveBeenCalledTimes(1);
        expect(fetchMock).toHaveBeenCalledWith(
            '/news/api/subscription/folders',
            { credentials: 'same-origin' },
        );
        const select = document.getElementById('subscription-folder');
        expect(Array.from(select.options, option => [
            option.value,
            option.textContent,
        ])).toEqual([
            ['', 'No folder'],
            ['folder-1', 'Daily briefings'],
            ['folder-2', 'Migration watch'],
        ]);
        expect(select.value).toBe('folder-2');
    });

    it('accepts the wrapped folder response used by compatible API versions', async () => {
        const loadSubscriptionFolders =
            compileSubscriptionFolderLoader('folder-3');
        document.body.innerHTML = `
            <select id="subscription-folder">
                <option value="">No folder</option>
            </select>
        `;
        vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
            ok: true,
            json: vi.fn().mockResolvedValue({
                folders: [
                    { id: 'folder-3', name: 'FastAPI migration' },
                ],
            }),
        }));

        await loadSubscriptionFolders();

        const select = document.getElementById('subscription-folder');
        expect(Array.from(select.options, option => [
            option.value,
            option.textContent,
        ])).toEqual([
            ['', 'No folder'],
            ['folder-3', 'FastAPI migration'],
        ]);
        expect(select.value).toBe('folder-3');
    });

    it('preserves the existing choice when the folder endpoint rejects the request', async () => {
        const loadSubscriptionFolders =
            compileSubscriptionFolderLoader('folder-4');
        document.body.innerHTML = `
            <select id="subscription-folder">
                <option value="folder-4" selected>Existing folder</option>
            </select>
        `;
        vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
            ok: false,
            status: 503,
            statusText: 'Unavailable',
        }));
        const error = vi.spyOn(console, 'error').mockImplementation(() => {});

        await loadSubscriptionFolders();

        const select = document.getElementById('subscription-folder');
        expect(select.value).toBe('folder-4');
        expect(select.options).toHaveLength(1);
        expect(error).toHaveBeenCalledWith(
            'Failed to load folders:',
            503,
            'Unavailable',
        );
    });
});