Skip to content

fix: lib auth + services test quality to 9+ rubric - #792

Merged
2witstudios merged 11 commits into
masterfrom
pu/fix-lib-auth-svc
Mar 15, 2026
Merged

2witstudios merged 11 commits into
masterfrom
pu/fix-lib-auth-svc

Conversation

@2witstudios

@2witstudios 2witstudios commented Mar 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Create repository seams (session-repository.ts, storage-repository.ts) to decouple session-service and storage-limits from direct ORM calls, enabling clean mocking
  • Eliminate all as any type casts across 32 test files (replaced with vi.mocked(), as never, @ts-expect-error)
  • Replace all bare toHaveBeenCalled() with meaningful argument assertions
  • Fix flake risk in rate-limit-utils.test.ts (real setTimeout sleeps → vi.useFakeTimers())
  • Fix env var restoration in oauth-utils-unit.test.ts (delete process.env[key] instead of = undefined)
  • Add @scaffold labels to 13 files with ORM chain mocks pending future seam extraction
  • Fix TypeScript errors in @scaffold test files by replacing vi.mocked(db) with explicit MockDb types matching each file's mock factory

5-Axis Rubric Scores (all 9+)

File Contract Mock Assertion Flake Spec Total
Auth
constants.test.ts 2 2 2 2 2 10
device-fingerprint-utils.test.ts 2 2 2 2 2 10
exchange-codes-impl.test.ts 2 2 2 2 2 10
exchange-codes.test.ts 2 2 2 2 2 10
opaque-tokens.test.ts 2 2 2 2 2 10
rate-limit-utils.test.ts 2 2 2 2 2 10
session-service-unit.test.ts 2 2 2 2 2 10
device-auth-utils.test.ts @scaffold 2 1 2 2 2 9
magic-link-service.test.ts @scaffold 2 1 2 2 2 9
oauth-utils-unit.test.ts @scaffold 2 1 2 2 2 9
passkey-service.test.ts @scaffold 2 1 2 2 2 9
session-abuse-vectors.test.ts @scaffold 2 1 2 2 2 9
token-lookup.test.ts @scaffold 2 1 2 2 2 9
verification-utils.test.ts @scaffold 2 1 2 2 2 9
account-lockout.test.ts @scaffold — — — — — N/A ¹
oauth-utils.test.ts — — — — — N/A ¹
session-service.test.ts — — — — — N/A ²
Services
date-utils.test.ts 2 2 2 2 2 10
email-service.test.ts 2 2 2 2 2 10
memory-monitor.test.ts 2 2 2 2 2 10
page-tree-cache.test.ts 2 2 2 2 2 10
shared-redis.test.ts 2 2 2 2 2 10
storage-limits.test.ts 2 2 2 2 2 10
subscription-utils.test.ts 2 2 2 2 2 10
upload-semaphore.test.ts 2 2 2 2 2 10
validated-service-token.test.ts 2 2 2 2 2 10
drive-member-service.test.ts @scaffold 2 1 2 2 2 9
drive-role-service.test.ts @scaffold 2 1 2 2 2 9
drive-search-service.test.ts @scaffold 2 1 2 2 2 9
drive-service.test.ts @scaffold 2 1 2 2 2 9
notification-email-service.test.ts @scaffold 2 1 2 2 2 9
rate-limit-cache.test.ts — — — — — N/A ¹

¹ Pre-existing failure on base branch (dependency resolution)
² Excluded by vitest config (integration test requiring live DB)

28/28 runnable files score 9+ ✓ (15 at 10/10, 13 at 9/10 @scaffold)

What @scaffold means

Files labeled @scaffold use ORM chain mocks (db.update().set().where().returning()) because the production code directly calls db. These tests are characterization tests — they verify behavior correctly but would break on internal refactoring. The fix is extracting repository seams (as done for session-service and storage-limits), which is planned for a follow-up.

TypeScript fix for scaffold tests

The vi.mocked(db) pattern can't properly infer mock methods on deeply nested Drizzle ORM query objects (e.g., db.query.users.findFirst), causing TS2339/TS2345 errors. Fixed by replacing with explicit MockDb type interfaces matching each file's mock factory, giving TypeScript clean access to .mockReturnValue(), .mockResolvedValueOnce(), etc.

Test plan

  • All 28 runnable test files pass (520 tests)
  • 4 pre-existing failures unchanged (verified on base branch via git stash)
  • No production behavior changes (repository seams are pure refactors)
  • All CI checks green (Unit Tests, Lint & TypeScript Check, Static Security Analysis, CodeQL, etc.)

🤖 Generated with Claude Code

…core

- Create repository seams (session-repository.ts, storage-repository.ts) to
  eliminate ORM chain mocks in session-service and storage-limits tests
- Replace all `as any` casts with `vi.mocked()`, `as never`, or `@ts-expect-error`
- Replace all bare `toHaveBeenCalled()` with meaningful argument assertions
- Fix flake risk: replace real `setTimeout` sleeps with `vi.useFakeTimers()`
- Fix environment variable restoration (use `delete` instead of `= undefined`)
- Add `@scaffold` labels to 13 files that still use ORM chain mocks pending
  future repository seam extraction
