Skip to content

[codex] Stabilize DB-backed test lanes#193

Merged
dzarlax merged 1 commit into
mainfrom
fix-issue-182-db-backed-tests
Jun 30, 2026
Merged

[codex] Stabilize DB-backed test lanes#193
dzarlax merged 1 commit into
mainfrom
fix-issue-182-db-backed-tests

Conversation

@dzarlax

@dzarlax dzarlax commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Stabilizes the DB-backed integration test workflow for issue #182.

This PR makes DB tests explicit, separates fast routine checks from full domain sweeps, and reduces remote Postgres overhead in the EnergyBank verdict-band tests.

What changed

  • Added internal/testdb for DB-test activation, test-sized pgx pools, schema lifecycle, and schema reset helpers.
  • Added HEALTH_DB_TESTS=1 as the required opt-in flag for DB-backed tests.
  • Added storage.NewFromPool so tests can use bounded test pools instead of production tenant pool sizing.
  • Reworked storage/UI DB test fixtures to use shared schemas with reset-before-test and TestMain cleanup.
  • Added explicit Make lanes:
    • make test-unit
    • make test-db
    • make test-db-ui-fast
    • make test-db-energy-smoke
    • make test-db-readiness
    • make test-db-ui
    • make test-db-energy
    • make test-db-storage
  • Kept make test as the existing HTTP smoke POST via smoke-health-post.
  • Updated CI to run make test-unit.
  • Added automatic PR/push CI for stable DB lanes against a Postgres service container:
    • routine make test-db
    • full make test-db-ui
    • full make test-db-energy
  • Kept full storage DB as a manual workflow lane because the current full storage package exceeds the 120s timeout in chronic-load writer tests.
  • Batched EnergyBank verdict-band test seed inserts, bringing the full Energy DB lane down substantially.
  • Updated contributor/test coverage docs and added HTML implementation plans.

Runtime impact

  • make test-db is now the routine DB contract check and does not run full UI, full Energy, or full Storage sweeps.
  • Full domain sweeps remain available when relevant:
    • make test-db-ui
    • make test-db-energy
    • make test-db-storage

Validation

  • GOCACHE=/tmp/health-go-build-cache make test-unit
  • source ~/.health-db && HEALTH_DB_TESTS=1 GOCACHE=/tmp/health-go-build-cache make test-db
  • source ~/.health-db && HEALTH_DB_TESTS=1 GOCACHE=/tmp/health-go-build-cache make test-db-energy
  • source ~/.health-db && HEALTH_DB_TESTS=1 GOCACHE=/tmp/health-go-build-cache make test-db-storage (expected current blocker: timed out at 120s in TestChronicLoad_Integration_NoLeakageInPerDayBaseline)
  • env -u READINESS_TEST_DSN -u PGHOST -u PGDATABASE -u PGUSER -u PGPASSWORD HEALTH_DB_TESTS=1 GOCACHE=/tmp/health-go-build-cache go test ./internal/storage -run TestSaveNaiveBaseline_NilValueWithValidReasonPersists -count=1
  • git diff --check

Observed DB timings on the current remote Postgres path:

  • Routine make test-db: UI-fast 10.7s, Energy-smoke 5.8s, Readiness 16.1s
  • Full make test-db-energy: 21.8s after batching seed inserts
  • Full make test-db-ui: 41.4s
  • Full make test-db-storage: currently not automatic; timed out at 120s in chronic-load writer coverage

Notes

Readiness monitoring tests are intentionally excluded from the routine readiness lane for now. A trial that included TestLoadReadinessMonitoringSummary_* ran for 68.3s and failed on a state-sensitive OverallStatus assertion, so that path remains manual/full-storage until its fixture is narrowed.

Summary by CodeRabbit

  • New Features

    • Added dedicated database test lanes for faster unit checks, routine DB checks, and targeted UI, energy, and readiness coverage.
    • Updated CI to run unit tests separately from DB-backed tests, with isolated database support.
  • Bug Fixes

    • DB-backed tests now fail clearly when database access is unavailable instead of silently skipping.
    • Improved test coverage around readiness and onboarding behavior.
  • Documentation

    • Expanded contributor and test coverage guidance to match the new test workflow and CI behavior.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 22557424-4098-4539-aa27-c5bedfa7ec3a

📥 Commits

Reviewing files that changed from the base of the PR and between 088ad3b and 1ff5c1d.

📒 Files selected for processing (16)
  • .github/workflows/ci.yml
  • CONTRIBUTING.md
  • Makefile
  • docs/TEST_COVERAGE.md
  • docs/ai-plans/2026-06-29-db-test-closure-plan.html
  • docs/ai-plans/2026-06-29-db-test-harness-redesign.html
  • docs/ai-plans/2026-06-29-issue-182-db-backed-integration-tests.html
  • internal/storage/baseline_reason_test.go
  • internal/storage/chip_calibration_writer_test.go
  • internal/storage/db.go
  • internal/storage/energy_bands_test.go
  • internal/storage/recovery_stability_writer_integration_test.go
  • internal/testdb/testdb.go
  • internal/ui/admin_readiness_redesign_config_test.go
  • internal/ui/admin_readiness_redesign_monitoring_test.go
  • internal/ui/onboarding_handlers_test.go
