chore(tooling): 对齐协作规范与浏览器验收工具

验证:713 项单测、8 视口环境、代码及格式检查通过;未运行生产构建。
This commit is contained in:
zhanjunhao
2026-11-17 14:48:50 +08:00
parent e7a7541b2b
commit 92380ebab6
30 changed files with 2089 additions and 160 deletions
+44 -79
View File
@@ -17,107 +17,72 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { defineConfig, devices } from '@playwright/test';
import { defineConfig } from '@playwright/test';
import {
browserChannel,
contextOptions,
projectViewports,
} from './scripts/browser-environment.mjs';
import { runtimeOptions } from './scripts/playwright-runtime.mjs';
const target = process.env.PW_TARGET || 'local-dev';
if (!['local-dist', 'local-dev', 'remote'].includes(target)) {
throw new Error('PW_TARGET must be local-dist, local-dev or remote');
}
if (target === 'remote' && !process.env.PW_BASE_URL) {
throw new Error('PW_BASE_URL is required for remote tests');
}
const baseURL = new URL(process.env.PW_BASE_URL || 'http://127.0.0.1:4175/');
const runtime = runtimeOptions({ defaultPort: 4176, defaultWorkers: 1 });
const { target, baseURL, isCI, headless, workers, outputDir, reportDir } =
runtime;
if (
!['http:', 'https:'].includes(baseURL.protocol) ||
baseURL.username ||
baseURL.password ||
baseURL.search ||
baseURL.hash
) {
throw new Error(
'PW_BASE_URL must be an HTTP(S) URL without credentials, query or hash',
);
}
if (!baseURL.pathname.endsWith('/'))
throw new Error('PW_BASE_URL directory path must end with /');
if (
target !== 'remote' &&
(baseURL.hostname !== '127.0.0.1' ||
baseURL.protocol !== 'http:' ||
baseURL.pathname !== '/')
) {
throw new Error('Local targets require http://127.0.0.1:<port>/');
}
const isCI = Boolean(process.env.CI && process.env.CI !== 'false');
const headless =
process.env.PW_HEADLESS === undefined
? isCI
: process.env.PW_HEADLESS === '1';
const viewports = [
['desktop', 1920, 1080],
['mobile', 360, 800],
];
if (process.env.PW_EXTENDED_LAYOUT === '1') {
viewports.push(
['tablet', 768, 1024],
['desktop-boundary', 1024, 768],
['laptop-hd', 1366, 768],
['laptop-wxga', 1440, 900],
['qhd', 2560, 1440],
['uhd', 3840, 2160],
);
}
isCI &&
process.argv.some((argument) =>
['--headed', '--debug', '--ui'].includes(argument),
)
)
throw new Error('CI requires headless non-interactive tests');
const viewports = runtime.extended
? projectViewports
: projectViewports.slice(0, 2);
// UI_VISUAL_OFFSCREEN=0 仅限人工本地可视化调试时临时使用。
// Agent、CI、无头/自动化环境禁止设置该值,必须保持默认离屏模式
// (或 UI_VISUAL_OFFSCREEN=1),避免弹出 UI 或依赖显示环境。
// 离屏不能保证系统启动时不激活窗口;公共 fixture 探测到前台或状态不明时停止。
const listOnly = process.argv.includes('--list');
export default defineConfig({
testDir: './e2e',
testMatch: '**/*.spec.js',
outputDir: 'test-results',
outputDir,
fullyParallel: false,
workers: 1,
forbidOnly: isCI || process.env.PW_REQUIRE_ALL === '1',
workers,
forbidOnly: isCI || runtime.requireAll,
failOnFlakyTests: isCI,
retries: isCI ? 1 : 0,
timeout: 30000,
expect: { timeout: 5000 },
reporter: [
['list'],
['html', { outputFolder: 'playwright-report', open: 'never' }],
...(isCI ? [['junit', { outputFile: 'test-results/junit.xml' }]] : []),
],
reporter: listOnly
? [['list']]
: [
['list'],
['html', { outputFolder: reportDir, open: 'never' }],
...(isCI ? [['junit', { outputFile: `${outputDir}/junit.xml` }]] : []),
['./scripts/playwright-reporter.mjs'],
],
metadata: {
platform: process.platform,
node: process.version,
target,
baseURL: baseURL.href,
runId: runtime.runId,
},
use: {
baseURL: baseURL.href,
headless,
launchOptions:
process.env.UI_VISUAL_OFFSCREEN === '1'
? {
args: [
'--window-position=-32000,-32000',
'--disable-backgrounding-occluded-windows',
],
}
: {},
locale: 'zh-CN',
timezoneId: 'Asia/Shanghai',
deviceScaleFactor: 1,
colorScheme: 'light',
reducedMotion: 'reduce',
launchOptions: { args: runtime.launchArgs },
...contextOptions('desktop'),
screenshot: { mode: 'only-on-failure', scale: 'css' },
trace: 'retain-on-failure',
screenshot: 'only-on-failure',
video: process.env.PW_VIDEO === '1' ? 'retain-on-failure' : 'off',
video: runtime.video ? 'retain-on-failure' : 'off',
},
projects: viewports.map(([name, width, height]) => ({
projects: viewports.map((name) => ({
name: `chrome-${name}`,
use: {
...(name === 'mobile' ? devices['Pixel 5'] : devices['Desktop Chrome']),
channel: 'chrome',
viewport: { width, height },
},
use: { ...contextOptions(name), channel: browserChannel },
})),
webServer:
target === 'remote'
@@ -125,7 +90,7 @@ export default defineConfig({
: {
command:
target === 'local-dist'
? `npm run preview -- --host 127.0.0.1 --port ${baseURL.port || 80}`
? `node scripts/preview-dist.mjs --port ${baseURL.port || 80}`
: `npm run dev -- --host 127.0.0.1 --port ${baseURL.port || 80} --strictPort`,
url: baseURL.href,
reuseExistingServer: false,