Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 51 additions & 0 deletions modules/runtime/server/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,44 @@ function getMockForUrl(url: string): MockResult | null {
return { data: null }
}

// npm attestations API - return empty attestations (provenance not needed in tests)
if (host === 'registry.npmjs.org' && pathname.startsWith('/-/npm/v1/attestations/')) {
return { data: { attestations: [] } }
}

// Constellation API - return empty results for link queries
if (host === 'constellation.microcosm.blue') {
if (pathname === '/links/distinct-dids') {
return { data: { total: 0, linking_dids: [], cursor: undefined } }
}
if (pathname === '/links/all') {
return { data: { links: {} } }
}
if (pathname === '/xrpc/blue.microcosm.links.getBacklinks') {
return { data: { total: 0, records: [], cursor: undefined } }
}
return { data: null }
}

// UNGH (GitHub proxy) - return mock repo metadata
if (host === 'ungh.cc') {
const repoMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)$/)
if (repoMatch?.[1] && repoMatch?.[2]) {
return {
data: {
repo: {
description: `${repoMatch[1]}/${repoMatch[2]} - mock repo description`,
stars: 1000,
forks: 100,
watchers: 50,
defaultBranch: 'main',
},
},
}
}
return { data: null }
}

// GitHub API - handled via fixtures, return null to use fixture system
// Note: The actual fixture loading is handled in fetchFromFixtures via special case
if (host === 'api.github.com') {
Expand Down Expand Up @@ -426,6 +464,19 @@ async function handleGitHubApi(
return { data: [] }
}

// Commits endpoint: /repos/{owner}/{repo}/commits
const commitsMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)\/commits$/)
if (commitsMatch) {
// Return a single-item array; fetchPageCount will use body.length when no Link header
return { data: [{ sha: 'mock-commit' }] }
}

// Search endpoint: /search/issues, /search/commits, etc.
const searchMatch = pathname.match(/^\/search\/(.+)$/)
if (searchMatch) {
return { data: { total_count: 0, incomplete_results: false, items: [] } }
}

// Other GitHub API endpoints can be added here as needed
return null
}
Expand Down
5 changes: 4 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process'
import { currentLocales } from './config/i18n'
import { isCI, provider } from 'std-env'
import { isCI, isTest, provider } from 'std-env'

export default defineNuxtConfig({
modules: [
Expand Down Expand Up @@ -216,6 +216,9 @@ export default defineNuxtConfig({
include: ['../test/unit/server/**/*.ts'],
},
},
replace: {
'import.meta.test': isTest,
},
},

fonts: {
Expand Down
14 changes: 6 additions & 8 deletions server/utils/atproto/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ export const handleResolver = new AtprotoDohHandleResolver({
})

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

const client_uri = clientUri
const redirect_uri = `${client_uri}/api/auth/atproto`

const client_id = dev
? `http://localhost?redirect_uri=${encodeURIComponent(redirect_uri)}&scope=${encodeURIComponent(scope)}`
: `${client_uri}/oauth-client-metadata.json`
const client_id =
import.meta.dev || import.meta.test
? `http://localhost?redirect_uri=${encodeURIComponent(redirect_uri)}&scope=${encodeURIComponent(scope)}`
: `${clientUri}/oauth-client-metadata.json`

// If anything changes here, please make sure to also update /shared/schemas/oauth.ts to match
return parse(OAuthMetadataSchema, {
client_name: 'npmx.dev',
client_id,
client_uri,
client_uri: clientUri,
scope,
redirect_uris: [redirect_uri] as [string, ...string[]],
grant_types: ['authorization_code', 'refresh_token'],
Expand Down
69 changes: 69 additions & 0 deletions test/fixtures/mock-routes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ function matchNpmRegistry(urlString) {
return json({ error: 'Not found' }, 404)
}

// Attestations endpoint - return empty attestations
if (pathname.startsWith('/-/npm/v1/attestations/')) {
return json({ attestations: [] })
}

// Packument
if (!pathname.startsWith('/-/')) {
let packageName = pathname.slice(1)
Expand Down Expand Up @@ -440,6 +445,52 @@ function matchGravatarApi(_urlString) {
return { status: 404, contentType: 'text/plain', body: 'Not found' }
}

/**
* @param {string} urlString
* @returns {MockResponse | null}
*/
function matchUnghApi(urlString) {
const url = new URL(urlString)

const repoMatch = url.pathname.match(/^\/repos\/([^/]+)\/([^/]+)$/)
if (repoMatch && repoMatch[1] && repoMatch[2]) {
return json({
repo: {
description: `${repoMatch[1]}/${repoMatch[2]} - mock repo description`,
stars: 1000,
forks: 100,
watchers: 50,
defaultBranch: 'main',
},
})
}

return json(null)
}

/**
* @param {string} urlString
* @returns {MockResponse | null}
*/
function matchConstellationApi(urlString) {
const url = new URL(urlString)

if (url.pathname === '/links/distinct-dids') {
return json({ total: 0, linking_dids: [], cursor: undefined })
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

cursor: undefined is silently dropped by JSON.stringify.

JSON.stringify({ cursor: undefined }) omits the cursor key entirely ({"total":0,"linking_dids":[]}). The cache.ts counterpart returns a plain JS object where cursor: undefined is still present as a property. While both yield undefined on property access, the 'cursor' in result check would differ between the two mock paths. Use null for explicit, round-trip-safe JSON serialisation.

🛠️ Suggested fix
-    return json({ total: 0, linking_dids: [], cursor: undefined })
+    return json({ total: 0, linking_dids: [], cursor: null })
-    return json({ total: 0, records: [], cursor: undefined })
+    return json({ total: 0, records: [], cursor: null })

Also applies to: 487-487

}

if (url.pathname === '/links/all') {
return json({ links: {} })
}

if (url.pathname === '/xrpc/blue.microcosm.links.getBacklinks') {
return json({ total: 0, records: [], cursor: undefined })
}

// Unknown constellation endpoint - return empty
return json(null)
}

/**
* @param {string} urlString
* @returns {MockResponse | null}
Expand All @@ -454,6 +505,18 @@ function matchGitHubApi(urlString) {
return json(fixture || [])
}

// Commits endpoint
const commitsMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)\/commits$/)
if (commitsMatch) {
return json([{ sha: 'mock-commit' }])
}

// Search endpoint (issues, commits, etc.)
const searchMatch = pathname.match(/^\/search\/(.+)$/)
if (searchMatch) {
return json({ total_count: 0, incomplete_results: false, items: [] })
}

return null
}

Expand All @@ -480,6 +543,12 @@ const routes = [
},
{ name: 'Gravatar API', pattern: 'https://www.gravatar.com/**', match: matchGravatarApi },
{ name: 'GitHub API', pattern: 'https://api.github.com/**', match: matchGitHubApi },
{ name: 'UNGH API', pattern: 'https://ungh.cc/**', match: matchUnghApi },
{
name: 'Constellation API',
pattern: 'https://constellation.microcosm.blue/**',
match: matchConstellationApi,
},
]

/**
Expand Down
Loading