Files
fuel-price/tests/Unit/Services/OilPriceServiceTest.php
Ovidiu U d5fb7f85bd
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled
feat: add Filament admin panel with migrations and design spec
- Add AdminPanelProvider mounting panel at `/admin` with `is_admin` auth guard
- Add `is_admin` boolean column to users table
- Add brent_prices and price_predictions tables with appropriate indexes
- Add comprehensive admin design spec covering resources, dashboard, navigation, and build order
- Configure default panel with amber primary color and standard middleware stack
- Add compiled Filament assets (actions.js, app.css)
2026-04-04 13:40:56 +01:00

230 lines
7.5 KiB
PHP

<?php
use App\Enums\PredictionSource;
use App\Enums\TrendDirection;
use App\Models\BrentPrice;
use App\Models\PricePrediction;
use App\Services\ApiLogger;
use App\Services\OilPriceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->service = new OilPriceService(new ApiLogger);
});
// --- fetchBrentPrices ---
it('fetches and stores brent prices from FRED', function (): void {
Http::fake([
'*/fred/series/observations*' => Http::response([
'observations' => [
['date' => '2026-03-31', 'value' => '74.50'],
['date' => '2026-04-01', 'value' => '75.10'],
['date' => '2026-04-02', 'value' => '73.80'],
],
]),
]);
$this->service->fetchBrentPrices();
expect(BrentPrice::count())->toBe(3)
->and(BrentPrice::find('2026-04-02')->price_usd)->toBe('73.80');
});
it('filters out FRED missing value markers', function (): void {
Http::fake([
'*/fred/series/observations*' => Http::response([
'observations' => [
['date' => '2026-04-01', 'value' => '75.10'],
['date' => '2026-04-02', 'value' => '.'], // weekend/holiday
['date' => '2026-04-03', 'value' => '74.20'],
],
]),
]);
$this->service->fetchBrentPrices();
expect(BrentPrice::count())->toBe(2)
->and(BrentPrice::find('2026-04-02'))->toBeNull();
});
it('upserts existing brent price rows on refetch', function (): void {
Http::fake([
'*/fred/series/observations*' => Http::sequence()
->push(['observations' => [['date' => '2026-04-01', 'value' => '74.00']]])
->push(['observations' => [['date' => '2026-04-01', 'value' => '75.50']]]),
]);
$this->service->fetchBrentPrices();
$this->service->fetchBrentPrices();
expect(BrentPrice::count())->toBe(1)
->and(BrentPrice::find('2026-04-01')->price_usd)->toBe('75.50');
});
// --- generateEwmaPrediction ---
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->service->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->service->generateEwmaPrediction($prices);
expect($prediction->direction)->toBe(TrendDirection::Falling)
->and($prediction->source)->toBe(PredictionSource::Ewma);
});
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->service->generateEwmaPrediction($prices);
expect($prediction->direction)->toBe(TrendDirection::Flat)
->and($prediction->confidence)->toBe(50);
});
it('returns null when fewer than 14 prices are available', 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->service->generateEwmaPrediction($prices))->toBeNull();
});
// --- generateLlmPrediction ---
it('generates an LLM prediction and stores it', function (): void {
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(14 - $i)->toDateString(),
'price_usd' => 75.0 + $i,
]));
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [
['text' => '{"direction":"rising","confidence":72,"reasoning":"Consistent upward trend over 14 days."}'],
],
]),
]);
$prediction = $this->service->generateLlmPrediction($prices);
expect($prediction->direction)->toBe(TrendDirection::Rising)
->and($prediction->source)->toBe(PredictionSource::Llm)
->and($prediction->confidence)->toBe(72)
->and($prediction->reasoning)->toBe('Consistent upward trend over 14 days.');
});
it('caps LLM confidence at 85', function (): void {
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(14 - $i)->toDateString(),
'price_usd' => 75.0,
]));
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [
['text' => '{"direction":"falling","confidence":99,"reasoning":"Very confident."}'],
],
]),
]);
$prediction = $this->service->generateLlmPrediction($prices);
expect($prediction->confidence)->toBe(85);
});
it('returns null when LLM returns malformed JSON', function (): void {
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(14 - $i)->toDateString(),
'price_usd' => 75.0,
]));
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [['text' => 'Sorry, I cannot help with that.']],
]),
]);
expect($this->service->generateLlmPrediction($prices))->toBeNull();
});
// --- generatePrediction (orchestrator) ---
it('uses LLM when API key is configured', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::insert(
collect(range(1, 20))->map(fn (int $i) => [
'date' => now()->subDays(20 - $i)->toDateString(),
'price_usd' => 75.0 + $i,
])->all()
);
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [
['text' => '{"direction":"rising","confidence":70,"reasoning":"Trend is up."}'],
],
]),
]);
$prediction = $this->service->generatePrediction();
expect($prediction->source)->toBe(PredictionSource::Llm)
->and(PricePrediction::count())->toBe(1);
});
it('falls back to EWMA when LLM fails', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::insert(
collect(range(1, 20))->map(fn (int $i) => [
'date' => now()->subDays(20 - $i)->toDateString(),
'price_usd' => 75.0 + ($i * 0.8),
])->all()
);
Http::fake([
'https://api.anthropic.com/*' => Http::response([], 500),
]);
$prediction = $this->service->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],
]);
expect($this->service->generatePrediction())->toBeNull()
->and(PricePrediction::count())->toBe(0);
});