feat: add GET /api/prediction endpoint

Implements PredictionRequest (fuel_type validation with ValueError→ValidationException), PredictionController delegating to NationalFuelPredictionService, and 5 feature tests. Also fixes LEAST() MySQL-only function to a CASE WHEN expression for SQLite test compatibility.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ovidiu U
2026-04-04 19:26:48 +01:00
parent 1c548eae87
commit cf6a1369d4
4 changed files with 119 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
<?php
use App\Enums\FuelType;
use App\Models\Station;
use App\Models\StationPriceCurrent;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('returns a prediction response for diesel', function () {
$this->getJson('/api/prediction?fuel_type=diesel')
->assertOk()
->assertJsonStructure([
'fuel_type', 'current_avg', 'predicted_direction', 'predicted_change_pence',
'confidence_score', 'confidence_label', 'action', 'reasoning',
'prediction_horizon_days', 'region_key', 'methodology',
'signals' => [
'trend' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
'day_of_week' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
'brand_behaviour' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
'national_momentum' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
'regional_momentum' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
'price_stickiness' => ['score', 'confidence', 'direction', 'detail', 'data_points', 'enabled'],
],
])
->assertJsonPath('fuel_type', 'b7_standard')
->assertJsonPath('region_key', 'national');
});
it('includes current average from live prices', function () {
$station = Station::factory()->create();
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::B7Standard,
'price_pence' => 14750,
]);
$response = $this->getJson('/api/prediction?fuel_type=diesel')->assertOk();
expect($response->json('current_avg'))->toBe(147.5);
});
it('accepts optional lat and lng for regional context', function () {
$this->getJson('/api/prediction?fuel_type=diesel&lat=52.5&lng=-0.2')
->assertOk()
->assertJsonPath('region_key', 'national'); // still national, regional_momentum signal updated internally
});
it('returns 422 when fuel_type is missing', function () {
$this->getJson('/api/prediction')
->assertUnprocessable()
->assertJsonValidationErrors(['fuel_type']);
});
it('returns 422 for unknown fuel_type alias', function () {
$this->getJson('/api/prediction?fuel_type=rocket_fuel')
->assertUnprocessable()
->assertJsonValidationErrors(['fuel_type']);
});