Files
fuel-alert/tests/Feature/Api/StationControllerTest.php
Ovidiu U ea22387c9d Reverse-geocode a general area label for logged searches
Each search now stores an `area_label` (district/town) reverse-geocoded from its
coarsened ~1km lat/lng bucket via postcodes.io, surfaced in the Filament Searches
admin as a sortable/searchable column plus an area filter. Geocoding is cached 30
days per bucket, queries a 2km radius so low-density buckets still match the
default 100m miss, and fails gracefully to null. Adds `searches:backfill-areas`
(scheduled hourly) to label existing rows and retry stragglers.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 09:05:03 +01:00

264 lines
9.9 KiB
PHP

<?php
use App\Enums\FuelType;
use App\Filament\Resources\UserResource;
use App\Models\Station;
use App\Models\StationPriceCurrent;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->withHeaders(['X-Api-Key' => config('app.api_secret_key')]);
// Every search reverse-geocodes its lat/lng bucket to an area label. Fake the
// postcodes.io reverse endpoint (query-string form) so tests never hit the
// network; the path form (/postcodes/SW1A1AA) used for forward lookups is
// matched by per-test stubs and is unaffected by this.
Http::fake([
'api.postcodes.io/postcodes?*' => Http::response([
'status' => 200,
'result' => [['admin_district' => 'Testshire']],
]),
]);
});
function asPaidUserOnStations(string $tier = 'plus'): User
{
test()->artisan('db:seed', ['--class' => 'PlanSeeder']);
$user = User::factory()->create();
UserResource::applyTier($user, $tier, 'monthly');
return $user;
}
it('returns stations near coordinates filtered by fuel type', function () {
$station = Station::factory()->create(['lat' => 52.555064, 'lng' => -0.256119]);
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::B7Standard,
'price_pence' => 14500,
]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=b7_standard&radius=10&sort=price')
->assertOk()
->assertJsonStructure([
'data' => [['station_id', 'name', 'brand', 'is_supermarket', 'lat', 'lng', 'distance_km', 'fuel_type', 'price_pence', 'price', 'price_updated_at']],
'meta' => ['count', 'fuel_type', 'radius_km', 'lowest_pence'],
])
->assertJsonPath('data.0.price_pence', 14500)
->assertJsonPath('meta.fuel_type', 'b7_standard');
});
it('excludes stations with no matching fuel type', function () {
$station = Station::factory()->create(['lat' => 52.555064, 'lng' => -0.256119]);
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10, // not diesel
'price_pence' => 13800,
]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=b7_standard&radius=10')
->assertOk()
->assertJsonPath('meta.count', 0);
});
it('excludes temporarily closed stations', function () {
$closed = Station::factory()->create([
'lat' => 52.555064, 'lng' => -0.256119,
'temporary_closure' => true,
]);
StationPriceCurrent::factory()->create([
'station_id' => $closed->node_id,
'fuel_type' => FuelType::B7Standard,
'price_pence' => 14200,
]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=b7_standard&radius=10')
->assertOk()
->assertJsonPath('meta.count', 0);
});
it('excludes stations beyond radius', function () {
// Station ~100km north
$farStation = Station::factory()->create(['lat' => 53.5, 'lng' => -0.256119]);
StationPriceCurrent::factory()->create([
'station_id' => $farStation->node_id,
'fuel_type' => FuelType::B7Standard,
'price_pence' => 14200,
]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=b7_standard&radius=10')
->assertOk()
->assertJsonPath('meta.count', 0);
});
it('sorts by price when sort=price', function () {
$sLat = 52.555;
$sLng = -0.256;
$cheap = Station::factory()->create(['lat' => $sLat, 'lng' => $sLng]);
$expensive = Station::factory()->create(['lat' => $sLat + 0.001, 'lng' => $sLng]);
StationPriceCurrent::factory()->create(['station_id' => $cheap->node_id, 'fuel_type' => FuelType::B7Standard, 'price_pence' => 13900]);
StationPriceCurrent::factory()->create(['station_id' => $expensive->node_id, 'fuel_type' => FuelType::B7Standard, 'price_pence' => 14500]);
$this->getJson("/api/stations?lat={$sLat}&lng={$sLng}&fuel_type=b7_standard&radius=10&sort=price")
->assertOk()
->assertJsonPath('data.0.price_pence', 13900);
});
it('logs a search record for each request', function () {
$station = Station::factory()->create(['lat' => 52.555064, 'lng' => -0.256119]);
StationPriceCurrent::factory()->create(['station_id' => $station->node_id, 'fuel_type' => FuelType::B7Standard, 'price_pence' => 14500]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=b7_standard&radius=10');
$this->assertDatabaseHas('searches', [
'lat_bucket' => '52.56',
'lng_bucket' => '-0.26',
'fuel_type' => 'b7_standard',
'results_count' => 1,
'lowest_pence' => 14500,
'highest_pence' => 14500,
]);
});
it('stores the reverse-geocoded area label on the search record', function () {
$station = Station::factory()->create(['lat' => 52.555064, 'lng' => -0.256119]);
StationPriceCurrent::factory()->create(['station_id' => $station->node_id, 'fuel_type' => FuelType::B7Standard, 'price_pence' => 14500]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=b7_standard&radius=10');
$this->assertDatabaseHas('searches', [
'lat_bucket' => '52.56',
'lng_bucket' => '-0.26',
'area_label' => 'Testshire',
]);
});
it('returns 422 when required params are missing', function () {
$this->getJson('/api/stations?lat=52.5')
->assertUnprocessable();
});
it('resolves a full postcode to coordinates and returns nearby stations', function () {
$station = Station::factory()->create(['lat' => 51.5010, 'lng' => -0.1415]);
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14200,
]);
Http::fake([
'api.postcodes.io/postcodes/SW1A1AA' => Http::response([
'status' => 200,
'result' => ['postcode' => 'SW1A 1AA', 'latitude' => 51.5010, 'longitude' => -0.1415],
]),
]);
$this->getJson('/api/stations?postcode=SW1A+1AA&fuel_type=e10&radius=1')
->assertOk()
->assertJsonPath('meta.count', 1);
});
it('resolves an outcode to coordinates', function () {
$station = Station::factory()->create(['lat' => 51.5010, 'lng' => -0.1415]);
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14200,
]);
Http::fake([
'api.postcodes.io/outcodes/SW1A' => Http::response([
'status' => 200,
'result' => ['outcode' => 'SW1A', 'latitude' => 51.5010, 'longitude' => -0.1415],
]),
]);
$this->getJson('/api/stations?postcode=SW1A&fuel_type=e10&radius=1')
->assertOk()
->assertJsonPath('meta.count', 1);
});
it('returns 422 when postcode cannot be resolved', function () {
Http::fake([
'api.postcodes.io/*' => Http::response(['status' => 404, 'error' => 'Postcode not found'], 404),
]);
$this->getJson('/api/stations?postcode=ZZ99+9ZZ&fuel_type=e10')
->assertUnprocessable()
->assertJsonValidationErrors(['postcode']);
});
it('includes resolved lat and lng in meta', function () {
$station = Station::factory()->create(['lat' => 52.555064, 'lng' => -0.256119]);
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::B7Standard,
'price_pence' => 14500,
]);
$this->getJson('/api/stations?lat=52.555064&lng=-0.256119&fuel_type=b7_standard&radius=10')
->assertOk()
->assertJsonPath('meta.lat', 52.555064)
->assertJsonPath('meta.lng', -0.256119);
});
it('includes resolved lat and lng in meta when postcode is provided', function () {
$station = Station::factory()->create(['lat' => 51.5010, 'lng' => -0.1415]);
StationPriceCurrent::factory()->create([
'station_id' => $station->node_id,
'fuel_type' => FuelType::E10,
'price_pence' => 14200,
]);
Http::fake([
'api.postcodes.io/postcodes/SW1A1AA' => Http::response([
'status' => 200,
'result' => ['postcode' => 'SW1A 1AA', 'latitude' => 51.5010, 'longitude' => -0.1415],
]),
]);
$this->getJson('/api/stations?postcode=SW1A+1AA&fuel_type=e10&radius=1')
->assertOk()
->assertJsonPath('meta.lat', 51.5010)
->assertJsonPath('meta.lng', -0.1415);
});
it('embeds a tier-locked prediction teaser for guest requests', function () {
Station::factory()->create(['lat' => 52.555, 'lng' => -0.256]);
$this->getJson('/api/stations?lat=52.555&lng=-0.256&fuel_type=e10&radius=10')
->assertOk()
->assertJsonPath('prediction.tier_locked', true)
->assertJsonStructure(['prediction' => ['fuel_type', 'predicted_direction', 'tier_locked']])
->assertJsonMissingPath('prediction.signals')
->assertJsonMissingPath('prediction.weekly_summary');
});
it('embeds a tier-locked teaser for free-tier authenticated users', function () {
asPaidUserOnStations('free');
$user = User::query()->latest('id')->first();
Station::factory()->create(['lat' => 52.555, 'lng' => -0.256]);
$this->actingAs($user, 'sanctum')
->getJson('/api/stations?lat=52.555&lng=-0.256&fuel_type=e10&radius=10')
->assertOk()
->assertJsonPath('prediction.tier_locked', true)
->assertJsonMissingPath('prediction.signals');
});
it('embeds the full prediction payload for plus users', function () {
$user = asPaidUserOnStations('plus');
Station::factory()->create(['lat' => 52.555, 'lng' => -0.256]);
$this->actingAs($user, 'sanctum')
->getJson('/api/stations?lat=52.555&lng=-0.256&fuel_type=e10&radius=10')
->assertOk()
->assertJsonStructure(['prediction' => ['fuel_type', 'predicted_direction', 'confidence_score', 'reasoning', 'weekly_summary', 'signals']])
->assertJsonMissingPath('prediction.tier_locked');
});