fix: use dynamic port in server scenario tests to avoid conflicts (#221)
Co-authored-by: Paul Carleton <paulcarletonjr@gmail.com>
This commit is contained in:
parent
8c828a7fab
commit
1d6cda37b8
1 changed files with 18 additions and 4 deletions
|
|
@ -1,14 +1,28 @@
|
||||||
import { spawn, ChildProcess } from 'child_process';
|
import { spawn, ChildProcess } from 'child_process';
|
||||||
|
import { createServer } from 'net';
|
||||||
import { getClientScenario, listActiveClientScenarios } from '../index';
|
import { getClientScenario, listActiveClientScenarios } from '../index';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
|
function getFreePort(): Promise<number> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const server = createServer();
|
||||||
|
server.listen(0, () => {
|
||||||
|
const port = (server.address() as { port: number }).port;
|
||||||
|
server.close(() => resolve(port));
|
||||||
|
});
|
||||||
|
server.on('error', reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe('Server Scenarios', () => {
|
describe('Server Scenarios', () => {
|
||||||
let serverProcess: ChildProcess | null = null;
|
let serverProcess: ChildProcess | null = null;
|
||||||
const TEST_PORT = 3001;
|
let serverUrl: string;
|
||||||
const SERVER_URL = `http://localhost:${TEST_PORT}/mcp`;
|
|
||||||
const SERVER_STARTUP_TIMEOUT = 30000; // 30 seconds for CI
|
const SERVER_STARTUP_TIMEOUT = 30000; // 30 seconds for CI
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
const testPort = await getFreePort();
|
||||||
|
serverUrl = `http://localhost:${testPort}/mcp`;
|
||||||
|
|
||||||
// Start the everything-server once for all scenarios in this file
|
// Start the everything-server once for all scenarios in this file
|
||||||
const serverPath = path.join(
|
const serverPath = path.join(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
|
|
@ -18,7 +32,7 @@ describe('Server Scenarios', () => {
|
||||||
// Use shell: true on Windows only (npx is npx.cmd on Windows)
|
// Use shell: true on Windows only (npx is npx.cmd on Windows)
|
||||||
const isWindows = process.platform === 'win32';
|
const isWindows = process.platform === 'win32';
|
||||||
serverProcess = spawn('npx', ['tsx', serverPath], {
|
serverProcess = spawn('npx', ['tsx', serverPath], {
|
||||||
env: { ...process.env, PORT: TEST_PORT.toString() },
|
env: { ...process.env, PORT: testPort.toString() },
|
||||||
stdio: ['ignore', 'pipe', 'pipe'],
|
stdio: ['ignore', 'pipe', 'pipe'],
|
||||||
detached: false,
|
detached: false,
|
||||||
shell: isWindows
|
shell: isWindows
|
||||||
|
|
@ -114,7 +128,7 @@ describe('Server Scenarios', () => {
|
||||||
throw new Error(`Scenario ${scenarioName} not found`);
|
throw new Error(`Scenario ${scenarioName} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const checks = await scenario.run(SERVER_URL);
|
const checks = await scenario.run(serverUrl);
|
||||||
|
|
||||||
// Verify checks were returned
|
// Verify checks were returned
|
||||||
expect(checks.length).toBeGreaterThan(0);
|
expect(checks.length).toBeGreaterThan(0);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue