require_relative "test_helper" require_relative "../lib/maptool/gnss_sim" class GnssSimTest < Minitest::Test # A 1 km route north, 36 km/h limit for the first half, untagged after. class FixedRoute < Maptool::GnssSim def random_route(_position) { geometry: { coordinates: [[17.0, 48.0], [17.0, 48.0 + 1000.0 / 111_320]] }, properties: { speed_limits: [[0.0, 40], [0.5, nil]] } } end end def test_drives_the_route_at_its_limits_and_ends_at_its_end fixes = [] sim = FixedRoute.new("http://valhalla", sleep: ->(_s) {}) last = sim.drive([17.0, 48.0]) { |fix| fixes << fix } assert_in_delta 36 / 3.6, fixes.first[:speed], 0.01 # 90 % of 40 assert_in_delta 45 / 3.6, fixes.last[:speed], 0.01 # 90 % of an untagged road's 50 assert_in_delta 48.0 + 1000.0 / 111_320, fixes.last[:lat], 1e-9 assert_equal [17.0, 48.0 + 1000.0 / 111_320], last assert fixes.each_cons(2).all? { |a, b| b[:lat] >= a[:lat] } # never backwards assert_in_delta (1000 / (36 / 3.6) / 2 + 500 / (45 / 3.6)) / Maptool::GnssSim::TICK_S, fixes.size, 3 # a fix every TICK_S end # A track replayed at its own pace: 100 m in 10 s (10 m/s), a stop of an # hour cut to MAX_GAP_S, another 100 m in 20 s; ends at its end. def test_replays_a_track_at_its_own_speed fixes = [] step = 100.0 / 111_320 track = [[17.0, 48.0, 1000], [17.0, 48.0 + step, 1010], [17.0, 48.0 + step, 4610], [17.0, 48.0 + 2 * step, 4630]] Maptool::GnssSim.new("http://valhalla", sleep: ->(_s) {}).replay(track) { |fix| fixes << fix } assert_in_delta 10.0, fixes.first[:speed], 0.01 assert_in_delta 5.0, fixes.last[:speed], 0.01 assert_in_delta 48.0 + 2 * step, fixes.last[:lat], 1e-9 assert_in_delta (10 + Maptool::GnssSim::MAX_GAP_S + 20) / Maptool::GnssSim::TICK_S, fixes.size, 2 end # Started with the trip's route: from its start along it, before any random trip. def test_starts_along_a_given_route fixes = Queue.new sim = Maptool::GnssSim.new("http://valhalla", sleep: ->(_s) {}, router: ->(*) { raise Maptool::Routing::RouteError, "done" }) route = { geometry: { coordinates: [[18.0, 49.0], [18.0, 49.001]] }, properties: { speed_limits: [[0.0, 50]] } } sim.start(route: route) { |fix| fixes << fix } first = fixes.pop assert_in_delta 49.0, first[:lat], 0.0001 assert_in_delta 18.0, first[:lon], 1e-9 ensure sim&.stop end # Sped up: five times the ground per tick, the fixes' clock five times # faster — the speed they report (and a recorded track's) stays real. def test_speedup_covers_more_ground_on_a_faster_clock times = ->(speedup) do sim = FixedRoute.new("http://valhalla", sleep: ->(_s) {}) sim.speedup = speedup fixes = [] sim.drive([17.0, 48.0]) { |fix| fixes << fix } fixes end real, fast = times.(1), times.(5) assert_in_delta real.size / 5.0, fast.size, 2 assert_in_delta real.last[:time] - real.first[:time], fast.last[:time] - fast.first[:time], 5 # the same simulated time assert_equal real.first[:speed], fast.first[:speed] sim = Maptool::GnssSim.new("http://valhalla") sim.speedup = 7 assert_equal 1, sim.speedup # not one of SPEEDUPS end # A long route gets detours: a stretch replaced by the router's way through # a point off to one side (here: straight there and back), the start and the # end kept, the limits in order; a detour with no way keeps the route. def test_a_route_gets_detours_off_to_the_side asked = [] router = lambda do |_url, (a, via, b), _vehicle| asked << via raise Maptool::Routing::RouteError, "no way" if asked.size == 2 { geometry: { coordinates: [a, via, b] }, properties: { speed_limits: [[0.0, 30]] } } end sim = Maptool::GnssSim.new("http://valhalla", random: Random.new(1), router: router) line = [[17.0, 48.0], [17.3, 48.0]] # ~22 km east route = sim.with_detours({ geometry: { coordinates: line }, properties: { speed_limits: [[0.0, 90]] } }) coords = route[:geometry][:coordinates] assert_equal [line.first, line.last], [coords.first, coords.last] assert_operator asked.size, :>=, 2 off = asked.map { |_lon, lat| (lat - 48.0).abs * 111_320 } assert(off.all? { |m| m.between?(250, 850) }, "#{off} m off the route") assert_includes coords, asked.first # the first detour driven through its point refute_includes coords, asked[1] # the second had no way: the route as it was starts = route[:properties][:speed_limits].map(&:first) assert_equal starts.sort, starts assert_includes route[:properties][:speed_limits].map(&:last), 30 end # No road at the first random place: another one, inside the bbox. class FirstPlaceAtSea < Maptool::GnssSim attr_reader :tried def random_route(position) (@tried ||= []) << position raise Maptool::Routing::RouteError, "no road" if @tried.size == 1 { geometry: { coordinates: [position, position] }, properties: { distance_km: 5 } } end end def test_a_random_start_is_inside_an_offline_country_with_a_road sim = FirstPlaceAtSea.new("http://valhalla", random: Random.new(3)) position, route = sim.random_start([[17.0, 48.0, 18.0, 49.0]]) assert_equal 2, sim.tried.size assert position[0].between?(17.0, 18.0) && position[1].between?(48.0, 49.0) assert_equal position, route[:geometry][:coordinates].first end def test_a_route_under_a_km_is_another_try_and_none_at_all_an_error lengths = [0.0, 0.2, 3.0] router = ->(*) { { geometry: { coordinates: [[1, 2], [1, 3]] }, properties: { distance_km: lengths.shift } } } sim = Maptool::GnssSim.new("http://valhalla", router: router) assert_equal 3.0, sim.random_route([1, 2])[:properties][:distance_km] lengths = [0.0] * 5 assert_raises(Maptool::Routing::RouteError) { sim.random_route([1, 2]) } end end