Files
compras/tests/Feature/RequisitionDuplicateHintsTest.php
T
nala eb32c74c56
tests / ci (push) Waiting to run
commit compras1 2811
2026-11-28 07:11:35 +02:00

77 lines
3.6 KiB
PHP

<?php
use App\Enums\PurchasingRole;
use App\Models\Requisition;
use App\Models\User;
use App\Services\RequisitionDuplicateHints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia as Assert;
require_once __DIR__.'/../Support/ReceiptFixture.php';
function duplicateHintCopy(Requisition $source, array $changes = []): Requisition
{
$copy = $source->replicate();
$copy->fill(['number' => 'REQ-HINT-'.Str::random(12), 'status' => 'draft', ...$changes])->save();
foreach ($source->items as $line) {
$copy->items()->save($line->replicate());
}
return $copy->fresh();
}
test('duplicate advisory is a private native read with no financial or workflow effects', function () {
$f = receiptFixture();
$draft = duplicateHintCopy($f['r']);
$this->actingAs($f['issuer']);
$before = [Requisition::query()->count(), DB::table('audit_logs')->count(), DB::table('budget_entries')->count()];
$this->get('/requisicoes/'.$draft->id)->assertOk()->assertInertia(fn (Assert $page) => $page
->has('duplicateHints.matches', 1)->where('duplicateHints.matches.0.id', $f['r']->id)
->where('duplicateHints.matches.0.status', 'approved')->where('duplicateHints.complete', true)
->missing('duplicateHints.matches.0.items')->missing('duplicateHints.matches.0.justification'));
expect([Requisition::query()->count(), DB::table('audit_logs')->count(), DB::table('budget_entries')->count()])->toBe($before)
->and($draft->fresh()->status)->toBe('draft');
});
test('duplicate advisory excludes different contents dates centres authors and inactive outcomes', function () {
$f = receiptFixture();
$draft = duplicateHintCopy($f['r']);
$this->actingAs($f['issuer']);
$hints = app(RequisitionDuplicateHints::class);
foreach (['cancelled', 'rejected'] as $status) {
$f['r']->update(['status' => $status]);
expect($hints->for($f['issuer'], $draft)['matches'])->toBe([]);
}
$f['r']->update(['status' => 'pending', 'needed_by' => today()->addDays(20)]);
expect($hints->for($f['issuer'], $draft)['matches'])->toBe([]);
$f['r']->update(['needed_by' => $draft->needed_by]);
$f['r']->items()->firstOrFail()->update(['description' => 'Outra necessidade sintética']);
expect($hints->for($f['issuer'], $draft)['matches'])->toBe([]);
$other = User::factory()->create(['tenant_id' => $f['tenant']->id, 'department_id' => $f['department']->id, 'purchasing_role' => PurchasingRole::Procurement]);
$f['r']->items()->firstOrFail()->update(['description' => $draft->items->first()->description]);
$f['r']->update(['requester_id' => $other->id]);
expect($hints->for($f['issuer'], $draft)['matches'])->toBe([]);
$f['r']->update(['requester_id' => $f['issuer']->id]);
$centre = $draft->costCenter->replicate();
$centre->code = 'OTHER-QA';
$centre->save();
$f['r']->update(['cost_center_id' => $centre->id]);
expect($hints->for($f['issuer'], $draft)['matches'])->toBe([]);
duplicateHintCopy($draft, ['requester_id' => $other->id]);
expect($hints->for($other, $draft))->toBeNull();
$draft->update(['total_minor' => null]);
expect($hints->for($f['issuer'], $draft))->toBeNull();
});
test('bounded duplicate comparison reports incomplete coverage without pretending absence', function () {
$f = receiptFixture();
$draft = duplicateHintCopy($f['r']);
for ($i = 0; $i < 100; $i++) {
duplicateHintCopy($f['r']);
}
$this->actingAs($f['issuer']);
$result = app(RequisitionDuplicateHints::class)->for($f['issuer'], $draft);
expect($result['complete'])->toBeFalse()->and($result['matches'])->toHaveCount(100);
});