Add a persisted test suite
`bundle exec rake test` (minitest, a default gem but pinned in Gemfile for reproducibility) now covers what ad-hoc scripts kept re-verifying by hand during development: Gpx/Fit/TrackStats parsing, GpxStore/LiveTrackStore file/TTL logic, LiveTrackClient's payload parsing, and app.rb's routes end to end via Rack::MockRequest. MAPTOOL_DATA_DIR (new, app.rb) lets the suite point GPX_STORE/ LIVETRACK_STORE at a throwaway directory instead of real data/ — without it every run would leave uploaded test files behind. Each lib file's old `if __FILE__ == $0` self-check is now redundant with real tests covering the same ground, so removed rather than kept as a second copy of the same assertions. Wired into ci.yml so it isn't just a local convenience.
This commit is contained in:
1 parent
a3c31ace06
commit
cd4eef342b
19 files changed
+706
-174
No files matched your search
@@ -0,0 +1,68 @@
|
||||
require_relative "test_helper"
|
||||
require "maptool/livetrack_client"
|
||||
|
||||
# No real network calls here — Garmin's page format was verified manually
|
||||
# against real sessions during development; a synthetic fixture (matching
|
||||
# the real page's structure: several `self.__next_f.push(...)` calls, one
|
||||
# holding the track points nested under some data-fetching library's own
|
||||
# structure) is what actually needs to keep working over time.
|
||||
class LiveTrackClientTest < Minitest::Test
|
||||
def setup
|
||||
@client = Maptool::LiveTrackClient.new
|
||||
end
|
||||
|
||||
FAKE_PAGE = <<~HTML.freeze
|
||||
<html><body>
|
||||
<script>self.__next_f.push([1,"6:[\\"$\\",\\"div\\",null,{}]"])</script>
|
||||
<script>self.__next_f.push([1,"9:{\\"session\\":{\\"sessionName\\":\\"Test Ride\\",\\"start\\":\\"2026-01-01T00:00:00Z\\",\\"end\\":\\"2026-01-01T01:00:00Z\\"}}"])</script>
|
||||
<script>self.__next_f.push([1,"10:{\\"unrelated\\":true,\\"nested\\":{\\"data\\":{\\"pages\\":[{\\"trackPoints\\":[{\\"position\\":{\\"lat\\":48.1,\\"lon\\":17.2},\\"dateTime\\":\\"2026-01-01T00:00:00Z\\",\\"totalDistanceMeters\\":\\"$undefined\\"},{\\"position\\":{\\"lat\\":48.2,\\"lon\\":17.3},\\"dateTime\\":\\"2026-01-01T00:01:00Z\\",\\"activityType\\":\\"CYCLING\\",\\"totalDistanceMeters\\":5000,\\"totalDurationSecs\\":600}]}]}}}"])</script>
|
||||
</body></html>
|
||||
HTML
|
||||
|
||||
def test_finds_track_points_and_session_across_separate_push_payloads
|
||||
payloads = @client.send(:parse_payloads, FAKE_PAGE)
|
||||
|
||||
points = @client.send(:search, payloads) { |n| n["trackPoints"] if n["trackPoints"].is_a?(Array) }
|
||||
assert_equal 2, points.size
|
||||
assert_in_delta 48.1, points[0]["position"]["lat"]
|
||||
|
||||
session = @client.send(:search, payloads) { |n| n if n["sessionName"] }
|
||||
assert_equal "Test Ride", session["sessionName"]
|
||||
end
|
||||
|
||||
def test_undefined_sentinel_is_cleaned_to_nil
|
||||
payloads = @client.send(:parse_payloads, FAKE_PAGE)
|
||||
points = @client.send(:search, payloads) { |n| n["trackPoints"] if n["trackPoints"].is_a?(Array) }
|
||||
cleaned = @client.send(:clean, points)
|
||||
|
||||
assert_nil cleaned[0]["totalDistanceMeters"]
|
||||
end
|
||||
|
||||
def test_summarize_pulls_from_last_point_and_session
|
||||
payloads = @client.send(:parse_payloads, FAKE_PAGE)
|
||||
points = @client.send(:clean, @client.send(:search, payloads) { |n| n["trackPoints"] if n["trackPoints"].is_a?(Array) })
|
||||
session = @client.send(:clean, @client.send(:search, payloads) { |n| n if n["sessionName"] })
|
||||
result = Maptool::LiveTrackClient::Result.new(points: points, session: session)
|
||||
|
||||
summary = Maptool::LiveTrackClient.summarize(result)
|
||||
|
||||
assert_equal "Test Ride", summary["session_name"]
|
||||
assert_equal "CYCLING", summary["activity_type"]
|
||||
assert_equal 5000, summary["distance_meters"]
|
||||
refute summary["active"], "session has an end timestamp"
|
||||
end
|
||||
|
||||
def test_summarize_marks_active_when_session_has_no_end
|
||||
payloads = @client.send(:parse_payloads, FAKE_PAGE)
|
||||
points = @client.send(:clean, @client.send(:search, payloads) { |n| n["trackPoints"] if n["trackPoints"].is_a?(Array) })
|
||||
session = @client.send(:clean, @client.send(:search, payloads) { |n| n if n["sessionName"] }).merge("end" => nil)
|
||||
result = Maptool::LiveTrackClient::Result.new(points: points, session: session)
|
||||
|
||||
assert Maptool::LiveTrackClient.summarize(result)["active"]
|
||||
end
|
||||
|
||||
def test_no_track_points_found_in_a_page_without_them
|
||||
points = @client.send(:search, @client.send(:parse_payloads, "<html></html>")) { |n| n["trackPoints"] if n["trackPoints"].is_a?(Array) }
|
||||
assert_nil points
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user