diff --git a/app/services/ad_watch.rb b/app/services/ad_watch.rb index ce9643d..fae423a 100644 --- a/app/services/ad_watch.rb +++ b/app/services/ad_watch.rb @@ -19,9 +19,17 @@ class AdWatch # Model names to search for: the owner's list, or the aircraft, engine and # propeller models. def self.default_terms(aircraft) - models = [ aircraft.model ] + aircraft.components.in_use.includes(:component_type) - .select { _1.component_type&.name.to_s.match?(/engine|propell?er|\bprop\b/i) }.map(&:model) - models.compact_blank.map { _1.strip }.uniq + engines = aircraft.components.in_use.includes(:component_type) + .select { _1.component_type&.name.to_s.match?(/engine|propell?er|\bprop\b/i) }.map(&:model).compact_blank.map(&:strip) + ([ aircraft.model.to_s.strip ] + engines.flat_map { [ _1, model_family(_1) ] }).compact_blank.uniq + end + + # The engine family ADs often name instead of the exact model, and that also + # catches a different spelling of the suffix: "O-200A" and "O-200-A" -> "O-200", + # "IO-550-N" -> "IO-550". Nil when there's no family to add. + def self.model_family(model) + family = model.to_s.upcase[/\A([A-Z]{1,5}-\d{3,4})(?=-?[A-Z0-9])/, 1] + family unless family.nil? || family == model.to_s.upcase end def terms diff --git a/test/integration/group_four_test.rb b/test/integration/group_four_test.rb index b4e9c89..d29b58d 100644 --- a/test/integration/group_four_test.rb +++ b/test/integration/group_four_test.rb @@ -167,7 +167,11 @@ class GroupFourTest < ActionDispatch::IntegrationTest test "newly published ADs are found, tracked or dismissed, and reported weekly" do create_component(@a, component_type: component_type("Engine"), model: "O-320-H2AD") - assert_equal [ "172N", "O-320-H2AD" ], AdWatch.default_terms(@a) + assert_equal [ "172N", "O-320-H2AD", "O-320" ], AdWatch.default_terms(@a) # engine family too + assert_equal "O-200", AdWatch.model_family("O-200A") + assert_equal "IO-550", AdWatch.model_family("IO-550-N") + assert_nil AdWatch.model_family("O-200") + assert_nil AdWatch.model_family("1A102/OCM6948") known = AirworthinessDirective.create!(name: "2020-01-01", short_description: "Already tracked") known.aircraft << @a stub = stub_request(:get, %r{\Ahttps://www\.federalregister\.gov/api/v1/documents\.json}).to_return(status: 200, body: FR_RESPONSE) @@ -175,7 +179,7 @@ class GroupFourTest < ActionDispatch::IntegrationTest sign_in @owner post check_aircraft_ad_alerts_path(@a) assert_redirected_to aircraft_airworthiness_directives_path(@a, anchor: "new-ads") - assert_requested stub, times: 2 # one search per term + assert_requested stub, times: 3 # one search per term (model, engine model, engine family) alert = @a.ad_alerts.sole # already-tracked and non-AD documents skipped assert_equal [ "2026-17-10", "39-23454", "new" ], [ alert.ad_number, alert.amendment, alert.status ] get aircraft_airworthiness_directives_path(@a)