Files
meshcore-stats/tests/charts/conftest.py
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

340 lines
9.9 KiB
Python

"""Fixtures for chart tests."""
import json
import re
from datetime import UTC, datetime, timedelta
from pathlib import Path
import pytest
from meshmon.charts import (
CHART_THEMES,
DataPoint,
TimeSeries,
)
@pytest.fixture
def light_theme():
"""Light chart theme."""
return CHART_THEMES["light"]
@pytest.fixture
def dark_theme():
"""Dark chart theme."""
return CHART_THEMES["dark"]
@pytest.fixture
def sample_timeseries():
"""Sample time series with 24 hours of data."""
now = datetime.now()
points = []
for i in range(24):
ts = now - timedelta(hours=23 - i)
# Simulate battery voltage pattern (higher during day, lower at night)
value = 3.7 + 0.3 * abs(12 - i) / 12
points.append(DataPoint(timestamp=ts, value=value))
return TimeSeries(
metric="bat",
role="repeater",
period="day",
points=points,
)
@pytest.fixture
def empty_timeseries():
"""Empty time series (no data)."""
return TimeSeries(
metric="bat",
role="repeater",
period="day",
points=[],
)
@pytest.fixture
def single_point_timeseries():
"""Time series with single data point."""
now = datetime.now()
return TimeSeries(
metric="bat",
role="repeater",
period="day",
points=[DataPoint(timestamp=now, value=3.85)],
)
@pytest.fixture
def counter_timeseries():
"""Sample counter time series (for rate calculation testing)."""
now = datetime.now()
points = []
for i in range(24):
ts = now - timedelta(hours=23 - i)
# Simulate increasing counter
value = float(i * 100)
points.append(DataPoint(timestamp=ts, value=value))
return TimeSeries(
metric="nb_recv",
role="repeater",
period="day",
points=points,
)
@pytest.fixture
def week_timeseries():
"""Sample week time series for binning tests."""
now = datetime.now()
points = []
# One point per hour for 7 days = 168 points
for i in range(168):
ts = now - timedelta(hours=167 - i)
value = 3.7 + 0.2 * (i % 24) / 24
points.append(DataPoint(timestamp=ts, value=value))
return TimeSeries(
metric="bat",
role="repeater",
period="week",
points=points,
)
def normalize_svg_for_snapshot(svg: str) -> str:
"""Normalize SVG for deterministic snapshot comparison.
Handles matplotlib's dynamic ID generation while preserving
semantic content that affects chart appearance. Uses sequential
normalized IDs to preserve relationships between definitions
and references.
IMPORTANT: Each ID type gets its own prefix to maintain uniqueness:
- tick_N: matplotlib tick marks (m[0-9a-f]{8,})
- clip_N: clipPath definitions (p[0-9a-f]{8,})
- glyph_N: font glyph definitions (DejaVuSans-XX)
This ensures that:
1. All IDs remain unique (no duplicates)
2. References (xlink:href, url(#...)) correctly resolve
3. SVG renders identically to the original
"""
# Patterns for matplotlib's random IDs, each with its own prefix
# to maintain uniqueness across different ID types
id_type_patterns = [
(r'm[0-9a-f]{8,}', 'tick'), # matplotlib tick marks
(r'p[0-9a-f]{8,}', 'clip'), # matplotlib clipPaths
(r'DejaVuSans-[0-9a-f]+', 'glyph'), # font glyphs (hex-named)
]
# Find all IDs in the document
all_ids = re.findall(r'id="([^"]+)"', svg)
# Create mapping for IDs that match random patterns
# Use separate counters per type to ensure predictable naming
id_mapping = {}
type_counters = {prefix: 0 for _, prefix in id_type_patterns}
for id_val in all_ids:
if id_val in id_mapping:
continue
for pattern, prefix in id_type_patterns:
if re.fullmatch(pattern, id_val):
new_id = f"{prefix}_{type_counters[prefix]}"
id_mapping[id_val] = new_id
type_counters[prefix] += 1
break
# Replace all occurrences of mapped IDs (definitions and references)
# Process in a deterministic order (sorted by original ID) for consistency
for old_id, new_id in sorted(id_mapping.items()):
# Replace id definitions
svg = svg.replace(f'id="{old_id}"', f'id="{new_id}"')
# Replace url(#...) references
svg = svg.replace(f'url(#{old_id})', f'url(#{new_id})')
# Replace xlink:href references
svg = svg.replace(f'xlink:href="#{old_id}"', f'xlink:href="#{new_id}"')
# Replace href references (SVG 2.0 style without xlink prefix)
svg = svg.replace(f'href="#{old_id}"', f'href="#{new_id}"')
# Remove matplotlib version comment (changes between versions)
svg = re.sub(r'<!-- Created with matplotlib.*?-->', '', svg)
# Normalize dc:date timestamp (changes on each render)
svg = re.sub(r'<dc:date>[^<]+</dc:date>', '<dc:date>NORMALIZED</dc:date>', svg)
# Normalize whitespace (but preserve newlines for readability)
svg = re.sub(r'[ \t]+', ' ', svg)
svg = re.sub(r' ?\n ?', '\n', svg)
return svg.strip()
def extract_svg_data_attributes(svg: str) -> dict:
"""Extract data-* attributes from SVG for validation.
Args:
svg: SVG string
Returns:
Dict with extracted data attributes
"""
data = {}
# Extract data-points JSON
points_match = re.search(r'data-points="([^"]+)"', svg)
if points_match:
points_str = points_match.group(1).replace('&quot;', '"')
try:
data["points"] = json.loads(points_str)
except json.JSONDecodeError:
data["points_raw"] = points_str
# Extract other data attributes
for attr in ["data-metric", "data-period", "data-theme",
"data-x-start", "data-x-end", "data-y-min", "data-y-max"]:
match = re.search(rf'{attr}="([^"]+)"', svg)
if match:
key = attr.replace("data-", "").replace("-", "_")
data[key] = match.group(1)
return data
@pytest.fixture
def snapshots_dir():
"""Path to snapshots directory."""
return Path(__file__).parent.parent / "snapshots" / "svg"
@pytest.fixture
def sample_raw_points():
"""Raw points for aggregation testing."""
now = datetime.now()
return [
(now - timedelta(hours=2), 3.7),
(now - timedelta(hours=1, minutes=45), 3.72),
(now - timedelta(hours=1, minutes=30), 3.75),
(now - timedelta(hours=1), 3.8),
(now - timedelta(minutes=30), 3.82),
(now, 3.85),
]
# --- Deterministic fixtures for snapshot testing ---
# These use fixed timestamps to produce consistent SVG output
@pytest.fixture
def snapshot_base_time():
"""Fixed base time for deterministic snapshot tests.
Uses 2024-01-15 12:00:00 UTC as a stable reference point.
Explicitly set to UTC to ensure consistent behavior across all machines.
"""
return datetime(2024, 1, 15, 12, 0, 0, tzinfo=UTC)
@pytest.fixture
def snapshot_gauge_timeseries(snapshot_base_time):
"""Deterministic gauge time series for snapshot testing.
Creates a battery voltage pattern over 24 hours with fixed timestamps.
"""
points = []
for i in range(24):
ts = snapshot_base_time - timedelta(hours=23 - i)
# Simulate battery voltage pattern (higher during day, lower at night)
value = 3.7 + 0.3 * abs(12 - i) / 12
points.append(DataPoint(timestamp=ts, value=value))
return TimeSeries(
metric="bat",
role="repeater",
period="day",
points=points,
)
@pytest.fixture
def snapshot_counter_timeseries(snapshot_base_time):
"""Deterministic counter time series for snapshot testing.
Creates a packet rate pattern over 24 hours with fixed timestamps.
This represents rate values (already converted from counter deltas).
"""
points = []
for i in range(24):
ts = snapshot_base_time - timedelta(hours=23 - i)
# Simulate packet rate - higher during day hours (6-18)
hour = (i + 12) % 24 # Convert to actual hour of day
value = (
2.0 + (hour - 6) * 0.3 # 2.0 to 5.6 packets/min
if 6 <= hour <= 18
else 0.5 + (hour % 6) * 0.1 # 0.5 to 1.1 packets/min (night)
)
points.append(DataPoint(timestamp=ts, value=value))
return TimeSeries(
metric="nb_recv",
role="repeater",
period="day",
points=points,
)
@pytest.fixture
def snapshot_empty_timeseries():
"""Empty time series for snapshot testing."""
return TimeSeries(
metric="bat",
role="repeater",
period="day",
points=[],
)
@pytest.fixture
def snapshot_single_point_timeseries(snapshot_base_time):
"""Time series with single data point for snapshot testing."""
return TimeSeries(
metric="bat",
role="repeater",
period="day",
points=[DataPoint(timestamp=snapshot_base_time, value=3.85)],
)
def normalize_svg_for_snapshot_full(svg: str) -> str:
"""Extended SVG normalization for full snapshot comparison.
In addition to standard normalization, this also:
- Removes timestamps from data-points to allow content-only comparison
- Normalizes floating point precision
Used when you want to compare the visual structure but not exact data values.
"""
# Apply standard normalization first
svg = normalize_svg_for_snapshot(svg)
# Normalize data-points timestamps (keep structure, normalize values)
# This allows charts with different base times to still match structure
svg = re.sub(r'"ts":\s*\d+', '"ts":0', svg)
# Normalize floating point values to 2 decimal places in attributes
def normalize_float(match):
try:
val = float(match.group(1))
return f'{val:.2f}'
except ValueError:
return match.group(0)
svg = re.sub(r'(\d+\.\d{3,})', normalize_float, svg)
return svg