Skip to content

test(coverage): db/cache/nosql local providers to >=95% via source seams#151

Merged
mastermanas805 merged 1 commit into
masterfrom
coverage/api-providers-seams-95
May 22, 2026
Merged

test(coverage): db/cache/nosql local providers to >=95% via source seams#151
mastermanas805 merged 1 commit into
masterfrom
coverage/api-providers-seams-95

Conversation

@mastermanas805

Copy link
Copy Markdown
Member

Summary

Closes the coverage gap on the three local-backend providers to the >=95% mandate using deterministic source seams (org policy now requires seams, not waivers). Production defaults are unchanged — every seam defaults to the real fn/value.

Package Before After
internal/providers/db 2.2% 97.8% (local.go fully covered)
internal/providers/cache 64.8% 98.2%
internal/providers/nosql 70.3% 100.0%

Seams added (package vars / interfaces, real default)

  • db/local.gorandInt (crypto/rand.Int), pgConn interface + pgxConnect factory. An in-memory fake pgConn drives the RNG failure, the conn.Close defer-error logs, and the non-fatal REVOKE / GRANT / DROP USER exec-error logs.
  • cache/redis.gorandRead (crypto/rand.Read); storageBytesScanCap (test-lowerable truncation ceiling, default storageMaxKeys=200k); redisScanner interface behind a storageBytes helper. The mid-scan vanished-key skip is now deterministic (a fake returns a SCAN key that MEMORY USAGE reports missing); the truncation branch fires with a handful of keys (no 200k write, no timing).
  • nosql/mongo.gorandRead (crypto/rand.Read); mongoDisconnect seam for the Disconnect defer-error logs; storageSizeToInt64 extracted so every numeric BSON arm is unit-testable.

Notable fixes over the prior attempt (supersedes #148)

  • Replaced the flaky timing-based MEMORY-USAGE-skip test with a deterministic interface fake.
  • Aligned the db test env var to TEST_POSTGRES_CUSTOMERS_URL (what CI's coverage.yml provides). The prior attempt used TEST_CUSTOMERS_URL and silently skipped in CI, contributing zero coverage.
  • Hardened uniqueToken with a per-run random seed so same-second runs against a shared Postgres can't collide with orphan databases.

Test plan

  • go test ./internal/providers/{db,cache,nosql} -count=1 -p 1 green against local docker pg16 / redis7 / mongo6 on 127.0.0.1
  • Per-package coverage all >=95% (97.8 / 98.2 / 100.0)
  • Stable across 3 consecutive full runs (no flake)
  • CI-safe: clean skip with no env vars; go build ./... + go vet ./... clean; gofmt -l clean
  • CI gate (coverage / ci / deploy) green on this PR

🤖 Generated with Claude Code

…rce seams

Closes the coverage gap on the three local-backend providers to the >=95%
mandate using deterministic source seams (org policy: seams, not waivers).
Production defaults are identical — every seam defaults to the real
fn/value.

Coverage (per-package, real pg16/redis7/mongo6 on 127.0.0.1):
- internal/providers/db:    2.2%  -> 97.8%  (local.go fully covered)
- internal/providers/cache: 64.8% -> 98.2%
- internal/providers/nosql: 70.3% -> 100.0%

Seams added (all package vars / interfaces, real default):
- db/local.go:    randInt (crypto/rand.Int), pgConn interface + pgxConnect
  factory — drives the RNG failure, conn.Close defer-error logs, and the
  non-fatal REVOKE/GRANT/DROP USER exec-error logs via an in-memory fake.
- cache/redis.go: randRead (crypto/rand.Read); storageBytesScanCap
  (test-lowerable truncation ceiling, default storageMaxKeys=200k);
  redisScanner interface behind storageBytes — drives the mid-scan
  vanished-key skip deterministically (fake returns a SCAN key that
  MEMORY USAGE reports missing) and the truncation branch with a handful
  of keys (no 200k write, no timing).
- nosql/mongo.go: randRead (crypto/rand.Read); mongoDisconnect seam for
  the Disconnect defer-error logs; storageSizeToInt64 extracted so every
  numeric BSON arm is unit-testable.

Replaces the flaky timing-based MEMORY-USAGE-skip test from the prior
attempt with a deterministic interface fake. Aligns the db test env var
to TEST_POSTGRES_CUSTOMERS_URL (what CI's coverage.yml provides; the prior
attempt used TEST_CUSTOMERS_URL and silently skipped in CI). Hardens
uniqueToken with a per-run random seed so same-second runs against a
shared Postgres can't collide.

Tests are CI-safe (skip cleanly with no env) and stable across 3
consecutive full runs. Supersedes PR #148.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mastermanas805
mastermanas805 merged commit 849058e into master May 22, 2026
10 of 11 checks passed
@mastermanas805
mastermanas805 deleted the coverage/api-providers-seams-95 branch May 22, 2026 02:48
mastermanas805 added a commit that referenced this pull request May 30, 2026
… follow-up) (#198)

Live verification via chrome devtools MCP:
  Access to fetch at 'https://api.instanode.dev/auth/exchange' from
  origin 'https://instanode.dev' has been blocked by CORS policy: The
  value of the 'Access-Control-Allow-Credentials' header in the
  response is '' which must be 'true' when the request's credentials
  mode is 'include'.

Root cause: fiberCORS.Config never set AllowCredentials. The dashboard
SPA's AUTH-004 cookie-exchange POST uses `credentials: 'include'` to
send the HttpOnly `instanode_session_exchange` cookie cross-origin
(instanode.dev → api.instanode.dev). The CORS spec requires the
response to carry `Access-Control-Allow-Credentials: true` for the
browser to ALLOW the SPA to read the response body. Without it the
fetch fails with "blocked by CORS policy" before the response body is
visible to the SPA. Existing localStorage-Bearer auth wasn't affected
because Bearer doesn't trigger the credentials-include code path.

Companion to instanode-web#150 + #151 (which added the SPA-side
cookie-exchange flow + dropped the Accept header to avoid an
unrelated preflight 403). After this api PR ships, the cookie-exchange
chain runs end-to-end: GitHub OAuth → api callback sets cookie + 302
→ SPA POSTs /auth/exchange with credentials → response includes both
the token AND Allow-Credentials: true → SPA stores token + navigates.

Safety: AllowCredentials:true requires the AllowOrigins allowlist to
be specific (no `*`). Ours is "https://instanode.dev,https://www.
instanode.dev" — explicit list — which satisfies the spec. The CORS
spec forbids credentials + wildcard origin precisely to prevent rogue
sites from siphoning cookies.

Tests added (cors_credentials_test.go): pin both the actual-CORS POST
response AND the OPTIONS preflight response carry
Access-Control-Allow-Credentials: true. A future router.New edit that
drops AllowCredentials regresses prod login and fails these tests.

`make gate` clean locally.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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