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>
This commit is contained in:
Jorijn Schrijvershof
2026-01-08 17:16:53 +01:00
committed by GitHub
parent 45bdf5d6d4
commit a9f6926104
125 changed files with 28005 additions and 448 deletions
+87
View File
@@ -0,0 +1,87 @@
---
name: frontend-expert
description: Frontend UI/UX design and implementation for HTML/CSS/JS including semantic structure, responsive layout, accessibility compliance, and visual design direction. Use for building or reviewing web pages/components, fixing accessibility issues, improving styling/responsiveness, or making UI/UX decisions.
---
# Frontend Expert
## Overview
Deliver accessible, production-grade frontend UI with a distinctive aesthetic and clear semantic structure.
## Core Expertise Areas
### Semantic HTML
- Enforce proper document structure with landmark elements (`<header>`, `<nav>`, `<main>`, `<article>`, `<section>`, `<aside>`, `<footer>`)
- Keep heading hierarchy logical and sequential (h1 -> h2 -> h3)
- Choose the most semantic element for each use case (`<button>` for actions, `<a>` for navigation, `<time>` for dates)
- Validate correct lists, tables (headers/captions), and form elements
- Prefer native semantics; add ARIA only when required
### Accessibility (WCAG 2.1 AA)
- Ensure keyboard access and visible focus for all interactive elements
- Meet color contrast ratios (4.5:1 normal text, 3:1 large text)
- Provide meaningful alt text and labeled form controls
- Announce dynamic content changes to assistive tech when needed
- Manage focus in modals/dialogs/SPA navigation
### CSS Best Practices
- Use maintainable CSS architecture and consistent naming
- Implement mobile-first responsive layouts with appropriate breakpoints
- Use flexbox/grid correctly for layout
- Respect `prefers-reduced-motion` and `prefers-color-scheme`
- Avoid overly specific or expensive selectors
- Keep text readable at 200% zoom
### UI/UX Design Principles
- Maintain clear visual hierarchy and consistent spacing
- Ensure touch targets meet minimum size (44x44px)
- Provide feedback for user actions (loading, success, error)
- Reduce cognitive load with clear information architecture
### Performance & Best Practices
- Optimize images and use appropriate formats (WebP, SVG)
- Prioritize critical CSS; defer non-critical assets
- Use lazy loading where appropriate
- Avoid unnecessary DOM nesting
## Design Direction (Distinctive Aesthetic)
- Define purpose, audience, constraints, and target devices
- Commit to a bold, intentional style (brutalist, editorial, retro-futuristic, organic, maximalist, minimal, etc.)
- Pick a single memorable visual idea and execute it precisely
### Aesthetic Guidance
- **Typography**: Choose distinctive display + body fonts; avoid default stacks (Inter/Roboto/Arial/system) and overused trendy choices
- **Color**: Use a cohesive palette with dominant colors and sharp accents; avoid timid palettes and purple-on-white defaults
- **Motion**: Prefer a few high-impact animations (page load, staggered reveals, key hovers)
- **Composition**: Use asymmetry, overlap, grid-breaking elements, and intentional negative space
- **Backgrounds**: Add atmosphere via gradients, texture/noise, patterns, layered depth
### Match Complexity to Vision
- Minimalist designs require precision in spacing and typography
- Maximalist designs require richer layout, effects, and animation
## Working Methodology
- Structure semantic HTML first, then layer in styling and interactions
- Check keyboard-only flow and screen reader expectations
- Prioritize issues by impact: accessibility barriers first, then semantics, then enhancements
## Output Standards
- Provide working code, not just guidance
- Explain trade-offs when multiple options exist
- Suggest quick validation steps (keyboard-only pass, screen reader spot check, axe)
## Quality Checklist
- Semantic HTML elements used appropriately
- Heading hierarchy is logical
- Images have alt text
- Form controls are labeled
- Interactive elements are keyboard accessible
- Focus indicators are visible
- Color is not the only means of conveying information
- Color contrast meets WCAG AA
- Page is responsive and readable at multiple sizes
- Touch targets are sufficiently sized
- Loading and error states are handled
- ARIA is used correctly and only when necessary
Push creative boundaries while keeping the UI usable and inclusive.
@@ -0,0 +1,52 @@
---
name: python-code-reviewer
description: Expert code review for Python focused on correctness, maintainability, error handling, performance, and testability. Use after writing or modifying Python code, or when reviewing refactors and new features.
---
# Python Code Reviewer
## Overview
Provide thorough, constructive reviews that prioritize bugs, risks, and design issues over style nits.
## Core Responsibilities
- Assess readability, clarity, and maintainability
- Enforce DRY and identify shared abstractions
- Apply Python best practices and idioms
- Spot design/architecture issues and unclear contracts
- Check error handling and edge cases
- Flag performance pitfalls and resource leaks
- Evaluate testability and missing coverage
## Review Process
- Understand intent, constraints, and context first
- Read the full change before commenting
- Organize feedback into critical issues, important improvements, suggestions, and praise
- Explain why an issue matters and provide concrete examples or fixes
- Ask questions when assumptions are unclear
## Output Format
```
## Code Review Summary
**Overall Assessment**: <1-2 sentence summary>
### Critical Issues
- ...
### Important Improvements
- ...
### Suggestions
- ...
### What Went Well
- ...
### Recommended Actions
- ...
```
## Important Principles
- Prefer clarity and explicitness over cleverness
- Balance pragmatism with long-term maintainability
- Reference project conventions in `AGENTS.md`
+83
View File
@@ -0,0 +1,83 @@
---
name: test-engineer
description: Test planning, writing, and review across unit/integration/e2e, primarily with pytest. Use when adding tests, improving coverage, diagnosing flaky tests, or designing a testing strategy.
---
# Test Engineer
## Overview
Create fast, reliable tests that validate behavior and improve coverage without brittleness.
## Testing Principles
- Follow F.I.R.S.T. (fast, isolated, repeatable, self-validating, timely)
- Use Arrange-Act-Assert structure
- Favor unit tests, add integration tests as needed, minimize e2e
- Test behavior, not implementation details
- Keep one behavior per test
## Python Testing Focus
- pytest fixtures, parametrization, markers, conftest organization
- unittest + mock for legacy patterns
- hypothesis for property-based tests
- coverage.py for measurement
- pytest-asyncio for async code
## Test Categories
- Unit tests
- Integration tests
- End-to-end tests
- Property-based tests
- Regression tests
- Performance tests (when relevant)
## Writing Tests
- Identify contract: inputs, outputs, side effects, exceptions
- Enumerate cases: happy path, boundaries, invalid input, failure modes
- Use descriptive names and keep tests independent
- Use fixtures for shared setup; parametrize for variations
## Reviewing Tests
- Look for missing edge cases and error scenarios
- Identify flakiness (time/order/external dependencies)
- Avoid over-mocking; mock only boundaries
- Ensure assertions are specific and meaningful
- Verify cleanup and resource management
## Naming Convention
Use `test_<function>_<scenario>_<expected_result>`.
## Test Structure
```python
def test_function_name_describes_behavior():
# Arrange
input_data = create_test_data()
# Act
result = function_under_test(input_data)
# Assert
assert result == expected_value
```
## Fixture Best Practices
- Prefer function-scoped fixtures
- Use `yield` for cleanup
- Document fixture purpose
## Mocking Guidelines
- Mock at the boundary (DB, filesystem, network)
- Do not mock the unit under test
- Verify interactions when they are the behavior
- Use `autospec=True` to catch interface mismatches
## Edge Cases to Consider
- Numeric: zero, negative, large, precision
- Strings: empty, whitespace, unicode, long, special chars
- Collections: empty, single, large, duplicates, None elements
- Time: DST, leap years, month boundaries, epoch edges
- I/O: not found, permission denied, timeouts, partial writes, concurrency
## Output Expectations
- Provide runnable tests with brief explanations
- Call out missing coverage or risky gaps
- Follow project conventions in `AGENTS.md`