This commit is contained in:
2026-11-02 20:31:15 +02:00
parent 8ddfa93f45
commit 1da273f1f3
131 changed files with 14802 additions and 526 deletions
@@ -0,0 +1,86 @@
<?php
// Synthetic metadata baseline for the historical fiscal preservation tests only.
use App\Models\SimulationFormDraft;
use App\Models\User;
use App\Support\IsolatedQaDatabase;
use Database\Seeders\ProductionReferenceSeeder;
use Database\Seeders\TerritoryReferenceSeeder;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
require dirname(__DIR__).'/vendor/autoload.php';
try {
if (PHP_SAPI !== 'cli' || ! isset($argv)) {
throw new RuntimeException('Explicit CLI arguments required.');
}
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$referencesOnly = in_array('--references-only', $argv, true);
$allowed = $referencesOnly ? ['simulator_fiscal_delta_20261021_test', 'proactive_investment_20261027_test'] : ['proactive_investment_20261027_test'];
if ($app->configurationIsCached() || ! IsolatedQaDatabase::allowsSchemaChanges()
|| ! in_array(DB::connection()->getDatabaseName(), $allowed, true)) {
throw new RuntimeException('Explicit isolated proactive QA database required.');
}
$marker = 'synthetic-fiscal-preservation-20261102';
$tables = ['opportunities', 'documents', 'opportunity_documents', 'simulation_form_drafts', 'users', 'organizations', 'audit_events'];
if (! $referencesOnly) {
DB::transaction(function () use ($marker, $tables): void {
DB::select('select pg_advisory_xact_lock(261102, 1)');
$actor = User::find(1021);
if ($actor && $actor->email !== 'synthetic-fiscal-preservation@example.invalid') {
throw new RuntimeException('Existing identity 1021 differs; no account may be reset.');
}
if (DB::table('audit_events')->where('source_system', $marker)->exists()) {
// Only this tool's own synthetic baseline may be archived. A live
// draft for this actor would correctly prevent creating another.
DB::table('simulation_form_drafts')->where('owner_user_id', 1021)
->where('create_key_hash', hash('sha256', $marker.'-key'))->whereNull('deleted_at')
->update(['deleted_at' => now()]);
foreach ($tables as $table) {
if (DB::table($table)->count() === 0) {
throw new RuntimeException('An existing baseline is incomplete; inspect before repairing.');
}
}
return;
}
if (! $actor) {
$actor = User::factory()->create(['id' => 1021, 'name' => 'SYNTHETIC QA fiscal preservation',
'email' => 'synthetic-fiscal-preservation@example.invalid', 'password' => bcrypt(Str::random(64)), 'email_verified_at' => now()]);
DB::statement("select setval(pg_get_serial_sequence('users','id'), greatest((select max(id) from users), 1021))");
}
$organization = (string) Str::uuid();
DB::table('organizations')->insert(['id' => $organization, 'name' => 'SYNTHETIC QA preservation organization',
'organization_type' => 'private', 'source_system' => $marker, 'data_classification' => 'internal']);
$sector = DB::table('sectors')->insertGetId(['name' => 'SYNTHETIC QA preservation sector', 'slug' => $marker, 'is_active' => true]);
$opportunity = DB::table('opportunities')->insertGetId(['reference' => $marker, 'slug' => $marker,
'title' => 'SYNTHETIC QA preservation opportunity', 'status' => 'draft', 'sector_id' => $sector]);
$document = (string) Str::uuid();
$bytes = 'SYNTHETIC QA metadata baseline; not a scanned or downloadable document.';
DB::table('documents')->insert(['id' => $document, 'file_name' => 'SYNTHETIC-QA-METADATA.txt', 'media_type' => 'text/plain',
'storage_key' => $marker.'/'.$document, 'sha256' => hash('sha256', $bytes), 'status' => 'active', 'uploaded_by' => $actor->id,
'source_system' => $marker, 'data_classification' => 'internal']);
DB::table('opportunity_documents')->insert(['id' => (string) Str::uuid(), 'opportunity_id' => $opportunity,
'document_id' => $document, 'payload' => json_encode(['title' => 'SYNTHETIC metadata preservation only', 'visibility' => 'internal']), 'created_by' => $actor->id]);
SimulationFormDraft::create(['id' => (string) Str::uuid(), 'owner_user_id' => $actor->id, 'version_no' => 1, 'deleted_at' => now(),
'create_key_hash' => hash('sha256', $marker.'-key'), 'create_payload_hash' => hash('sha256', $marker.'-payload'),
'snapshot_sha256' => hash('sha256', $marker.'-snapshot'), 'personal_snapshot' => SimulationFormDraft::encryptSnapshot([]),
'economic_snapshot' => SimulationFormDraft::encryptSnapshot(['project_title' => 'SYNTHETIC pre-existing draft'])]);
DB::table('audit_events')->insert(['source_system' => $marker, 'entity_type' => 'qa-fixture', 'actor_user_id' => $actor->id,
'action' => 'synthetic_preservation_baseline', 'after_data' => json_encode(['synthetic' => true, 'metadata_only' => true])]);
});
}
$seeder = $referencesOnly ? ProductionReferenceSeeder::class : TerritoryReferenceSeeder::class;
if (Artisan::call('db:seed', ['--class' => $seeder, '--force' => true]) !== 0) {
throw new RuntimeException('Packaged territory reference preparation failed.');
}
echo json_encode(['database' => DB::connection()->getDatabaseName(), 'synthetic_metadata' => ! $referencesOnly, 'packaged_territories' => DB::table('geo_locations')->count(),
'counts' => array_combine($tables, array_map(fn (string $table): int => DB::table($table)->count(), $tables))], JSON_THROW_ON_ERROR).PHP_EOL;
} catch (Throwable $error) {
fwrite(STDERR, $error::class.': fixture preparation failed ('.$error->getCode().').'.PHP_EOL);
exit(1);
}
+164
View File
@@ -0,0 +1,164 @@
<?php
// A real two-process check, restricted to this disposable loopback QA database.
use App\Models\User;
use App\Services\FundingProgrammeReferences;
use App\Services\FundingProgrammes;
use App\Support\IsolatedQaDatabase;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\RecordNotFoundException;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Process\Process;
require dirname(__DIR__).'/vendor/autoload.php';
function requireCheck(bool $condition, string $message): void
{
if (! $condition) {
throw new RuntimeException($message);
}
}
try {
requireCheck(PHP_SAPI === 'cli' && getenv('APP_ENV') === 'testing'
&& getenv('DB_CONNECTION') === 'pgsql' && getenv('DB_HOST') === '127.0.0.1'
&& getenv('DB_PORT') === '55441' && preg_match('/^funding_concurrency_20261102[a-z]?_test$/D', getenv('DB_DATABASE') ?: '') === 1
&& in_array(getenv('DB_URL'), [false, ''], true), 'Explicit disposable funding QA environment required.');
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
requireCheck(! $app->configurationIsCached() && IsolatedQaDatabase::allowsSchemaChanges()
&& DB::connection()->getDatabaseName() === getenv('DB_DATABASE'), 'Effective QA target rejected.');
DB::statement("SET statement_timeout = '45s'");
$service = app(FundingProgrammes::class);
requireCheck($service->installed(), 'Migrate the empty disposable database first.');
$directory = storage_path('app/private/funding-concurrency-20261102');
if (($argv[1] ?? null) === '--worker') {
$path = realpath($argv[2] ?? '');
requireCheck($path !== false && dirname($path) === realpath($directory), 'Worker context path rejected.');
$context = json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
requireCheck(($context['database'] ?? null) === DB::connection()->getDatabaseName(), 'Worker database context differs.');
$task = $argv[3] ?? '';
$index = (int) ($argv[4] ?? -1);
requireCheck(in_array($index, [0, 1], true) && in_array($task, ['prepare', 'save', 'publish', 'interest', 'withdraw'], true), 'Worker operation rejected.');
$actor = User::findOrFail($context['actor']);
requireCheck($actor->email === 'synthetic-funding-concurrency@example.invalid', 'Synthetic identity required.');
config(['portal.managers' => [$actor->email]]);
$application = 'funding-qa-'.$context['run'].'-'.$task.'-'.$index;
DB::select("SELECT set_config('application_name', ?, false)", [$application]);
try {
$result = match ($task) {
'prepare' => app(FundingProgrammeReferences::class)->prepare($actor, 'fdel', $context['reference_hash']),
'save' => ['id' => $service->save($actor, $context['id'], ['version' => $context['version'], 'code' => 'fdel',
'reason' => 'Alteração sintética concorrente do processo '.$index.'.',
'draft' => [...$context['draft'], 'summary' => 'SINTÉTICO QA — Alteração concorrente '.$index]])],
'publish' => ['version_id' => $service->publish($actor, $context['id'], ['version' => $context['version'], 'hash' => $context['publication_hash'],
'reason' => 'Publicação sintética concorrente para ensaio isolado.', 'confirm' => true])],
'interest' => (function () use ($service, $actor, $context): array {
$service->saveInterest($actor, $context['id'], $context['published'], true);
return ['saved' => true];
})(),
'withdraw' => (function () use ($service, $actor, $context): array {
$service->withdraw($actor, $context['id'], $context['version'], 'Retirada sintética concorrente para ensaio isolado.');
return ['withdrawn' => true];
})(),
};
echo json_encode(['status' => 'ok', 'result' => $result], JSON_THROW_ON_ERROR).PHP_EOL;
} catch (HttpExceptionInterface $error) {
echo json_encode(['status' => $error->getStatusCode() === 409 ? 'conflict' : 'http_error', 'code' => $error->getStatusCode()], JSON_THROW_ON_ERROR).PHP_EOL;
} catch (RecordNotFoundException $error) {
echo json_encode(['status' => 'unavailable', 'code' => 404], JSON_THROW_ON_ERROR).PHP_EOL;
}
exit(0);
}
requireCheck(($argv[1] ?? null) === '--run', 'Use --run with an explicitly configured empty QA database.');
$lock = fopen(storage_path('framework/qa-database-'.hash('sha256', '127.0.0.1:55441:'.DB::connection()->getDatabaseName()).'.lock'), 'c');
requireCheck($lock !== false && flock($lock, LOCK_EX | LOCK_NB), 'Another QA runner owns this database.');
requireCheck(DB::table('users')->count() === 0 && DB::table('funding_programmes')->count() === 0, 'Disposable database must be empty; existing records will not be reset.');
if (! is_dir($directory)) {
mkdir($directory, 0700, true);
}
$actor = User::factory()->create(['name' => 'SYNTHETIC QA funding concurrency', 'email' => 'synthetic-funding-concurrency@example.invalid']);
config(['portal.managers' => [$actor->email]]);
$run = bin2hex(random_bytes(5));
$context = ['actor' => $actor->id, 'run' => $run, 'database' => DB::connection()->getDatabaseName(), 'reference_hash' => app(FundingProgrammeReferences::class)->preview($actor, 'fdel')['hash']];
$checks = [];
$phase = function (string $name, array $tasks, string $heldLock) use (&$context, $directory, &$checks): array {
$path = $directory.'/'.$name.'-'.$context['run'].'.json';
file_put_contents($path, json_encode($context, JSON_THROW_ON_ERROR));
DB::beginTransaction();
if ($heldLock === 'reference') {
DB::select('select pg_advisory_xact_lock(261102, hashtext(?))', ['fdel']);
} elseif ($heldLock === 'actor') {
DB::table('users')->where('id', $context['actor'])->lockForUpdate()->firstOrFail();
} else {
DB::table('funding_programmes')->where('id', $context['id'])->lockForUpdate()->firstOrFail();
}
$processes = [];
$observed = [];
foreach ($tasks as $index => $task) {
$process = new Process([PHP_BINARY, __FILE__, '--worker', $path, $task, (string) $index], dirname(__DIR__));
$process->setTimeout(60);
$process->start();
$processes[] = $process;
$application = 'funding-qa-'.$context['run'].'-'.$task.'-'.$index;
$deadline = microtime(true) + 40;
do {
DB::select('SELECT pg_stat_clear_snapshot()');
$waiting = DB::selectOne("SELECT pid,wait_event,pg_blocking_pids(pid) AS blockers FROM pg_stat_activity WHERE datname=current_database() AND application_name=? AND wait_event_type='Lock'", [$application]);
if ($waiting) {
$observed[] = (array) $waiting;
break;
}
requireCheck($process->isRunning(), 'Worker exited before overlapping lock wait: '.$name.' '.$process->getErrorOutput());
usleep(100000);
} while (microtime(true) < $deadline);
requireCheck($waiting !== null, 'No actual lock overlap observed: '.$name);
}
DB::commit();
$results = [];
foreach ($processes as $process) {
$process->wait();
requireCheck($process->isSuccessful(), 'Worker failed: '.$name.' '.$process->getErrorOutput());
$results[] = json_decode(trim($process->getOutput()), true, 512, JSON_THROW_ON_ERROR);
}
$checks[$name] = ['overlap_observed' => $observed, 'results' => $results];
echo json_encode(['phase' => $name, 'workers' => count($results), 'lock_overlap_verified' => true], JSON_THROW_ON_ERROR).PHP_EOL;
return $results;
};
$results = $phase('reference', ['prepare', 'prepare'], 'reference');
requireCheck(array_column($results, 'status') === ['ok', 'ok']
&& count(array_filter(array_column(array_column($results, 'result'), 'created'))) === 1, 'Reference preparation duplicated or failed.');
requireCheck(DB::table('funding_programmes')->count() === 1 && DB::table('audit_events')->where('action', 'reference_prepared')->count() === 1, 'Reference history duplicated.');
$row = DB::table('funding_programmes')->where('code', 'fdel')->firstOrFail();
$context = [...$context, 'id' => $row->id, 'version' => (int) $row->version, 'draft' => json_decode($row->draft, true)];
$results = $phase('draft', ['save', 'save'], 'reference');
$statuses = array_column($results, 'status');
sort($statuses);
requireCheck($statuses === ['conflict', 'ok'] && (int) DB::table('funding_programmes')->where('id', $row->id)->value('version') === 2, 'Concurrent stale draft was not rejected.');
$plan = $service->preview($actor, $row->id);
$context = [...$context, 'version' => $plan['version'], 'publication_hash' => $plan['hash']];
$results = $phase('publication', ['publish', 'publish'], 'programme');
requireCheck(array_column($results, 'status') === ['ok', 'ok'] && $results[0]['result']['version_id'] === $results[1]['result']['version_id']
&& DB::table('funding_programme_versions')->count() === 1, 'Concurrent publication duplicated.');
$context['published'] = $results[0]['result']['version_id'];
$results = $phase('interest', ['interest', 'interest'], 'actor');
requireCheck(array_column($results, 'status') === ['ok', 'ok'] && DB::table('funding_programme_interests')->count() === 1
&& DB::table('audit_events')->where('action', 'interest_saved')->count() === 1, 'Concurrent interest duplicated.');
$results = $phase('withdrawal_vs_interest', ['withdraw', 'interest'], 'programme');
requireCheck($results[0]['status'] === 'ok' && in_array($results[1]['status'], ['ok', 'unavailable'], true)
&& DB::table('funding_programmes')->where('id', $row->id)->value('published_version_id') === null
&& DB::table('funding_programme_interests')->count() === 1, 'Withdrawal/interest race violated preservation.');
echo json_encode(['result' => 'passed', 'database' => DB::connection()->getDatabaseName(), 'synthetic' => true, 'phases' => $checks,
'active_writes' => 0, 'external_messages' => 0], JSON_THROW_ON_ERROR).PHP_EOL;
} catch (Throwable $error) {
if (isset($app) && DB::connection()->transactionLevel() > 0) {
DB::rollBack();
}
fwrite(STDERR, json_encode(['result' => 'failed', 'exception' => $error::class, 'code' => $error->getCode(),
'message' => $error instanceof RuntimeException ? $error->getMessage() : 'Inspect isolated QA process failure.'], JSON_THROW_ON_ERROR).PHP_EOL);
exit(1);
}