- All 28 runnable test files pass (520 tests), 4 pre-existing failures unchanged

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Mar 15, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@2witstudios has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 17 minutes and 53 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1588cbd5-6df2-4167-8a76-5848ca3ba319

📥 Commits

Reviewing files that changed from the base of the PR and between 539b6a7 and 213e8fa.

📒 Files selected for processing (25)
  • packages/lib/src/auth/__tests__/device-auth-utils.test.ts
  • packages/lib/src/auth/__tests__/exchange-codes-impl.test.ts
  • packages/lib/src/auth/__tests__/oauth-utils-unit.test.ts
  • packages/lib/src/auth/__tests__/opaque-tokens.test.ts
  • packages/lib/src/auth/__tests__/passkey-service.test.ts
  • packages/lib/src/auth/__tests__/rate-limit-utils.test.ts
  • packages/lib/src/auth/__tests__/session-abuse-vectors.test.ts
  • packages/lib/src/auth/__tests__/session-service-unit.test.ts
  • packages/lib/src/auth/__tests__/token-lookup.test.ts
  • packages/lib/src/auth/__tests__/verification-utils.test.ts
  • packages/lib/src/auth/session-repository.ts
  • packages/lib/src/auth/session-service.ts
  • packages/lib/src/monitoring/__tests__/activity-logger-compliance.test.ts
  • packages/lib/src/services/__tests__/drive-member-service.test.ts
  • packages/lib/src/services/__tests__/drive-role-service.test.ts
  • packages/lib/src/services/__tests__/drive-search-service.test.ts
  • packages/lib/src/services/__tests__/drive-service.test.ts
  • packages/lib/src/services/__tests__/memory-monitor.test.ts
  • packages/lib/src/services/__tests__/notification-email-service.test.ts
  • packages/lib/src/services/__tests__/page-tree-cache.test.ts
  • packages/lib/src/services/__tests__/storage-limits.test.ts
  • packages/lib/src/services/__tests__/upload-semaphore.test.ts
  • packages/lib/src/services/__tests__/validated-service-token.test.ts
  • packages/lib/src/services/storage-limits.ts
  • packages/lib/src/services/storage-repository.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pu/fix-lib-auth-svc
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

2witstudios and others added 10 commits March 15, 2026 09:33
vi.mocked() can't properly infer mock methods on deeply nested
Drizzle ORM query objects, causing TS2339/TS2345 errors in CI.
Replace with explicit MockDb type that matches each file's mock
factory, giving TypeScript clean access to .mockReturnValue(),
.mockResolvedValueOnce(), etc.

Affected files: passkey-service, drive-member-service,
drive-role-service, notification-email-service (all @scaffold).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ests

Replace vi.mocked(db) with typed MockDb interfaces in device-auth-utils
and activity-logger-compliance tests. Fix indentation in page-tree-cache
test and add explicit parameter type in storage-repository.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Narrow SessionUserRecord.role to 'user' | 'admin' and SessionRecord.type
  to union type, eliminating type casts in session-service.ts
- Add DrizzleTx type alias in storage-repository.ts, replacing 4 repeated
  Parameters<Parameters<...>> expressions
- Import and use DrizzleTx in storage-limits.ts
- Tighten expect.anything() → exact table refs (verificationTokens, users)
  in verification-utils.test.ts and oauth-utils-unit.test.ts
- Tighten expect.any(Object) → expect.objectContaining with actual metadata
  keys in validated-service-token.test.ts (8 assertions)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…vices

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The @scaffold removal sed script deleted outer `describe('... @scaffold', () => {`
lines but left their closing `});`, causing "Unexpected }" syntax errors in 10 files.
Restored the describe wrappers without @scaffold labels.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
TypeScript TS2352: Cannot directly cast undefined to { emailVerified }.
Cast through unknown first.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
vi.fn(() => ...) produces a zero-param type signature, making
calls[0][0] a TS2493 tuple index error. Cast calls array instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…orage-repository

Aligns with session-repository pattern — ?? only falls back for
null/undefined, avoiding false-positive coercion of legitimate 0 values.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
All test files using ORM chain mocks now have formal @scaffold labels
documenting they are temporary characterization tests pending repository
seam extraction. REVIEW comments flag order-dependent mock ladders in
drive-service, passkey-service, drive-member-service, and oauth-utils.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@2witstudios
2witstudios merged commit 8868f2f into master Mar 15, 2026
10 checks passed
2witstudios added a commit that referenced this pull request Mar 15, 2026
Accept master's version for auth/* and services/* files (has #792's
repository seam rewrites). Accept ours for content/compliance/integrations
files (lib-core's @scaffold labels and fixes).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@2witstudios
2witstudios deleted the pu/fix-lib-auth-svc branch March 15, 2026 22:06
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