You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/UserCopyCoverageScannerTest...

76 lines
2.6 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\UserCopy\UserCopyCoverageScanner;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$rules = static function (array $violations): array {
return array_values(array_unique(array_column($violations, 'rule')));
};
$javascript = <<<'JS'
toast('Saved');
window.confirm("Continue?");
date.toLocaleString('zh-CN');
new Intl.NumberFormat('zh-CN');
node.textContent = payload?.message;
node.textContent = response['detail'];
toast(error?.message, 'error');
const key = `sample.dynamic.h12abcdef`;
const visible = '未登记文案';
const classifier = /访客代理/u; // user-copy: allow technical-regex non-rendered provider classifier
const protocol = 'HTTP'; // user-copy: allow technical-constant non-rendered protocol token
const invalid = '错误放行'; // user-copy: allow arbitrary-reason
const missingCategory = '分类缺失'; // user-copy: allow
const missingReason = '理由缺失'; // user-copy: allow technical-regex
JS;
$javascriptViolations = UserCopyCoverageScanner::scanContents(
'fixture.js',
$javascript
);
$javascriptRules = $rules($javascriptViolations);
foreach ([
'direct_toast_text',
'direct_confirm_text',
'hardcoded_zh_locale',
'raw_backend_copy',
'raw_error_feedback',
'generated_copy_key',
'visible_chinese_literal',
'invalid_allow_marker',
] as $rule) {
$assert(in_array($rule, $javascriptRules, true),
'Scanner did not detect rule: ' . $rule);
}
$invalidMarkers = array_values(array_filter(
$javascriptViolations,
static fn (array $violation): bool =>
($violation['rule'] ?? null) === 'invalid_allow_marker'
));
$assert(count($invalidMarkers) === 3,
'Unclassified, unsupported and empty-reason markers must all fail.');
$templateRules = $rules(UserCopyCoverageScanner::scanContents(
'fixture.tpl',
'<button title="Continue">Support</button>'
));
$assert(in_array('visible_english_text', $templateRules, true),
'Scanner did not detect visible English template text.');
$assert(in_array('visible_english_attribute', $templateRules, true),
'Scanner did not detect a visible English template attribute.');
$allowed = UserCopyCoverageScanner::scanContents(
'fixture.js',
"const sample = '中文'; // user-copy: allow test-fixture scanner rejection sample\n"
. "const shared = '后台兼容'; // user-copy: allow admin-compatibility shared admin fallback"
);
$assert($allowed === [], 'A categorized marker with a reason was not honored.');
echo "User-copy coverage scanner strict rules passed.\n";