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.

17.1 KB
Raw Blame
/**
 * Puppeteer test for OpenAI API key configuration through the UI.
 *
 * This test verifies:
 * 1. User can navigate to settings
 * 2. User can enter OpenAI API key
 * 3. Settings are saved correctly
 * 4. API key is used for research
 */

const puppeteer = require('puppeteer');
const { expect } = require('chai');
const { getLaunchOptions } = require('./helpers');

// Test configuration
const BASE_URL = process.env.TEST_URL || 'http://localhost:5000';
const TEST_USERNAME = 'test_openai_user';
const TEST_PASSWORD = 'Test_password_123';
const TEST_OPENAI_KEY = 'sk-test-1234567890abcdef';

/**
 * Expand the settings section that owns a control, so a real mouse
 * interaction can reach it.
 *
 * Settings sections start collapsed on every viewport, and
 * `.ldr-settings-section-body.collapsed { display: none }` takes the body out
 * of the layout: `page.$` still finds the control (it does not require
 * visibility) but `click()` has no clickable point. Same helper as the one in
 * test_deep_functionality.js — this file, like loginUser below, keeps its own
 * copy rather than reaching across suites.
 *
 * Not equivalent to tests/ui_tests/test_lib/test_utils.js's
 * `expandSettingsSectionFor`: a string `target` here is resolved once via
 * `page.$(target)` into a handle and reused, with no `waitForSelector` first
 * and no re-query — unlike test_utils.js, which `await
 * page.waitForSelector(target, { timeout })`s and then `page.$eval(target,
 * expandFor)`s the live element.
 *
 * A target that is already visible, is not inside a section, or is not on the
 * page is left alone.
 *
 * @param {object} page - Puppeteer page
 * @param {object|string} target - ElementHandle for the control, or a CSS selector
 * @returns {Promise<string>} outcome, for logging
 */
async function expandSettingsSectionFor(page, target) {
    const expandFor = (el) => {
        if (!el) return 'no-target';
        const body = el.closest('.ldr-settings-section-body');
        if (!body) return 'no-section';
        if (!body.classList.contains('collapsed')) return 'already-expanded';

        // Resolve the header by node relationship / data-target equality
        // instead of building a selector out of the server-derived body.id.
        let header = body.previousElementSibling;
        if (!header || !header.classList.contains('ldr-settings-section-header')) {
            header = Array.from(document.querySelectorAll('.ldr-settings-section-header'))
                .find(candidate => candidate.dataset.target === body.id) || null;
        }
        if (!header) return 'no-header';
        header.click();
        return 'expanded';
    };

    const handle = typeof target === 'string' ? await page.$(target) : target;
    if (!handle) return 'no-target';

    const outcome = await page.evaluate(expandFor, handle);
    if (outcome === 'expanded') {
        await page.waitForFunction(
            (el) => {
                if (!el) return false;
                const body = el.closest('.ldr-settings-section-body');
                return !body || !body.classList.contains('collapsed');
            },
            { timeout: 10000 },
            handle
        );
    }
    return outcome;
}

