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.

11.4 KB
Raw Blame
/** Runtime coverage for theme identity, hydration, and browser lifecycle hooks. */

const originalReadyState = Object.getOwnPropertyDescriptor(
    document,
    'readyState',
);
const originalVisibilityState = Object.getOwnPropertyDescriptor(
    document,
    'visibilityState',
);

const mediaListeners = [];
const mediaQuery = {
    matches: true,
    addEventListener: vi.fn((event, callback) => {
        if (event === 'change') mediaListeners.push(callback);
    }),
};

let theme;

beforeAll(async () => {
    // Hashed is the shipped dark default while light themes need refinement.
    // Injecting Nord verifies that fallbacks follow the configured default.
    window.LDR_DEFAULT_THEME = 'nord';
    Object.defineProperty(document, 'readyState', {
        configurable: true,
        get: () => 'loading',
    });
    window.LDR_THEME_METADATA = {
        hashed: { label: 'Hashed', icon: 'fa-hashtag', group: 'core' },
        light: { label: 'Light', icon: 'fa-sun', group: 'core' },
        nord: { label: 'Nord', icon: 'fa-snowflake', group: 'dev' },
        dracula: { label: 'Dracula', icon: 'fa-ghost', group: 'dev' },
        sepia: { label: 'Sepia', icon: 'fa-book', group: 'research' },
        system: { label: 'System', icon: 'fa-desktop', group: 'system' },
    };
    vi.stubGlobal('matchMedia', vi.fn(() => mediaQuery));
    window.api = { getCsrfToken: vi.fn(() => 'csrf-theme-lifecycle') };
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
        ok: false,
        status: 404,
        json: vi.fn().mockResolvedValue({}),
    }));

    await import('@js/services/theme.js');
    theme = window.themeService;
});

beforeEach(() => {
    localStorage.clear();
    document.head.querySelectorAll('meta[name="user-id"]').forEach(node => (
        node.remove()
    ));
    document.body.replaceChildren();
    delete document.body.dataset.userId;
    mediaQuery.matches = true;
    mediaListeners.length = 0;
    mediaQuery.addEventListener.mockClear();
    window.api = { getCsrfToken: vi.fn(() => 'csrf-theme-lifecycle') };
    vi.mocked(fetch).mockReset().mockResolvedValue({
        ok: false,
        status: 404,
        json: vi.fn().mockResolvedValue({}),
    });
});

afterAll(() => {
    localStorage.clear();
    document.body.replaceChildren();
    delete window.LDR_THEME_METADATA;
    delete window.LDR_DEFAULT_THEME;
    delete window.api;
    vi.unstubAllGlobals();
    if (originalReadyState) {
        Object.defineProperty(document, 'readyState', originalReadyState);
    } else {
        delete document.readyState;
    }
    if (originalVisibilityState) {
        Object.defineProperty(
            document,
            'visibilityState',
            originalVisibilityState,
        );
    } else {
        delete document.visibilityState;
    }
});

it('isolates persisted themes by meta user, body user, and anonymous context', () => {
    const meta = document.createElement('meta');
    meta.name = 'user-id';
    meta.content = '  alice  ';
    document.head.appendChild(meta);
    document.body.dataset.userId = 'ignored-body-user';

    theme.setTheme('light', false);
    expect(localStorage.getItem('ldr-theme-alice')).toBe('light');

    meta.remove();
    document.body.dataset.userId = '  bob  ';
    expect(theme.getCurrentTheme()).toBe('nord');
    theme.setTheme('nord', false);
    expect(localStorage.getItem('ldr-theme-bob')).toBe('nord');

    delete document.body.dataset.userId;
    expect(theme.getCurrentTheme()).toBe('nord');
    theme.setTheme('hashed', false);
    expect(localStorage.getItem('ldr-theme-anonymous')).toBe('hashed');
});

