TEST-001: Add unit test coverage for parseSearchQuery + keywords field - #438
Conversation
Extends ParsedSearchQuery on both frontend and backend with a
keywords: string[] field (derived from the existing textQuery),
satisfying TEST-001's required { keywords, usernames, hashtags }
shape without breaking existing consumers (SearchContainer,
postsResolver) that still rely on textQuery/tokens.
Also expands unit test coverage for parseSearchQuery per the issue's
full matrix: plain keywords, @username, #hashtag, all mixed
combinations, empty/whitespace input, case normalization, repeated
whitespace, lone @/#, hyphen/underscore handling, multiple
usernames/hashtags, and order-independence.
Frontend: 3 -> 26 tests
Backend: ~20 -> 28 tests, 100% coverage on parseSearchQuery.ts maintained
|
@Sakshamk17 is attempting to deploy a commit to the Louis Girifalco's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@Sakshamk17 thank you for adding focused coverage around @motirebuma could you provide the technical review once that clarification is recorded, with particular attention to backward compatibility for existing search consumers? The Vercel authorization warning appears separate from the unit-test result, so it may be documented rather than treated as evidence that the test suite failed. |
motirebuma
left a comment
There was a problem hiding this comment.
hi @Sakshamk17 - PR is good, the keywords field is a welcomed addition, and the test expansion is thorough.
What's good
-
keywordsfield - splitting the text query on spaces is simple and correct, guarding against empty string is also a good idea to avoid[''], and it's applied symmetrically on both sides. TheParsedSearchQuerytype is correctly updated in both the backend (quotevote-backend/app/types/search.ts) and the frontend (quotevote-frontend/src/types/search.ts). -
Backend test expansion - all existing tests assert the
keywordsfield, and more importantly, there are additional tests for single word queries, mixed queries (school safety @marta #education), token ordering, isolated@and#, and collapsed whitespace. Everything seems well-covered. -
Frontend test expansion - going from 4 tests to something comprehensive is great to see, and they're well-organized with
describes for each category (plain keywords,@,#, mixed, whitespace). There are tests for case folding, deduplication, hyphen truncation, underscores, and isolated symbols. Everything seems well-covered. -
Nothing changed where nothing was supposed to change -
keywordsare a new addition, and the existingusernames,hashtags,textQuery, andtokensare all untouched.
Items to address
-
Newline at the end of file is missing -
quotevote-backend/__tests__/unit/parseSearchQuery.test.tsandquotevote-frontend/src/__tests__/utils/parseSearchQuery.test.tsboth have the last line end with});without a newline after. It's a minor style issue but most linters and git_configs will warn about it - just add an extra newline at the end of the file. -
The
keywordsfield may contain punctuation from the query - if the input was@ hello,keywordswould be['@', 'hello'], same with# hello→['#', 'hello']. It's a minor issue really - those are valid entries if you consider@and#to be standalone tokens, but it's worth noting that the field may contain such punctuation. Unless, of course, you actually parse@and#out into their own fields, but I don't think you do - you probably just full-text search overkeywords, which is fine.
Thank you @Sakshamk17
- keywords no longer includes tokens made up entirely of punctuation (e.g. a lone '@' or '#'); textQuery is unaffected and still retains them verbatim for backwards compatibility - words containing punctuation (e.g. user@example.com) are unaffected - addresses review feedback from @flyblackbox on QuoteVote#388
|
Thanks for the thorough review! Fixed the trailing newlines on both test files. Pushed as a follow-up commit — all tests green . |
motirebuma
left a comment
There was a problem hiding this comment.
hey @Sakshamk17 - good job with the follow-up. The punctuation filtering is handled correctly now.
Previously raised issues
Punctuation in keywords - RESOLVED. The keywords array is now correctly filtered using .filter((word) => /\w/.test(word)) to remove punctuation-only tokens like standalone @ and #. Updated tests, including a new section titled "punctuation-only tokens" that checks for single @, single #, punctuation-only words, email addresses, and punctuation mixed with letters. Added JSDoc to the type description. Great job.
What's good (as mentioned in the previous review)
-
The
keywordsfield is simple, correct, and consistently used on both sides -
Good test coverage for both FE and BE, with tests nicely organized into
describes -
Purely additive - existing
usernames,hashtags,textQuery, andtokensare untouched
CI is all green.
Thanks!
Closes #388
Summary
Adds comprehensive unit test coverage for
parseSearchQueryon both thefrontend and backend, and extends the parsing utility to return the
{ keywords, usernames, hashtags }shape requested in the issue.Background / design decision
A
parseSearchQueryutility already existed in both packages(
quotevote-frontend/src/utils/parseSearchQuery.tsandquotevote-backend/app/data/utils/parseSearchQuery.ts), returning{ usernames, hashtags, textQuery, tokens }.textQueryandtokensarealready consumed elsewhere:
SearchContainer.tsx(frontend) builds its "matching "..."" label fromtextQuerypostsResolver.ts(backend) usestextQueryfor the Mongo$textsearchfilter, and
tokensfor username/hashtag filter constructionRather than replace that shape and risk a breaking refactor, this PR
extends it with a
keywords: string[]field (the existingtextQuerysplit on whitespace), so the issue's required shape is satisfied as a
strict superset.
textQueryandtokensare unchanged and kept forbackwards compatibility — no consumer needed to change.
Changes
quotevote-frontend/src/types/search.ts— addedkeywords: readonly string[]toParsedSearchQueryquotevote-frontend/src/utils/parseSearchQuery.ts— compute and returnkeywordsquotevote-frontend/src/__tests__/utils/parseSearchQuery.test.ts— expanded from 3 → 26 testsquotevote-backend/app/types/search.ts— addedkeywords: readonly string[]toParsedSearchQueryquotevote-backend/app/data/utils/parseSearchQuery.ts— compute and returnkeywordsquotevote-backend/__tests__/unit/parseSearchQuery.test.ts— expanded from ~20 → 28 testsTest coverage added (per issue's matrix)
@usernamesearch (single, multiple, dedup, case normalization, hyphen/underscore rules, lone@,@+ trailing text)#hashtagsearch (single, multiple, dedup, case normalization, hyphen/underscore rules, lone#,#+ trailing text)user@example.comshould not be parsed as a username)Test plan
All existing tests pass with no regressions, including
SearchContainer.test.tsx,NewSearchContainer.test.tsx, andpostsResolver.test.ts— confirming thekeywordsaddition doesn'tdisturb existing
textQuery/tokensconsumers.Acceptance criteria checklist
Note for reviewer
keywordspreserves the original casing of the input text (onlyusernames/hashtagsare lowercased) — e.g.parseSearchQuery('Hello World')returns
keywords: ['Hello', 'World'], matching the existingtextQuerybehavior. Flagging this in case keyword-level case normalization is also
expected — happy to add
.toLowerCase()to the keywords array if so.