describe('OpenAI API Key Configuration UI Test', function() {
    this.timeout(60000); // 60 second timeout for UI tests

    let browser;
    let page;

    before(async () => {
        // Shared options include the seeded profile that disables Chrome's
        // password leak detection (see helpers/index.js and #4430).
        browser = await puppeteer.launch(getLaunchOptions());
        page = await browser.newPage();

        // Set viewport for consistent testing
        await page.setViewport({ width: 1280, height: 720 });

        // Enable console logging for debugging
        page.on('console', msg => console.log('Browser console:', msg.text()));
    });

    after(async () => {
        if (browser) {
            await browser.close();
        }
    });

    beforeEach(async () => {
        // Navigate to home page before each test
        await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
    });

    describe('User Registration and Login', () => {
        it('should register a new user', async () => {
            // Click on Register link. The auth blueprint mounts at
            // /auth, so the link target is /auth/register, not /register.
            await page.waitForSelector('a[href="/auth/register"]');
            await page.click('a[href="/auth/register"]');

            // Wait for registration form. The form has no id; match on
            // the form action URL instead, which is stable.
            await page.waitForSelector('form[action="/auth/register"]');

            // Fill registration form
            await page.type('input[name="username"]', TEST_USERNAME);
            await page.type('input[name="password"]', TEST_PASSWORD);
            await page.type('input[name="confirm_password"]', TEST_PASSWORD);

            // The form's client-side JS blocks submit until the
            // acknowledge checkbox is ticked (encryption warning).
            await page.click('input[name="acknowledge"]');

            // Submit form
            await page.click('button[type="submit"]');

            // Wait for redirect to login or dashboard
            await page.waitForNavigation({ waitUntil: 'domcontentloaded' });

            // Check if registration was successful. Under FastAPI, a
            // successful registration auto-creates a session and
            // redirects to ``/`` (the research dashboard); a failure
            // keeps the user on /auth/register or sends them to
            // /auth/login. Accept any of those.
            const url = page.url();
            const ok = url.endsWith('/') ||
                       url.includes('/auth/login') ||
                       url.includes('/auth/register');
            expect(ok, `Unexpected post-register URL: ${url}`).to.be.true;
        });

        it('should login with credentials', async () => {
            // Navigate to login if not already there. After successful
            // registration the page may already be at ``/`` with a live
            // session — re-go to the login URL so we exercise the form.
            const currentUrl = page.url();
            if (!currentUrl.includes('/auth/login')) {
                await page.goto(`${BASE_URL}/auth/login`, {
                    waitUntil: 'domcontentloaded',
                });
            }

            // Wait for login form
            await page.waitForSelector('form[action="/auth/login"]');

            // Fill login form. The csrf_token hidden field is already
            // populated by the template render — page.type only adds
            // text to the focused input, so we don't disturb it.
            await page.type('input[name="username"]', TEST_USERNAME);
            await page.type('input[name="password"]', TEST_PASSWORD);

            // Submit form
            await page.click('button[type="submit"]');

            // Wait for redirect away from /auth/login
            await page.waitForNavigation({ waitUntil: 'domcontentloaded' });

            // Verify we're logged in: post-login lands on ``/`` (the
            // research dashboard); a failure stays on /auth/login.
            const finalUrl = page.url();
            expect(
                finalUrl.includes('/auth/login'),
                `Login appears to have failed; URL: ${finalUrl}`
            ).to.be.false;
        });
    });

    describe('OpenAI API Key Configuration', () => {
        beforeEach(async () => {
            // Ensure we're logged in
            await loginUser(page, TEST_USERNAME, TEST_PASSWORD);
        });

        it('should navigate to settings page', async () => {
            // Click on Settings link/button
            const settingsSelector = 'a[href="/settings"], button:contains("Settings"), .settings-link';
            await page.waitForSelector(settingsSelector);
            await page.click(settingsSelector);

            // Wait for settings page to load
            await page.waitForSelector('.settings-page, #settings, [data-page="settings"]');

            // Verify we're on settings page
            const url = page.url();
            expect(url).to.include('/settings');
        });

        it('should configure OpenAI provider and API key', async () => {
            // Navigate to settings
            await page.goto(`${BASE_URL}/settings`, { waitUntil: 'domcontentloaded' });

            // Select OpenAI as provider
            const providerSelect = await page.$('select[name="llm.provider"], #llm-provider');
            if (providerSelect) {
                await page.select('select[name="llm.provider"], #llm-provider', 'openai');
            } else {
                // Alternative: click on OpenAI option if using custom dropdown
                await page.click('[data-provider="openai"], .provider-option[data-value="openai"]');
            }

            // Wait for OpenAI settings to appear. The control renders
            // inside a section body that starts collapsed, so expand it first
            // or the `visible: true` wait can never be satisfied.
            await page.waitForSelector('input[name="llm.openai.api_key"], #openai-api-key');
            await expandSettingsSectionFor(page, 'input[name="llm.openai.api_key"], #openai-api-key');
            await page.waitForSelector('input[name="llm.openai.api_key"], #openai-api-key', { visible: true });

            // Clear and enter API key
            const apiKeyInput = await page.$('input[name="llm.openai.api_key"], #openai-api-key');
            await apiKeyInput.click({ clickCount: 3 }); // Select all
            await apiKeyInput.type(TEST_OPENAI_KEY);

            // Select model (optional)
            const modelSelect = await page.$('select[name="llm.model"], #llm-model');
            if (modelSelect) {
                await page.select('select[name="llm.model"], #llm-model', 'gpt-3.5-turbo');
            }

            // Save settings
            await page.click('button[type="submit"], .save-settings, #save-settings');

            // Wait for save confirmation
            await page.waitForSelector('.success-message, .toast-success, [data-status="success"]', {
                timeout: 5000
            });

            // Verify settings were saved
            const successMessage = await page.$eval(
                '.success-message, .toast-success, [data-status="success"]',
                el => el.textContent
            );
            expect(successMessage).to.include('saved').or.to.include('success');
        });

        it('should mask API key after saving', async () => {
            // Navigate to settings
            await page.goto(`${BASE_URL}/settings`, { waitUntil: 'domcontentloaded' });

            // Wait for settings to load
            await page.waitForSelector('input[name="llm.openai.api_key"], #openai-api-key');

            // Check if API key is masked
            const apiKeyInput = await page.$('input[name="llm.openai.api_key"], #openai-api-key');
            const inputType = await apiKeyInput.evaluate(el => el.type);
            const inputValue = await apiKeyInput.evaluate(el => el.value);

            // Should be password type or show masked value
            expect(inputType).to.equal('password').or.expect(inputValue).to.match(/\*{4,}/);
        });
    });

    describe('Research with OpenAI', () => {
        beforeEach(async () => {
            // Ensure we're logged in and have OpenAI configured
            await loginUser(page, TEST_USERNAME, TEST_PASSWORD);
        });

        it('should perform research using configured OpenAI API', async () => {
            // Navigate to home/research page
            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });

            // Find and fill research query input
            await page.waitForSelector('input[name="query"], #query, .search-input');
            await page.type('input[name="query"], #query, .search-input', 'What is machine learning?');

            // Click search/research button
            await page.click('button[type="submit"], .search-button, #start-research');

            // Wait for research to start
            await page.waitForSelector('.research-status, .loading, [data-status="running"]', {
                timeout: 10000
            });

            // Wait for research to complete (with longer timeout)
            await page.waitForSelector('.research-complete, .research-summary, [data-status="completed"]', {
                timeout: 30000
            });

            // Verify results contain expected content
            const summaryText = await page.$eval(
                '.research-summary, .summary-content, [data-content="summary"]',
                el => el.textContent
            );

            expect(summaryText).to.include('machine learning').or.to.include('Machine Learning');
            expect(summaryText.length).to.be.greaterThan(100); // Should have substantial content
        });

        it('should show error if API key is invalid', async () => {
            // Configure with invalid API key
            await page.goto(`${BASE_URL}/settings`, { waitUntil: 'domcontentloaded' });

            // Set invalid API key
            const apiKeyInput = await page.$('input[name="llm.openai.api_key"], #openai-api-key');
            await expandSettingsSectionFor(page, apiKeyInput);
            await apiKeyInput.click({ clickCount: 3 });
            await apiKeyInput.type('sk-invalid-key');

            // Save settings
            await page.click('button[type="submit"], .save-settings, #save-settings');
            await page.waitForTimeout(1000); // Wait for save

            // Try to perform research
            await page.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
            await page.type('input[name="query"], #query, .search-input', 'Test query');
            await page.click('button[type="submit"], .search-button, #start-research');

            // Wait for error message
            await page.waitForSelector('.error-message, .toast-error, [data-status="error"]', {
                timeout: 15000
            });

            // Verify error message mentions API key or authentication
            const errorText = await page.$eval(
                '.error-message, .toast-error, [data-status="error"]',
                el => el.textContent
            );

            expect(errorText.toLowerCase()).to.include('api').or.include('auth').or.include('key');
        });
    });

    describe('Settings Persistence', () => {
        it('should persist OpenAI settings across sessions', async () => {
            // Login
            await loginUser(page, TEST_USERNAME, TEST_PASSWORD);

            // Configure OpenAI
            await page.goto(`${BASE_URL}/settings`, { waitUntil: 'domcontentloaded' });
            await page.select('select[name="llm.provider"], #llm-provider', 'openai');
            await expandSettingsSectionFor(page, 'input[name="llm.openai.api_key"], #openai-api-key');
            await page.type('input[name="llm.openai.api_key"], #openai-api-key', TEST_OPENAI_KEY);
            await page.click('button[type="submit"], .save-settings, #save-settings');

            // Wait for save
            await page.waitForTimeout(2000);

            // Logout
            await page.click('a[href="/logout"], .logout-button, #logout');
            await page.waitForNavigation({ waitUntil: 'domcontentloaded' });

            // Login again
            await loginUser(page, TEST_USERNAME, TEST_PASSWORD);

            // Go to settings
            await page.goto(`${BASE_URL}/settings`, { waitUntil: 'domcontentloaded' });

            // Verify provider is still OpenAI
            const selectedProvider = await page.$eval(
                'select[name="llm.provider"], #llm-provider',
                el => el.value
            );
            expect(selectedProvider).to.equal('openai');

            // Verify API key field has value (even if masked)
            const apiKeyValue = await page.$eval(
                'input[name="llm.openai.api_key"], #openai-api-key',
                el => el.value
            );
            expect(apiKeyValue).to.not.be.empty;
        });
    });
});

// Helper function to login.
//
// Idempotent: if a session is already active on the page, this is a
// no-op. Otherwise navigates to /auth/login and submits the form.
async function loginUser(page, username, password) {
    // Navigate to /auth/login. If a session is already live, the
    // server redirects to ``/``; we detect that by URL after navigate.
    await page.goto(`${BASE_URL}/auth/login`, {
        waitUntil: 'domcontentloaded',
    });
    const arrivedAt = page.url();
    if (!arrivedAt.includes('/auth/login')) {
        // Already logged in — server redirected away from the login
        // page. Nothing to do.
        return;
    }

    // Fill and submit login form. csrf_token hidden field is already
    // populated by the template; page.type doesn't disturb it.
    await page.type('input[name="username"]', username);
    await page.type('input[name="password"]', password);
    await page.click('button[type="submit"]');

    // Wait for redirect
    await page.waitForNavigation({ waitUntil: 'domcontentloaded' });
}