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_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