Skip to content

refactor(idempotency): _setDbForTesting seam + full round-trip coverage#97

Merged
CryptoJones merged 1 commit into
masterfrom
refactor/idempotency-injectable-db
May 18, 2026
Merged

refactor(idempotency): _setDbForTesting seam + full round-trip coverage#97
CryptoJones merged 1 commit into
masterfrom
refactor/idempotency-injectable-db

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Mirror of #86 (P5-M) for the idempotency middleware.

Summary

  • app/middleware/idempotency.js swaps its top-level db = require(...) for a late-bound getDb() accessor + _setDbForTesting(stub) setter (matching the P5-M pattern in auth.js).
  • New HTTP-level test exercises the full first-write → replay → 409 cycle with an in-memory stub backing the SELECT + INSERT calls. Was previously integration-only.

Test plan

  • 478 pass / 4 skip. Lint clean.

This code proudly made in Nebraska. GO BIG RED! 🌽 https://xkcd.com/2347/

…verage

Mirror of P5-M for the idempotency middleware. The cache lookup +
INSERT paths previously could not be exercised at the HTTP level
because vi.mock doesn't reliably intercept the codebase's CJS
require chain (same limitation that drove P5-M for auth.js).

Two changes to app/middleware/idempotency.js:

1. The top-level `const db = require(...)` is replaced with a
   late-bound `getDb()` accessor that returns `_dbOverride` when
   set or the real db.config.js module otherwise.
2. A `_setDbForTesting(stub)` setter on `module.exports` lets
   tests inject a stub `{ sequelize: { query }, Sequelize:
   { QueryTypes } }`. Production code MUST NOT call it.

New HTTP-level test exercises the full first-write → replay → 409
cycle:
- First request: cache miss → controller runs → response stored
  by INSERT.
- Second request: same key + body → SELECT hits → replay with
  `Idempotency-Replay: true` and the cached status + body.
- Third request: same key, DIFFERENT body → 409 with
  `code: idempotency_key_reused`.

The stub uses an in-memory Map to act as both SELECT source and
INSERT sink, so the test stays self-contained.

Tests: 478 pass / 4 skip (was 477/4 — +1 net; one big test
exercises three semantic paths). Lint clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit 42c5de4 into master May 18, 2026
3 checks passed
@CryptoJones CryptoJones deleted the refactor/idempotency-injectable-db branch May 18, 2026 05:51
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