mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-06 08:02:04 +00:00
apps/lite is a separate Vite app (Astryx, React 19, StyleX) on the same honk API, for people who want to switch nodes and edit groups, subscriptions, nodes, routing and DNS without the observability pages. Every change goes into a draft and one review dialog shows it in plain words before it is written; dae syntax stays in the text editor. It has a demo mode backed by an in-browser mock, browser, motion and performance checks in apps/lite/scripts, and UI rules and working notes in apps/lite/docs. It is linted with the repository config.
280 lines
14 KiB
JavaScript
280 lines
14 KiB
JavaScript
const {chromium, expect} = require('@playwright/test');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const base = process.env.LITE_PREVIEW_URL;
|
|
if (!base) throw new Error('Set LITE_PREVIEW_URL to the preview server.');
|
|
const shots = process.env.LITE_RHYTHM_SHOTS;
|
|
const records = [];
|
|
const routes = ['groups', 'subscriptions', 'nodes', 'routing', 'dns'];
|
|
const first = {
|
|
groups: '[data-group-row]',
|
|
subscriptions: '[data-provider-id]',
|
|
nodes: 'tbody tr',
|
|
routing: '[data-rule-index]',
|
|
dns: '[data-rule-list="request"] [data-rule-index]'
|
|
};
|
|
const titles = {en: ['Groups', 'Subscriptions', 'Nodes', 'Routing', 'DNS'], 'zh-CN': ['策略组', '订阅', '节点', '路由', 'DNS']};
|
|
async function settle(p) {
|
|
await p.evaluate(async () => {
|
|
await document.fonts.ready;
|
|
await Promise.all(
|
|
document
|
|
.getAnimations()
|
|
.filter(a => a.effect.getTiming().iterations !== Infinity)
|
|
.map(a => a.finished.catch(() => {}))
|
|
);
|
|
await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)));
|
|
});
|
|
}
|
|
async function screenshot(p, name) {
|
|
if (shots) {
|
|
fs.mkdirSync(shots, {recursive: true});
|
|
await p.screenshot({path: path.join(shots, `${name}.png`), fullPage: true});
|
|
}
|
|
}
|
|
async function controls(p) {
|
|
const toolbars = p.locator('[data-collection-toolbar]:visible');
|
|
expect(await toolbars.count()).toBeGreaterThan(0);
|
|
for (const toolbar of await toolbars.all()) {
|
|
const result = await toolbar.evaluate(root => {
|
|
const selector = '.astryx-text-input,.astryx-selector,.astryx-segmented-control,.astryx-button';
|
|
const controls = [...root.querySelectorAll(selector)].filter(e => e.checkVisibility() && !e.parentElement.closest(selector));
|
|
return controls.map(e => {
|
|
const rect = e.getBoundingClientRect();
|
|
const walker = document.createTreeWalker(e, NodeFilter.SHOW_TEXT);
|
|
const labels = [];
|
|
while (walker.nextNode()) {
|
|
const node = walker.currentNode;
|
|
if (!node.textContent.trim() || !node.parentElement.checkVisibility() || node.parentElement.closest('[role="tooltip"],.astryx-popover')) continue;
|
|
const range = document.createRange();
|
|
range.selectNodeContents(node);
|
|
const lines = new Set([...range.getClientRects()].filter(r => r.width && r.height).map(r => Math.round(r.y)));
|
|
labels.push({text: node.textContent, lines: lines.size});
|
|
}
|
|
return {
|
|
tag: e.tagName,
|
|
className: [...e.classList].filter(c => c.startsWith('astryx-')).join(' '),
|
|
name: e.getAttribute('aria-label'),
|
|
y: rect.y,
|
|
height: rect.height,
|
|
labels
|
|
};
|
|
});
|
|
});
|
|
expect(result.length).toBeGreaterThan(0);
|
|
for (const control of result) {
|
|
expect(control.height, JSON.stringify(control)).toBeCloseTo(36, 0);
|
|
for (const label of control.labels) expect(label.lines, label.text).toBeLessThanOrEqual(1);
|
|
const peers = result.filter(other => Math.abs(other.y - control.y) <= 1);
|
|
for (const peer of peers) expect(Math.abs(peer.height - control.height), JSON.stringify(peers)).toBeLessThanOrEqual(1);
|
|
if (/latency:|延迟|延遲/.test(control.name ?? '') && control.className.includes('astryx-button')) expect(peers.length).toBeGreaterThan(1);
|
|
}
|
|
}
|
|
}
|
|
async function footer(p, width, locale) {
|
|
if (width === 390) {
|
|
await p.getByRole('button', {name: locale === 'en' ? 'Open navigation' : '打开导航', exact: true}).click();
|
|
}
|
|
const nav = width === 390 ? p.locator('dialog.astryx-mobile-nav[open]') : p.locator('nav').first();
|
|
const theme = nav.locator('[data-theme-toggle]');
|
|
const language = nav.locator('[data-language-menu]');
|
|
await expect(theme).toBeVisible();
|
|
await expect(language).toBeVisible();
|
|
const a = await theme.boundingBox(),
|
|
b = await language.boundingBox();
|
|
expect(Math.abs(a.height - b.height)).toBeLessThanOrEqual(1);
|
|
expect(Math.abs(a.y - b.y)).toBeLessThanOrEqual(1);
|
|
if (width === 1440) {
|
|
const collapse = await nav.getByRole('button', {name: locale === 'en' ? 'Collapse sidebar' : '折叠侧边栏', exact: true}).boundingBox();
|
|
expect(Math.abs(a.height - collapse.height)).toBeLessThanOrEqual(1);
|
|
expect(Math.abs(a.y - collapse.y)).toBeLessThanOrEqual(1);
|
|
} else {
|
|
await nav.getByRole('button', {name: locale === 'en' ? 'Close navigation' : '关闭导航', exact: true}).click();
|
|
await expect(nav).not.toBeVisible();
|
|
}
|
|
}
|
|
async function ruleLabels(p) {
|
|
const clipped = await p.locator('.rule-chip:visible').evaluateAll(tokens =>
|
|
tokens.flatMap(token => {
|
|
const bounds = token.getBoundingClientRect();
|
|
const walker = document.createTreeWalker(token, NodeFilter.SHOW_TEXT);
|
|
const failures = [];
|
|
while (walker.nextNode()) {
|
|
if (!walker.currentNode.textContent.trim()) continue;
|
|
const range = document.createRange();
|
|
range.selectNodeContents(walker.currentNode);
|
|
if (
|
|
[...range.getClientRects()].some(
|
|
r => r.left < bounds.left - 1 || r.right > bounds.right + 1 || r.top < bounds.top - 1 || r.bottom > bounds.bottom + 1
|
|
)
|
|
)
|
|
failures.push(walker.currentNode.textContent);
|
|
}
|
|
return failures;
|
|
})
|
|
);
|
|
expect(clipped).toEqual([]);
|
|
}
|
|
async function searchInsets(p) {
|
|
for (const toolbar of await p.locator('[data-collection-toolbar]:visible').all()) {
|
|
const delta = await toolbar.evaluate(root => {
|
|
const search = root.querySelector('.astryx-text-input');
|
|
if (!search) return null;
|
|
let scope = root.parentElement;
|
|
while (scope) {
|
|
const row = scope.querySelector('[data-member-row], [data-rule-index], [data-subscription-nodes] > li, [data-group-row], [data-provider-id], tbody tr');
|
|
if (row) return Math.abs(search.getBoundingClientRect().x - row.getBoundingClientRect().x);
|
|
scope = scope.parentElement;
|
|
}
|
|
throw new Error('Search toolbar has no collection row');
|
|
});
|
|
if (delta !== null) expect(delta).toBeLessThanOrEqual(1);
|
|
}
|
|
}
|
|
(async () => {
|
|
const browser = await chromium.launch();
|
|
try {
|
|
for (const width of [1440, 390])
|
|
for (const locale of ['en', 'zh-CN']) {
|
|
const p = await browser.newPage({viewport: {width, height: 1000}, locale, reducedMotion: 'reduce', colorScheme: 'light'});
|
|
p.setDefaultTimeout(15000);
|
|
await p.addInitScript(
|
|
({locale}) => {
|
|
localStorage.setItem('doona-lite.locale', locale);
|
|
localStorage.setItem('doona-lite.theme', 'light');
|
|
},
|
|
{locale}
|
|
);
|
|
await p.goto(base);
|
|
await p.getByRole('button', {name: locale === 'en' ? 'Try the demo' : '试用演示', exact: true}).click();
|
|
await p.locator('[data-group-row]').first().waitFor();
|
|
const rhythm = [];
|
|
for (const route of routes) {
|
|
await p.evaluate(route => (location.hash = route), route);
|
|
await expect(p.locator('h1:visible')).toHaveText(titles[locale][routes.indexOf(route)]);
|
|
await p.locator('[data-page-frame]:visible [data-collection]').first().waitFor();
|
|
await p.locator(`${first[route]}:visible`).first().waitFor();
|
|
await settle(p);
|
|
const measurement = await p.evaluate(first => {
|
|
const box = e => e.getBoundingClientRect();
|
|
const frame = Array.from(document.querySelectorAll('[data-page-frame]')).find(e => e.checkVisibility());
|
|
const toolbar = frame.querySelector('[data-collection-toolbar]');
|
|
const collection = toolbar.parentElement.querySelector('[data-collection]') ?? frame.querySelector('[data-collection]');
|
|
const header = Array.from(document.querySelectorAll('[data-page-header]')).find(e => e.checkVisibility());
|
|
// Rules and DNS toolbars now follow their section headings.
|
|
const headings = [...frame.querySelectorAll('h2')].filter(e => box(e).top < box(toolbar).top);
|
|
const heading = headings.at(-1)?.parentElement;
|
|
const search = toolbar.querySelector('.astryx-text-input');
|
|
const row = frame.querySelector(first);
|
|
return {
|
|
headerGap: box(frame.querySelector('h2')?.parentElement ?? toolbar).top - box(header).bottom,
|
|
headingToolbarGap: heading ? box(toolbar).top - box(heading).bottom : null,
|
|
collectionGap: box(collection).top - box(toolbar).bottom,
|
|
searchX: search ? box(search).x : null,
|
|
rowX: box(row).x,
|
|
overflow: document.documentElement.scrollWidth - innerWidth
|
|
};
|
|
}, first[route]);
|
|
expect(measurement.headerGap).toBeCloseTo(16, 0);
|
|
if (measurement.headingToolbarGap !== null) expect(measurement.headingToolbarGap).toBeCloseTo(16, 0);
|
|
expect(measurement.collectionGap).toBeCloseTo(16, 0);
|
|
if (measurement.searchX !== null) expect(Math.abs(measurement.searchX - measurement.rowX)).toBeLessThanOrEqual(1);
|
|
expect(measurement.overflow).toBeLessThanOrEqual(0);
|
|
rhythm.push(measurement);
|
|
await controls(p);
|
|
await expect(p.locator('[data-theme-toggle]:visible')).toHaveCount(1);
|
|
await expect(p.locator('[data-language-menu]:visible')).toHaveCount(1);
|
|
await searchInsets(p);
|
|
await ruleLabels(p);
|
|
await footer(p, width, locale);
|
|
records.push({width, locale, route, ...measurement});
|
|
await screenshot(p, `${route}-${width}-${locale}-light`);
|
|
if (route === 'groups') {
|
|
await p.locator('[data-group-row="auto"] button').click();
|
|
const inspector = p.locator('[data-group-inspector="auto"]:visible');
|
|
await expect(inspector.getByRole('radio').first()).toBeVisible();
|
|
await expect(inspector.locator('button[aria-label="' + (locale === 'en' ? 'Sort' : '排序') + '"]')).toHaveText(
|
|
locale === 'en' ? 'By latency' : '按延迟'
|
|
);
|
|
await controls(p);
|
|
await searchInsets(p);
|
|
await screenshot(p, `inspector-${width}-${locale}-light`);
|
|
if (width === 390) await p.keyboard.press('Escape');
|
|
}
|
|
if (route === 'nodes') {
|
|
await p.locator('tbody input[type="checkbox"]').first().check();
|
|
const toolbar = p.locator('[data-page-frame]:visible [data-collection-toolbar]').first();
|
|
await expect(toolbar).toContainText(locale === 'en' ? '1 selected' : '已选择 1 项');
|
|
await controls(p);
|
|
expect(await p.evaluate(() => document.documentElement.scrollWidth - innerWidth)).toBeLessThanOrEqual(0);
|
|
const a = await toolbar.boundingBox(),
|
|
b = await p.locator('[data-page-frame]:visible [data-collection]').first().boundingBox();
|
|
expect(b.y - a.y - a.height).toBeCloseTo(16, 0);
|
|
await screenshot(p, `nodes-selected-${width}-${locale}-light`);
|
|
await toolbar.getByRole('button', {name: locale === 'en' ? 'Clear selection' : '清除选择', exact: true}).click();
|
|
await expect(p.locator('tbody input[type="checkbox"]').first()).not.toBeChecked();
|
|
}
|
|
if (route === 'dns') {
|
|
const advanced = p.getByRole('button', {name: locale === 'en' ? 'Advanced' : '高级', exact: true});
|
|
await advanced.click();
|
|
const response = p.locator('[data-rule-list="response"]');
|
|
await expect(response.locator('[data-rule-index]').first()).toBeVisible();
|
|
await expect(response.locator('table')).toHaveCount(0);
|
|
await controls(p);
|
|
await searchInsets(p);
|
|
await ruleLabels(p);
|
|
await screenshot(p, `dns-response-${width}-${locale}-light`);
|
|
await advanced.click();
|
|
await p.locator('.lite-page-content:visible').evaluate(e => (e.scrollTop = 0));
|
|
}
|
|
if (route === 'subscriptions') {
|
|
const sub = p.locator('[data-provider-id="harbor"]');
|
|
const trigger = sub.locator('.astryx-collapsible-trigger');
|
|
await trigger.click();
|
|
await expect(trigger).toHaveAttribute('aria-expanded', 'true');
|
|
await expect(sub.locator('[data-subscription-nodes] > li').first()).toBeVisible();
|
|
await expect(sub.locator('table')).toHaveCount(0);
|
|
await controls(p);
|
|
await searchInsets(p);
|
|
await screenshot(p, `subscription-expanded-${width}-${locale}-light`);
|
|
await trigger.click();
|
|
}
|
|
}
|
|
for (const key of ['headerGap', 'collectionGap'])
|
|
expect(Math.max(...rhythm.map(r => r[key])) - Math.min(...rhythm.map(r => r[key]))).toBeLessThanOrEqual(1);
|
|
if (width === 1440) {
|
|
await p.getByRole('button', {name: locale === 'en' ? 'Collapse sidebar' : '折叠侧边栏', exact: true}).click();
|
|
await settle(p);
|
|
const nav = p.locator('nav').first();
|
|
await expect(nav.locator('[data-theme-toggle]')).toBeVisible();
|
|
await expect(nav.locator('[data-language-menu]')).toBeVisible();
|
|
const centres = await nav
|
|
.locator('a,button')
|
|
.evaluateAll(items => items.filter(e => e.checkVisibility()).map(e => e.getBoundingClientRect().x + e.getBoundingClientRect().width / 2));
|
|
expect(Math.max(...centres) - Math.min(...centres)).toBeLessThanOrEqual(0.5);
|
|
await screenshot(p, `collapsed-rail-${locale}-light`);
|
|
}
|
|
if (width === 1440 && locale === 'zh-CN') {
|
|
await p.getByRole('button', {name: '展开侧边栏', exact: true}).click();
|
|
await p.locator('[data-theme-toggle]:visible').click();
|
|
for (const route of routes) {
|
|
await p.evaluate(route => (location.hash = route), route);
|
|
await expect(p.locator('h1:visible')).toHaveText(titles[locale][routes.indexOf(route)]);
|
|
await p.locator('[data-page-frame]:visible [data-collection]').first().waitFor();
|
|
await p.locator(`${first[route]}:visible`).first().waitFor();
|
|
await settle(p);
|
|
await screenshot(p, `${route}-1440-zh-CN-dark`);
|
|
}
|
|
}
|
|
console.log(`PASS layout rhythm, md controls, single-line labels, insets and footer: ${width} ${locale}`);
|
|
await p.close();
|
|
}
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
if (process.env.LITE_RHYTHM_OUTPUT) fs.writeFileSync(process.env.LITE_RHYTHM_OUTPUT, JSON.stringify(records, null, 2));
|
|
})().catch(error => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|