feat(tstest): Implement dynamic port allocation for HTTP and WebSocket connections, add tests for port validation
This commit is contained in:
		| @@ -316,6 +316,32 @@ import '${absoluteTestFile.replace(/\\/g, '/')}'; | ||||
|     return tapParser; | ||||
|   } | ||||
|  | ||||
|   private async findFreePorts(): Promise<{ httpPort: number; wsPort: number }> { | ||||
|     const smartnetwork = new plugins.smartnetwork.SmartNetwork(); | ||||
|      | ||||
|     // Find HTTP port in range 30000-40000 | ||||
|     const httpPort = await smartnetwork.findFreePort(30000, 40000); | ||||
|     if (!httpPort) { | ||||
|       throw new Error('Could not find a free HTTP port in range 30000-40000'); | ||||
|     } | ||||
|      | ||||
|     // Find WebSocket port in range 30000-40000 (different from HTTP port) | ||||
|     let wsPort = await smartnetwork.findFreePort(httpPort + 1, 40000); | ||||
|     if (!wsPort) { | ||||
|       // Try again from the beginning of the range if we couldn't find one after httpPort | ||||
|       wsPort = await smartnetwork.findFreePort(30000, httpPort - 1); | ||||
|       if (!wsPort) { | ||||
|         throw new Error('Could not find a free WebSocket port in range 30000-40000'); | ||||
|       } | ||||
|     } | ||||
|      | ||||
|     // Log selected ports for debugging | ||||
|     if (!this.logger.options.quiet) { | ||||
|       console.log(`Selected ports - HTTP: ${httpPort}, WebSocket: ${wsPort}`); | ||||
|     } | ||||
|     return { httpPort, wsPort }; | ||||
|   } | ||||
|  | ||||
|   public async runInChrome(fileNameArg: string, index: number, total: number): Promise<TapParser> { | ||||
|     this.logger.testFileStart(fileNameArg, 'chromium', index, total); | ||||
|  | ||||
| @@ -330,10 +356,13 @@ import '${absoluteTestFile.replace(/\\/g, '/')}'; | ||||
|       bundler: 'esbuild', | ||||
|     }); | ||||
|  | ||||
|     // Find free ports for HTTP and WebSocket | ||||
|     const { httpPort, wsPort } = await this.findFreePorts(); | ||||
|  | ||||
|     // lets create a server | ||||
|     const server = new plugins.typedserver.servertools.Server({ | ||||
|       cors: true, | ||||
|       port: 3007, | ||||
|       port: httpPort, | ||||
|     }); | ||||
|     server.addRoute( | ||||
|       '/test', | ||||
| @@ -344,6 +373,7 @@ import '${absoluteTestFile.replace(/\\/g, '/')}'; | ||||
|           <head> | ||||
|             <script> | ||||
|               globalThis.testdom = true; | ||||
|               globalThis.wsPort = ${wsPort}; | ||||
|             </script> | ||||
|           </head> | ||||
|           <body></body> | ||||
| @@ -357,7 +387,7 @@ import '${absoluteTestFile.replace(/\\/g, '/')}'; | ||||
|  | ||||
|     // lets handle realtime comms | ||||
|     const tapParser = new TapParser(fileNameArg + ':chrome', this.logger); | ||||
|     const wss = new plugins.ws.WebSocketServer({ port: 8080 }); | ||||
|     const wss = new plugins.ws.WebSocketServer({ port: wsPort }); | ||||
|     wss.on('connection', (ws) => { | ||||
|       ws.on('message', (message) => { | ||||
|         const messageStr = message.toString(); | ||||
| @@ -374,10 +404,10 @@ import '${absoluteTestFile.replace(/\\/g, '/')}'; | ||||
|     await this.smartbrowserInstance.start(); | ||||
|      | ||||
|     const evaluatePromise = this.smartbrowserInstance.evaluateOnPage( | ||||
|       `http://localhost:3007/test?bundleName=${bundleFileName}`, | ||||
|       `http://localhost:${httpPort}/test?bundleName=${bundleFileName}`, | ||||
|       async () => { | ||||
|         // lets enable real time comms | ||||
|         const ws = new WebSocket('ws://localhost:8080'); | ||||
|         const ws = new WebSocket(`ws://localhost:${globalThis.wsPort}`); | ||||
|         await new Promise((resolve) => (ws.onopen = resolve)); | ||||
|  | ||||
|         // Ensure this function is declared with 'async' | ||||
|   | ||||
| @@ -17,6 +17,7 @@ import * as smartchok from '@push.rocks/smartchok'; | ||||
| import * as smartdelay from '@push.rocks/smartdelay'; | ||||
| import * as smartfile from '@push.rocks/smartfile'; | ||||
| import * as smartlog from '@push.rocks/smartlog'; | ||||
| import * as smartnetwork from '@push.rocks/smartnetwork'; | ||||
| import * as smartpromise from '@push.rocks/smartpromise'; | ||||
| import * as smartshell from '@push.rocks/smartshell'; | ||||
| import * as tapbundle from '../dist_ts_tapbundle/index.js'; | ||||
| @@ -28,6 +29,7 @@ export { | ||||
|   smartdelay, | ||||
|   smartfile, | ||||
|   smartlog, | ||||
|   smartnetwork, | ||||
|   smartpromise, | ||||
|   smartshell, | ||||
|   tapbundle, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user