MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / createFakeServerFactory

Function createFakeServerFactory

tests/e2e/helpers/app-launcher.test.js:9–43  ·  view source on GitHub ↗
(plan)

Source from the content-addressed store, hash-verified

7const { launchApp, __test__ } = require('./app-launcher')
8
9function createFakeServerFactory(plan) {
10 let index = 0
11 const state = {
12 listenPorts: [],
13 closeCount: 0,
14 }
15
16 function createServer() {
17 const behavior = plan[index++] || { type: 'success' }
18 const handlers = {}
19
20 return {
21 on(event, handler) {
22 handlers[event] = handler
23 },
24 listen(port, callback) {
25 state.listenPorts.push(port)
26 setImmediate(() => {
27 if (behavior.type === 'success') {
28 callback()
29 return
30 }
31 if (handlers.error) {
32 handlers.error({ code: behavior.code || 'EADDRINUSE' })
33 }
34 })
35 },
36 close() {
37 state.closeCount += 1
38 },
39 }
40 }
41
42 return { createServer, state }
43}
44
45test('findAvailablePortWithReservation 会重试并返回可用端口', async () => {
46 const { createServer, state } = createFakeServerFactory([{ type: 'error', code: 'EADDRINUSE' }, { type: 'success' }])

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected