Skip to content

test(pendingAttempt): cover the guest exam-save persistence and flush #73

Description

@nastaso

Context

src/lib/pendingAttempt.ts is the guest mock-exam save path: readPendingAttempt validates the payload shape and expires entries past a 24h TTL, and flushPendingAttempt (line 122) mirrors the live save with a compensating delete on a partial write (deletes the exam_attempts row if the attempt_questions insert fails) plus a concurrency guard. All of it is untested, and a regression could silently drop a guest's completed attempt or leave a scored attempt with zero reviewable questions.

Scope

  • Add src/lib/pendingAttempt.test.ts (co-located, Vitest). Reuse the mock-Supabase harness pattern from src/lib/supabaseUtils.test.ts. Tests only; do not change production behavior.

Cases to cover

  • storePendingAttempt then readPendingAttempt round-trips a valid payload.
  • readPendingAttempt returns null for an expired entry (past the TTL) and for a malformed / shape-invalid entry.
  • flushPendingAttempt: success path inserts both rows; on an attempt_questions failure it deletes the exam_attempts row (compensating rollback); the concurrency guard prevents a double flush.

Acceptance criteria

  • src/lib/pendingAttempt.test.ts covers round-trip, TTL expiry, shape rejection, partial-write rollback, and the concurrency guard.
  • npm run test passes; no new dependencies; no production code changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is welcometestAdds or improves automated tests

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions