1 # frozen_string_literal: true
 
   3 require "application_system_test_case"
 
   5 class QueryFeaturesSystemTest < ApplicationSystemTestCase
 
   6   test "sorts enclosing features correctly" do
 
   7     visit "/#map=15/54.18315/7.88473"
 
  10       click_on "Query features"
 
  12       stub_overpass :enclosing_elements => [
 
  17             "minlat" => 47.2701114,
 
  18             "minlon" => 5.8663153,
 
  19             "maxlat" => 55.0991610,
 
  20             "maxlon" => 15.0419309
 
  24             "border_type" => "country",
 
  25             "boundary" => "administrative",
 
  26             "name" => "Deutschland",
 
  27             "name:de" => "Deutschland",
 
  28             "name:en" => "Germany",
 
  36             "minlat" => 53.3598160,
 
  37             "minlon" => 7.5211615,
 
  38             "maxlat" => 55.0991610,
 
  39             "maxlon" => 11.6723860
 
  43             "border_type" => "state",
 
  44             "boundary" => "administrative",
 
  45             "name" => "Schleswig-Holstein",
 
  46             "name:de" => "Schleswig-Holstein",
 
  47             "name:en" => "Schleswig-Holstein",
 
  55             "minlat" => 54.1693099,
 
  56             "minlon" => 7.8648436,
 
  57             "maxlat" => 54.1907839,
 
  61             "name" => "Helgoland",
 
  62             "name:de" => "Helgoland",
 
  63             "name:en" => "Heligoland",
 
  73       assert_link "Heligoland"
 
  74       assert_link "Schleswig-Holstein", :below => find_link("Heligoland")
 
  75       assert_link "Germany", :below => find_link("Schleswig-Holstein")
 
  79   test "sorts enclosing features correctly across antimeridian" do
 
  80     visit "/#map=15/60/30"
 
  83       click_on "Query features"
 
  85       stub_overpass :enclosing_elements => [
 
  90             "minlat" => 41.1850968,
 
  91             "minlon" => 19.4041722,
 
  92             "maxlat" => 82.0586232,
 
  93             "maxlon" => -168.9769440
 
  97             "border_type" => "nation",
 
  98             "boundary" => "administrative",
 
 100             "name:en" => "Russia",
 
 101             "name:ru" => "Россия",
 
 106           "type" => "relation",
 
 109             "minlat" => 59.8587853,
 
 110             "minlon" => 29.6720697,
 
 111             "maxlat" => 60.0370554,
 
 112             "maxlon" => 30.2307057
 
 115             "name" => "Невская губа",
 
 116             "name:en" => "Neva Bay",
 
 117             "name:ru" => "Невская губа",
 
 119             "type" => "multipolygon"
 
 128       assert_link "Neva Bay"
 
 129       assert_link "Russia", :below => find_link("Neva Bay")
 
 133   test "sorts enclosing features correctly with multiple bboxes across antimeridian" do
 
 134     visit "/#map=15/-16.155/179.995"
 
 137       click_on "Query features"
 
 139       stub_overpass :enclosing_elements => [
 
 141           "type" => "relation",
 
 144             "minlat" => -21.9434274,
 
 145             "minlon" => 174.4214965,
 
 146             "maxlat" => -12.2613866,
 
 147             "maxlon" => -178.0034928
 
 150             "admin_level" => "2",
 
 151             "boundary" => "administrative",
 
 159           "type" => "relation",
 
 162             "minlat" => -17.0160140,
 
 163             "minlon" => 178.4754914,
 
 164             "maxlat" => -16.1243512,
 
 165             "maxlon" => -179.6630100
 
 168             "name" => "Vanua Levu Group",
 
 169             "place" => "archipelago",
 
 170             "type" => "multipolygon",
 
 171             "wikidata" => "Q2756586"
 
 175           "type" => "relation",
 
 178             "minlat" => -17.0160140,
 
 179             "minlon" => 178.4754914,
 
 180             "maxlat" => -16.1243512,
 
 181             "maxlon" => -179.9513876
 
 184             "name" => "Vanua Levu",
 
 186             "type" => "multipolygon",
 
 187             "wikidata" => "Q327733"
 
 196       assert_link "Vanua Levu"
 
 197       assert_link "Vanua Levu Group", :below => find_link("Vanua Levu")
 
 198       assert_link "Fiji", :below => find_link("Vanua Levu Group")
 
 204   def stub_overpass(nearby_elements: [], enclosing_elements: [])
 
 205     execute_script <<~SCRIPT
 
 207         const originalFetch = fetch;
 
 208         window.fetch = (...args) => {
 
 209           const [resource, options] = args;
 
 211           if (resource != OSM.OVERPASS_URL) return originalFetch(...args);
 
 213           const data = options.body.get("data"),
 
 214                 elements = data.includes("is_in") ? #{enclosing_elements.to_json} : #{nearby_elements.to_json};
 
 216           return Promise.resolve({
 
 217             json: () => Promise.resolve({ elements })