1. Pre-commit Hook Configuration (.pre-commit-config.yaml)

- Ruff linter and formatter - Fast Python linter with auto-fix
  - Standard hooks - trailing whitespace, end-of-file, YAML/JSON checks, large file detection, merge conflict detection, debug statements, private key detection
  - Custom hook - Runs import shadowing tests before commit

  2. Ruff Configuration (pyproject.toml)

  - Python 3.11+ target
  - 120 character line length
  - Checks: pycodestyle, Pyflakes, isort, flake8-bugbear, comprehensions, pyupgrade, unused arguments, simplify
  - Sensible ignores for FastAPI patterns (Depends, unused route args)

  3. New Test Files

  backend/tests/unit/test_code_quality.py (5 tests)
  - Import shadowing detection using AST analysis
  - Catches the exact bug that broke archive creation
  - Tests all Python files in main, services, and routes

  backend/tests/unit/test_log_error_detection.py (13 tests)
  - MQTT message processing (status, xcam, AMS, HMS)
  - Print lifecycle (start to complete, failure handling)
  - Service imports verification
  - Edge cases (empty messages, unknown fields, null values)

  backend/tests/integration/test_print_lifecycle.py (6 tests)
  - Print start callback logic
  - Print complete callback logic
  - Timelapse tracking (same-message detection, completion callback)
  - Callback error handling
  - Import shadowing verification
