Files
Investidor-V2/tools/controlled-investment-qa.php
T

91 lines
6.2 KiB
PHP

<?php
use App\Models\Sector;
use App\Models\User;
use App\Services\InvestorContext;
use App\Services\InvestorPath;
use App\Services\VirtualDesk;
use App\Support\IsolatedQaDatabase;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
try {
// Long-lived synthetic QA. Never run RefreshDatabase suites against this database.
$qaDatabase = 'controlled_5f7db7cd_20261120_test';
$qaConfigCache = dirname(__DIR__).'/outputs/controlled-5f7db7cd-20261120/no-config.php';
putenv('APP_CONFIG_CACHE='.$qaConfigCache);
$_ENV['APP_CONFIG_CACHE'] = $_SERVER['APP_CONFIG_CACHE'] = $qaConfigCache;
foreach (['APP_ENV' => 'testing', 'DB_CONNECTION' => 'pgsql', 'DB_HOST' => '127.0.0.1', 'DB_PORT' => '55441', 'DB_DATABASE' => $qaDatabase, 'DB_USERNAME' => 'investmoz_test', 'DB_PASSWORD' => '', 'DB_URL' => '', 'DATABASE_URL' => '', 'CACHE_STORE' => 'array', 'MAIL_MAILER' => 'array', 'QUEUE_CONNECTION' => 'sync', 'BROADCAST_CONNECTION' => 'null', 'SUITECRM_ENABLED' => 'false', 'INVESTMOZ_AI_ENABLED' => 'false', 'INVESTOR_PATH_ENABLED' => 'true'] as $key => $value) {
putenv($key.'='.$value);
$_ENV[$key] = $_SERVER[$key] = $value;
}
require __DIR__.'/continuity-qa-bootstrap.php';
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$identity = DB::selectOne('select current_database() as name, inet_server_port() as port');
if (! IsolatedQaDatabase::allowsSchemaChanges() || $identity->name !== $qaDatabase || (int) $identity->port !== 55441) {
throw new RuntimeException('Controlled synthetic QA identity rejected');
}
if (in_array('--migrate', $argv, true)) {
$tables = DB::select("select tablename from pg_tables where schemaname='public' and tablename <> 'spatial_ref_sys'");
if ($tables !== []) {
throw new RuntimeException('Schema preparation requires a fresh empty QA database; existing state preserved');
}
Artisan::call('migrate', ['--force' => true]);
echo Artisan::output();
}
if (in_array('--prepare-contexts', $argv, true)) {
$fixture = json_decode(file_get_contents(dirname(__DIR__).'/outputs/controlled-5f7db7cd-20261120/synthetic-fixture.json'), true, 512, JSON_THROW_ON_ERROR);
$owner = User::findOrFail($fixture['owner_id']);
foreach ($fixture['cases'] as $key => $item) {
$case = app(VirtualDesk::class)->case($owner, $item['id']);
if ((int) $case->version !== 1) {
throw new RuntimeException('Context already edited; preserve deliberate changes');
}
app(InvestorPath::class)->initialize($owner, $case->id);
$case = app(VirtualDesk::class)->case($owner, $case->id);
$input = ['version' => $case->version, 'selection' => ['sector' => 'sector:'.$item['sector_id'], 'territory' => 'territory:'.$fixture['geography']['locality']], 'opportunity_choices' => [], 'declarations' => ['project_title' => $key === 'hotel' ? 'Criar Hotel — SINTÉTICO QA' : 'Agronegócio — SINTÉTICO QA', 'products_services' => $key === 'hotel' ? 'Hotel — ensaio sintético' : 'Produção agrícola — ensaio sintético', 'currency' => 'MZN', 'investment_budget_min' => '1000000', 'investment_budget_max' => '5000000']];
$preview = app(InvestorContext::class)->preview($owner, $case->id, $input);
app(InvestorContext::class)->commit($owner, $case->id, [...$input, 'preview_hash' => $preview['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true]);
}
echo "Two synthetic contexts prepared through the normal preview/commit services.\n";
}
if (in_array('--seed', $argv, true)) {
$existing = DB::table('users')->where('email', 'controlled-investor-20261120@example.invalid')->first();
if ($existing) {
throw new RuntimeException('Synthetic actor already exists; preserve it and its history');
}
$fixture = DB::transaction(function () {
$owner = User::factory()->create(['name' => 'Investidor sintético QA controlado', 'email' => 'controlled-investor-20261120@example.invalid']);
$parent = null;
$geography = [];
foreach (['country', 'province', 'district', 'admin_post', 'locality'] as $type) {
$id = (string) Str::uuid();
DB::table('geo_locations')->insert(['id' => $id, 'parent_id' => $parent, 'name' => 'SINTÉTICO QA '.$type, 'code' => $id, 'location_type' => $type, 'data_classification' => 'public', 'source_system' => 'cartografia-workbook', 'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:QA:'.$id]);
$geography[$type] = $id;
$parent = $id;
}
$cases = [];
foreach (['hotel' => 'Turismo sintético QA', 'agro' => 'Agronegócio sintético QA'] as $key => $name) {
$sector = Sector::create(['name' => $name, 'slug' => 'controlled-qa-'.$key, 'is_active' => true, 'published_at' => now()]);
$case = app(VirtualDesk::class)->createCase($owner, ['title' => $name, 'client_key' => (string) Str::uuid(), 'reuse_consent' => true]);
$cases[$key] = ['id' => $case->id, 'sector_id' => $sector->id];
}
return ['synthetic' => true, 'restoration_of_previous_qa' => false, 'owner_id' => $owner->id, 'cases' => $cases, 'geography' => $geography];
});
$directory = dirname(__DIR__).'/outputs/controlled-5f7db7cd-20261120';
if (! is_dir($directory)) {
mkdir($directory, 0700, true);
}
file_put_contents($directory.'/synthetic-fixture.json', json_encode($fixture, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
echo "New synthetic QA fixture recorded; no prior history restored.\n";
}
echo json_encode(['database' => $identity->name, 'port' => $identity->port, 'users' => DB::table('users')->count(), 'cases' => DB::table('desk_cases')->count(), 'baseline' => '5f7db7cd', 'restoration' => false], JSON_THROW_ON_ERROR)."\n";
} catch (Throwable $error) {
fwrite(STDERR, 'QA action rejected: '.$error->getMessage().PHP_EOL);
exit(1);
}