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
+36 -14
View File
@@ -23,6 +23,7 @@ if (! $app->environment('testing') || DB::getDriverName() !== 'pgsql' || DB::con
require_once __DIR__.'/../Support/ReceiptFixture.php';
require_once __DIR__.'/../Support/BudgetDistributionSchema.php';
require_once __DIR__.'/../Support/BudgetOrderPartsFixture.php';
$amendmentMode = in_array($argv[2] ?? '', ['prepare-amend', 'write-amend', 'race-amend'], true);
if (($argv[2] ?? '') === 'legacy') {
$actor = User::query()->findOrFail((int) ($argv[3] ?? 0));
auth()->guard()->setUser($actor);
@@ -37,7 +38,7 @@ if (($argv[2] ?? '') === 'legacy') {
}
exit;
}
if (($argv[2] ?? '') === 'prepare') {
if (in_array($argv[2] ?? '', ['prepare', 'prepare-amend'], true)) {
$f = receiptFixture();
$source = unissuedOrderPartsSource($f, 10000);
$line = $source->items()->sole();
@@ -52,20 +53,36 @@ if (($argv[2] ?? '') === 'prepare') {
$engine = app(CommonBudget::class)->for($f['issuer']);
$lines = [$line->id => ['subtotal_minor' => 10000, 'total_minor' => 10000, 'required_kind' => 'operating']];
$plan = [['allocation_key' => 'synthetic', 'line_id' => $line->id, 'centre_id' => $f['r']->cost_center_id, 'subtotal_minor' => 10000, 'total_minor' => 10000]];
if ($amendmentMode) {
$plan = [[...$plan[0], 'allocation_key' => 'a', 'subtotal_minor' => 5000, 'total_minor' => 5000],
[...$plan[0], 'allocation_key' => 'b', 'subtotal_minor' => 5000, 'total_minor' => 5000]];
}
$engine->recordDistribution($source->id, 'reserve', $plan, $lines, str_repeat('a', 64), (string) Str::uuid());
$award = $engine->recordDistribution($source->id, 'award', $plan, $lines, str_repeat('b', 64), (string) Str::uuid());
echo json_encode(['tenant' => $f['tenant']->id, 'actor' => $f['issuer']->id, 'budget' => $budget, 'source' => $award['distribution_id'],
$input = ['tenant' => $f['tenant']->id, 'actor' => $f['issuer']->id, 'budget' => $budget, 'source' => $award['distribution_id'],
'order' => (string) Str::uuid(), 'operation' => (string) Str::uuid(), 'sources' => [['origin' => $source->id, 'version' => 0,
'plan_hash' => $award['plan_hash'], 'allocations' => [['allocation_key' => 'synthetic', 'subtotal_minor' => 4000, 'total_minor' => 4000]]]]], JSON_THROW_ON_ERROR);
'plan_hash' => $award['plan_hash'], 'allocations' => [['allocation_key' => 'synthetic', 'subtotal_minor' => 4000, 'total_minor' => 4000]]]]];
if ($amendmentMode) {
$input['sources'][0]['allocations'] = [['allocation_key' => 'a', 'subtotal_minor' => 2000, 'total_minor' => 2000],
['allocation_key' => 'b', 'subtotal_minor' => 2000, 'total_minor' => 2000]];
$engine->commitOrderParts($input['order'], $input['sources'], (string) Str::uuid());
$input['actor'] = $finance->id;
$input['sources'][0]['version'] = 1;
$input['sources'][0]['allocations'][0]['subtotal_minor'] = $input['sources'][0]['allocations'][0]['total_minor'] = 1000;
$input['sources'][0]['allocations'][1]['subtotal_minor'] = $input['sources'][0]['allocations'][1]['total_minor'] = 3000;
}
echo json_encode($input, JSON_THROW_ON_ERROR);
exit;
}
if (($argv[2] ?? '') === 'write') {
if (in_array($argv[2] ?? '', ['write', 'write-amend'], true)) {
$input = json_decode($argv[3], true, 32, JSON_THROW_ON_ERROR);
DB::select('select set_config(?, ?, false)', ['application_name', 'sgc_order_parts_write_'.getmypid()]);
$actor = User::query()->findOrFail($input['actor']);
auth()->guard()->setUser($actor);
try {
$result = app(CommonBudget::class)->for($actor)->commitOrderParts($input['order'], $input['sources'], $input['operation']);
$engine = app(CommonBudget::class)->for($actor);
$result = $amendmentMode ? $engine->amendOrderParts($input['order'], $input['sources'], 'Explicit synthetic concurrent Finance amendment.', $input['operation'])
: $engine->commitOrderParts($input['order'], $input['sources'], $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);
@@ -74,13 +91,13 @@ if (($argv[2] ?? '') === 'write') {
}
exit;
}
if (($argv[2] ?? '') !== 'race') {
if (! in_array($argv[2] ?? '', ['race', 'race-amend'], true)) {
throw new InvalidArgumentException('Invalid order-part worker mode.');
}
budgetDistributionFixtureSchema();
(require __DIR__.'/../../database/qa-migrations/2026_11_22_000007_create_budget_order_parts.php')->up();
foreach (['same_operation', 'conflicting_version'] as $scenario) {
$prepare = new Process([PHP_BINARY, __FILE__, $database, 'prepare'], dirname(__DIR__, 2), timeout: 30);
$prepare = new Process([PHP_BINARY, __FILE__, $database, $amendmentMode ? 'prepare-amend' : 'prepare'], dirname(__DIR__, 2), timeout: 30);
$prepare->mustRun();
$input = json_decode($prepare->getOutput(), true, 32, JSON_THROW_ON_ERROR);
$workers = [];
@@ -91,11 +108,16 @@ foreach (['same_operation', 'conflicting_version'] as $scenario) {
for ($index = 0; $index < 2; $index++) {
if ($index === 1 && $scenario === 'conflicting_version') {
$input['operation'] = (string) Str::uuid();
$input['order'] = (string) Str::uuid();
$input['sources'][0]['allocations'][0]['subtotal_minor'] = 6000;
$input['sources'][0]['allocations'][0]['total_minor'] = 6000;
if ($amendmentMode) {
$input['sources'][0]['allocations'][0]['subtotal_minor'] = $input['sources'][0]['allocations'][0]['total_minor'] = 1500;
$input['sources'][0]['allocations'][1]['subtotal_minor'] = $input['sources'][0]['allocations'][1]['total_minor'] = 2500;
} else {
$input['order'] = (string) Str::uuid();
$input['sources'][0]['allocations'][0]['subtotal_minor'] = 6000;
$input['sources'][0]['allocations'][0]['total_minor'] = 6000;
}
}
$worker = new Process([PHP_BINARY, __FILE__, $database, 'write', json_encode($input, JSON_THROW_ON_ERROR)], dirname(__DIR__, 2), timeout: 30);
$worker = new Process([PHP_BINARY, __FILE__, $database, $amendmentMode ? 'write-amend' : 'write', json_encode($input, JSON_THROW_ON_ERROR)], dirname(__DIR__, 2), timeout: 30);
$workers[] = $worker;
$worker->start();
}
@@ -125,14 +147,14 @@ foreach (['same_operation', 'conflicting_version'] as $scenario) {
$entries = $db->table('budget_distribution_entries')->where('distribution_id', $input['source']);
$reserved = (int) (clone $entries)->sum('reserved_minor');
$committed = (int) (clone $entries)->sum('committed_minor');
$commands = $db->table('budget_order_commands')->where('tenant_id', $input['tenant'])->count();
$audits = $db->table('audit_logs')->where('tenant_id', $input['tenant'])->where('action', 'budget.orders_committed')->count();
$commands = $db->table('budget_order_commands')->where('tenant_id', $input['tenant'])->where('kind', $amendmentMode ? 'amend' : 'commit')->count();
$audits = $db->table('audit_logs')->where('tenant_id', $input['tenant'])->where('action', $amendmentMode ? 'budget.order_parts_amended' : 'budget.orders_committed')->count();
if ($statuses !== ($scenario === 'same_operation' ? [200, 200] : [200, 409]) || $commands !== 1 || $audits !== 1
|| $reserved + $committed !== 10000 || ! in_array($committed, [4000, 6000], true)
|| ($scenario === 'same_operation' && count(array_filter($results, fn ($result) => $result['replayed'])) !== 1)) {
throw new RuntimeException('Contention did not preserve one command, one audit and the exact original budget.');
}
echo json_encode(['scenario' => $scenario, 'statuses' => $statuses, 'blocked_writers_observed' => $blocked,
echo json_encode(['scenario' => $scenario, 'amendment' => $amendmentMode, 'statuses' => $statuses, 'blocked_writers_observed' => $blocked,
'commands' => $commands, 'audits' => $audits, 'reserved_minor' => $reserved, 'committed_minor' => $committed], JSON_THROW_ON_ERROR).PHP_EOL;
if ($scenario === 'same_operation') {
$proof = json_decode(file_get_contents(base_path('docs/proofs/rq01-distribution-artifact.json')), true, 32, JSON_THROW_ON_ERROR);