|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { flattenArticles, deriveStopWords, searchArticles } from '@/landings/lib/article-search' |
| 4 | +import type { TocItem, ChildTocItem } from '@/landings/types' |
| 5 | + |
| 6 | +const makeArticle = ( |
| 7 | + title: string, |
| 8 | + intro: string, |
| 9 | + fullPath: string, |
| 10 | + category?: string[], |
| 11 | +): ChildTocItem => ({ |
| 12 | + title, |
| 13 | + intro, |
| 14 | + fullPath, |
| 15 | + category: category || null, |
| 16 | +}) |
| 17 | + |
| 18 | +describe('flattenArticles', () => { |
| 19 | + test('flattens nested tocItems into leaf articles', () => { |
| 20 | + const items: TocItem[] = [ |
| 21 | + { |
| 22 | + title: 'Parent', |
| 23 | + fullPath: '/parent', |
| 24 | + childTocItems: [ |
| 25 | + { title: 'Child A', fullPath: '/parent/a', intro: 'Intro A' }, |
| 26 | + { title: 'Child B', fullPath: '/parent/b', intro: 'Intro B' }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + ] |
| 30 | + const result = flattenArticles(items) |
| 31 | + expect(result).toHaveLength(2) |
| 32 | + expect(result.map((a) => a.title)).toEqual(['Child A', 'Child B']) |
| 33 | + }) |
| 34 | + |
| 35 | + test('deduplicates articles by fullPath', () => { |
| 36 | + const items: TocItem[] = [ |
| 37 | + { title: 'Article', fullPath: '/same', intro: 'First' }, |
| 38 | + { title: 'Article', fullPath: '/same', intro: 'Duplicate' }, |
| 39 | + ] |
| 40 | + const result = flattenArticles(items) |
| 41 | + expect(result).toHaveLength(1) |
| 42 | + }) |
| 43 | + |
| 44 | + test('sorts alphabetically by title', () => { |
| 45 | + const items: TocItem[] = [ |
| 46 | + { title: 'Zebra', fullPath: '/z', intro: '' }, |
| 47 | + { title: 'Alpha', fullPath: '/a', intro: '' }, |
| 48 | + { title: 'Middle', fullPath: '/m', intro: '' }, |
| 49 | + ] |
| 50 | + const result = flattenArticles(items) |
| 51 | + expect(result.map((a) => a.title)).toEqual(['Alpha', 'Middle', 'Zebra']) |
| 52 | + }) |
| 53 | + |
| 54 | + test('excludes index pages (parents with children)', () => { |
| 55 | + const items: TocItem[] = [ |
| 56 | + { |
| 57 | + title: 'Index page', |
| 58 | + fullPath: '/index', |
| 59 | + childTocItems: [{ title: 'Leaf', fullPath: '/index/leaf', intro: '' }], |
| 60 | + }, |
| 61 | + ] |
| 62 | + const result = flattenArticles(items) |
| 63 | + expect(result).toHaveLength(1) |
| 64 | + expect(result[0].title).toBe('Leaf') |
| 65 | + }) |
| 66 | +}) |
| 67 | + |
| 68 | +describe('deriveStopWords', () => { |
| 69 | + test('finds words appearing in 80%+ of articles', () => { |
| 70 | + const articles = [ |
| 71 | + makeArticle('GitHub Copilot agents', 'Use Copilot for coding', '/a'), |
| 72 | + makeArticle('GitHub Copilot billing', 'Manage Copilot billing', '/b'), |
| 73 | + makeArticle('GitHub Copilot extensions', 'Build Copilot extensions', '/c'), |
| 74 | + makeArticle('GitHub Copilot settings', 'Configure Copilot settings', '/d'), |
| 75 | + makeArticle('GitHub Copilot MCP', 'Use Copilot with MCP servers', '/e'), |
| 76 | + ] |
| 77 | + const stopWords = deriveStopWords(articles) |
| 78 | + expect(stopWords).toContain('copilot') |
| 79 | + expect(stopWords).toContain('github') |
| 80 | + expect(stopWords).not.toContain('agents') |
| 81 | + expect(stopWords).not.toContain('billing') |
| 82 | + }) |
| 83 | + |
| 84 | + test('returns empty array for empty articles', () => { |
| 85 | + expect(deriveStopWords([])).toEqual([]) |
| 86 | + }) |
| 87 | + |
| 88 | + test('respects custom threshold', () => { |
| 89 | + const articles = [ |
| 90 | + makeArticle('Copilot agents', 'Use agents', '/a'), |
| 91 | + makeArticle('Copilot billing', 'Manage billing', '/b'), |
| 92 | + makeArticle('Copilot settings', 'Configure settings', '/c'), |
| 93 | + ] |
| 94 | + // "copilot" appears in 3/3 = 100%, always a stop word |
| 95 | + expect(deriveStopWords(articles, 0.5)).toContain('copilot') |
| 96 | + // At threshold 1.0, only words in every single article qualify |
| 97 | + const strict = deriveStopWords(articles, 1.0) |
| 98 | + expect(strict).toContain('copilot') |
| 99 | + expect(strict).not.toContain('agents') |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +describe('searchArticles', () => { |
| 104 | + const articles = [ |
| 105 | + makeArticle('Copilot billing plans', 'Manage your billing', '/billing', ['Billing']), |
| 106 | + makeArticle('Copilot agent features', 'Use coding agents', '/agents', ['Agents']), |
| 107 | + makeArticle('Copilot extensions', 'Build and install extensions', '/ext', ['Extensions']), |
| 108 | + ] |
| 109 | + |
| 110 | + test('returns matching articles ranked by score', () => { |
| 111 | + const results = searchArticles(articles, 'billing', []) |
| 112 | + expect(results[0].title).toBe('Copilot billing plans') |
| 113 | + }) |
| 114 | + |
| 115 | + test('returns all articles when query is only stop words', () => { |
| 116 | + const results = searchArticles(articles, 'copilot', ['copilot']) |
| 117 | + expect(results).toHaveLength(3) |
| 118 | + }) |
| 119 | + |
| 120 | + test('strips stop words from query before matching', () => { |
| 121 | + const results = searchArticles(articles, 'copilot billing', ['copilot']) |
| 122 | + expect(results[0].title).toBe('Copilot billing plans') |
| 123 | + }) |
| 124 | + |
| 125 | + test('returns empty array when no articles match', () => { |
| 126 | + const results = searchArticles(articles, 'xyznonexistent', []) |
| 127 | + expect(results).toHaveLength(0) |
| 128 | + }) |
| 129 | + |
| 130 | + test('searches title, intro, and category fields', () => { |
| 131 | + const results = searchArticles(articles, 'agents', []) |
| 132 | + expect(results.length).toBeGreaterThan(0) |
| 133 | + expect(results.some((a) => a.title === 'Copilot agent features')).toBe(true) |
| 134 | + }) |
| 135 | +}) |
0 commit comments