Skip to content

Commit 6a7c378

Browse files
committed
refactor(test): simplify test server startup using PTYServer
Remove custom port finding and process killing logic. Use PTYServer.createServer() for cleaner server initialization and better CI reliability.
1 parent 53e8517 commit 6a7c378

1 file changed

Lines changed: 7 additions & 54 deletions

File tree

test/start-server.ts

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { initManager, manager } from 'opencode-pty/src/plugin/pty/manager'
2-
import { startWebServer } from 'opencode-pty/src/web/server/server'
2+
import { PTYServer } from '../src/web/server/server'
33

44
// Set NODE_ENV if not set
55
if (!process.env.NODE_ENV) {
@@ -13,72 +13,25 @@ const fakeClient = {
1313
} as any
1414
initManager(fakeClient)
1515

16-
// Cleanup on process termination
17-
process.on('SIGTERM', () => {
18-
manager.cleanupAll()
19-
process.exit(0)
20-
})
21-
22-
process.on('SIGINT', () => {
23-
manager.cleanupAll()
24-
process.exit(0)
25-
})
26-
27-
// Use the specified port after cleanup
28-
function findAvailablePort(port: number): number {
29-
// Only kill processes if we're confident they belong to our test servers
30-
// In parallel execution, avoid killing other workers' servers
31-
if (process.env.TEST_WORKER_INDEX) {
32-
// For parallel workers, assume the port is available since we assign unique ports
33-
return port
34-
}
35-
36-
// For single execution, clean up any stale processes
37-
Bun.spawnSync(['sh', '-c', `lsof -ti:${port} | xargs kill -9 2>/dev/null || true`])
38-
// Small delay to allow cleanup
39-
Bun.sleepSync(200)
40-
return port
41-
}
42-
43-
// Parse command line arguments
44-
import yargs from 'yargs'
45-
import { hideBin } from 'yargs/helpers'
46-
47-
const argv = yargs(hideBin(process.argv))
48-
.option('port', {
49-
alias: 'p',
50-
type: 'number',
51-
description: 'Port to run the server on',
52-
default: 8877,
53-
})
54-
.parseSync()
55-
56-
let basePort = argv.port
57-
58-
// For parallel workers, ensure unique start ports
59-
if (process.env.TEST_WORKER_INDEX) {
60-
const workerIndex = parseInt(process.env.TEST_WORKER_INDEX, 10)
61-
basePort = 8877 + workerIndex
62-
}
63-
64-
let port = findAvailablePort(basePort)
65-
66-
await startWebServer({ port })
16+
const server = await PTYServer.createServer()
6717

6818
// Only log in non-test environments or when explicitly requested
6919

7020
// Write port to file for tests to read
7121
if (process.env.NODE_ENV === 'test') {
7222
const workerIndex = process.env.TEST_WORKER_INDEX || '0'
73-
await Bun.write(`/tmp/test-server-port-${workerIndex}.txt`, port.toString())
23+
if (!server.server.port) {
24+
throw new Error('Unix sockets not supported. File an issue if you need this feature.')
25+
}
26+
await Bun.write(`/tmp/test-server-port-${workerIndex}.txt`, server.server.port.toString())
7427
}
7528

7629
// Health check for test mode
7730
if (process.env.NODE_ENV === 'test') {
7831
let retries = 20 // 10 seconds
7932
while (retries > 0) {
8033
try {
81-
const response = await fetch(`http://localhost:${port}/api/sessions`)
34+
const response = await fetch(`${server.server.url}/api/sessions`)
8235
if (response.ok) {
8336
break
8437
}

0 commit comments

Comments
 (0)