Files
meshcore-stats/tests/charts/test_chart_render.py
T
Jorijn Schrijvershof a9f6926104 test: add comprehensive pytest test suite with 95% coverage (#29)
* test: add comprehensive pytest test suite with 95% coverage

Add full unit and integration test coverage for the meshcore-stats project:

- 1020 tests covering all modules (db, charts, html, reports, client, etc.)
- 95.95% code coverage with pytest-cov (95% threshold enforced)
- GitHub Actions CI workflow for automated testing on push/PR
- Proper mocking of external dependencies (meshcore, serial, filesystem)
- SVG snapshot infrastructure for chart regression testing
- Integration tests for collection and rendering pipelines

Test organization:
- tests/charts/: Chart rendering and statistics
- tests/client/: MeshCore client and connection handling
- tests/config/: Environment and configuration parsing
- tests/database/: SQLite operations and migrations
- tests/html/: HTML generation and Jinja templates
- tests/reports/: Report generation and formatting
- tests/retry/: Circuit breaker and retry logic
- tests/unit/: Pure unit tests for utilities
- tests/integration/: End-to-end pipeline tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore: add test-engineer agent configuration

Add project-local test-engineer agent for pytest test development,
coverage analysis, and test review tasks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: comprehensive test suite review with 956 tests analyzed

Conducted thorough review of all 956 test cases across 47 test files:

- Unit Tests: 338 tests (battery, metrics, log, telemetry, env, charts, html, reports, formatters)
- Config Tests: 53 tests (env loading, config file parsing)
- Database Tests: 115 tests (init, insert, queries, migrations, maintenance, validation)
- Retry Tests: 59 tests (circuit breaker, async retries, factory)
- Charts Tests: 76 tests (transforms, statistics, timeseries, rendering, I/O)
- HTML Tests: 81 tests (site generation, Jinja2, metrics builders, reports index)
- Reports Tests: 149 tests (location, JSON/TXT formatting, aggregation, counter totals)
- Client Tests: 63 tests (contacts, connection, meshcore availability, commands)
- Integration Tests: 22 tests (reports, collection, rendering pipelines)

Results:
- Overall Pass Rate: 99.7% (953/956)
- 3 tests marked for improvement (empty test bodies in client tests)
- 0 tests requiring fixes

Key findings documented in test_review/tests.md including quality
observations, F.I.R.S.T. principle adherence, and recommendations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: implement snapshot testing for charts and reports

Add comprehensive snapshot testing infrastructure:

SVG Chart Snapshots:
- Deterministic fixtures with fixed timestamps (2024-01-15 12:00:00)
- Tests for gauge/counter metrics in light/dark themes
- Empty chart and single-point edge cases
- Extended normalize_svg_for_snapshot_full() for reproducible comparisons

TXT Report Snapshots:
- Monthly/yearly report snapshots for repeater and companion
- Empty report handling tests
- Tests in tests/reports/test_snapshots.py

Infrastructure:
- tests/snapshots/conftest.py with shared fixtures
- UPDATE_SNAPSHOTS=1 environment variable for regeneration
- scripts/generate_snapshots.py for batch snapshot generation

Run `UPDATE_SNAPSHOTS=1 pytest tests/charts/test_chart_render.py::TestSvgSnapshots`
to generate initial snapshots.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: fix SVG normalization and generate initial snapshots

Fix normalize_svg_for_snapshot() to handle:
- clipPath IDs like id="p47c77a2a6e"
- url(#p...) references
- xlink:href="#p..." references
- <dc:date> timestamps

Generated initial snapshot files:
- 7 SVG chart snapshots (gauge, counter, empty, single-point in light/dark)
- 6 TXT report snapshots (monthly/yearly for repeater/companion + empty)

All 13 snapshot tests now pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: fix SVG normalization to preserve axis rendering

The SVG normalization was replacing all matplotlib-generated IDs with
the same value, causing duplicate IDs that broke SVG rendering:
- Font glyphs, clipPaths, and tick marks all got id="normalized"
- References couldn't resolve to the correct elements
- X and Y axes failed to render in normalized snapshots

Fix uses type-specific prefixes with sequential numbering:
- glyph_N for font glyphs (DejaVuSans-XX patterns)
- clip_N for clipPath definitions (p[0-9a-f]{8,} patterns)
- tick_N for tick marks (m[0-9a-f]{8,} patterns)

This ensures all IDs remain unique while still being deterministic
for snapshot comparison.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore: add coverage and pytest artifacts to gitignore

Add .coverage, .coverage.*, htmlcov/, and .pytest_cache/ to prevent
test artifacts from being committed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* style: fix all ruff lint errors across codebase

- Sort and organize imports (I001)
- Use modern type annotations (X | Y instead of Union, collections.abc)
- Remove unused imports (F401)
- Combine nested if statements (SIM102)
- Use ternary operators where appropriate (SIM108)
- Combine nested with statements (SIM117)
- Use contextlib.suppress instead of try-except-pass (SIM105)
- Add noqa comments for intentional SIM115 violations (file locks)
- Add TYPE_CHECKING import for forward references
- Fix exception chaining (B904)

All 1033 tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: add TDD workflow and pre-commit requirements to CLAUDE.md

- Add mandatory test-driven development workflow (write tests first)
- Add pre-commit requirements (must run lint and tests before committing)
- Document test organization and running commands
- Document 95% coverage requirement

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: resolve mypy type checking errors with proper structural fixes

- charts.py: Create PeriodConfig dataclass for type-safe period configuration,
  use mdates.date2num() for matplotlib datetime handling, fix x-axis limits
  for single-point charts
- db.py: Add explicit int() conversion with None handling for SQLite returns
- env.py: Add class-level type annotations to Config class
- html.py: Add MetricDisplay TypedDict, fix import order, add proper type
  annotations for table data functions
- meshcore_client.py: Add return type annotation

Update tests to use new dataclass attribute access and regenerate SVG
snapshots. Add mypy step to CLAUDE.md pre-commit requirements.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: cast Jinja2 template.render() to str for mypy

Jinja2's type stubs declare render() as returning Any, but it actually
returns str. Wrap with str() to satisfy mypy's no-any-return check.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* ci: improve workflow security and reliability

- test.yml: Pin all actions by SHA, add concurrency control to cancel
  in-progress runs on rapid pushes
- release-please.yml: Pin action by SHA, add 10-minute timeout
- conftest.py: Fix snapshot_base_time to use explicit UTC timezone for
  consistent behavior across CI and local environments

Regenerate SVG snapshots with UTC-aware timestamps.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: add mypy command to permissions in settings.local.json

* test: add comprehensive script tests with coroutine warning fixes

- Add tests/scripts/ with tests for collect_companion, collect_repeater,
  and render scripts (1135 tests total, 96% coverage)
- Fix unawaited coroutine warnings by using AsyncMock properly for async
  functions and async_context_manager_factory fixture for context managers
- Add --cov=scripts to CI workflow and pyproject.toml coverage config
- Omit scripts/generate_snapshots.py from coverage (dev utility)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: migrate claude setup to codex skills

* feat: migrate dependencies to uv (#31)

* fix: run tests through uv

* test: fix ruff lint issues in tests

Consolidate patch context managers and clean unused imports/variables

Use datetime.UTC in snapshot fixtures

* test: avoid unawaited async mocks in entrypoint tests

* ci: replace codecov with github coverage artifacts

Add junit XML output and coverage summary in job output

Upload HTML and XML coverage artifacts (3.12 only) on every run

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-08 17:16:53 +01:00

419 lines
14 KiB
Python

"""Tests for SVG chart rendering."""
import os
from datetime import datetime, timedelta
from pathlib import Path
import pytest
from meshmon.charts import (
CHART_THEMES,
DataPoint,
TimeSeries,
render_chart_svg,
)
from .conftest import extract_svg_data_attributes, normalize_svg_for_snapshot
class TestRenderChartSvg:
"""Tests for render_chart_svg function."""
def test_returns_svg_string(self, sample_timeseries, light_theme):
"""Returns valid SVG string."""
svg = render_chart_svg(sample_timeseries, light_theme)
assert svg.startswith("<?xml") or svg.startswith("<svg")
assert "</svg>" in svg
def test_includes_svg_namespace(self, sample_timeseries, light_theme):
"""SVG includes xmlns namespace."""
svg = render_chart_svg(sample_timeseries, light_theme)
assert 'xmlns="http://www.w3.org/2000/svg"' in svg
def test_respects_width_height(self, sample_timeseries, light_theme):
"""SVG respects specified dimensions."""
svg = render_chart_svg(sample_timeseries, light_theme, width=600, height=200)
# Check viewBox or dimensions in SVG attributes
assert "600" in svg or "viewBox" in svg
def test_uses_theme_colors(self, sample_timeseries, light_theme, dark_theme):
"""Different themes produce different colors."""
light_svg = render_chart_svg(sample_timeseries, light_theme)
dark_svg = render_chart_svg(sample_timeseries, dark_theme)
# Check theme colors are present
assert light_theme.line in light_svg or f"#{light_theme.line}" in light_svg
assert dark_theme.line in dark_svg or f"#{dark_theme.line}" in dark_svg
class TestEmptyChartRendering:
"""Tests for rendering empty charts."""
def test_empty_chart_renders(self, empty_timeseries, light_theme):
"""Empty time series renders without error."""
svg = render_chart_svg(empty_timeseries, light_theme)
assert "</svg>" in svg
def test_empty_chart_shows_message(self, empty_timeseries, light_theme):
"""Empty chart shows 'No data available' message."""
svg = render_chart_svg(empty_timeseries, light_theme)
assert "No data available" in svg
class TestDataPointsInjection:
"""Tests for data-points attribute injection."""
def test_includes_data_points(self, sample_timeseries, light_theme):
"""SVG includes data-points attribute."""
svg = render_chart_svg(sample_timeseries, light_theme)
assert "data-points=" in svg
def test_data_points_valid_json(self, sample_timeseries, light_theme):
"""data-points contains valid JSON array."""
svg = render_chart_svg(sample_timeseries, light_theme)
data = extract_svg_data_attributes(svg)
assert "points" in data
assert isinstance(data["points"], list)
def test_data_points_count_matches(self, sample_timeseries, light_theme):
"""data-points count matches time series points."""
svg = render_chart_svg(sample_timeseries, light_theme)
data = extract_svg_data_attributes(svg)
assert len(data["points"]) == len(sample_timeseries.points)
def test_data_points_structure(self, sample_timeseries, light_theme):
"""Each data point has ts and v keys."""
svg = render_chart_svg(sample_timeseries, light_theme)
data = extract_svg_data_attributes(svg)
for point in data["points"]:
assert "ts" in point
assert "v" in point
def test_includes_metadata_attributes(self, sample_timeseries, light_theme):
"""SVG includes metric, period, theme attributes."""
svg = render_chart_svg(sample_timeseries, light_theme)
data = extract_svg_data_attributes(svg)
assert data.get("metric") == "bat"
assert data.get("period") == "day"
assert data.get("theme") == "light"
def test_includes_axis_range_attributes(self, sample_timeseries, light_theme):
"""SVG includes x and y axis range attributes."""
svg = render_chart_svg(sample_timeseries, light_theme)
data = extract_svg_data_attributes(svg)
assert "x_start" in data
assert "x_end" in data
assert "y_min" in data
assert "y_max" in data
class TestYAxisLimits:
"""Tests for Y-axis limit handling."""
def test_fixed_y_limits(self, sample_timeseries, light_theme):
"""Fixed Y limits are applied."""
svg = render_chart_svg(
sample_timeseries, light_theme,
y_min=3.0, y_max=4.5
)
data = extract_svg_data_attributes(svg)
assert float(data["y_min"]) == 3.0
assert float(data["y_max"]) == 4.5
def test_auto_y_limits_with_padding(self, light_theme):
"""Auto Y limits add padding around data."""
now = datetime.now()
points = [
DataPoint(timestamp=now, value=10.0),
DataPoint(timestamp=now + timedelta(hours=1), value=20.0),
]
ts = TimeSeries(metric="test", role="repeater", period="day", points=points)
svg = render_chart_svg(ts, light_theme)
data = extract_svg_data_attributes(svg)
y_min = float(data["y_min"])
y_max = float(data["y_max"])
# Auto limits should extend beyond data range
assert y_min < 10.0
assert y_max > 20.0
class TestXAxisLimits:
"""Tests for X-axis limit handling."""
def test_fixed_x_limits(self, sample_timeseries, light_theme):
"""Fixed X limits are applied."""
x_start = datetime(2024, 1, 1, 0, 0, 0)
x_end = datetime(2024, 1, 2, 0, 0, 0)
svg = render_chart_svg(
sample_timeseries, light_theme,
x_start=x_start, x_end=x_end
)
data = extract_svg_data_attributes(svg)
assert int(data["x_start"]) == int(x_start.timestamp())
assert int(data["x_end"]) == int(x_end.timestamp())
class TestChartThemes:
"""Tests for chart theme constants."""
def test_light_theme_exists(self):
"""Light theme is defined."""
assert "light" in CHART_THEMES
def test_dark_theme_exists(self):
"""Dark theme is defined."""
assert "dark" in CHART_THEMES
def test_themes_have_required_colors(self):
"""Themes have all required color attributes."""
required = ["background", "canvas", "text", "axis", "grid", "line", "area"]
for theme in CHART_THEMES.values():
for attr in required:
assert hasattr(theme, attr), f"Theme missing {attr}"
assert getattr(theme, attr), f"Theme {attr} is empty"
def test_theme_colors_are_valid_hex(self):
"""Theme colors are valid hex strings."""
import re
hex_pattern = re.compile(r'^[0-9a-fA-F]{6,8}$')
for name, theme in CHART_THEMES.items():
for attr in ["background", "canvas", "text", "axis", "grid", "line", "area"]:
color = getattr(theme, attr)
assert hex_pattern.match(color), f"{name}.{attr} = {color} is not valid hex"
class TestSvgNormalization:
"""Tests for SVG snapshot normalization helper."""
def test_normalize_removes_matplotlib_ids(self, sample_timeseries, light_theme):
"""Normalization removes matplotlib-generated IDs."""
svg = render_chart_svg(sample_timeseries, light_theme)
normalized = normalize_svg_for_snapshot(svg)
# Should not have matplotlib's randomized IDs
import re
# Look for patterns like id="abc123-def456"
random_ids = re.findall(r'id="[a-z0-9]+-[0-9a-f]{8,}"', normalized)
assert len(random_ids) == 0
def test_normalize_preserves_data_attributes(self, sample_timeseries, light_theme):
"""Normalization preserves data-* attributes."""
svg = render_chart_svg(sample_timeseries, light_theme)
normalized = normalize_svg_for_snapshot(svg)
assert "data-metric=" in normalized
assert "data-points=" in normalized
def test_normalize_removes_matplotlib_comment(self, sample_timeseries, light_theme):
"""Normalization removes matplotlib version comment."""
svg = render_chart_svg(sample_timeseries, light_theme)
normalized = normalize_svg_for_snapshot(svg)
assert "Created with matplotlib" not in normalized
class TestSvgSnapshots:
"""Snapshot tests for SVG chart rendering.
These tests compare rendered SVG output against saved snapshots
to detect unintended changes in chart appearance.
To update snapshots, run: UPDATE_SNAPSHOTS=1 pytest tests/charts/test_chart_render.py
"""
@pytest.fixture
def update_snapshots(self):
"""Return True if snapshots should be updated."""
return os.environ.get("UPDATE_SNAPSHOTS", "").lower() in ("1", "true", "yes")
def _assert_snapshot_match(
self,
actual: str,
snapshot_path: Path,
update: bool,
) -> None:
"""Compare SVG against snapshot, with optional update mode."""
# Normalize for comparison
normalized = normalize_svg_for_snapshot(actual)
if update:
# Update mode: write normalized SVG to snapshot
snapshot_path.parent.mkdir(parents=True, exist_ok=True)
snapshot_path.write_text(normalized, encoding="utf-8")
pytest.skip(f"Snapshot updated: {snapshot_path}")
else:
# Compare mode
if not snapshot_path.exists():
# Create new snapshot if it doesn't exist
snapshot_path.parent.mkdir(parents=True, exist_ok=True)
snapshot_path.write_text(normalized, encoding="utf-8")
pytest.fail(
f"Snapshot created: {snapshot_path}\n"
f"Run tests again to verify, or set UPDATE_SNAPSHOTS=1 to regenerate."
)
expected = snapshot_path.read_text(encoding="utf-8")
if normalized != expected:
# Show first difference for debugging
norm_lines = normalized.splitlines()
exp_lines = expected.splitlines()
diff_info = []
for i, (n, e) in enumerate(zip(norm_lines, exp_lines, strict=False), 1):
if n != e:
diff_info.append(f"Line {i} differs:")
diff_info.append(f" Expected: {e[:100]}...")
diff_info.append(f" Actual: {n[:100]}...")
if len(diff_info) > 12:
diff_info.append(" (more differences omitted)")
break
if len(norm_lines) != len(exp_lines):
diff_info.append(
f"Line count: expected {len(exp_lines)}, got {len(norm_lines)}"
)
pytest.fail(
f"Snapshot mismatch: {snapshot_path}\n"
f"Set UPDATE_SNAPSHOTS=1 to regenerate.\n\n"
+ "\n".join(diff_info)
)
def test_gauge_chart_light_theme(
self,
snapshot_gauge_timeseries,
light_theme,
snapshots_dir,
update_snapshots,
):
"""Gauge metric chart with light theme matches snapshot."""
svg = render_chart_svg(
snapshot_gauge_timeseries,
light_theme,
y_min=3.0,
y_max=4.2,
)
snapshot_path = snapshots_dir / "bat_day_light.svg"
self._assert_snapshot_match(svg, snapshot_path, update_snapshots)
def test_gauge_chart_dark_theme(
self,
snapshot_gauge_timeseries,
dark_theme,
snapshots_dir,
update_snapshots,
):
"""Gauge metric chart with dark theme matches snapshot."""
svg = render_chart_svg(
snapshot_gauge_timeseries,
dark_theme,
y_min=3.0,
y_max=4.2,
)
snapshot_path = snapshots_dir / "bat_day_dark.svg"
self._assert_snapshot_match(svg, snapshot_path, update_snapshots)
def test_counter_chart_light_theme(
self,
snapshot_counter_timeseries,
light_theme,
snapshots_dir,
update_snapshots,
):
"""Counter metric (rate) chart with light theme matches snapshot."""
svg = render_chart_svg(
snapshot_counter_timeseries,
light_theme,
)
snapshot_path = snapshots_dir / "nb_recv_day_light.svg"
self._assert_snapshot_match(svg, snapshot_path, update_snapshots)
def test_counter_chart_dark_theme(
self,
snapshot_counter_timeseries,
dark_theme,
snapshots_dir,
update_snapshots,
):
"""Counter metric (rate) chart with dark theme matches snapshot."""
svg = render_chart_svg(
snapshot_counter_timeseries,
dark_theme,
)
snapshot_path = snapshots_dir / "nb_recv_day_dark.svg"
self._assert_snapshot_match(svg, snapshot_path, update_snapshots)
def test_empty_chart_light_theme(
self,
snapshot_empty_timeseries,
light_theme,
snapshots_dir,
update_snapshots,
):
"""Empty chart with 'No data available' matches snapshot."""
svg = render_chart_svg(
snapshot_empty_timeseries,
light_theme,
)
snapshot_path = snapshots_dir / "empty_day_light.svg"
self._assert_snapshot_match(svg, snapshot_path, update_snapshots)
def test_empty_chart_dark_theme(
self,
snapshot_empty_timeseries,
dark_theme,
snapshots_dir,
update_snapshots,
):
"""Empty chart with dark theme matches snapshot."""
svg = render_chart_svg(
snapshot_empty_timeseries,
dark_theme,
)
snapshot_path = snapshots_dir / "empty_day_dark.svg"
self._assert_snapshot_match(svg, snapshot_path, update_snapshots)
def test_single_point_chart(
self,
snapshot_single_point_timeseries,
light_theme,
snapshots_dir,
update_snapshots,
):
"""Chart with single data point matches snapshot."""
svg = render_chart_svg(
snapshot_single_point_timeseries,
light_theme,
y_min=3.0,
y_max=4.2,
)
snapshot_path = snapshots_dir / "single_point_day_light.svg"
self._assert_snapshot_match(svg, snapshot_path, update_snapshots)