Skip to content

Commit 9a4ae93

Browse files
authored
test: add more fixtures for api responses in e2e testing (#1599)
1 parent 3d99592 commit 9a4ae93

File tree

5 files changed

+130
-9
lines changed

5 files changed

+130
-9
lines changed

modules/runtime/server/cache.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,44 @@ function getMockForUrl(url: string): MockResult | null {
195195
return { data: null }
196196
}
197197

198+
// npm attestations API - return empty attestations (provenance not needed in tests)
199+
if (host === 'registry.npmjs.org' && pathname.startsWith('/-/npm/v1/attestations/')) {
200+
return { data: { attestations: [] } }
201+
}
202+
203+
// Constellation API - return empty results for link queries
204+
if (host === 'constellation.microcosm.blue') {
205+
if (pathname === '/links/distinct-dids') {
206+
return { data: { total: 0, linking_dids: [], cursor: undefined } }
207+
}
208+
if (pathname === '/links/all') {
209+
return { data: { links: {} } }
210+
}
211+
if (pathname === '/xrpc/blue.microcosm.links.getBacklinks') {
212+
return { data: { total: 0, records: [], cursor: undefined } }
213+
}
214+
return { data: null }
215+
}
216+
217+
// UNGH (GitHub proxy) - return mock repo metadata
218+
if (host === 'ungh.cc') {
219+
const repoMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)$/)
220+
if (repoMatch?.[1] && repoMatch?.[2]) {
221+
return {
222+
data: {
223+
repo: {
224+
description: `${repoMatch[1]}/${repoMatch[2]} - mock repo description`,
225+
stars: 1000,
226+
forks: 100,
227+
watchers: 50,
228+
defaultBranch: 'main',
229+
},
230+
},
231+
}
232+
}
233+
return { data: null }
234+
}
235+
198236
// GitHub API - handled via fixtures, return null to use fixture system
199237
// Note: The actual fixture loading is handled in fetchFromFixtures via special case
200238
if (host === 'api.github.com') {
@@ -426,6 +464,19 @@ async function handleGitHubApi(
426464
return { data: [] }
427465
}
428466

467+
// Commits endpoint: /repos/{owner}/{repo}/commits
468+
const commitsMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)\/commits$/)
469+
if (commitsMatch) {
470+
// Return a single-item array; fetchPageCount will use body.length when no Link header
471+
return { data: [{ sha: 'mock-commit' }] }
472+
}
473+
474+
// Search endpoint: /search/issues, /search/commits, etc.
475+
const searchMatch = pathname.match(/^\/search\/(.+)$/)
476+
if (searchMatch) {
477+
return { data: { total_count: 0, incomplete_results: false, items: [] } }
478+
}
479+
429480
// Other GitHub API endpoints can be added here as needed
430481
return null
431482
}

nuxt.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import process from 'node:process'
22
import { currentLocales } from './config/i18n'
3-
import { isCI, provider } from 'std-env'
3+
import { isCI, isTest, provider } from 'std-env'
44

