opencode/packages/core/test/mcp-oauth.test.ts

35 lines
1.3 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { refreshAuthorization } from "@modelcontextprotocol/sdk/client/auth.js"
describe("MCP OAuth", () => {
test("shares concurrent refreshes for the same token", async () => {
let requests = 0
const pending = Promise.withResolvers<void>()
const options = {
metadata: {
issuer: "https://auth.example.com",
authorization_endpoint: "https://auth.example.com/authorize",
token_endpoint: "https://auth.example.com/token",
response_types_supported: ["code"],
},
clientInformation: { client_id: "client" },
refreshToken: "refresh",
fetchFn: async () => {
requests++
await pending.promise
return Response.json({ access_token: "access", token_type: "Bearer", refresh_token: "next" })
},
}
const first = refreshAuthorization(new URL("https://auth.example.com"), options)
const second = refreshAuthorization(new URL("https://auth.example.com"), options)
await Promise.resolve()
expect(requests).toBe(1)
pending.resolve()
expect(await Promise.all([first, second])).toEqual([
{ access_token: "access", token_type: "Bearer", refresh_token: "next" },
{ access_token: "access", token_type: "Bearer", refresh_token: "next" },
])
})
})