Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

fix: decode legroom ratings from seat quality instead of Wi-Fi (#232)

davefmurrayc005d705f465
diff --git a/fli-js/src/search/decoders.ts b/fli-js/src/search/decoders.ts
index fe911e8..fac86a8 100644
--- a/fli-js/src/search/decoders.ts
+++ b/fli-js/src/search/decoders.ts
@@ -71,12 +71,12 @@ function safeAirline(code: unknown): Airline | null {
return null;
}
-function parseAmenities(slots: unknown): Amenities | null {
- if (!Array.isArray(slots) || slots.length === 0) return null;
+function parseAmenities(slots: unknown, seatQuality: unknown = null): Amenities | null {
const wifi = asBool(safeGet(slots, 1));
const power = asBool(safeGet(slots, 5));
const onDemandVideo = asBool(safeGet(slots, 9));
- const legroomRating = asNonNegativeInt(safeGet(slots, 11));
+ // leg[12][11] is the Wi-Fi tier; seat quality lives separately at leg[13].
+ const legroomRating = asNonNegativeInt(seatQuality);
if (wifi == null && power == null && onDemandVideo == null && legroomRating == null) {
return null;
}
@@ -121,7 +121,7 @@ function parseLeg(fl: unknown[]): FlightLeg {
const opCode = safeGet(airlineInfo, 2);
const operatingAirline = opCode ? safeAirline(opCode) : null;
- const amenities = parseAmenities(safeGet(fl, 12));
+ const amenities = parseAmenities(safeGet(fl, 12), safeGet(fl, 13));
const aircraft = asStr(safeGet(fl, 17));
const legroomShort = asStr(safeGet(fl, 14));
const legroomLong = asStr(safeGet(fl, 30));
diff --git a/fli-js/tests/search/legroom_rating.test.ts b/fli-js/tests/search/legroom_rating.test.ts
new file mode 100644
index 0000000..3b82ba1
--- /dev/null
+++ b/fli-js/tests/search/legroom_rating.test.ts
@@ -0,0 +1,68 @@
+import { describe, expect, test } from "bun:test";
+import { readFileSync } from "node:fs";
+import { parseFlightRow } from "../../src/search/decoders.ts";
+
+const fixture = readFileSync(
+ new URL("../../../tests/search/fixtures/flight_search_jfk_lax_oneway_usd.bin", import.meta.url),
+ "utf8",
+);
+const outer = JSON.parse(fixture.slice(fixture.indexOf("[")));
+const inner = JSON.parse(outer[0][2]);
+const rows: unknown[][] = [2, 3].flatMap((i) => (Array.isArray(inner[i]) ? inner[i][0] : []));
+
+function rowWithRating(rating: unknown, slots: unknown): unknown[] {
+ const row = structuredClone(rows[0]) as unknown[];
+ const detail = row[0] as unknown[];
+ const leg = (detail[2] as unknown[][])[0] as unknown[];
+ leg[12] = slots;
+ leg[13] = rating;
+ return row;
+}
+
+describe("legroom rating", () => {
+ test("captured flights with the same Wi-Fi tier have different seat ratings", () => {
+ const legs = rows.flatMap((row) => parseFlightRow(row).legs);
+ const jetblue = legs.find((leg) => leg.airline === "B6" && leg.flight_number === "123");
+ const american = legs.find((leg) => leg.airline === "AA" && leg.flight_number === "300");
+ expect(jetblue?.amenities?.legroom_rating).toBe(3);
+ expect(american?.amenities?.legroom_rating).toBe(1);
+ });
+
+ for (const tier of [2, 3]) {
+ test.each([
+ 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ ])(`reads seat rating %d with Wi-Fi tier ${tier}`, (rating) => {
+ const slots: unknown[] = Array.from({ length: 12 }, () => null);
+ slots[11] = tier;
+ const flight = parseFlightRow(rowWithRating(rating, slots));
+ expect(flight.legs[0]?.amenities?.legroom_rating).toBe(rating);
+ });
+ }
+
+ test.each<[unknown]>([
+ [null],
+ [[]],
+ ["invalid"],
+ ])("preserves rating without amenities array: %j", (slots) => {
+ const flight = parseFlightRow(rowWithRating(3, slots));
+ expect(flight.legs[0]?.amenities?.legroom_rating).toBe(3);
+ expect(flight.legs[0]?.amenities?.wifi).toBeNull();
+ });
+
+ test.each([
+ null,
+ -1,
+ true,
+ "3",
+ ])("does not fall back to Wi-Fi for invalid rating: %j", (rating) => {
+ const slots: unknown[] = Array.from({ length: 12 }, () => null);
+ slots[1] = true;
+ slots[11] = 2;
+ const flight = parseFlightRow(rowWithRating(rating, slots));
+ expect(flight.legs[0]?.amenities?.legroom_rating).toBeNull();
+ });
+
+ test("returns no amenities when neither source has data", () => {
+ expect(parseFlightRow(rowWithRating(null, null)).legs[0]?.amenities).toBeNull();
+ });
+});
diff --git a/fli/models/google_flights/base.py b/fli/models/google_flights/base.py
index 10117c6..32122eb 100644
--- a/fli/models/google_flights/base.py
+++ b/fli/models/google_flights/base.py
@@ -235,8 +235,10 @@ class LayoverRestrictions(BaseModel):
class Amenities(BaseModel):
"""Per-leg amenities reported by Google Flights.
- All fields are tri-state (`True`, `False`, or `None` when Google did not
- publish that signal for the leg).
+ Boolean fields are tri-state (`True`, `False`, or `None` when Google did
+ not publish that signal). ``legroom_rating`` is Google's seat-quality
+ code from ``leg[13]``, or ``None`` when unavailable; it is not an ordered
+ numeric score or the Wi-Fi tier.
"""
wifi: bool | None = None
diff --git a/fli/search/_decoders.py b/fli/search/_decoders.py
index 481c557..5a3b96e 100644
--- a/fli/search/_decoders.py
+++ b/fli/search/_decoders.py
@@ -85,7 +85,7 @@ def _parse_leg(fl: list) -> FlightLeg:
op_code = safe_get(airline_info, 2)
operating_airline = _safe_airline(op_code) if op_code else None
- amenities = _parse_amenities(safe_get(fl, 12))
+ amenities = _parse_amenities(safe_get(fl, 12), safe_get(fl, 13))
aircraft = as_str(safe_get(fl, 17))
legroom_short = as_str(safe_get(fl, 14))
legroom_long = as_str(safe_get(fl, 30))
@@ -113,26 +113,18 @@ def _parse_leg(fl: list) -> FlightLeg:
)
-def _parse_amenities(slots: Any) -> Amenities | None:
- """Decode the 12-slot amenities array at ``leg[12]``.
+def _parse_amenities(slots: Any, seat_quality: Any = None) -> Amenities | None:
+ """Decode amenities at ``leg[12]`` and the seat-quality code at ``leg[13]``.
- Confirmed slot mapping (live captures, May 2026):
+ ``leg[12][11]`` is the Wi-Fi tier, not a legroom rating. Seat quality
+ may be present even when the amenities array is missing.
- - slot 1 → wifi (bool|None)
- - slot 5 → power outlet (bool|None)
- - slot 9 → on-demand video (bool|None)
- - slot 11 → integer legroom rating (2 or 3 observed)
-
- Returns None when none of the known slots carry a usable value (avoids
- creating empty ``Amenities`` instances that would imply we know nothing
- about the leg).
+ Return None when neither source carries a usable value.
"""
- if not isinstance(slots, list) or not slots:
- return None
wifi = as_bool(safe_get(slots, 1))
power = as_bool(safe_get(slots, 5))
on_demand_video = as_bool(safe_get(slots, 9))
- legroom_rating = as_non_negative_int(safe_get(slots, 11))
+ legroom_rating = as_non_negative_int(seat_quality)
if wifi is None and power is None and on_demand_video is None and legroom_rating is None:
return None
return Amenities(
diff --git a/tests/search/test_parse_flights_data.py b/tests/search/test_parse_flights_data.py
index 0de2475..67e9d9d 100644
--- a/tests/search/test_parse_flights_data.py
+++ b/tests/search/test_parse_flights_data.py
@@ -6,6 +6,8 @@ them as in-test constants avoids large committed binary blobs while still
locking down the parser positions we depend on.
"""
+import pytest
+
from fli.models import Airline, Airport
from fli.search.flights import SearchFlights
@@ -32,6 +34,7 @@ def _leg(
cabin=2,
overnight=False,
amenities=None,
+ legroom_rating=None,
):
amenities = amenities or [None] * 12
leg = [None] * 33
@@ -43,6 +46,7 @@ def _leg(
leg[10] = list(arr_time)
leg[11] = duration
leg[12] = amenities
+ leg[13] = legroom_rating
leg[14] = legroom_short
leg[17] = aircraft
leg[19] = overnight
@@ -112,6 +116,7 @@ class TestParseFlightsDataNonStop:
dep_iata="JFK",
arr_iata="LAX",
amenities=[None, True, None, None, None, True, None, None, None, True, None, 2],
+ legroom_rating=3,
)
],
)
@@ -147,7 +152,7 @@ class TestParseFlightsDataNonStop:
assert am.wifi is True
assert am.power is True
assert am.on_demand_video is True
- assert am.legroom_rating == 2
+ assert am.legroom_rating == 3
# USB/in-seat-video slots not yet disambiguated — left as None to
# avoid lying about what we know.
assert am.usb_power is None
@@ -170,6 +175,52 @@ class TestParseFlightsDataNonStop:
assert self.flight.booking_token == "CAISA1VTRBoDCNR/sample"
+class TestLegroomRating:
+ @pytest.mark.parametrize("rating", range(1, 10))
+ @pytest.mark.parametrize("wifi_tier", [2, 3])
+ def test_reads_seat_quality_independently_of_wifi(self, rating, wifi_tier):
+ amenities = [None] * 12
+ amenities[11] = wifi_tier
+ row = _row(
+ legs=[
+ _leg(
+ dep_iata="JFK",
+ arr_iata="LAX",
+ amenities=amenities,
+ legroom_rating=rating,
+ )
+ ]
+ )
+ flight = SearchFlights._parse_flights_data(row)
+ assert flight.legs[0].amenities.legroom_rating == rating
+
+ @pytest.mark.parametrize("slots", [None, [], "invalid"])
+ def test_preserves_rating_without_amenities_array(self, slots):
+ leg = _leg(dep_iata="JFK", arr_iata="LAX", legroom_rating=3)
+ leg[12] = slots
+ flight = SearchFlights._parse_flights_data(_row(legs=[leg]))
+ assert flight.legs[0].amenities.legroom_rating == 3
+ assert flight.legs[0].amenities.wifi is None
+
+ @pytest.mark.parametrize("rating", [None, -1, True, "3"])
+ def test_missing_or_invalid_rating_does_not_fall_back_to_wifi(self, rating):
+ amenities = [None] * 12
+ amenities[1] = True
+ amenities[11] = 2
+ row = _row(
+ legs=[
+ _leg(
+ dep_iata="JFK",
+ arr_iata="LAX",
+ amenities=amenities,
+ legroom_rating=rating,
+ )
+ ]
+ )
+ flight = SearchFlights._parse_flights_data(row)
+ assert flight.legs[0].amenities.legroom_rating is None
+
+
class TestParseFlightsDataLayover:
"""A multi-leg trip derives layover info from leg timestamps."""
diff --git a/tests/search/test_snapshot_fixtures.py b/tests/search/test_snapshot_fixtures.py
index 6e32ef8..4d4a7f7 100644
--- a/tests/search/test_snapshot_fixtures.py
+++ b/tests/search/test_snapshot_fixtures.py
@@ -184,6 +184,19 @@ class TestSnapshotFlightShape:
with_ac = [leg for f in jfk_lax_oneway for leg in f.legs if leg.aircraft]
assert with_ac, "Expected aircraft set on at least one leg"
+ def test_seat_ratings_differ_on_flights_with_the_same_wifi_tier(self, jfk_lax_oneway):
+ # Captured B6 123 and AA 300 both have Wi-Fi tier 2 at leg[12][11],
+ # but their seat-quality codes at leg[13] are 3 and 1 respectively.
+ legs = [leg for flight in jfk_lax_oneway for leg in flight.legs]
+ jetblue = next(
+ leg for leg in legs if leg.airline.name == "B6" and leg.flight_number == "123"
+ )
+ american = next(
+ leg for leg in legs if leg.airline.name == "AA" and leg.flight_number == "300"
+ )
+ assert jetblue.amenities.legroom_rating == 3
+ assert american.amenities.legroom_rating == 1
+
class TestSnapshotFilterEnforcement:
"""The captured filter responses should reflect the filter applied."""