it('hydrates a valid migrated setting and updates the live dropdown', async () => {
    localStorage.setItem('ldr-theme-anonymous', 'light');
    document.body.innerHTML = '<select id="theme-dropdown"></select>';
    vi.mocked(fetch).mockResolvedValueOnce({
        ok: true,
        json: vi.fn().mockResolvedValue({ value: 'nord' }),
    });

    theme.initializeTheme();

    await vi.waitFor(() => {
        expect(theme.getCurrentTheme()).toBe('nord');
    });
    expect(fetch).toHaveBeenCalledWith('/settings/api/app.theme');
    expect(document.documentElement.dataset.theme).toBe('nord');
    expect(document.getElementById('theme-dropdown').value).toBe('nord');
    expect(document.querySelectorAll('#theme-dropdown option')).toHaveLength(
        Object.keys(window.LDR_THEME_METADATA).length,
    );
    expect(mediaQuery.addEventListener).toHaveBeenCalledWith(
        'change',
        expect.any(Function),
    );
});

it('resets a corrupted stored theme before server hydration settles', () => {
    localStorage.setItem('ldr-theme-anonymous', 'javascript:alert(1)');
    vi.mocked(fetch).mockImplementationOnce(() => new Promise(() => {}));

    theme.initializeTheme();

    expect(theme.getCurrentTheme()).toBe('nord');
    expect(document.documentElement.dataset.theme).toBe('nord');
});

it('uses the injected default when a requested theme is invalid', () => {
    theme.setTheme('removed-theme', false);

    expect(theme.getCurrentTheme()).toBe('nord');
    expect(document.documentElement.dataset.theme).toBe('nord');
});

it('reacts to a system color-scheme change only for the system preference', () => {
    theme.setTheme('system', false);
    expect(theme.getEffectiveTheme('system')).toBe('hashed');
    theme.initializeTheme();
    expect(mediaListeners).toHaveLength(1);

    mediaQuery.matches = false;
    mediaListeners[0]();
    expect(document.documentElement.dataset.theme).toBe('light');

    expect(theme.getCurrentTheme()).toBe('system');

    mediaQuery.matches = true;
    mediaListeners[0]();
    expect(document.documentElement.dataset.theme).toBe('hashed');

    theme.setTheme('sepia', false);
    mediaQuery.matches = false;
    mediaListeners[0]();
    expect(document.documentElement.dataset.theme).toBe('sepia');
    expect(theme.getCurrentTheme()).toBe('sepia');
});

it('wires one dropdown change to the migrated settings PUT with CSRF', async () => {
    document.body.innerHTML = '<select id="theme-dropdown"></select>';
    vi.mocked(fetch).mockResolvedValue({
        ok: true,
        status: 200,
        json: vi.fn().mockResolvedValue({ status: 'success' }),
    });
    theme.setupHeaderDropdown();
    theme.setupHeaderDropdown();

    const dropdown = document.getElementById('theme-dropdown');
    dropdown.value = 'dracula';
    dropdown.dispatchEvent(new Event('change'));

    await vi.waitFor(() => {
        expect(fetch).toHaveBeenCalledOnce();
    });
    expect(fetch).toHaveBeenCalledWith('/settings/api/app.theme', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-theme-lifecycle',
        },
        body: JSON.stringify({ value: 'dracula' }),
    });
    expect(theme.getCurrentTheme()).toBe('dracula');
});

it.each([
    ['an HTTP error', () => Promise.resolve({ ok: false, status: 500 })],
    ['a network error', () => Promise.reject(new Error('offline'))],
])('puts the previous theme back when the dropdown save fails with %s', async (_label, failedSave) => {
    theme.setTheme('nord', false);
    document.body.innerHTML = '<select id="theme-dropdown"></select>';
    vi.mocked(fetch).mockImplementation(failedSave);
    theme.setupHeaderDropdown();

    const dropdown = document.getElementById('theme-dropdown');
    dropdown.value = 'dracula';
    dropdown.dispatchEvent(new Event('change'));
    expect(theme.getCurrentTheme()).toBe('dracula');

    await vi.waitFor(() => {
        expect(theme.getCurrentTheme()).toBe('nord');
    });
    expect(fetch).toHaveBeenCalledOnce();
    expect(document.documentElement.dataset.theme).toBe('nord');
    expect(dropdown.value).toBe('nord');
});

