Tracker and ITS-G5 as separate overlays, Overlays grouped by category: the tracker gets a name/icon/color, charging from its USB power topic, a floating info card (per-device opacity), a trail of the last 1–24 h and optional position logging that survives restarts (LiveStore); hiking trails keep their main checkbox as an all-countries toggle
CI / check (push) Successful in 20s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-19 11:57:36 +01:00
1 parent 3fc8c75c60
commit 0e5f79f8e4
9 files changed
+801 -205

No files matched your search

+15 -7
View File
@@ -137,22 +137,30 @@ class AppTest < Minitest::Test
assert_equal 401, request.get("/api/live").status
end
# The car's last position stays (with its age); ITS stations expire.
def test_live_keeps_the_vehicle_and_drops_stale_its_stations
# MQTT messages land in the tracker / ITS-G5 parts of /api/live.
def test_live_routes_topics_into_the_snapshot
app = Maptool::App
now = Time.now.to_f
app.record_live(app::TRACKER_TOPIC, '{"latitude":48.1,"longitude":17.1,"speed":3}', at: now - 600)
app.record_live(app::ITS_TOPIC, '{"id":"old","msg":"CAM","lat":48,"lon":17}', at: now - app::ITS_STALE_AFTER - 5)
app.record_live(app::TRACKER_POWER_TOPIC, "ON")
app.record_live(app::ITS_TOPIC, '{"id":"new","msg":"DENM","lat":48.2,"lon":17.2}', at: now)
app.record_live(app::ITS_TOPIC, "not json")
body = json_body(request.get("/api/live", **logged_in_env))
assert_equal 48.1, body["vehicle"]["latitude"]
assert_in_delta 600, body["vehicle"]["age"], 2
refute body["vehicle"].key?("received_at")
assert_equal 48.1, body["tracker"]["latitude"]
assert_in_delta 600, body["tracker"]["age"], 2
assert_equal true, body["tracker"]["usb_power"]
assert_equal ["new"], body["its"].map { |s| s["id"] }
assert_equal "Tracker", body["settings"]["name"]
assert_equal false, body["available"] # no MQTT_URL in tests
end
def test_live_settings_require_login_and_save
assert_equal 401, request.put("/api/live/settings", params: { name: "x" }).status
body = json_body(request.put("/api/live/settings", params: { name: "Car", trail_hours: "2" }, **logged_in_env))
assert_equal "Car", body["name"]
assert_equal 2, body["trail_hours"]
ensure
app::LIVE_MUTEX.synchronize { app::LIVE.replace(vehicle: nil, its: {}) }
Maptool::App::LIVE_STORE.update_settings("name" => "Tracker", "trail_hours" => "1")
end
# --- auth guard ---
+79
View File
@@ -0,0 +1,79 @@
require_relative "test_helper"
require "tmpdir"
require_relative "../lib/maptool/live_store"
class LiveStoreTest < Minitest::Test
def setup
@dir = Dir.mktmpdir("maptool-live-test")
end
def teardown
FileUtils.remove_entry(@dir)
end
def position(lat, lon, extra = {})
{ "latitude" => lat, "longitude" => lon }.merge(extra)
end
def test_tracker_snapshot_with_age_power_and_settings
store = Maptool::LiveStore.new(@dir)
now = Time.now.to_f
store.record_tracker(position(48.1, 17.1, "battery_level" => 80), at: now - 30)
store.record_usb_power("ON")
tracker = store.snapshot(now: now)[:tracker]
assert_equal 48.1, tracker["latitude"]
assert_equal 30, tracker["age"]
assert_equal true, tracker["usb_power"]
refute tracker.key?("received_at")
assert_equal "Tracker", store.snapshot[:settings]["name"]
store.record_usb_power("OFF")
assert_equal false, store.snapshot(now: now)[:tracker]["usb_power"]
end
# A parked tracker repeating itself doesn't grow the trail; moving does,
# and the trail shown is only the last trail_hours.
def test_trail_grows_on_movement_and_is_cut_to_trail_hours
store = Maptool::LiveStore.new(@dir)
now = Time.now.to_f
store.record_tracker(position(48.0, 17.0), at: now - 7200)
store.record_tracker(position(48.0, 17.0), at: now - 7100) # same spot, 100 s later: skipped
store.record_tracker(position(48.01, 17.0), at: now - 600) # ~1.1 km on
store.record_tracker(position(48.02, 17.0), at: now - 60)
assert_equal [[17.0, 48.01], [17.0, 48.02]], store.snapshot(now: now)[:tracker]["trail"] # trail_hours 1
store.update_settings("trail_hours" => "4")
assert_equal 3, store.snapshot(now: now)[:tracker]["trail"].size
store.update_settings("trail_hours" => "0")
assert_empty store.snapshot(now: now)[:tracker]["trail"]
end
def test_logged_trail_survives_a_restart_and_unlogged_does_not
store = Maptool::LiveStore.new(@dir)
now = Time.now.to_f
store.record_tracker(position(48.0, 17.0), at: now - 120)
store.update_settings("logging" => "true")
store.record_tracker(position(48.01, 17.0), at: now - 60)
store.record_tracker(position(48.02, 17.0), at: now)
reloaded = Maptool::LiveStore.new(@dir)
reloaded.record_tracker(position(48.02, 17.0), at: now) # same spot: no new point
assert_equal [[17.0, 48.01], [17.0, 48.02]], reloaded.snapshot(now: now)[:tracker]["trail"]
assert_equal true, reloaded.settings["logging"]
end
def test_settings_are_sanitized_and_persisted
store = Maptool::LiveStore.new(@dir)
store.update_settings("name" => " Run ", "icon" => "walking", "color" => "#7cc957", "trail_hours" => "2", "evil" => "x")
store.update_settings("color" => "red", "trail_hours" => "3", "name" => "")
settings = Maptool::LiveStore.new(@dir).settings
assert_equal({ "name" => "Run", "icon" => "walking", "color" => "#7cc957", "logging" => false, "trail_hours" => 2 }, settings)
end
def test_its_stations_expire
store = Maptool::LiveStore.new(@dir)
now = Time.now.to_f
store.record_its({ "id" => "old", "lat" => 48, "lon" => 17 }, at: now - Maptool::LiveStore::ITS_STALE_AFTER - 5)
store.record_its({ "id" => "new", "lat" => 48, "lon" => 17 }, at: now)
store.record_its({ "lat" => 48 }, at: now) # no id: ignored
assert_equal ["new"], store.snapshot(now: now)[:its].map { |s| s["id"] }
end
end