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.

21.6 KB
Raw Blame
/**
 * A secret configured during THIS session must stay clearable.
 *
 * `notifications.service_url` ships unset (`value: ""`, `ui_element:
 * "textarea"`), and an empty value is not redacted, so the control first
 * renders as a plain textarea with no `data-redacted`. The moment the user
 * saves a URL into it the server echoes `[REDACTED]`, which moves the
 * dirty-check baseline to `''` (a redacted control renders blank, so its
 * baseline has to be blank too). Nothing re-renders after a save, so the
 * three pieces that describe the control — the baseline, the module's
 * `redactedSettingKeys` set and the node's own `data-redacted` — have to be
 * brought into line by the write-back itself. If only the baseline moves,
 * the field still shows the URL but compares equal to `''` once emptied:
 * clearing it submits nothing, the modified marker is dropped, and the
 * webhook stays configured with no error shown.
 *
 * This drives the real module against a happy-dom settings page and pins
 * the whole sequence: configure in-session, the post-save control state,
 * that an untouched blur does NOT wipe the stored secret, that the
 * advertised clear gesture DOES reach the server, and that the Test
 * Notification button follows the same state in both directions (it tests
 * the stored URL while one is configured, and goes back to disabled once
 * the clear lands).
 *
 * That settled ordering is only half of it. A save keeps its key pending
 * until the response lands, and the response blanks the control, so there
 * is a window in which the field is blank AND a save for it is still in
 * flight. The last two tests hold a save open across that window and pin
 * what may be sent from inside it: an empty body only for the explicit
 * Enter gesture, one request per Enter (the module blurs the field after
 * an Enter save, and that re-entry must not queue a duplicate), and a
 * genuinely different value still saved while an earlier one is pending.
 */

import '@js/config/urls.js';
import '@js/services/api.js';
import '@js/utils/alert-helpers.js';
import '@js/utils/provider-options.js';
import '@js/utils/value-helpers.js';

const WEBHOOK_KEY = 'notifications.service_url';
const NICKNAME_KEY = 'app.nickname';
const SECRET_URL = 'discord://HOOKID_XYZ/TOKEN_SECRET_abcdefghijklmnop';
const NEW_URL = 'discord://NEW/TOK';
const OTHER_URL = 'discord://OTHER/TOK';
const REDACTED = '[REDACTED]';

const flushPromises = async (turns = 12) => {
    for (let turn = 0; turn < turns; turn += 1) await Promise.resolve();
};

function jsonResponse(payload, status = 200) {
    return new Response(JSON.stringify(payload), {
        status,
        headers: { 'Content-Type': 'application/json' },
    });
}

function setting(overrides) {
    return {
        category: 'general',
        description: '',
        editable: true,
        max_value: null,
        min_value: null,
        options: null,
        step: null,
        visible: true,
        ...overrides,
    };
}

beforeEach(() => {
    vi.resetModules();
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.useRealTimers();
    delete window.ui;
    delete window.matchMedia;
    delete window.modelProvidersRequestInProgress;
    delete window.searchEnginesRequestInProgress;
    delete window.modelDropdownsInitialized;
    delete window.searchEngineDropdownInitialized;
    document.body.replaceChildren();
});

