|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { hasSearchOperators, parseSearchOperators } from '~/composables/useStructuredFilters' |
| 2 | +import { |
| 3 | + hasSearchOperators, |
| 4 | + parseSearchOperators, |
| 5 | + removeKeywordFromQuery, |
| 6 | +} from '~/composables/useStructuredFilters' |
3 | 7 |
|
4 | 8 | describe('parseSearchOperators', () => { |
5 | 9 | describe('basic operator parsing', () => { |
@@ -185,3 +189,166 @@ describe('hasSearchOperators', () => { |
185 | 189 | expect(hasSearchOperators({ name: [], keywords: [] })).toBe(false) |
186 | 190 | }) |
187 | 191 | }) |
| 192 | + |
| 193 | +describe('keyword deduplication', () => { |
| 194 | + it('deduplicates same keyword from kw: and keyword: operators', () => { |
| 195 | + const result = parseSearchOperators('kw:react keyword:react') |
| 196 | + expect(result.keywords).toEqual(['react']) |
| 197 | + }) |
| 198 | + |
| 199 | + it('deduplicates case-insensitively', () => { |
| 200 | + const result = parseSearchOperators('kw:React keyword:REACT kw:react') |
| 201 | + expect(result.keywords).toEqual(['React']) |
| 202 | + }) |
| 203 | + |
| 204 | + it('preserves different keywords', () => { |
| 205 | + const result = parseSearchOperators('kw:react keyword:vue') |
| 206 | + expect(result.keywords).toEqual(['react', 'vue']) |
| 207 | + }) |
| 208 | + |
| 209 | + it('deduplicates within comma-separated values', () => { |
| 210 | + const result = parseSearchOperators('kw:react,vue keyword:react,angular') |
| 211 | + expect(result.keywords).toEqual(['react', 'vue', 'angular']) |
| 212 | + }) |
| 213 | +}) |
| 214 | + |
| 215 | +describe('keyword clearing scenarios', () => { |
| 216 | + it('returns keywords when kw: operator is present', () => { |
| 217 | + const result = parseSearchOperators('test kw:react') |
| 218 | + expect(result.keywords).toEqual(['react']) |
| 219 | + expect(result.text).toBe('test') |
| 220 | + }) |
| 221 | + |
| 222 | + it('returns undefined keywords when kw: operator is removed', () => { |
| 223 | + const result = parseSearchOperators('test') |
| 224 | + expect(result.keywords).toBeUndefined() |
| 225 | + expect(result.text).toBe('test') |
| 226 | + }) |
| 227 | + |
| 228 | + it('handles transition from keyword to no keyword', () => { |
| 229 | + // Simulate the state transition when user removes keyword from search |
| 230 | + const withKeyword = parseSearchOperators('test kw:react') |
| 231 | + expect(withKeyword.keywords).toEqual(['react']) |
| 232 | + |
| 233 | + const withoutKeyword = parseSearchOperators('test') |
| 234 | + expect(withoutKeyword.keywords).toBeUndefined() |
| 235 | + |
| 236 | + // This is what useStructuredFilters does in the watcher: |
| 237 | + // filters.value.keywords = [...(parsed.keywords ?? [])] |
| 238 | + const updatedKeywords = [...(withoutKeyword.keywords ?? [])] |
| 239 | + expect(updatedKeywords).toEqual([]) |
| 240 | + }) |
| 241 | + |
| 242 | + it('returns empty keywords array after nullish coalescing', () => { |
| 243 | + // Verify the exact logic used in useStructuredFilters watcher |
| 244 | + const testCases = ['', 'test', 'some search query', 'name:package', 'desc:something'] |
| 245 | + |
| 246 | + for (const query of testCases) { |
| 247 | + const parsed = parseSearchOperators(query) |
| 248 | + // This is the exact line from useStructuredFilters.ts: |
| 249 | + const keywords = [...(parsed.keywords ?? [])] |
| 250 | + expect(keywords).toEqual([]) |
| 251 | + } |
| 252 | + }) |
| 253 | +}) |
| 254 | + |
| 255 | +describe('removeKeywordFromQuery', () => { |
| 256 | + describe('standalone keyword removal', () => { |
| 257 | + it('removes standalone kw:value', () => { |
| 258 | + expect(removeKeywordFromQuery('test kw:react', 'react')).toBe('test') |
| 259 | + }) |
| 260 | + |
| 261 | + it('removes standalone keyword:value', () => { |
| 262 | + expect(removeKeywordFromQuery('test keyword:react', 'react')).toBe('test') |
| 263 | + }) |
| 264 | + |
| 265 | + it('removes keyword at start of query', () => { |
| 266 | + expect(removeKeywordFromQuery('kw:react test', 'react')).toBe('test') |
| 267 | + }) |
| 268 | + |
| 269 | + it('removes keyword when it is the entire query', () => { |
| 270 | + expect(removeKeywordFromQuery('kw:react', 'react')).toBe('') |
| 271 | + }) |
| 272 | + |
| 273 | + it('is case-insensitive', () => { |
| 274 | + expect(removeKeywordFromQuery('kw:React', 'react')).toBe('') |
| 275 | + expect(removeKeywordFromQuery('kw:react', 'React')).toBe('') |
| 276 | + expect(removeKeywordFromQuery('kw:REACT', 'react')).toBe('') |
| 277 | + }) |
| 278 | + }) |
| 279 | + |
| 280 | + describe('comma-separated keyword removal', () => { |
| 281 | + it('removes keyword from middle of comma list', () => { |
| 282 | + expect(removeKeywordFromQuery('kw:foo,bar,baz', 'bar')).toBe('kw:foo,baz') |
| 283 | + }) |
| 284 | + |
| 285 | + it('removes keyword from start of comma list', () => { |
| 286 | + expect(removeKeywordFromQuery('kw:foo,bar,baz', 'foo')).toBe('kw:bar,baz') |
| 287 | + }) |
| 288 | + |
| 289 | + it('removes keyword from end of comma list', () => { |
| 290 | + expect(removeKeywordFromQuery('kw:foo,bar,baz', 'baz')).toBe('kw:foo,bar') |
| 291 | + }) |
| 292 | + |
| 293 | + it('removes only keyword in comma list (drops operator)', () => { |
| 294 | + expect(removeKeywordFromQuery('test kw:react', 'react')).toBe('test') |
| 295 | + }) |
| 296 | + |
| 297 | + it('removes keyword from two-item list', () => { |
| 298 | + expect(removeKeywordFromQuery('kw:foo,bar', 'foo')).toBe('kw:bar') |
| 299 | + expect(removeKeywordFromQuery('kw:foo,bar', 'bar')).toBe('kw:foo') |
| 300 | + }) |
| 301 | + |
| 302 | + it('is case-insensitive within comma list', () => { |
| 303 | + expect(removeKeywordFromQuery('kw:Foo,Bar,Baz', 'bar')).toBe('kw:Foo,Baz') |
| 304 | + }) |
| 305 | + }) |
| 306 | + |
| 307 | + describe('duplicate keyword removal', () => { |
| 308 | + it('removes all occurrences across multiple operators', () => { |
| 309 | + expect(removeKeywordFromQuery('kw:react keyword:react', 'react')).toBe('') |
| 310 | + }) |
| 311 | + |
| 312 | + it('removes from both standalone and comma-separated', () => { |
| 313 | + expect(removeKeywordFromQuery('kw:react,vue keyword:react', 'react')).toBe('kw:vue') |
| 314 | + }) |
| 315 | + |
| 316 | + it('removes duplicate within same comma list', () => { |
| 317 | + expect(removeKeywordFromQuery('kw:react,vue,react', 'react')).toBe('kw:vue') |
| 318 | + }) |
| 319 | + }) |
| 320 | + |
| 321 | + describe('preserves unrelated content', () => { |
| 322 | + it('preserves other operators', () => { |
| 323 | + expect(removeKeywordFromQuery('name:foo kw:react desc:bar', 'react')).toBe( |
| 324 | + 'name:foo desc:bar', |
| 325 | + ) |
| 326 | + }) |
| 327 | + |
| 328 | + it('preserves free text', () => { |
| 329 | + expect(removeKeywordFromQuery('hello world kw:react', 'react')).toBe('hello world') |
| 330 | + }) |
| 331 | + |
| 332 | + it('does not remove substring matches', () => { |
| 333 | + expect(removeKeywordFromQuery('kw:react-hooks', 'react')).toBe('kw:react-hooks') |
| 334 | + }) |
| 335 | + |
| 336 | + it('does not remove keyword that is a prefix of another in comma list', () => { |
| 337 | + expect(removeKeywordFromQuery('kw:react,react-hooks', 'react')).toBe('kw:react-hooks') |
| 338 | + }) |
| 339 | + |
| 340 | + it('does not modify query when keyword is not present', () => { |
| 341 | + expect(removeKeywordFromQuery('kw:vue,angular test', 'react')).toBe('kw:vue,angular test') |
| 342 | + }) |
| 343 | + }) |
| 344 | + |
| 345 | + describe('whitespace handling', () => { |
| 346 | + it('collapses multiple spaces after removal', () => { |
| 347 | + expect(removeKeywordFromQuery('test kw:react more', 'react')).toBe('test more') |
| 348 | + }) |
| 349 | + |
| 350 | + it('trims leading and trailing spaces', () => { |
| 351 | + expect(removeKeywordFromQuery(' kw:react ', 'react')).toBe('') |
| 352 | + }) |
| 353 | + }) |
| 354 | +}) |
0 commit comments