code
Clone this repository
https://tangled.org/bretton.dev/coves
git@knot.bretton.dev:bretton.dev/coves
For self-hosted knots, clone URLs may differ based on your setup.
Add 22 test functions with 32 test scenarios achieving 94.3% code coverage of the
comment service layer. Uses manual mocks (no dependencies) following existing patterns.
Test coverage breakdown:
GetComments() validation:
- Valid request with all required parameters
- Missing PostURI validation
- Invalid sort parameter validation
- Negative limit validation
- Negative depth validation
- Limit exceeding maximum validation
GetComments() functionality:
- Empty result set handling
- Viewer authentication state (authenticated vs unauthenticated)
- Nested replies with hasMore flag
- Multiple comments with correct ordering
- Repository error propagation
buildThreadViews():
- Flat structure (all root comments, no nesting)
- Single-level nesting (comments with direct replies)
- Multi-level nesting (recursive reply chains)
- Depth limiting (respects maxDepth parameter)
- Reply limiting (respects maxRepliesPerParent)
- Empty input handling
buildCommentView():
- Complete comment with all fields populated
- Viewer state hydration (vote direction + voteUri)
- Missing author handling (returns nil)
- Nil input handling
Implementation details:
- Manual mock repositories (mockCommentRepo, mockUserRepo, mockPostRepo, mockCommunityRepo)
- No external dependencies (testify/mock, gomock, etc.)
- Fast execution (~10ms, no database required)
- Comprehensive edge case coverage
This addresses PR review feedback requesting unit test coverage for the service layer
to complement existing integration tests.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Add end-to-end integration tests validating comment voting functionality including
vote creation, count updates, and viewer state hydration.
Test coverage:
- TestCommentVote_CreateAndUpdate: Vote count increments and viewer state
- Upvote increments upvote_count and score
- Downvote increments downvote_count and decreases score
- Vote changes properly update counts (up→down, removal)
- TestCommentVote_ViewerState: Viewer-specific state in API responses
- Authenticated viewer sees their vote state (direction + voteUri)
- Authenticated viewer without vote sees null viewer state
- Unauthenticated requests have no viewer object
All tests use fixed timestamps (time.Date) instead of time.Now() to prevent race
conditions and flaky tests. This ensures deterministic test behavior across runs.
Test data setup:
- Uses Jetstream event consumers for realistic indexing flow
- Creates test users, communities, posts, comments, and votes
- Validates full round-trip: event → indexing → query API → response
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Remove unused cid and created_at columns from batch vote query. These fields were
being fetched but never used in the result mapping.
Changes:
- Remove cid and created_at from SELECT clause
- Keep only subject_uri, direction, and uri (all actively used)
- Maintain same query performance characteristics
This reduces memory usage and network overhead for viewer state hydration without
changing behavior. Each comment query fetches vote state for potentially hundreds
of comments, so column reduction has meaningful impact at scale.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Type assertions on map values return pointers to loop variables, which can cause
nil pointer dereferences or incorrect values if addresses are taken directly.
Changes:
- Create explicit copies of type-asserted direction and voteURI values
- Take addresses of copies instead of loop variables for Viewer.Vote and Viewer.VoteURI
- Add DefaultRepliesPerParent package-level constant (was magic number)
- Document constant rationale: balances UX context with query performance
This fixes potential nil pointer panics in comment viewer state hydration and
improves code maintainability by making magic numbers visible and documented.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Add comprehensive documentation explaining return value semantics for malformed URIs.
Changes:
- Clarify that empty string indicates "unknown/unsupported collection"
- Document that callers should validate return value before using for DB queries
- Add examples of expected collection names (e.g., "social.coves.feed.comment")
- Explain format expectations (at://did/collection/rkey)
This addresses PR review feedback about input validation documentation. Empty string
is the correct sentinel value indicating unparseable/invalid URIs, and callers must
handle this appropriately.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Replace hardcoded post-only count updates with collection-aware routing that handles
both posts and comments. This enables proper vote and reply count tracking for comments.
Changes:
- Extract collection from subject/parent URIs using ExtractCollectionFromURI
- Route updates to correct table based on collection type:
- social.coves.community.post → posts table
- social.coves.feed.comment → comments table
- Add comprehensive error handling for unknown collections
- Maintain atomic transaction boundaries for data integrity
This prepares the indexing pipeline for Phase 2B comment voting where votes can target
either posts OR comments. Previously, all votes assumed post subjects.
Performance: ExtractCollectionFromURI is 1,000-20,000x faster than DB lookups for
collection detection.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
When comments arrive before their parent posts (common with cross-repo Jetstream ordering),
post comment_count would remain at 0 even after comments were successfully indexed.
Changes:
- Add indexPostAndReconcileCounts() method to post consumer
- Use atomic transaction to insert post + reconcile comment count
- Reconciliation query counts all non-deleted comments with matching parent_uri
- Update constructor signature to accept database for transaction support
This fixes P0 data integrity issue where posts had permanently stale comment counts.
Test coverage:
- Existing integration tests validate reconciliation behavior
- Post consumer now matches comment consumer pattern (lines 343-356)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>