219 lines
12 KiB
Ruby
219 lines
12 KiB
Ruby
require_relative "test_helper"
|
|
require "maptool/routing"
|
|
require "socket"
|
|
|
|
class RoutingTest < Minitest::Test
|
|
def setup
|
|
@data = Dir.mktmpdir
|
|
@routing = File.join(@data, "routing")
|
|
FileUtils.mkdir_p([@routing, File.join(@data, "geofabrik_cache")])
|
|
Maptool::PmtilesBuilder.set_selected_regions(@data, %w[SK])
|
|
end
|
|
|
|
def teardown
|
|
FileUtils.remove_entry(@data)
|
|
Maptool::PmtilesBuilder.reset!
|
|
Maptool::Routing.reset!
|
|
end
|
|
|
|
def test_a_build_needs_the_extracts_and_writes_a_request_for_the_container
|
|
assert_raises(Maptool::PmtilesBuilder::BuildError) { Maptool::Routing.request_build(@data) }
|
|
|
|
File.write(File.join(@data, "geofabrik_cache", "SK.osm.pbf"), "pbf")
|
|
Maptool::Routing.request_build(@data)
|
|
assert_equal %w[SK], JSON.parse(File.read(File.join(@routing, "build-request.json")))["regions"]
|
|
assert_equal "waiting for the routing container", Maptool::Routing.build_status(@data)[:step]
|
|
end
|
|
|
|
def test_a_request_while_the_offline_build_downloads_waits_for_the_extracts_only
|
|
Maptool::PmtilesBuilder.instance_variable_set(:@fetching_extracts, true)
|
|
Maptool::Routing.request_build(@data)
|
|
assert_equal "waiting for the offline map downloads", Maptool::Routing.build_status(@data)[:step]
|
|
refute File.exist?(File.join(@routing, "build-request.json"))
|
|
|
|
# Planetiler still running doesn't matter, the extracts are in.
|
|
File.write(File.join(@data, "geofabrik_cache", "SK.osm.pbf"), "pbf")
|
|
Maptool::PmtilesBuilder::OFFLINE_STATUS.replace(state: "running")
|
|
Maptool::PmtilesBuilder.send(:extracts_ready, @data)
|
|
assert File.exist?(File.join(@routing, "build-request.json"))
|
|
refute Maptool::PmtilesBuilder.fetching_extracts?
|
|
end
|
|
|
|
def test_update_check_compares_regions_and_extract_age
|
|
assert_nil Maptool::Routing.update_available?(@data)
|
|
|
|
pbf = File.join(@data, "geofabrik_cache", "SK.osm.pbf")
|
|
File.write(pbf, "pbf")
|
|
File.utime(Time.now - 60, Time.now - 60, pbf)
|
|
File.write(File.join(@routing, "tiles.tar"), "tiles")
|
|
File.write(File.join(@routing, "built.json"), JSON.generate(regions: %w[SK]))
|
|
refute Maptool::Routing.update_available?(@data)
|
|
|
|
File.utime(Time.now + 60, Time.now + 60, pbf)
|
|
assert Maptool::Routing.update_available?(@data)
|
|
Maptool::PmtilesBuilder.set_selected_regions(@data, %w[SK CZ])
|
|
assert Maptool::Routing.update_available?(@data)
|
|
assert_equal [{ code: "SK" }], Maptool::Routing.tiles_status(@data, "")[:meta][:sources]
|
|
end
|
|
|
|
def test_the_container_status_file_is_passed_through
|
|
assert_equal({ state: "idle" }, Maptool::Routing.build_status(@data))
|
|
File.write(File.join(@routing, "status.json"), JSON.generate(state: "running", step: "packing tiles"))
|
|
assert_equal "packing tiles", Maptool::Routing.build_status(@data)[:step]
|
|
end
|
|
|
|
def test_apply_during_a_build_of_the_same_regions_queues_nothing
|
|
File.write(File.join(@data, "geofabrik_cache", "SK.osm.pbf"), "pbf")
|
|
FileUtils.mkdir_p(File.join(@routing, "staging"))
|
|
File.write(File.join(@routing, "staging", "request.json"), JSON.generate(regions: %w[SK]))
|
|
File.write(File.join(@routing, "status.json"), JSON.generate(state: "running", step: "routing graph (SK)"))
|
|
Maptool::Routing.request_build(@data)
|
|
refute File.exist?(File.join(@routing, "build-request.json"))
|
|
|
|
# Different countries do queue, and the running build's step still shows.
|
|
File.write(File.join(@data, "geofabrik_cache", "CZ.osm.pbf"), "pbf")
|
|
Maptool::PmtilesBuilder.set_selected_regions(@data, %w[SK CZ])
|
|
with_osmium("cat \"$3\" \"$4\" > \"$6\"") { Maptool::Routing.request_build(@data) }
|
|
assert File.exist?(File.join(@routing, "build-request.json"))
|
|
assert_equal "routing graph (SK)", Maptool::Routing.build_status(@data)[:step]
|
|
end
|
|
|
|
def test_several_countries_are_merged_into_one_file_for_valhalla
|
|
%w[SK CZ].each { |code| File.write(File.join(@data, "geofabrik_cache", "#{code}.osm.pbf"), code) }
|
|
Maptool::PmtilesBuilder.set_selected_regions(@data, %w[SK CZ])
|
|
with_osmium("cat \"$3\" \"$4\" > \"$6\"") { Maptool::Routing.request_build(@data) }
|
|
|
|
request = JSON.parse(File.read(File.join(@routing, "build-request.json")))
|
|
assert_equal ["routing/merged.osm.pbf", %w[SK CZ]], request.values_at("pbf", "regions")
|
|
assert_equal "SKCZ", File.read(File.join(@data, "routing", "merged.osm.pbf"))
|
|
|
|
File.delete(File.join(@routing, "build-request.json"))
|
|
with_osmium("echo broken >&2; exit 1") { Maptool::Routing.request_build(@data) }
|
|
assert_equal "error", Maptool::Routing.build_status(@data)[:state]
|
|
assert_includes Maptool::Routing.build_status(@data)[:message], "broken"
|
|
refute File.exist?(File.join(@routing, "build-request.json"))
|
|
end
|
|
|
|
def test_vehicle_profiles_are_validated_and_stored
|
|
assert_equal %w[car camper bike mtb run hike], Maptool::Routing.vehicles(@data).map { |v| v[:id] }
|
|
assert_equal "Car", Maptool::Routing.vehicle(@data, "nope")[:name]
|
|
|
|
Maptool::Routing.save_vehicles(@data, [{ "id" => "car", "name" => "Car", "height" => "" },
|
|
{ "id" => "van", "name" => "Van", "height" => "3.4", "weight" => "4.2" }])
|
|
assert_equal({ id: "van", name: "Van", type: "car", height: 3.4, weight: 4.2 }, Maptool::Routing.vehicle(@data, "van"))
|
|
assert_equal({ id: "car", name: "Car", type: "car" }, Maptool::Routing.vehicle(@data, "car"))
|
|
assert_equal %w[car van], Maptool::Routing.vehicles(@data).map { |v| v[:id] } # deleted bike/foot stay deleted
|
|
Maptool::Routing.save_vehicles(@data, [{ "id" => "b", "name" => "B", "type" => "bicycle", "bicycle_type" => "Road", "speed" => "28", "height" => "" }])
|
|
assert_equal({ id: "b", name: "B", type: "bicycle", bicycle_type: "Road", speed: 28.0 }, Maptool::Routing.vehicle(@data))
|
|
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, [{ "id" => "x", "name" => "X", "type" => "boat" }]) }
|
|
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, [{ "id" => "x", "name" => "X", "type" => "bicycle", "bicycle_type" => "Tandem" }]) }
|
|
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, [{ "id" => "x", "name" => "X", "type" => "foot", "sac" => "7" }]) }
|
|
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, [{ "id" => "x", "name" => "X", "height" => "30" }]) }
|
|
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, [{ "id" => "x", "name" => "" }]) }
|
|
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, []) }
|
|
end
|
|
|
|
def test_the_old_single_vehicle_becomes_the_camper
|
|
File.write(File.join(@data, "vehicle.json"), JSON.generate(height: 3.3, width: 2.3, length: 7, weight: 3.5))
|
|
assert_equal 3.3, Maptool::Routing.vehicle(@data, "camper")[:height]
|
|
end
|
|
|
|
def test_decode_polyline_reads_valhallas_precision_6
|
|
# Two points, encoded with Valhalla's (Google-style, 1e6) polyline.
|
|
assert_equal [[9.521006, 47.140956], [9.521034, 47.140974]], Maptool::Routing.decode_polyline("wdg|xA{ubdQc@w@")
|
|
end
|
|
|
|
def test_route_picks_the_costing_by_weight_and_returns_a_line
|
|
shape = "wdg|xA{ubdQc@w@"
|
|
reply = JSON.generate(trip: { summary: { length: 1.5, time: 90 },
|
|
legs: [{ shape: shape, maneuvers: [{ instruction: "Drive north.", length: 1.5 }] }] })
|
|
# The per-country lookup failing (400) leaves the route as it is.
|
|
request = serve_once("HTTP/1.1 200 OK\r\nContent-Length: #{reply.bytesize}\r\n\r\n#{reply}", "HTTP/1.1 400 Bad Request\r\nContent-Length: 2\r\n\r\n{}") do |url|
|
|
route = Maptool::Routing.route(url, [[9.52, 47.14], [9.53, 47.15]], Maptool::Routing::DEFAULT_VEHICLES[1])
|
|
assert_equal [2, "auto", "Drive north."], [route[:geometry][:coordinates].size, route[:properties][:costing], route[:properties][:maneuvers][0][:text]]
|
|
end
|
|
assert_includes request, %("costing_options":{"auto":{"height":3.0,"width":2.3}})
|
|
|
|
request = serve_once("HTTP/1.1 400 Bad Request\r\nContent-Length: 20\r\n\r\n{\"error\":\"no route\"}") do |url|
|
|
error = assert_raises(Maptool::Routing::RouteError) { Maptool::Routing.route(url, [[1, 2], [3, 4]], Maptool::Routing::DEFAULT_VEHICLES[1].merge(weight: 7.5)) }
|
|
assert_equal "no route", error.message
|
|
end
|
|
assert_includes request, %("costing":"truck")
|
|
assert_raises(Maptool::Routing::Unavailable) { Maptool::Routing.route("", [[1, 2], [3, 4]], {}) }
|
|
end
|
|
|
|
def test_a_route_sums_motorway_and_toll_km_per_country_and_can_avoid_tolls
|
|
route = JSON.generate(trip: { summary: { length: 3, time: 90 }, legs: [{ shape: "wdg|xA{ubdQc@w@", maneuvers: [] }] })
|
|
attributes = JSON.generate(admins: [{ country_code: "AT" }, { country_code: "SK" }],
|
|
edges: [{ length: 1.0, road_class: "motorway", toll: false, end_node: { admin_index: 0 } },
|
|
{ length: 0.5, road_class: "primary", toll: false, end_node: { admin_index: 0 } },
|
|
{ length: 1.5, road_class: "motorway", toll: true, end_node: { admin_index: 1 } }])
|
|
ok = ->(body) { "HTTP/1.1 200 OK\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}" }
|
|
request = serve_once(ok.(route), ok.(attributes)) do |url|
|
|
countries = Maptool::Routing.route(url, [[1, 2], [3, 4]], { id: "car" }, { "tolls" => true })[:properties][:countries]
|
|
assert_equal [{ code: "AT", km: 1.5, motorway_km: 1.0, toll_km: 0.0 }, { code: "SK", km: 1.5, motorway_km: 1.5, toll_km: 1.5 }], countries
|
|
end
|
|
assert_includes request, %("costing_options":{"auto":{"use_tolls":0.0}})
|
|
assert_includes request, "/trace_attributes"
|
|
end
|
|
|
|
def test_bicycle_and_foot_use_their_own_costing_without_the_country_lookup
|
|
reply = JSON.generate(trip: { summary: { length: 1.5, time: 90 }, legs: [{ shape: "wdg|xA{ubdQc@w@", maneuvers: [] }] })
|
|
request = serve_once("HTTP/1.1 200 OK\r\nContent-Length: #{reply.bytesize}\r\n\r\n#{reply}") do |url|
|
|
route = Maptool::Routing.route(url, [[1, 2], [3, 4]], Maptool::Routing::ACTIVE_VEHICLES[2], { "tolls" => true })
|
|
assert_nil route[:properties][:countries]
|
|
end
|
|
assert_includes request, %("costing_options":{"pedestrian":{"walking_speed":10.0,"max_hiking_difficulty":2}})
|
|
|
|
request = serve_once("HTTP/1.1 200 OK\r\nContent-Length: #{reply.bytesize}\r\n\r\n#{reply}") do |url|
|
|
Maptool::Routing.route(url, [[1, 2], [3, 4]], Maptool::Routing::ACTIVE_VEHICLES[1])
|
|
end
|
|
assert_includes request, %("costing_options":{"bicycle":{"bicycle_type":"Mountain"}})
|
|
end
|
|
|
|
def test_isochrone_asks_valhalla_for_each_time_and_checks_them
|
|
reply = JSON.generate(type: "FeatureCollection", features: [])
|
|
request = serve_once("HTTP/1.1 200 OK\r\nContent-Length: #{reply.bytesize}\r\n\r\n#{reply}") do |url|
|
|
assert_equal "FeatureCollection", Maptool::Routing.isochrone(url, [17.1, 48.1], [60, 30, 60], Maptool::Routing::DEFAULT_VEHICLES[1])["type"]
|
|
end
|
|
assert_includes request, %("contours":[{"time":30},{"time":60}])
|
|
assert_raises(Maptool::Routing::RouteError) { Maptool::Routing.isochrone("http://x", [17.1, 48.1], [600], {}) }
|
|
assert_raises(Maptool::Routing::RouteError) { Maptool::Routing.isochrone("http://x", nil, [30], {}) }
|
|
end
|
|
|
|
private
|
|
|
|
# A stand-in osmium ($1 merge, $2 -O, $3.. inputs, then -o OUTPUT); waits for the merge thread.
|
|
def with_osmium(script)
|
|
bin = File.join(@data, "osmium")
|
|
File.write(bin, "#!/bin/sh\n#{script}\n")
|
|
File.chmod(0o755, bin)
|
|
ENV["OSMIUM_BIN"] = bin
|
|
yield
|
|
sleep 0.02 while Maptool::Routing.build_status(@data)[:step] == "merging extracts with osmium"
|
|
ensure
|
|
ENV.delete("OSMIUM_BIN")
|
|
end
|
|
|
|
# Answers one HTTP request per response, in order; returns what was received.
|
|
def serve_once(*responses)
|
|
server = TCPServer.new("127.0.0.1", 0)
|
|
received = +""
|
|
thread = Thread.new do
|
|
responses.each do |response|
|
|
client = server.accept
|
|
request = client.readpartial(65_536)
|
|
request << client.readpartial(65_536) unless request.include?("{")
|
|
received << request
|
|
client.write(response)
|
|
client.close
|
|
end
|
|
end
|
|
yield "http://127.0.0.1:#{server.addr[1]}"
|
|
thread.join
|
|
received
|
|
ensure
|
|
server&.close
|
|
end
|
|
end
|