it.each([
    ['fails', false, 'nord'],
    ['succeeds', true, 'dracula'],
])('returns to the last saved theme when an earlier queued save %s', async (_label, firstSaved, expectedTheme) => {
    theme.setTheme('nord', false);
    const saves = [];
    vi.mocked(fetch).mockImplementation(() => new Promise(resolve => {
        saves.push(resolve);
    }));

    theme.setTheme('dracula');
    theme.setTheme('light');
    await vi.waitFor(() => {
        expect(saves).toHaveLength(1);
    });
    saves[0]({
        ok: firstSaved,
        status: firstSaved ? 200 : 500,
        json: vi.fn().mockResolvedValue({ status: 'success' }),
    });
    await vi.waitFor(() => {
        expect(saves).toHaveLength(2);
    });
    expect(theme.getCurrentTheme()).toBe('light');
    saves[1]({ ok: false, status: 500 });

    await vi.waitFor(() => {
        expect(theme.getCurrentTheme()).toBe(expectedTheme);
    });
    expect(document.documentElement.dataset.theme).toBe(expectedTheme);
});

it('returns to the last saved theme when its response body is slow and the queued save fails', async () => {
    theme.setTheme('nord', false);
    let finishFirstBody;
    vi.mocked(fetch)
        .mockResolvedValue({ ok: false, status: 500 })
        .mockResolvedValueOnce({
            ok: true,
            status: 200,
            json: () => new Promise(resolve => {
                finishFirstBody = resolve;
            }),
        });

    theme.setTheme('dracula');
    theme.setTheme('light');
    await vi.waitFor(() => {
        expect(finishFirstBody).toBeDefined();
    });
    expect(theme.getCurrentTheme()).toBe('light');
    finishFirstBody({ status: 'success' });

    await vi.waitFor(() => {
        expect(fetch).toHaveBeenCalledTimes(2);
    });
    await vi.waitFor(() => {
        expect(theme.getCurrentTheme()).toBe('dracula');
    });
    expect(document.documentElement.dataset.theme).toBe('dracula');
});

it('keeps a theme applied after a failing save was sent', async () => {
    theme.setTheme('nord', false);
    let failSave;
    vi.mocked(fetch).mockImplementation(() => new Promise((_resolve, reject) => {
        failSave = reject;
    }));

    theme.setTheme('dracula');
    await vi.waitFor(() => {
        expect(fetch).toHaveBeenCalledOnce();
    });
    // The settings form applies its theme this way and saves it itself.
    theme.setTheme('sepia', false);
    const themeChanges = vi.fn();
    window.addEventListener('themechange', themeChanges);
    failSave(new Error('offline'));
    await Promise.resolve();
    await Promise.resolve();
    await Promise.resolve();
    window.removeEventListener('themechange', themeChanges);

    expect(fetch).toHaveBeenCalledOnce();
    expect(themeChanges).not.toHaveBeenCalled();
    expect(theme.getCurrentTheme()).toBe('sepia');
});

it('keeps the theme when a missing CSRF token stops the save', async () => {
    theme.setTheme('nord', false);
    window.api.getCsrfToken.mockReturnValue('');

    theme.setTheme('dracula');
    await Promise.resolve();
    await Promise.resolve();

    expect(fetch).not.toHaveBeenCalled();
    expect(theme.getCurrentTheme()).toBe('dracula');
});

it('repopulates an empty dropdown after bfcache restore and tab visibility', () => {
    document.body.innerHTML = '<select id="theme-dropdown"></select>';
    const dropdown = document.getElementById('theme-dropdown');
    const pageShow = new Event('pageshow');
    Object.defineProperty(pageShow, 'persisted', { value: true });

    window.dispatchEvent(pageShow);
    expect(dropdown.options.length).toBeGreaterThan(0);

    dropdown.replaceChildren();
    Object.defineProperty(document, 'visibilityState', {
        configurable: true,
        get: () => 'visible',
    });
    document.dispatchEvent(new Event('visibilitychange'));

    expect(dropdown.options.length).toBeGreaterThan(0);
});