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/BackupNetworkUserCopyContra...

138 lines
4.8 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\UserCopy\UserCopyCoverageScanner;
use App\Services\UserCopy\UserCopyManifest;
$root = dirname(__DIR__);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$read = static function (string $path) use ($root): string {
$contents = file_get_contents($root . '/' . $path);
if (!is_string($contents)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $contents;
};
$surfaces = [
'backups' => [
'page' => 'cloud.backups',
'template' => 'resources/views/venix/cloud/pages/backups.tpl',
'script' => 'public/cloud/backups.js',
],
'snapshots' => [
'page' => 'cloud.snapshots',
'template' => 'resources/views/venix/cloud/pages/cloud-snapshots.tpl',
'script' => 'public/cloud/cloud-snapshots.js',
],
'traffic' => [
'page' => 'cloud.traffic',
'template' => 'resources/views/venix/cloud/pages/traffic.tpl',
'script' => 'public/cloud/traffic.js',
],
'vpc' => [
'page' => 'cloud.vpcs',
'template' => 'resources/views/venix/cloud/pages/vpcs.tpl',
'script' => 'public/cloud/vpcs.js',
],
];
$manifest = UserCopyManifest::load();
$entries = array_column($manifest['entries'], null, 'key');
$fragment = $read('resources/user-copy/entries/backup-network.entries.php');
$assert(preg_match(
'/[\'\"](?:en|english|translation)[\'\"]\s*=>/i',
$fragment
) !== 1, 'The backup/network source fragment contains target translations.');
foreach ($surfaces as $scope => $surface) {
$page = $surface['page'];
$assert(in_array($scope, $manifest['pages'][$page]['scopes'] ?? [], true),
'The page does not publish its expected copy scope: ' . $page);
$template = $read($surface['template']);
$script = $read($surface['script']);
$violations = UserCopyCoverageScanner::scanContents(
$surface['template'],
$template
);
array_push($violations, ...UserCopyCoverageScanner::scanContents(
$surface['script'],
$script
));
$assert($violations === [],
'Migrated backup/network source still contains direct visible copy: '
. json_encode($violations, JSON_UNESCAPED_UNICODE));
$assert(str_contains($script, 'globalThis.VenixUserCopy')
&& str_contains($script, 'i18n.text(key, parameters)'),
'The page script does not use the shared copy runtime: ' . $page);
preg_match_all(
'/\{user_copy\s+key=[\'\"]([^\'\"]+)[\'\"]/',
$template,
$templateMatches
);
$assert(($templateMatches[1] ?? []) !== [],
'The migrated template has no stable copy keys: ' . $page);
$combined = $template . "\n" . $script;
preg_match_all(
'/[\'\"](' . preg_quote($scope, '/')
. '\.[a-z0-9_.]+)[\'\"]/',
$combined,
$keyMatches
);
$usedKeys = array_values(array_unique($keyMatches[1] ?? []));
$assert($usedKeys !== [], 'No scoped copy keys found for ' . $page);
foreach ($usedKeys as $key) {
$assert(isset($entries[$key]),
'The page references an undeclared copy key: ' . $key);
$assert(($entries[$key]['scope'] ?? null) === $scope
&& ($entries[$key]['page'] ?? null) === $page,
'The copy key belongs to the wrong page or scope: ' . $key);
}
foreach ($entries as $key => $entry) {
if (($entry['scope'] ?? null) !== $scope) {
continue;
}
$assert(str_contains($combined, "'" . $key . "'")
|| str_contains($combined, '"' . $key . '"'),
'The source fragment contains an unused page key: ' . $key);
}
$assert(preg_match(
'/\b(?:error|payload|response)\?*\.(?:message|detail|msg|error)\b/',
$script
) !== 1,
'A raw backend error field remains in the page script: ' . $page);
}
$toastGroups = [];
foreach ($entries as $key => $entry) {
if (!array_key_exists((string) ($entry['scope'] ?? ''), $surfaces)
|| !str_starts_with((string) ($entry['kind'] ?? ''), 'toast_')) {
continue;
}
$suffix = ($entry['kind'] ?? '') === 'toast_title' ? '.title' : '.body';
$assert(str_ends_with($key, $suffix),
'Toast keys must use explicit title/body suffixes: ' . $key);
$group = substr($key, 0, -strlen($suffix));
$toastGroups[$group][$entry['kind']] = $entry;
}
foreach ($toastGroups as $group => $pair) {
$assert(isset($pair['toast_title'], $pair['toast_body']),
'Toast title/body pair is incomplete: ' . $group);
$assert($pair['toast_title']['toast_tone']
=== $pair['toast_body']['toast_tone'],
'Toast title/body tones do not match: ' . $group);
}
echo "Backup/network user-copy contracts passed.\n";