Files
Agent-Workbench/scripts/verify-orven-appearance.mjs

188 lines
14 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench } from '../tests/helpers.mjs';
const disposers = [], t = { after: dispose => disposers.push(dispose) };
const f = fixture(t, { modernDefaults: true }); f.store.close();
const host = await launchWorkbench(t, f.data);
assert.notEqual(new URL(host.url).port, '4318');
const output = path.resolve(process.env.ORVEN_QA_OUTPUT ?? 'output/playwright/orven-appearance'); fs.mkdirSync(output, { recursive: true });
const browser = await chromium.launch({ channel: 'msedge', headless: true });
const context = await browser.newContext({ viewport: { width: 1440, height: 960 } });
const page = await context.newPage(), checks = [], errors = [], mutations = [], contrast = [];
let persistent;
function observeContext(context) {
const observePage = page => page.on('pageerror', error => errors.push(error.message));
context.pages().forEach(observePage); context.on('page', observePage);
context.on('request', request => { if (request.url().includes('/api/') && request.method() !== 'GET') mutations.push(request.method() + ' ' + new URL(request.url()).pathname); });
}
observeContext(context);
const mark = value => { checks.push(value); console.log('PASS ' + value); };
const chosen = (page, palette) => expect(page.locator('html')).toHaveAttribute('data-palette', palette);
const appearance = () => page.getByRole('dialog', { name: '外观', exact: true });
const shot = name => page.screenshot({ path: path.join(output, name + '.png'), fullPage: true });
async function panelFits(page) {
const fits = await page.getByRole('dialog', { name: '外观', exact: true }).evaluate(dialog => {
const rect = dialog.getBoundingClientRect();
return rect.left >= 0 && rect.right <= innerWidth && rect.top >= 0 && rect.bottom <= innerHeight && dialog.scrollWidth <= dialog.clientWidth + 1 && dialog.scrollHeight <= dialog.clientHeight + 1;
});
assert.ok(fits, 'all three choices and saved state should fit within the viewport');
}
async function sample(locator, label) {
const colors = await locator.evaluate(element => {
let node = element, background = 'rgb(255, 255, 255)';
while (node) { const color = getComputedStyle(node).backgroundColor; if (color !== 'transparent' && color !== 'rgba(0, 0, 0, 0)') { background = color; break; } node = node.parentElement; }
return { foreground: getComputedStyle(element).color, background };
});
const luminance = color => color.match(/[\d.]+/g).slice(0, 3).map(Number).map(value => { const n = value / 255; return n <= .04045 ? n / 12.92 : ((n + .055) / 1.055) ** 2.4; }).reduce((sum, value, index) => sum + value * [.2126, .7152, .0722][index], 0);
const a = luminance(colors.foreground), b = luminance(colors.background), ratio = Math.round((Math.max(a, b) + .05) / (Math.min(a, b) + .05) * 100) / 100;
contrast.push({ label, ...colors, ratio }); assert.ok(ratio >= 4.5, label + ': ' + ratio);
}
try {
await page.goto(host.url);
await page.locator('.connection.connected').waitFor(); await chosen(page, 'green');
await page.getByLabel('任务内容').fill('这段草稿在切换配色后应当保留。');
await page.keyboard.press('Control+k');
await page.getByLabel('搜索工作台工具').fill('外观');
await page.getByRole('dialog', { name: '工具箱', exact: true }).getByRole('button', { name: '外观', exact: true }).click();
await expect(appearance().getByRole('radio', { name: '深青绿', exact: true })).toBeChecked();
await expect(appearance().getByRole('radio')).toHaveCount(3);
await appearance().getByRole('radio', { name: '雾蓝', exact: true }).check();
await chosen(page, 'blue');
await expect(page.locator('.appearance-note')).toContainText('已设为默认配色,下次打开会继续使用');
await expect(page.locator('meta[name="theme-color"]')).toHaveAttribute('content', '#4f6ef7');
await expect(page.locator('link[rel="icon"]')).toHaveAttribute('href', '/brand/orven-icon-blue.svg');
assert.equal(await page.locator('.task-pane').evaluate(element => getComputedStyle(element).backgroundColor), 'rgb(246, 248, 252)');
await sample(page.locator('.appearance-choice.selected .appearance-choice-description'), 'blue appearance description');
await sample(page.locator('.appearance-note'), 'blue saved state');
await shot('appearance-blue');
await page.keyboard.press('Escape');
await expect(page.getByLabel('任务内容')).toHaveValue('这段草稿在切换配色后应当保留。');
await page.reload(); await page.locator('.connection.connected').waitFor(); await chosen(page, 'blue');
await sample(page.locator('.orven-welcome > p'), 'blue welcome text');
await sample(page.locator('.sidebar-view-tabs button[aria-selected="true"]'), 'blue selected navigation');
await shot('blue-desktop');
await page.getByRole('button', { name: '选择与管理模型', exact: true }).click();
await sample(page.getByRole('button', { name: '从上游添加模型', exact: true }), 'blue primary action');
await sample(page.locator('.mc-tabs button[aria-current="page"]'), 'blue model category');
await page.keyboard.press('Escape');
mark('visible appearance choice updates all surfaces, favicon and metadata; draft survives switching and selection survives reload');
const other = await context.newPage(); await other.goto(host.url); await chosen(other, 'blue');
await page.getByRole('button', { name: '外观', exact: true }).click();
const blueRadio = appearance().getByRole('radio', { name: '雾蓝', exact: true });
await blueRadio.focus(); await blueRadio.press('ArrowLeft');
await expect(appearance().getByRole('radio', { name: '深青绿', exact: true })).toBeChecked();
await chosen(page, 'green'); await chosen(other, 'green');
await sample(page.locator('.appearance-choice.selected .appearance-choice-description'), 'green appearance description');
await page.keyboard.press('Escape');
await expect(page.getByRole('button', { name: '外观', exact: true })).toBeFocused();
await shot('green-desktop');
mark('native keyboard selection, focus return and cross-tab preference sync work without backend mutations');
await page.getByRole('button', { name: '外观', exact: true }).click();
await appearance().getByRole('radio', { name: '鸢尾紫', exact: true }).check();
await chosen(page, 'purple'); await chosen(other, 'purple');
await expect(page.locator('meta[name="theme-color"]')).toHaveAttribute('content', '#6253b5');
await expect(page.locator('link[rel="icon"]')).toHaveAttribute('href', '/brand/orven-icon-purple.svg');
assert.equal(await page.locator('.task-pane').evaluate(element => getComputedStyle(element).backgroundColor), 'rgb(255, 255, 255)');
assert.equal(await page.locator('.project-sidebar').evaluate(element => getComputedStyle(element).backgroundColor), 'rgb(245, 245, 248)');
await sample(page.locator('.appearance-choice.selected .appearance-choice-description'), 'purple appearance description');
await sample(page.locator('.appearance-note'), 'purple saved state');
await panelFits(page); await shot('appearance-purple');
await page.keyboard.press('Escape');
await sample(page.locator('.orven-welcome > p'), 'purple welcome text');
await sample(page.locator('.sidebar-view-tabs button[aria-selected="true"]'), 'purple selected navigation');
await shot('purple-desktop');
await page.getByRole('button', { name: '选择与管理模型', exact: true }).click();
await sample(page.getByRole('button', { name: '从上游添加模型', exact: true }), 'purple primary action');
await sample(page.locator('.mc-tabs button[aria-current="page"]'), 'purple model category');
await page.keyboard.press('Escape');
await page.reload(); await chosen(page, 'purple'); await other.close();
mark('original purple palette restores white workspace and gray navigation, updates metadata and persists across reload and tabs');
await page.goto(host.url + '/?palette=purple&keep=1#review'); await chosen(page, 'purple');
assert.equal(new URL(page.url()).search, '?keep=1'); assert.equal(new URL(page.url()).hash, '#review');
await page.getByRole('button', { name: '外观', exact: true }).click();
await appearance().getByRole('radio', { name: '深青绿', exact: true }).check();
await page.reload(); await chosen(page, 'green');
await page.evaluate(() => localStorage.setItem('orven-palette', 'invalid-palette'));
await page.reload(); await chosen(page, 'green');
mark('preview links select once and preserve other URL data; latest selection wins on reload and invalid stored choices recover');
await page.goto(host.url + '/?palette=purple'); await page.locator('.connection.connected').waitFor();
for (const viewport of [{ width: 600, height: 900 }, { width: 320, height: 740 }, { width: 390, height: 844 }]) {
await page.setViewportSize(viewport);
await page.getByRole('button', { name: '外观', exact: true }).click();
await expect(appearance().getByRole('radio')).toHaveCount(3);
await expect(appearance().getByRole('radio', { name: '鸢尾紫', exact: true })).toBeChecked();
await panelFits(page); await shot('appearance-' + viewport.width); await page.keyboard.press('Escape');
}
await page.goto(host.url + '/mobile.html'); await chosen(page, 'purple');
await expect(page.locator('link[rel="icon"]')).toHaveAttribute('href', '/mobile-icon.svg');
await page.getByRole('button', { name: '外观', exact: true }).click();
await expect(appearance().getByRole('radio', { name: '鸢尾紫', exact: true })).toBeChecked();
await appearance().getByRole('radio', { name: '深青绿', exact: true }).check();
await page.keyboard.press('Escape'); await page.reload(); await chosen(page, 'green');
await page.getByRole('button', { name: '外观', exact: true }).click();
await appearance().getByRole('radio', { name: '鸢尾紫', exact: true }).check();
await panelFits(page); await shot('companion-appearance');
const overflow = await page.evaluate(() => document.documentElement.scrollWidth > innerWidth + 1);
assert.equal(overflow, false);
mark('600px, 390px and 320px panels fit all three choices; actual companion shell retains local preference and gateway icon');
const restricted = await browser.newContext({ viewport: { width: 390, height: 844 } });
observeContext(restricted);
await restricted.addInitScript(() => {
const original = Storage.prototype.setItem;
Storage.prototype.setItem = function (key, value) { if (key === 'orven-palette') throw new DOMException('Storage unavailable', 'QuotaExceededError'); return original.call(this, key, value); };
});
const limited = await restricted.newPage();
await limited.goto(host.url); await limited.locator('.connection.connected').waitFor({ state: 'attached' });
await limited.getByRole('button', { name: '外观', exact: true }).click();
await limited.getByRole('radio', { name: '鸢尾紫', exact: true }).check(); await chosen(limited, 'purple');
await expect(limited.locator('.appearance-note')).toContainText('暂时无法保存');
await limited.reload(); await chosen(limited, 'green'); await restricted.close();
mark('unavailable preference storage keeps switching usable and reports that the choice was not saved');
// Use an isolated on-disk profile: no injected storage state or user's browser profile.
const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'orven-appearance-'));
assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));
disposers.push(() => fs.rmSync(profile, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }));
async function reopen() {
persistent = await chromium.launchPersistentContext(profile, { channel: 'msedge', headless: true, viewport: { width: 1440, height: 960 } });
observeContext(persistent);
const reopened = persistent.pages()[0] ?? await persistent.newPage();
await reopened.goto(host.url); await reopened.locator('.connection.connected').waitFor({ state: 'attached' });
return reopened;
}
let reopened = await reopen();
for (const [palette, name] of [['blue', '雾蓝'], ['green', '深青绿'], ['purple', '鸢尾紫']]) {
await reopened.keyboard.press('Escape');
await reopened.getByRole('button', { name: '外观', exact: true }).click();
await reopened.getByRole('radio', { name, exact: true }).check();
await chosen(reopened, palette);
await expect(reopened.locator('.appearance-note')).toContainText('下次打开会继续使用');
await persistent.close(); persistent = undefined;
reopened = await reopen(); await chosen(reopened, palette);
assert.equal(await reopened.evaluate(() => localStorage.getItem('orven-palette')), palette);
await reopened.getByRole('button', { name: '外观', exact: true }).click();
await expect(reopened.getByRole('radio', { name, exact: true })).toBeChecked();
}
await reopened.screenshot({ path: path.join(output, 'reopened-purple.png'), fullPage: true });
await persistent.close(); persistent = undefined;
mark('each palette survives a full browser close and relaunch using the same isolated persistent profile');
assert.deepEqual(errors, []); assert.deepEqual(mutations, []);
fs.writeFileSync(path.join(output, 'result.json'), JSON.stringify({ checks, contrast, errors, backendMutations: mutations, realModelCalls: 0, userDataAccessed: false }, null, 2));
} catch (error) {
await shot('failure').catch(() => {});
fs.writeFileSync(path.join(output, 'failure.json'), JSON.stringify({ checks, errors, message: error.message }, null, 2)); throw error;
} finally {
await persistent?.close();
await browser.close(); for (const dispose of disposers.reverse()) await dispose();
}