Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

147 lines
6.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const AUTHORITATIVE_METADATA = {
issuer: 'https://us.hanzo.ai',
authorization_endpoint: 'https://us.hanzo.ai/oauth/authorize/',
token_endpoint: 'https://us.hanzo.ai/oauth/token/',
revocation_endpoint: 'https://us.hanzo.ai/oauth/revoke/',
introspection_endpoint: 'https://us.hanzo.ai/oauth/introspect/',
userinfo_endpoint: 'https://us.hanzo.ai/oauth/userinfo/',
jwks_uri: 'https://us.hanzo.ai/.well-known/jwks.json',
registration_endpoint: 'https://us.hanzo.ai/oauth/register/',
scopes_supported: ['openid', 'profile', 'email'],
response_types_supported: ['code'],
response_modes_supported: ['query'],
grant_types_supported: ['authorization_code', 'refresh_token', 'urn:ietf:params:oauth:grant-type:jwt-bearer'],
authorization_grant_profiles_supported: ['urn:ietf:params:oauth:grant-profile:id-jag'],
token_endpoint_auth_methods_supported: ['none', 'client_secret_post'],
code_challenge_methods_supported: ['S256'],
service_documentation: 'https://hanzo.ai/docs/model-context-protocol',
client_id_metadata_document_supported: true,
}
describe('handleMetadata', () => {
beforeEach(() => {
vi.resetModules()
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve(AUTHORITATIVE_METADATA),
})
)
})
afterEach(() => {
vi.restoreAllMocks()
})
it('returns valid OAuth authorization server metadata with endpoints rewritten', async () => {
const { handleMetadata } = await import('@/handlers/metadata')
const request = new Request('https://oauth.hanzo.ai/.well-known/oauth-authorization-server')
const response = await handleMetadata(request)
const data = (await response.json()) as Record<string, unknown>
expect(response.status).toBe(200)
expect(data.issuer).toBe('https://oauth.hanzo.ai')
expect(data.authorization_endpoint).toBe('https://oauth.hanzo.ai/oauth/authorize/')
expect(data.token_endpoint).toBe('https://oauth.hanzo.ai/oauth/token/')
expect(data.revocation_endpoint).toBe('https://oauth.hanzo.ai/oauth/revoke/')
expect(data.introspection_endpoint).toBe('https://oauth.hanzo.ai/oauth/introspect/')
expect(data.userinfo_endpoint).toBe('https://oauth.hanzo.ai/oauth/userinfo/')
expect(data.jwks_uri).toBe('https://oauth.hanzo.ai/.well-known/jwks.json')
expect(data.registration_endpoint).toBe('https://oauth.hanzo.ai/oauth/register/')
})
it('preserves non-endpoint fields from authoritative metadata', async () => {
const { handleMetadata } = await import('@/handlers/metadata')
const request = new Request('https://oauth.hanzo.ai/.well-known/oauth-authorization-server')
const response = await handleMetadata(request)
const data = (await response.json()) as Record<string, unknown>
expect(data.scopes_supported).toEqual(AUTHORITATIVE_METADATA.scopes_supported)
expect(data.response_types_supported).toEqual(AUTHORITATIVE_METADATA.response_types_supported)
expect(data.grant_types_supported).toEqual(AUTHORITATIVE_METADATA.grant_types_supported)
expect(data.authorization_grant_profiles_supported).toEqual(
AUTHORITATIVE_METADATA.authorization_grant_profiles_supported
)
expect(data.code_challenge_methods_supported).toContain('S256')
expect(data.service_documentation).toBe(AUTHORITATIVE_METADATA.service_documentation)
expect(data.client_id_metadata_document_supported).toBe(true)
})
it('caches the authoritative metadata', async () => {
const { handleMetadata } = await import('@/handlers/metadata')
const request = new Request('https://oauth.hanzo.ai/.well-known/oauth-authorization-server')
await handleMetadata(request)
await handleMetadata(request)
expect(fetch).toHaveBeenCalledTimes(1)
})
it('re-fetches after cache expires', async () => {
vi.useFakeTimers()
const { handleMetadata } = await import('@/handlers/metadata')
const request = new Request('https://oauth.hanzo.ai/.well-known/oauth-authorization-server')
await handleMetadata(request)
expect(fetch).toHaveBeenCalledTimes(1)
// Advance past the 10-minute cache TTL
vi.advanceTimersByTime(601 * 1000)
await handleMetadata(request)
expect(fetch).toHaveBeenCalledTimes(2)
vi.useRealTimers()
})
it('returns 502 when authoritative metadata fetch fails and no cache exists', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: false,
statusText: 'Internal Server Error',
})
)
const { handleMetadata } = await import('@/handlers/metadata')
const request = new Request('https://oauth.hanzo.ai/.well-known/oauth-authorization-server')
const response = await handleMetadata(request)
expect(response.status).toBe(502)
const data = (await response.json()) as Record<string, unknown>
expect(data.error).toBe('server_error')
})
it('returns 502 when metadata refresh fails after cache expires', async () => {
vi.useFakeTimers()
const { handleMetadata } = await import('@/handlers/metadata')
const request = new Request('https://oauth.hanzo.ai/.well-known/oauth-authorization-server')
// Populate cache with a successful fetch
const response1 = await handleMetadata(request)
expect(response1.status).toBe(200)
// Expire cache
vi.advanceTimersByTime(601 * 1000)
// Make fetch fail
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: false,
statusText: 'Service Unavailable',
})
)
const response2 = await handleMetadata(request)
expect(response2.status).toBe(502)
const data = (await response2.json()) as Record<string, unknown>
expect(data.error).toBe('server_error')
vi.useRealTimers()
})
})