buckets/tests/Feature/ProjectionPreviewTest.php

236 lines
7.8 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature;
use App\Models\Bucket;
use App\Models\Scenario;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProjectionPreviewTest extends TestCase
{
use RefreshDatabase;
private Scenario $scenario;
protected function setUp(): void
{
parent::setUp();
$this->scenario = Scenario::factory()->create();
}
public function test_preview_returns_correct_allocations(): void
{
// allocation_value is in dollars, but PipelineAllocationService treats
// getEffectiveCapacity() return as cents (known unit mismatch).
// fixedLimit(50000) = $500 capacity as seen by the service.
Bucket::factory()->need()->fixedLimit(50000)->create([
'scenario_id' => $this->scenario->id,
'name' => 'Rent',
'priority' => 1,
'starting_amount' => 0,
]);
Bucket::factory()->want()->fixedLimit(30000)->create([
'scenario_id' => $this->scenario->id,
'name' => 'Fun',
'priority' => 2,
'starting_amount' => 0,
]);
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 700]
);
$response->assertOk();
$response->assertJsonCount(2, 'allocations');
$response->assertJsonPath('allocations.0.bucket_name', 'Rent');
$response->assertJsonPath('allocations.0.allocated_amount', 500);
$response->assertJsonPath('allocations.1.bucket_name', 'Fun');
$response->assertJsonPath('allocations.1.allocated_amount', 200);
$response->assertJsonPath('total_allocated', 700);
$response->assertJsonPath('unallocated', 0);
}
public function test_preview_returns_remaining_capacity(): void
{
// fixedLimit(50000) → getEffectiveCapacity() = 50000 (treated as cents by service)
// Input $300 = 30000 cents, allocated = min(50000, 30000) = 30000 cents = $300
// remaining = (50000 - 30000) / 100 = $200
Bucket::factory()->need()->fixedLimit(50000)->create([
'scenario_id' => $this->scenario->id,
'name' => 'Savings',
'priority' => 1,
'starting_amount' => 0,
]);
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 300]
);
$response->assertOk();
$response->assertJsonPath('allocations.0.allocated_amount', 300);
$response->assertJsonPath('allocations.0.remaining_capacity', 200);
}
public function test_preview_returns_uuids_not_integer_ids(): void
{
$bucket = Bucket::factory()->need()->fixedLimit(50000)->create([
'scenario_id' => $this->scenario->id,
'priority' => 1,
'starting_amount' => 0,
]);
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 100]
);
$response->assertOk();
$response->assertJsonPath('allocations.0.bucket_id', $bucket->uuid);
}
public function test_preview_includes_bucket_type(): void
{
Bucket::factory()->need()->fixedLimit(50000)->create([
'scenario_id' => $this->scenario->id,
'priority' => 1,
'starting_amount' => 0,
]);
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 100]
);
$response->assertOk();
$response->assertJsonPath('allocations.0.bucket_type', 'need');
}
public function test_preview_rejects_missing_amount(): void
{
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
[]
);
$response->assertUnprocessable();
$response->assertJsonValidationErrors('amount');
}
public function test_preview_rejects_zero_amount(): void
{
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 0]
);
$response->assertUnprocessable();
$response->assertJsonValidationErrors('amount');
}
public function test_preview_rejects_negative_amount(): void
{
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => -50]
);
$response->assertUnprocessable();
$response->assertJsonValidationErrors('amount');
}
public function test_preview_with_no_buckets_returns_empty_allocations(): void
{
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 1000]
);
$response->assertOk();
$response->assertJsonPath('allocations', []);
$response->assertJsonPath('total_allocated', 0);
$response->assertJsonPath('unallocated', 1000);
}
public function test_preview_respects_priority_ordering(): void
{
Bucket::factory()->want()->fixedLimit(20000)->create([
'scenario_id' => $this->scenario->id,
'name' => 'Low Priority',
'priority' => 3,
'starting_amount' => 0,
]);
Bucket::factory()->need()->fixedLimit(30000)->create([
'scenario_id' => $this->scenario->id,
'name' => 'High Priority',
'priority' => 1,
'starting_amount' => 0,
]);
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 400]
);
$response->assertOk();
$response->assertJsonPath('allocations.0.bucket_name', 'High Priority');
$response->assertJsonPath('allocations.0.allocated_amount', 300);
$response->assertJsonPath('allocations.1.bucket_name', 'Low Priority');
$response->assertJsonPath('allocations.1.allocated_amount', 100);
}
public function test_preview_handles_dollar_cent_conversion_accurately(): void
{
Bucket::factory()->need()->fixedLimit(100000)->create([
'scenario_id' => $this->scenario->id,
'priority' => 1,
'starting_amount' => 0,
]);
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 15.50]
);
$response->assertOk();
$response->assertJsonPath('allocations.0.allocated_amount', 15.5);
$response->assertJsonPath('total_allocated', 15.5);
$response->assertJsonPath('unallocated', 0);
}
public function test_preview_with_overflow_bucket_captures_remainder(): void
{
Bucket::factory()->need()->fixedLimit(20000)->create([
'scenario_id' => $this->scenario->id,
'name' => 'Rent',
'priority' => 1,
'starting_amount' => 0,
]);
Bucket::factory()->overflow()->create([
'scenario_id' => $this->scenario->id,
'name' => 'Overflow',
'priority' => 2,
'starting_amount' => 0,
]);
$response = $this->postJson(
"/scenarios/{$this->scenario->uuid}/projections/preview",
['amount' => 500]
);
$response->assertOk();
$response->assertJsonPath('allocations.0.bucket_name', 'Rent');
$response->assertJsonPath('allocations.0.allocated_amount', 200);
$response->assertJsonPath('allocations.1.bucket_name', 'Overflow');
$response->assertJsonPath('allocations.1.allocated_amount', 300);
$response->assertJsonPath('allocations.1.remaining_capacity', null);
$response->assertJsonPath('total_allocated', 500);
$response->assertJsonPath('unallocated', 0);
}
}