chore: retire legacy oil prediction pipeline

Removes everything that was made redundant by the new forecasting
stack. Per docs/superpowers/specs/2026-05-01-prediction-rebuild-design.md,
this was the cleanup planned at the end of Phase 4.

Deleted services and code:
- App\Services\Prediction\Signals\* (the old six-signal aggregator —
  trend, supermarket, day-of-week, brand-behaviour, stickiness,
  regional-momentum, oil — replaced by RidgeRegressionModel).
- App\Services\NationalFuelPredictionService (the post-Phase-4 thin
  shim; StationSearchService now depends on WeeklyForecastService
  directly, set up in the previous commit).
- App\Services\LlmPrediction\* (AbstractLlmPredictionProvider plus
  the four provider implementations — Anthropic, OpenAI, Gemini, and
  the OilPredictionProvider router. Replaced by LlmOverlayService).
- App\Services\BrentPricePredictor and App\Services\Ewma. The Ewma
  helper had no callers left after BrentPricePredictor went.
- App\Models\PricePrediction and its factory.
- App\Console\Commands\PredictOilPrices (the oil:predict command).
- App\Filament\Resources\OilPredictionResource and its Pages.

Schema and dashboard:
- Drop the price_predictions table via a new migration.
- Repoint the Filament StatsOverviewWidget tile from PricePrediction
  to WeeklyForecast so the dashboard reflects the new pipeline.
- Remove the OilPredictionProvider binding from AppServiceProvider.

Test cleanup:
- Delete tests for every retired service.
- Update StatsOverviewWidgetTest to seed weekly_forecasts instead of
  price_predictions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ovidiu U
2026-05-03 08:40:28 +01:00
parent ddd591ad47
commit 203200acb9
32 changed files with 61 additions and 2727 deletions

View File

@@ -1,27 +0,0 @@
<?php
use App\Filament\Resources\OilPredictionResource\Pages\ListOilPredictions;
use App\Models\PricePrediction;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->admin = User::factory()->admin()->create();
$this->actingAs($this->admin);
});
it('renders the oil prediction list', function () {
$predictions = PricePrediction::factory()->count(3)->create();
Livewire::test(ListOilPredictions::class)
->assertOk()
->assertCanSeeTableRecords($predictions);
});
it('has a run prediction header action', function () {
Livewire::test(ListOilPredictions::class)
->assertActionExists('runPrediction');
});

View File

