Files
pricebuddy/tests/Unit/Services/Insights/TargetTrackerCalculatorTest.php
Jeremy d1a8f1d009 feat: product insights tab
Add a Product Insights tab backed by a suite of price-analysis calculators and a
redesigned price history chart.

- Insights calculators: listing-history and daily best-price series builders,
  price stats, percentile, deal score, distribution, drop events, store showdown,
  seasonality, availability timeline, and target tracker
- ProductInsights aggregator service composing the calculators
- Price history chart: best-price and target datasets, best-price line honours the
  unit/retail filter, horizontal scroll on mobile
- Rename the History tab to Insights with consistent heading styling
- Feature and unit test coverage, plus review-feedback fixes
2026-06-18 23:13:14 +10:00

75 lines
2.4 KiB
PHP

<?php
namespace Tests\Unit\Services\Insights;
use App\Services\Insights\TargetTrackerCalculator;
use Tests\TestCase;
class TargetTrackerCalculatorTest extends TestCase
{
public function test_it_tracks_progress_and_hits_against_target(): void
{
$series = collect([
'2026-01-01' => 60.0,
'2026-01-02' => 38.0,
'2026-01-03' => 42.0,
]);
$result = (new TargetTrackerCalculator)->calculate($series, 42.0, 38.0);
$this->assertNotNull($result);
$this->assertSame(4.0, $result->gap);
$this->assertSame(1, $result->hitCount);
$this->assertSame('2026-01-02', $result->lastHitDate);
$this->assertSame(82, $result->progressPercent);
}
public function test_no_target_returns_null(): void
{
$this->assertNull((new TargetTrackerCalculator)->calculate(collect(['a' => 10.0]), 10.0, null));
}
public function test_empty_series_returns_null(): void
{
$this->assertNull((new TargetTrackerCalculator)->calculate(collect(), 10.0, 8.0));
}
public function test_invalid_target_returns_null(): void
{
$series = collect(['2026-01-01' => 10.0]);
$this->assertNull((new TargetTrackerCalculator)->calculate($series, 10.0, 0.0));
$this->assertNull((new TargetTrackerCalculator)->calculate($series, 10.0, -5.0));
}
public function test_high_less_than_or_equal_to_target(): void
{
// Every observed price is already at/below the target, so the range is non-positive
// and progress falls back to the current-vs-target check (100 when current <= target).
$series = collect([
'2026-01-01' => 30.0,
'2026-01-02' => 28.0,
]);
$result = (new TargetTrackerCalculator)->calculate($series, 30.0, 50.0);
$this->assertNotNull($result);
$this->assertSame(100, $result->progressPercent);
}
public function test_no_hits_returns_null_for_last_hit_date(): void
{
// All prices stay above the target -> no hits.
$series = collect([
'2026-01-01' => 60.0,
'2026-01-02' => 70.0,
]);
$result = (new TargetTrackerCalculator)->calculate($series, 60.0, 50.0);
$this->assertNotNull($result);
$this->assertSame(0, $result->hitCount);
$this->assertNull($result->lastHitDate);
}
}