require_relative "test_helper" require "tmpdir" require "timeout" require_relative "../lib/maptool/trip" class TripTest < Minitest::Test def test_an_update_routes_bumps_the_version_and_survives_a_restart Dir.mktmpdir do |dir| path = File.join(dir, "trip.json") store = Maptool::TripStore.new(path) assert_equal 0, store.get[:version] points = [{ lngLat: [17.1, 48.1], name: "Home" }, { lngLat: [17.2, 48.2] }] calls = [] router = ->(locations, vehicle, avoid) { calls << [locations, vehicle, avoid]; { type: "Feature", km: 1 } } trip = store.update(points: points, vehicle: "camper", avoid: { tolls: true }, editing: nil, &router) assert_equal [1, { type: "Feature", km: 1 }], trip.values_at(:version, :route) assert_equal [[[[17.1, 48.1], [17.2, 48.2]], "camper", { tolls: true }]], calls # only a name/nav changed: no routing, the route stays points[1] = { lngLat: [17.2, 48.2], name: "Camp", nav: true } trip = store.update(points: points, vehicle: "camper", avoid: { tolls: true }, editing: nil, &router) assert_equal [2, 1, "Camp"], [trip[:version], calls.size, trip[:points][1][:name]] reloaded = Maptool::TripStore.new(path).get assert_equal [2, { type: "Feature", km: 1 }, "Camp"], [reloaded[:version], reloaded[:route], reloaded[:points][1][:name]] end end def test_the_itinerary_keeps_nights_stays_notes_and_the_start_without_routing_again Dir.mktmpdir do |dir| store = Maptool::TripStore.new(File.join(dir, "trip.json")) calls = 0 router = ->(*) { calls += 1; { type: "Feature" } } points = [{ lngLat: [17.1, 48.1] }, { lngLat: [17.2, 48.2] }] store.update(points: points, vehicle: nil, avoid: {}, editing: nil, &router) points[1] = { lngLat: [17.2, 48.2], night: true, stay: "90", note: " call ahead " } trip = store.update(points: points, vehicle: nil, avoid: {}, editing: nil, name: "Alps", start: "2026-12-12T08:00", day_start: "09:30", &router) assert_equal 1, calls # an itinerary change isn't a route change assert_equal({ lngLat: [17.2, 48.2], name: nil, nav: false, passed: false, night: true, stay: 90, note: "call ahead" }.compact, trip[:points][1]) assert_equal ["Alps", "2026-12-12T08:00", "09:30"], trip.values_at(:name, :start, :day_start) assert_raises(ArgumentError) { store.update(points: [{ lngLat: [1, 2], stay: "-5" }], vehicle: nil, avoid: {}, editing: nil, &router) } assert_raises(ArgumentError) { store.update(points: points, vehicle: nil, avoid: {}, editing: nil, start: "tomorrow", &router) } end end # Off the route while driving for 3 fixes: routed again from the position # to the points not reached yet (the first via passed: dropped). def test_following_reroutes_from_the_position_off_the_route Dir.mktmpdir do |dir| store = Maptool::TripStore.new(File.join(dir, "trip.json")) line = { geometry: { coordinates: [[17.0, 48.0], [17.1, 48.0], [17.2, 48.0]] }, properties: { legs_km: [7.44, 7.44] } } points = [{ lngLat: [17.0, 48.0] }, { lngLat: [17.1, 48.0] }, { lngLat: [17.2, 48.0] }] calls = [] router = ->(locations, _vehicle, _avoid) { calls << locations; line } store.update(points: points, vehicle: nil, avoid: {}, editing: nil, &router) calls.clear store.follow([17.15, 48.0], speed: 20, now: 0, &router) # on it, past the via 3.times { |i| store.follow([17.15, 48.01], speed: 20, now: i + 1, &router) } # ~1.1 km off Timeout.timeout(5) { sleep 0.05 until store.get[:version] == 2 } assert_equal [[[17.15, 48.01], [17.2, 48.0]]], calls # the start and the via reached: kept, marked passed; the detour starts here assert_equal [[true, nil], [true, nil], [false, "My position"], [false, nil]], store.get[:points].map { |p| p.values_at(:passed, :name) } store.follow([17.15, 48.02], speed: 20, now: 5, &router) # within REROUTE_EVERY_S: nothing assert_equal 1, calls.size end end # A passed point stays in the trip, not in the route; marking one passed # routes again (fewer locations), only renaming it doesn't. def test_passed_points_are_kept_but_not_routed Dir.mktmpdir do |dir| store = Maptool::TripStore.new(File.join(dir, "trip.json")) calls = [] router = ->(locations, *) { calls << locations; :route } points = [{ lngLat: [1, 1], passed: true }, { lngLat: [2, 2] }, { lngLat: [3, 3] }] trip = store.update(points: points, vehicle: nil, avoid: {}, editing: nil, &router) assert_equal [[[2.0, 2.0], [3.0, 3.0]]], calls assert_equal [true, false, false], trip[:points].map { |p| p[:passed] } points[0] = points[0].merge(name: "Missed it") store.update(points: points, vehicle: nil, avoid: {}, editing: nil, &router) assert_equal 1, calls.size end end def test_a_routing_error_is_kept_with_the_points_and_tried_again_next_time Dir.mktmpdir do |dir| store = Maptool::TripStore.new(File.join(dir, "trip.json")) points = [{ lngLat: [1, 2] }, { lngLat: [3, 4] }] trip = store.update(points: points, vehicle: nil, avoid: {}, editing: nil) { raise Maptool::Routing::RouteError, "no route" } assert_equal [nil, "no route", 2], [trip[:route], trip[:error], trip[:points].size] trip = store.update(points: points, vehicle: nil, avoid: {}, editing: nil) { :routed } assert_equal [:routed, nil], trip.values_at(:route, :error) end end end