Audit items #7 and #5. #7 — BrentPricePredictor::generatePrediction previously wrote both an EWMA row and an LLM row to price_predictions on every run. The downstream OilSignal already prefers llm_with_context > llm > ewma, so the EWMA row was dead weight 95% of the time. Now we try LLM first; if it returns null (no API key, parse failure, etc.) we compute and persist EWMA as a real fallback. This also avoids redundant work on the success path. Updated the "stores both" test to "stores only LLM" — asserts no EWMA row is written when the provider succeeds. #5 — BrentPricePredictor and AnthropicPredictionProvider both had byte-identical computeEwma() methods with identical EWMA_ALPHA = 0.3 constants. Extracted to App\Services\Ewma::compute() and dropped both private methods + their alpha constants. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
146 lines
4.9 KiB
PHP
146 lines
4.9 KiB
PHP
<?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()
|
|
);
|
|
}
|