true]); $this->withoutVite(); $this->f = receiptFixture(); $this->actingAs($this->f['issuer']); $this->input = ['operation_key' => (string) Str::uuid(), 'record_key' => null, 'version' => 0, 'type' => 'project', 'code' => 'PRJ-QA', 'name' => 'Projecto estritamente sintético', 'department_id' => $this->f['department']->id, 'equipment_id' => null, 'reference' => 'Documento fictício para cadastro independente de Compras', 'state' => 'active', 'reason' => 'Preparação de referência sintética para testes de compras']; }); test('own project register versions preserves identity and replay without financial or stock effects', function () { $service = app(CapitalRegister::class); $before = []; foreach (['budget_entries', 'stock_movements', 'invoices', 'purchase_orders'] as $table) { $before[$table] = DB::table($table)->orderBy('id')->get()->toJson(); } $first = $service->save($this->f['issuer'], $this->input); expect($first->version)->toBe(1)->and($first->snapshot['source_system'])->toBe('compras'); expect($service->save($this->f['issuer'], $this->input)->id)->toBe($first->id); $archive = [...$this->input, 'operation_key' => (string) Str::uuid(), 'record_key' => $first->record_key, 'version' => 1, 'state' => 'archived']; $second = $service->save($this->f['issuer'], $archive); expect($second->version)->toBe(2)->and($second->state)->toBe('archived') ->and($first->fresh()->state)->toBe('active')->and(SupplyRecord::query()->where('kind', 'capital_register')->count())->toBe(2); foreach ($before as $table => $rows) { expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($rows); } expect(fn () => $service->save($this->f['issuer'], [...$archive, 'operation_key' => (string) Str::uuid()]))->toThrow(HttpException::class); expect(fn () => $service->save($this->f['issuer'], [...$this->input, 'operation_key' => (string) Str::uuid()]))->toThrow(ValidationException::class); }); test('register rejects reused operation content and renaming a permanent code', function () { $service = app(CapitalRegister::class); $first = $service->save($this->f['issuer'], $this->input); expect(fn () => $service->save($this->f['issuer'], [...$this->input, 'name' => 'Conteúdo diferente']))->toThrow(HttpException::class); expect(fn () => $service->save($this->f['issuer'], [...$this->input, 'operation_key' => (string) Str::uuid(), 'record_key' => $first->record_key, 'version' => 1, 'code' => 'OUTRO']))->toThrow(ValidationException::class); expect(SupplyRecord::query()->where('kind', 'capital_register')->count())->toBe(1); }); test('asset references reuse a physical equipment identity and reject duplicate or removed links', function () { $level = CriticalityLevel::query()->create(['tenant_id' => $this->f['tenant']->id, 'created_by' => $this->f['issuer']->id, 'created_at' => now(), 'code' => 'QA', 'name' => 'Ensaio', 'criteria' => 'Só fixture sintética', 'is_critical' => false]); $equipment = Equipment::query()->create(['tenant_id' => $this->f['tenant']->id, 'created_by' => $this->f['issuer']->id, 'code' => 'EQ-QA', 'description' => 'Equipamento físico sintético', 'area' => 'Área sintética', 'status' => 'active', 'department_id' => $this->f['department']->id, 'criticality_level_id' => $level->id]); $input = [...$this->input, 'type' => 'asset', 'code' => 'ASSET-QA', 'equipment_id' => $equipment->id]; $service = app(CapitalRegister::class); $first = $service->save($this->f['issuer'], $input); expect($first->snapshot['equipment']['id'])->toBe($equipment->id)->and(Equipment::query()->count())->toBe(1); expect(fn () => $service->save($this->f['issuer'], [...$input, 'operation_key' => (string) Str::uuid(), 'code' => 'OTHER-QA']))->toThrow(ValidationException::class); expect(fn () => $service->save($this->f['issuer'], [...$input, 'operation_key' => (string) Str::uuid(), 'record_key' => $first->record_key, 'version' => 1, 'equipment_id' => null]))->toThrow(ValidationException::class); $this->get('/projectos-imobilizado?history='.$first->record_key)->assertOk()->assertInertia(fn (Assert $page) => $page->has('history.data', 1)); expect(fn () => $service->save($this->f['issuer'], [...$input, 'operation_key' => (string) Str::uuid(), 'code' => ['invalid']]))->toThrow(ValidationException::class); }); test('native register page is encrypted private paginated and gated off outside QA', function () { app(CapitalRegister::class)->save($this->f['issuer'], $this->input); $this->get('/projectos-imobilizado')->assertOk()->assertHeader('Cache-Control', 'max-age=0, no-store, private') ->assertViewHas('page', fn (array $page) => $page['encryptHistory'] === true) ->assertInertia(fn (Assert $page) => $page->component('equipment/capital-register') ->has('records.data', 1)->where('records.data.0.data.code', 'PRJ-QA')->where('canManage', true)); config(['capital-register.qa_enabled' => false]); $this->get('/projectos-imobilizado')->assertStatus(503); }); test('native register revalidates operator and hides foreign department identity without creating versions', function () { $service = app(CapitalRegister::class); $first = $service->save($this->f['issuer'], $this->input); $this->actingAs($this->f['receiver']); expect(fn () => $service->save($this->f['issuer'], [...$this->input, 'operation_key' => (string) Str::uuid()]))->toThrow(HttpException::class); $this->actingAs($this->f['issuer']); $other = receiptFixture(); $this->actingAs($other['issuer']); $this->get('/projectos-imobilizado')->assertOk()->assertInertia(fn (Assert $page) => $page->has('records.data', 0)); $this->post('/projectos-imobilizado', [...$this->input, 'operation_key' => (string) Str::uuid(), 'record_key' => $first->record_key, 'version' => 1])->assertNotFound(); $this->actingAs($this->f['issuer']); expect(SupplyRecord::query()->where('kind', 'capital_register')->count())->toBe(1); }); test('line destination freezes the chosen project version and preserves it after archival without financial effects', function () { $register = app(CapitalRegister::class); $source = $register->save($this->f['issuer'], $this->input); $this->f['r']->update(['status' => 'draft']); $item = $this->f['r']->items()->firstOrFail(); $input = ['operation_key' => (string) Str::uuid(), 'version' => 0, 'item_id' => $item->id, 'capital_id' => $source->id, 'reason' => 'Escolha de destino sintético da linha da requisição']; $service = app(CapitalAssignments::class); $first = $service->save($this->f['issuer'], $this->f['r'], $input); expect($first->state)->toBe('linked')->and($service->save($this->f['issuer'], $this->f['r'], $input)->id)->toBe($first->id); $register->save($this->f['issuer'], [...$this->input, 'operation_key' => (string) Str::uuid(), 'record_key' => $source->record_key, 'version' => 1, 'state' => 'archived', 'name' => 'Designação posterior sintética']); $frozen = $service->freezeForItems($this->f['issuer'], [$item->id]); expect($frozen[$item->id]['snapshot']['source']['version'])->toBe(1) ->and($frozen[$item->id]['snapshot']['source']['data']['name'])->toBe($this->input['name']); $this->get('/requisicoes/'.$this->f['r']->id.'/projectos-imobilizado')->assertOk() ->assertInertia(fn (Assert $page) => $page->component('requisitions/capital-assignments')->where('canWrite', true)->has('choices.data', 0)->has('current', 1)); expect(fn () => $service->save($this->f['issuer'], $this->f['r'], [...$input, 'operation_key' => (string) Str::uuid(), 'version' => 1]))->toThrow(ModelNotFoundException::class); expect(DB::table('budget_entries')->count())->toBe(0)->and(DB::table('stock_movements')->count())->toBe(0); $item->update(['description' => 'Artigo alterado após a escolha']); expect(fn () => $service->freezeForItems($this->f['issuer'], [$item->id]))->toThrow(HttpException::class); $cleared = $service->save($this->f['issuer'], $this->f['r'], [...$input, 'operation_key' => (string) Str::uuid(), 'version' => 1, 'capital_id' => null]); expect($cleared->state)->toBe('cleared')->and($service->freezeForItems($this->f['issuer'], [$item->id]))->toBe([]) ->and($first->fresh()->state)->toBe('linked'); }); test('submitted requisitions reject new line destinations and stale versions do not replace choices', function () { $source = app(CapitalRegister::class)->save($this->f['issuer'], $this->input); $item = $this->f['r']->items()->firstOrFail(); $input = ['operation_key' => (string) Str::uuid(), 'version' => 0, 'item_id' => $item->id, 'capital_id' => $source->id, 'reason' => 'Escolha documental sintética preservando decisões anteriores']; $service = app(CapitalAssignments::class); expect(fn () => $service->save($this->f['issuer'], $this->f['r'], $input))->toThrow(HttpException::class); $this->f['r']->update(['status' => 'draft']); $service->save($this->f['issuer'], $this->f['r'], $input); expect(fn () => $service->save($this->f['issuer'], $this->f['r'], [...$input, 'operation_key' => (string) Str::uuid(), 'capital_id' => null]))->toThrow(HttpException::class); config(['capital-register.qa_enabled' => false]); expect(fn () => $service->freezeForItems($this->f['issuer'], [$item->id]))->toThrow(HttpException::class); expect(SupplyRecord::query()->where('kind', 'capital_assignment')->count())->toBe(1); });