---
id: w8/m4/t008
title: Test coverage — meaningful tests for compaction + boundary-crossing period queries
worker: worker8
status: done
estimate: 30m
depends_on: [w8/m4/t006]
---

## Objective

Add unit/integration tests asserting real behavior and failure modes for the compaction routine and the hot-window read branch — not just the happy path exercised by t006's acceptance pass.

## Context

- `lego/backend/internal/store/usage_test.go` and `lego/backend/internal/usage/service_test.go` are the existing test homes to extend.
- Tests must assert real behavior/failure modes — no trivial or tautological assertions.

## Steps

1. Store-level test: `CompactUsage` never touches rows inside the hot window, is idempotent, and correctly sums quantities across all rows in a month.
2. Service-level test: `monthToDateAt` falls back to `usage_hourly` when `usage_monthly` has no row yet for a requested month (compaction hasn't run), and reads `usage_monthly` when it has.
3. Failure-mode test: a transaction failure mid-compaction leaves neither `usage_monthly` nor `usage_hourly` partially modified for that month (rollback correctness).

## Files

- `lego/backend/internal/store/usage_test.go`
- `lego/backend/internal/usage/service_test.go`

## Acceptance criteria

- [ ] `go test ./...` (from `lego/backend/`) covers idempotency, hot-window boundary, and rollback-on-failure — not just the seed-and-compact happy path.
- [ ] No snapshot-everything or tautological assertions.

## Out of scope

- End-to-end acceptance (already covered by t006).
