Skip to content

Fix Database Persistence Operations (7 test failures) #231

Closed

Description

@malibio

Bug: Database Persistence Failures

Related to: Issue #208 (Comprehensive Test Coverage)
Test File: src/tests/services/database-persistence.test.ts
Failures: 7 tests failing

⚠️ Agent Instructions - READ FIRST

1. Prerequisites Check

Answer: Issue #245 IS COMPLETE. ✅ Proceed with implementation.

The PersistenceCoordinator test infrastructure is now in place. You can run tests reliably.

2. Scope & Priority

Answer: Address issues in this order:

Priority 1 - Infrastructure Issues (Fix First):

  1. Read-only operations triggering writes → Test infrastructure issue
  2. Rapid sequential updates → May be fixed by PersistenceCoordinator already

Priority 2 - Validation Issues:
3. Empty node validation → Frontend/backend coordination
4. Placeholder behavior → Related to empty nodes
5. Sibling update persistence → Verify PersistenceCoordinator handles this

Priority 3 - Design Decisions:
6. Node deletion cascade/promotion → Business logic decision needed
7. Non-idempotent delete → See answer #5 below

Start by running tests to see which are actually still failing after #245.

3. Testing Approach

Answer: YES, run tests FIRST to see current state.

bun run test src/tests/services/database-persistence.test.ts

Many issues may already be fixed by:

Document which tests still fail and tackle those.

4. Branching Strategy

Answer: Create a single feature branch for this issue.

git checkout -b feature/issue-231-database-persistence-fixes

5. Design Decision: Idempotent Delete

Answer: Make deletion idempotent (return success on non-existent nodes).

Rationale:

  • RESTful best practice: DELETE should be idempotent
  • Simplifies client code (no need to check existence first)
  • Prevents race conditions in distributed scenarios
  • HTTP DELETE typically returns 200/204 even if resource doesn't exist

Implementation:


⚠️ CRITICAL: Use PersistenceCoordinator Pattern

DO NOT call SharedNodeStore directly for persistence operations.

All persistence should go through the proper flow:

// ✅ CORRECT - Use service layer
reactiveNodeService.updateNode(nodeId, changes);
  └─> sharedNodeStore.updateNode(...)
      └─> PersistenceCoordinator.persist(...)

// ❌ WRONG - Don't bypass the service layer
sharedNodeStore.setNode(...);  // Unless you're the service layer

The PersistenceCoordinator handles:

  • Dependency tracking (parent before child)
  • Debouncing (content) vs immediate (structural)
  • FOREIGN KEY constraint ordering
  • Race condition prevention

Files you'll likely modify:

  • packages/desktop-app/src/lib/services/reactive-node-service.svelte.ts (service layer)
  • packages/desktop-app/src-tauri/src/commands.rs (backend validation/idempotent delete)
  • Test file to adjust expectations after fixes

Prerequisites

✅ Issue #245 is COMPLETE - Test infrastructure fixed, proceed with implementation.

Problem

The comprehensive tests have exposed bugs in database persistence layer:

  • Empty nodes not being rejected properly
  • Placeholder behavior inconsistent
  • Sibling updates (beforeSiblingId changes) not persisting
  • Rapid sequential updates causing errors
  • Node deletion cascade/promotion issues
  • Non-idempotent delete (errors on non-existent)
  • Read operations triggering unintended writes

Failing Tests

  1. should reject empty nodes - backend validation prevents placeholder creation
  2. should persist node after content added (first character triggers)
  3. should persist sibling updates (beforeSiblingId changes saved)
  4. should handle rapid sequential updates without database errors
  5. should delete node and handle children via cascade/promotion
  6. should error when deleting non-existent node → UPDATE: Make idempotent
  7. should load nodes without triggering writes (read-only operation)

Implementation Steps

Step 1: Run Tests & Document Current State

bun run test src/tests/services/database-persistence.test.ts

Document which tests fail. Some may already pass after #245 and recent refactoring.

Step 2: Fix Remaining Issues

Based on test results, fix in priority order:

  1. Read-only operations (test infrastructure)
  2. Empty node validation
  3. Sibling update persistence
  4. Rapid updates
  5. Deletion behavior

Step 3: Make Delete Idempotent

Update backend to return success (not error) when deleting non-existent node.

File: packages/desktop-app/src-tauri/src/commands.rs

Look for delete_node command and handle "not found" case gracefully.

Step 4: Verify All Tests Pass

bun run test src/tests/services/database-persistence.test.ts

Acceptance Criteria

  • All 7 tests pass
  • Empty node validation works correctly
  • Sibling updates persist via PersistenceCoordinator
  • Concurrent operations safe (PersistenceCoordinator handles this)
  • Deletion is idempotent (succeeds on non-existent nodes)
  • Read operations don't trigger writes
  • Quality checks pass: bun run quality:fix

Estimated Effort

  • Time: 2-3 hours
  • Complexity: Low-Medium (many issues may already be fixed)
  • Risk: Low (test-driven, clear validation)

Testing

Run: bun run test src/tests/services/database-persistence.test.ts

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions