Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/tools/trending-scrape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ const article = (description: string) => `
</span> </div>
</article>`

// Same shape, but the counts vary — copied from github.com/trending/javascript?since=daily.
const articleWithCounts = (name: string, stars: string, today: string) => `
<article class="Box-row">
<h2 class="h3 lh-condensed">
<a href="/${name}">${name.replace('/', ' / ')}</a>
</h2>
<p class="col-9 color-fg-muted my-1 pr-4">a repo</p>
<div class="f6 color-fg-muted mt-2">
<span itemprop="programmingLanguage">JavaScript</span>
<a href="/${name}/stargazers"><svg aria-hidden="true"></svg>
${stars}</a>
<span class="d-inline-block float-sm-right"><svg aria-hidden="true"></svg>
${today}
</span> </div>
</article>`

const descriptionOf = (raw: string) => parseTrendingHtml(article(raw))[0].description

test('decodes the escaped ampersand GitHub emits in repo descriptions', () => {
Expand Down Expand Up @@ -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')
})
2 changes: 1 addition & 1 deletion src/tools/trending-scrape.tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading