mirror of
https://github.com/RunLit/Bambu-Run.git
synced 2026-06-24 23:00:20 +01:00
Feature/multi printer support (#12)
* Initial implementation of multi-printer support. * Always show device dropdown and add bambu_diagnose for multi-printer troubleshooting. * Add multi-AMS support: per-unit snapshot/usage tracking, grouped dashboard panels with real type labels, and dual-nozzle card UX fixes. Fixes a real-world AMS info-code parsing bug found by inspecting live H2C data. * Add Vortek hotend rack tracking: per-SN registry with slot mapping confirmed against live MQTT capture, plus a fallback for non-inductive nozzles (e.g. H2C's fixed left nozzle) shown read-only without fabricated identity. New dashboard card hides entirely on printers with no Vortek/nozzle-info data at all.
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
40
tests/settings.py
Normal file
40
tests/settings.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Minimal Django settings for running bambu_run's pytest suite (in-memory SQLite)."""
|
||||
|
||||
SECRET_KEY = "test-secret-key"
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"bambu_run",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
]
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"APP_DIRS": True,
|
||||
"OPTIONS": {
|
||||
"context_processors": [
|
||||
"django.template.context_processors.request",
|
||||
"django.contrib.auth.context_processors.auth",
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": ":memory:",
|
||||
}
|
||||
}
|
||||
|
||||
USE_TZ = True
|
||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
|
||||
ROOT_URLCONF = "tests.urls"
|
||||
25
tests/test_ams_type_from_info.py
Normal file
25
tests/test_ams_type_from_info.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
from bambu_run.models import ams_type_from_info
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"info_code,expected",
|
||||
[
|
||||
# Real-world 8-char info codes captured from a live H2C with
|
||||
# AMS 2 Pro (unit 0), AMS (unit 1), AMS HT (unit 128).
|
||||
("10001003", "AMS 2 Pro"),
|
||||
("10001001", "AMS"),
|
||||
("11002104", "AMS HT"),
|
||||
# Bare 4-digit codes (original assumption) still resolve.
|
||||
("1001", "AMS"),
|
||||
("1003", "AMS 2 Pro"),
|
||||
("2104", "AMS HT"),
|
||||
# Unknown/missing codes resolve to empty string, not an error.
|
||||
("99999999", ""),
|
||||
("", ""),
|
||||
(None, ""),
|
||||
],
|
||||
)
|
||||
def test_ams_type_from_info(info_code, expected):
|
||||
assert ams_type_from_info(info_code) == expected
|
||||
68
tests/test_diagnostics.py
Normal file
68
tests/test_diagnostics.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import pytest
|
||||
|
||||
from bambu_run.diagnostics import redact_diagnostics, build_diagnostics_report
|
||||
|
||||
|
||||
def test_redacts_password_and_token_like_keys():
|
||||
data = {"BAMBU_PASSWORD": "hunter2", "access_token": "abc123", "ok": "fine"}
|
||||
|
||||
redacted = redact_diagnostics(data)
|
||||
|
||||
assert redacted["BAMBU_PASSWORD"] == "***REDACTED***"
|
||||
assert redacted["access_token"] == "***REDACTED***"
|
||||
assert redacted["ok"] == "fine"
|
||||
|
||||
|
||||
def test_masks_known_identifier_keys_partially():
|
||||
data = {"dev_id": "31B8BP592601478", "tray_uuid": "EE37828FA8844DE1AB12"}
|
||||
|
||||
redacted = redact_diagnostics(data)
|
||||
|
||||
assert redacted["dev_id"] == "31B8...1478"
|
||||
assert redacted["tray_uuid"] == "EE37...AB12"
|
||||
|
||||
|
||||
def test_short_identifier_values_fully_masked():
|
||||
data = {"dev_id": "short"}
|
||||
|
||||
redacted = redact_diagnostics(data)
|
||||
|
||||
assert redacted["dev_id"] == "***"
|
||||
|
||||
|
||||
def test_recurses_into_nested_structures():
|
||||
data = {"devices": [{"dev_id": "31B8BP592601478", "name": "RNL-H2C"}]}
|
||||
|
||||
redacted = redact_diagnostics(data)
|
||||
|
||||
assert redacted["devices"][0]["dev_id"] == "31B8...1478"
|
||||
assert redacted["devices"][0]["name"] == "RNL-H2C"
|
||||
|
||||
|
||||
def test_no_redact_passthrough_keeps_original_values():
|
||||
data = {"dev_id": "31B8BP592601478", "BAMBU_PASSWORD": "hunter2"}
|
||||
|
||||
result = redact_diagnostics(data, redact=False)
|
||||
|
||||
assert result == data
|
||||
|
||||
|
||||
def test_build_diagnostics_report_structure():
|
||||
devices = [{"dev_id": "SERIAL-A", "name": "Printer A", "dev_product_name": "H2C"}]
|
||||
raw_payloads = {"SERIAL-A": {"device": {"extruder": {"info": []}}}}
|
||||
|
||||
report = build_diagnostics_report(devices, raw_payloads)
|
||||
|
||||
assert report["device_count"] == 1
|
||||
assert "generated_at" in report
|
||||
assert report["devices"][0]["device_info"]["dev_id"] == "SERIAL-A"
|
||||
assert report["devices"][0]["raw_mqtt_payload"] == {"device": {"extruder": {"info": []}}}
|
||||
|
||||
|
||||
def test_build_diagnostics_report_handles_missing_payload():
|
||||
devices = [{"dev_id": "SERIAL-A", "name": "Printer A"}]
|
||||
|
||||
report = build_diagnostics_report(devices, raw_payloads={})
|
||||
|
||||
assert report["devices"][0]["raw_mqtt_payload"] is None
|
||||
assert report["devices"][0]["note"] == "No MQTT data received within the listen window."
|
||||
180
tests/test_filament_context.py
Normal file
180
tests/test_filament_context.py
Normal file
@@ -0,0 +1,180 @@
|
||||
import pytest
|
||||
from decimal import Decimal
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from bambu_run.models import Printer, PrinterMetrics, FilamentSnapshot
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def logged_in_client(client, django_user_model):
|
||||
user = django_user_model.objects.create_user(username="tester", password="pw")
|
||||
client.force_login(user)
|
||||
return client
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_filaments_carry_ams_unit_info(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
metric = PrinterMetrics.objects.create(device=printer, timestamp=timezone.now())
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=0, ams_type="AMS",
|
||||
type="PLA", remain_percent=80,
|
||||
)
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=128, ams_type="AMS HT",
|
||||
type="PA-CF", remain_percent=50,
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
filaments = resp.context["stats"]["filaments"]
|
||||
assert len(filaments) == 2
|
||||
units = {(f["ams_unit_id"], f["ams_type"]) for f in filaments}
|
||||
assert units == {(0, "AMS"), (128, "AMS HT")}
|
||||
|
||||
ams_units = resp.context["stats"]["ams_units"]
|
||||
assert ams_units == [
|
||||
{"ams_unit_id": 0, "ams_type": "AMS"},
|
||||
{"ams_unit_id": 128, "ams_type": "AMS HT"},
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_filament_timeline_keeps_same_tray_id_units_separate(logged_in_client):
|
||||
from bambu_run.views import PrinterDashboardView
|
||||
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
metric = PrinterMetrics.objects.create(device=printer, timestamp=timezone.now())
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=0, ams_type="AMS",
|
||||
type="PLA", sub_type="PLA Basic", color="FF0000", remain_percent=80,
|
||||
)
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=128, ams_type="AMS HT",
|
||||
type="PLA", sub_type="PLA Basic", color="FF0000", remain_percent=50,
|
||||
)
|
||||
|
||||
view = PrinterDashboardView()
|
||||
timeline = view._prepare_filament_timeline(PrinterMetrics.objects.filter(pk=metric.pk))
|
||||
|
||||
assert len(timeline) == 2
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_renders_unit_pills_and_badges_with_multiple_units(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
metric = PrinterMetrics.objects.create(device=printer, timestamp=timezone.now())
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=0, ams_type="AMS",
|
||||
type="PLA", color="FF0000FF", remain_percent=80,
|
||||
)
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=128, ams_type="AMS HT",
|
||||
type="PA-CF", color="00FF00FF", remain_percent=50,
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
html = resp.content.decode()
|
||||
assert "ams-filter-pills" in html
|
||||
assert "ams-badge-ams" in html
|
||||
assert "ams-badge-ams-ht" in html
|
||||
assert 'data-ams-unit-id="0"' in html
|
||||
assert 'data-ams-unit-id="128"' in html
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_groups_filaments_by_ams_unit(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
metric = PrinterMetrics.objects.create(
|
||||
device=printer, timestamp=timezone.now(),
|
||||
ams_units=[
|
||||
{"unit_id": "0", "ams_type": "AMS 2 Pro", "humidity": 5, "temp": 22.5},
|
||||
{"unit_id": "128", "ams_type": "AMS HT", "humidity": 8, "temp": 60.0},
|
||||
],
|
||||
)
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=0, ams_type="AMS 2 Pro",
|
||||
type="ABS", remain_percent=80,
|
||||
)
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=1, ams_unit_id=0, ams_type="AMS 2 Pro",
|
||||
type="ABS", remain_percent=60,
|
||||
)
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=128, ams_type="AMS HT",
|
||||
type="PA-CF", remain_percent=50,
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
groups = resp.context["stats"]["ams_groups"]
|
||||
assert len(groups) == 2
|
||||
|
||||
ams2pro_group, ht_group = groups
|
||||
assert ams2pro_group["unit_id"] == 0
|
||||
assert ams2pro_group["label"] == "AMS 2 Pro (Unit 0)"
|
||||
assert ams2pro_group["humidity"] == 5
|
||||
assert ams2pro_group["temp"] == 22.5
|
||||
assert len(ams2pro_group["filaments"]) == 2
|
||||
|
||||
assert ht_group["unit_id"] == 128
|
||||
assert ht_group["label"] == "AMS HT (Unit 128)"
|
||||
assert ht_group["humidity"] == 8
|
||||
assert len(ht_group["filaments"]) == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_renders_wide_and_compact_panels(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
metric = PrinterMetrics.objects.create(
|
||||
device=printer, timestamp=timezone.now(),
|
||||
ams_units=[
|
||||
{"unit_id": "0", "ams_type": "AMS 2 Pro", "humidity": 5, "temp": 22.5},
|
||||
{"unit_id": "128", "ams_type": "AMS HT", "humidity": 8, "temp": 60.0},
|
||||
],
|
||||
)
|
||||
for tray_id in range(4):
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=tray_id, ams_unit_id=0, ams_type="AMS 2 Pro",
|
||||
type="ABS", remain_percent=80,
|
||||
)
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=128, ams_type="AMS HT",
|
||||
type="PA-CF", remain_percent=50,
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
html = resp.content.decode()
|
||||
assert "ams-group--wide" in html
|
||||
assert "ams-group--compact" in html
|
||||
assert "AMS 2 Pro (Unit 0)" in html
|
||||
assert "AMS HT (Unit 128)" in html
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_hides_unit_pills_with_single_unit(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
metric = PrinterMetrics.objects.create(device=printer, timestamp=timezone.now())
|
||||
FilamentSnapshot.objects.create(
|
||||
printer_metric=metric, tray_id=0, ams_unit_id=0, ams_type="AMS",
|
||||
type="PLA", color="FF0000FF", remain_percent=80,
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert "ams-filter-pills" not in resp.content.decode()
|
||||
121
tests/test_hotend_collection.py
Normal file
121
tests/test_hotend_collection.py
Normal file
@@ -0,0 +1,121 @@
|
||||
import pytest
|
||||
|
||||
from bambu_run.management.commands.bambu_collector import Command, DeviceSession, resolve_printer_device
|
||||
from bambu_run.models import Hotend, HotendSnapshot, PrinterMetrics
|
||||
|
||||
|
||||
class FakeClient:
|
||||
"""Stub in place of BambuPrinter — returns canned snapshots, no real MQTT."""
|
||||
|
||||
def __init__(self, snapshots):
|
||||
self._snapshots = snapshots
|
||||
self._index = 0
|
||||
self._client = None
|
||||
|
||||
def get_snapshot(self):
|
||||
snap = self._snapshots[min(self._index, len(self._snapshots) - 1)]
|
||||
self._index += 1
|
||||
return snap
|
||||
|
||||
|
||||
def make_session(device_id, name, snapshots):
|
||||
printer = resolve_printer_device(device_id, {"name": name, "dev_product_name": "H2C"})
|
||||
return DeviceSession(device_id=device_id, client=FakeClient(snapshots), printer=printer)
|
||||
|
||||
|
||||
def hotends_snapshot(used_time=11472, wear=100.0):
|
||||
return {
|
||||
"gcode_state": "IDLE",
|
||||
"hotends": [
|
||||
{
|
||||
"raw_id": 21, "serial_number": "20D06A5B2918952", "nozzle_type": "HS01",
|
||||
"diameter": 0.4, "fila_id": "GFA01", "color": "FFFFFF",
|
||||
"used_time_seconds": used_time, "wear_percent": wear, "stat": 0,
|
||||
"is_toolhead": False, "is_empty": False, "slot_number": 6,
|
||||
},
|
||||
{
|
||||
"raw_id": 1, "serial_number": "N/A", "nozzle_type": "HS01",
|
||||
"diameter": 0.4, "fila_id": "", "color": None,
|
||||
"used_time_seconds": 0, "wear_percent": 0.0, "stat": 0,
|
||||
"is_toolhead": False, "is_empty": True, "slot_number": None,
|
||||
},
|
||||
{
|
||||
"raw_id": 0, "serial_number": "20D06A5C0426280", "nozzle_type": "HS01",
|
||||
"diameter": 0.4, "fila_id": "GFA00", "color": "FEC600",
|
||||
"used_time_seconds": 93490, "wear_percent": 100.0, "stat": 0,
|
||||
"is_toolhead": True, "is_empty": False, "slot_number": None,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_first_poll_creates_one_hotend_per_non_empty_entry():
|
||||
session = make_session("SERIAL-A", "Printer A", [hotends_snapshot()])
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session)
|
||||
|
||||
hotends = Hotend.objects.filter(printer=session.printer)
|
||||
assert hotends.count() == 2 # empty bay (sn="N/A") skipped
|
||||
|
||||
rack = hotends.get(serial_number="20D06A5B2918952")
|
||||
assert rack.raw_id == 21
|
||||
assert rack.slot_number == 6
|
||||
assert rack.is_toolhead is False
|
||||
assert rack.used_time_seconds == 11472
|
||||
assert rack.wear_percent == 100.0
|
||||
assert rack.nozzle_type == "HS01"
|
||||
assert rack.last_filament_profile_id == "GFA01"
|
||||
assert rack.last_color == "FFFFFF"
|
||||
|
||||
toolhead = hotends.get(serial_number="20D06A5C0426280")
|
||||
assert toolhead.is_toolhead is True
|
||||
assert toolhead.slot_number is None
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_first_poll_creates_one_snapshot_per_non_empty_hotend():
|
||||
session = make_session("SERIAL-A", "Printer A", [hotends_snapshot()])
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session)
|
||||
|
||||
metric = PrinterMetrics.objects.get(device=session.printer)
|
||||
assert HotendSnapshot.objects.filter(printer_metric=metric).count() == 2
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_collector_persists_raw_nozzle_info_including_non_inductive_entries():
|
||||
session = make_session("SERIAL-A", "Printer A", [hotends_snapshot()])
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session)
|
||||
|
||||
metric = PrinterMetrics.objects.get(device=session.printer)
|
||||
assert len(metric.nozzle_info) == 3 # all entries, including the empty/non-inductive one
|
||||
serials = {h["serial_number"] for h in metric.nozzle_info}
|
||||
assert serials == {"20D06A5B2918952", "N/A", "20D06A5C0426280"}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_second_poll_updates_existing_hotend_instead_of_duplicating():
|
||||
session = make_session(
|
||||
"SERIAL-A", "Printer A",
|
||||
[hotends_snapshot(used_time=11472, wear=100.0), hotends_snapshot(used_time=11500, wear=100.0)],
|
||||
)
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session)
|
||||
cmd._collect_printer_data(session)
|
||||
|
||||
hotends = Hotend.objects.filter(printer=session.printer, serial_number="20D06A5B2918952")
|
||||
assert hotends.count() == 1
|
||||
assert hotends.first().used_time_seconds == 11500
|
||||
|
||||
snapshots = HotendSnapshot.objects.filter(hotend=hotends.first())
|
||||
assert snapshots.count() == 2
|
||||
128
tests/test_hotend_dashboard.py
Normal file
128
tests/test_hotend_dashboard.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from bambu_run.models import Printer, PrinterMetrics, Hotend
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def logged_in_client(client, django_user_model):
|
||||
user = django_user_model.objects.create_user(username="tester", password="pw")
|
||||
client.force_login(user)
|
||||
return client
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_context_includes_hotends_toolhead_first(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
PrinterMetrics.objects.create(device=printer, timestamp=timezone.now())
|
||||
|
||||
Hotend.objects.create(
|
||||
printer=printer, serial_number="RACK-SN", raw_id=16, slot_number=1,
|
||||
is_toolhead=False, nozzle_type="HS01", used_time_seconds=3600, wear_percent=50,
|
||||
)
|
||||
Hotend.objects.create(
|
||||
printer=printer, serial_number="TOOLHEAD-SN", raw_id=0, slot_number=None,
|
||||
is_toolhead=True, nozzle_type="HS01", used_time_seconds=7200, wear_percent=80,
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
hotends = resp.context["stats"]["hotends"]
|
||||
assert len(hotends) == 2
|
||||
assert hotends[0].serial_number == "TOOLHEAD-SN"
|
||||
assert hotends[1].serial_number == "RACK-SN"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_context_includes_non_inductive_nozzle_positions(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
PrinterMetrics.objects.create(
|
||||
device=printer, timestamp=timezone.now(),
|
||||
nozzle_info=[
|
||||
{
|
||||
"raw_id": 1, "serial_number": "N/A", "nozzle_type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "", "color": None, "used_time_seconds": 0, "wear_percent": 0.0,
|
||||
"stat": 0, "is_toolhead": False, "is_empty": True, "slot_number": None,
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
positions = resp.context["stats"]["nozzle_positions"]
|
||||
assert len(positions) == 1
|
||||
assert positions[0]["nozzle_type"] == "HS01"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_omits_nozzle_positions_with_no_readable_data(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
PrinterMetrics.objects.create(
|
||||
device=printer, timestamp=timezone.now(),
|
||||
nozzle_info=[
|
||||
{
|
||||
"raw_id": 1, "serial_number": "N/A", "nozzle_type": "", "diameter": 0,
|
||||
"fila_id": "", "color": None, "used_time_seconds": 0, "wear_percent": 0.0,
|
||||
"stat": 0, "is_toolhead": False, "is_empty": True, "slot_number": None,
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
assert resp.context["stats"]["nozzle_positions"] == []
|
||||
assert "<h5>Hotends</h5>" not in resp.content.decode()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_renders_nozzle_position_without_serial_or_wear(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
PrinterMetrics.objects.create(
|
||||
device=printer, timestamp=timezone.now(),
|
||||
nozzle_info=[
|
||||
{
|
||||
"raw_id": 1, "serial_number": "N/A", "nozzle_type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "", "color": None, "used_time_seconds": 0, "wear_percent": 0.0,
|
||||
"stat": 0, "is_toolhead": False, "is_empty": True, "slot_number": None,
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
html = resp.content.decode()
|
||||
assert "Hotends" in html
|
||||
assert "HS01" in html
|
||||
assert "SN: N/A" not in html
|
||||
assert "SN N/A" not in html
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_renders_hotends_card(logged_in_client):
|
||||
printer = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
PrinterMetrics.objects.create(device=printer, timestamp=timezone.now())
|
||||
|
||||
Hotend.objects.create(
|
||||
printer=printer, serial_number="RACK-SN", raw_id=18, slot_number=3,
|
||||
is_toolhead=False, nozzle_type="HS01", diameter=0.4,
|
||||
used_time_seconds=3661, wear_percent=50, last_filament_profile_id="GFA01",
|
||||
last_color="DE4343",
|
||||
)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer.pk})
|
||||
)
|
||||
|
||||
html = resp.content.decode()
|
||||
assert "Hotends" in html
|
||||
assert "RACK-SN" in html
|
||||
assert "Slot 3" in html
|
||||
100
tests/test_hotend_parsing.py
Normal file
100
tests/test_hotend_parsing.py
Normal file
@@ -0,0 +1,100 @@
|
||||
from bambu_run.mqtt_client import PrinterState
|
||||
|
||||
|
||||
def real_nozzle_payload():
|
||||
"""Real captured device.nozzle payload from a live H2C with a Vortek rack
|
||||
(1x AMS, 1x AMS 2 Pro, 1x AMS HT physically connected — unrelated here).
|
||||
SN/used-time cross-checked against the user's Bambu Studio Hotends Info table."""
|
||||
return {
|
||||
"exist": 3997699,
|
||||
"src_id": 17,
|
||||
"tar_id": 17,
|
||||
"state": 0,
|
||||
"info": [
|
||||
{"id": 21, "sn": "20D06A5B2918952", "type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "GFA01", "color_m": "FFFFFFFF", "p_t": 11472, "wear": 128.0, "stat": 0, "tm": 350},
|
||||
{"id": 1, "sn": "N/A", "type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "", "color_m": "00000000", "p_t": 0, "wear": 0.0, "stat": 0, "tm": 0},
|
||||
{"id": 16, "sn": "20D06A5B2919219", "type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "GFA01", "color_m": "A3D8E1FF", "p_t": 105386, "wear": 128.0, "stat": 0, "tm": 350},
|
||||
{"id": 20, "sn": "20D06A590610257", "type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "GFG01", "color_m": "00000000", "p_t": 81506, "wear": 128.0, "stat": 0, "tm": 350},
|
||||
{"id": 18, "sn": "20D06A591506263", "type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "GFA01", "color_m": "DE4343FF", "p_t": 30962, "wear": 128.0, "stat": 0, "tm": 350},
|
||||
{"id": 0, "sn": "20D06A5C0426280", "type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "GFA00", "color_m": "FEC600FF", "p_t": 93490, "wear": 128.0, "stat": 0, "tm": 350},
|
||||
{"id": 19, "sn": "20D06A5C0207881", "type": "HS01", "diameter": 0.4,
|
||||
"fila_id": "GFA01", "color_m": "DE4343FF", "p_t": 1430, "wear": 128.0, "stat": 0, "tm": 350},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def make_data(nozzle_payload):
|
||||
return {"print": {"gcode_state": "IDLE", "device": {"nozzle": nozzle_payload}}}
|
||||
|
||||
|
||||
def test_snapshot_includes_one_hotend_per_nozzle_info_entry():
|
||||
state = PrinterState.from_mqtt_data(make_data(real_nozzle_payload()))
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
assert len(snapshot["hotends"]) == 7
|
||||
|
||||
|
||||
def test_hotend_fields_extracted_correctly():
|
||||
state = PrinterState.from_mqtt_data(make_data(real_nozzle_payload()))
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
by_sn = {h["serial_number"]: h for h in snapshot["hotends"]}
|
||||
h = by_sn["20D06A5B2919219"]
|
||||
|
||||
assert h["raw_id"] == 16
|
||||
assert h["nozzle_type"] == "HS01"
|
||||
assert h["diameter"] == 0.4
|
||||
assert h["fila_id"] == "GFA01"
|
||||
assert h["color"] == "A3D8E1" # alpha stripped
|
||||
assert h["used_time_seconds"] == 105386
|
||||
assert h["wear_percent"] == 100.0 # 128/128*100
|
||||
assert h["is_empty"] is False
|
||||
|
||||
|
||||
def test_id_zero_is_toolhead_and_resolves_slot_number():
|
||||
state = PrinterState.from_mqtt_data(make_data(real_nozzle_payload()))
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
by_sn = {h["serial_number"]: h for h in snapshot["hotends"]}
|
||||
toolhead = by_sn["20D06A5C0426280"]
|
||||
|
||||
assert toolhead["raw_id"] == 0
|
||||
assert toolhead["is_toolhead"] is True
|
||||
assert toolhead["slot_number"] is None # true bay address hidden while id==0 sentinel
|
||||
|
||||
|
||||
def test_rack_bay_ids_resolve_to_slot_numbers_one_through_six():
|
||||
state = PrinterState.from_mqtt_data(make_data(real_nozzle_payload()))
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
by_sn = {h["serial_number"]: h for h in snapshot["hotends"]}
|
||||
|
||||
assert by_sn["20D06A5B2919219"]["slot_number"] == 1 # raw_id 16
|
||||
assert by_sn["20D06A591506263"]["slot_number"] == 3 # raw_id 18
|
||||
assert by_sn["20D06A5C0207881"]["slot_number"] == 4 # raw_id 19
|
||||
assert by_sn["20D06A590610257"]["slot_number"] == 5 # raw_id 20
|
||||
assert by_sn["20D06A5B2918952"]["slot_number"] == 6 # raw_id 21
|
||||
|
||||
|
||||
def test_empty_bay_with_na_serial_is_flagged_empty():
|
||||
state = PrinterState.from_mqtt_data(make_data(real_nozzle_payload()))
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
by_sn = {h["serial_number"]: h for h in snapshot["hotends"]}
|
||||
empty = by_sn["N/A"]
|
||||
|
||||
assert empty["is_empty"] is True
|
||||
assert empty["is_toolhead"] is False
|
||||
|
||||
|
||||
def test_snapshot_hotends_empty_list_when_no_nozzle_payload():
|
||||
state = PrinterState.from_mqtt_data({"print": {"gcode_state": "IDLE"}})
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
assert snapshot["hotends"] == []
|
||||
104
tests/test_multi_ams_collection.py
Normal file
104
tests/test_multi_ams_collection.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import pytest
|
||||
|
||||
from bambu_run.management.commands.bambu_collector import Command, DeviceSession, resolve_printer_device
|
||||
from bambu_run.models import Filament, FilamentSnapshot, FilamentUsage, PrinterMetrics
|
||||
|
||||
|
||||
class FakeClient:
|
||||
"""Stub in place of BambuPrinter — returns canned snapshots, no real MQTT."""
|
||||
|
||||
def __init__(self, snapshots):
|
||||
self._snapshots = snapshots
|
||||
self._index = 0
|
||||
self._client = None
|
||||
|
||||
def get_snapshot(self):
|
||||
snap = self._snapshots[min(self._index, len(self._snapshots) - 1)]
|
||||
self._index += 1
|
||||
return snap
|
||||
|
||||
|
||||
def make_session(device_id, name, snapshots):
|
||||
printer = resolve_printer_device(device_id, {"name": name, "dev_product_name": "H2C"})
|
||||
return DeviceSession(device_id=device_id, client=FakeClient(snapshots), printer=printer)
|
||||
|
||||
|
||||
def two_unit_tray0_snapshot():
|
||||
"""Two AMS units (AMS unit_id=0, AMS HT unit_id=128) both report tray_id=0,
|
||||
with different filament types loaded — these must not collide."""
|
||||
return {
|
||||
"gcode_state": "IDLE",
|
||||
"ams_units": [
|
||||
{"unit_id": "0", "ams_type": "AMS", "humidity": 30, "temp": 25.0},
|
||||
{"unit_id": "128", "ams_type": "AMS HT", "humidity": 20, "temp": 60.0},
|
||||
],
|
||||
"filaments": [
|
||||
{
|
||||
"tray_id": 0, "type": "PLA", "sub_type": "PLA Basic", "color": "FF0000FF",
|
||||
"tray_uuid": "UUID-UNIT0-TRAY0",
|
||||
"remain_percent": 80, "ams_unit_id": 0, "ams_type": "AMS",
|
||||
},
|
||||
{
|
||||
"tray_id": 0, "type": "PA-CF", "sub_type": "PA6-CF", "color": "00FF00FF",
|
||||
"tray_uuid": "UUID-UNIT128-TRAY0",
|
||||
"remain_percent": 50, "ams_unit_id": 128, "ams_type": "AMS HT",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_two_ams_units_with_same_tray_id_create_distinct_snapshots():
|
||||
session = make_session("SERIAL-A", "Printer A", [two_unit_tray0_snapshot()])
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session)
|
||||
|
||||
metric = PrinterMetrics.objects.get(device=session.printer)
|
||||
snapshots = FilamentSnapshot.objects.filter(printer_metric=metric).order_by("ams_unit_id")
|
||||
|
||||
assert snapshots.count() == 2
|
||||
|
||||
ams_snap, ht_snap = snapshots
|
||||
assert ams_snap.tray_id == 0
|
||||
assert ams_snap.ams_unit_id == 0
|
||||
assert ams_snap.ams_type == "AMS"
|
||||
assert ams_snap.type == "PLA"
|
||||
|
||||
assert ht_snap.tray_id == 0
|
||||
assert ht_snap.ams_unit_id == 128
|
||||
assert ht_snap.ams_type == "AMS HT"
|
||||
assert ht_snap.type == "PA-CF"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_filament_usage_matches_correct_unit_when_tray_ids_collide():
|
||||
start_snapshot = two_unit_tray0_snapshot()
|
||||
start_snapshot.update({"gcode_state": "RUNNING", "subtask_name": "job_1", "print_percent": 1, "tray_now": "0"})
|
||||
|
||||
end_snapshot = two_unit_tray0_snapshot()
|
||||
end_snapshot["filaments"][0]["remain_percent"] = 70 # AMS unit 0 consumed
|
||||
end_snapshot["filaments"][1]["remain_percent"] = 50 # AMS HT unit 128 untouched
|
||||
end_snapshot.update({"gcode_state": "FINISH", "subtask_name": "job_1", "print_percent": 100})
|
||||
|
||||
session = make_session("SERIAL-A", "Printer A", [start_snapshot, end_snapshot])
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session)
|
||||
cmd._collect_printer_data(session)
|
||||
|
||||
usages = FilamentUsage.objects.filter(print_job__device=session.printer).order_by("ams_unit_id")
|
||||
# Both units reported tray_id=0 with a tracked filament loaded throughout the
|
||||
# job — usage is recorded per physical unit, not collapsed into one ambiguous row.
|
||||
assert usages.count() == 2
|
||||
|
||||
ams_usage, ht_usage = usages
|
||||
assert ams_usage.ams_unit_id == 0
|
||||
assert ams_usage.starting_percent == 80
|
||||
assert ams_usage.ending_percent == 70
|
||||
|
||||
assert ht_usage.ams_unit_id == 128
|
||||
assert ht_usage.starting_percent == 50
|
||||
assert ht_usage.ending_percent == 50
|
||||
90
tests/test_multi_device_collection.py
Normal file
90
tests/test_multi_device_collection.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import pytest
|
||||
|
||||
from bambu_run.management.commands.bambu_collector import (
|
||||
Command,
|
||||
DeviceSession,
|
||||
resolve_printer_device,
|
||||
)
|
||||
from bambu_run.models import PrintJob, PrinterMetrics
|
||||
|
||||
|
||||
class FakeClient:
|
||||
"""Stub in place of BambuPrinter — returns canned snapshots, no real MQTT."""
|
||||
|
||||
def __init__(self, snapshots):
|
||||
self._snapshots = snapshots
|
||||
self._index = 0
|
||||
self._client = None # cloud BambuClient handle used by cloud task sync
|
||||
|
||||
def get_snapshot(self):
|
||||
snap = self._snapshots[min(self._index, len(self._snapshots) - 1)]
|
||||
self._index += 1
|
||||
return snap
|
||||
|
||||
|
||||
def make_session(device_id, name, snapshots):
|
||||
printer = resolve_printer_device(device_id, {"name": name, "dev_product_name": "H2C"})
|
||||
return DeviceSession(device_id=device_id, client=FakeClient(snapshots), printer=printer)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_collects_metrics_against_the_correct_printer_per_session():
|
||||
session_a = make_session("SERIAL-A", "Printer A", [{"nozzle_temp": 200, "gcode_state": "IDLE"}])
|
||||
session_b = make_session("SERIAL-B", "Printer B", [{"nozzle_temp": 210, "gcode_state": "IDLE"}])
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session_a)
|
||||
cmd._collect_printer_data(session_b)
|
||||
|
||||
metric_a = PrinterMetrics.objects.get(device=session_a.printer)
|
||||
metric_b = PrinterMetrics.objects.get(device=session_b.printer)
|
||||
assert metric_a.nozzle_temp == 200
|
||||
assert metric_b.nozzle_temp == 210
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_print_job_tracking_is_isolated_per_session():
|
||||
session_a = make_session(
|
||||
"SERIAL-A",
|
||||
"Printer A",
|
||||
[
|
||||
{"gcode_state": "RUNNING", "subtask_name": "job_A", "print_percent": 10},
|
||||
{"gcode_state": "FINISH", "subtask_name": "job_A", "print_percent": 100},
|
||||
],
|
||||
)
|
||||
session_b = make_session("SERIAL-B", "Printer B", [{"gcode_state": "IDLE"}])
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session_a)
|
||||
cmd._collect_printer_data(session_b)
|
||||
cmd._collect_printer_data(session_a)
|
||||
|
||||
assert PrintJob.objects.filter(device=session_a.printer).count() == 1
|
||||
job = PrintJob.objects.get(device=session_a.printer)
|
||||
assert job.final_status == "FINISH"
|
||||
assert session_a.current_print_job is None
|
||||
|
||||
assert PrintJob.objects.filter(device=session_b.printer).count() == 0
|
||||
assert session_b.current_print_job is None
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_one_session_error_does_not_affect_another_session():
|
||||
session_a = make_session("SERIAL-A", "Printer A", [{"nozzle_temp": 200, "gcode_state": "IDLE"}])
|
||||
session_b = make_session("SERIAL-B", "Printer B", [{"nozzle_temp": 210, "gcode_state": "IDLE"}])
|
||||
|
||||
class ExplodingClient:
|
||||
def get_snapshot(self):
|
||||
raise RuntimeError("MQTT connection lost")
|
||||
|
||||
session_a.client = ExplodingClient()
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session_a)
|
||||
cmd._collect_printer_data(session_b)
|
||||
|
||||
assert session_a.error_count == 1
|
||||
assert PrinterMetrics.objects.filter(device=session_b.printer).exists()
|
||||
78
tests/test_printer_routing.py
Normal file
78
tests/test_printer_routing.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
|
||||
from bambu_run.models import Printer
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def logged_in_client(client, django_user_model):
|
||||
user = django_user_model.objects.create_user(username="tester", password="pw")
|
||||
client.force_login(user)
|
||||
return client
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_with_no_printers_shows_error(logged_in_client):
|
||||
resp = logged_in_client.get(reverse("bambu_run:printer_dashboard"))
|
||||
assert resp.status_code == 200
|
||||
assert "error" in resp.context
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_defaults_to_first_active_printer(logged_in_client):
|
||||
printer = Printer.objects.create(name="Only Printer", model="H2C", is_active=True)
|
||||
|
||||
resp = logged_in_client.get(reverse("bambu_run:printer_dashboard"))
|
||||
|
||||
assert resp.context["printer_device"].pk == printer.pk
|
||||
assert list(resp.context["all_printers"]) == [printer]
|
||||
# Switcher shows even with a single printer, as a hint that multi-printer exists.
|
||||
assert resp.context["show_printer_switcher"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_hides_switcher_with_zero_printers(logged_in_client):
|
||||
resp = logged_in_client.get(reverse("bambu_run:printer_dashboard"))
|
||||
|
||||
assert resp.context["show_printer_switcher"] is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_pk_route_shows_requested_printer(logged_in_client):
|
||||
Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
printer_b = Printer.objects.create(name="Printer B", model="X1C", is_active=True)
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": printer_b.pk})
|
||||
)
|
||||
|
||||
assert resp.context["printer_device"].pk == printer_b.pk
|
||||
assert resp.context["device_name"] == "Printer B"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dashboard_unknown_pk_returns_404(logged_in_client):
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_dashboard", kwargs={"pk": 99999})
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_api_pk_route_returns_only_requested_printer_data(logged_in_client):
|
||||
from bambu_run.models import PrinterMetrics
|
||||
from django.utils import timezone
|
||||
from decimal import Decimal
|
||||
|
||||
printer_a = Printer.objects.create(name="Printer A", model="H2C", is_active=True)
|
||||
printer_b = Printer.objects.create(name="Printer B", model="X1C", is_active=True)
|
||||
PrinterMetrics.objects.create(device=printer_a, timestamp=timezone.now(), nozzle_temp=Decimal("200"))
|
||||
PrinterMetrics.objects.create(device=printer_b, timestamp=timezone.now(), nozzle_temp=Decimal("210"))
|
||||
|
||||
resp = logged_in_client.get(
|
||||
reverse("bambu_run:printer_api", kwargs={"pk": printer_b.pk})
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["nozzle_temp"] == [210.0]
|
||||
78
tests/test_resolve_printer_device.py
Normal file
78
tests/test_resolve_printer_device.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import pytest
|
||||
|
||||
from bambu_run.management.commands.bambu_collector import resolve_printer_device
|
||||
from bambu_run.models import Printer
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_creates_new_printer_keyed_by_serial():
|
||||
printer = resolve_printer_device(
|
||||
"0309DA123456", {"name": "RNL-H2C", "dev_product_name": "H2C"}
|
||||
)
|
||||
|
||||
assert printer.serial_number == "0309DA123456"
|
||||
assert printer.name == "RNL-H2C"
|
||||
assert printer.model == "H2C"
|
||||
assert printer.is_active is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_second_call_with_same_serial_does_not_create_duplicate():
|
||||
first = resolve_printer_device("SERIAL-A", {"name": "Printer A", "dev_product_name": "H2C"})
|
||||
second = resolve_printer_device("SERIAL-A", {"name": "Printer A", "dev_product_name": "H2C"})
|
||||
|
||||
assert first.pk == second.pk
|
||||
assert Printer.objects.filter(serial_number="SERIAL-A").count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_two_different_serials_create_two_printers():
|
||||
a = resolve_printer_device("SERIAL-A", {"name": "Printer A", "dev_product_name": "H2C"})
|
||||
b = resolve_printer_device("SERIAL-B", {"name": "Printer B", "dev_product_name": "X1C"})
|
||||
|
||||
assert a.pk != b.pk
|
||||
assert Printer.objects.count() == 2
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_backfills_single_legacy_printer_with_null_serial():
|
||||
legacy = Printer.objects.create(
|
||||
name="Bambu Lab Printer", model="Bambu Lab", manufacturer="Bambu Lab", is_active=True
|
||||
)
|
||||
|
||||
resolved = resolve_printer_device("SERIAL-A", {"name": "RNL-H2C", "dev_product_name": "H2C"})
|
||||
|
||||
legacy.refresh_from_db()
|
||||
assert resolved.pk == legacy.pk
|
||||
assert legacy.serial_number == "SERIAL-A"
|
||||
assert Printer.objects.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_does_not_guess_when_multiple_legacy_printers_exist():
|
||||
Printer.objects.create(name="Legacy 1", model="Bambu Lab")
|
||||
Printer.objects.create(name="Legacy 2", model="Bambu Lab")
|
||||
|
||||
resolved = resolve_printer_device("SERIAL-A", {"name": "RNL-H2C", "dev_product_name": "H2C"})
|
||||
|
||||
assert resolved.serial_number == "SERIAL-A"
|
||||
assert Printer.objects.count() == 3
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_falls_back_to_generic_defaults_without_device_info():
|
||||
printer = resolve_printer_device("SERIAL-A", None)
|
||||
|
||||
assert printer.serial_number == "SERIAL-A"
|
||||
assert printer.name == "Bambu Lab Printer"
|
||||
assert printer.model == "Bambu Lab"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_updates_name_and_model_on_existing_printer_when_changed():
|
||||
resolve_printer_device("SERIAL-A", {"name": "Old Name", "dev_product_name": "H2C"})
|
||||
|
||||
updated = resolve_printer_device("SERIAL-A", {"name": "New Name", "dev_product_name": "H2C"})
|
||||
|
||||
assert updated.name == "New Name"
|
||||
assert Printer.objects.filter(serial_number="SERIAL-A").count() == 1
|
||||
43
tests/test_vortek_groundwork.py
Normal file
43
tests/test_vortek_groundwork.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import pytest
|
||||
|
||||
from bambu_run.mqtt_client import PrinterState
|
||||
from bambu_run.management.commands.bambu_collector import Command, DeviceSession, resolve_printer_device
|
||||
from bambu_run.models import PrinterMetrics
|
||||
|
||||
|
||||
def test_snapshot_includes_raw_device_payload_for_future_vortek_modeling():
|
||||
raw_device = {
|
||||
"extruder": {"info": [{"id": 0, "temp": 12058720}, {"id": 1, "temp": 11534560}]},
|
||||
"nozzle": {"info": [{"id": 0, "diameter": 0.4}]},
|
||||
}
|
||||
data = {"print": {"device": raw_device, "gcode_state": "IDLE"}}
|
||||
|
||||
state = PrinterState.from_mqtt_data(data)
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
assert snapshot["vortek_raw"] == raw_device
|
||||
|
||||
|
||||
def test_snapshot_vortek_raw_defaults_to_empty_dict_when_no_device_payload():
|
||||
state = PrinterState.from_mqtt_data({"print": {"gcode_state": "IDLE"}})
|
||||
snapshot = state.get_snapshot()
|
||||
|
||||
assert snapshot["vortek_raw"] == {}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_collector_persists_vortek_raw_onto_printer_metrics():
|
||||
printer = resolve_printer_device("SERIAL-A", {"name": "H2C", "dev_product_name": "H2C"})
|
||||
|
||||
class FakeClient:
|
||||
def get_snapshot(self):
|
||||
return {"gcode_state": "IDLE", "vortek_raw": {"extruder": {"info": []}}}
|
||||
|
||||
session = DeviceSession(device_id="SERIAL-A", client=FakeClient(), printer=printer)
|
||||
|
||||
cmd = Command()
|
||||
cmd.verbose = False
|
||||
cmd._collect_printer_data(session)
|
||||
|
||||
metric = PrinterMetrics.objects.get(device=printer)
|
||||
assert metric.vortek_raw == {"extruder": {"info": []}}
|
||||
5
tests/urls.py
Normal file
5
tests/urls.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("", include("bambu_run.urls")),
|
||||
]
|
||||
Reference in New Issue
Block a user