diff --git a/src/tools/trending-scrape.test.ts b/src/tools/trending-scrape.test.ts index e36e41e..0b16b98 100644 --- a/src/tools/trending-scrape.test.ts +++ b/src/tools/trending-scrape.test.ts @@ -22,6 +22,22 @@ const article = (description: string) => ` ` +// Same shape, but the counts vary — copied from github.com/trending/javascript?since=daily. +const articleWithCounts = (name: string, stars: string, today: string) => ` +
+

+ ${name.replace('/', ' / ')} +

+

a repo

+
+ JavaScript + + ${stars} + + ${today} +
+
` + const descriptionOf = (raw: string) => parseTrendingHtml(article(raw))[0].description test('decodes the escaped ampersand GitHub emits in repo descriptions', () => { @@ -54,3 +70,26 @@ test('still reads the name, language and both star counts', () => { assert.equal(repo.stars, 386) assert.equal(repo.todayStars, 28) }) + +test('reads the singular "1 star today" GitHub renders, not just the plural form', () => { + const repos = parseTrendingHtml( + articleWithCounts('OWASP/threat-dragon', '1,554', '1 star today') + articleWithCounts('addyosmani/agent-skills', '2,100', '226 stars today') + ) + + assert.equal(repos[0].todayStars, 1, 'a repo that gained one star must not be recorded as gaining none') + assert.equal(repos[1].todayStars, 226) +}) + +test('still parses the plural form, thousands separator and all', () => { + const [repo] = parseTrendingHtml(articleWithCounts('TencentCloud/TencentDB-Agent-Memory', '15,584', '1,892 stars today')) + + assert.equal(repo.todayStars, 1892) + assert.equal(repo.stars, 15584) +}) + +test('falls back to zero only when the growth line is genuinely absent', () => { + const [repo] = parseTrendingHtml(articleWithCounts('foo/bar', '10', '')) + + assert.equal(repo.todayStars, 0) + assert.equal(repo.name, 'foo/bar') +}) diff --git a/src/tools/trending-scrape.tool.ts b/src/tools/trending-scrape.tool.ts index 201f79e..7228852 100644 --- a/src/tools/trending-scrape.tool.ts +++ b/src/tools/trending-scrape.tool.ts @@ -45,7 +45,7 @@ export function parseTrendingHtml(html: string): TrendingRepo[] { const stars = starsMatch ? parseInt(starsMatch[1].replace(/,/g, ''), 10) : 0 // Stars today - const todayMatch = block.match(/([\d,]+)\s+stars\s+today/) + const todayMatch = block.match(/([\d,]+)\s+stars?\s+today/) const todayStars = todayMatch ? parseInt(todayMatch[1].replace(/,/g, ''), 10) : 0 repos.push({