fix(server): avoid fixed test server ports

Use OS-assigned ports for server integration tests and publish the actual Bun server port after startup, preventing local port collisions from failing server checks.

Tested: bun test src/server/__tests__/e2e/business-flow.test.ts
Tested: bun test src/server/__tests__/e2e/full-flow.test.ts src/server/__tests__/conversations.test.ts src/server/__tests__/tasks.test.ts src/server/__tests__/haha-openai-oauth-api.test.ts
Tested: bun run check:server
Confidence: high
Scope-risk: narrow
This commit is contained in:
程序员阿江(Relakkes) 2026-06-03 19:22:05 +08:00
parent f567236330
commit e8bc9e9342
6 changed files with 18 additions and 21 deletions

View File

@ -966,11 +966,10 @@ describe('WebSocket Chat Integration', () => {
)
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
const port = 15000 + Math.floor(Math.random() * 1000)
const { startServer } = await import('../index.js')
server = startServer(port, '127.0.0.1')
baseUrl = `http://127.0.0.1:${port}`
wsUrl = `ws://127.0.0.1:${port}`
server = startServer(0, '127.0.0.1')
baseUrl = `http://127.0.0.1:${server.port}`
wsUrl = `ws://127.0.0.1:${server.port}`
})
afterAll(async () => {

View File

@ -51,10 +51,9 @@ async function startTestServer() {
await fs.mkdir(path.join(tmpDir, 'agents'), { recursive: true })
const { startServer } = await import('../../index.js')
const port = 14000 + Math.floor(Math.random() * 1000)
server = startServer(port, '127.0.0.1')
baseUrl = `http://127.0.0.1:${port}`
wsUrl = `ws://127.0.0.1:${port}`
server = startServer(0, '127.0.0.1')
baseUrl = `http://127.0.0.1:${server.port}`
wsUrl = `ws://127.0.0.1:${server.port}`
}
async function api(method: string, urlPath: string, body?: unknown) {

View File

@ -51,9 +51,8 @@ async function startTestServer() {
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
const { startServer } = await import('../../index.js')
const port = 13456 + Math.floor(Math.random() * 1000)
server = startServer(port, '127.0.0.1')
baseUrl = `http://127.0.0.1:${port}`
server = startServer(0, '127.0.0.1')
baseUrl = `http://127.0.0.1:${server.port}`
}
async function api(method: string, path: string, body?: unknown): Promise<{ status: number; data: any }> {

View File

@ -191,11 +191,10 @@ describe('GET /auth/callback', () => {
afterEach(teardown)
test('routes the OpenAI Codex redirect path to the desktop callback page', async () => {
const port = await getFreePort()
const originalServerPort = ProviderService.getServerPort()
const server = startServer(port, '127.0.0.1')
const server = startServer(0, '127.0.0.1')
try {
const res = await fetch(`http://127.0.0.1:${port}/auth/callback`)
const res = await fetch(`http://127.0.0.1:${server.port}/auth/callback`)
expect(res.status).toBe(200)
const html = await res.text()
expect(html).toContain('OpenAI Login Failed')

View File

@ -129,10 +129,9 @@ describe('Tasks API', () => {
process.env.CLAUDE_CONFIG_DIR = tmpDir
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
const port = 15500 + Math.floor(Math.random() * 500)
const { startServer } = await import('../../server/index.js')
server = startServer(port, '127.0.0.1')
baseUrl = `http://127.0.0.1:${port}`
server = startServer(0, '127.0.0.1')
baseUrl = `http://127.0.0.1:${server.port}`
})
afterEach(async () => {

View File

@ -127,7 +127,7 @@ export function startServer(port = PORT, host = HOST) {
enableConfigs()
diagnosticsService.installConsoleCapture()
diagnosticsService.installProcessCapture()
ProviderService.setServerPort(port)
let serverPort = port
const localConnectHost =
host === '0.0.0.0' || host === '127.0.0.1' || host === 'localhost'
? '127.0.0.1'
@ -225,7 +225,7 @@ export function startServer(port = PORT, host = HOST) {
connectedAt: Date.now(),
channel: 'client',
sdkToken: null,
serverPort: port,
serverPort,
serverHost: localConnectHost,
},
})
@ -260,7 +260,7 @@ export function startServer(port = PORT, host = HOST) {
connectedAt: Date.now(),
channel: 'sdk',
sdkToken: url.searchParams.get('token'),
serverPort: port,
serverPort,
serverHost: localConnectHost,
},
})
@ -428,6 +428,8 @@ export function startServer(port = PORT, host = HOST) {
websocket: handleWebSocket,
})
serverPort = server.port
ProviderService.setServerPort(serverPort)
} catch (error) {
const message = error instanceof Error && error.message
? error.message
@ -448,7 +450,7 @@ export function startServer(port = PORT, host = HOST) {
)
})
console.log(`[Server] Claude Code API server running at http://${host}:${port}`)
console.log(`[Server] Claude Code API server running at http://${host}:${serverPort}`)
return server
}