92380ebab6
验证:713 项单测、8 视口环境、代码及格式检查通过;未运行生产构建。
82 lines
2.4 KiB
JavaScript
82 lines
2.4 KiB
JavaScript
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
import { createHash } from 'node:crypto';
|
|
import { devices } from '@playwright/test';
|
|
import { browserStabilityArgs } from './playwright-runtime.mjs';
|
|
|
|
// The review JSON is also read by Python; keep dimensions and device choices there.
|
|
const config = JSON.parse(
|
|
readFileSync(new URL('./ui-review/config.json', import.meta.url), 'utf8'),
|
|
);
|
|
const aliases = {
|
|
'desktop-boundary': 'laptop',
|
|
'laptop-hd': 'hd',
|
|
'laptop-wxga': 'wxga',
|
|
};
|
|
export const browserChannel = config.browser.channel;
|
|
export const projectViewports = [
|
|
'desktop',
|
|
'mobile',
|
|
'tablet',
|
|
'desktop-boundary',
|
|
'laptop-hd',
|
|
'laptop-wxga',
|
|
'qhd',
|
|
'uhd',
|
|
];
|
|
|
|
export function deviceOptions(name) {
|
|
const key = aliases[name] || name;
|
|
const size = config.viewports[key];
|
|
if (!size) throw new Error(`Unknown viewport: ${name}`);
|
|
const descriptor =
|
|
devices[
|
|
config.browser[key === 'mobile' ? 'mobileDevice' : 'desktopDevice']
|
|
];
|
|
if (!descriptor) throw new Error(`Unknown device profile for ${name}`);
|
|
const { defaultBrowserType, ...context } = descriptor;
|
|
return { ...context, viewport: { width: size[0], height: size[1] } };
|
|
}
|
|
|
|
export function contextOptions(name, reducedMotion = 'reduce') {
|
|
return {
|
|
...deviceOptions(name),
|
|
locale: config.browser.locale,
|
|
timezoneId: config.browser.timezone,
|
|
colorScheme: 'light',
|
|
reducedMotion,
|
|
};
|
|
}
|
|
|
|
export function environmentFingerprint(name, reducedMotion = 'reduce') {
|
|
return createHash('sha256')
|
|
.update(
|
|
JSON.stringify({
|
|
version: 2,
|
|
channel: browserChannel,
|
|
options: contextOptions(name, reducedMotion),
|
|
launchArgs: browserStabilityArgs,
|
|
screenshotScale: 'css',
|
|
}),
|
|
)
|
|
.digest('hex');
|
|
}
|