@@ -2,10 +2,11 @@
use App\Filament\Widgets\StatsOverviewWidget;
use App\Models\ApiLog;
use App\Models\PricePrediction;
use App\Models\Station;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
@@ -18,7 +19,20 @@ beforeEach(function () {
it('renders the stats overview widget', function () {
User::factory()->count(3)->create();
Station::factory()->count(2)->create();
PricePrediction::factory()->create(['generated_at' => now()->subHours(2)]);
DB::table('weekly_forecasts')->insert([
'forecast_for' => now()->next(Carbon::MONDAY)->toDateString(),
'model_version' => 'ridge-v1-test',
'direction' => 'rising',
'magnitude_pence' => 80,
'ridge_confidence' => 65,
'flagged_duty_change' => false,
'reasoning' => 'test',
'generated_at' => now()->subHours(2),
'created_at' => now(),
'updated_at' => now(),
]);
ApiLog::factory()->count(2)->create(['status_code' => 200, 'error' => null, 'created_at' => now()->subMinutes(30)]);
Livewire::test(StatsOverviewWidget::class)

View File

@@ -1,145 +0,0 @@
<?php
use App\Enums\PredictionSource;
use App\Enums\TrendDirection;
use App\Models\BrentPrice;
use App\Models\PricePrediction;
use App\Services\BrentPricePredictor;
use App\Services\LlmPrediction\OilPredictionProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->provider = Mockery::mock(OilPredictionProvider::class);
$this->predictor = new BrentPricePredictor($this->provider);
});
it('detects a rising trend when 3-day EWMA exceeds 7-day EWMA by threshold', function (): void {
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(14 - $i)->toDateString(),
'price_usd' => 70.0 + ($i * 2.0),
]));
$prediction = $this->predictor->generateEwmaPrediction($prices);
expect($prediction->direction)->toBe(TrendDirection::Rising)
->and($prediction->source)->toBe(PredictionSource::Ewma)
->and($prediction->confidence)->toBeGreaterThan(0)
->and($prediction->confidence)->toBeLessThanOrEqual(65);
});
it('detects a falling trend when 3-day EWMA falls below 7-day EWMA by threshold', function (): void {
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(14 - $i)->toDateString(),
'price_usd' => 85.0 - ($i * 2.0),
]));
$prediction = $this->predictor->generateEwmaPrediction($prices);
expect($prediction->direction)->toBe(TrendDirection::Falling);
});
it('returns flat when price movement is within threshold', function (): void {
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(14 - $i)->toDateString(),
'price_usd' => 75.0 + (($i % 2 === 0) ? 0.1 : -0.1),
]));
$prediction = $this->predictor->generateEwmaPrediction($prices);
expect($prediction->direction)->toBe(TrendDirection::Flat)
->and($prediction->confidence)->toBe(50);
});
it('returns null when fewer than 14 prices are available for EWMA', function (): void {
$prices = collect(range(1, 10))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(10 - $i)->toDateString(),
'price_usd' => 75.0,
]));
expect($this->predictor->generateEwmaPrediction($prices))->toBeNull();
});
it('stores only the LLM prediction when the provider succeeds', function (): void {
seedPrices(20);
$this->provider->shouldReceive('predict')->once()->andReturn(new PricePrediction([
'predicted_for' => now()->toDateString(),
'source' => PredictionSource::LlmWithContext,
'direction' => TrendDirection::Rising,
'confidence' => 70,
'reasoning' => 'Trend is up.',
'generated_at' => now(),
]));
$prediction = $this->predictor->generatePrediction();
expect($prediction->source)->toBe(PredictionSource::LlmWithContext)
->and(PricePrediction::count())->toBe(1)
->and(PricePrediction::where('source', PredictionSource::Ewma)->count())->toBe(0);
});
it('falls back to EWMA when provider returns null', function (): void {
seedPrices(20, slope: 0.8);
$this->provider->shouldReceive('predict')->once()->andReturn(null);
$prediction = $this->predictor->generatePrediction();
expect($prediction->source)->toBe(PredictionSource::Ewma)
->and(PricePrediction::count())->toBe(1);
});
it('returns null when there is insufficient price data', function (): void {
BrentPrice::insert([
['date' => now()->subDays(2)->toDateString(), 'price_usd' => 75.0],
['date' => now()->subDay()->toDateString(), 'price_usd' => 76.0],
]);
$this->provider->shouldNotReceive('predict');
expect($this->predictor->generatePrediction())->toBeNull()
->and(PricePrediction::count())->toBe(0);
});
it('flags latest brent price as prediction generated on success', function (): void {
seedPrices(20);
$this->provider->shouldReceive('predict')->once()->andReturn(null);
$this->predictor->generatePrediction();
$latest = BrentPrice::orderBy('date', 'desc')->first();
expect($latest->prediction_generated_at)->not->toBeNull();
});
it('does not flag when prediction cannot be generated', function (): void {
BrentPrice::insert([
['date' => now()->subDay()->toDateString(), 'price_usd' => 75.0],
]);
$this->provider->shouldNotReceive('predict');
$this->predictor->generatePrediction();
expect(BrentPrice::first()->prediction_generated_at)->toBeNull();
});
it('returns the latest price row', function (): void {
seedPrices(3);
expect($this->predictor->latestPrice())->not->toBeNull()
->and($this->predictor->latestPrice()->date->toDateString())->toBe(now()->toDateString());
});
function seedPrices(int $count, float $slope = 1.0): void
{
BrentPrice::insert(
collect(range(1, $count))->map(fn (int $i) => [
'date' => now()->subDays($count - $i)->toDateString(),
'price_usd' => 75.0 + ($i * $slope),
])->all()
);
}

View File

