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

96 lines
4.4 KiB
PHP

<?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);
}
};
$manifest = UserCopyManifest::load();
$entries = array_column($manifest['entries'], null, 'key');
$assert(($manifest['pages']['cloud.isos']['scopes'] ?? null) === ['common', 'isos']
&& ($manifest['pages']['cloud.keys']['scopes'] ?? null) === ['common', 'ssh_keys'],
'ISO and SSH-key pages must publish only their declared copy scopes.');
$required = [
'isos.list.aria', 'isos.empty', 'isos.confirm.delete.body',
'isos.confirm.cleanup_downloaded.body', 'isos.task.failure.recovering',
'isos.task.failure.effect_unknown', 'isos.task.receipt.invalid',
'isos.toast.import.ready.body', 'isos.error.mutation_unknown',
'ssh_keys.list.aria', 'ssh_keys.empty.title',
'ssh_keys.confirm.delete.body', 'ssh_keys.toast.deleted.body',
'ssh_keys.error.mutation_unknown', 'ssh_keys.error.initialize',
];
foreach ($required as $key) {
$assert(isset($entries[$key]), 'Required media/key copy is missing: ' . $key);
}
$assert($entries['isos.item.summary_digest']['parameters'] === [
'digest' => 'string', 'size' => 'string',
] && $entries['isos.confirm.delete.body']['parameters'] === ['name' => 'string']
&& $entries['ssh_keys.confirm.delete.body']['parameters'] === ['name' => 'string']
&& $entries['isos.error.request']['parameters'] === ['status' => 'integer']
&& $entries['ssh_keys.error.request']['parameters'] === ['status' => 'integer'],
'Media/key parameter contracts drifted from their call sites.');
foreach ($entries as $entry) {
if (!in_array($entry['scope'], ['isos', 'ssh_keys'], true)
|| !str_starts_with($entry['kind'], 'toast_')) {
continue;
}
$assert(in_array($entry['toast_tone'], ['success', 'warning', 'error', 'info'], true),
'Media/key Toast copy is missing an explicit tone: ' . $entry['key']);
}
$paths = [
$root . '/resources/views/venix/cloud/pages/isos.tpl',
$root . '/resources/views/venix/cloud/pages/keys.tpl',
$root . '/public/cloud/iso-images.js',
$root . '/public/cloud/ssh-keys.js',
];
$violations = UserCopyCoverageScanner::scanFiles($paths);
$assert($violations === [], 'Media/key pages retain direct user-visible copy: '
. json_encode($violations, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$referenced = [];
foreach ($paths as $path) {
$source = file_get_contents($path);
preg_match_all(
'/(?:user_copy\s+key=|\bt\()[\'\"]((?:isos|ssh_keys)\.[a-z0-9_.]+)[\'\"]/',
$source,
$matches
);
array_push($referenced, ...$matches[1]);
}
foreach (array_unique($referenced) as $key) {
$assert(isset($entries[$key]), 'Media/key source references an undeclared key: ' . $key);
}
$isoModule = file_get_contents($root . '/public/cloud/iso-images.js');
$keyModule = file_get_contents($root . '/public/cloud/ssh-keys.js');
$assert(str_contains($isoModule, 'globalThis.VenixUserCopy')
&& str_contains($keyModule, 'globalThis.VenixUserCopy'),
'Media/key modules must resolve feedback through the user-copy runtime.');
$assert(!preg_match('/payload\?\.(?:detail|message|msg|error)/', $keyModule)
&& !preg_match('/task\?\.error/', $isoModule)
&& !str_contains($isoModule, 'recoveryTaskFailureNotice(task)')
&& !preg_match('/showError\([^\n]+error\?*\.message/', $isoModule . "\n" . $keyModule),
'Media/key feedback must not expose raw backend or task messages.');
$assert(str_contains($isoModule, "t('isos.confirm.delete.body', { name: isoName })")
&& str_contains($keyModule, "t('ssh_keys.confirm.delete.body', { name: key.name })")
&& str_contains($isoModule, "t('isos.toast.import.ready.title')")
&& str_contains($keyModule, "t('ssh_keys.toast.deleted.title')"),
'Media/key confirmations and Toasts must use their parameterized copy contracts.');
$assert(str_contains($isoModule, 'uncertainMutations: new Set()')
&& str_contains($isoModule, "pageState.activeStatusUrl = 'unknown'")
&& substr_count($keyModule, 'if (!keepLocked) setMutationBusy(false);') === 2,
'Unknown media/key mutations must remain locally locked until a fresh page reconciliation.');
echo "ISO and SSH-key user-copy contract checks passed.\n";