Skip to content

test(comments): unit tests for NIP-22 comment tags and usePostComment #341

@DSanich

Description

@DSanich

Summary

Add pure unit tests for NIP-22 (kind 1111) tag construction and hook tests for usePostComment, including onMutate / onSettled cache behaviour for ['nostr', 'comments', …] queries. Comments power threading and Funnelcake indexing — tag mistakes cause missing threads, not loud UI errors.

Problem

  • src/hooks/usePostComment.ts builds a large conditional matrix: URL vs event roots, addressable vs replaceable kinds, reply vs top-level branch, NKinds from @nostrify/nostrify.
  • Logic is inline in mutationFn — hard to review and not covered by CommentForm.test.tsx (that file mocks the hook entirely).
  • Wrong E/A/a/e/P/p/K/k combinations are exactly the bugs this suite should prevent.

Expected outcome

  1. Preferred: Extract buildNip22CommentTags(root, reply, content) (or similarly named) into src/lib/ returning string[][], with table-driven tests for representative cases:
    • Addressable 34236 video root with d tag → expect A, E, K, P as in current code.
    • Top-level vs reply: duplicate a/e branch when reply is absent (mirror implementation).
    • URL roots: I/i tags and hostname-based K/k.
  2. usePostComment.test.ts: mock useNostrPublish, assert publishEvent receives built tags; verify onMutate cancels/updates queries as implemented.

What needs to be tested

Pure tag builder (if extracted)

  • At minimum three fixtures: video addressable event, generic event root, URL root; plus one reply-to-comment case if supported by types.
  • Snapshot or deep-equal tags array order — order may matter to some indexers.

Hook

  • publishEvent invoked with kind: 1111 and expected tags for each fixture.
  • Cache: cancelQueries / invalidateQueries keys align with rootId derivation (root.id vs URL string).

Mocking strategy

  • Construct minimal NostrEvent objects (id, pubkey, kind, tags, content, created_at, sig) — no relay I/O.
  • vi.mock('@/hooks/useNostrPublish').

Acceptance criteria

  • Tests live next to code or under src/lib/ for extracted builder + src/hooks/usePostComment.test.ts.
  • No network.
  • npm run test + npx tsc --noEmit pass.

Related files

File Role
src/hooks/usePostComment.ts Mutation + tag logic
src/components/comments/CommentForm.test.tsx Mocks hook — not coverage

Notes

  • Out of scope: full useComments infinite query tests unless time permits — file a follow-up issue if parsing/query filters need coverage too.
  • If extraction is rejected by reviewers, document mutation-only tests with duplicated expectations — still better than zero coverage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions