Files
maptool/test/test_livetrack_store.rb
T
randy 0c4ee97368
CI / check (push) Successful in 42s
CI / deploy (push) Successful in 20s
Flag LiveTrack sessions whose map Garmin no longer serves
Ended sessions past the ~24h Garmin appears to keep a live-track page
around for get verified once at app startup and, once confirmed dead,
flagged so the panel greys them out instead of only failing after a
click. Already-flagged sessions are skipped on later startups so this
doesn't keep re-hitting Garmin for the same dead session.
2026-09-08 14:19:15 +02:00

93 lines
3.1 KiB
Ruby

require_relative "test_helper"
require "maptool/livetrack_store"
class LiveTrackStoreTest < Minitest::Test
def setup
@store = Maptool::LiveTrackStore.new(File.join(Dir.mktmpdir, "livetrack.json"))
end
def test_starts_empty
assert_empty @store.list
end
def test_add_and_find
@store.add("abc123", "tok-abc")
@store.add("def456", "tok-def")
assert_equal 2, @store.list.size
assert_equal "def456", @store.list.first["id"], "newest received first"
assert_equal "tok-abc", @store.find("abc123")["token"]
assert_nil @store.find("nope")
end
def test_re_adding_moves_to_front_without_duplicating
@store.add("abc123", "tok-abc")
@store.add("def456", "tok-def")
@store.add("abc123", "tok-abc-2")
assert_equal %w[abc123 def456], @store.list.map { |s| s["id"] }
assert_equal "tok-abc-2", @store.find("abc123")["token"], "token updated on re-add"
end
def test_summary_carries_over_across_re_add
@store.add("abc123", "tok-abc")
@store.set_summary("abc123", { "session_name" => "Test Ride" })
@store.add("abc123", "tok-abc-2")
assert_equal({ "session_name" => "Test Ride" }, @store.find("abc123")["summary"])
end
def test_unsummarized_sessions_are_pruned_after_the_ttl_but_summarized_ones_are_kept
@store.add("old_no_summary", "tok1")
@store.add("old_with_summary", "tok2")
@store.set_summary("old_with_summary", { "session_name" => "Kept" })
path = @store.instance_variable_get(:@path)
raw = JSON.parse(File.read(path))
raw.each { |s| s["received_at"] = (Time.now.utc - 1000 * 60 * 60).iso8601 }
File.write(path, JSON.generate(raw))
reloaded = Maptool::LiveTrackStore.new(path)
ids = reloaded.list.map { |s| s["id"] }
refute_includes ids, "old_no_summary"
assert_includes ids, "old_with_summary"
end
def test_stale_ended_finds_old_ended_sessions_but_not_active_recent_or_already_flagged_ones
@store.add("old_ended", "tok1")
@store.set_summary("old_ended", { "active" => false, "end" => (Time.now.utc - 25 * 60 * 60).iso8601 })
@store.add("recent_ended", "tok2")
@store.set_summary("recent_ended", { "active" => false, "end" => Time.now.utc.iso8601 })
@store.add("still_active", "tok3")
@store.set_summary("still_active", { "active" => true })
@store.add("already_flagged", "tok4")
@store.set_summary("already_flagged", { "active" => false, "end" => (Time.now.utc - 25 * 60 * 60).iso8601 })
@store.mark_unavailable("already_flagged")
ids = @store.stale_ended(24 * 60 * 60).map { |s| s["id"] }
assert_equal ["old_ended"], ids
end
def test_mark_unavailable_flags_the_session_without_touching_its_summary
@store.add("abc123", "tok-abc")
@store.set_summary("abc123", { "session_name" => "Test Ride" })
@store.mark_unavailable("abc123")
session = @store.find("abc123")
assert_equal false, session["map_available"]
assert_equal({ "session_name" => "Test Ride" }, session["summary"])
end
def test_data_survives_reload_from_disk
@store.add("abc123", "tok-abc")
path = @store.instance_variable_get(:@path)
reloaded = Maptool::LiveTrackStore.new(path)
assert_equal 1, reloaded.list.size
end
end