Bicycle and foot route profiles (Valhalla bicycle/pedestrian costing); hiking trail colours in the elevation profile; power, splits and best efforts in the profile; TCX import; FIT course export
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-16 12:18:06 +01:00
1 parent 4c704045c5
commit f8755ea65a
21 files changed
+634 -40

No files matched your search

+25 -4
View File
@@ -1,5 +1,6 @@
require_relative "test_helper"
require "maptool/fit"
require "maptool/fit_course"
class FitTest < Minitest::Test
def le32(n)
@@ -71,16 +72,16 @@ class FitTest < Minitest::Test
body << [0b00000001].pack("C") << [sport].pack("C")
end
record_definition = [0, 0].pack("CC") + [20].pack("S<") + [5].pack("C") +
record_definition = [0, 0].pack("CC") + [20].pack("S<") + [6].pack("C") +
[0, 4, 0x85].pack("C3") + [1, 4, 0x85].pack("C3") +
[3, 1, 0x02].pack("C3") + [4, 1, 0x02].pack("C3") + [6, 2, 0x84].pack("C3")
[3, 1, 0x02].pack("C3") + [4, 1, 0x02].pack("C3") + [6, 2, 0x84].pack("C3") + [7, 2, 0x84].pack("C3")
body << [0b01000000].pack("C") << record_definition
records.each do |r|
body << [0b00000000].pack("C") << le32(r[:lat]) << le32(r[:lon])
body << [r[:heart_rate] || 0xFF].pack("C") << [r[:cadence] || 0xFF].pack("C")
speed_raw = r[:speed_mps] ? (r[:speed_mps] * 1000).round : 0xFFFF
body << [speed_raw].pack("S<")
body << [speed_raw].pack("S<") << [r[:power] || 0xFFFF].pack("S<")
end
header = [12, 16, 0, 0].pack("C2S<") + le32(body.bytesize) + ".FIT"
@@ -109,7 +110,7 @@ class FitTest < Minitest::Test
def test_profile_carries_heart_rate_cadence_and_speed_treating_0xff_as_absent
path = write_activity_fit([
{ lat: 100_000_000, lon: 200_000_000, heart_rate: 140, cadence: 80, speed_mps: 3.5 },
{ lat: 100_000_000, lon: 200_000_000, heart_rate: 140, cadence: 80, speed_mps: 3.5, power: 250 },
{ lat: 100_000_100, lon: 200_000_100 }, # no extras -> 0xFF/0xFFFF sentinels
])
@@ -118,7 +119,27 @@ class FitTest < Minitest::Test
assert_equal 140, profile[0]["heart_rate_bpm"]
assert_equal 80, profile[0]["cadence_rpm"]
assert_in_delta 3.5, profile[0]["speed_mps"]
assert_equal 250, profile[0]["power_w"]
assert_nil profile[1]["power_w"]
assert_nil profile[1]["heart_rate_bpm"]
assert_nil profile[1]["speed_mps"]
end
def test_a_fit_course_reads_back_with_its_points_time_and_a_valid_crc
coordinates = [[17.1, 48.1, 140.0], [17.11, 48.11, 150.0], [17.12, 48.1]]
bytes = Maptool::FitCourse.encode(coordinates, name: "Loop", sport: "cycling", duration: 600)
path = File.join(Dir.mktmpdir, "course.fit")
File.binwrite(path, bytes)
feature = Maptool::Fit.to_geojson(path)["features"][0]
assert_equal 600, feature["properties"]["duration_secs"]
feature["geometry"]["coordinates"][0].zip(coordinates).each do |got, want|
assert_in_delta want[0], got[0], 1e-6
assert_in_delta want[1], got[1], 1e-6
assert_in_delta want[2], got[2], 0.2 if want[2]
end
# CRC-16 over a whole FIT file (its own CRC included) comes out 0.
assert_equal 0, Maptool::FitCourse.send(:crc, bytes)
assert_equal 0xBB3D, Maptool::FitCourse.send(:crc, "123456789")
end
end