2026-01-29 19:48:36 +09:00
|
|
|
import { afterEach, describe, expect, it } from "bun:test"
|
|
|
|
|
import { findAvailablePort, startCallbackServer, type CallbackServer } from "./callback-server"
|
|
|
|
|
|
2026-01-30 22:10:52 +09:00
|
|
|
const nativeFetch = Bun.fetch.bind(Bun)
|
|
|
|
|
|
2026-01-29 19:48:36 +09:00
|
|
|
describe("findAvailablePort", () => {
|
|
|
|
|
it("returns the start port when it is available", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const startPort = 19877
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const port = await findAvailablePort(startPort)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(port).toBeGreaterThanOrEqual(startPort)
|
|
|
|
|
expect(port).toBeLessThan(startPort + 20)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("skips busy ports and returns next available", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const blocker = Bun.serve({
|
|
|
|
|
port: 19877,
|
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
|
fetch: () => new Response(),
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const port = await findAvailablePort(19877)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(port).toBeGreaterThan(19877)
|
|
|
|
|
blocker.stop(true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("startCallbackServer", () => {
|
|
|
|
|
let server: CallbackServer | null = null
|
|
|
|
|
|
2026-01-30 22:10:52 +09:00
|
|
|
afterEach(async () => {
|
2026-01-29 19:48:36 +09:00
|
|
|
server?.close()
|
|
|
|
|
server = null
|
2026-01-30 22:10:52 +09:00
|
|
|
// Allow time for port to be released before next test
|
|
|
|
|
await Bun.sleep(10)
|
2026-01-29 19:48:36 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("starts server and returns port", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given - no preconditions
|
2026-01-29 19:48:36 +09:00
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
server = await startCallbackServer()
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(server.port).toBeGreaterThanOrEqual(19877)
|
|
|
|
|
expect(typeof server.waitForCallback).toBe("function")
|
|
|
|
|
expect(typeof server.close).toBe("function")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("resolves callback with code and state from query params", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
server = await startCallbackServer()
|
|
|
|
|
const callbackUrl = `http://127.0.0.1:${server.port}/oauth/callback?code=test-code&state=test-state`
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-30 22:10:52 +09:00
|
|
|
// Use Promise.all to ensure fetch and waitForCallback run concurrently
|
|
|
|
|
// This prevents race condition where waitForCallback blocks before fetch starts
|
|
|
|
|
const [result, response] = await Promise.all([
|
|
|
|
|
server.waitForCallback(),
|
|
|
|
|
nativeFetch(callbackUrl)
|
|
|
|
|
])
|
2026-01-29 19:48:36 +09:00
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toEqual({ code: "test-code", state: "test-state" })
|
|
|
|
|
expect(response.status).toBe(200)
|
|
|
|
|
const html = await response.text()
|
|
|
|
|
expect(html).toContain("Authorization successful")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns 404 for non-callback routes", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
server = await startCallbackServer()
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-30 22:10:52 +09:00
|
|
|
const response = await nativeFetch(`http://127.0.0.1:${server.port}/other`)
|
2026-01-29 19:48:36 +09:00
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(response.status).toBe(404)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns 400 and rejects when code is missing", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
server = await startCallbackServer()
|
|
|
|
|
const callbackRejection = server.waitForCallback().catch((e: Error) => e)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-30 22:10:52 +09:00
|
|
|
const response = await nativeFetch(`http://127.0.0.1:${server.port}/oauth/callback?state=s`)
|
2026-01-29 19:48:36 +09:00
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(response.status).toBe(400)
|
|
|
|
|
const error = await callbackRejection
|
|
|
|
|
expect(error).toBeInstanceOf(Error)
|
|
|
|
|
expect((error as Error).message).toContain("missing code or state")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns 400 and rejects when state is missing", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
server = await startCallbackServer()
|
|
|
|
|
const callbackRejection = server.waitForCallback().catch((e: Error) => e)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-30 22:10:52 +09:00
|
|
|
const response = await nativeFetch(`http://127.0.0.1:${server.port}/oauth/callback?code=c`)
|
2026-01-29 19:48:36 +09:00
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(response.status).toBe(400)
|
|
|
|
|
const error = await callbackRejection
|
|
|
|
|
expect(error).toBeInstanceOf(Error)
|
|
|
|
|
expect((error as Error).message).toContain("missing code or state")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("close stops the server immediately", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
server = await startCallbackServer()
|
|
|
|
|
const port = server.port
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
server.close()
|
|
|
|
|
server = null
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
try {
|
2026-01-30 22:10:52 +09:00
|
|
|
await nativeFetch(`http://127.0.0.1:${port}/oauth/callback?code=c&state=s`)
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(true).toBe(false)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
expect(error).toBeDefined()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|