test_nfc_reader.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Tests for daemon.nfc_reader — tray UUID extraction and raw blocks from Bambu tag blocks."""
  2. from daemon.nfc_reader import _extract_tray_uuid, _raw_blocks
  3. # Measured on a real spool: block 9 equals the tray_uuid the AMS reports (#984).
  4. BLOCK9 = bytes.fromhex("9E0B0717BEE94D7887EB1D8DFD1A14F3")
  5. # Block 4 of a "PLA Matte" spool -- what the old blocks 4+5 read sent as tray_uuid.
  6. BLOCK4_PLA_MATTE = b"PLA Matte" + b"\x00" * 7
  7. class TestExtractTrayUuid:
  8. def test_reads_block_9(self):
  9. blocks = {1: b"\x01" * 16, 2: b"\x02" * 16, 4: BLOCK4_PLA_MATTE, 5: b"\x00" * 16, 9: BLOCK9}
  10. assert _extract_tray_uuid(blocks) == "9E0B0717BEE94D7887EB1D8DFD1A14F3"
  11. def test_ignores_blocks_4_and_5(self):
  12. """Without block 9 there is no tray UUID, never the filament type from block 4."""
  13. blocks = {4: BLOCK4_PLA_MATTE, 5: b"\x00" * 16}
  14. assert _extract_tray_uuid(blocks) is None
  15. def test_all_zero_block_9_is_no_uuid(self):
  16. assert _extract_tray_uuid({9: b"\x00" * 16}) is None
  17. def test_short_block_9_is_no_uuid(self):
  18. assert _extract_tray_uuid({9: b"\x9e\x0b"}) is None
  19. def test_empty_blocks(self):
  20. assert _extract_tray_uuid({}) is None
  21. class TestRawBlocks:
  22. def test_blocks_as_uppercase_hex_keyed_by_block_number(self):
  23. assert _raw_blocks({9: BLOCK9, 4: BLOCK4_PLA_MATTE}) == {
  24. "4": "504C41204D6174746500000000000000",
  25. "9": "9E0B0717BEE94D7887EB1D8DFD1A14F3",
  26. }
  27. def test_empty_blocks(self):
  28. assert _raw_blocks({}) == {}