From d985157d023ac23194a35fa9142479426ad0769b Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Fri, 29 May 2026 18:29:30 +0900 Subject: [PATCH] test(mcp-oauth): exercise callback server Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- .../mcp-oauth/callback-server.test.ts | 82 ++----------------- 1 file changed, 5 insertions(+), 77 deletions(-) diff --git a/src/features/mcp-oauth/callback-server.test.ts b/src/features/mcp-oauth/callback-server.test.ts index 063f2bdea..528d5bdb8 100644 --- a/src/features/mcp-oauth/callback-server.test.ts +++ b/src/features/mcp-oauth/callback-server.test.ts @@ -1,93 +1,21 @@ -import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test" +/// + +import { afterEach, describe, expect, it } from "bun:test" import { startCallbackServer, type CallbackServer } from "./callback-server" 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 -} describe("startCallbackServer", () => { let server: CallbackServer | null = null - let serveSpy: ReturnType | null = null - let activeServer: MockServerState | null = null async function request(url: string): Promise { - if (canBindRealSockets) { - return nativeFetch(url) - } - - if (!activeServer || activeServer.stopped) { - throw new Error("Connection refused") - } - - return await activeServer.fetch(new Request(url)) + return await Bun.fetch(url) } - beforeEach(() => { - if (canBindRealSockets) { - return - } - - activeServer = null - serveSpy = spyOn(Bun, "serve").mockImplementation((options: { - port: number - hostname?: string - fetch: (request: Request) => Response | Promise - }) => { - 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 - }) - }) - afterEach(async () => { server?.close() server = null - - if (serveSpy) { - serveSpy.mockRestore() - serveSpy = null - } - activeServer = null - - if (canBindRealSockets) { - await Bun.sleep(10) - } + await Bun.sleep(10) }) it("starts server and returns port", async () => {