it('keeps baseline, redaction set and data-redacted together when a secret is configured in-session', async () => {
    vi.useFakeTimers();
    document.head.insertAdjacentHTML(
        'beforeend',
        '<meta name="csrf-token" content="csrf-redacted-baseline">',
    );
    document.body.innerHTML = `
        <form id="settings-form">
            <input id="settings-search">
            <button type="button" class="ldr-settings-tab active" data-tab="all">All</button>
            <div id="settings-alert"></div>
            <div id="settings-content"></div>
        </form>
    `;

    // Fresh install: nothing configured, so the API returns the real
    // (empty) value and the control renders WITHOUT data-redacted.
    const settings = {
        'app.nickname': setting({
            name: 'Nickname',
            type: 'APP',
            ui_element: 'text',
            value: 'original',
        }),
        [WEBHOOK_KEY]: setting({
            category: 'notifications',
            name: 'Notification Service URL',
            type: 'APP',
            ui_element: 'textarea',
            value: '',
        }),
    };

    const saveBodies = [];
    const testUrlBodies = [];
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === URLS.SETTINGS_API.BASE) {
            return Promise.resolve(jsonResponse({ status: 'success', settings }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_MODELS) {
            return Promise.resolve(jsonResponse({
                providers: { ollama_models: [] },
                provider_options: [],
            }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
            return Promise.resolve(jsonResponse({ engine_options: [] }));
        }
        if (url === '/settings/api/data-location') {
            return Promise.resolve(jsonResponse({
                data_directory: '/ldr-data',
                security_notice: { encrypted: false },
            }));
        }
        if (url === URLS.SETTINGS_API.BACKUP_STATUS) {
            return Promise.resolve(jsonResponse({ enabled: false, count: 0, backups: [] }));
        }
        if (url === '/settings/api/notifications/test-url') {
            testUrlBodies.push(JSON.parse(options.body));
            return Promise.resolve(jsonResponse({
                success: true,
                message: 'Test notification sent successfully',
                error: '',
            }));
        }
        if (url === URLS.SETTINGS_API.SAVE_ALL_SETTINGS) {
            const body = JSON.parse(options.body);
            saveBodies.push(body);
            const responseSettings = {};
            Object.entries(body).forEach(([key, value]) => {
                settings[key] = { ...settings[key], value };
                // The server redacts a sensitive setting's echo exactly
                // when it is configured: an empty value stays readable
                // (DataSanitizer.redact_value), which is what makes the
                // configure/clear round trip flip data-redacted twice.
                const echoed = key === WEBHOOK_KEY && value !== ''
                    ? REDACTED
                    : value;
                responseSettings[key] = { ...settings[key], value: echoed };
            });
            return Promise.resolve(jsonResponse({
                status: 'success',
                settings: responseSettings,
            }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    window.ui = { showMessage: vi.fn() };
    window.matchMedia = vi.fn(() => ({ matches: false }));

    await import('@js/components/settings.js');
    if (document.readyState === 'loading') {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }
    await flushPromises();
    await vi.advanceTimersByTimeAsync(301);

    // The Test Notification button is created by setupRefreshButtons,
    // which only runs on a tab click.
    document.querySelector('[data-tab="all"]').click();
    await vi.advanceTimersByTimeAsync(101);
    await flushPromises();

    const field = () => document.getElementById('setting-notifications-service_url');
    const testButton = () => document.getElementById('test-notification-button');

    expect(field()).not.toBeNull();
    expect(testButton()).not.toBeNull();
    // Nothing configured: blank, unmarked, and nothing to test.
    expect(field().value).toBe('');
    expect(field().dataset.redacted).toBeUndefined();
    expect(testButton().disabled).toBe(true);

    // --- configure it in this session -------------------------------------
    field().value = SECRET_URL;
    field().dispatchEvent(new Event('blur'));
    await flushPromises(40);

    expect(saveBodies).toEqual([{ [WEBHOOK_KEY]: SECRET_URL }]);
    // The save echo is the sentinel, so the control must now be exactly
    // what a re-render would produce: blank, marked, and advertising the
    // clear gesture — the same state its blank baseline assumes.
    expect(field().value).toBe('');
    expect(field().dataset.redacted).toBe('true');
    expect(field().placeholder).toContain('press Enter while empty to clear');
    // ...and the unrelated control is untouched by the re-sync.
    expect(document.getElementById('setting-app-nickname').value).toBe('original');
    // The confirmation masks a value that is a secret after this save.
    expect(window.ui.showMessage).toHaveBeenLastCalledWith(
        'Service url updated', 'success', 6000,
    );

    // Configured-but-blank still counts as testable, and the empty body is
    // what tells the endpoint to use the STORED url.
    expect(testButton().disabled).toBe(false);
    testButton().click();
    await flushPromises(40);
    expect(testUrlBodies).toEqual([{ service_url: '' }]);

    // --- an untouched visit must not wipe the stored secret ---------------
    field().dispatchEvent(new Event('blur'));
    await flushPromises(40);
    expect(saveBodies).toEqual([{ [WEBHOOK_KEY]: SECRET_URL }]);

    // --- the advertised clear gesture must reach the server ---------------
    field().dispatchEvent(new KeyboardEvent('keydown', {
        key: 'Enter',
        bubbles: true,
        cancelable: true,
    }));
    await flushPromises(40);

    expect(saveBodies).toEqual([
        { [WEBHOOK_KEY]: SECRET_URL },
        { [WEBHOOK_KEY]: '' },
    ]);
    // Cleared: the echo is readable again, so the mark comes off and the
    // button stops offering to test a URL that no longer exists.
    expect(field().dataset.redacted).toBeUndefined();
    expect(field().placeholder).toBe('');
    expect(testButton().disabled).toBe(true);

    // ...and it masks one that was a secret *before* it: the write-back
    // has already dropped the key from the redaction set by the time the
    // confirmation is built, so reading only the new state here would
    // start printing values for the setting that was secret a moment ago.
    expect(window.ui.showMessage).toHaveBeenLastCalledWith(
        'Service url updated', 'success', 6000,
    );

    // The secret never reached a save confirmation.
    expect(window.ui.showMessage.mock.calls.flat().join(' ')).not.toContain(SECRET_URL);
    expect(window.ui.showMessage.mock.calls.flat().join(' ')).not.toContain(REDACTED);
});

/**
 * Mount the dashboard with the webhook ALREADY configured (the server
 * echoes the sentinel, so the control renders blank and marked), and keep
 * every save response under the test's control: `pendingSaves` holds one
 * resolver per issued request, so a save can be left in flight while the
 * next gesture is made. Queued saves only reach fetch once the request
 * they are blocked on settles, so `saveBodies` is read after settleSaves.
 */
async function mountConfigured() {
    document.body.innerHTML = `
        <form id="settings-form">
            <input id="settings-search">
            <button type="button" class="ldr-settings-tab active" data-tab="all">All</button>
            <div id="settings-alert"></div>
            <div id="settings-content"></div>
        </form>
    `;

    const settings = {
        [NICKNAME_KEY]: setting({
            name: 'Nickname',
            type: 'APP',
            ui_element: 'text',
            value: 'original',
        }),
        [WEBHOOK_KEY]: setting({
            category: 'notifications',
            name: 'Notification Service URL',
            type: 'APP',
            ui_element: 'textarea',
            value: REDACTED,
        }),
    };

    const saveBodies = [];
    const pendingSaves = [];
    const pendingNotificationTests = [];
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === URLS.SETTINGS_API.BASE) {
            return Promise.resolve(jsonResponse({ status: 'success', settings }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_MODELS) {
            return Promise.resolve(jsonResponse({
                providers: { ollama_models: [] },
                provider_options: [],
            }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
            return Promise.resolve(jsonResponse({ engine_options: [] }));
        }
        if (url === '/settings/api/data-location') {
            return Promise.resolve(jsonResponse({
                data_directory: '/ldr-data',
                security_notice: { encrypted: false },
            }));
        }
        if (url === URLS.SETTINGS_API.BACKUP_STATUS) {
            return Promise.resolve(jsonResponse({ enabled: false, count: 0, backups: [] }));
        }
        if (url === '/settings/api/notifications/test-url') {
            return new Promise(resolveResponse => {
                pendingNotificationTests.push(() => resolveResponse(jsonResponse({
                    success: true,
                    message: 'Test notification sent successfully',
                    error: '',
                })));
            });
        }
        if (url === URLS.SETTINGS_API.SAVE_ALL_SETTINGS) {
            const body = JSON.parse(options.body);
            saveBodies.push(body);
            return new Promise(resolveResponse => {
                pendingSaves.push(() => {
                    const responseSettings = {};
                    Object.entries(body).forEach(([key, value]) => {
                        settings[key] = { ...settings[key], value };
                        const echoed = key === WEBHOOK_KEY && value !== ''
                            ? REDACTED
                            : value;
                        responseSettings[key] = { ...settings[key], value: echoed };
                    });
                    resolveResponse(jsonResponse({
                        status: 'success',
                        settings: responseSettings,
                    }));
                });
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    window.ui = { showMessage: vi.fn() };
    window.matchMedia = vi.fn(() => ({ matches: false }));

    // The module initialises on import unless the document is still
    // loading, so this mount registers no DOMContentLoaded listener that a
    // later mount in the same file could re-trigger.
    vi.spyOn(document, 'readyState', 'get').mockReturnValue('complete');
    await import('@js/components/settings.js');
    await flushPromises();
    await vi.advanceTimersByTimeAsync(301);
    // Same settling sequence the first test uses to reach a rendered tab.
    document.querySelector('[data-tab="all"]').click();
    await vi.advanceTimersByTimeAsync(101);
    await flushPromises();

    // Answer everything outstanding, including saves that were queued
    // behind another save and only issued once it settled.
    const settleSaves = async (rounds = 6) => {
        for (let round = 0; round < rounds; round += 1) {
            pendingSaves.splice(0, pendingSaves.length).forEach(answer => answer());
            await flushPromises(40);
        }
    };

    return { settings, saveBodies, pendingSaves, pendingNotificationTests, settleSaves };
}

const pressEnter = element => element.dispatchEvent(new KeyboardEvent('keydown', {
    key: 'Enter',
    bubbles: true,
    cancelable: true,
}));

it('sends an empty value for a redacted control only for the Enter gesture, in flight or not', async () => {
    vi.useFakeTimers();
    const { settings, saveBodies, pendingSaves, settleSaves } = await mountConfigured();
    const field = () => document.getElementById('setting-notifications-service_url');

    // Configured before this session: blank, marked, blank baseline.
    expect(field().value).toBe('');
    expect(field().dataset.redacted).toBe('true');

    // --- replace the stored URL, and hold that save in flight -------------
    field().focus();
    // The module calls input.blur() after an Enter save; that only
    // dispatches a blur for the focused node, and the blur listener
    // re-entering handleInputChange is the sequence under test here.
    expect(document.activeElement).toBe(field());
    field().value = NEW_URL;
    field().dispatchEvent(new Event('input'));
    pressEnter(field());
    await flushPromises(40);

    expect(document.activeElement).not.toBe(field());
    expect(saveBodies).toEqual([{ [WEBHOOK_KEY]: NEW_URL }]);
    expect(pendingSaves).toHaveLength(1);

    // --- the save is still in flight; the control is still marked ---------
    // Emptying it and leaving is not the clear gesture: the field renders
    // blank either way, so nothing here may be sent. The key stays pending
    // for as long as the save is open, and that alone used to make this
    // blur look like a change worth saving.
    field().value = '';
    field().dispatchEvent(new Event('input'));
    field().dispatchEvent(new Event('blur'));
    field().dispatchEvent(new Event('blur'));
    await flushPromises(40);

    // Settle first: a submit made from inside that window is queued behind
    // the open save and only reaches fetch once it lands, so the strays
    // are invisible until here.
    await settleSaves();
    expect(saveBodies.filter(body => body[WEBHOOK_KEY] === '')).toEqual([]);
    expect(saveBodies).toEqual([{ [WEBHOOK_KEY]: NEW_URL }]);
    // The response left the control in its redacted, blank state.
    expect(field().value).toBe('');
    expect(field().dataset.redacted).toBe('true');

    // --- the gesture that IS a clear still reaches the server -------------
    // Start another save and clear the field while that one is open, so
    // the gesture is made from the same in-flight window the strays were.
    field().focus();
    field().value = OTHER_URL;
    field().dispatchEvent(new Event('input'));
    pressEnter(field());
    await flushPromises(40);
    expect(saveBodies).toHaveLength(2);
    expect(pendingSaves).toHaveLength(1);

    field().focus();
    field().value = '';
    field().dispatchEvent(new Event('input'));
    pressEnter(field());
    await flushPromises(40);
    await settleSaves();

    // One clear, made by the gesture; the strays above sent nothing.
    expect(saveBodies).toEqual([
        { [WEBHOOK_KEY]: NEW_URL },
        { [WEBHOOK_KEY]: OTHER_URL },
        { [WEBHOOK_KEY]: '' },
    ]);
    expect(settings[WEBHOOK_KEY].value).toBe('');
    // Cleared: the echo is readable again, so the mark comes off.
    expect(field().dataset.redacted).toBeUndefined();
});

it('queues one save per Enter and still saves a value changed while one is pending', async () => {
    vi.useFakeTimers();
    const { settings, saveBodies, settleSaves } = await mountConfigured();
    const field = () => document.getElementById('setting-notifications-service_url');
    const nickname = () => document.getElementById('setting-app-nickname');

    field().focus();
    field().value = NEW_URL;
    field().dispatchEvent(new Event('input'));
    pressEnter(field());
    await flushPromises(40);
    expect(document.activeElement).not.toBe(field());

    // A different value typed while that save is open is a real intent:
    // the earlier write may commit after it, so it still has to be sent.
    field().value = OTHER_URL;
    field().dispatchEvent(new Event('input'));
    field().dispatchEvent(new Event('blur'));
    await flushPromises(40);
    await settleSaves();

    // Two requests, not three: the programmatic blur after the Enter
    // carried the value already in flight and added nothing.
    expect(saveBodies).toEqual([
        { [WEBHOOK_KEY]: NEW_URL },
        { [WEBHOOK_KEY]: OTHER_URL },
    ]);
    expect(settings[WEBHOOK_KEY].value).toBe(OTHER_URL);

    // ...and on a control that is not redacted at all, a reversal all the
    // way back to the acknowledged value while a save is pending is still
    // a write, because that older save can commit after it.
    const beforeReversal = saveBodies.length;
    nickname().value = 'changed';
    nickname().dispatchEvent(new Event('blur'));
    await flushPromises(40);
    nickname().value = 'original';
    nickname().dispatchEvent(new Event('blur'));
    await flushPromises(40);
    await settleSaves();

    expect(saveBodies.slice(beforeReversal).map(body => body[NICKNAME_KEY]))
        .toEqual(['changed', 'original']);
    expect(settings[NICKNAME_KEY].value).toBe('original');
});


it('keeps an active notification test disabled across other saves and a clear', async () => {
    vi.useFakeTimers();
    const { pendingNotificationTests, settleSaves } = await mountConfigured();
    const button = () => document.getElementById('test-notification-button');
    const field = () => document.getElementById('setting-notifications-service_url');
    const nickname = document.getElementById('setting-app-nickname');

    button().click();
    await flushPromises(40);
    expect(pendingNotificationTests).toHaveLength(1);
    expect(button().disabled).toBe(true);

    // Saving another key must not discard the active request's busy state.
    nickname.value = 'updated while testing';
    nickname.dispatchEvent(new Event('blur'));
    await settleSaves();
    expect(button().disabled).toBe(true);
    button().click();
    await flushPromises(40);
    expect(pendingNotificationTests).toHaveLength(1);

    // Completing the earlier test must consult the newly cleared setting.
    field().focus();
    field().value = '';
    pressEnter(field());
    await settleSaves();
    expect(field().dataset.redacted).toBeUndefined();
    pendingNotificationTests[0]();
    await flushPromises(40);
    expect(button().disabled).toBe(true);
    expect(button().textContent).not.toContain('Testing');
});