55
export default defineNuxtConfig({
66
modules: [
@@ -216,6 +216,9 @@ export default defineNuxtConfig({
216216
include: ['../test/unit/server/**/*.ts'],
217217
},
218218
},
219+
replace: {
220+
'import.meta.test': isTest,
221+
},
219222
},
220223

221224
fonts: {

server/utils/atproto/oauth.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@ export const handleResolver = new AtprotoDohHandleResolver({
1919
})
2020

2121
export function getOauthClientMetadata() {
22-
const dev = import.meta.dev
22+
const redirect_uri = `${clientUri}/api/auth/atproto`
2323

24-
const client_uri = clientUri
25-
const redirect_uri = `${client_uri}/api/auth/atproto`
26-
27-
const client_id = dev
28-
? `http://localhost?redirect_uri=${encodeURIComponent(redirect_uri)}&scope=${encodeURIComponent(scope)}`
29-
: `${client_uri}/oauth-client-metadata.json`
24+
const client_id =
25+
import.meta.dev || import.meta.test
26+
? `http://localhost?redirect_uri=${encodeURIComponent(redirect_uri)}&scope=${encodeURIComponent(scope)}`
27+
: `${clientUri}/oauth-client-metadata.json`
3028

3129
// If anything changes here, please make sure to also update /shared/schemas/oauth.ts to match
3230
return parse(OAuthMetadataSchema, {
3331
client_name: 'npmx.dev',
3432
client_id,
35-
client_uri,
33+
client_uri: clientUri,
3634
scope,
3735
redirect_uris: [redirect_uri] as [string, ...string[]],
3836
grant_types: ['authorization_code', 'refresh_token'],
1.24 KB
Loading

test/fixtures/mock-routes.cjs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ function matchNpmRegistry(urlString) {
124124
return json({ error: 'Not found' }, 404)
125125
}
126126

127+
// Attestations endpoint - return empty attestations
128+
if (pathname.startsWith('/-/npm/v1/attestations/')) {
129+
return json({ attestations: [] })
130+
}
131+
127132
// Packument
128133
if (!pathname.startsWith('/-/')) {
129134
let packageName = pathname.slice(1)
@@ -440,6 +445,52 @@ function matchGravatarApi(_urlString) {
440445
return { status: 404, contentType: 'text/plain', body: 'Not found' }
441446
}
442447

448+
/**
449+
* @param {string} urlString
450+
* @returns {MockResponse | null}
451+
*/
452+
function matchUnghApi(urlString) {
453+
const url = new URL(urlString)
454+
455+
const repoMatch = url.pathname.match(/^\/repos\/([^/]+)\/([^/]+)$/)
456+
if (repoMatch && repoMatch[1] && repoMatch[2]) {
457+
return json({
458+
repo: {
459+
description: `${repoMatch[1]}/${repoMatch[2]} - mock repo description`,
460+
stars: 1000,
461+
forks: 100,
462+
watchers: 50,
463+
defaultBranch: 'main',
464+
},
465+
})
466+
}
467+
468+
return json(null)
469+
}
470+
471+
/**
472+
* @param {string} urlString
473+
* @returns {MockResponse | null}
474+
*/
475+
function matchConstellationApi(urlString) {
476+
const url = new URL(urlString)
477+
478+
if (url.pathname === '/links/distinct-dids') {
479+
return json({ total: 0, linking_dids: [], cursor: undefined })
480+
}
481+
482+
if (url.pathname === '/links/all') {
483+
return json({ links: {} })
484+
}
485+
486+
if (url.pathname === '/xrpc/blue.microcosm.links.getBacklinks') {
487+
return json({ total: 0, records: [], cursor: undefined })
488+
}
489+
490+
// Unknown constellation endpoint - return empty
491+
return json(null)
492+
}
493+
443494
/**
444495
* @param {string} urlString
445496
* @returns {MockResponse | null}
@@ -454,6 +505,18 @@ function matchGitHubApi(urlString) {
454505
return json(fixture || [])
455506
}
456507

508+
// Commits endpoint
509+
const commitsMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)\/commits$/)
510+
if (commitsMatch) {
511+
return json([{ sha: 'mock-commit' }])
512+
}
513+
514+
// Search endpoint (issues, commits, etc.)
515+
const searchMatch = pathname.match(/^\/search\/(.+)$/)
516+
if (searchMatch) {
517+
return json({ total_count: 0, incomplete_results: false, items: [] })
518+
}
519+
457520
return null
458521
}
459522

@@ -480,6 +543,12 @@ const routes = [
480543
},
481544
{ name: 'Gravatar API', pattern: 'https://www.gravatar.com/**', match: matchGravatarApi },
482545
{ name: 'GitHub API', pattern: 'https://api.github.com/**', match: matchGitHubApi },
546+
{ name: 'UNGH API', pattern: 'https://ungh.cc/**', match: matchUnghApi },
547+
{
548+
name: 'Constellation API',
549+
pattern: 'https://constellation.microcosm.blue/**',
550+
match: matchConstellationApi,
551+
},
483552
]
484553

485554
/**

0 commit comments

Comments
 (0)