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.

15.1 KB
Raw Blame
/** Actual rendered contrast, including shared controls that bypass theme tokens. */
import { test, expect, devices } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';

const pages = [
    '/', '/settings/', '/history/', '/library/', '/news/', '/chat/',
    '/notes/', '/library/collections', '/library/collections/create',
    '/library/download-manager', '/library/search/', '/library/zotero',
    '/news/subscriptions', '/metrics/', '/metrics/journals', '/benchmark/',
    '/benchmark/results', '/library/embedding-settings',
];

async function assertContrast(page, include, { requireResolved = false } = {}) {
    let builder = new AxeBuilder({ page }).withRules(['color-contrast']);
    if (include) builder = builder.include(include);
    const { violations, incomplete } = await builder.analyze();
    const failures = violations.flatMap(violation => violation.nodes.map(node => ({
        target: node.target,
        reason: node.failureSummary,
    })));
    expect(failures).toEqual([]);
    if (requireResolved) {
        expect(incomplete.flatMap(rule => rule.nodes.map(node => ({
            target: node.target,
            reason: node.any.map(check => check.message),
        }))), 'unresolved contrast is not a passing contrast check').toEqual([]);
    }
}

// Axe checks text contrast but does not detect a disappearing close icon or
// an input outline blending into its surface. Check their rendered colors too.
async function assertControlContrast(locator, pseudo = null) {
    const result = await locator.evaluate((element, pseudoElement) => {
        const foregroundStyle = getComputedStyle(element, pseudoElement);
        const foreground = pseudoElement
            ? foregroundStyle.backgroundColor : foregroundStyle.borderTopColor;
        let surface = element;
        let background = getComputedStyle(surface).backgroundColor;
        while (background === 'rgba(0, 0, 0, 0)' && surface.parentElement) {
            surface = surface.parentElement;
            background = getComputedStyle(surface).backgroundColor;
        }
        // rgb(r, g, b) is opaque; rgba(r, g, b, a) carries its alpha. Dropping
        // the alpha would turn a missing pseudo-element into opaque black.
        const channels = color => color.match(/[\d.]+/g).map(Number);
        const [fr, fg, fb, alpha = 1] = channels(foreground);
        const [br, bg, bb] = channels(background);
        // A translucent stroke is seen blended over the surface behind it.
        const blended = [[fr, br], [fg, bg], [fb, bb]]
            .map(([front, back]) => alpha * front + (1 - alpha) * back);
        const luminance = rgb => rgb
            .map(value => value / 255)
            .map(value => (value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4))
            .reduce((sum, value, index) => sum + value * [0.2126, 0.7152, 0.0722][index], 0);
        const [dark, light] = [luminance(blended), luminance([br, bg, bb])].sort((a, b) => a - b);
        return {
            ratio: (light + 0.05) / (dark + 0.05),
            alpha,
            content: pseudoElement ? foregroundStyle.content : null,
            color: foreground,
        };
    }, pseudo);
    if (pseudo) {
        expect(result.content, `${pseudo} must be rendered`).not.toBe('none');
        expect(result.content, `${pseudo} must be rendered`).not.toBe('normal');
    }
    expect(result.alpha, `icon or control boundary must not be transparent (${result.color})`).toBeGreaterThan(0);
    expect(result.ratio, 'visible icon or control boundary needs at least 3:1 contrast').toBeGreaterThanOrEqual(3);
    return result;
}

async function applyTheme(page, theme) {
    // Avoid persisting test preferences or hitting the settings write rate limit.
    await page.evaluate(id => window.themeService.setTheme(id, false), theme);
    await expect(page.locator('html')).toHaveAttribute('data-theme', theme);
    // Allow the actual color/background transitions to finish before axe reads them.
    await page.waitForTimeout(400);
}

