test(mcp-oauth): exercise callback server
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,93 +1,21 @@
|
|||||||
import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test"
|
/// <reference types="bun-types" />
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, it } from "bun:test"
|
||||||
import { startCallbackServer, type CallbackServer } from "./callback-server"
|
import { startCallbackServer, type CallbackServer } from "./callback-server"
|
||||||
|
|
||||||
const HOSTNAME = "127.0.0.1"
|
const HOSTNAME = "127.0.0.1"
|
||||||
const nativeFetch = Bun.fetch.bind(Bun)
|
|
||||||
|
|
||||||
function supportsRealSocketBinding(): boolean {
|
|
||||||
try {
|
|
||||||
const server = Bun.serve({
|
|
||||||
port: 0,
|
|
||||||
hostname: HOSTNAME,
|
|
||||||
fetch: () => new Response("probe"),
|
|
||||||
})
|
|
||||||
server.stop(true)
|
|
||||||
return true
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const canBindRealSockets = supportsRealSocketBinding()
|
|
||||||
|
|
||||||
type MockServerState = {
|
|
||||||
port: number
|
|
||||||
stopped: boolean
|
|
||||||
fetch: (request: Request) => Response | Promise<Response>
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("startCallbackServer", () => {
|
describe("startCallbackServer", () => {
|
||||||
let server: CallbackServer | null = null
|
let server: CallbackServer | null = null
|
||||||
let serveSpy: ReturnType<typeof spyOn> | null = null
|
|
||||||
let activeServer: MockServerState | null = null
|
|
||||||
|
|
||||||
async function request(url: string): Promise<Response> {
|
async function request(url: string): Promise<Response> {
|
||||||
if (canBindRealSockets) {
|
return await Bun.fetch(url)
|
||||||
return nativeFetch(url)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!activeServer || activeServer.stopped) {
|
|
||||||
throw new Error("Connection refused")
|
|
||||||
}
|
|
||||||
|
|
||||||
return await activeServer.fetch(new Request(url))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
if (canBindRealSockets) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
activeServer = null
|
|
||||||
serveSpy = spyOn(Bun, "serve").mockImplementation((options: {
|
|
||||||
port: number
|
|
||||||
hostname?: string
|
|
||||||
fetch: (request: Request) => Response | Promise<Response>
|
|
||||||
}) => {
|
|
||||||
const state: MockServerState = {
|
|
||||||
port: options.port === 0 ? 19877 : options.port,
|
|
||||||
stopped: false,
|
|
||||||
fetch: options.fetch,
|
|
||||||
}
|
|
||||||
|
|
||||||
const handle = {
|
|
||||||
port: state.port,
|
|
||||||
stop: (_force?: boolean) => {
|
|
||||||
state.stopped = true
|
|
||||||
if (activeServer === state) {
|
|
||||||
activeServer = null
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
activeServer = state
|
|
||||||
return handle as ReturnType<typeof Bun.serve>
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
server?.close()
|
server?.close()
|
||||||
server = null
|
server = null
|
||||||
|
await Bun.sleep(10)
|
||||||
if (serveSpy) {
|
|
||||||
serveSpy.mockRestore()
|
|
||||||
serveSpy = null
|
|
||||||
}
|
|
||||||
activeServer = null
|
|
||||||
|
|
||||||
if (canBindRealSockets) {
|
|
||||||
await Bun.sleep(10)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("starts server and returns port", async () => {
|
it("starts server and returns port", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user