@@ -1,219 +0,0 @@
<?php
use App\Enums\PredictionSource;
use App\Enums\TrendDirection;
use App\Models\BrentPrice;
use App\Services\ApiLogger;
use App\Services\LlmPrediction\AnthropicPredictionProvider;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
beforeEach(function (): void {
Http::preventStrayRequests();
config(['services.anthropic.api_key' => 'test-key']);
$this->provider = new AnthropicPredictionProvider(new ApiLogger);
});
it('returns null when api key is not configured', function (): void {
config(['services.anthropic.api_key' => null]);
$prices = fakePrices(14);
expect($this->provider->predict($prices))->toBeNull();
});
it('uses submit_prediction tool in the basic request', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'rising', 'confidence' => 72, 'reasoning' => 'Prices rising.'],
]],
]),
]);
// context request fails, falls back to basic
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([], 500)
->push([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'rising', 'confidence' => 72, 'reasoning' => 'Prices rising.'],
]],
]),
]);
$this->provider->predict(fakePrices(14));
Http::assertSent(function ($request) {
$tools = $request->data()['tools'] ?? [];
return collect($tools)->contains(fn ($t) => $t['name'] === 'submit_prediction');
});
});
it('returns a prediction with Llm source from basic tool use', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([], 500) // context fails
->push([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'rising', 'confidence' => 72, 'reasoning' => 'Consistent upward trend.'],
]],
]),
]);
$prediction = $this->provider->predict(fakePrices(14));
expect($prediction->direction)->toBe(TrendDirection::Rising)
->and($prediction->source)->toBe(PredictionSource::Llm)
->and($prediction->confidence)->toBe(72)
->and($prediction->reasoning)->toBe('Consistent upward trend.');
});
it('caps confidence at 85', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([], 500)
->push([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'falling', 'confidence' => 99, 'reasoning' => 'Very confident.'],
]],
]),
]);
$prediction = $this->provider->predict(fakePrices(14));
expect($prediction->confidence)->toBe(85);
});
it('returns null when tool_use block is missing from response', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([], 500)
->push([
'stop_reason' => 'end_turn',
'content' => [['type' => 'text', 'text' => 'Sorry, I cannot help.']],
]),
]);
expect($this->provider->predict(fakePrices(14)))->toBeNull();
});
it('sends web_search tool during context prediction phase', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([
'stop_reason' => 'end_turn',
'content' => [['type' => 'text', 'text' => 'Searched and analysed.']],
])
->push([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'flat', 'confidence' => 50, 'reasoning' => 'No clear trend.'],
]],
]),
]);
$this->provider->predict(fakePrices(20));
Http::assertSent(function ($request) {
$tools = $request->data()['tools'] ?? [];
return collect($tools)->contains(fn ($t) => ($t['type'] ?? '') === 'web_search_20250305');
});
});
it('returns LlmWithContext source when context prediction succeeds', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([
'stop_reason' => 'end_turn',
'content' => [['type' => 'text', 'text' => 'Analysed news.']],
])
->push([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'rising', 'confidence' => 70, 'reasoning' => 'OPEC+ cuts support prices.'],
]],
]),
]);
$prediction = $this->provider->predict(fakePrices(20));
expect($prediction->source)->toBe(PredictionSource::LlmWithContext)
->and($prediction->direction)->toBe(TrendDirection::Rising);
});
it('continues on pause_turn during web search phase', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([
'stop_reason' => 'pause_turn',
'content' => [['type' => 'server_tool_use', 'name' => 'web_search', 'input' => ['query' => 'Brent crude']]],
])
->push([
'stop_reason' => 'end_turn',
'content' => [['type' => 'text', 'text' => 'Done searching.']],
])
->push([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'falling', 'confidence' => 60, 'reasoning' => 'Demand fears.'],
]],
]),
]);
$prediction = $this->provider->predict(fakePrices(20));
expect($prediction)->not->toBeNull()
->and($prediction->direction)->toBe(TrendDirection::Falling);
Http::assertSentCount(3);
});
it('falls back to basic prediction when context phase fails', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([], 500) // context search fails
->push([
'stop_reason' => 'tool_use',
'content' => [[
'type' => 'tool_use',
'name' => 'submit_prediction',
'input' => ['direction' => 'rising', 'confidence' => 65, 'reasoning' => 'Rising trend.'],
]],
]),
]);
$prediction = $this->provider->predict(fakePrices(14));
expect($prediction->source)->toBe(PredictionSource::Llm);
});
// --- helpers ---
function fakePrices(int $count): Collection
{
return collect(range(1, $count))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays($count - $i)->toDateString(),
'price_usd' => 75.0 + $i,
]));
}

View File

