mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-08-01 16:43:37 +08:00
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:
parent
f567236330
commit
e8bc9e9342
@ -966,11 +966,10 @@ describe('WebSocket Chat Integration', () => {
|
|||||||
)
|
)
|
||||||
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
|
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
|
||||||
|
|
||||||
const port = 15000 + Math.floor(Math.random() * 1000)
|
|
||||||
const { startServer } = await import('../index.js')
|
const { startServer } = await import('../index.js')
|
||||||
server = startServer(port, '127.0.0.1')
|
server = startServer(0, '127.0.0.1')
|
||||||
baseUrl = `http://127.0.0.1:${port}`
|
baseUrl = `http://127.0.0.1:${server.port}`
|
||||||
wsUrl = `ws://127.0.0.1:${port}`
|
wsUrl = `ws://127.0.0.1:${server.port}`
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
|
|||||||
@ -51,10 +51,9 @@ async function startTestServer() {
|
|||||||
await fs.mkdir(path.join(tmpDir, 'agents'), { recursive: true })
|
await fs.mkdir(path.join(tmpDir, 'agents'), { recursive: true })
|
||||||
|
|
||||||
const { startServer } = await import('../../index.js')
|
const { startServer } = await import('../../index.js')
|
||||||
const port = 14000 + Math.floor(Math.random() * 1000)
|
server = startServer(0, '127.0.0.1')
|
||||||
server = startServer(port, '127.0.0.1')
|
baseUrl = `http://127.0.0.1:${server.port}`
|
||||||
baseUrl = `http://127.0.0.1:${port}`
|
wsUrl = `ws://127.0.0.1:${server.port}`
|
||||||
wsUrl = `ws://127.0.0.1:${port}`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function api(method: string, urlPath: string, body?: unknown) {
|
async function api(method: string, urlPath: string, body?: unknown) {
|
||||||
|
|||||||
@ -51,9 +51,8 @@ async function startTestServer() {
|
|||||||
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
|
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
|
||||||
|
|
||||||
const { startServer } = await import('../../index.js')
|
const { startServer } = await import('../../index.js')
|
||||||
const port = 13456 + Math.floor(Math.random() * 1000)
|
server = startServer(0, '127.0.0.1')
|
||||||
server = startServer(port, '127.0.0.1')
|
baseUrl = `http://127.0.0.1:${server.port}`
|
||||||
baseUrl = `http://127.0.0.1:${port}`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function api(method: string, path: string, body?: unknown): Promise<{ status: number; data: any }> {
|
async function api(method: string, path: string, body?: unknown): Promise<{ status: number; data: any }> {
|
||||||
|
|||||||
@ -191,11 +191,10 @@ describe('GET /auth/callback', () => {
|
|||||||
afterEach(teardown)
|
afterEach(teardown)
|
||||||
|
|
||||||
test('routes the OpenAI Codex redirect path to the desktop callback page', async () => {
|
test('routes the OpenAI Codex redirect path to the desktop callback page', async () => {
|
||||||
const port = await getFreePort()
|
|
||||||
const originalServerPort = ProviderService.getServerPort()
|
const originalServerPort = ProviderService.getServerPort()
|
||||||
const server = startServer(port, '127.0.0.1')
|
const server = startServer(0, '127.0.0.1')
|
||||||
try {
|
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)
|
expect(res.status).toBe(200)
|
||||||
const html = await res.text()
|
const html = await res.text()
|
||||||
expect(html).toContain('OpenAI Login Failed')
|
expect(html).toContain('OpenAI Login Failed')
|
||||||
|
|||||||
@ -129,10 +129,9 @@ describe('Tasks API', () => {
|
|||||||
process.env.CLAUDE_CONFIG_DIR = tmpDir
|
process.env.CLAUDE_CONFIG_DIR = tmpDir
|
||||||
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
|
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
|
||||||
|
|
||||||
const port = 15500 + Math.floor(Math.random() * 500)
|
|
||||||
const { startServer } = await import('../../server/index.js')
|
const { startServer } = await import('../../server/index.js')
|
||||||
server = startServer(port, '127.0.0.1')
|
server = startServer(0, '127.0.0.1')
|
||||||
baseUrl = `http://127.0.0.1:${port}`
|
baseUrl = `http://127.0.0.1:${server.port}`
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
|
|||||||
@ -127,7 +127,7 @@ export function startServer(port = PORT, host = HOST) {
|
|||||||
enableConfigs()
|
enableConfigs()
|
||||||
diagnosticsService.installConsoleCapture()
|
diagnosticsService.installConsoleCapture()
|
||||||
diagnosticsService.installProcessCapture()
|
diagnosticsService.installProcessCapture()
|
||||||
ProviderService.setServerPort(port)
|
let serverPort = port
|
||||||
const localConnectHost =
|
const localConnectHost =
|
||||||
host === '0.0.0.0' || host === '127.0.0.1' || host === 'localhost'
|
host === '0.0.0.0' || host === '127.0.0.1' || host === 'localhost'
|
||||||
? '127.0.0.1'
|
? '127.0.0.1'
|
||||||
@ -225,7 +225,7 @@ export function startServer(port = PORT, host = HOST) {
|
|||||||
connectedAt: Date.now(),
|
connectedAt: Date.now(),
|
||||||
channel: 'client',
|
channel: 'client',
|
||||||
sdkToken: null,
|
sdkToken: null,
|
||||||
serverPort: port,
|
serverPort,
|
||||||
serverHost: localConnectHost,
|
serverHost: localConnectHost,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -260,7 +260,7 @@ export function startServer(port = PORT, host = HOST) {
|
|||||||
connectedAt: Date.now(),
|
connectedAt: Date.now(),
|
||||||
channel: 'sdk',
|
channel: 'sdk',
|
||||||
sdkToken: url.searchParams.get('token'),
|
sdkToken: url.searchParams.get('token'),
|
||||||
serverPort: port,
|
serverPort,
|
||||||
serverHost: localConnectHost,
|
serverHost: localConnectHost,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -428,6 +428,8 @@ export function startServer(port = PORT, host = HOST) {
|
|||||||
|
|
||||||
websocket: handleWebSocket,
|
websocket: handleWebSocket,
|
||||||
})
|
})
|
||||||
|
serverPort = server.port
|
||||||
|
ProviderService.setServerPort(serverPort)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error && error.message
|
const message = error instanceof Error && error.message
|
||||||
? 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
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user