"""Tests for ``parse_ams_filament_backup_from_cfg`` (#1766 prefer_lowest gate). The function extracts bit 18 of Bambu's top-level ``print.cfg`` hex string, which OrcaSlicer's DeviceManager.cpp:4961 maps to AMS Filament Backup. These tests pin the bit position + cover the absent / malformed cases A1-family printers and pre-init pushes produce. """ import pytest from backend.app.services.bambu_mqtt import ( parse_ams_filament_backup_from_cfg, parse_ams_filament_backup_from_home_flag, ) class TestParseAmsFilamentBackupFromCfg: def test_h2d_on_capture(self): # Captured 2026-06-20 from H2D fw 01.03.00.00 with backup ON. # Hex "C0340FC219" has bit 18 set (nibble 5 = F = 0b1111). assert parse_ams_filament_backup_from_cfg("C0340FC219") is True def test_h2d_off_capture(self): # Same printer, backup toggled OFF — only bit 18 flips: # "C0340BC219" — nibble 5 = B = 0b1011. assert parse_ams_filament_backup_from_cfg("C0340BC219") is False def test_x1c_short_hex_string_on(self): # X1C cfg in the investigation snapshots is short ("FCA09"). # Bit 18 of 0xFCA09 = 0b1111110010100001001, bit18 set. assert parse_ams_filament_backup_from_cfg("FCA09") is True def test_lowercase_hex(self): # Robustness: int(s, 16) accepts both cases; check we don't regress. assert parse_ams_filament_backup_from_cfg("c0340fc219") is True def test_only_bit_18_isolated(self): # Sanity: a value with ONLY bit 18 set must parse as True. assert parse_ams_filament_backup_from_cfg(hex(1 << 18)[2:]) is True def test_bit_18_clear_but_others_set(self): # Set every bit EXCEPT 18 — must parse as False. mask = (~(1 << 18)) & 0xFFFFFFFF assert parse_ams_filament_backup_from_cfg(hex(mask)[2:]) is False @pytest.mark.parametrize( "value", [ None, # field omitted (A1 family old protocol) "", # empty string 123, # firmware-emitted int instead of hex string (defensive) "not_hex", # malformed "0xZZ", # invalid hex ["FCA09"], # wrong shape {"cfg": "FCA09"}, # nested by mistake ], ) def test_invalid_returns_none(self, value): # None preserves today's behaviour for callers gating on backup state — # NOT False. Treating absent as OFF would regress A1-family scheduling. assert parse_ams_filament_backup_from_cfg(value) is None class TestParseAmsFilamentBackupFromHomeFlag: """``home_flag`` bit 10, the only source on families without ``cfg`` (#3259). Values are from support-bundle push_status snapshots.""" def test_p1s_on(self): # P1S fw 01.10.00.00 full status: no cfg, home_flag bit 10 set. assert parse_ams_filament_backup_from_home_flag(7554719) is True def test_a1_off(self): # A1 fw 01.08.01.00: home_flag bit 10 clear. assert parse_ams_filament_backup_from_home_flag(846152095) is False def test_negative_value(self): # X1-class firmware sends the 32-bit flag as a signed int; its bit 10 # matches cfg bit 18 in the same frames (ON and OFF). assert parse_ams_filament_backup_from_home_flag(-1067070056) is True assert parse_ams_filament_backup_from_home_flag(-1067069137) is False @pytest.mark.parametrize("value", [None, "1024", True, 10.0, [1024]]) def test_invalid_returns_none(self, value): assert parse_ams_filament_backup_from_home_flag(value) is None