Skip to content

Commit f5abe19

Browse files
committed
refactor: update mcpServerPort initialization and improve server start logic
1 parent 43fab81 commit f5abe19

File tree

22 files changed

+484
-176
lines changed

22 files changed

+484
-176
lines changed

exercises/01.simple/01.problem.raw-html/test/globalSetup.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ declare module 'vitest' {
1111
type OutputBuffer = Array<{ channel: 'stdout' | 'stderr'; data: Buffer }>
1212

1313
export default async function setup(project: TestProject) {
14-
const mcpServerPort = await getPort()
14+
const mcpServerPort = process.env.PORT
15+
? Number(process.env.PORT)
16+
: await getPort()
1517

1618
project.provide('mcpServerPort', mcpServerPort)
1719

@@ -122,22 +124,34 @@ export default async function setup(project: TestProject) {
122124
})
123125
}
124126

125-
async function startServers() {
126-
console.log('Starting servers...')
127-
128-
// Start app server if necessary
129-
await startAppServerIfNecessary()
127+
async function startMcpServerIfNecessary() {
128+
const isMcpRunning = await fetch(
129+
`http://localhost:${mcpServerPort}/healthcheck`,
130+
).catch(() => ({ ok: false }))
131+
if (isMcpRunning.ok) {
132+
return
133+
}
130134

131-
// Start the MCP server from the exercise directory
135+
// Start the MCP server if necessary
132136
console.log(`Starting MCP server on port ${mcpServerPort}...`)
133-
mcpServerProcess = execa('npm', ['run', 'dev:server', '--silent'], {
137+
mcpServerProcess = execa('npm', ['run', 'dev:server'], {
134138
cwd: process.cwd(),
135139
stdio: ['ignore', 'pipe', 'pipe'],
136140
env: {
137141
...process.env,
138142
PORT: mcpServerPort.toString(),
139143
},
140144
})
145+
}
146+
147+
async function startServers() {
148+
console.log('Starting servers...')
149+
150+
// Start app server if necessary
151+
await startAppServerIfNecessary()
152+
153+
// Start the MCP server from the exercise directory
154+
await startMcpServerIfNecessary()
141155

142156
try {
143157
// Wait for both servers to be ready simultaneously

exercises/01.simple/01.solution.raw-html/test/globalSetup.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ declare module 'vitest' {
1111
type OutputBuffer = Array<{ channel: 'stdout' | 'stderr'; data: Buffer }>
1212

1313
export default async function setup(project: TestProject) {
14-
const mcpServerPort = await getPort()
14+
const mcpServerPort = process.env.PORT
15+
? Number(process.env.PORT)
16+
: await getPort()
1517

1618
project.provide('mcpServerPort', mcpServerPort)
1719

@@ -122,22 +124,34 @@ export default async function setup(project: TestProject) {
122124
})
123125
}
124126

125-
async function startServers() {
126-
console.log('Starting servers...')
127-
128-
// Start app server if necessary
129-
await startAppServerIfNecessary()
127+
async function startMcpServerIfNecessary() {
128+
const isMcpRunning = await fetch(
129+
`http://localhost:${mcpServerPort}/healthcheck`,
130+
).catch(() => ({ ok: false }))
131+
if (isMcpRunning.ok) {
132+
return
133+
}
130134

131-
// Start the MCP server from the exercise directory
135+
// Start the MCP server if necessary
132136
console.log(`Starting MCP server on port ${mcpServerPort}...`)
133-
mcpServerProcess = execa('npm', ['run', 'dev:server', '--silent'], {
137+
mcpServerProcess = execa('npm', ['run', 'dev:server'], {
134138
cwd: process.cwd(),
135139
stdio: ['ignore', 'pipe', 'pipe'],
136140
env: {
137141
...process.env,
138142
PORT: mcpServerPort.toString(),
139143
},
140144
})
145+
}
146+
147+
async function startServers() {
148+
console.log('Starting servers...')
149+
150+
// Start app server if necessary
151+
await startAppServerIfNecessary()
152+
153+
// Start the MCP server from the exercise directory
154+
await startMcpServerIfNecessary()
141155

142156
try {
143157
// Wait for both servers to be ready simultaneously

exercises/02.consistent/01.problem.remote-dom/test/globalSetup.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ declare module 'vitest' {
1111
type OutputBuffer = Array<{ channel: 'stdout' | 'stderr'; data: Buffer }>
1212

1313
export default async function setup(project: TestProject) {
14-
const mcpServerPort = await getPort()
14+
const mcpServerPort = process.env.PORT
15+
? Number(process.env.PORT)
16+
: await getPort()
1517

1618
project.provide('mcpServerPort', mcpServerPort)
1719

@@ -122,22 +124,34 @@ export default async function setup(project: TestProject) {
122124
})
123125
}
124126

125-
async function startServers() {
126-
console.log('Starting servers...')
127-
128-
// Start app server if necessary
129-
await startAppServerIfNecessary()
127+
async function startMcpServerIfNecessary() {
128+
const isMcpRunning = await fetch(
129+
`http://localhost:${mcpServerPort}/healthcheck`,
130+
).catch(() => ({ ok: false }))
131+
if (isMcpRunning.ok) {
132+
return
133+
}
130134

131-
// Start the MCP server from the exercise directory
135+
// Start the MCP server if necessary
132136
console.log(`Starting MCP server on port ${mcpServerPort}...`)
133-
mcpServerProcess = execa('npm', ['run', 'dev:server', '--silent'], {
137+
mcpServerProcess = execa('npm', ['run', 'dev:server'], {
134138
cwd: process.cwd(),
135139
stdio: ['ignore', 'pipe', 'pipe'],
136140
env: {
137141
...process.env,
138142
PORT: mcpServerPort.toString(),
139143
},
140144
})
145+
}
146+
147+
async function startServers() {
148+
console.log('Starting servers...')
149+
150+
// Start app server if necessary
151+
await startAppServerIfNecessary()
152+
153+
// Start the MCP server from the exercise directory
154+
await startMcpServerIfNecessary()
141155

142156
try {
143157
// Wait for both servers to be ready simultaneously

exercises/02.consistent/01.solution.remote-dom/test/globalSetup.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ declare module 'vitest' {
1111
type OutputBuffer = Array<{ channel: 'stdout' | 'stderr'; data: Buffer }>
1212

1313
export default async function setup(project: TestProject) {
14-
const mcpServerPort = await getPort()
14+
const mcpServerPort = process.env.PORT
15+
? Number(process.env.PORT)
16+
: await getPort()
1517

1618
project.provide('mcpServerPort', mcpServerPort)
1719

@@ -122,22 +124,34 @@ export default async function setup(project: TestProject) {
122124
})
123125
}
124126

125-
async function startServers() {
126-
console.log('Starting servers...')
127-
128-
// Start app server if necessary
129-
await startAppServerIfNecessary()
127+
async function startMcpServerIfNecessary() {
128+
const isMcpRunning = await fetch(
129+
`http://localhost:${mcpServerPort}/healthcheck`,
130+
).catch(() => ({ ok: false }))
131+
if (isMcpRunning.ok) {
132+
return
133+
}
130134

131-
// Start the MCP server from the exercise directory
135+
// Start the MCP server if necessary
132136
console.log(`Starting MCP server on port ${mcpServerPort}...`)
133-
mcpServerProcess = execa('npm', ['run', 'dev:server', '--silent'], {
137+
mcpServerProcess = execa('npm', ['run', 'dev:server'], {
134138
cwd: process.cwd(),
135139
stdio: ['ignore', 'pipe', 'pipe'],
136140
env: {
137141
...process.env,
138142
PORT: mcpServerPort.toString(),
139143
},
140144
})
145+
}
146+
147+
async function startServers() {
148+
console.log('Starting servers...')
149+
150+
// Start app server if necessary
151+
await startAppServerIfNecessary()
152+
153+
// Start the MCP server from the exercise directory
154+
await startMcpServerIfNecessary()
141155

142156
try {
143157
// Wait for both servers to be ready simultaneously

exercises/03.complex/01.problem.iframe/test/globalSetup.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ declare module 'vitest' {
1111
type OutputBuffer = Array<{ channel: 'stdout' | 'stderr'; data: Buffer }>
1212

1313
export default async function setup(project: TestProject) {
14-
const mcpServerPort = await getPort()
14+
const mcpServerPort = process.env.PORT
15+
? Number(process.env.PORT)
16+
: await getPort()
1517

1618
project.provide('mcpServerPort', mcpServerPort)
1719

@@ -122,22 +124,34 @@ export default async function setup(project: TestProject) {
122124
})
123125
}
124126

125-
async function startServers() {
126-
console.log('Starting servers...')
127-
128-
// Start app server if necessary
129-
await startAppServerIfNecessary()
127+
async function startMcpServerIfNecessary() {
128+
const isMcpRunning = await fetch(
129+
`http://localhost:${mcpServerPort}/healthcheck`,
130+
).catch(() => ({ ok: false }))
131+
if (isMcpRunning.ok) {
132+
return
133+
}
130134

131-
// Start the MCP server from the exercise directory
135+
// Start the MCP server if necessary
132136
console.log(`Starting MCP server on port ${mcpServerPort}...`)
133-
mcpServerProcess = execa('npm', ['run', 'dev:server', '--silent'], {
137+
mcpServerProcess = execa('npm', ['run', 'dev:server'], {
134138
cwd: process.cwd(),
135139
stdio: ['ignore', 'pipe', 'pipe'],
136140
env: {
137141
...process.env,
138142
PORT: mcpServerPort.toString(),
139143
},
140144
})
145+
}
146+
147+
async function startServers() {
148+
console.log('Starting servers...')
149+
150+
// Start app server if necessary
151+
await startAppServerIfNecessary()
152+
153+
// Start the MCP server from the exercise directory
154+
await startMcpServerIfNecessary()
141155

142156
try {
143157
// Wait for both servers to be ready simultaneously

exercises/03.complex/01.solution.iframe/test/globalSetup.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ declare module 'vitest' {
1111
type OutputBuffer = Array<{ channel: 'stdout' | 'stderr'; data: Buffer }>
1212

1313
export default async function setup(project: TestProject) {
14-
const mcpServerPort = await getPort()
14+
const mcpServerPort = process.env.PORT
15+
? Number(process.env.PORT)
16+
: await getPort()
1517

1618
project.provide('mcpServerPort', mcpServerPort)
1719

@@ -122,22 +124,34 @@ export default async function setup(project: TestProject) {
122124
})
123125
}
124126

125-
async function startServers() {
126-
console.log('Starting servers...')
127-
128-
// Start app server if necessary
129-
await startAppServerIfNecessary()
127+
async function startMcpServerIfNecessary() {
128+
const isMcpRunning = await fetch(
129+
`http://localhost:${mcpServerPort}/healthcheck`,
130+
).catch(() => ({ ok: false }))
131+
if (isMcpRunning.ok) {
132+
return
133+
}
130134

131-
// Start the MCP server from the exercise directory
135+
// Start the MCP server if necessary
132136
console.log(`Starting MCP server on port ${mcpServerPort}...`)
133-
mcpServerProcess = execa('npm', ['run', 'dev:server', '--silent'], {
137+
mcpServerProcess = execa('npm', ['run', 'dev:server'], {
134138
cwd: process.cwd(),
135139
stdio: ['ignore', 'pipe', 'pipe'],
136140
env: {
137141
...process.env,
138142
PORT: mcpServerPort.toString(),
139143
},
140144
})
145+
}
146+
147+
async function startServers() {
148+
console.log('Starting servers...')
149+
150+
// Start app server if necessary
151+
await startAppServerIfNecessary()
152+
153+
// Start the MCP server from the exercise directory
154+
await startMcpServerIfNecessary()
141155

142156
try {
143157
// Wait for both servers to be ready simultaneously

exercises/03.complex/02.problem.ready/test/globalSetup.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ declare module 'vitest' {
1111
type OutputBuffer = Array<{ channel: 'stdout' | 'stderr'; data: Buffer }>
1212

1313
export default async function setup(project: TestProject) {
14-
const mcpServerPort = await getPort()
14+
const mcpServerPort = process.env.PORT
15+
? Number(process.env.PORT)
16+
: await getPort()
1517

1618
project.provide('mcpServerPort', mcpServerPort)
1719

@@ -122,22 +124,34 @@ export default async function setup(project: TestProject) {
122124
})
123125
}
124126

125-
async function startServers() {
126-
console.log('Starting servers...')
127-
128-
// Start app server if necessary
129-
await startAppServerIfNecessary()
127+
async function startMcpServerIfNecessary() {
128+
const isMcpRunning = await fetch(
129+
`http://localhost:${mcpServerPort}/healthcheck`,
130+
).catch(() => ({ ok: false }))
131+
if (isMcpRunning.ok) {
132+
return
133+
}
130134

131-
// Start the MCP server from the exercise directory
135+
// Start the MCP server if necessary
132136
console.log(`Starting MCP server on port ${mcpServerPort}...`)
133-
mcpServerProcess = execa('npm', ['run', 'dev:server', '--silent'], {
137+
mcpServerProcess = execa('npm', ['run', 'dev:server'], {
134138
cwd: process.cwd(),
135139
stdio: ['ignore', 'pipe', 'pipe'],
136140
env: {
137141
...process.env,
138142
PORT: mcpServerPort.toString(),
139143
},
140144
})
145+
}
146+
147+
async function startServers() {
148+
console.log('Starting servers...')
149+
150+
// Start app server if necessary
151+
await startAppServerIfNecessary()
152+
153+
// Start the MCP server from the exercise directory
154+
await startMcpServerIfNecessary()
141155

142156
try {
143157
// Wait for both servers to be ready simultaneously

0 commit comments

Comments
 (0)