Skip to content

Commit a832fa2

Browse files
committed
test: handle test environment in oauth metadata
1 parent 00545e5 commit a832fa2

4 files changed

Lines changed: 35 additions & 9 deletions

File tree

modules/runtime/server/cache.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,19 @@ async function handleGitHubApi(
464464
return { data: [] }
465465
}
466466

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+
467480
// Other GitHub API endpoints can be added here as needed
468481
return null
469482
}

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'],

test/fixtures/mock-routes.cjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,18 @@ function matchGitHubApi(urlString) {
505505
return json(fixture || [])
506506
}
507507

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+
508520
return null
509521
}
510522

0 commit comments

Comments
 (0)