From 95aa8507f05c099a679bc5db204e558c3c0e1338 Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Sat, 15 Aug 2026 14:15:05 +0200 Subject: [PATCH] test: add big-endian round-trip coverage for FIT field patching --- tests/fit/test_rewriter_patching.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/fit/test_rewriter_patching.py b/tests/fit/test_rewriter_patching.py index a17f6ba..0dccbfb 100644 --- a/tests/fit/test_rewriter_patching.py +++ b/tests/fit/test_rewriter_patching.py @@ -30,6 +30,17 @@ def _build_fixture() -> bytes: return make_fit(records) +def _build_big_endian_fixture() -> bytes: + """Big-endian (architecture=1) file_id definition/data pair. Closes the + big-endian coverage gap deferred from Task 2: no fixture anywhere in the FIT + test suite previously passed endian=">" to definition()/struct.pack, so the + architecture-byte branch in _read_definition (and the endian format string it + threads into every field read/write) was never actually exercised.""" + file_def = definition(0, FILE_ID_MESG_NUM, [(1, 2, 0x84), (2, 2, 0x84)], endian=">") + file_data = data(0, struct.pack(">HH", 255, 999)) + return make_fit(file_def + file_data) + + def _build_no_device_index_fixture() -> bytes: """device_info record entirely missing field 0 (device_index) must be left untouched.""" file_def = definition(0, FILE_ID_MESG_NUM, [(1, 2, 0x84), (2, 2, 0x84)]) @@ -161,6 +172,24 @@ def test_convert_fit_device_patches_product_name_string_fields(tmp_path: Path) - assert values_by_key[(DEVICE_INFO_MESG_NUM, 27)] == "Edge 1030 Plus" +def test_convert_fit_device_round_trips_big_endian_fields(tmp_path: Path) -> None: + """Proves both the read side (parsing manufacturer/product under architecture=1) + and the write side (patching them back in big-endian byte order) are correct -- + a byte-order bug on either side would flip 255/999 or the patched 1/3570 into an + unrelated value once read back with the (still big-endian) definition.""" + source = tmp_path / "source.fit" + source.write_bytes(_build_big_endian_fixture()) + output = tmp_path / "output.fit" + + convert_fit_device(source, output) + + values = read_device_field_values(output) + values_by_key = {(v.global_message_num, v.field_num): v.value for v in values} + + assert values_by_key[(FILE_ID_MESG_NUM, 1)] == 1 + assert values_by_key[(FILE_ID_MESG_NUM, 2)] == 3570 + + def test_convert_fit_device_defaults_to_edge_1030_plus() -> None: device = GarminDevice() assert device.manufacturer_id == 1