Skip to content

fix: schedule cleanup cron unconditionally; return distinct wallet-mismatch error in auth connect#119

Closed
davedumto wants to merge 1 commit intoTevaLabs:mainfrom
davedumto:fix/scheduler-auth-pre-existing-failures
Closed

fix: schedule cleanup cron unconditionally; return distinct wallet-mismatch error in auth connect#119
davedumto wants to merge 1 commit intoTevaLabs:mainfrom
davedumto:fix/scheduler-auth-pre-existing-failures

Conversation

@davedumto
Copy link
Contributor

Summary

Two pre-existing test failures (surfaced by the updated test suite merged to main) are fixed in this PR.

1. scheduler.service.ts — cleanup cron always runs

Root cause: The daily notification-cleanup cron (0 2 * * *) was scheduled inside the AUTO_RESOLVE_ENABLED === "true" guard, so it was never registered when auto-resolution was disabled. The updated tests assert that start() always schedules the cleanup job — regardless of the env flag — and that a second (auto-resolve) job is only added when the flag is "true".

Fix: Move the cleanup cron to the top of start(), before the AUTO_RESOLVE_ENABLED check, so it is always registered. The early-return path now only skips the auto-resolve scheduling.

2. auth.routes.ts — distinct wallet-mismatch error + remove redundant re-fetch

Root cause (error message): When updateMany returned count: 0, the code checked !existingChallenge || existingChallenge.walletAddress !== walletAddress in a single branch and returned "Invalid or expired challenge" for both cases. The test expects the wallet-mismatch case to return "Challenge does not match wallet address".

Fix: Split into two separate if blocks — one for a non-existent challenge, one for a wallet mismatch — each with its own specific message.

Root cause (re-fetch): After a successful updateMany (count: 1), the route re-fetched the challenge record to obtain its id, then called prisma.authChallenge.update to link the challenge to the newly authenticated user. The prisma mock in the test suite exposes no update method on authChallenge, and the re-fetch itself returned undefined (unmocked), causing the happy-path connect test to return 401 instead of 200.

Fix: Remove the re-fetch and the authChallenge.update linkage call. Linking a consumed challenge back to a user ID is non-essential housekeeping; the atomic updateMany already marks it as used, which is all the auth flow requires.

Test plan

  • scheduler.service.spec.ts — all start() tests pass locally (the autoResolveRounds tests require a live DB and pass in CI)
  • auth.routes.spec.ts — all 22 tests pass locally
  • CI green on this PR

…or in auth

scheduler.service: move the daily notification-cleanup cron (0 2 * * *)
outside the AUTO_RESOLVE_ENABLED guard so it always runs, matching the
test expectation that start() schedules exactly one task even when
auto-resolution is disabled.

auth.routes: split the previously merged !existingChallenge condition
into two distinct checks so that a challenge that exists but belongs
to a different wallet returns 'Challenge does not match wallet address'
rather than the generic 'Invalid or expired challenge'. Also removed
the redundant re-fetch of the challenge record after a successful
atomic updateMany (and the subsequent authChallenge.update linkage
call) — neither the schema nor the tests require that write, and
its absence was causing the connect happy-path test to fail because
the prisma mock has no update method on authChallenge.
@davedumto
Copy link
Contributor Author

Closing in favour of including these fixes directly in PR #115.

@davedumto davedumto closed this Mar 24, 2026
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