The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeNet from "node:net"; |
| 2 | import { assert, describe, expect, it } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import * as pathe from "pathe"; |
| 8 | import * as Command from "@/Command/index.ts"; |
| 9 | import * as Provider from "@/Provider.ts"; |
| 10 | import { findAvailablePort } from "@/Util/Node.ts"; |
| 11 | import { assertDead, lifecycleFixture } from "./fixture/lifecycle-support.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ |
| 14 | // DevServer is provider-agnostic — register it directly without dragging |
| 15 | // in a cloud provider's auth chain. |
| 16 | providers: Command.providers(), |
| 17 | dev: true, |
| 18 | }); |
| 19 | |
| 20 | // Typed provider errors are an in-process contract: across the `alchemy dev` |
| 21 | // RPC boundary (the default dev-test topology) a failed lifecycle op is |
| 22 | // re-thrown as a flattened defect — `_tag`/`reason` do not survive |
| 23 | // `Schema.Defect` serialization — so assertions on `CommandError`'s shape |
| 24 | // must run against the in-process provider. |
| 25 | const { test: inProcessTest } = Test.make({ |
| 26 | providers: Command.providers(), |
| 27 | dev: true, |
| 28 | sidecar: false, |
| 29 | }); |
| 30 | |
| 31 | const fixtureDir = pathe.resolve(import.meta.dirname, "fixture"); |
| 32 | const fixtureScript = pathe.join(fixtureDir, "long-running.cjs"); |
| 33 | const urlServerScript = pathe.join(fixtureDir, "url-server.cjs"); |
| 34 | const portServerScript = pathe.join(fixtureDir, "port-server.cjs"); |
| 35 | const dieScript = pathe.join(fixtureDir, "die.cjs"); |
| 36 | |
| 37 | // The provider runs `command.split(" ")` and uses `shell: false`, so the |
| 38 | // fixture path must not contain spaces. The in-repo path doesn't, but a CI |
| 39 | // clone under e.g. `C:\Program Files\...` would. Fail loudly with a clear |
| 40 | // message instead of letting the test hang on a misparsed argv. |
| 41 | if (fixtureScript.includes(" ") || urlServerScript.includes(" ")) { |
| 42 | throw new Error( |
| 43 | `DevServer test fixture path contains a space, which the provider's ` + |
| 44 | `argv split cannot represent: ${fixtureScript} / ${urlServerScript}`, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | const isAlive = (pid: number) => |
| 49 | Effect.sync(() => { |
| 50 | try { |
| 51 | process.kill(pid, 0); |
| 52 | return true; |
| 53 | } catch { |
| 54 | return false; |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | const readPidFile = (path: string) => |
| 59 | Effect.gen(function* () { |
| 60 | const fs = yield* FileSystem.FileSystem; |
| 61 | const content = yield* fs.readFileString(path); |
| 62 | return JSON.parse(content) as { pid: number; marker: string }; |
| 63 | }); |
| 64 | |
| 65 | const waitForPidFile = (path: string, marker: string) => |
| 66 | Effect.gen(function* () { |
| 67 | const fs = yield* FileSystem.FileSystem; |
| 68 | const exists = yield* fs.exists(path); |
| 69 | if (!exists) { |
| 70 | return yield* Effect.fail(new Error("pid file not yet present")); |
| 71 | } |
| 72 | const parsed = yield* readPidFile(path); |
| 73 | if (parsed.marker !== marker) { |
| 74 | return yield* Effect.fail(new Error(`pid file marker ${parsed.marker} !== ${marker}`)); |
| 75 | } |
| 76 | return parsed; |
| 77 | }).pipe(Effect.retry({ schedule: Schedule.spaced("100 millis"), times: 100 })); |
| 78 | |
| 79 | const waitForDeath = (pid: number) => |
| 80 | isAlive(pid).pipe( |
| 81 | Effect.flatMap((alive) => |
| 82 | alive ? Effect.fail(new Error(`pid ${pid} still alive`)) : Effect.succeed(undefined), |
| 83 | ), |
| 84 | Effect.retry({ schedule: Schedule.spaced("100 millis"), times: 50 }), |
| 85 | ); |
| 86 | |
| 87 | test.provider( |
| 88 | "list returns [] (non-listable local dev-server process)", |
| 89 | () => |
| 90 | Effect.gen(function* () { |
| 91 | // DevServer is a local dev-server child process, not a cloud resource — |
| 92 | // there is no remote enumeration API, so list() is the non-listable |
| 93 | // pattern and always returns []. No deploy is needed to observe this. |
| 94 | const provider = yield* Provider.findProvider(Command.Dev); |
| 95 | const all = yield* provider.list(); |
| 96 | expect(all).toEqual([]); |
| 97 | }), |
| 98 | { tags: ["local"], timeout: 30_000 }, |
| 99 | ); |
| 100 | |
| 101 | test.provider( |
| 102 | "starts the process", |
| 103 | (stack) => |
| 104 | Effect.gen(function* () { |
| 105 | const fs = yield* FileSystem.FileSystem; |
| 106 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 107 | const pidFile = pathe.join(tmp, "pid.json"); |
| 108 | |
| 109 | yield* stack.deploy( |
| 110 | Command.Dev("Dev", { |
| 111 | command: `node ${fixtureScript}`, |
| 112 | env: { PID_FILE: pidFile, MARKER: "start" }, |
| 113 | }), |
| 114 | ); |
| 115 | |
| 116 | const { pid } = yield* waitForPidFile(pidFile, "start"); |
| 117 | expect(yield* isAlive(pid)).toBe(true); |
| 118 | |
| 119 | yield* stack.destroy(); |
| 120 | yield* waitForDeath(pid); |
| 121 | }), |
| 122 | { tags: ["local"], timeout: 30_000 }, |
| 123 | ); |
| 124 | |
| 125 | test.provider( |
| 126 | "runs multiple distinct dev servers concurrently", |
| 127 | (stack) => |
| 128 | Effect.gen(function* () { |
| 129 | const fs = yield* FileSystem.FileSystem; |
| 130 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 131 | const pidFileA = pathe.join(tmp, "pid-a.json"); |
| 132 | const pidFileB = pathe.join(tmp, "pid-b.json"); |
| 133 | |
| 134 | yield* stack.deploy( |
| 135 | Effect.gen(function* () { |
| 136 | yield* Command.Dev("Alpha", { |
| 137 | command: `node ${fixtureScript}`, |
| 138 | env: { PID_FILE: pidFileA, MARKER: "alpha" }, |
| 139 | }); |
| 140 | yield* Command.Dev("Beta", { |
| 141 | command: `node ${fixtureScript}`, |
| 142 | env: { PID_FILE: pidFileB, MARKER: "beta" }, |
| 143 | }); |
| 144 | }), |
| 145 | ); |
| 146 | |
| 147 | const alpha = yield* waitForPidFile(pidFileA, "alpha"); |
| 148 | const beta = yield* waitForPidFile(pidFileB, "beta"); |
| 149 | |
| 150 | expect(alpha.pid).not.toBe(beta.pid); |
| 151 | expect(yield* isAlive(alpha.pid)).toBe(true); |
| 152 | expect(yield* isAlive(beta.pid)).toBe(true); |
| 153 | |
| 154 | yield* stack.destroy(); |
| 155 | yield* waitForDeath(alpha.pid); |
| 156 | yield* waitForDeath(beta.pid); |
| 157 | }), |
| 158 | { tags: ["local"], timeout: 30_000 }, |
| 159 | ); |
| 160 | |
| 161 | test.provider( |
| 162 | "keeps the process running across an unchanged update", |
| 163 | (stack) => |
| 164 | Effect.gen(function* () { |
| 165 | const fs = yield* FileSystem.FileSystem; |
| 166 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 167 | const pidFile = pathe.join(tmp, "pid.json"); |
| 168 | |
| 169 | const program = Command.Dev("Dev", { |
| 170 | command: `node ${fixtureScript}`, |
| 171 | env: { PID_FILE: pidFile, MARKER: "stable" }, |
| 172 | }); |
| 173 | |
| 174 | yield* stack.deploy(program); |
| 175 | const first = yield* waitForPidFile(pidFile, "stable"); |
| 176 | |
| 177 | // Re-deploy the same props. Provider hashes match → keep running. |
| 178 | yield* stack.deploy(program); |
| 179 | // Give the provider a moment in case it would (incorrectly) respawn. |
| 180 | yield* Effect.sleep("500 millis"); |
| 181 | |
| 182 | const second = yield* readPidFile(pidFile); |
| 183 | expect(second.pid).toBe(first.pid); |
| 184 | expect(second.marker).toBe("stable"); |
| 185 | expect(yield* isAlive(first.pid)).toBe(true); |
| 186 | |
| 187 | yield* stack.destroy(); |
| 188 | yield* waitForDeath(first.pid); |
| 189 | }), |
| 190 | { tags: ["local"], timeout: 30_000 }, |
| 191 | ); |
| 192 | |
| 193 | test.provider( |
| 194 | "restarts the process when props change", |
| 195 | (stack) => |
| 196 | Effect.gen(function* () { |
| 197 | const fs = yield* FileSystem.FileSystem; |
| 198 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 199 | const pidFile = pathe.join(tmp, "pid.json"); |
| 200 | |
| 201 | yield* stack.deploy( |
| 202 | Command.Dev("Dev", { |
| 203 | command: `node ${fixtureScript}`, |
| 204 | env: { PID_FILE: pidFile, MARKER: "v1" }, |
| 205 | }), |
| 206 | ); |
| 207 | const first = yield* waitForPidFile(pidFile, "v1"); |
| 208 | expect(yield* isAlive(first.pid)).toBe(true); |
| 209 | |
| 210 | // Change the env (and therefore the hash) — provider should kill the |
| 211 | // running process and spawn a fresh one with the new marker. |
| 212 | yield* stack.deploy( |
| 213 | Command.Dev("Dev", { |
| 214 | command: `node ${fixtureScript}`, |
| 215 | env: { PID_FILE: pidFile, MARKER: "v2" }, |
| 216 | }), |
| 217 | ); |
| 218 | const second = yield* waitForPidFile(pidFile, "v2"); |
| 219 | |
| 220 | expect(second.pid).not.toBe(first.pid); |
| 221 | expect(yield* isAlive(second.pid)).toBe(true); |
| 222 | yield* waitForDeath(first.pid); |
| 223 | |
| 224 | yield* stack.destroy(); |
| 225 | yield* waitForDeath(second.pid); |
| 226 | }), |
| 227 | { tags: ["local"], timeout: 30_000 }, |
| 228 | ); |
| 229 | |
| 230 | test.provider( |
| 231 | "extracts the first URL printed to stdout", |
| 232 | (stack) => |
| 233 | Effect.gen(function* () { |
| 234 | const fs = yield* FileSystem.FileSystem; |
| 235 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 236 | const pidFile = pathe.join(tmp, "pid.json"); |
| 237 | |
| 238 | const output = yield* stack.deploy( |
| 239 | Command.Dev("Dev", { |
| 240 | command: `node ${urlServerScript}`, |
| 241 | env: { |
| 242 | PID_FILE: pidFile, |
| 243 | MARKER: "url-stdout", |
| 244 | URL_LINE: "Local: http://localhost:5173/", |
| 245 | }, |
| 246 | }), |
| 247 | ); |
| 248 | |
| 249 | expect(output.url).toBe("http://localhost:5173/"); |
| 250 | |
| 251 | const { pid } = yield* waitForPidFile(pidFile, "url-stdout"); |
| 252 | yield* stack.destroy(); |
| 253 | yield* waitForDeath(pid); |
| 254 | }), |
| 255 | { tags: ["local"], timeout: 30_000 }, |
| 256 | ); |
| 257 | |
| 258 | test.provider( |
| 259 | "extracts a URL printed to stderr", |
| 260 | (stack) => |
| 261 | Effect.gen(function* () { |
| 262 | const fs = yield* FileSystem.FileSystem; |
| 263 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 264 | const pidFile = pathe.join(tmp, "pid.json"); |
| 265 | |
| 266 | const output = yield* stack.deploy( |
| 267 | Command.Dev("Dev", { |
| 268 | command: `node ${urlServerScript}`, |
| 269 | env: { |
| 270 | PID_FILE: pidFile, |
| 271 | MARKER: "url-stderr", |
| 272 | URL_LINE: "ready - started server on http://127.0.0.1:3000", |
| 273 | URL_STREAM: "stderr", |
| 274 | }, |
| 275 | }), |
| 276 | ); |
| 277 | |
| 278 | expect(output.url).toBe("http://127.0.0.1:3000"); |
| 279 | |
| 280 | const { pid } = yield* waitForPidFile(pidFile, "url-stderr"); |
| 281 | yield* stack.destroy(); |
| 282 | yield* waitForDeath(pid); |
| 283 | }), |
| 284 | { tags: ["local"], timeout: 30_000 }, |
| 285 | ); |
| 286 | |
| 287 | test.provider( |
| 288 | "strips ANSI escapes before matching", |
| 289 | (stack) => |
| 290 | Effect.gen(function* () { |
| 291 | const fs = yield* FileSystem.FileSystem; |
| 292 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 293 | const pidFile = pathe.join(tmp, "pid.json"); |
| 294 | |
| 295 | // Vite-style colored output: " ➜ Local: http://localhost:5173/" |
| 296 | // with green + cyan SGR sequences around the URL. |
| 297 | const ansi = (open: string, body: string) => `\x1b[${open}m${body}\x1b[0m`; |
| 298 | const line = ` ➜ ${ansi("32", "Local:")} ${ansi("36", "http://localhost:5173/")}`; |
| 299 | |
| 300 | const output = yield* stack.deploy( |
| 301 | Command.Dev("Dev", { |
| 302 | command: `node ${urlServerScript}`, |
| 303 | env: { PID_FILE: pidFile, MARKER: "url-ansi", URL_LINE: line }, |
| 304 | }), |
| 305 | ); |
| 306 | |
| 307 | expect(output.url).toBe("http://localhost:5173/"); |
| 308 | |
| 309 | const { pid } = yield* waitForPidFile(pidFile, "url-ansi"); |
| 310 | yield* stack.destroy(); |
| 311 | yield* waitForDeath(pid); |
| 312 | }), |
| 313 | { tags: ["local"], timeout: 30_000 }, |
| 314 | ); |
| 315 | |
| 316 | test.provider( |
| 317 | "favors a localhost URL over an unrelated URL printed first (#695)", |
| 318 | (stack) => |
| 319 | Effect.gen(function* () { |
| 320 | const fs = yield* FileSystem.FileSystem; |
| 321 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 322 | const pidFile = pathe.join(tmp, "pid.json"); |
| 323 | |
| 324 | const output = yield* stack.deploy( |
| 325 | Command.Dev("Dev", { |
| 326 | command: `node ${urlServerScript}`, |
| 327 | env: { |
| 328 | PID_FILE: pidFile, |
| 329 | MARKER: "url-favor-local", |
| 330 | // A docs link is printed first (as Astro does on a content |
| 331 | // error), then the dev server prints its own localhost URL. |
| 332 | URL_LINE: "https://docs.astro.build/en/guides/content-collections/", |
| 333 | URL_LINE_2: " ➜ Local: http://localhost:5001/", |
| 334 | URL_DELAY_2_MS: "300", |
| 335 | }, |
| 336 | }), |
| 337 | ); |
| 338 | |
| 339 | expect(output.url).toBe("http://localhost:5001/"); |
| 340 | |
| 341 | const { pid } = yield* waitForPidFile(pidFile, "url-favor-local"); |
| 342 | yield* stack.destroy(); |
| 343 | yield* waitForDeath(pid); |
| 344 | }), |
| 345 | { tags: ["local"], timeout: 30_000 }, |
| 346 | ); |
| 347 | |
| 348 | test.provider( |
| 349 | "falls back to a non-local URL when no localhost URL appears (#695)", |
| 350 | (stack) => |
| 351 | Effect.gen(function* () { |
| 352 | const fs = yield* FileSystem.FileSystem; |
| 353 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 354 | const pidFile = pathe.join(tmp, "pid.json"); |
| 355 | |
| 356 | const output = yield* stack.deploy( |
| 357 | Command.Dev("Dev", { |
| 358 | command: `node ${urlServerScript}`, |
| 359 | env: { |
| 360 | PID_FILE: pidFile, |
| 361 | MARKER: "url-fallback", |
| 362 | // Only a non-local URL is ever printed — it should still surface. |
| 363 | URL_LINE: "https://docs.astro.build/en/guides/content-collections/", |
| 364 | }, |
| 365 | }), |
| 366 | ); |
| 367 | |
| 368 | expect(output.url).toBe("https://docs.astro.build/en/guides/content-collections/"); |
| 369 | |
| 370 | const { pid } = yield* waitForPidFile(pidFile, "url-fallback"); |
| 371 | yield* stack.destroy(); |
| 372 | yield* waitForDeath(pid); |
| 373 | }), |
| 374 | { tags: ["local"], timeout: 30_000 }, |
| 375 | ); |
| 376 | |
| 377 | test.provider( |
| 378 | "returns undefined when no URL is printed within the timeout", |
| 379 | (stack) => |
| 380 | Effect.gen(function* () { |
| 381 | const fs = yield* FileSystem.FileSystem; |
| 382 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 383 | const pidFile = pathe.join(tmp, "pid.json"); |
| 384 | |
| 385 | const output = yield* stack.deploy( |
| 386 | Command.Dev("Dev", { |
| 387 | command: `node ${urlServerScript}`, |
| 388 | env: { |
| 389 | PID_FILE: pidFile, |
| 390 | MARKER: "no-url", |
| 391 | // URL_LINE intentionally unset — process stays silent so |
| 392 | // reconcile waits the full URL_EXTRACT_TIMEOUT and falls back. |
| 393 | }, |
| 394 | }), |
| 395 | ); |
| 396 | |
| 397 | expect(output.url).toBeUndefined(); |
| 398 | |
| 399 | const { pid } = yield* waitForPidFile(pidFile, "no-url"); |
| 400 | expect(yield* isAlive(pid)).toBe(true); |
| 401 | |
| 402 | yield* stack.destroy(); |
| 403 | yield* waitForDeath(pid); |
| 404 | }), |
| 405 | { tags: ["local"], timeout: 30_000 }, |
| 406 | ); |
| 407 | |
| 408 | test.provider( |
| 409 | "waitForUrl waits past the default 5 seconds for the URL", |
| 410 | (stack) => |
| 411 | Effect.gen(function* () { |
| 412 | const fs = yield* FileSystem.FileSystem; |
| 413 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 414 | const pidFile = pathe.join(tmp, "pid.json"); |
| 415 | |
| 416 | const output = yield* stack.deploy( |
| 417 | Command.Dev("Dev", { |
| 418 | command: `node ${urlServerScript}`, |
| 419 | env: { |
| 420 | PID_FILE: pidFile, |
| 421 | MARKER: "wait-for-url", |
| 422 | URL_LINE: "Local: http://localhost:5173/", |
| 423 | // Printed after the 5 seconds the resource waits by default. |
| 424 | URL_DELAY_MS: "6000", |
| 425 | }, |
| 426 | waitForUrl: true, |
| 427 | }), |
| 428 | ); |
| 429 | |
| 430 | expect(output.url).toBe("http://localhost:5173/"); |
| 431 | |
| 432 | const { pid } = yield* waitForPidFile(pidFile, "wait-for-url"); |
| 433 | yield* stack.destroy(); |
| 434 | yield* waitForDeath(pid); |
| 435 | }), |
| 436 | { tags: ["local"], timeout: 30_000 }, |
| 437 | ); |
| 438 | |
| 439 | test.provider( |
| 440 | "waitForUrl settles with a late non-local URL when no localhost URL follows", |
| 441 | (stack) => |
| 442 | Effect.gen(function* () { |
| 443 | const fs = yield* FileSystem.FileSystem; |
| 444 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 445 | const pidFile = pathe.join(tmp, "pid.json"); |
| 446 | |
| 447 | const output = yield* stack.deploy( |
| 448 | Command.Dev("Dev", { |
| 449 | command: `node ${urlServerScript}`, |
| 450 | env: { |
| 451 | PID_FILE: pidFile, |
| 452 | MARKER: "wait-for-url-fallback", |
| 453 | URL_LINE: "https://docs.astro.build/en/guides/content-collections/", |
| 454 | URL_DELAY_MS: "6000", |
| 455 | }, |
| 456 | waitForUrl: true, |
| 457 | }), |
| 458 | ); |
| 459 | |
| 460 | expect(output.url).toBe("https://docs.astro.build/en/guides/content-collections/"); |
| 461 | |
| 462 | const { pid } = yield* waitForPidFile(pidFile, "wait-for-url-fallback"); |
| 463 | yield* stack.destroy(); |
| 464 | yield* waitForDeath(pid); |
| 465 | }), |
| 466 | { tags: ["local"], timeout: 30_000 }, |
| 467 | ); |
| 468 | |
| 469 | inProcessTest.provider( |
| 470 | "waitForUrl keeps waiting for a command that prints no URL", |
| 471 | (stack) => |
| 472 | Effect.gen(function* () { |
| 473 | const fs = yield* FileSystem.FileSystem; |
| 474 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 475 | const pidFile = pathe.join(tmp, "pid.json"); |
| 476 | |
| 477 | const result = yield* stack |
| 478 | .deploy( |
| 479 | Command.Dev("Dev", { |
| 480 | command: `node ${urlServerScript}`, |
| 481 | // URL_LINE intentionally unset — the process stays silent. |
| 482 | env: { PID_FILE: pidFile, MARKER: "wait-for-url-none" }, |
| 483 | waitForUrl: true, |
| 484 | }), |
| 485 | ) |
| 486 | // Well past the 5 seconds the resource waits by default. |
| 487 | .pipe(Effect.timeoutOption("8 seconds")); |
| 488 | expect(result._tag).toBe("None"); |
| 489 | |
| 490 | // Interrupting the deploy (Ctrl+C) leaves no process behind. |
| 491 | const { pid } = yield* waitForPidFile(pidFile, "wait-for-url-none"); |
| 492 | yield* waitForDeath(pid); |
| 493 | yield* stack.destroy(); |
| 494 | }), |
| 495 | { tags: ["local"], timeout: 30_000 }, |
| 496 | ); |
| 497 | |
| 498 | inProcessTest.provider( |
| 499 | "waitForUrl fails with UnexpectedExit when the command exits first", |
| 500 | (stack) => |
| 501 | Effect.gen(function* () { |
| 502 | const error = yield* stack |
| 503 | .deploy(Command.Dev("Dev", { command: `node ${dieScript}`, waitForUrl: true })) |
| 504 | .pipe(Effect.flip); |
| 505 | assert(Command.isCommandError(error)); |
| 506 | assert(error.reason._tag === "UnexpectedExit"); |
| 507 | expect(error.reason.exitCode).toBe(1); |
| 508 | }), |
| 509 | { tags: ["local"], timeout: 30_000 }, |
| 510 | ); |
| 511 | |
| 512 | test.provider( |
| 513 | "port is ready once the command listens on it, past the default 5 seconds", |
| 514 | (stack) => |
| 515 | Effect.gen(function* () { |
| 516 | const fs = yield* FileSystem.FileSystem; |
| 517 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 518 | const pidFile = pathe.join(tmp, "pid.json"); |
| 519 | const port = yield* findAvailablePort(); |
| 520 | |
| 521 | const output = yield* stack.deploy( |
| 522 | Command.Dev("Dev", { |
| 523 | // Listens on `PORT`, printing nothing, after the 5 seconds the |
| 524 | // resource waits for a URL by default. |
| 525 | command: `node ${portServerScript}`, |
| 526 | env: { PID_FILE: pidFile, MARKER: "port", LISTEN_DELAY_MS: "6000" }, |
| 527 | port, |
| 528 | }), |
| 529 | ); |
| 530 | |
| 531 | expect(output.url).toBe(`http://localhost:${port}`); |
| 532 | |
| 533 | const { pid } = yield* waitForPidFile(pidFile, "port"); |
| 534 | yield* stack.destroy(); |
| 535 | yield* waitForDeath(pid); |
| 536 | }), |
| 537 | { tags: ["local"], timeout: 30_000 }, |
| 538 | ); |
| 539 | |
| 540 | inProcessTest.provider( |
| 541 | "port fails with UnexpectedExit when the command exits first", |
| 542 | (stack) => |
| 543 | Effect.gen(function* () { |
| 544 | const port = yield* findAvailablePort(); |
| 545 | const error = yield* stack |
| 546 | .deploy(Command.Dev("Dev", { command: `node ${dieScript}`, port })) |
| 547 | .pipe(Effect.flip); |
| 548 | assert(Command.isCommandError(error)); |
| 549 | assert(error.reason._tag === "UnexpectedExit"); |
| 550 | expect(error.reason.exitCode).toBe(1); |
| 551 | }), |
| 552 | { tags: ["local"], timeout: 30_000 }, |
| 553 | ); |
| 554 | |
| 555 | inProcessTest.provider( |
| 556 | "port fails without starting the command when another process listens on it", |
| 557 | (stack) => |
| 558 | Effect.gen(function* () { |
| 559 | const fs = yield* FileSystem.FileSystem; |
| 560 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 561 | const pidFile = pathe.join(tmp, "pid.json"); |
| 562 | const server = yield* Effect.acquireRelease( |
| 563 | Effect.callback<NodeNet.Server>((resume) => { |
| 564 | const server = NodeNet.createServer(); |
| 565 | server.listen(0, "127.0.0.1", () => resume(Effect.succeed(server))); |
| 566 | }), |
| 567 | (server) => Effect.callback<void>((resume) => void server.close(() => resume(Effect.void))), |
| 568 | ); |
| 569 | const { port } = server.address() as NodeNet.AddressInfo; |
| 570 | |
| 571 | const error = yield* stack |
| 572 | .deploy( |
| 573 | Command.Dev("Dev", { |
| 574 | command: `node ${portServerScript}`, |
| 575 | env: { PID_FILE: pidFile, MARKER: "port-busy" }, |
| 576 | port, |
| 577 | }), |
| 578 | ) |
| 579 | .pipe(Effect.flip); |
| 580 | assert(Command.isCommandError(error)); |
| 581 | expect(error.reason._tag).toBe("Busy"); |
| 582 | expect(yield* fs.exists(pidFile)).toBe(false); |
| 583 | }), |
| 584 | { tags: ["local"], timeout: 30_000 }, |
| 585 | ); |
| 586 | |
| 587 | test.provider( |
| 588 | "stops the process on destroy", |
| 589 | (stack) => |
| 590 | Effect.gen(function* () { |
| 591 | const fs = yield* FileSystem.FileSystem; |
| 592 | const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "devcmd-" }); |
| 593 | const pidFile = pathe.join(tmp, "pid.json"); |
| 594 | |
| 595 | yield* stack.deploy( |
| 596 | Command.Dev("Dev", { |
| 597 | command: `node ${fixtureScript}`, |
| 598 | env: { PID_FILE: pidFile, MARKER: "stop" }, |
| 599 | }), |
| 600 | ); |
| 601 | const { pid } = yield* waitForPidFile(pidFile, "stop"); |
| 602 | expect(yield* isAlive(pid)).toBe(true); |
| 603 | |
| 604 | yield* stack.destroy(); |
| 605 | yield* waitForDeath(pid); |
| 606 | expect(yield* isAlive(pid)).toBe(false); |
| 607 | }), |
| 608 | { tags: ["local"], timeout: 30_000 }, |
| 609 | ); |
| 610 | |
| 611 | inProcessTest.provider( |
| 612 | "errors when the command fails in first 5 seconds", |
| 613 | (stack) => |
| 614 | Effect.gen(function* () { |
| 615 | const error = yield* stack |
| 616 | .deploy(Command.Dev("Dev", { command: `node ${dieScript}` })) |
| 617 | .pipe(Effect.flip); |
| 618 | assert(Command.isCommandError(error)); |
| 619 | assert(error.reason._tag === "UnexpectedExit"); |
| 620 | expect(error.reason.exitCode).toBe(1); |
| 621 | expect(error.reason.stderr).toContain("I'm not feeling it..."); |
| 622 | }), |
| 623 | { tags: ["local"] }, |
| 624 | ); |
| 625 | |
| 626 | describe("extractUrl", { tags: ["unit", "local"] }, () => { |
| 627 | it("returns a plain URL when it is the only match", () => { |
| 628 | expect(Command.extractUrl("Local: http://localhost:5173/")).toBe("http://localhost:5173/"); |
| 629 | }); |
| 630 | |
| 631 | it("favors a localhost URL over an unrelated URL printed first", () => { |
| 632 | // Astro prints a docs link on a content error before the dev server |
| 633 | // prints its own localhost URL. See issue #695. |
| 634 | const buffer = |
| 635 | "see https://docs.astro.build/en/guides/content-collections/\n" + |
| 636 | " ➜ Local: http://localhost:5001/\n"; |
| 637 | expect(Command.extractUrl(buffer)).toBe("http://localhost:5001/"); |
| 638 | }); |
| 639 | |
| 640 | it("favors an IP URL over an unrelated URL printed first", () => { |
| 641 | const buffer = |
| 642 | "update available https://example.com/release\n" + |
| 643 | "ready - started server on http://127.0.0.1:3000\n"; |
| 644 | expect(Command.extractUrl(buffer)).toBe("http://127.0.0.1:3000"); |
| 645 | }); |
| 646 | |
| 647 | it("falls back to a non-local URL when no local URL is present", () => { |
| 648 | expect(Command.extractUrl("docs https://docs.astro.build/en/guides/x/")).toBe( |
| 649 | "https://docs.astro.build/en/guides/x/", |
| 650 | ); |
| 651 | }); |
| 652 | |
| 653 | it("normalizes a bind-all address to localhost", () => { |
| 654 | // Nuxt prints its *bind* address (`0.0.0.0`), which is not a |
| 655 | // connectable host — consumers of `url` must be able to dial it. |
| 656 | expect(Command.extractUrl("Listening on http://0.0.0.0:3000/")).toBe("http://localhost:3000/"); |
| 657 | expect(Command.extractUrl("Listening on http://[::]:3000/")).toBe("http://localhost:3000/"); |
| 658 | }); |
| 659 | |
| 660 | it("strips ANSI escapes before matching", () => { |
| 661 | expect(Command.extractUrl("\x1b[36mhttp://localhost:5173/\x1b[0m")).toBe( |
| 662 | "http://localhost:5173/", |
| 663 | ); |
| 664 | }); |
| 665 | |
| 666 | it("returns undefined when there is no URL", () => { |
| 667 | expect(Command.extractUrl("no url here")).toBeUndefined(); |
| 668 | }); |
| 669 | }); |
| 670 | |
| 671 | test.provider.skipIf(process.platform === "win32")( |
| 672 | "restart and destroy let wrappers clean their detached children", |
| 673 | (stack) => |
| 674 | Effect.gen(function* () { |
| 675 | yield* stack.destroy(); |
| 676 | const first = yield* lifecycleFixture(); |
| 677 | const second = yield* lifecycleFixture(); |
| 678 | yield* stack.deploy(Command.Dev("Graceful", first.props)); |
| 679 | const old = yield* first.ready; |
| 680 | yield* stack.deploy(Command.Dev("Graceful", second.props)); |
| 681 | const current = yield* second.ready; |
| 682 | expect(yield* first.has("wrapper.clean")).toBe(true); |
| 683 | yield* assertDead(old.wrapper); |
| 684 | yield* assertDead(old.leaf); |
| 685 | yield* stack.destroy(); |
| 686 | expect(yield* second.has("wrapper.clean")).toBe(true); |
| 687 | yield* assertDead(current.wrapper); |
| 688 | yield* assertDead(current.leaf); |
| 689 | }), |
| 690 | { tags: ["local"], timeout: 30_000 }, |
| 691 | ); |