Skip to content

fix(tests): harden agent worker test environment#831

Merged
zbigniewsobiecki merged 1 commit intodevfrom
fix/tests-harden-worker-env
Mar 14, 2026
Merged

fix(tests): harden agent worker test environment#831
zbigniewsobiecki merged 1 commit intodevfrom
fix/tests-harden-worker-env

Conversation

@zbigniewsobiecki
Copy link
Copy Markdown
Member

Summary

Three hardening changes to prevent agent confusion and fragile test isolation when running tests inside worker containers, identified from analysis of agent run 87523cb1.

  • CLAUDE.md: Expand the Testing section with the full command list (npm test, test:unit, test:integration, test:all, test:coverage, test:watch) and add an explicit warning against npm test -- --project integration, which adds the integration project on top of the 4 hardcoded unit flags rather than filtering to it. Documents how integration tests locate their database.
  • beforeAll(truncateAll): Add file-level DB truncation to all 6 top-level integration test files (github-personas, integration-validation, multi-provider-credentials, pm-provider-switching, trigger-registry, webhook-logging). Ensures each file starts from a clean slate regardless of what previous test files left in the DB — purely additive, existing beforeEach(truncateAll) unchanged.
  • withTestTransaction helper + _setTestDb hook: Implement the correct transaction-rollback pattern for integration tests. Adds _setTestDb(db | null) to src/db/client.ts so getDb() can be overridden with an active transaction object, and exports withTestTransaction from the integration test helpers. Prevents future agents from re-inventing a broken global-singleton approach.

Tests

  • tests/unit/db/client.test.ts — 3 new tests covering _setTestDb/getDb() override mechanism
  • tests/unit/integration-helpers/withTestTransaction.test.ts — 5 new tests covering rollback-on-success, error propagation, and _setTestDb lifecycle (called with tx before fn, null in finally)
  • All 4797 unit tests pass; all 306 integration tests pass; lint and typecheck clean

Test plan

  • npm test — all 4797 unit tests pass
  • npm run test:integration — all 306 integration tests pass
  • npm run typecheck — clean
  • npm run lint — clean

🤖 Generated with Claude Code

Three changes to prevent agent confusion when running tests inside worker containers:

1. **CLAUDE.md**: Document correct test commands (npm test, test:unit, test:integration,
   test:all) and add a warning against `npm test -- --project integration`, which adds
   rather than replaces the unit project flags.

2. **beforeAll(truncateAll)**: Add file-level truncation to all 6 top-level integration
   test files so each file starts from a known-clean state regardless of what previous
   test files left in the DB.

3. **withTestTransaction helper**: Implement a correct transaction-rollback pattern for
   integration tests. Adds `_setTestDb` hook to getDb() so the active transaction can
   be injected, and exports `withTestTransaction` from tests/integration/helpers/db.ts
   for future use without re-inventing it.

New unit tests cover _setTestDb/getDb override and withTestTransaction lifecycle
(rollback-on-success, error propagation, _setTestDb cleanup in finally).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@zbigniewsobiecki zbigniewsobiecki merged commit b68b38a into dev Mar 14, 2026
6 checks passed
@zbigniewsobiecki zbigniewsobiecki deleted the fix/tests-harden-worker-env branch March 16, 2026 16:43
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