markTestSkipped('Use the owned standalone upgrade runner.'); } if (! app()->environment('testing') || DB::getDriverName() !== 'pgsql' || DB::connection()->getConfig('host') !== '127.0.0.1' || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', DB::connection()->getDatabaseName()) || config('standalone-runtime.enabled') === true || config('standalone-runtime.prepare_only') !== true) { throw new LogicException('Owned synthetic legacy component fixture required.'); } BudgetDistributionSchema::install(); SettlementSchema::install(); config(['common-payables.source_instance' => 'synthetic-legacy-component-upgrade']); $this->installer = app(StandaloneInstallation::class); $this->arguments = [DB::connection()->getDatabaseName(), DB::connection()->getConfig('username'), 'Synthetic named upgrade operator', 'Explicit synthetic preservation of recognised canonical components']; }); test('existing canonical components require explicit selection and unknown or partial structures are refused before writes', function () { expect(StandaloneComponentSchema::reusable(DB::connection()))->toBe(array_keys(StandaloneComponentSchema::SUPPORTED)); expect(fn () => $this->installer->prepare(...[...$this->arguments, true])) ->toThrow(LogicException::class, 'COMPRAS_RUNTIME_EXTENSION_SCHEMA_EXISTS'); expect(DB::getSchemaBuilder()->hasTable('compras_runtime_installations'))->toBeFalse() ->and(DB::getSchemaBuilder()->hasTable('gl_accounts'))->toBeFalse(); DB::statement('ALTER TABLE ps_payments DISABLE TRIGGER ps_history_immutable'); expect(fn () => $this->installer->prepare(...[...$this->arguments, false, false, true])) ->toThrow(LogicException::class, 'COMPRAS_RUNTIME_COMPONENT_SCHEMA_UNSUPPORTED:supplier-settlements-v1'); DB::statement('ALTER TABLE ps_payments ENABLE TRIGGER ps_history_immutable'); DB::statement('ALTER TABLE ps_payments ADD COLUMN unknown_contract text'); expect(fn () => $this->installer->prepare(...[...$this->arguments, true, false, true])) ->toThrow(LogicException::class, 'COMPRAS_RUNTIME_COMPONENT_SCHEMA_UNSUPPORTED:supplier-settlements-v1'); DB::statement('ALTER TABLE ps_payments DROP COLUMN unknown_contract'); DB::statement('ALTER TABLE ps_outbox RENAME TO ps_outbox_unsupported'); expect(fn () => $this->installer->prepare(...[...$this->arguments, true, false, true])) ->toThrow(LogicException::class, 'COMPRAS_RUNTIME_COMPONENT_SCHEMA_UNSUPPORTED:supplier-settlements-v1'); expect(DB::getSchemaBuilder()->hasTable('compras_runtime_installations'))->toBeFalse() ->and(DB::getSchemaBuilder()->hasTable('gl_accounts'))->toBeFalse(); }); test('explicit component upgrade preserves an independently confirmed partial payment and all original columns', function () { $f = paymentFixture(); foreach ([$f['author'], $f['validator']] as $actor) { foreach (['view', 'prepare', 'validate', 'payments_view', 'payment_prepare', 'payment_confirm', 'allocation_prepare', 'allocation_confirm'] as $action) { DB::table('pa_access')->insert(['tenant_id' => $actor->tenant_id, 'company_id' => 0, 'unit_id' => 0, 'user_id' => $actor->id, 'person' => 'synthetic-upgrade-person-'.$actor->id, 'action' => $action]); } } $payables = app(SupplierPayables::class); $this->actingAs($f['author']); $proposal = $payables->engine($f['author'])->command('prepare', $f['invoice']->id, 'Legacy synthetic invoice capture', (string) Str::uuid()); $this->actingAs($f['validator']); $recognised = $payables->engine($f['validator'])->command('validate', $proposal['proposal_id'], 'Independent legacy recognition', (string) Str::uuid()); $run = function ($actor, $action, $data) use ($payables) { $this->actingAs($actor); return $payables->engine($actor)->settlements()->command($action, $data, (string) Str::uuid()); }; $payment = $run($f['author'], 'payment-prepare', ['obligation_id' => $recognised['obligation_id'], 'source_revision' => $recognised['source_revision'], 'amount' => '40.00', 'currency' => 'MZN', 'paid_on' => today()->toDateString(), 'fact_reference' => 'SIM-UPGRADE-PAYMENT', 'evidence_reference' => 'SIM-UPGRADE-EVIDENCE', 'reason' => 'Existing synthetic partial payment']); $run($f['validator'], 'payment-decide', ['payment_id' => $payment['payment_id'], 'decision' => 'confirmed', 'evidence_reference' => 'SIM-UPGRADE-CONFIRMED', 'reason' => 'Independent legacy payment confirmation']); $allocation = $run($f['author'], 'allocation-prepare', ['payment_id' => $payment['payment_id'], 'reference' => 'SIM-UPGRADE-ALLOCATION', 'allocations' => [['obligation_id' => $recognised['obligation_id'], 'source_revision' => $recognised['source_revision'], 'amount' => '40.00']], 'reason' => 'Explicit legacy invoice allocation']); $run($f['validator'], 'allocation-decide', ['proposal_id' => $allocation['proposal_id'], 'decision' => 'confirmed', 'reason' => 'Independent legacy allocation confirmation']); $columns = $before = []; foreach (DB::getSchemaBuilder()->getTableListing() as $table) { $columns[$table] = DB::getSchemaBuilder()->getColumnListing($table); $before[$table] = DB::table($table)->select($columns[$table])->orderByRaw('1')->get()->toJson(); } $prepare = function (bool $apply): array { $arguments = ['--expected-database' => $this->arguments[0], '--expected-role' => $this->arguments[1], '--operator' => $this->arguments[2], '--reason' => $this->arguments[3], '--reuse-components' => true]; if ($apply) { $arguments['--apply'] = true; } expect(Artisan::call('compras:runtime:prepare', $arguments))->toBe(0); return json_decode(Artisan::output(), true, 32, JSON_THROW_ON_ERROR); }; $preview = $prepare(false); expect($preview['written'])->toBeFalse()->and($preview['existing_components'])->toBe(array_keys(StandaloneComponentSchema::SUPPORTED)) ->and(DB::getSchemaBuilder()->hasTable('compras_runtime_installations'))->toBeFalse(); $applied = $prepare(true); expect($applied['state'])->toBe('prepared')->and($applied['written'])->toBeTrue(); expect($prepare(true)['state'])->toBe('already_prepared'); foreach ($before as $table => $expected) { expect(DB::table($table)->select($columns[$table])->orderByRaw('1')->get()->toJson(), $table)->toBe($expected); } expect(DB::table('compras_runtime_installations')->count())->toBe(1) ->and(DB::table('compras_runtime_installations')->value('reason'))->toContain('supplier-settlements-v1') ->and(DB::table('ps_proposals')->where('id', $allocation['proposal_id'])->value('allocation_cycle'))->toBe(0); $settings = config('standalone-runtime'); $settings['enabled'] = true; $settings['prepare_only'] = false; $settings['features'] = array_fill_keys(StandaloneRuntime::FEATURES, true); config(['standalone-runtime' => $settings]); $runtime = new StandaloneRuntime($settings, new StandaloneArtifact); app()->instance(StandaloneRuntime::class, $runtime); $runtime->configure(DB::connection()); $this->actingAs($f['validator']); $listing = $payables->settlements($f['validator'])->listing(); expect($listing['payments'])->toHaveCount(1)->and($listing['allocations'])->toHaveCount(1) ->and($listing['payments'][0]['amount_minor'])->toBe(4000) ->and($payables->engine($f['validator'])->positionForSource($f['invoice']->id)['balance_minor'])->toBe(2000) ->and(DB::table('gl_journals')->count())->toBe(0)->and(DB::table('external_payments')->count())->toBe(0); }); test('a failure during additive component upgrade rolls back all new schema and permits one safe retry', function () { DB::statement('CREATE VIEW ps_reversals AS SELECT 1 AS synthetic_conflict'); expect(fn () => $this->installer->prepare(...[...$this->arguments, true, false, true])) ->toThrow(QueryException::class); expect(StandaloneComponentSchema::reusable(DB::connection()))->toBe(array_keys(StandaloneComponentSchema::SUPPORTED)) ->and(DB::getSchemaBuilder()->hasTable('compras_runtime_installations'))->toBeFalse() ->and(DB::getSchemaBuilder()->hasTable('gl_accounts'))->toBeFalse() ->and(DB::getSchemaBuilder()->hasTable('budget_order_commands'))->toBeFalse() ->and(DB::getSchemaBuilder()->hasColumn('ps_proposals', 'allocation_cycle'))->toBeFalse(); DB::statement('DROP VIEW ps_reversals'); expect($this->installer->prepare(...[...$this->arguments, true, false, true])['state'])->toBe('prepared') ->and(DB::table('compras_runtime_installations')->count())->toBe(1); });