62729213d7
AA was silently returning nil because the server now rejects User-Agent
"Android/2025.31" with HTTP 403 ("Please update your version of the
American Airlines app"). Bumped to "2026.14" (matches the APK in
airlines/) and centralized to a constant so the next bump is one line.
Added comprehensive logging to fetchAmericanLoad (was zero) so the next
breakage won't be silent — including an explicit ⚠️ when the server
returns the "update your version" payload.
New FlightsTests target with AirlineLoadIntegrationTests — hits live
airline APIs to verify each fetcher still returns data. Per-airline
strategy:
- Try route-explorer /departures from carrier hubs for a flight in the
next 24h (works for AA/UA/AS/B6).
- Fall back to a known-good daily flight when route-explorer doesn't
have the carrier in its data (NK/EK/KE — ULCC + some intl carriers).
- B6/EK/NK are status-only by design (no standby data without a PNR);
asserted as non-nil only.
- XE (JSX) skipped: needs WKWebView host.
Retries on route-explorer 429 by parsing the `retryAfter` field and
sleeping the indicated number of seconds. Static-shared client+services
across tests so the token cache survives.
Results 2026-05-26 (xcodebuild test -scheme Flights):
✅ AA, AS, B6, EK, KE, UA ❌ NK ⏭️ XE
NK (Spirit) is now broken: GetFlightInfoBI returns HTTP 403 with
{"getFlightInfoBIResult":null}. APIM key still accepted (401 without
it), but the call itself is rejected. Documented in
AIRLINE_INTEGRATION_GUIDE.md as a known regression to fix; likely
needs reverse-engineering against the current Spirit APK in airlines/.
Also: enable shared schemes in .gitignore so `xcodebuild test` works
out of the box for anyone cloning the repo.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
261 lines
11 KiB
Swift
261 lines
11 KiB
Swift
import XCTest
|
|
@testable import Flights
|
|
|
|
/// Integration tests for the airline load fetchers in `AirlineLoadService`.
|
|
///
|
|
/// These tests hit **live airline APIs**. They will:
|
|
/// - Take 10-30s each (network)
|
|
/// - Fail loudly when an airline rotates auth, gates on a new app version,
|
|
/// or otherwise changes their API shape. That's by design — this is the
|
|
/// regression net for "does X airline still work?"
|
|
///
|
|
/// For each carrier, the test:
|
|
/// 1. Uses `RouteExplorerClient` to find a real flight on that carrier
|
|
/// departing within the next 24 hours from one of its hubs.
|
|
/// 2. Calls `AirlineLoadService.fetchLoad(...)` for that specific flight.
|
|
/// 3. Asserts the response is meaningful (non-nil and has at least one
|
|
/// of: cabins / standby list / upgrade list / seat availability).
|
|
///
|
|
/// Pre-existing limitations (NOT bugs in these tests):
|
|
/// - JSX (XE) uses a WKWebView path and can't run from unit tests on the
|
|
/// simulator without a host scene. Skipped with a `XCTSkip`.
|
|
/// - Spirit (NK) intentionally returns an empty FlightLoad — they have no
|
|
/// standby/load program. Test just asserts non-nil.
|
|
/// - Some carriers (notably AA, AS waitlist) only open the load endpoint
|
|
/// close to departure. Tests prefer flights leaving < 24h out and skip
|
|
/// with a helpful message if nothing's findable.
|
|
final class AirlineLoadIntegrationTests: XCTestCase {
|
|
|
|
// Static so the token cache + URLSession survive across tests in
|
|
// a single run, and so the route-explorer rate limit applies once
|
|
// per suite rather than per test.
|
|
private static let routeExplorer = RouteExplorerClient()
|
|
private static let airportDatabase = AirportDatabase()
|
|
private static let loadService = AirlineLoadService(airportDatabase: airportDatabase)
|
|
|
|
private var routeExplorer: RouteExplorerClient { Self.routeExplorer }
|
|
private var loadService: AirlineLoadService { Self.loadService }
|
|
|
|
/// Airlines whose load endpoint deliberately returns only flight
|
|
/// status (no seat/standby data). We assert non-nil for these and
|
|
/// stop short of the "must have data" check.
|
|
private static let statusOnlyAirlines: Set<String> = ["NK", "B6", "EK"]
|
|
|
|
/// Hardcoded daily flights used as fallbacks when route-explorer's
|
|
/// `/departures` data doesn't include the carrier we're looking for
|
|
/// (notably ULCCs like NK and some international carriers like EK/KE
|
|
/// that aren't in route-explorer's schedule feed). Each entry is a
|
|
/// well-known daily operation that's been stable over time; if any
|
|
/// of these stop operating, update the entry.
|
|
private static let knownDailyFlights: [String: (flightNumber: String, origin: String, destination: String)] = [
|
|
"NK": ("401", "LAS", "FLL"), // Spirit Las Vegas → Fort Lauderdale, daily
|
|
"EK": ("201", "JFK", "DXB"), // Emirates JFK → Dubai, daily flagship
|
|
"KE": ("82", "JFK", "ICN"), // Korean Air JFK → Incheon, daily
|
|
]
|
|
|
|
// MARK: - Per-airline tests
|
|
|
|
func test_AA_americanAirlines() async throws {
|
|
try await runAirlineLoadTest(
|
|
carrier: "AA",
|
|
hubs: ["DFW", "CLT", "PHL", "ORD", "MIA", "PHX"]
|
|
)
|
|
}
|
|
|
|
func test_UA_united() async throws {
|
|
try await runAirlineLoadTest(
|
|
carrier: "UA",
|
|
hubs: ["EWR", "IAH", "DEN", "ORD", "SFO", "IAD", "LAX"]
|
|
)
|
|
}
|
|
|
|
func test_AS_alaska() async throws {
|
|
try await runAirlineLoadTest(
|
|
carrier: "AS",
|
|
hubs: ["SEA", "PDX", "ANC", "SAN", "LAX"]
|
|
)
|
|
}
|
|
|
|
func test_B6_jetBlue() async throws {
|
|
try await runAirlineLoadTest(
|
|
carrier: "B6",
|
|
hubs: ["JFK", "BOS", "FLL", "MCO", "LAX"]
|
|
)
|
|
}
|
|
|
|
func test_NK_spirit() async throws {
|
|
try await runAirlineLoadTest(
|
|
carrier: "NK",
|
|
hubs: ["FLL", "MCO", "LAS", "DTW", "ORD"]
|
|
)
|
|
}
|
|
|
|
func test_KE_koreanAir() async throws {
|
|
try await runAirlineLoadTest(
|
|
carrier: "KE",
|
|
hubs: ["ICN", "LAX", "JFK", "SFO", "ATL"]
|
|
)
|
|
}
|
|
|
|
func test_EK_emirates() async throws {
|
|
try await runAirlineLoadTest(
|
|
carrier: "EK",
|
|
hubs: ["DXB", "JFK", "LAX", "ORD", "IAD", "SFO", "BOS"]
|
|
)
|
|
}
|
|
|
|
func test_XE_jsx() async throws {
|
|
// JSX uses a WKWebView path that needs a host scene / main thread.
|
|
// Skipped here; manual verification via the app remains.
|
|
throw XCTSkip("JSX uses WKWebView and cannot run from a unit-test bundle.")
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Pulls departures from `hubs` for `carrier`, picks the first flight
|
|
/// leaving in (now, now+24h), and runs the airline-specific fetcher.
|
|
/// XCTSkips (rather than fails) if no flight can be found at all —
|
|
/// that's a route-explorer / schedule problem, not a load-fetcher bug.
|
|
private func runAirlineLoadTest(
|
|
carrier: String,
|
|
hubs: [String],
|
|
file: StaticString = #file,
|
|
line: UInt = #line
|
|
) async throws {
|
|
let now = Date()
|
|
let cutoff = now.addingTimeInterval(24 * 3600)
|
|
|
|
var pickedFlight: RouteFlight?
|
|
var pickedHub: String?
|
|
|
|
for hub in hubs {
|
|
let candidate = await departuresWithRetry(from: hub, after: now, before: cutoff, carrier: carrier)
|
|
if let candidate {
|
|
pickedFlight = candidate
|
|
pickedHub = hub
|
|
break
|
|
}
|
|
}
|
|
|
|
// Fallback path: route-explorer didn't return any flights for
|
|
// this carrier (typical for ULCCs and some international ops).
|
|
// Use a known-good daily flight if we have one configured.
|
|
if pickedFlight == nil, let known = Self.knownDailyFlights[carrier] {
|
|
NSLog("[\(carrier)Test] No \(carrier) flight in route-explorer data; using known daily \(carrier)\(known.flightNumber) \(known.origin)→\(known.destination)")
|
|
let load = await loadService.fetchLoad(
|
|
airlineCode: carrier,
|
|
flightNumber: known.flightNumber,
|
|
date: Date(),
|
|
origin: known.origin,
|
|
destination: known.destination,
|
|
departureTime: nil
|
|
)
|
|
try assertLoad(load, carrier: carrier, flightLabel: "\(carrier)\(known.flightNumber) \(known.origin)→\(known.destination)", file: file, line: line)
|
|
return
|
|
}
|
|
|
|
guard let flight = pickedFlight, let hub = pickedHub else {
|
|
throw XCTSkip("Could not find a \(carrier) flight in the next 24h from any of: \(hubs.joined(separator: ", "))")
|
|
}
|
|
|
|
NSLog("[\(carrier)Test] Using \(carrier)\(flight.flightNumber) \(flight.departure.airportIata)→\(flight.arrival.airportIata) departing \(flight.departure.dateTime) (hub queried: \(hub))")
|
|
|
|
let load = await loadService.fetchLoad(
|
|
airlineCode: flight.carrierIata,
|
|
flightNumber: "\(flight.flightNumber)",
|
|
date: flight.departure.dateTime,
|
|
origin: flight.departure.airportIata,
|
|
destination: flight.arrival.airportIata,
|
|
departureTime: nil
|
|
)
|
|
|
|
let flightLabel = "\(carrier)\(flight.flightNumber) \(flight.departure.airportIata)→\(flight.arrival.airportIata) departing \(flight.departure.dateTime)"
|
|
try assertLoad(load, carrier: carrier, flightLabel: flightLabel, file: file, line: line)
|
|
}
|
|
|
|
/// Shared assertion path for both the dynamic-discovery and
|
|
/// hardcoded-fallback test routes.
|
|
private func assertLoad(
|
|
_ load: FlightLoad?,
|
|
carrier: String,
|
|
flightLabel: String,
|
|
file: StaticString,
|
|
line: UInt
|
|
) throws {
|
|
XCTAssertNotNil(
|
|
load,
|
|
"\(carrier) load fetcher returned nil for \(flightLabel). "
|
|
+ "Check the [\(carrier)] console logs above for the underlying failure mode.",
|
|
file: file,
|
|
line: line
|
|
)
|
|
|
|
guard let load else { return }
|
|
|
|
NSLog("[\(carrier)Test] ✅ cabins=\(load.cabins.count) standby=\(load.standbyList.count) upgrade=\(load.upgradeList.count) seatAvail=\(load.seatAvailability.count)")
|
|
|
|
if Self.statusOnlyAirlines.contains(carrier) {
|
|
XCTAssertEqual(load.airlineCode, carrier)
|
|
return
|
|
}
|
|
|
|
let hasAnyData = !load.cabins.isEmpty
|
|
|| !load.standbyList.isEmpty
|
|
|| !load.upgradeList.isEmpty
|
|
|| !load.seatAvailability.isEmpty
|
|
|
|
XCTAssertTrue(
|
|
hasAnyData,
|
|
"\(carrier) returned a FlightLoad but every collection is empty — "
|
|
+ "the endpoint likely succeeded but with no data for this flight, "
|
|
+ "or the response shape changed.",
|
|
file: file,
|
|
line: line
|
|
)
|
|
}
|
|
|
|
/// Fetch departures from `hub` and pick the first flight matching
|
|
/// `carrier` in the time window. On HTTP 429 (route-explorer rate
|
|
/// limit), parse `retryAfter` and retry once after that delay.
|
|
private func departuresWithRetry(
|
|
from hub: String,
|
|
after: Date,
|
|
before: Date,
|
|
carrier: String,
|
|
attemptsRemaining: Int = 2
|
|
) async -> RouteFlight? {
|
|
do {
|
|
let result = try await routeExplorer.searchDepartures(
|
|
from: hub, date: after, maxStops: 0, limit: 300
|
|
)
|
|
let allLegs = result.connections.flatMap { $0.flights }
|
|
let inWindow = allLegs.filter { $0.departure.dateTime > after && $0.departure.dateTime <= before }
|
|
let carrierMatches = inWindow.filter { $0.carrierIata == carrier }
|
|
NSLog("[\(carrier)Test] hub \(hub): legs=\(allLegs.count) inWindow=\(inWindow.count) \(carrier)Matches=\(carrierMatches.count)")
|
|
return carrierMatches.first
|
|
} catch let RouteExplorerClient.ClientError.requestFailed(status: 429, body: body) {
|
|
let retryAfter = parseRetryAfter(body: body) ?? 25
|
|
NSLog("[\(carrier)Test] hub \(hub) rate-limited (429), sleeping \(retryAfter)s then retrying (attemptsRemaining=\(attemptsRemaining - 1))")
|
|
if attemptsRemaining <= 1 { return nil }
|
|
try? await Task.sleep(nanoseconds: UInt64(retryAfter) * 1_000_000_000)
|
|
return await departuresWithRetry(from: hub, after: after, before: before, carrier: carrier, attemptsRemaining: attemptsRemaining - 1)
|
|
} catch let RouteExplorerClient.ClientError.tokenFetchFailed(status: 429) {
|
|
NSLog("[\(carrier)Test] hub \(hub) token rate-limited (429), sleeping 25s then retrying (attemptsRemaining=\(attemptsRemaining - 1))")
|
|
if attemptsRemaining <= 1 { return nil }
|
|
try? await Task.sleep(nanoseconds: 25 * 1_000_000_000)
|
|
return await departuresWithRetry(from: hub, after: after, before: before, carrier: carrier, attemptsRemaining: attemptsRemaining - 1)
|
|
} catch {
|
|
NSLog("[\(carrier)Test] hub \(hub) lookup failed: \(error)")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
private func parseRetryAfter(body: String?) -> Int? {
|
|
guard let body, let data = body.data(using: .utf8) else { return nil }
|
|
if let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
|
|
return json["retryAfter"] as? Int
|
|
}
|
|
return nil
|
|
}
|
|
}
|