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
- 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.
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
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.
Summary
Add pure unit tests for NIP-22 (kind 1111) tag construction and hook tests for
usePostComment, includingonMutate/onSettledcache behaviour for['nostr', 'comments', …]queries. Comments power threading and Funnelcake indexing — tag mistakes cause missing threads, not loud UI errors.Problem
src/hooks/usePostComment.tsbuilds a large conditional matrix: URL vs event roots, addressable vs replaceable kinds, reply vs top-level branch, NKinds from@nostrify/nostrify.mutationFn— hard to review and not covered byCommentForm.test.tsx(that file mocks the hook entirely).E/A/a/e/P/p/K/kcombinations are exactly the bugs this suite should prevent.Expected outcome
buildNip22CommentTags(root, reply, content)(or similarly named) intosrc/lib/returningstring[][], with table-driven tests for representative cases:dtag → expectA,E,K,Pas in current code.a/ebranch whenreplyis absent (mirror implementation).I/itags and hostname-basedK/k.usePostComment.test.ts: mockuseNostrPublish, assertpublishEventreceives built tags; verifyonMutatecancels/updates queries as implemented.What needs to be tested
Pure tag builder (if extracted)
tagsarray order — order may matter to some indexers.Hook
publishEventinvoked withkind: 1111and expected tags for each fixture.cancelQueries/invalidateQuerieskeys align withrootIdderivation (root.idvs URL string).Mocking strategy
NostrEventobjects (id, pubkey, kind, tags, content, created_at, sig) — no relay I/O.vi.mock('@/hooks/useNostrPublish').Acceptance criteria
src/lib/for extracted builder +src/hooks/usePostComment.test.ts.npm run test+npx tsc --noEmitpass.Related files
src/hooks/usePostComment.tssrc/components/comments/CommentForm.test.tsxNotes
useCommentsinfinite query tests unless time permits — file a follow-up issue if parsing/query filters need coverage too.