Files
rickyandClaude Opus 5.5 5bf682502b AirTrail sync and export, GPS tracks, airport database with autocomplete
AirTrail (https://github.com/johanohly/AirTrail):
- File export (Flights → Export for AirTrail) in AirTrail's import
  format, validated against its schema; same-day repeat legs get leg
  numbers so AirTrail's de-duplication doesn't merge them.
- Automatic sync through AirTrail's REST API (Profile → AirTrail sync):
  per-user connection with an encrypted API key and scope check; flights
  are created/updated/deleted in background jobs; a full sync first
  links flights already in AirTrail (e.g. from a file import) instead of
  duplicating them; airports are resolved and cached, with manual
  mapping for unknown codes.
- GPS tracks: GPX, KML and CSV track logs are converted to AirTrail's
  track format for both sync and export; the flight page shows a summary.
- Aircraft ICAO type (e.g. C150) for AirTrail's aircraft type.

Airport database (OurAirports, the source AirTrail uses):
- Managed like AirTrail: downloaded in the background on first start,
  updated in place from Admin → Airports (adds, changes, removes; ids
  stable), with admin-managed custom airports that updates never touch.
- Each airport carries AirTrail's code, so logged codes translate to
  AirTrail's exactly (1B9 → K1B9, 4MB → K5M3).
- From/To autocomplete on new flights (airports this aircraft has used
  rank first); From is pre-filled with the last flight's destination;
  codes are normalized to uppercase.
- Flights → Standardize airport codes: rewrite logged codes to the
  standard code, and fix codes not in the database (typos such as 0R8 →
  08R are suggested).

Also: admin pages grouped under an Admin dropdown menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 19:40:20 -05:00

53 lines
2.7 KiB
Ruby

require "test_helper"
class AirportCodeStandardizerTest < ActiveSupport::TestCase
setup do
AirportData.sync!(StringIO.new(<<~CSV))
ident,type,name,latitude_deg,longitude_deg,elevation_ft,iso_country,iso_region,municipality,gps_code,iata_code,local_code
KOQU,medium_airport,Quonset State Airport,41.6,-71.4,18,US,US-RI,North Kingstown,KOQU,NCO,OQU
K1B9,small_airport,Mansfield Municipal Airport,42,-71.2,122,US,US-MA,Mansfield,1B9,,1B9
K5M3,small_airport,Moultonborough Airport,43.8,-71.4,571,US,US-NH,Moultonborough,,,4MB
08R,small_airport,Richmond Airport,41.5,-71.6,90,US,US-RI,West Kingston,,,08R
KZZZ,closed,Old Field,40,-70,0,US,US-RI,Nowhere,,,ZZZ
CSV
@a = create_aircraft
other = create_aircraft
fly = ->(ac, from, to) { ac.flight_logs.create!(date: Date.current, tach_time: 1, departure_location: from, destination_location: to) }
fly.(@a, "KOQU", "1B9")
fly.(@a, "1B9", "4MB")
fly.(@a, "4MB", "0R8")
fly.(@a, "08R", "ZZZ")
fly.(other, "1B9", "KOQU") # another aircraft: untouched
@s = AirportCodeStandardizer.new(@a)
end
test "previews each logged code against the standard code" do
by_code = @s.changes.index_by(&:from)
assert_equal %w[1B9 4MB ZZZ], by_code.keys.sort # KOQU and 08R already standard
assert_equal [ "K1B9", 2, true, nil ], by_code["1B9"].to_h.values_at(:to, :flights, :suggested, :note)
assert_match "Different identifier", by_code["4MB"].note
assert_equal false, by_code["ZZZ"].suggested # closed airport: not pre-selected
assert_equal [ "0R8", 1, "08R" ], @s.unknown.first.to_h.values_at(:code, :flights).push(@s.unknown.first.suggestion&.code)
end
test "applies only the chosen codes, to this aircraft only" do
assert_equal 4, @s.apply!(%w[1B9 4MB]) # 1B9 twice, 4MB twice
assert_equal [ %w[KOQU K1B9], %w[K1B9 K5M3], %w[K5M3 0R8], %w[08R ZZZ] ],
@a.flight_logs.order(:id).pluck(:departure_location, :destination_location)
assert_equal "1B9", FlightLog.where.not(aircraft: @a).first.departure_location
assert_match "1B9→K1B9", @a.events.first.notes
assert_equal %w[ZZZ], AirportCodeStandardizer.new(@a).changes.map(&:from)
end
test "fixes unknown codes (typos) to a chosen airport's standard code" do
assert_equal 1, @s.apply!([], fixes: { "0R8" => "08R", "KOQU" => "1B9", "XXXX" => "1B9" }) # only real unknown codes
assert_equal %w[4MB 08R], @a.flight_logs.order(:id).third.slice(:departure_location, :destination_location).values
assert_empty AirportCodeStandardizer.new(@a).unknown
assert_equal "KOQU", @a.flight_logs.order(:id).first.departure_location
end
test "a fix to an airport not in the database is ignored" do
assert_equal 0, @s.apply!([], fixes: { "0R8" => "NOPE" })
end
end