scenario = Scenario::factory()->create(); } public function test_apply_updates_bucket_starting_amounts(): void { 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/apply", ['amount' => 70000] ); $response->assertOk(); $this->assertDatabaseHas('buckets', [ 'name' => 'Rent', 'starting_amount' => 50000, ]); $this->assertDatabaseHas('buckets', [ 'name' => 'Fun', 'starting_amount' => 20000, ]); } public function test_apply_is_additive_to_existing_balance(): void { Bucket::factory()->need()->fixedLimit(50000)->create([ 'scenario_id' => $this->scenario->id, 'name' => 'Rent', 'priority' => 1, 'starting_amount' => 20000, ]); $response = $this->postJson( "/scenarios/{$this->scenario->uuid}/projections/apply", ['amount' => 15000] ); $response->assertOk(); $this->assertDatabaseHas('buckets', [ 'name' => 'Rent', 'starting_amount' => 35000, ]); } public function test_apply_returns_allocation_breakdown(): void { $bucket = Bucket::factory()->need()->fixedLimit(50000)->create([ 'scenario_id' => $this->scenario->id, 'name' => 'Rent', 'priority' => 1, 'starting_amount' => 0, ]); $response = $this->postJson( "/scenarios/{$this->scenario->uuid}/projections/apply", ['amount' => 30000] ); $response->assertOk(); $response->assertJsonPath('allocations.0.bucket_id', $bucket->uuid); $response->assertJsonPath('allocations.0.bucket_name', 'Rent'); $response->assertJsonPath('allocations.0.allocated_amount', 30000); $response->assertJsonPath('total_allocated', 30000); $response->assertJsonPath('unallocated', 0); } public function test_apply_rejects_missing_amount(): void { $response = $this->postJson( "/scenarios/{$this->scenario->uuid}/projections/apply", [] ); $response->assertUnprocessable(); $response->assertJsonValidationErrors('amount'); } public function test_apply_rejects_zero_amount(): void { $response = $this->postJson( "/scenarios/{$this->scenario->uuid}/projections/apply", ['amount' => 0] ); $response->assertUnprocessable(); $response->assertJsonValidationErrors('amount'); } public function test_apply_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/apply", ['amount' => 50000] ); $response->assertOk(); $this->assertDatabaseHas('buckets', [ 'name' => 'Rent', 'starting_amount' => 20000, ]); $this->assertDatabaseHas('buckets', [ 'name' => 'Overflow', 'starting_amount' => 30000, ]); } public function test_apply_with_no_buckets_returns_empty(): void { $response = $this->postJson( "/scenarios/{$this->scenario->uuid}/projections/apply", ['amount' => 100000] ); $response->assertOk(); $response->assertJsonPath('allocations', []); $response->assertJsonPath('total_allocated', 0); $response->assertJsonPath('unallocated', 100000); } }