require_relative "test_helper" require "tmpdir" require "maptool/library" require "maptool/gpx_store" require "maptool/trip_log" class TripLogTest < Minitest::Test # Start makes the folder with the plan and its named stops; fixes 10 m # apart become the car's track (written on the first flush, replaced # after); stop closes it. def test_a_trip_records_into_its_folder Dir.mktmpdir do |dir| library = Maptool::Library.new(File.join(dir, "library.sqlite"), gpx_store: Maptool::GpxStore.new(File.join(dir, "gpx")), sources_dir: File.join(dir, "sources")) log = Maptool::TripLog.new(File.join(dir, "trip_log.json"), library: library) plan = { geometry: { type: "LineString", coordinates: [[17.0, 48.0], [17.1, 48.0]] }, properties: { distance_km: 7.4, time_s: 600 } } folder = log.start("Tatry", plan: plan, stops: [{ lngLat: [17.0, 48.0], name: "My position" }, { lngLat: [17.1, 48.0], name: "Camp" }], now: Time.utc(2026, 12, 1, 8)) assert_equal "Trips/2026-12-01 Tatry", folder assert_raises(ArgumentError) { log.start("again") } log.record(17.0, 48.0, 150, 1000) log.record(17.00001, 48.0, 150, 1001) # < 10 m: skipped log.record(17.001, 48.0, 151, 1002) log.record(17.002, 48.0, 152, 1100) # a minute on: flushed assert_equal 3, log.status[:points] assert_equal [[17.001, 48.0], [17.002, 48.0]], log.status(since: 1)[:coordinates] assert_equal folder, log.stop refute log.active? names = library.nodes.flat_map { |label, _key, _stamp, read| [label, *JSON.parse(read.call)["features"].map { |f| f.dig("properties", "name") }] }.compact assert(library.nodes.all? { |label, *| label.include?("2026-12-01 Tatry") }) # all in the trip's folder assert_includes names.join(" "), "Camp" assert_includes names.join(" "), "Car 2026-12-01" assert_includes names.join(" "), "Plan" refute_includes names.join(" "), "My position" end end end