require_relative "test_helper" require "maptool/gpx" class GpxTest < Minitest::Test def test_kinds_detects_waypoints_and_tracks assert_equal %w[waypoint track], Maptool::Gpx.kinds(fixture_path("sample.gpx")) end def test_to_geojson_produces_a_waypoint_point_feature geojson = Maptool::Gpx.to_geojson(fixture_path("sample.gpx")) waypoint = geojson["features"].find { |f| f["properties"]["kind"] == "waypoint" } assert_equal "Point", waypoint["geometry"]["type"] assert_equal [17.2, 48.1, 200.0], waypoint["geometry"]["coordinates"] assert_equal "Start", waypoint["properties"]["name"] assert_includes waypoint["properties"]["description"], "example.com" end def test_to_geojson_produces_a_track_multilinestring_with_stats geojson = Maptool::Gpx.to_geojson(fixture_path("sample.gpx")) track = geojson["features"].find { |f| f["properties"]["kind"] == "track" } assert_equal "MultiLineString", track["geometry"]["type"] assert_equal 3, track["geometry"]["coordinates"][0].size assert_equal "Morning Loop", track["properties"]["name"] assert_equal "running", track["properties"]["activity_type"] assert_operator track["properties"]["distance_meters"], :>, 0 assert_equal 50.0, track["properties"]["elevation_gain_meters"], "200->250 is the only gain, 250->230 descent not subtracted" assert_equal 1200, track["properties"]["duration_secs"] end def test_track_profile_carries_extension_fields_and_tolerates_points_without_them geojson = Maptool::Gpx.to_geojson(fixture_path("sample.gpx")) profile = geojson["features"].find { |f| f["properties"]["kind"] == "track" }["properties"]["profile"] assert_equal 3, profile.size assert_equal 140, profile[0]["heart_rate_bpm"] assert_equal 84, profile[1]["cadence_rpm"] assert_nil profile[2]["heart_rate_bpm"], "third point has no " assert_equal 0.0, profile[0]["distance_meters"] assert_equal [17.2, 48.1], [profile[0]["lon"], profile[0]["lat"]] end def test_invalid_xml_raises_parse_error path = File.join(Dir.mktmpdir, "bad.gpx") File.write(path, "") assert_raises(Maptool::Gpx::ParseError) { Maptool::Gpx.to_geojson(path) } end def test_non_gpx_root_raises_parse_error path = File.join(Dir.mktmpdir, "notgpx.gpx") File.write(path, "") assert_raises(Maptool::Gpx::ParseError) { Maptool::Gpx.to_geojson(path) } end end