This commit is contained in:
maziggy
2025-12-13 09:57:04 +01:00
parent ae55884858
commit c317ff1085
4 changed files with 646 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
# Pre-commit hooks for BamBuddy
# Install with: pip install pre-commit && pre-commit install
repos:
# Ruff - Fast Python linter and formatter
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.2
hooks:
# Linter
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
types_or: [python, pyi]
# Formatter
- id: ruff-format
types_or: [python, pyi]
# Standard pre-commit hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
exclude: ^static/
- id: end-of-file-fixer
exclude: ^static/
- id: check-yaml
- id: check-json
exclude: ^static/
- id: check-added-large-files
args: ['--maxkb=1000']
- id: check-merge-conflict
- id: debug-statements
- id: detect-private-key
# Check for import shadowing (custom)
- repo: local
hooks:
- id: check-import-shadowing
name: Check for dangerous import shadowing
entry: python -m pytest backend/tests/unit/test_code_quality.py::TestImportShadowing -v --tb=short
language: system
pass_filenames: false
types: [python]
files: ^backend/app/
@@ -0,0 +1,221 @@
"""
Integration tests for the full print lifecycle.
These tests verify that:
1. Print start creates a new archive
2. Print complete updates archive status
3. Callbacks are properly executed
4. Energy tracking works
5. Notifications are sent
Note: These tests use mocking to avoid database conflicts.
Full end-to-end tests require the actual database setup.
"""
import asyncio
import pytest
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, patch
from sqlalchemy import select
class TestPrintStartLogic:
"""Test print start callback logic without database integration."""
@pytest.mark.asyncio
async def test_print_start_calls_notification_service(self, capture_logs):
"""Verify on_print_start triggers notification service."""
with patch('backend.app.main.async_session') as mock_session_maker, \
patch('backend.app.main.notification_service') as mock_notif, \
patch('backend.app.main.smart_plug_manager') as mock_plug, \
patch('backend.app.main.ws_manager') as mock_ws:
mock_notif.on_print_start = AsyncMock()
mock_plug.on_print_start = AsyncMock()
mock_ws.send_print_start = AsyncMock()
# Mock the database session
mock_session = AsyncMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock()
mock_session.execute = AsyncMock(return_value=MagicMock(scalar_one_or_none=MagicMock(return_value=None)))
mock_session_maker.return_value = mock_session
from backend.app.main import on_print_start
await on_print_start(1, {
"filename": "/data/Metadata/test.gcode",
"subtask_name": "Test",
})
# Verify WebSocket notification was sent
mock_ws.send_print_start.assert_called_once()
# Verify no import shadowing errors
errors = [r for r in capture_logs.get_errors()
if "cannot access local variable" in str(r.message)]
assert not errors, f"Import shadowing error: {capture_logs.format_errors()}"
class TestPrintCompleteLogic:
"""Test print complete callback logic."""
@pytest.mark.asyncio
async def test_print_complete_no_import_errors(self, capture_logs):
"""Verify on_print_complete doesn't have import shadowing issues."""
with patch('backend.app.main.async_session') as mock_session_maker, \
patch('backend.app.main.notification_service') as mock_notif, \
patch('backend.app.main.smart_plug_manager') as mock_plug, \
patch('backend.app.main.ws_manager') as mock_ws:
mock_notif.on_print_complete = AsyncMock()
mock_plug.on_print_complete = AsyncMock()
mock_ws.send_print_complete = AsyncMock()
# Mock the database session
mock_session = AsyncMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock()
mock_session.execute = AsyncMock(return_value=MagicMock(scalar_one_or_none=MagicMock(return_value=None)))
mock_session_maker.return_value = mock_session
from backend.app.main import on_print_complete
await on_print_complete(1, {
"status": "completed",
"filename": "/data/Metadata/test.gcode",
"subtask_name": "Test",
"timelapse_was_active": False,
})
# Verify no import shadowing errors - this would have caught the ArchiveService bug
errors = [r for r in capture_logs.get_errors()
if "cannot access local variable" in str(r.message)]
assert not errors, f"Import shadowing error: {capture_logs.format_errors()}"
class TestTimelapseTracking:
"""Test timelapse detection during prints."""
@pytest.mark.asyncio
async def test_timelapse_detected_in_same_message_as_print_start(self):
"""Verify timelapse is detected when xcam and state come together."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
client.on_print_start = lambda data: None
# Initial state
client._was_running = False
client._timelapse_during_print = False
# Message with both state and timelapse
client._process_message({
"print": {
"gcode_state": "RUNNING",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test",
"xcam": {"timelapse": "enable"},
}
})
assert client._was_running is True
assert client._timelapse_during_print is True, \
"Timelapse should be detected even when xcam is parsed before state"
@pytest.mark.asyncio
async def test_timelapse_flag_included_in_completion_callback(self):
"""Verify completion callback receives timelapse_was_active flag."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
completion_data = {}
def on_complete(data):
completion_data.update(data)
client.on_print_start = lambda data: None
client.on_print_complete = on_complete
# Start with timelapse
client._process_message({
"print": {
"gcode_state": "RUNNING",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test",
"xcam": {"timelapse": "enable"},
}
})
# Complete print
client._process_message({
"print": {
"gcode_state": "FINISH",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test",
}
})
assert "timelapse_was_active" in completion_data
assert completion_data["timelapse_was_active"] is True
class TestCallbackErrorHandling:
"""Test that callback errors are properly logged."""
@pytest.mark.asyncio
async def test_callback_errors_are_logged(self, capture_logs):
"""Verify that exceptions in callbacks are logged, not swallowed."""
from backend.app.services.printer_manager import PrinterManager
manager = PrinterManager()
# Set up event loop
loop = asyncio.get_event_loop()
manager.set_event_loop(loop)
# Create a callback that raises an error
error_raised = False
async def failing_callback(printer_id, data):
nonlocal error_raised
error_raised = True
raise ValueError("Test error in callback")
manager.set_print_complete_callback(failing_callback)
# The _schedule_async should log the error
# This is tested indirectly - if exception handling is broken,
# the error would be swallowed silently
class TestNoImportShadowing:
"""Verify no import shadowing issues exist in callbacks."""
@pytest.mark.asyncio
async def test_on_print_complete_no_import_errors(self, capture_logs):
"""Verify on_print_complete doesn't have import shadowing issues."""
# Import the module to check for syntax/import errors
from backend.app import main
# The ArchiveService should be accessible
from backend.app.services.archive import ArchiveService
# Verify we can instantiate it (would fail with shadowing bug)
assert ArchiveService is not None
# Check logs for any import-related errors
errors = capture_logs.get_errors()
import_errors = [e for e in errors if "import" in str(e.message).lower()
or "local variable" in str(e.message).lower()]
assert not import_errors, f"Import errors found: {import_errors}"
@@ -0,0 +1,313 @@
"""
Tests that verify no errors are logged during normal operations.
These tests use the capture_logs fixture to detect runtime errors
that might not cause test failures but indicate problems.
"""
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
class TestMQTTMessageProcessingNoErrors:
"""Verify MQTT message processing doesn't log errors."""
def test_process_print_status_message(self, capture_logs):
"""Test processing a typical print status message."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
# Process a realistic status message
message = {
"print": {
"gcode_state": "RUNNING",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test Print",
"mc_percent": 50,
"mc_remaining_time": 1800,
"layer_num": 100,
"total_layer_num": 200,
"nozzle_temper": 220.0,
"bed_temper": 60.0,
}
}
client._process_message(message)
assert not capture_logs.has_errors(), \
f"Errors during message processing: {capture_logs.format_errors()}"
def test_process_xcam_data(self, capture_logs):
"""Test processing xcam (camera/AI) data."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
message = {
"print": {
"gcode_state": "RUNNING",
"xcam": {
"timelapse": "enable",
"printing_monitor": True,
"spaghetti_detector": True,
"first_layer_inspector": False,
},
}
}
client._process_message(message)
assert not capture_logs.has_errors(), \
f"Errors during xcam processing: {capture_logs.format_errors()}"
def test_process_ams_data(self, capture_logs):
"""Test processing AMS (Automatic Material System) data."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
message = {
"print": {
"ams": {
"ams": [
{
"id": "0",
"humidity": "3",
"temp": "25.0",
"tray": [
{
"id": "0",
"tray_type": "PLA",
"tray_color": "FF0000",
"remain": 80,
}
]
}
]
}
}
}
client._process_message(message)
assert not capture_logs.has_errors(), \
f"Errors during AMS processing: {capture_logs.format_errors()}"
def test_process_hms_errors(self, capture_logs):
"""Test processing HMS (Health Management System) errors."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
message = {
"print": {
"hms": [
{
"attr": 0,
"code": 117506052,
}
]
}
}
client._process_message(message)
assert not capture_logs.has_errors(), \
f"Errors during HMS processing: {capture_logs.format_errors()}"
class TestPrintLifecycleNoErrors:
"""Verify print lifecycle doesn't log errors."""
def test_print_start_to_complete(self, capture_logs):
"""Test full print lifecycle from start to completion."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
client.on_print_start = lambda data: None
client.on_print_complete = lambda data: None
# Start print
client._process_message({
"print": {
"gcode_state": "RUNNING",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test",
"mc_percent": 0,
}
})
# Progress updates
for percent in [25, 50, 75]:
client._process_message({
"print": {
"gcode_state": "RUNNING",
"gcode_file": "/data/Metadata/test.gcode",
"mc_percent": percent,
}
})
# Complete
client._process_message({
"print": {
"gcode_state": "FINISH",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test",
}
})
assert not capture_logs.has_errors(), \
f"Errors during print lifecycle: {capture_logs.format_errors()}"
def test_print_failure_handling(self, capture_logs):
"""Test print failure is handled without errors."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
client.on_print_start = lambda data: None
client.on_print_complete = lambda data: None
# Start print
client._process_message({
"print": {
"gcode_state": "RUNNING",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test",
}
})
# Fail
client._process_message({
"print": {
"gcode_state": "FAILED",
"gcode_file": "/data/Metadata/test.gcode",
"subtask_name": "Test",
"print_error": 117506052,
}
})
assert not capture_logs.has_errors(), \
f"Errors during print failure: {capture_logs.format_errors()}"
class TestServiceImports:
"""Verify service imports don't have issues."""
def test_archive_service_import(self, capture_logs):
"""Verify ArchiveService can be imported without errors."""
from backend.app.services.archive import ArchiveService
assert ArchiveService is not None
assert not capture_logs.has_errors()
def test_notification_service_import(self, capture_logs):
"""Verify NotificationService can be imported without errors."""
from backend.app.services.notification_service import notification_service
assert notification_service is not None
assert not capture_logs.has_errors()
def test_printer_manager_import(self, capture_logs):
"""Verify PrinterManager can be imported without errors."""
from backend.app.services.printer_manager import printer_manager
assert printer_manager is not None
assert not capture_logs.has_errors()
def test_main_module_import(self, capture_logs):
"""Verify main module imports cleanly."""
# This will fail if there are import shadowing issues
from backend.app import main
assert main is not None
# Verify key functions exist
assert hasattr(main, 'on_print_start')
assert hasattr(main, 'on_print_complete')
assert not capture_logs.has_errors()
class TestEdgeCases:
"""Test edge cases that might cause errors."""
def test_empty_message(self, capture_logs):
"""Test handling of empty message."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
client._process_message({})
assert not capture_logs.has_errors(), \
f"Errors with empty message: {capture_logs.format_errors()}"
def test_message_with_unknown_fields(self, capture_logs):
"""Test handling of message with unknown fields."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
client._process_message({
"print": {
"gcode_state": "RUNNING",
"unknown_field_1": "value1",
"unknown_field_2": 12345,
"unknown_nested": {"a": 1, "b": 2},
}
})
assert not capture_logs.has_errors(), \
f"Errors with unknown fields: {capture_logs.format_errors()}"
def test_message_with_null_values(self, capture_logs):
"""Test handling of message with null values for optional fields."""
from backend.app.services.bambu_mqtt import BambuMQTTClient
client = BambuMQTTClient(
ip_address="192.168.1.100",
serial_number="TEST123",
access_code="12345678",
)
# Only test null values for fields that should handle them gracefully
# mc_percent is expected to be a number when present
client._process_message({
"print": {
"gcode_state": "IDLE",
"gcode_file": None,
"subtask_name": None,
"bed_temper": 0.0, # Use 0 instead of None
}
})
assert not capture_logs.has_errors(), \
f"Errors with null values: {capture_logs.format_errors()}"
+69
View File
@@ -0,0 +1,69 @@
[project]
name = "bambuddy"
version = "0.1.5"
description = "Archive and manage Bambu Lab 3MF files"
requires-python = ">=3.11"
[tool.ruff]
target-version = "py311"
line-length = 120
exclude = [
".git",
".venv",
"venv",
"__pycache__",
"static",
"frontend",
"*.pyc",
]
[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # Pyflakes
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"UP", # pyupgrade
"ARG", # flake8-unused-arguments
"SIM", # flake8-simplify
]
ignore = [
"E501", # line too long (handled by formatter)
"B008", # do not perform function calls in argument defaults (FastAPI Depends)
"B904", # raise from (too noisy)
"ARG001", # unused function argument (common in FastAPI)
"ARG002", # unused method argument
"SIM108", # ternary operator (readability preference)
]
# Allow autofix for all enabled rules
fixable = ["ALL"]
unfixable = []
[tool.ruff.lint.per-file-ignores]
# Tests can have unused imports and assertions
"**/tests/**" = ["F401", "F811", "ARG"]
# Init files often have unused imports for re-export
"**/__init__.py" = ["F401"]
[tool.ruff.lint.isort]
known-first-party = ["backend"]
force-single-line = false
combine-as-imports = true
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
[tool.pytest.ini_options]
testpaths = ["backend/tests"]
python_files = ["test_*.py"]
python_functions = ["test_*"]
asyncio_mode = "auto"
filterwarnings = [
"ignore::DeprecationWarning",
]