commit compras 2711
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-27 13:49:31 +02:00
parent dc73540558
commit e709d900d1
81 changed files with 5352 additions and 107 deletions
+55 -13
View File
@@ -10,6 +10,7 @@ use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Process\Process;
use Tempo\Finance\Budget\BudgetControl;
use Tempo\Finance\Budget\BudgetFailure;
use Tempo\Finance\Budget\BudgetImportEstimate;
require __DIR__.'/../../vendor/autoload.php';
$database = $argv[1] ?? '';
@@ -21,22 +22,24 @@ if (! $app->environment('testing') || DB::getDriverName() !== 'pgsql' || DB::con
throw new LogicException('Landed-cost contention requires the approved common source and isolated local QA.');
}
require_once __DIR__.'/../Support/ReceiptFixture.php';
if (($argv[2] ?? '') === 'legacy') {
if (in_array($argv[2] ?? '', ['legacy', 'legacy-estimate'], true)) {
$legacyEstimate = ($argv[2] ?? '') === 'legacy-estimate';
$actor = User::query()->findOrFail((int) ($argv[3] ?? 0));
auth()->guard()->setUser($actor);
try {
app(CommonBudget::class)->for($actor);
throw new RuntimeException('The old runtime must refuse balances with landed-cost effects.');
} catch (HttpExceptionInterface $error) {
if ($error->getStatusCode() !== 503 || (defined(BudgetControl::class.'::LANDED_COSTS_VERSION') && BudgetControl::LANDED_COSTS_VERSION === CommonBudget::LANDED_COSTS_CONTRACT)) {
if ($error->getStatusCode() !== 503 || ($legacyEstimate ? defined(BudgetControl::class.'::IMPORT_ESTIMATE_VERSION')
: (defined(BudgetControl::class.'::LANDED_COSTS_VERSION') && BudgetControl::LANDED_COSTS_VERSION === CommonBudget::LANDED_COSTS_CONTRACT))) {
throw $error;
}
echo json_encode(['old_runtime_rejected_before_balance' => true, 'status' => 503,
echo json_encode(['old_runtime_rejected_before_balance' => true, 'old_estimate_runtime_rejected' => $legacyEstimate, 'status' => 503,
'runtime_landed_version' => defined(BudgetControl::class.'::LANDED_COSTS_VERSION') ? BudgetControl::LANDED_COSTS_VERSION : null], JSON_THROW_ON_ERROR).PHP_EOL;
}
exit;
}
if (($argv[2] ?? '') === 'prepare') {
if (in_array($argv[2] ?? '', ['prepare', 'prepare-estimate', 'prepare-reconcile'], true)) {
$f = receiptFixture();
$finance = User::factory()->create(['tenant_id' => $f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]);
$reviewer = User::factory()->create(['tenant_id' => $f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]);
@@ -48,11 +51,22 @@ if (($argv[2] ?? '') === 'prepare') {
$money = ['subtotal_minor' => 2000, 'tax_minor' => 0, 'total_minor' => 2000,
'reference_subtotal_minor' => 2000, 'reference_tax_minor' => 0, 'reference_total_minor' => 2000];
// Contract fixture, not a native document upload or statutory customs action.
echo json_encode(['tenant' => $f['tenant']->id, 'actor' => $finance->id, 'budget' => $budget, 'plan' => (string) Str::uuid(),
$prepared = ['tenant' => $f['tenant']->id, 'actor' => $finance->id, 'budget' => $budget, 'plan' => (string) Str::uuid(),
'estimate' => in_array($argv[2] ?? '', ['prepare-estimate', 'prepare-reconcile'], true), 'reconciliation' => ($argv[2] ?? '') === 'prepare-reconcile', 'requisition' => $f['r']->id,
'version' => 1, 'operation' => (string) Str::uuid(),
'documents' => ['customs' => ['kind' => 'customs_assessment', 'id' => 1, 'source_key' => (string) Str::uuid(), 'source_version' => 1,
'creditor_key' => 'synthetic-authority', 'currency' => 'MZN', 'revision' => hash('sha256', 'Synthetic customs source'), ...$money]],
'components' => [['component_key' => 'duty', 'kind' => 'duty', 'origin' => 'customs', ...$money]]], JSON_THROW_ON_ERROR);
'components' => [['component_key' => 'duty', 'kind' => 'duty', 'origin' => 'customs', ...$money]]];
if ($prepared['reconciliation']) {
$components = array_map(fn ($kind) => ['key' => $kind, 'kind' => $kind, 'status' => 'not_applicable', 'basis' => 'Synthetic contention assumption',
'subtotal_minor' => null, 'tax_minor' => null, 'total_minor' => null], BudgetImportEstimate::KINDS);
$components[0] = [...$components[0], 'status' => 'known', 'subtotal_minor' => 2000, 'tax_minor' => 0, 'total_minor' => 2000];
auth()->guard()->setUser($finance);
$estimate = app(CommonBudget::class)->for($finance)->reserveImportEstimate($prepared['plan'], 1, $budget, $f['r']->id,
['subtotal_minor' => 10000, 'tax_minor' => 0, 'total_minor' => 10000], $components, (string) Str::uuid());
$prepared += ['estimate_hash' => $estimate['plan_hash'], 'actual_plan' => (string) Str::uuid(), 'actual_operation' => (string) Str::uuid()];
}
echo json_encode($prepared, JSON_THROW_ON_ERROR);
exit;
}
if (($argv[2] ?? '') === 'write') {
@@ -61,7 +75,19 @@ if (($argv[2] ?? '') === 'write') {
$actor = User::query()->findOrFail($input['actor']);
auth()->guard()->setUser($actor);
try {
$result = app(CommonBudget::class)->for($actor)->reserveLandedCosts($input['plan'], $input['version'], $input['budget'], $input['components'], $input['documents'], $input['operation']);
$engine = app(CommonBudget::class)->for($actor);
if ($input['reconciliation'] ?? false) {
$result = $engine->reconcileImportEstimate($input['plan'], $input['version'], $input['estimate_hash'], 1, 1000,
$input['actual_plan'], 1, $input['budget'], $input['components'], $input['documents'], $input['operation'], $input['actual_operation']);
} elseif ($input['estimate'] ?? false) {
$components = array_map(fn ($kind) => ['key' => $kind, 'kind' => $kind, 'status' => 'not_applicable', 'basis' => 'Synthetic contention assumption',
'subtotal_minor' => null, 'tax_minor' => null, 'total_minor' => null], BudgetImportEstimate::KINDS);
$components[0] = [...$components[0], 'status' => 'known', 'subtotal_minor' => 2000, 'tax_minor' => 0, 'total_minor' => 2000];
$result = $engine->reserveImportEstimate($input['plan'], $input['version'], $input['budget'], $input['requisition'],
['subtotal_minor' => 10000, 'tax_minor' => 0, 'total_minor' => 10000], $components, $input['operation']);
} else {
$result = $engine->reserveLandedCosts($input['plan'], $input['version'], $input['budget'], $input['components'], $input['documents'], $input['operation']);
}
echo json_encode(['status' => 200, 'replayed' => $result['replayed']], JSON_THROW_ON_ERROR);
} catch (BudgetFailure $error) {
echo json_encode(['status' => $error->status], JSON_THROW_ON_ERROR);
@@ -70,12 +96,15 @@ if (($argv[2] ?? '') === 'write') {
}
exit;
}
if (($argv[2] ?? '') !== 'race') {
if (! in_array($argv[2] ?? '', ['race', 'race-estimate', 'race-estimate-reconcile'], true)) {
throw new InvalidArgumentException('Invalid landed-cost worker mode.');
}
(require __DIR__.'/../../database/qa-migrations/2026_11_22_000009_create_budget_landed_costs.php')->up();
foreach (['same_operation', 'conflicting_plan_version', 'same_source_different_plan'] as $scenario) {
$prepare = new Process([PHP_BINARY, __FILE__, $database, 'prepare'], dirname(__DIR__, 2), timeout: 30);
$prepareMode = match ($argv[2] ?? '') {
'race-estimate-reconcile' => 'prepare-reconcile', 'race-estimate' => 'prepare-estimate', default => 'prepare'
};
$prepare = new Process([PHP_BINARY, __FILE__, $database, $prepareMode], dirname(__DIR__, 2), timeout: 30);
$prepare->mustRun();
$input = json_decode($prepare->getOutput(), true, 32, JSON_THROW_ON_ERROR);
$workers = [];
@@ -88,7 +117,7 @@ foreach (['same_operation', 'conflicting_plan_version', 'same_source_different_p
if ($index === 1 && $scenario !== 'same_operation') {
$contender['operation'] = (string) Str::uuid();
if ($scenario === 'same_source_different_plan') {
$contender['plan'] = (string) Str::uuid();
$contender[($input['reconciliation'] ?? false) ? 'actual_plan' : 'plan'] = (string) Str::uuid();
} else {
$contender['version'] = 2;
}
@@ -121,13 +150,13 @@ foreach (['same_operation', 'conflicting_plan_version', 'same_source_different_p
$statuses = array_column($results, 'status');
sort($statuses);
$commands = $db->table('budget_landed_commands')->where('tenant_id', $input['tenant'])->count();
$audits = $db->table('audit_logs')->where('tenant_id', $input['tenant'])->where('action', 'budget.landed_reserve')->count();
$audits = $db->table('audit_logs')->where('tenant_id', $input['tenant'])->where('action', ($input['reconciliation'] ?? false) ? 'budget.landed_estimate_reconcile' : (($input['estimate'] ?? false) ? 'budget.landed_estimate_reserve' : 'budget.landed_reserve'))->count();
$reserved = (int) $db->table('budget_landed_entries')->where('tenant_id', $input['tenant'])->sum('reserved_minor');
if ($statuses !== ($scenario === 'same_operation' ? [200, 200] : [200, 409]) || $commands !== 1 || $audits !== 1 || $reserved !== 2000
if ($statuses !== ($scenario === 'same_operation' ? [200, 200] : [200, 409]) || $commands !== (($input['reconciliation'] ?? false) ? 3 : 1) || $audits !== 1 || $reserved !== (($input['reconciliation'] ?? false) ? 3000 : 2000)
|| ($scenario === 'same_operation' && count(array_filter($results, fn ($result) => $result['replayed'])) !== 1)) {
throw new RuntimeException('Contention must retain exactly one source, budget effect and audit.');
}
echo json_encode(['scenario' => $scenario, 'statuses' => $statuses, 'blocked_writers_observed' => $blocked,
echo json_encode(['scenario' => $scenario, 'estimate' => $input['estimate'] ?? false, 'reconciliation' => $input['reconciliation'] ?? false, 'statuses' => $statuses, 'blocked_writers_observed' => $blocked,
'commands' => $commands, 'audits' => $audits, 'reserved_minor' => $reserved], JSON_THROW_ON_ERROR).PHP_EOL;
if ($scenario === 'same_operation') {
$artifact = json_decode(file_get_contents(base_path('docs/proofs/rq01-distribution-artifact.json')), true, 32, JSON_THROW_ON_ERROR);
@@ -149,6 +178,19 @@ foreach (['same_operation', 'conflicting_plan_version', 'same_source_different_p
throw new RuntimeException('The earlier whole-command runtime must refuse the new per-origin data.');
}
echo $whole->getOutput();
if ($input['estimate'] ?? false) {
$prior = base_path('storage/framework/testing/budget-import-estimate-prior-qa-20261127');
if (hash_file('sha256', $prior.'/src/BudgetControl.php') !== 'd81f8fb172683727c221395224cecf9acb8285ab94e71e914141d41cf9e79efa') {
throw new RuntimeException('Preserve the private prior runtime used for estimate downgrade verification.');
}
$oldEstimate = new Process([PHP_BINARY, __FILE__, $database, 'legacy-estimate', (string) $input['actor']], dirname(__DIR__, 2), ['COMMON_BUDGET_PATH' => $prior], timeout: 30);
$oldEstimate->mustRun();
$receipt = json_decode($oldEstimate->getOutput(), true, 32, JSON_THROW_ON_ERROR);
if (($receipt['old_estimate_runtime_rejected'] ?? false) !== true || ($receipt['runtime_landed_version'] ?? null) !== CommonBudget::LANDED_COSTS_CONTRACT) {
throw new RuntimeException('The prior per-origin runtime must refuse estimate effects before balance access.');
}
echo $oldEstimate->getOutput();
}
}
} finally {
if ($db->transactionLevel() > 0) {