fix: DNF status transitions now create new sessions#391
fix: DNF status transitions now create new sessions#391
Conversation
Treat DNF as a terminal state similar to 'read'. When transitioning from DNF to any other status (read-next, to-read, reading), the system now: 1. Archives the existing DNF session (preserves history) 2. Creates a new session with incremented session number 3. Preserves the original DNF completedDate Changes: - Updated backward movement detection to include DNF transitions - DNF sessions always archived (no progress check needed) - Added validation to block direct DNF → read transitions - Auto-set startedDate when transitioning DNF → reading - Preserve DNF completedDate when archiving (don't overwrite) Tests: - Added 5 unit tests for DNF transitions in session/core.test.ts - Added 5 E2E API tests in dnf-transitions.test.ts - All 3900+ tests pass Fixes issue where DNF → read-next would overwrite the DNF session instead of creating a new session, losing the DNF history. Closes #[issue-number]
- Refactor DNF transition E2E tests to import and call route handlers directly - Fix error handling in status route to return 400 for DNF validation errors - All 5 E2E tests now passing (no more NetworkError) - Full test suite: 3904 tests passing Technical changes: - Import POST handler from route.ts instead of using fetch() - Use createMockRequest() helper to create NextRequest objects - Add vi.mock for next/cache revalidation - Update error handling to catch "Cannot mark" errors with 400 status - Adjust test expectations for flattened response structure when sessionArchived=true Co-authored-by: OpenCode <opencode@anomaly.co>
✅ CI Test Failures FixedFixed the 5 failing E2E tests in CI. The issue was that the tests were attempting to make real HTTP requests to Changes in commit
|
- Add calibreService mock to prevent file I/O during tests - Add streakService mock to prevent side effects - Align with other E2E test patterns (reread.test.ts, mark-as-read.test.ts, status.test.ts) - Add mock rationale comments explaining why each mock is needed - All 5 tests still passing Co-authored-by: OpenCode <opencode@anomaly.co>
✅ Full E2E Pattern Alignment (commit
|
masonfox
left a comment
There was a problem hiding this comment.
Please review this PR for code quality, best practices, and potential issues.
…ix comments) - Add SessionValidationError class with typed error codes (PAGES_REQUIRED, DNF_TO_READ_BLOCKED) - Replace error string matching with instanceof checks in API route - Optimize progress lookup: skip DB query when completedDate already exists - Fix misleading comment: terminal sessions keep isActive=true (not false) All 3904 tests passing. Addresses GitHub Copilot review #3864087654
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## develop #391 +/- ##
===========================================
- Coverage 78.55% 78.54% -0.02%
===========================================
Files 167 167
Lines 7509 7533 +24
Branches 1829 1843 +14
===========================================
+ Hits 5899 5917 +18
- Misses 1129 1132 +3
- Partials 481 484 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
masonfox
left a comment
There was a problem hiding this comment.
Re-requesting review after addressing feedback from Review #3864087654:
✅ Comment 1 (Misleading comment): Fixed comment to accurately reflect that terminal sessions keep isActive=true
✅ Comment 2 (Brittle error handling): Replaced string matching with typed SessionValidationError class and instanceof checks
✅ Comment 3 (Unnecessary query): Optimized to skip DB query when completedDate already exists
All 3904 tests still passing. Please review the updated implementation.
Resolves conflict by accepting deletion of review-loop.md from develop branch
- Set isActive=false for both 'read' and 'dnf' terminal statuses - Add readNextOrder assignment for DNF → read-next transitions (fixes Copilot Issue #1) - Fix misleading comments about terminal state behavior (fixes Copilot Issue #2) - Add 8 new tests covering terminal state transitions (fixes Copilot Issues #3 & #4) - Update 11 existing tests to expect isActive=false for terminal states - Add transaction support to SessionRepository.getNextReadNextOrder() Resolves Copilot PR review #3885064799 (all 4 issues addressed) Aligns with ADR-004, ADR-008, ADR-011 documentation Matches 97% of production data (62/64 read/dnf sessions have is_active=0) All 3999 tests passing ✅
- Extract complex boolean conditions into named variables (isReadingToPlanning, isDnfToAnyOther) - Consolidate completedDate calculation into helper function (getArchiveCompletedDate) - Reduce nesting with early returns and optional chaining - Improves maintainability without changing behavior All 3999 tests passing ✅ Co-authored-by: Claude <claude@anthropic.com>
Summary
Fixes a bug where transitioning a book from DNF (Did Not Finish) status back to "Read Next" or "Want To Read" was overwriting the existing DNF session instead of creating a new session with an incremented session number, causing DNF history to be lost.
Changes:
Technical Details
Modified:
lib/services/session.service.tsModified:
app/api/books/[id]/status/route.tsTests Added:
__tests__/integration/services/sessions/session/core.test.ts__tests__/e2e/api/books/dnf-transitions.test.tsValid Transitions FROM DNF
dnfto-readdnfread-nextdnfreadingdnfreadTesting
✅ All 3904 tests passing (including new DNF transition tests)
✅ Manually verified on production database (book ID 743302)
✅ CI passing with all E2E tests working correctly
Files Changed
lib/services/session.service.ts(+58, -22)app/api/books/[id]/status/route.ts(+1, -1)__tests__/integration/services/sessions/session/core.test.ts(+131)__tests__/e2e/api/books/dnf-transitions.test.ts(+190, new file)Total: +380 insertions, -23 deletions