import { test, expect } from "bun:test"
import { Hype } from "../index.tsx"

test("defaults returns next available port in dev mode", () => {
  // Start a server on port 4000 to block it
  const blocker = Bun.serve({
    port: 4000,
    fetch: () => new Response("blocker"),
  })

  // Set NODE_ENV to development and PORT to 4000
  const originalNodeEnv = process.env.NODE_ENV
  const originalPort = process.env.PORT
  process.env.NODE_ENV = "development"
  process.env.PORT = "4000"

  const app = new Hype({})
  const config = app.defaults

  // Should have found port 4001 since 4000 is taken
  expect(config.port).toBe(4001)

  // Clean up
  blocker.stop(true)
  process.env.NODE_ENV = originalNodeEnv
  process.env.PORT = originalPort
})

test("defaults uses exact port in production mode", () => {
  const originalNodeEnv = process.env.NODE_ENV
  const originalPort = process.env.PORT
  process.env.NODE_ENV = "production"
  process.env.PORT = "4002"

  const app = new Hype({})
  const config = app.defaults

  // Should use exact port in production
  expect(config.port).toBe(4002)

  // Clean up
  process.env.NODE_ENV = originalNodeEnv
  process.env.PORT = originalPort
})
