commit de armazens
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-04 09:40:26 +02:00
parent e753f7a0a4
commit 63d20b10fc
245 changed files with 10847 additions and 230 deletions
+140
View File
@@ -11,15 +11,155 @@ use App\Models\StockMovement;
use App\Models\StockOperation;
use App\Models\TechnicalReview;
use App\Models\User;
use App\Models\WarehouseRequest;
use App\Services\EquipmentWorkflow;
use App\Services\ReceiptWorkflow;
use App\Services\RequisitionWorkflow;
use App\Services\StockWorkflow;
use App\Services\WarehouseFulfilment;
use App\Services\WarehouseRequests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Inertia\Testing\AssertableInertia as Assert;
require_once __DIR__.'/../Support/ReceiptFixture.php';
require_once __DIR__.'/../Support/SupplyFixture.php';
function equipmentWarehouseDraft(array $f, bool $mixed = false): WarehouseRequest
{
return app(WarehouseRequests::class)->draft($f['receiver'], ['operation_key' => (string) Str::uuid(), 'warehouse_id' => $f['warehouse']->id,
'cost_center_id' => $f['r']->cost_center_id, 'destination' => 'Destino do equipamento', 'reason' => 'Manutenção interna com material já registado.', 'priority' => 'normal',
'lines' => [['stock_item_id' => $f['stock']->id, 'quantity' => '0.100', 'use' => 'equipment', 'equipment_id' => $f['asset']->id],
...($mixed ? [['stock_item_id' => $f['stock']->id, 'quantity' => '9.000', 'use' => 'internal', 'equipment_id' => null]] : [])]]);
}
test('equipment warehouse requests use native scope and expose only matching lines without mutations', function () {
$this->withoutVite();
supplyGrant($this->f['receiver'], ['warehouse', 'warehouse_request_prepare']);
$record = equipmentWarehouseDraft($this->f, true);
$before = [];
foreach (['warehouse_requests', 'warehouse_request_events', 'warehouse_reservations', 'stock_operations', 'stock_movements', 'audit_logs'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toArray();
}
$this->get('/equipamentos/'.$this->f['asset']->id)->assertOk()->assertInertia(fn (Assert $p) => $p
->where('warehouseRequests.total', 1)->where('warehouseRequests.data.0.id', $record->id)->where('warehouseRequests.data.0.state', 'draft')
->has('warehouseRequests.data.0.lines', 1)->where('warehouseRequests.data.0.lines.0.requested_milli', 100)
->where('warehouseRequests.data.0.lines.0.delivered_milli', 0)->where('warehouseRequests.data.0.lines.0.remaining_milli', 100)
->missing('warehouseRequests.data.0.snapshot')->missing('warehouseRequests.data.0.events'));
foreach ($before as $table => $rows) {
expect(DB::table($table)->orderBy('id')->get()->toArray())->toEqual($rows);
}
supplyGrant($this->f['finance'], ['finance']);
$this->actingAs($this->f['finance'])->get('/equipamentos/'.$this->f['asset']->id)->assertOk()
->assertInertia(fn (Assert $p) => $p->where('warehouseRequests', null));
supplyGrant($this->f['issuer'], ['procurement', 'warehouse_request_prepare']);
$this->actingAs($this->f['issuer'])->get('/equipamentos/'.$this->f['asset']->id)->assertOk()
->assertInertia(fn (Assert $p) => $p->where('warehouseRequests.total', 0));
});
test('equipment warehouse requests paginate old pending documents independently', function () {
$this->withoutVite();
supplyGrant($this->f['receiver'], ['warehouse', 'warehouse_request_prepare']);
foreach (range(1, 16) as $index) {
$last = equipmentWarehouseDraft($this->f);
}
$url = '/equipamentos/'.$this->f['asset']->id.'?requests_page=2&purchases_page=1&movements_page=1';
$response = $this->get($url)->assertOk()->assertInertia(fn (Assert $p) => $p->where('warehouseRequests.total', 16)
->has('warehouseRequests.data', 1)->where('warehouseRequests.data.0.id', $last->id)->where('purchases.current_page', 1)->where('movements.current_page', 1));
expect($response->inertiaProps('warehouseRequests.prev_page_url'))->toContain('purchases_page=1', 'movements_page=1');
$this->getJson('/equipamentos/'.$this->f['asset']->id.'?requests_page=0')->assertUnprocessable();
});
test('equipment warehouse requests retain department and tenant equipment boundaries', function () {
$this->withoutVite();
supplyGrant($this->f['receiver'], ['warehouse', 'warehouse_request_prepare']);
equipmentWarehouseDraft($this->f);
supplyGrant($this->f['head'], ['department_head', 'warehouse_request_review']);
$this->actingAs($this->f['head'])->get('/equipamentos/'.$this->f['asset']->id)->assertOk()
->assertInertia(fn (Assert $p) => $p->where('warehouseRequests.total', 1));
$department = Department::query()->create(['tenant_id' => $this->f['tenant']->id, 'name' => 'Outra área']);
$this->f['asset']->update(['department_id' => $department->id]);
$this->get('/equipamentos/'.$this->f['asset']->id)->assertNotFound();
$foreign = receiptFixture();
$this->actingAs($foreign['receiver'])->get('/equipamentos/'.$this->f['asset']->id)->assertNotFound();
});
test('equipment integration block reuses the stock operation produced by an authorized internal warehouse delivery', function () {
$this->withoutVite();
supplyGrant($this->f['issuer'], ['warehouse_request_prepare']);
supplyGrant($this->f['head'], ['warehouse_request_review', 'warehouse_request_read']);
supplyGrant($this->f['receiver'], ['warehouse', 'warehouse_request_deliver']);
$this->actingAs($this->f['receiver']);
app(ReceiptWorkflow::class)->confirm($this->f['receiver'], $this->f['order'], receiptPayload($this->f));
$this->actingAs($this->f['issuer']);
$request = app(WarehouseRequests::class)->draft($this->f['issuer'], ['operation_key' => (string) Str::uuid(), 'warehouse_id' => $this->f['warehouse']->id,
'cost_center_id' => $this->f['r']->cost_center_id, 'destination' => 'Linha do equipamento', 'reason' => 'Peça para manutenção interna do equipamento.', 'priority' => 'normal',
'lines' => [['stock_item_id' => $this->f['stock']->id, 'quantity' => '0.100', 'use' => 'equipment', 'equipment_id' => $this->f['asset']->id]]]);
$flow = app(WarehouseFulfilment::class);
foreach ([[$this->f['issuer'], 'submit'], [$this->f['head'], 'authorize'], [$this->f['receiver'], 'deliver']] as [$actor, $action]) {
$this->actingAs($actor);
$flow->command($actor, $request->id, ['operation_key' => (string) Str::uuid(), 'version' => $flow->progress($request)['version'], 'action' => $action,
'reason' => 'Intervenção nativa do percurso interno de teste.', ...($action === 'deliver' ? ['lines' => [['index' => 0, 'quantity' => '0.100']]] : [])]);
}
$operation = StockOperation::query()->where('equipment_id', $this->f['asset']->id)->sole();
$before = DB::table('stock_movements')->count();
$this->get('/equipamentos/'.$this->f['asset']->id)->assertOk()->assertInertia(fn (Assert $p) => $p->where('movements.total', 1)
->where('movements.data.0.id', $operation->id)->where('movements.data.0.quantity_milli', 100)
->where('warehouseRequests.total', 1)->where('warehouseRequests.data.0.state', 'delivered')
->where('warehouseRequests.data.0.lines.0.delivered_milli', 100)->where('warehouseRequests.data.0.lines.0.remaining_milli', 0));
expect(DB::table('stock_movements')->count())->toBe($before)->and($flow->progress($request)['state'])->toBe('delivered');
});
test('equipment integration block separates native purchase requests from actual equipment stock issues without mutations', function () {
$this->withoutVite();
$request = app(RequisitionWorkflow::class)->save($this->f['receiver'], equipmentRequestData($this->f, 'spare_part'));
app(ReceiptWorkflow::class)->confirm($this->f['receiver'], $this->f['order'], receiptPayload($this->f));
$operation = app(StockWorkflow::class)->record($this->f['receiver'], ['kind' => 'issue', 'reference' => 'EQ-ISSUE', 'idempotency_key' => (string) Str::uuid(),
'responsible_id' => $this->f['receiver']->id, 'warehouse_id' => $this->f['warehouse']->id, 'stock_item_id' => $this->f['stock']->id, 'equipment_id' => $this->f['asset']->id,
'quantity' => '0.100', 'destination' => 'Equipamento de teste', 'reason' => 'Saída física nativa para o equipamento.']);
$before = [];
foreach (['audit_logs', 'requisitions', 'requisition_items', 'approval_steps', 'stock_movements', 'stock_operations', 'equipment'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toArray();
}
$this->get('/equipamentos/'.$this->f['asset']->id)->assertOk()->assertHeader('Cache-Control', 'max-age=0, no-store, private')
->assertInertia(fn (Assert $p) => $p->component('equipment/show')->where('equipment.code', 'EQ-TEST')->where('purchases.total', 1)
->where('purchases.data.0.id', $request->id)->where('purchases.data.0.items.0.quantity', '1.000')->missing('purchases.data.0.items.0.unit_price_minor')
->where('movements.total', 1)->where('movements.data.0.id', $operation->id)->where('movements.data.0.quantity_milli', 100)->missing('movements.data.0.snapshot'));
foreach ($before as $table => $rows) {
expect(DB::table($table)->orderBy('id')->get()->toArray())->toEqual($rows);
}
});
test('equipment integration block preserves department and native requisition visibility with tenant isolation', function () {
$this->withoutVite();
$request = app(RequisitionWorkflow::class)->save($this->f['receiver'], equipmentRequestData($this->f));
$this->actingAs($this->f['finance'])->get('/equipamentos/'.$this->f['asset']->id)->assertOk()->assertInertia(fn (Assert $p) => $p->where('purchases.total', 1));
$otherDepartment = Department::query()->create(['tenant_id' => $this->f['tenant']->id, 'name' => 'Outra área']);
$request->update(['department_id' => $otherDepartment->id]);
$this->actingAs($this->f['head'])->get('/equipamentos/'.$this->f['asset']->id)->assertOk()->assertInertia(fn (Assert $p) => $p->where('purchases.total', 0));
$this->f['asset']->update(['department_id' => $otherDepartment->id]);
$this->get('/equipamentos/'.$this->f['asset']->id)->assertNotFound();
$foreign = receiptFixture();
$this->actingAs($foreign['receiver'])->get('/equipamentos/'.$this->f['asset']->id)->assertNotFound();
$user = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'purchasing_role' => PurchasingRole::Requester]);
$this->actingAs($user)->get('/equipamentos/'.$this->f['asset']->id)->assertForbidden();
});
test('equipment integration block keeps old purchase and movement pages independently accessible', function () {
$this->withoutVite();
app(ReceiptWorkflow::class)->confirm($this->f['receiver'], $this->f['order'], receiptPayload($this->f));
foreach (range(1, 16) as $index) {
app(RequisitionWorkflow::class)->save($this->f['receiver'], equipmentRequestData($this->f));
app(StockWorkflow::class)->record($this->f['receiver'], ['kind' => 'issue', 'reference' => 'EQ-PAGE-'.$index, 'idempotency_key' => (string) Str::uuid(),
'responsible_id' => $this->f['receiver']->id, 'warehouse_id' => $this->f['warehouse']->id, 'stock_item_id' => $this->f['stock']->id, 'equipment_id' => $this->f['asset']->id,
'quantity' => '0.001', 'reason' => 'Saída física nativa para paginação.']);
}
$this->get('/equipamentos/'.$this->f['asset']->id.'?purchases_page=2&movements_page=1')->assertOk()
->assertInertia(fn (Assert $p) => $p->where('purchases.total', 16)->has('purchases.data', 1)->where('purchases.current_page', 2)->has('movements.data', 15)->where('movements.current_page', 1));
$this->get('/equipamentos/'.$this->f['asset']->id.'?purchases_page=1&movements_page=2')->assertOk()
->assertInertia(fn (Assert $p) => $p->has('purchases.data', 15)->has('movements.data', 1)->where('movements.data.0.reference', 'EQ-PAGE-16'));
});
function equipmentRequestData(array $f, string $type = 'equipment'): array
{