✅ Files skipped from review due to trivial changes (5)
  • internal/ui/admin_readiness_redesign_monitoring_test.go
  • docs/TEST_COVERAGE.md
  • docs/ai-plans/2026-06-29-db-test-closure-plan.html
  • docs/ai-plans/2026-06-29-issue-182-db-backed-integration-tests.html
  • docs/ai-plans/2026-06-29-db-test-harness-redesign.html
🚧 Files skipped from review as they are similar to previous changes (9)
  • internal/storage/db.go
  • internal/ui/onboarding_handlers_test.go
  • internal/storage/chip_calibration_writer_test.go
  • internal/storage/baseline_reason_test.go
  • internal/ui/admin_readiness_redesign_config_test.go
  • CONTRIBUTING.md
  • internal/storage/energy_bands_test.go
  • internal/storage/recovery_stability_writer_integration_test.go
  • internal/testdb/testdb.go

📝 Walkthrough

Walkthrough

Adds shared Postgres test helpers, migrates storage and UI integration tests to shared schemas with resets, and updates Makefile, CI, docs, and planning files to use the new DB test lanes and gating.

Changes

DB Test Harness Redesign & Test Lane Structure

Layer / File(s) Summary
New testdb helpers
internal/testdb/testdb.go, internal/storage/db.go
Adds the shared Postgres test helper package with DB enablement gating, DSN selection, schema naming, pool creation, schema DDL, and current-schema truncation. Adds storage.NewFromPool to wrap an existing pool in *DB.
Shared storage fixtures
internal/storage/recovery_stability_writer_integration_test.go
Replaces per-test schema creation with shared full and readiness schemas, lazy initializers, mutex-guarded fixture access, reset helpers, and suite cleanup in storage integration tests.
Storage test migrations
internal/storage/baseline_reason_test.go, internal/storage/chip_calibration_writer_test.go, internal/storage/energy_bands_test.go
Switches storage tests to the new fixtures, adds readiness-schema drift reset logic, and refactors energy-band tests to use bulk snapshot seeding.
Shared UI tenant fixture
internal/ui/admin_readiness_redesign_config_test.go, internal/ui/admin_readiness_redesign_monitoring_test.go, internal/ui/onboarding_handlers_test.go
Moves the admin readiness UI tests onto a shared tenant schema with lazy initialization, per-test resets, and suite cleanup, and adjusts onboarding and monitoring assertions.
Makefile and CI lanes
Makefile, .github/workflows/ci.yml
Adds unit and DB test targets, a DB lane aggregate, and CI workflow inputs and job conditions that split unit, DB, and Docker execution paths.
Docs and plan updates
CONTRIBUTING.md, docs/TEST_COVERAGE.md, docs/ai-plans/*
Updates developer and test coverage docs, and adds the DB test planning HTML documents that describe the lane structure, implementation steps, verification, and closure criteria.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

Possibly related PRs

Poem

🐇 I sniffed the lanes and hopped the seam,
One DB gate, one tidy test dream.
Shared schemas hum, then cleanly rest,
make test-unit keeps the fast path blessed.
The carrots glow in CI light—
green rows, warm pools, and a calmer night 🌙

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed, but it misses several required template sections like Linked Issue, Privacy/Data Impact, and Deployment Notes. Add the missing template sections: Linked Issue, Privacy/Data Impact, Evidence Boundary, complete Test Plan checkboxes, Methodology/Documentation, and Deployment Notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 18.52% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and clearly reflects the main change: stabilizing DB-backed test lanes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-issue-182-db-backed-tests

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@dzarlax dzarlax marked this pull request as ready for review June 30, 2026 16:42
@dzarlax dzarlax force-pushed the fix-issue-182-db-backed-tests branch from 088ad3b to 4d9ce0e Compare June 30, 2026 16:51

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
Makefile (1)

69-75: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Add the missing X-API-Key header to the smoke request.

/health is registered through h.auth(...), so requests without X-API-Key are rejected with 401 before the POST body is processed. As written, make test now delegates to a smoke target that will fail against a normally configured server.

Suggested fix
+API_KEY ?=
+
 smoke-health-post:
+	test -n "$(API_KEY)"
 	curl -s -X POST http://localhost$(ADDR)/health \
+		-H "X-API-Key: $(API_KEY)" \
 		-H "Content-Type: application/json" \
 		-H "automation-name: Test" \
 		-H "automation-id: test-001" \
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Makefile` around lines 69 - 75, The smoke-health-post target is missing the
required X-API-Key header, so the authenticated /health route will reject the
request before the body is handled. Update the curl invocation in
smoke-health-post to include the same API key header expected by h.auth, keeping
the rest of the request intact so the smoke test can pass against a normal
server configuration.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.github/workflows/ci.yml:
- Around line 45-54: Add an explicit minimal permissions block for the build
workflow so the default inherited GITHUB_TOKEN access is not broader than
needed. Update the workflow near the build job/test section to set permissions
at the workflow or job level, using the build job context and the existing Test
step as the location anchor, and keep it limited to read-only repository access
such as contents: read.

In `@docs/TEST_COVERAGE.md`:
- Line 22: The Energy DB full entry in TEST_COVERAGE.md has a stale runtime
note; update the “currently around 56s after fixture narrowing” text to the
newly measured full-lane runtime after batching seed inserts. Edit the Energy DB
full row only, keeping the command and description intact, and replace the old
estimate with the current ~21.8s figure so the documentation matches the latest
verification result.

In `@internal/storage/energy_bands_test.go`:
- Around line 470-478: Remove the now-dead single-row helpers
`insertEnergySnapshot` and `insertEnergySnapshotAt` from the test file, since
the bulk seeding flow now uses `insertEnergySnapshots` directly and these
wrappers are no longer referenced. Keep the shared helpers like
`insertEnergySnapshots`, `energySnapshot`, and `energySnapshotAt` intact, and
ensure any remaining call sites are updated to use the bulk insert path so the
`unused` linter no longer flags these functions.

In `@internal/storage/recovery_stability_writer_integration_test.go`:
- Around line 151-165: After CreateSchema succeeds in
recovery_stability_writer_integration_test.go, ensure any later setup failure
cleans up the isolated schema before calling t.Fatalf. In the test flow around
CreateSchema, NewPool, and VerifyReadinessRedesignSchema, add schema-drop
cleanup on the error paths so a failed pool open or schema verification does not
leave a stale schema behind. Use the existing testdb and db/pool lifecycle
points to place the cleanup in the same setup sequence.
- Around line 196-220: The shared test DB initialization path can leak schemas
when setup fails after CreateSchema but before the DB is cached, so update the
shared initializers and cleanupSharedTestDBs to track and drop partially created
schemas even on failure. Use the existing shared initializer flow around
CreateSchema, NewPool, NewFromPool, EnsureAllTables, and
VerifyReadinessRedesignSchema to record the schema name for cleanup before any
early t.Fatalf path, and make cleanupSharedTestDBs remove those orphaned schemas
in addition to the cached shared*DB handles.

In `@internal/ui/admin_readiness_redesign_config_test.go`:
- Around line 147-151: The shared-schema reset is not failing fast because
db.EnsureReadinessRedesignTables only logs errors and the test setup ignores any
bootstrap failure. Update EnsureReadinessRedesignTables to return an error
instead of swallowing DDL issues, then check and fail immediately in
admin_readiness_redesign_config_test.go alongside the existing
EnsureSubjectiveCheckinsTable setup so the shared fixture cannot continue with a
partially reset schema.

In `@internal/ui/onboarding_handlers_test.go`:
- Around line 61-80: The fallback assertion in onboarding handler tests is too
narrow because it only fails on one specific 400 message; update the check in
the missing-schema fallback case so any HTTP 400 response is treated as a
failure for endpoints with checkMissingFallback=true. Use the existing
dispatchOnboardingHandler and ep.checkMissingFallback test path to assert that
missing schema must never be rejected as a bad request, regardless of the
response body text.

---

Outside diff comments:
In `@Makefile`:
- Around line 69-75: The smoke-health-post target is missing the required
X-API-Key header, so the authenticated /health route will reject the request
before the body is handled. Update the curl invocation in smoke-health-post to
include the same API key header expected by h.auth, keeping the rest of the
request intact so the smoke test can pass against a normal server configuration.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f0de827f-17dd-4ebd-aa49-eed67f38a40b

📥 Commits

Reviewing files that changed from the base of the PR and between 973ecf8 and 088ad3b.

📒 Files selected for processing (16)
  • .github/workflows/ci.yml
  • CONTRIBUTING.md
  • Makefile
  • docs/TEST_COVERAGE.md
  • docs/ai-plans/2026-06-29-db-test-closure-plan.html
  • docs/ai-plans/2026-06-29-db-test-harness-redesign.html
  • docs/ai-plans/2026-06-29-issue-182-db-backed-integration-tests.html
  • internal/storage/baseline_reason_test.go
  • internal/storage/chip_calibration_writer_test.go
  • internal/storage/db.go
  • internal/storage/energy_bands_test.go
  • internal/storage/recovery_stability_writer_integration_test.go
  • internal/testdb/testdb.go
  • internal/ui/admin_readiness_redesign_config_test.go
  • internal/ui/admin_readiness_redesign_monitoring_test.go
  • internal/ui/onboarding_handlers_test.go

Comment thread .github/workflows/ci.yml
Comment thread docs/TEST_COVERAGE.md Outdated
Comment thread internal/storage/energy_bands_test.go Outdated
Comment thread internal/storage/recovery_stability_writer_integration_test.go
Comment thread internal/storage/recovery_stability_writer_integration_test.go
Comment thread internal/ui/admin_readiness_redesign_config_test.go
Comment thread internal/ui/onboarding_handlers_test.go
@dzarlax dzarlax force-pushed the fix-issue-182-db-backed-tests branch from 4d9ce0e to 1ff5c1d Compare June 30, 2026 17:49
@dzarlax dzarlax merged commit f3cd4d2 into main Jun 30, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant