commit compras5 2911
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-29 19:49:19 +02:00
parent a935499d64
commit 96b30368bc
96 changed files with 3397 additions and 111 deletions
@@ -0,0 +1,127 @@
<?php
use App\Services\InventoryConversionRuntime;
use Symfony\Component\Process\Process;
require dirname(__DIR__).'/vendor/autoload.php';
if (count($argv) !== 2 || ! in_array($argv[1], ['--preview', '--apply-authorized'], true)) {
throw new LogicException('Use --preview or --apply-authorized.');
}
$root = dirname(__DIR__);
$proposal = $root.'/docs/proofs/inventory-conversion-pilot-proposal.json';
$plan = json_decode(file_get_contents($proposal), true, 512, JSON_THROW_ON_ERROR);
$approval = json_decode(file_get_contents($root.'/docs/proofs/inventory-conversion-pilot-authorization.json'), true, 32, JSON_THROW_ON_ERROR);
if ($approval['state'] !== 'approved' || ! hash_equals($approval['proposal_sha256'], hash_file('sha256', $proposal))) {
throw new LogicException('Inventory installation authorization mismatch.');
}
$settingsPath = 'C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json';
$destination = 'C:/laragon/data/compras/runtime/inventory-8608e6551dea6448';
$resultPath = $root.'/docs/proofs/inventory-conversion-pilot-installation.json';
if ($plan['target']['settings'] !== $settingsPath || $plan['target']['inventory_directory'] !== $destination || is_file($resultPath)) {
throw new LogicException('Installation target or existing result differs.');
}
$before = file_get_contents($settingsPath);
if (! hash_equals($plan['target']['settings_before_sha256'], hash('sha256', $before))) {
throw new LogicException('Pilot settings changed since authorization.');
}
$settings = json_decode($before, true, 32, JSON_THROW_ON_ERROR);
if ($settings['DB_HOST'] !== '127.0.0.1' || (string) $settings['DB_PORT'] !== '5432'
|| ! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $settings['DB_DATABASE'], $match)
|| $settings['DB_USERNAME'] !== 'payable_qa_'.$match[1] || $settings['APP_ENV'] !== 'payable-testing'
|| $settings['COMPRAS_RUNTIME_INSTALLATION_ID'] !== $plan['target']['financial_installation_id']) {
throw new LogicException('Pilot identity differs.');
}
$archive = $root.'/storage/framework/testing/compras-inventory-'.InventoryConversionRuntime::MANIFEST_SHA256.'.zip';
if (! hash_equals($plan['target']['inventory_archive_sha256'], hash_file('sha256', $archive))) {
throw new LogicException('Inventory archive differs.');
}
$changes = ['COMPRAS_INVENTORY_CONVERSION_ENABLED' => 'true', 'COMPRAS_INVENTORY_ROOT' => $destination];
if ($changes !== $plan['proposed_settings_only']) {
throw new LogicException('Unreviewed settings changes refused.');
}
$after = [...$settings, ...$changes];
$encoded = json_encode($after, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).PHP_EOL;
$preflight = new Process([PHP_BINARY, 'scripts/check-inventory-conversion-pilot.php', '--read-only'], $root, timeout: 30);
$preflight->mustRun();
$check = json_decode($preflight->getOutput(), true, 32, JSON_THROW_ON_ERROR);
if ($check['state'] !== 'passed' || $check['active_settings_changed'] !== false) {
throw new LogicException('Read-only installation preflight failed.');
}
if ($argv[1] === '--preview') {
echo json_encode(['state' => 'preview', 'verified_archive' => true, 'read_only_preflight' => true,
'changed_keys' => array_keys($changes), 'settings_or_database_written' => false], JSON_THROW_ON_ERROR).PHP_EOL;
exit;
}
if (file_exists($destination)) {
throw new LogicException('Existing inventory destination refused; preserve and inspect it.');
}
$backup = 'C:/laragon/data/compras/backups/inventory-activation-'.gmdate('Ymd-His').'-'.bin2hex(random_bytes(6));
if (! mkdir($backup, 0700, true) || file_put_contents($backup.'/settings.before.private.json', $before, LOCK_EX) !== strlen($before)
|| ! hash_equals(hash('sha256', $before), hash_file('sha256', $backup.'/settings.before.private.json'))) {
throw new LogicException('Verified private settings backup required.');
}
$db = new PDO('pgsql:host=127.0.0.1;port=5432;dbname='.$settings['DB_DATABASE'], $settings['DB_USERNAME'], $settings['DB_PASSWORD'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$db->exec('SET default_transaction_read_only=on');
$frames = static function () use ($db): array {
$db->beginTransaction();
try {
$db->exec('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
$out = [];
foreach ($db->query("SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename")->fetchAll(PDO::FETCH_COLUMN) as $table) {
$quoted = '"'.str_replace('"', '""', $table).'"';
$rows = $db->query('SELECT row_to_json(t)::text FROM '.$quoted.' t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN);
$out[$table] = hash('sha256', json_encode($rows, JSON_THROW_ON_ERROR));
}
$db->commit();
return $out;
} finally {
if ($db->inTransaction()) {
$db->rollBack();
}
}
};
$baseline = $frames();
file_put_contents($backup.'/table-hashes.private.json', json_encode($baseline, JSON_THROW_ON_ERROR));
$protected = ['env' => hash_file('sha256', $root.'/.env'), 'frontend' => hash_file('sha256', $root.'/public/build/manifest.json')];
$zip = new ZipArchive;
if ($zip->open($archive) !== true || $zip->numFiles !== 4) {
throw new LogicException('Reviewed archive required.');
}
InventoryConversionRuntime::assertArchiveEntries($zip);
if (! mkdir($destination, 0700) || ! $zip->extractTo($destination) || ! $zip->close()) {
throw new LogicException('Inventory extraction failed; settings not changed.');
}
InventoryConversionRuntime::verifyArtifact($destination);
$temporary = $settingsPath.'.inventory-'.bin2hex(random_bytes(6)).'.tmp';
try {
if (file_put_contents($temporary, $encoded, LOCK_EX) !== strlen($encoded)
|| ! hash_equals(hash('sha256', $before), hash_file('sha256', $settingsPath)) || ! rename($temporary, $settingsPath)) {
throw new LogicException('Atomic settings selection failed.');
}
$postcheck = new Process([PHP_BINARY, 'scripts/check-inventory-conversion-pilot.php', '--installed-read-only'], $root, timeout: 30);
$postcheck->mustRun();
$verified = json_decode($postcheck->getOutput(), true, 32, JSON_THROW_ON_ERROR);
if ($verified['state'] !== 'passed' || ! $verified['inventory_component_installed_or_activated'] || $frames() !== $baseline
|| hash_file('sha256', $root.'/.env') !== $protected['env'] || hash_file('sha256', $root.'/public/build/manifest.json') !== $protected['frontend']
|| json_decode(file_get_contents($settingsPath), true, 32, JSON_THROW_ON_ERROR) !== $after) {
throw new LogicException('Installed component conservation failed.');
}
$result = ['date' => gmdate('c'), 'state' => 'passed', 'authorization' => 'inventory-conversion-pilot-authorization.json',
'target' => 'http://127.0.0.1:8005', 'backup_directory' => $backup, 'inventory_directory' => $destination,
'inventory_manifest_sha256' => InventoryConversionRuntime::MANIFEST_SHA256,
'settings_before_sha256' => hash('sha256', $before), 'settings_after_sha256' => hash('sha256', $encoded),
'changed_keys' => array_keys($changes), 'all_database_tables_preserved' => count($baseline),
'unrelated_settings_env_frontend_and_financial_receipt_preserved' => true,
'new_permissions_users_migrations_or_business_commands' => false, 'application_complete' => false];
file_put_contents($resultPath, json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;
} catch (Throwable $error) {
// No blind downgrade after possible converted effects or concurrent business edits.
file_put_contents($backup.'/failure.private.json', json_encode(['state' => 'requires_inspection_before_configuration_recovery', 'error_class' => $error::class], JSON_THROW_ON_ERROR));
throw $error;
} finally {
if (is_file($temporary)) {
unlink($temporary);
}
}