@@ -1,394 +0,0 @@
<?php
use App\Enums\FuelType;
use App\Models\Station;
use App\Models\StationPrice;
use App\Models\StationPriceCurrent;
use App\Services\NationalFuelPredictionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
it('returns no_signal when there is insufficient price history', function () {
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['predicted_direction'])->toBe('stable')
->and($result['signals']['trend']['enabled'])->toBeFalse()
->and($result['action'])->toBe('no_signal');
});
it('detects rising trend from consistently increasing daily averages', function () {
$station = Station::factory()->create();
// 7 days of prices rising at ~100 pence/day
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14000 + ((6 - $daysAgo) * 100),
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['signals']['trend']['direction'])->toBe('up')
->and($result['signals']['trend']['enabled'])->toBeTrue()
->and($result['predicted_direction'])->toBe('up')
->and($result['action'])->toBe('fill_now');
});
it('detects falling trend from consistently decreasing daily averages', function () {
$station = Station::factory()->create();
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 16000 - ((6 - $daysAgo) * 100),
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['signals']['trend']['direction'])->toBe('down')
->and($result['predicted_direction'])->toBe('down')
->and($result['action'])->toBe('wait');
});
it('returns current_avg from station_prices_current', function () {
$station = Station::factory()->create();
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14750,
]);
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['current_avg'])->toBe(147.5);
});
it('includes all required keys in response', function () {
$result = app(NationalFuelPredictionService::class)->predict();
expect($result)
->toHaveKeys([
'fuel_type', 'current_avg', 'predicted_direction', 'predicted_change_pence',
'confidence_score', 'confidence_label', 'action', 'reasoning',
'prediction_horizon_days', 'region_key', 'methodology',
'weekly_summary', 'signals',
])
->and($result['signals'])->toHaveKeys([
'trend', 'day_of_week', 'brand_behaviour',
'national_momentum', 'regional_momentum', 'price_stickiness', 'oil',
])
->and($result['weekly_summary'])->toHaveKeys([
'yesterday_avg', 'today_avg', 'tomorrow_estimated_avg',
'yesterday_today_delta_pence', 'last_7_days_series',
'last_7_days_change_pence', 'cheapest_day', 'priciest_day', 'is_regional',
]);
});
it('weekly_summary returns null prices and empty series when there is no data', function () {
$result = app(NationalFuelPredictionService::class)->predict();
$weekly = $result['weekly_summary'];
expect($weekly['yesterday_avg'])->toBeNull()
->and($weekly['yesterday_today_delta_pence'])->toBeNull()
->and($weekly['last_7_days_series'])->toBe([])
->and($weekly['cheapest_day'])->toBeNull()
->and($weekly['priciest_day'])->toBeNull()
->and($weekly['is_regional'])->toBeFalse();
});
it('weekly_summary populates yesterday avg, today avg and 7-day series from station_prices', function () {
$station = Station::factory()->create();
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14000,
]);
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14000 + ($daysAgo * 50),
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
$result = app(NationalFuelPredictionService::class)->predict();
$weekly = $result['weekly_summary'];
expect($weekly['yesterday_avg'])->toBe(140.5)
->and($weekly['today_avg'])->toBe(140.0)
->and($weekly['yesterday_today_delta_pence'])->toBe(-0.5)
->and(count($weekly['last_7_days_series']))->toBe(7)
->and($weekly['cheapest_day']['avg'])->toBe(140.0)
->and($weekly['priciest_day']['avg'])->toBe(143.0);
});
it('weekly_summary falls back from regional to national when regional data is empty', function () {
$station = Station::factory()->create(['lat' => 51.5, 'lng' => -0.1]);
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14000,
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
// Coordinates 600+ km away from any station — no regional data available.
$result = app(NationalFuelPredictionService::class)->predict(58.0, -3.0);
$weekly = $result['weekly_summary'];
expect($weekly['is_regional'])->toBeFalse()
->and(count($weekly['last_7_days_series']))->toBe(7);
});
it('weekly_summary marks is_regional true when stations exist within 50km of coordinates', function () {
$station = Station::factory()->create(['lat' => 51.5, 'lng' => -0.1]);
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14000,
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
$result = app(NationalFuelPredictionService::class)->predict(51.5074, -0.1278);
expect($result['weekly_summary']['is_regional'])->toBeTrue();
});
it('always returns e10 as fuel_type', function () {
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['fuel_type'])->toBe('e10');
});
it('returns national region_key without coordinates', function () {
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['region_key'])->toBe('national');
});
it('returns regional region_key when coordinates are provided', function () {
$result = app(NationalFuelPredictionService::class)->predict(51.5074, -0.1278);
expect($result['region_key'])->toBe('regional');
});
it('enables regional_momentum signal when coordinates are provided', function () {
$station = Station::factory()->create(['lat' => 51.5, 'lng' => -0.1]);
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14000 + ((6 - $daysAgo) * 100),
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
$result = app(NationalFuelPredictionService::class)->predict(51.5074, -0.1278);
expect($result['signals']['regional_momentum']['enabled'])->toBeTrue();
});
it('disables regional_momentum signal without coordinates', function () {
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['signals']['regional_momentum']['enabled'])->toBeFalse();
});
it('disables trend signal when r_squared is below 0.5', function () {
$station = Station::factory()->create();
// Highly erratic prices (zigzag pattern) — low R²
$prices = [14000, 16000, 13000, 17000, 12000, 18000, 14500];
foreach ($prices as $daysAgo => $price) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => $price,
'price_effective_at' => now()->subDays(count($prices) - 1 - $daysAgo)->setTime(12, 0),
]);
}
$result = app(NationalFuelPredictionService::class)->predict();
// Trend signal may be disabled if both 5-day and 14-day lookbacks fail R² threshold
expect($result['signals']['trend']['data_points'])->toBeInt();
});
it('oil signal is disabled when no price_predictions row covers today or later', function () {
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['signals']['oil']['enabled'])->toBeFalse();
});
it('oil signal picks up an llm prediction over an ewma one for the same date', function () {
DB::table('price_predictions')->insert([
[
'predicted_for' => now()->toDateString(),
'source' => 'ewma',
'direction' => 'flat',
'confidence' => 60,
'reasoning' => null,
'generated_at' => now()->subHour(),
],
[
'predicted_for' => now()->toDateString(),
'source' => 'llm',
'direction' => 'rising',
'confidence' => 75,
'reasoning' => 'OPEC cut',
'generated_at' => now(),
],
]);
$oil = app(NationalFuelPredictionService::class)->predict()['signals']['oil'];
expect($oil['enabled'])->toBeTrue()
->and($oil['direction'])->toBe('up')
->and($oil['score'])->toBe(1.0)
->and($oil['confidence'])->toBe(0.75);
});
it('oil signal prefers llm_with_context over plain llm', function () {
DB::table('price_predictions')->insert([
[
'predicted_for' => now()->toDateString(),
'source' => 'llm',
'direction' => 'falling',
'confidence' => 70,
'reasoning' => 'baseline',
'generated_at' => now(),
],
[
'predicted_for' => now()->toDateString(),
'source' => 'llm_with_context',
'direction' => 'rising',
'confidence' => 82,
'reasoning' => 'with context',
'generated_at' => now(),
],
]);
$oil = app(NationalFuelPredictionService::class)->predict()['signals']['oil'];
expect($oil['direction'])->toBe('up')
->and($oil['confidence'])->toBe(0.82);
});
it('confidence reaches "high" when trend and oil agree strongly', function () {
$station = Station::factory()->create();
// Strong falling trend over 7 days, ~1p/day
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 15000 - ((6 - $daysAgo) * 100),
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
DB::table('price_predictions')->insert([
'predicted_for' => now()->toDateString(),
'source' => 'llm',
'direction' => 'falling',
'confidence' => 80,
'reasoning' => 'agree',
'generated_at' => now(),
]);
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['predicted_direction'])->toBe('down')
->and($result['confidence_score'])->toBeGreaterThanOrEqual(70)
->and($result['confidence_label'])->toBe('high');
});
it('confidence drops when trend and oil disagree', function () {
$station = Station::factory()->create();
// Strong falling trend
for ($daysAgo = 6; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 15000 - ((6 - $daysAgo) * 100),
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
// Oil disagrees: rising
DB::table('price_predictions')->insert([
'predicted_for' => now()->toDateString(),
'source' => 'llm',
'direction' => 'rising',
'confidence' => 80,
'reasoning' => 'opec',
'generated_at' => now(),
]);
$agree = app(NationalFuelPredictionService::class)->predict();
// Replace oil with one that agrees instead — confidence should be higher
DB::table('price_predictions')->update([
'direction' => 'falling',
]);
$disagreeReplaced = app(NationalFuelPredictionService::class)->predict();
expect($agree['confidence_score'])->toBeLessThan($disagreeReplaced['confidence_score']);
});
it('day-of-week signal activates at 21 days of history (no longer 56)', function () {
$station = Station::factory()->create();
for ($daysAgo = 25; $daysAgo >= 0; $daysAgo--) {
StationPrice::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14000 + ($daysAgo % 7) * 50,
'price_effective_at' => now()->subDays($daysAgo)->setTime(12, 0),
]);
}
$result = app(NationalFuelPredictionService::class)->predict();
expect($result['signals']['day_of_week']['enabled'])->toBeTrue();
});
it('reasoning fallback for the wait action does not say "fill up"', function () {
// No data → trend disabled, brand disabled, oil disabled.
// Force a "down" direction by injecting an oil prediction that points down with low confidence.
DB::table('price_predictions')->insert([
'predicted_for' => now()->toDateString(),
'source' => 'ewma',
'direction' => 'falling',
'confidence' => 50,
'reasoning' => null,
'generated_at' => now(),
]);
$result = app(NationalFuelPredictionService::class)->predict();
if ($result['action'] === 'wait') {
expect($result['reasoning'])->not->toContain('fill up at the cheapest');
} else {
// If thresholds keep this at no_signal, still verify action-aware fallback exists
expect($result['reasoning'])->toBeString();
}
});