Siri: scope cheapest-near-me to the saved search radius (5/10/15 mi)

This commit is contained in:
FuelBoard Contributor
2026-08-14 12:49:30 +01:00
parent be781d80f5
commit 037c2418af
3 changed files with 64 additions and 28 deletions
@@ -412,41 +412,62 @@ final class SiriCheapestLookupTests: XCTestCase {
FuelStation(id: id, name: id, brand: "X", address: "", postcode: "", lat: lat, lng: lng, prices: prices, priceUpdated: nil)
}
func testCheapestPicksLowestPriceNotNearest() {
// Near station is pricier; far station is cheaper price must win.
// Origin (53.01, -1.01). near 0.8 mi away; mid 3.2 mi away; far 36 mi away.
private let nearLat = 53.0, nearLng = -1.0
private let midLat = 53.05, midLng = -1.05
private let farLat = 53.5, farLng = -1.5
func testCheapestPicksLowestPriceNotNearestWithinRadius() {
// Both inside the 5 mi radius; near station is pricier price wins.
let stations = [
station("near", lat: 53.0, lng: -1.0, [.e10: 145.9]),
station("far", lat: 53.5, lng: -1.5, [.e10: 139.9]),
station("near", lat: nearLat, lng: nearLng, [.e10: 145.9]),
station("mid", lat: midLat, lng: midLng, [.e10: 139.9]),
]
let result = SiriCheapestLookup.cheapest(in: stations, fuel: .e10, fromLat: 53.01, lng: -1.01)
XCTAssertEqual(result?.id, "far", "cheapest by price, not by distance")
let result = SiriCheapestLookup.cheapest(in: stations, fuel: .e10, fromLat: 53.01, lng: -1.01, withinMiles: 5)
XCTAssertEqual(result?.id, "mid", "cheapest by price within the radius")
}
func testCheapestTieBreaksByDistance() {
let stations = [
station("near", lat: 53.0, lng: -1.0, [.e10: 140.0]),
station("far", lat: 53.9, lng: -1.9, [.e10: 140.0]),
station("near", lat: nearLat, lng: nearLng, [.e10: 140.0]),
station("mid", lat: midLat, lng: midLng, [.e10: 140.0]),
]
let result = SiriCheapestLookup.cheapest(in: stations, fuel: .e10, fromLat: 53.01, lng: -1.01)
let result = SiriCheapestLookup.cheapest(in: stations, fuel: .e10, fromLat: 53.01, lng: -1.01, withinMiles: 5)
XCTAssertEqual(result?.id, "near", "equal prices resolve to the nearest station")
}
func testCheapestSkipsStationsWithoutThatFuel() {
let stations = [
station("noDiesel", lat: 53.0, lng: -1.0, [.e10: 139.9]),
station("sellsDiesel", lat: 53.5, lng: -1.5, [.diesel: 149.9]),
station("noDiesel", lat: nearLat, lng: nearLng, [.e10: 139.9]),
station("sellsDiesel", lat: midLat, lng: midLng, [.diesel: 149.9]),
]
let result = SiriCheapestLookup.cheapest(in: stations, fuel: .diesel, fromLat: 53.01, lng: -1.01)
let result = SiriCheapestLookup.cheapest(in: stations, fuel: .diesel, fromLat: 53.01, lng: -1.01, withinMiles: 5)
XCTAssertEqual(result?.id, "sellsDiesel", "stations without the fuel are skipped")
}
func testCheapestIgnoresCheaperStationOutsideRadius() {
// The reported bug: the UK-wide minimum (100.9p, 36 mi away) must NOT
// beat a closer 129.9p station when the radius is 5 mi.
let stations = [
station("near", lat: nearLat, lng: nearLng, [.diesel: 129.9]),
station("farCheap", lat: farLat, lng: farLng, [.diesel: 100.9]),
]
let result = SiriCheapestLookup.cheapest(in: stations, fuel: .diesel, fromLat: 53.01, lng: -1.01, withinMiles: 5)
XCTAssertEqual(result?.id, "near", "cheaper station outside the radius is excluded")
}
func testCheapestReturnsNilWhenOnlyStationsOutsideRadius() {
let stations = [station("far", lat: farLat, lng: farLng, [.diesel: 100.9])]
XCTAssertNil(SiriCheapestLookup.cheapest(in: stations, fuel: .diesel, fromLat: 53.01, lng: -1.01, withinMiles: 5))
}
func testCheapestReturnsNilWhenNoStationSellsFuel() {
let stations = [station("a", lat: 53.0, lng: -1.0, [.e10: 139.9])]
XCTAssertNil(SiriCheapestLookup.cheapest(in: stations, fuel: .diesel, fromLat: 53.0, lng: -1.0))
let stations = [station("a", lat: nearLat, lng: nearLng, [.e10: 139.9])]
XCTAssertNil(SiriCheapestLookup.cheapest(in: stations, fuel: .diesel, fromLat: 53.01, lng: -1.01, withinMiles: 5))
}
func testCheapestReturnsNilForEmptyInput() {
XCTAssertNil(SiriCheapestLookup.cheapest(in: [], fuel: .e10, fromLat: 53.0, lng: -1.0))
XCTAssertNil(SiriCheapestLookup.cheapest(in: [], fuel: .e10, fromLat: 53.01, lng: -1.01, withinMiles: 5))
}
func testFreshnessLabelUsesDataUpdatedStamp() {