fix(mcp): treat ending_percent=-1 as unread sentinel in normalize_consumed

This commit is contained in:
RNL
2026-08-19 21:52:48 +10:00
parent 8a498e933e
commit df34ec8e3e
2 changed files with 10 additions and 1 deletions

View File

@@ -108,7 +108,10 @@ def normalize_consumed(starting_percent, ending_percent, initial_weight_grams):
fabricated zero (spec: 24/269 usage rows go negative; a -1 sentinel fabricated zero (spec: 24/269 usage rows go negative; a -1 sentinel
subtracted as a real value produces e.g. consumed=-101%). subtracted as a real value produces e.g. consumed=-101%).
""" """
if starting_percent is None or starting_percent == -1 or ending_percent is None: if (
starting_percent is None or starting_percent == -1
or ending_percent is None or ending_percent == -1
):
return None, None, "unreliable" return None, None, "unreliable"
consumed_percent = starting_percent - ending_percent consumed_percent = starting_percent - ending_percent
if consumed_percent < 0: if consumed_percent < 0:

View File

@@ -109,6 +109,12 @@ def test_normalize_consumed_sentinel_start_goes_unreliable():
assert normalize_consumed(-1, 100, initial_weight_grams=1000) == (None, None, "unreliable") assert normalize_consumed(-1, 100, initial_weight_grams=1000) == (None, None, "unreliable")
def test_normalize_consumed_sentinel_end_goes_unreliable():
# symmetric case: ending_percent == -1 (no RFID reading at job end)
# must not fabricate a plausible-looking consumption figure
assert normalize_consumed(74, -1, initial_weight_grams=1000) == (None, None, "unreliable")
@pytest.mark.django_db @pytest.mark.django_db
def test_resolve_printer_by_id(): def test_resolve_printer_by_id():
p = Printer.objects.create(name="RNL-H2C", model="H2C", is_active=True) p = Printer.objects.create(name="RNL-H2C", model="H2C", is_active=True)