This commit is contained in:
2026-11-02 20:27:11 +02:00
parent b44378b899
commit badaada043
300 changed files with 17458 additions and 1056 deletions
+40
View File
@@ -0,0 +1,40 @@
<?php
use Tempo\Integration\WarehouseInbox;
require __DIR__.'/../Support/InboxFixture.php';
if (getenv('INBOX_TEST_PG') !== '1') {
throw new LogicException('Executor limitado à prova PostgreSQL descartável.');
}
$mode = $argv[1] ?? '';
if ($mode === 'prepare') {
$db = inboxDatabase();
$inbox = new WarehouseInbox($db, 'synthetic-sgc-a', 7, 99, 'reader');
$inbox->accept(inboxPage([], 'cursor-start'), 0, null);
echo json_encode(['checkpoint' => $inbox->checkpoint()], JSON_THROW_ON_ERROR);
exit;
}
$db = inboxConnection();
$inbox = new WarehouseInbox($db, 'synthetic-sgc-a', 7, 99, 'reader');
if ($mode === 'hold') {
$db->beginTransaction();
$db->query('SELECT version FROM sgc_exchange_streams FOR UPDATE')->fetchColumn();
echo 'locked';
flush();
usleep(1500000);
$db->commit();
}
if (! in_array($mode, ['hold', 'run', 'exit-after-commit'], true)) {
throw new LogicException('Modo inválido.');
}
$id = (int) ($argv[2] ?? 1);
try {
$result = $inbox->accept(inboxPage([inboxEvent($id)], 'cursor-'.$id), 1, 'cursor-start');
if ($mode === 'exit-after-commit') {
exit(42); // committed, acknowledgement intentionally lost
}
echo json_encode(['result' => 'accepted', ...$result], JSON_THROW_ON_ERROR);
} catch (RuntimeException $e) {
echo json_encode(['result' => 'conflict'], JSON_THROW_ON_ERROR);
}
+57
View File
@@ -0,0 +1,57 @@
<?php
use App\Models\User;
use App\Services\AutomaticPurchases;
use App\Services\ReplenishmentQueue;
use App\Services\StockWorkflow;
use App\Services\SupplyPlanning;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
require dirname(__DIR__, 2).'/vendor/autoload.php';
$app = require dirname(__DIR__, 2).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (! app()->environment('testing') || DB::getDriverName() !== 'pgsql' || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', DB::connection()->getDatabaseName())) {
throw new LogicException('Apenas base descartável.');
}
require_once dirname(__DIR__).'/Support/SuggestionFixture.php';
if (($argv[1] ?? '') === 'prepare') {
$f = suggestionFixture();
app(SupplyPlanning::class)->save($f['receiver'], 'policy', supplyInput([], 'suspend', $f['policy']));
auth()->guard()->setUser($f['issuer']);
$policy = app(SupplyPlanning::class)->save($f['issuer'], 'policy', supplyInput([...supplyPolicy($f), 'purpose' => 'internal', 'agreement_key' => $f['agreement']->record_key]));
auth()->guard()->setUser($f['receiver']);
$policy = app(SupplyPlanning::class)->save($f['receiver'], 'policy', supplyInput([], 'approve', $policy));
auth()->guard()->setUser($f['issuer']);
$rule = app(AutomaticPurchases::class)->save($f['issuer'], ['operation_key' => (string) Str::uuid(), 'version' => 0, 'action' => 'save', 'reason' => 'Configuração da prova de recuperação isolada.', 'data' => ['policy_key' => $policy->record_key, 'requester_id' => $f['issuer']->id, 'cost_center_id' => $f['r']->cost_center_id, 'unit_price' => null, 'tax_rate' => null, 'needed_days' => 5, 'valid_until' => today()->addMonth()->toDateString()]]);
auth()->guard()->setUser($f['receiver']);
app(AutomaticPurchases::class)->save($f['receiver'], ['operation_key' => (string) Str::uuid(), 'record_key' => $rule->record_key, 'version' => 1, 'action' => 'approve', 'reason' => 'Activação independente da regra de ensaio.', 'data' => []]);
// Controlled abrupt exit at the start of the post-commit callback. Enqueue remains real.
app()->instance(ReplenishmentQueue::class, new class extends ReplenishmentQueue
{
public function interactive(User $actor, int $id): bool
{
$work = DB::table('replenishment_work')->where('id', $id)->sole();
echo json_encode(['work' => $work->id, 'tenant' => $work->tenant_id, 'stock' => $work->stock_item_id, 'warehouse' => $work->warehouse_id], JSON_THROW_ON_ERROR);
exit;
}
});
app(StockWorkflow::class)->record($f['receiver'], ['kind' => 'issue', 'reference' => 'CRASH-'.Str::uuid(), 'idempotency_key' => (string) Str::uuid(), 'responsible_id' => $f['issuer']->id, 'warehouse_id' => $f['warehouse']->id, 'stock_item_id' => $f['stock']->id, 'quantity' => '120.000', 'destination' => 'Oficina interna', 'reason' => 'Saída confirmada antes da interrupção real do processo.']);
throw new RuntimeException('A interrupção devia terminar o processo.');
}
$data = json_decode($argv[2], true, 32, JSON_THROW_ON_ERROR);
if (($argv[1] ?? '') === 'hold') {
$held = false;
DB::listen(function (QueryExecuted $query) use (&$held) {
if (! $held && str_contains($query->sql, '"tenants"') && str_contains(strtolower($query->sql), 'for update')) {
$held = true;
echo "locked\n";
flush();
usleep(2000000);
}
});
}
$ok = app(ReplenishmentQueue::class)->scheduled($data['work']);
echo json_encode(['result' => $ok ? 'saved' : 'failed', 'authenticated' => auth()->check(), 'drafts' => DB::table('requisitions')->where('tenant_id', $data['tenant'])->where('status', 'draft')->pluck('id')->all()], JSON_THROW_ON_ERROR);
@@ -0,0 +1,48 @@
<?php
use App\Models\User;
use App\Services\AutomaticPurchases;
use App\Services\StockWorkflow;
use App\Services\SupplyPlanning;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
require dirname(__DIR__, 2).'/vendor/autoload.php';
$app = require dirname(__DIR__, 2).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (! app()->environment('testing') || DB::getDriverName() !== 'pgsql' || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', DB::connection()->getDatabaseName())) {
throw new LogicException('Apenas base descartável.');
}
require_once dirname(__DIR__).'/Support/SuggestionFixture.php';
if (($argv[1] ?? '') === 'prepare') {
$f = suggestionFixture();
app(SupplyPlanning::class)->save($f['receiver'], 'policy', supplyInput([], 'suspend', $f['policy']));
auth()->guard()->setUser($f['issuer']);
$policy = app(SupplyPlanning::class)->save($f['issuer'], 'policy', supplyInput([...supplyPolicy($f), 'purpose' => 'internal', 'agreement_key' => $f['agreement']->record_key]));
auth()->guard()->setUser($f['receiver']);
$policy = app(SupplyPlanning::class)->save($f['receiver'], 'policy', supplyInput([], 'approve', $policy));
app(StockWorkflow::class)->record($f['receiver'], ['kind' => 'issue', 'reference' => 'EMPTY-'.Str::uuid(), 'idempotency_key' => (string) Str::uuid(), 'responsible_id' => $f['issuer']->id, 'warehouse_id' => $f['warehouse']->id, 'stock_item_id' => $f['stock']->id, 'quantity' => '120.000', 'destination' => 'Oficina interna', 'reason' => 'Consumo efectivo antes da configuração de reposição.']);
auth()->guard()->setUser($f['issuer']);
$rule = app(AutomaticPurchases::class)->save($f['issuer'], ['operation_key' => (string) Str::uuid(), 'version' => 0, 'action' => 'save', 'reason' => 'Configuração fictícia da prova concorrente.', 'data' => ['policy_key' => $policy->record_key, 'requester_id' => $f['issuer']->id, 'cost_center_id' => $f['r']->cost_center_id, 'unit_price' => null, 'tax_rate' => null, 'needed_days' => 5, 'valid_until' => today()->addMonth()->toDateString()]]);
echo json_encode(['user' => $f['receiver']->id, 'tenant' => $f['tenant']->id, 'stock' => $f['stock']->id, 'warehouse' => $f['warehouse']->id, 'input' => ['operation_key' => (string) Str::uuid(), 'record_key' => $rule->record_key, 'version' => 1, 'action' => 'approve', 'reason' => 'Activação concorrente da parametrização testada.', 'data' => []]], JSON_THROW_ON_ERROR);
exit;
}
$data = json_decode($argv[2], true, 32, JSON_THROW_ON_ERROR);
$actor = User::query()->withoutGlobalScopes()->findOrFail($data['user']);
auth()->guard()->setUser($actor);
if (($argv[1] ?? '') === 'hold') {
$held = false;
DB::listen(function (QueryExecuted $query) use (&$held) {
if (! $held && str_contains($query->sql, '"tenants"') && str_contains(strtolower($query->sql), 'for update')) {
$held = true;
echo "locked\n";
flush();
usleep(2000000);
}
});
}
$rule = app(AutomaticPurchases::class)->save($actor, $data['input']);
$need = app(SupplyPlanning::class)->latest('auto_need')->where('stock_item_id', $data['stock'])->where('warehouse_id', $data['warehouse'])->sole();
echo json_encode(['result' => 'saved', 'rule' => $rule->id, 'purchases' => $need->data['purchases']], JSON_THROW_ON_ERROR);