Files
compras/scripts/distribution-visual-qa.php
T
nala 36dcfdf00c
tests / ci (push) Canceled after 0s
commit melhorias compras4
2026-11-22 16:28:03 +02:00

124 lines
7.5 KiB
PHP

<?php
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
require dirname(__DIR__).'/vendor/autoload.php';
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (in_array('--refresh-running-preview', $argv, true)) {
$proof = json_decode(file_get_contents(storage_path('framework/testing/distribution-visual-running.json')), true, 32, JSON_THROW_ON_ERROR);
$private = 'C:/laragon/data/sgf-tempo/qa/distribution-visual-'.$proof['database'];
if (! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $proof['database']) || str_replace('\\', '/', dirname($proof['stop_file'])) !== $private
|| ! is_file($private.'/settings.json')) {
throw new LogicException('Private review identity mismatch.');
}
$candidate = storage_path('framework/testing/sgc-distribution-integrated-qa-20261122');
$expected = $argv[array_search('--refresh-running-preview', $argv, true) + 1] ?? '';
if (! preg_match('/^[a-f0-9]{64}$/D', $expected) || hash_file('sha256', $candidate.'/manifest.json') !== $expected) {
throw new LogicException('Explicit validated private manifest hash required.');
}
$target = $private.'/public/build';
$previous = hash_file('sha256', $target.'/manifest.json');
copy($target.'/manifest.json', $private.'/manifest-before-'.$previous.'.json');
(new Filesystem)->copyDirectory($candidate.'/assets', $target.'/assets');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($candidate.'/assets', FilesystemIterator::SKIP_DOTS)) as $file) {
$relative = substr($file->getPathname(), strlen($candidate.'/assets'));
if (hash_file('sha256', $file->getPathname()) !== hash_file('sha256', $target.'/assets'.$relative)) {
throw new RuntimeException('Private candidate asset verification failed.');
}
}
copy($candidate.'/fonts-manifest.json', $target.'/fonts-manifest.json');
copy($candidate.'/manifest.json', $target.'/manifest.next.json');
if (! rename($target.'/manifest.next.json', $target.'/manifest.json')) {
throw new RuntimeException('Private manifest replacement failed.');
}
$proof['private_manifest_sha256'] = $expected;
file_put_contents(storage_path('framework/testing/distribution-visual-running.json'), json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
echo json_encode(['private_preview_refreshed' => true, 'previous_manifest' => $previous, 'manifest_sha256' => $expected, 'published_assets_changed' => false], JSON_THROW_ON_ERROR).PHP_EOL;
exit;
}
if (! in_array('--prepare-private-review', $argv, true)) {
throw new LogicException('Explicit private QA review switch required.');
}
$socket = @stream_socket_server('tcp://127.0.0.1:8006', $errorCode, $error);
if ($socket === false) {
throw new RuntimeException('QA port 8006 is already in use; no existing server was altered.');
}
fclose($socket);
$config = DB::connection('pgsql')->getConfig();
unset($config['name']);
if ($config['host'] !== '127.0.0.1') {
throw new LogicException('Only local PostgreSQL QA is supported.');
}
$database = 'compras_receipts_test_'.bin2hex(random_bytes(6));
$private = 'C:/laragon/data/sgf-tempo/qa/distribution-visual-'.$database;
if (! mkdir($private.'/public', 0700, true) && ! is_dir($private.'/public')) {
throw new RuntimeException('Private review directory creation failed.');
}
$candidate = storage_path('framework/testing/sgc-distribution-integrated-qa-20261122');
if (hash_file('sha256', $candidate.'/manifest.json') !== '1edb9ab0aa62fcf4e9c9075ce6e405de5435dc8ea4799d1949535a96c718df4e') {
throw new RuntimeException('The private candidate differs from the validated build.');
}
(new Filesystem)->copyDirectory($candidate, $private.'/public/build');
foreach (['favicon.ico', 'favicon.svg'] as $file) {
if (is_file(public_path($file))) {
copy(public_path($file), $private.'/public/'.$file);
}
}
$environment = ['APP_ENV' => 'testing', 'APP_DEBUG' => 'false', 'APP_KEY' => config('app.key'), 'APP_NAME' => 'SGC QA sintética',
'APP_URL' => 'http://127.0.0.1:8006', 'ASSET_URL' => 'http://127.0.0.1:8006', 'DB_CONNECTION' => 'pgsql', 'DB_URL' => '',
'DB_HOST' => $config['host'], 'DB_PORT' => (string) $config['port'], 'DB_DATABASE' => $database, 'DB_USERNAME' => $config['username'], 'DB_PASSWORD' => $config['password'],
'CACHE_STORE' => 'array', 'SESSION_DRIVER' => 'file', 'SESSION_COOKIE' => 'sgc_distribution_qa_'.substr($database, -12),
'MAIL_MAILER' => 'array', 'QUEUE_CONNECTION' => 'sync', 'BROADCAST_CONNECTION' => 'null', 'BCRYPT_ROUNDS' => '4',
'COMMON_BUDGET_PATH' => 'C:/laragon/www/sgf-tempo/packages/budget-control', 'SGC_BUDGET_DISTRIBUTIONS_QA_ENABLED' => 'true',
'SGC_DISTRIBUTION_QA_PUBLIC' => $private.'/public'];
file_put_contents($private.'/settings.json', json_encode($environment, JSON_THROW_ON_ERROR));
config(['database.connections.distribution_visual_admin' => [...$config, 'database' => 'postgres', 'url' => null]]);
$admin = DB::connection('distribution_visual_admin');
if (! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $database) || $database === $config['database']) {
throw new LogicException('Invalid disposable visual database.');
}
$admin->statement('CREATE DATABASE "'.$database.'"');
$server = null;
try {
foreach ([[PHP_BINARY, 'tests/Concurrency/quotation-exchange-worker.php', $database, 'migrate'],
[PHP_BINARY, 'tests/Concurrency/budget-distribution-visual-seed.php', $database]] as $command) {
$process = new Process($command, dirname(__DIR__), $environment, timeout: 60);
$process->mustRun();
$seedOutput = $process->getOutput();
}
$seed = json_decode(trim($seedOutput), true, 32, JSON_THROW_ON_ERROR);
$server = new Process([PHP_BINARY, '-S', '127.0.0.1:8006', '-t', $private.'/public', 'tests/Support/distribution-visual-router.php'],
dirname(__DIR__), [...$environment, 'SGC_DISTRIBUTION_QA_SETTINGS_FILE' => $private.'/settings.json'], timeout: null);
$server->start();
$proof = ['status' => 'private_review_running', 'url' => 'http://127.0.0.1:8006', 'database' => $database,
'stop_file' => $private.'/stop', 'synthetic_fixture' => $seed, 'published_assets_changed' => false, 'separate_session_cookie' => true];
file_put_contents(storage_path('framework/testing/distribution-visual-running.json'), json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
echo json_encode($proof, JSON_THROW_ON_ERROR).PHP_EOL;
$deadline = microtime(true) + 3600;
while (! is_file($private.'/stop') && microtime(true) < $deadline) {
if (! $server->isRunning()) {
throw new RuntimeException('Private QA server stopped unexpectedly.');
}
$server->getIncrementalOutput();
$server->getIncrementalErrorOutput();
usleep(100000);
}
} finally {
$server?->stop();
$admin->statement('DROP DATABASE "'.$database.'" WITH (FORCE)');
if (is_file($private.'/settings.json')) {
unlink($private.'/settings.json');
}
if (isset($proof)) {
$proof['status'] = 'private_review_stopped';
$proof['isolated_database_removed'] = true;
$proof['private_settings_removed'] = true;
file_put_contents(storage_path('framework/testing/distribution-visual-running.json'), json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
}
echo json_encode(['private_server_stopped' => true, 'isolated_database_removed' => true, 'private_settings_removed' => true], JSON_THROW_ON_ERROR).PHP_EOL;
}