-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathbadge.spec.ts
More file actions
175 lines (139 loc) · 6.38 KB
/
badge.spec.ts
File metadata and controls
175 lines (139 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { expect, test } from './test-utils'
function toLocalUrl(baseURL: string | undefined, path: string): string {
if (!baseURL) return path
return baseURL.endsWith('/') ? `${baseURL}${path.slice(1)}` : `${baseURL}${path}`
}
async function fetchBadge(page: { request: { get: (url: string) => Promise<any> } }, url: string) {
const response = await page.request.get(url)
const body = await response.text()
return { response, body }
}
test.describe('badge API', () => {
const badgeMap: Record<string, string> = {
'version': 'version',
'license': 'license',
'size': 'install size',
'downloads': 'downloads/mo',
'downloads-day': 'downloads/day',
'downloads-week': 'downloads/wk',
'downloads-month': 'downloads/mo',
'downloads-year': 'downloads/yr',
'vulnerabilities': 'vulns',
'dependencies': 'dependencies',
'updated': 'updated',
'engines': 'node',
'types': 'types',
'created': 'created',
'maintainers': 'maintainers',
'deprecated': 'status',
'quality': 'quality',
'popularity': 'popularity',
'maintenance': 'maintenance',
'score': 'score',
}
const percentageTypes = new Set(['quality', 'popularity', 'maintenance', 'score'])
for (const [type, expectedLabel] of Object.entries(badgeMap)) {
test.describe(`${type} badge`, () => {
test('renders correct label', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, `/api/registry/badge/${type}/nuxt`)
const { response, body } = await fetchBadge(page, url)
expect(response.status()).toBe(200)
expect(response.headers()['content-type']).toContain('image/svg+xml')
expect(body).toContain(expectedLabel)
})
test('scoped package renders successfully', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, `/api/registry/badge/${type}/@nuxt/kit`)
const { response } = await fetchBadge(page, url)
expect(response.status()).toBe(200)
})
test('explicit version badge renders successfully', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, `/api/registry/badge/${type}/nuxt/v/3.12.0`)
const { response, body } = await fetchBadge(page, url)
expect(response.status()).toBe(200)
if (type === 'version') {
expect(body).toContain('v3.12.0')
}
})
test('respects name=true parameter', async ({ page, baseURL }) => {
const packageName = 'nuxt'
const url = toLocalUrl(baseURL, `/api/registry/badge/${type}/${packageName}?name=true`)
const { body } = await fetchBadge(page, url)
expect(body).toContain(packageName)
expect(body).not.toContain(expectedLabel)
})
if (percentageTypes.has(type)) {
test('contains percentage value', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, `/api/registry/badge/${type}/vue`)
const { body } = await fetchBadge(page, url)
expect(body).toMatch(/\d+%|unknown/)
})
}
})
}
test.describe('specific scenarios', () => {
test('downloads-year handles large numbers', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, '/api/registry/badge/downloads-year/vue')
const { body } = await fetchBadge(page, url)
expect(body).toContain('downloads/yr')
expect(body).not.toContain('NaN')
})
test('deprecated badge shows active for non-deprecated packages', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, '/api/registry/badge/deprecated/vue')
const { body } = await fetchBadge(page, url)
expect(body).toContain('active')
})
})
test('custom labelColor parameter is applied to SVG', async ({ page, baseURL }) => {
const customColor = '00ff00'
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?labelColor=${customColor}`)
const { body } = await fetchBadge(page, url)
expect(body).toContain(`fill="#${customColor}"`)
})
test('custom color parameter is applied to SVG', async ({ page, baseURL }) => {
const customColor = 'ff69b4'
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?color=${customColor}`)
const { body } = await fetchBadge(page, url)
expect(body).toContain(`fill="#${customColor}"`)
})
test('custom label parameter is applied to SVG', async ({ page, baseURL }) => {
const customLabel = 'my-label'
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?label=${customLabel}`)
const { body } = await fetchBadge(page, url)
expect(body).toContain(customLabel)
})
test('style=default keeps current badge renderer', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, '/api/registry/badge/version/nuxt?style=default')
const { body } = await fetchBadge(page, url)
expect(body).toContain('font-family="Geist, system-ui, -apple-system, sans-serif"')
})
test('style=shieldsio renders shields.io-like badge', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, '/api/registry/badge/version/nuxt?style=shieldsio')
const { body } = await fetchBadge(page, url)
expect(body).toContain('font-family="Verdana, Geneva, DejaVu Sans, sans-serif"')
})
test('invalid badge type defaults to version strategy', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, '/api/registry/badge/invalid-type/nuxt')
const { body } = await fetchBadge(page, url)
expect(body).toContain('version')
})
test('missing package returns 404', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, '/api/registry/badge/version/')
const { response } = await fetchBadge(page, url)
expect(response.status()).toBe(404)
})
test('endpoint badge renders from external JSON', async ({ page, baseURL }) => {
const endpointUrl = encodeURIComponent(
'https://raw.githubusercontent.com/solidjs-community/solid-primitives/main/assets/badges/stage-2.json',
)
const url = toLocalUrl(baseURL, `/api/registry/badge/endpoint/_?url=${endpointUrl}`)
const { body, response } = await fetchBadge(page, url)
expect(response.status()).toBe(200)
expect(body).toContain('STAGE')
expect(body).toContain('>2<')
})
test('endpoint badge without url returns 400', async ({ page, baseURL }) => {
const url = toLocalUrl(baseURL, '/api/registry/badge/endpoint/_')
const { response } = await fetchBadge(page, url)
expect(response.status()).toBe(400)
})
})