// Exercise empty, partly indexed and fully indexed cards without creating
// collections or starting embedding work on the test server.
async function loadCollectionFixtures(page) {
    await page.route('**/library/api/collections', route => route.fulfill({ json: {
        success: true,
        collections: [
            { id: 'empty', name: 'Empty collection', description: 'Ready for your first documents.', document_count: 0 },
            { id: 'pending', name: 'Literature review — renewable energy for homes',
                description: 'Working papers and source material for the research project.',
                document_count: 5, indexed_document_count: 3, created_at: '2026-01-15T12:00:00Z',
                embedding: { provider: 'sentence_transformers', model: 'sentence-transformers/all-MiniLM-L6-v2' } },
            { id: 'indexed', name: 'Indexed collection', document_count: 3, indexed_document_count: 3 },
        ],
    } }));
    // The scheduled-indexing row appears asynchronously. It must be present to
    // reproduce the toolbar overflow; the initial hidden row can mask the bug.
    await page.route(/\/settings\/api\/document_scheduler\.(enabled|sweep_library_collections|generate_rag)$/, route =>
        route.fulfill({ json: { value: route.request().url().endsWith('.enabled') } }));
    await page.goto('/library/collections');
    await page.waitForFunction(() => window.themeService);
    await expect(page.locator('#background-sweep-toggle-row')).toBeVisible();
    await expect(page.locator('.ldr-collection-card-wrapper')).toHaveCount(3);
    await expect(page.locator('.ldr-pending-index-badge')).toHaveText('2 pending indexing');
}

async function assertHorizontallyVisible(locator) {
    const bounds = await locator.evaluate(element => {
        const rect = element.getBoundingClientRect();
        return { left: rect.left, right: rect.right, viewport: window.innerWidth };
    });
    // overflow-x:hidden can keep document.scrollWidth equal to the viewport
    // even when a button is completely clipped. Check the element itself.
    expect(bounds.left).toBeGreaterThanOrEqual(0);
    expect(bounds.right).toBeLessThanOrEqual(bounds.viewport);
}

test('collection controls remain reachable from narrow phones to desktop', async ({ page }) => {
    await loadCollectionFixtures(page);
    await applyTheme(page, 'sepia');
    for (const width of [320, 393, 768, 1024, 1440]) {
        await test.step(`${width}px`, async () => {
            await page.setViewportSize({ width, height: 900 });
            for (const selector of ['#create-collection-btn', '#auto-index-toggle', '#background-sweep-toggle']) {
                const control = page.locator(selector);
                await assertHorizontallyVisible(control);
                await control.scrollIntoViewIfNeeded();
                await control.click({ trial: true });
            }
            for (const card of await page.locator('.ldr-collection-card-wrapper').all()) {
                await assertHorizontallyVisible(card);
                for (const label of await card.locator('h3, .ldr-stat-item span').all()) {
                    await assertHorizontallyVisible(label);
                }
            }
        });
    }
    await page.setViewportSize({ width: 393, height: 851 });
    await page.getByRole('link', { name: 'Create Collection', exact: true }).click();
    await expect(page).toHaveURL(/\/library\/collections\/create$/);
    await expect(page.getByRole('heading', { name: 'Create New Collection' })).toBeVisible();
});

// Both profiles run under the standard chromium project, including CI.
for (const [profile, device] of [
    ['desktop', devices['Desktop Chrome']],
    ['mobile', devices['Pixel 5']],
]) {
    test.describe(profile, () => {
        const { defaultBrowserType: _defaultBrowserType, ...contextOptions } = device;
        test.use({ ...contextOptions, colorScheme: 'light' });

        for (const route of pages) {
            test(`all light themes have readable text on ${route}`, async ({ page }) => {
                test.setTimeout(120_000);
                await page.goto(route);
                await page.waitForFunction(() => window.themeService && window.LDR_THEME_METADATA);
                if (route === '/') {
                    // A cold provider discovery can otherwise hide half the advanced form.
                    await expect(page.locator('#model_provider')).not.toContainText('Loading providers', { timeout: 30_000 });
                }
                if (route === '/settings/') {
                    // Settings sections start collapsed on every viewport (#5425).
                    // Open the first three (database, backups, LLM) just as a
                    // reader would, so axe samples real rendered settings text.
                    for (let index = 0; index < 3; index++) {
                        await page.locator('.ldr-settings-section-header.collapsed').first().click();
                    }
                    await expect(page.locator('.ldr-settings-item:visible').first()).toBeVisible({ timeout: 30_000 });
                }
                if (route === '/metrics/journals') {
                    await expect(page.locator('#ldr-sources-grid > div').first()).toBeVisible({ timeout: 30_000 });
                }
                const themes = await page.evaluate(() => Object.entries(window.LDR_THEME_METADATA)
                    .filter(([, metadata]) => metadata.type === 'light')
                    .map(([id]) => id));
                expect(themes).toContain('sepia');
                expect(themes.length).toBeGreaterThan(1);
                for (const theme of themes) {
                    await test.step(theme, async () => {
                        await applyTheme(page, theme);
                        await assertContrast(page);
                    });
                }
            });
        }

        test('collection cards have resolved text contrast in every light theme', async ({ page, isMobile }) => {
            test.setTimeout(120_000);
            await loadCollectionFixtures(page);
            const themes = await page.evaluate(() => Object.entries(window.LDR_THEME_METADATA)
                .filter(([, metadata]) => metadata.type === 'light').map(([id]) => id));
            for (const theme of themes) {
                await test.step(theme, async () => {
                    await applyTheme(page, theme);
                    // Gradients and overlays previously caused axe to mark every
                    // card as incomplete, silently passing the violations-only scan.
                    await assertContrast(page, '.ldr-collection-card-wrapper', { requireResolved: true });
                    if (!isMobile) {
                        await page.locator('.ldr-collection-view-link').first().hover();
                        await page.waitForTimeout(400);
                        await assertContrast(page, '.ldr-collection-card-wrapper', { requireResolved: true });
                        await page.mouse.move(0, 0);
                    }
                });
            }
        });

        test('light themes keep the note editor controls visible and usable', async ({ page, isMobile }) => {
            test.setTimeout(120_000);
            await page.goto('/notes/');
            await page.waitForFunction(() => window.themeService);
            await page.locator('[data-action="create-new-note"]').first().click();
            const modal = page.locator('#noteModal');
            const close = modal.getByRole('button', { name: 'Close', exact: true });
            await expect(modal).toBeVisible();
            const themes = await page.evaluate(() => Object.entries(window.LDR_THEME_METADATA)
                .filter(([, metadata]) => metadata.type === 'light').map(([id]) => id));
            for (const theme of themes) {
                await test.step(theme, async () => {
                    await applyTheme(page, theme);
                    await assertContrast(page, '#noteModal');
                    await assertControlContrast(modal.locator('#note-title'));
                    await assertControlContrast(modal.locator('.ldr-markdown-editor-container'));
                    const resting = await assertControlContrast(close, '::before');
                    // Computed colors ignore CSS filters, so the contrast
                    // check cannot see an invert() turning the X white (the
                    // original bug). Require that nothing filters it.
                    await expect(close).toHaveCSS('filter', 'none');
                    if (!isMobile) {
                        // Bootstrap's .btn-close:hover re-applies its own icon
                        // color and a lower opacity; the icon must keep its ink.
                        await close.hover();
                        await page.waitForTimeout(400);
                        const hovered = await assertControlContrast(close, '::before');
                        expect(hovered.color, 'close icon keeps its color on hover').toBe(resting.color);
                        await expect(close).toHaveCSS('opacity', '1');
                        await expect(close).toHaveCSS('filter', 'none');
                        await page.mouse.move(0, 0);
                    }
                });
            }
            // Confirm the visible close control remains keyboard operable.
            await close.focus();
            await close.press('Enter');
            await expect(modal).toBeHidden();
        });

        test('sepia keeps focused links, hovered buttons and privacy scopes readable', async ({ page }) => {
            // Exercise the real UI cues without changing a shared test account's policy.
            // Persistence is outside this visual contrast test.
            await page.route('**/settings/api/policy.egress_scope', async route => {
                if (route.request().method() === 'PUT') {
                    await route.fulfill({ json: { status: 'success' } });
                } else {
                    await route.continue();
                }
            });
            await page.goto('/');
            await page.waitForFunction(() => window.themeService);
            await applyTheme(page, 'sepia');
            const skipLink = page.locator('.ldr-skip-link');
            await skipLink.focus();
            await expect(skipLink).toBeInViewport();
            await assertContrast(page, '.ldr-skip-link');

            await page.locator('#query').fill('Theme readability check');
            await page.locator('#start-research-btn').hover();
            await page.waitForTimeout(400);
            await assertContrast(page, '#start-research-btn');
            await page.mouse.move(0, 0);

            const scopes = await page.locator('#policy_egress_scope option').evaluateAll(options => options.map(option => option.value));
            expect(scopes).toEqual(expect.arrayContaining(['adaptive', 'public_only', 'private_only', 'strict']));
            for (const scope of scopes) {
                await page.locator('#policy_egress_scope').selectOption(scope);
                await expect(page.locator('.ldr-privacy-panel')).toHaveAttribute('data-scope', scope);
                await page.waitForTimeout(400);
                await assertContrast(page, '.ldr-privacy-panel');
            }
        });
    });
}