/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { mkdtemp, writeFile, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { spawn } from 'node:child_process'; const root = fileURLToPath(new URL('../', import.meta.url)); const directory = await mkdtemp(path.join(tmpdir(), 'pw-environment-')); const url = (name) => pathToFileURL(path.join(root, name)).href; try { await writeFile( path.join(directory, 'playwright.config.mjs'), ` import config from ${JSON.stringify(url('playwright.config.js'))} export default { ...config, testDir: ${JSON.stringify(directory)}, testMatch: '**/*.spec.mjs', fullyParallel: false, workers: 1, outputDir: ${JSON.stringify(root)} + config.outputDir, reporter: config.reporter.map(([name, options]) => [name.startsWith('.') ? ${JSON.stringify(root)} + name : name, name === 'html' ? { ...options, outputFolder: ${JSON.stringify(root)} + options.outputFolder } : options]), webServer: config.webServer ? { ...config.webServer, cwd: ${JSON.stringify(root)} } : undefined } `, ); await writeFile( path.join(directory, 'environment.spec.mjs'), ` import { test, expect } from ${JSON.stringify(url('scripts/playwright-test.mjs'))} import { contextOptions } from ${JSON.stringify(url('scripts/browser-environment.mjs'))} import { captureStatic } from ${JSON.stringify(url('scripts/browser-capture.mjs'))} test('设备上下文、触摸、DPR、CSS 截图与稳定字体', async ({ page, baseURL }, info) => { const expected = contextOptions(info.project.name.replace('chrome-', '')) await page.route('**/__pw_environment__', route => route.fulfill({ contentType: 'text/html', body: '
环境验证
' })) await page.goto(new URL('__pw_environment__', baseURL).href) const actual = await page.evaluate(() => ({ width: innerWidth, height: innerHeight, dpr: devicePixelRatio, ua: navigator.userAgent, touch: navigator.maxTouchPoints > 0, language: navigator.language, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, reduced: matchMedia('(prefers-reduced-motion: reduce)').matches })) expect(actual).toMatchObject({ width: expected.viewport.width, height: expected.viewport.height, dpr: expected.deviceScaleFactor, touch: expected.hasTouch, language: expected.locale, timezone: expected.timezoneId, reduced: true }) expect(actual.ua).toBe(expected.userAgent) const image = await captureStatic(page) expect(image.readUInt32BE(16)).toBe(expected.viewport.width) expect(image.readUInt32BE(20)).toBe(expected.viewport.height) expect((await captureStatic(page)).equals(image)).toBe(true) }) `, ); const env = { ...process.env, PW_HEADLESS: '1', UI_VISUAL_OFFSCREEN: '1', PW_EXTENDED_LAYOUT: '1', PW_WORKERS: '1', }; delete env.PW_RUN_ID; delete env.PLAYWRIGHT_HTML_OUTPUT_DIR; const code = await new Promise((resolve, reject) => { const child = spawn( process.execPath, [ path.join(root, 'node_modules/@playwright/test/cli.js'), 'test', '--config', path.join(directory, 'playwright.config.mjs'), ], { cwd: root, env, stdio: 'inherit' }, ); child.on('error', reject); child.on('exit', (code) => resolve(code ?? 1)); }); process.exitCode = code; } finally { await rm(directory, { recursive: true, force: true }); }