tenant = Tenant::query()->create(['name' => 'Prestadora de teste']); $this->admin = User::factory()->create(['tenant_id' => $this->tenant->id, 'purchasing_role' => PurchasingRole::Admin]); $this->actingAs($this->admin)->withoutVite(); $this->clientInput = ['client_key' => (string) Str::uuid(), 'code' => 'CLIENTE-001', 'name' => 'Entidade cliente de teste', 'tax_number' => 'FISCAL-EXEMPLO', 'email' => null, 'address' => null, 'active' => true, 'reason' => 'Identificação sintética para conferir o cadastro.']; }); test('business clients are saved once and preserve frozen snapshots after versioned edits', function () { $service = app(BusinessClients::class); $record = $service->save($this->admin, null, $this->clientInput); expect($service->save($this->admin, null, $this->clientInput)->id)->toBe($record->id) ->and(BusinessClient::query()->count())->toBe(1); $frozen = DB::transaction(fn () => $service->snapshot($this->admin, $record->id, 1)); $edited = $this->clientInput; unset($edited['client_key']); $edited['version'] = 1; $edited['name'] = 'Cliente com nome actualizado'; $service->save($this->admin, $record->id, $edited); expect($frozen['name'])->toBe('Entidade cliente de teste')->and($record->fresh()->name)->toBe($edited['name']) ->and($record->fresh()->version)->toBe(2); $this->patch('/empresa/clientes/'.$record->id, $edited)->assertStatus(409); $this->get('/empresa/clientes')->assertOk()->assertSee('Cliente com nome actualizado'); }); test('business clients refuse changed replays blank identities and a foreign company', function () { $service = app(BusinessClients::class); $record = $service->save($this->admin, null, $this->clientInput); $this->post('/empresa/clientes', [...$this->clientInput, 'name' => 'Outro conteúdo para a mesma chave'])->assertStatus(409); $this->post('/empresa/clientes', [...$this->clientInput, 'client_key' => (string) Str::uuid(), 'code' => ' ', 'name' => ' '])->assertSessionHasErrors(['code', 'name']); $foreign = Tenant::query()->create(['name' => 'Outra empresa']); $other = User::factory()->create(['tenant_id' => $foreign->id, 'purchasing_role' => PurchasingRole::Admin]); $this->actingAs($other)->patch('/empresa/clientes/'.$record->id, ['version' => 1, 'reason' => 'Não alterar outra empresa.'])->assertNotFound(); expect(BusinessClient::withoutGlobalScope('tenant')->count())->toBe(1); }); test('a create replay preserves its identity after an edited code is reused', function () { $service = app(BusinessClients::class); $record = $service->save($this->admin, null, $this->clientInput); $edited = $this->clientInput; unset($edited['client_key']); $service->save($this->admin, $record->id, [...$edited, 'version' => 1, 'code' => 'CLIENTE-RENOMEADO']); $other = $service->save($this->admin, null, [...$this->clientInput, 'client_key' => (string) Str::uuid()]); expect($service->save($this->admin, null, $this->clientInput)->id)->toBe($record->id) ->and($record->fresh()->code)->toBe('CLIENTE-RENOMEADO') ->and($other->fresh()->code)->toBe($this->clientInput['code']) ->and(BusinessClient::query()->count())->toBe(2); }); test('client identities cannot be moved or deleted by direct SQL and inactive clients cannot start a new process', function () { $service = app(BusinessClients::class); $record = $service->save($this->admin, null, $this->clientInput); expect(fn () => DB::transaction(fn () => DB::table('business_clients')->where('id', $record->id)->update(['client_key' => (string) Str::uuid(), 'version' => 2])))->toThrow(QueryException::class); expect(fn () => DB::transaction(fn () => DB::table('business_clients')->where('id', $record->id)->delete()))->toThrow(QueryException::class); $edited = $this->clientInput; unset($edited['client_key']); $service->save($this->admin, $record->id, [...$edited, 'version' => 1, 'active' => false]); expect($service->choices($this->admin))->toBe([]); expect(fn () => DB::transaction(fn () => $service->snapshot($this->admin, $record->id, 2)))->toThrow(HttpException::class); });