The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - builds real worktree layouts on disk. |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as NodeChildProcess from "node:child_process"; |
| 4 | import * as NodeFS from "node:fs"; |
| 5 | import * as NodeOS from "node:os"; |
| 6 | import * as NodePath from "node:path"; |
| 7 | import * as NetService from "@t3tools/shared/Net"; |
| 8 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 9 | import { assert, describe, it } from "@effect/vitest"; |
| 10 | import * as ByteSize from "effect/ByteSize"; |
| 11 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as FileSystem from "effect/FileSystem"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | import * as Logger from "effect/Logger"; |
| 16 | import * as Path from "effect/Path"; |
| 17 | import * as PlatformError from "effect/PlatformError"; |
| 18 | import * as Sink from "effect/Sink"; |
| 19 | import * as Stream from "effect/Stream"; |
| 20 | import { ChildProcessSpawner } from "effect/process"; |
| 21 | |
| 22 | import { |
| 23 | checkPortAvailabilityOnHosts, |
| 24 | createDevRunnerEnv, |
| 25 | findFirstAvailableOffset, |
| 26 | resolveModePortOffsets, |
| 27 | runDevRunnerWithInput, |
| 28 | } from "./dev-runner.ts"; |
| 29 | |
| 30 | const emptyConfigLayer = ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })); |
| 31 | const netServiceLayer = Layer.succeed(NetService.NetService, { |
| 32 | canListenOnHost: () => Effect.succeed(true), |
| 33 | isPortAvailableOnLoopback: () => Effect.succeed(true), |
| 34 | hasListenerOnHost: () => Effect.succeed(false), |
| 35 | reserveLoopbackPort: () => Effect.succeed(49_152), |
| 36 | findAvailablePort: (port) => Effect.succeed(port), |
| 37 | }); |
| 38 | |
| 39 | function mockProcess(exit: number | PlatformError.PlatformError) { |
| 40 | return ChildProcessSpawner.makeHandle({ |
| 41 | pid: ChildProcessSpawner.ProcessId(1), |
| 42 | exitCode: |
| 43 | typeof exit === "number" |
| 44 | ? Effect.succeed(ChildProcessSpawner.ExitCode(exit)) |
| 45 | : Effect.fail(exit), |
| 46 | isRunning: Effect.succeed(false), |
| 47 | kill: () => Effect.void, |
| 48 | unref: Effect.succeed(Effect.void), |
| 49 | stdin: Sink.drain, |
| 50 | stdout: Stream.empty, |
| 51 | stderr: Stream.empty, |
| 52 | all: Stream.empty, |
| 53 | getInputFd: () => Sink.drain, |
| 54 | getOutputFd: () => Stream.empty, |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | const devServerInput = { |
| 59 | mode: "dev:server", |
| 60 | t3Home: "/tmp/t3code-dev-runner", |
| 61 | browser: undefined, |
| 62 | autoBootstrapProjectFromCwd: undefined, |
| 63 | logWebSocketEvents: undefined, |
| 64 | host: undefined, |
| 65 | port: 13_773, |
| 66 | devUrl: undefined, |
| 67 | dryRun: false, |
| 68 | share: false, |
| 69 | built: false, |
| 70 | runArgs: ["--inspect", "secret-token-value"], |
| 71 | } as const; |
| 72 | |
| 73 | it.layer(NodeServices.layer)("dev-runner", (it) => { |
| 74 | describe("createDevRunnerEnv", () => { |
| 75 | it.effect("forwards the reusable auth token to web dev and removes it for desktop", () => |
| 76 | Effect.gen(function* () { |
| 77 | const input = { |
| 78 | baseEnv: { T3CODE_DEV_AUTH_TOKEN: "reusable-dev-auth-token-that-is-long-enough" }, |
| 79 | serverOffset: 0, |
| 80 | webOffset: 0, |
| 81 | t3Home: undefined, |
| 82 | browser: undefined, |
| 83 | autoBootstrapProjectFromCwd: undefined, |
| 84 | logWebSocketEvents: undefined, |
| 85 | host: undefined, |
| 86 | port: undefined, |
| 87 | devUrl: undefined, |
| 88 | } as const; |
| 89 | const web = yield* createDevRunnerEnv({ ...input, mode: "dev" }); |
| 90 | const desktop = yield* createDevRunnerEnv({ ...input, mode: "dev:desktop" }); |
| 91 | |
| 92 | assert.equal(web.T3CODE_DEV_AUTH_TOKEN, input.baseEnv.T3CODE_DEV_AUTH_TOKEN); |
| 93 | assert.equal(desktop.T3CODE_DEV_AUTH_TOKEN, undefined); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | it.effect("turns product analytics off unless the developer opts in", () => |
| 98 | Effect.gen(function* () { |
| 99 | const input = { |
| 100 | mode: "dev", |
| 101 | serverOffset: 0, |
| 102 | webOffset: 0, |
| 103 | t3Home: undefined, |
| 104 | browser: undefined, |
| 105 | autoBootstrapProjectFromCwd: undefined, |
| 106 | logWebSocketEvents: undefined, |
| 107 | host: undefined, |
| 108 | port: undefined, |
| 109 | devUrl: undefined, |
| 110 | } as const; |
| 111 | const byDefault = yield* createDevRunnerEnv({ ...input, baseEnv: {} }); |
| 112 | const optedIn = yield* createDevRunnerEnv({ |
| 113 | ...input, |
| 114 | baseEnv: { T3CODE_TELEMETRY_ENABLED: "true" }, |
| 115 | }); |
| 116 | |
| 117 | assert.equal(byDefault.T3CODE_TELEMETRY_ENABLED, "false"); |
| 118 | assert.equal(optedIn.T3CODE_TELEMETRY_ENABLED, "true"); |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | it.effect("strips inherited service-launcher context", () => |
| 123 | Effect.gen(function* () { |
| 124 | const env = yield* createDevRunnerEnv({ |
| 125 | mode: "dev", |
| 126 | baseEnv: { |
| 127 | T3_SERVICE_LAUNCHER_CONTEXT: '{"childVersion":"9.9.9"}', |
| 128 | T3_BOOT_SERVICE_UNIT: "t3code.service", |
| 129 | }, |
| 130 | serverOffset: 0, |
| 131 | webOffset: 0, |
| 132 | t3Home: undefined, |
| 133 | browser: undefined, |
| 134 | autoBootstrapProjectFromCwd: undefined, |
| 135 | logWebSocketEvents: undefined, |
| 136 | host: undefined, |
| 137 | port: undefined, |
| 138 | devUrl: undefined, |
| 139 | }); |
| 140 | |
| 141 | assert.equal(env.T3_SERVICE_LAUNCHER_CONTEXT, undefined); |
| 142 | assert.equal(env.T3_BOOT_SERVICE_UNIT, undefined); |
| 143 | }), |
| 144 | ); |
| 145 | |
| 146 | it.effect.each(["dev", "dev:web"] as const)( |
| 147 | "leaves the client backend URLs unset in %s mode", |
| 148 | (mode) => |
| 149 | Effect.gen(function* () { |
| 150 | const env = yield* createDevRunnerEnv({ |
| 151 | mode, |
| 152 | baseEnv: { |
| 153 | VITE_HTTP_URL: "http://localhost:1234", |
| 154 | VITE_WS_URL: "ws://localhost:1234", |
| 155 | }, |
| 156 | serverOffset: 0, |
| 157 | webOffset: 0, |
| 158 | t3Home: undefined, |
| 159 | browser: undefined, |
| 160 | autoBootstrapProjectFromCwd: undefined, |
| 161 | logWebSocketEvents: undefined, |
| 162 | host: undefined, |
| 163 | port: undefined, |
| 164 | devUrl: undefined, |
| 165 | }); |
| 166 | |
| 167 | assert.equal(env.VITE_HTTP_URL, undefined); |
| 168 | assert.equal(env.VITE_WS_URL, undefined); |
| 169 | assert.equal(env.T3CODE_PORT, "13773"); |
| 170 | assert.equal(env.T3CODE_SINGLE_ORIGIN_DEV, "1"); |
| 171 | }), |
| 172 | ); |
| 173 | |
| 174 | it.effect.each(["dev", "dev:web"] as const)("drops an inherited HOST in %s mode", (mode) => |
| 175 | Effect.gen(function* () { |
| 176 | const env = yield* createDevRunnerEnv({ |
| 177 | mode, |
| 178 | baseEnv: { HOST: "0.0.0.0" }, |
| 179 | serverOffset: 0, |
| 180 | webOffset: 0, |
| 181 | t3Home: undefined, |
| 182 | browser: undefined, |
| 183 | autoBootstrapProjectFromCwd: undefined, |
| 184 | logWebSocketEvents: undefined, |
| 185 | host: undefined, |
| 186 | port: undefined, |
| 187 | devUrl: undefined, |
| 188 | }); |
| 189 | |
| 190 | assert.equal(env.HOST, undefined); |
| 191 | }), |
| 192 | ); |
| 193 | |
| 194 | it.effect("drops an inherited T3CODE_HOST and T3CODE_LAN from a parent T3 server", () => |
| 195 | Effect.gen(function* () { |
| 196 | const env = yield* createDevRunnerEnv({ |
| 197 | mode: "dev", |
| 198 | baseEnv: { T3CODE_PORT: "3773", T3CODE_HOST: "0.0.0.0", T3CODE_LAN: "true" }, |
| 199 | serverOffset: 0, |
| 200 | webOffset: 0, |
| 201 | t3Home: undefined, |
| 202 | browser: undefined, |
| 203 | autoBootstrapProjectFromCwd: undefined, |
| 204 | logWebSocketEvents: undefined, |
| 205 | host: undefined, |
| 206 | port: undefined, |
| 207 | devUrl: undefined, |
| 208 | }); |
| 209 | |
| 210 | assert.equal(env.T3CODE_PORT, "13773"); |
| 211 | assert.equal(env.T3CODE_HOST, undefined); |
| 212 | assert.equal(env.T3CODE_LAN, undefined); |
| 213 | }), |
| 214 | ); |
| 215 | }); |
| 216 | |
| 217 | describe("cli", () => { |
| 218 | it("ignores the parent T3 server's T3CODE_PORT and T3CODE_HOST", () => { |
| 219 | const result = NodeChildProcess.spawnSync( |
| 220 | process.execPath, |
| 221 | ["scripts/dev-runner.ts", "dev:server", "--dry-run"], |
| 222 | { |
| 223 | cwd: NodePath.resolve(import.meta.dirname, ".."), |
| 224 | encoding: "utf8", |
| 225 | env: { |
| 226 | ...process.env, |
| 227 | T3CODE_PORT: "3773", |
| 228 | T3CODE_HOST: "0.0.0.0", |
| 229 | }, |
| 230 | }, |
| 231 | ); |
| 232 | |
| 233 | assert.equal(result.status, 0, result.stderr); |
| 234 | const output = `${result.stdout}${result.stderr}`; |
| 235 | assert.match(output, /serverPort=\d+ /); |
| 236 | assert.notMatch(output, /serverPort=3773 /); |
| 237 | }); |
| 238 | }); |
| 239 | |
| 240 | describe("findFirstAvailableOffset", () => { |
| 241 | it.effect("advances until all required ports are available", () => |
| 242 | Effect.gen(function* () { |
| 243 | const taken = new Set([13773, 5733, 13774, 5734]); |
| 244 | const offset = yield* findFirstAvailableOffset({ |
| 245 | startOffset: 0, |
| 246 | requireServerPort: true, |
| 247 | requireWebPort: true, |
| 248 | checkPortAvailability: (port) => Effect.succeed(!taken.has(port)), |
| 249 | }); |
| 250 | |
| 251 | assert.equal(offset, 2); |
| 252 | }), |
| 253 | ); |
| 254 | |
| 255 | it.effect("skips browser-blocked web ports before probing availability", () => |
| 256 | Effect.gen(function* () { |
| 257 | const probed: Array<{ port: number; role: string | undefined }> = []; |
| 258 | const offset = yield* findFirstAvailableOffset({ |
| 259 | // 5733 + 833 = 6566, which browsers block as sane-port. |
| 260 | startOffset: 833, |
| 261 | requireServerPort: true, |
| 262 | requireWebPort: true, |
| 263 | checkPortAvailability: (port, role) => { |
| 264 | probed.push({ port, role }); |
| 265 | return Effect.succeed(true); |
| 266 | }, |
| 267 | }); |
| 268 | |
| 269 | assert.equal(offset, 834); |
| 270 | assert.deepStrictEqual(probed, [ |
| 271 | { port: 14_607, role: "server" }, |
| 272 | { port: 6567, role: "web" }, |
| 273 | ]); |
| 274 | }), |
| 275 | ); |
| 276 | |
| 277 | it.effect("does not reject a server-only offset because its unused web port is blocked", () => |
| 278 | Effect.gen(function* () { |
| 279 | const offset = yield* findFirstAvailableOffset({ |
| 280 | startOffset: 833, |
| 281 | requireServerPort: true, |
| 282 | requireWebPort: false, |
| 283 | checkPortAvailability: () => Effect.succeed(true), |
| 284 | }); |
| 285 | |
| 286 | assert.equal(offset, 833); |
| 287 | }), |
| 288 | ); |
| 289 | }); |
| 290 | |
| 291 | describe("checkPortAvailabilityOnHosts", () => { |
| 292 | it.effect("checks overlapping hosts sequentially to avoid self-interference", () => |
| 293 | Effect.gen(function* () { |
| 294 | let inFlightCount = 0; |
| 295 | const calls: Array<[number, string]> = []; |
| 296 | |
| 297 | const available = yield* checkPortAvailabilityOnHosts( |
| 298 | 13_773, |
| 299 | ["127.0.0.1", "0.0.0.0", "::"], |
| 300 | (port, host) => |
| 301 | Effect.promise(async () => { |
| 302 | calls.push([port, host]); |
| 303 | inFlightCount += 1; |
| 304 | const overlapped = inFlightCount > 1; |
| 305 | await Promise.resolve(); |
| 306 | inFlightCount -= 1; |
| 307 | return !overlapped; |
| 308 | }), |
| 309 | ); |
| 310 | |
| 311 | assert.equal(available, true); |
| 312 | assert.deepStrictEqual(calls, [ |
| 313 | [13_773, "127.0.0.1"], |
| 314 | [13_773, "0.0.0.0"], |
| 315 | [13_773, "::"], |
| 316 | ]); |
| 317 | }), |
| 318 | ); |
| 319 | }); |
| 320 | |
| 321 | describe("devPortProbeHosts", () => { |
| 322 | it.effect("passes the port role so only the server port sees the bind host", () => |
| 323 | Effect.gen(function* () { |
| 324 | const probed: Array<{ port: number; role: string | undefined }> = []; |
| 325 | |
| 326 | yield* resolveModePortOffsets({ |
| 327 | mode: "dev", |
| 328 | startOffset: 0, |
| 329 | hasExplicitServerPort: false, |
| 330 | hasExplicitDevUrl: false, |
| 331 | checkPortAvailability: (port, role) => { |
| 332 | probed.push({ port, role }); |
| 333 | return Effect.succeed(true); |
| 334 | }, |
| 335 | }); |
| 336 | |
| 337 | assert.deepStrictEqual(probed, [ |
| 338 | { port: 13_773, role: "server" }, |
| 339 | { port: 5733, role: "web" }, |
| 340 | ]); |
| 341 | }), |
| 342 | ); |
| 343 | }); |
| 344 | |
| 345 | describe("resolveModePortOffsets", () => { |
| 346 | it.effect("uses a shared fallback offset for dev mode", () => |
| 347 | Effect.gen(function* () { |
| 348 | const taken = new Set([13773, 5733]); |
| 349 | const offsets = yield* resolveModePortOffsets({ |
| 350 | mode: "dev", |
| 351 | startOffset: 0, |
| 352 | hasExplicitServerPort: false, |
| 353 | hasExplicitDevUrl: false, |
| 354 | checkPortAvailability: (port) => Effect.succeed(!taken.has(port)), |
| 355 | }); |
| 356 | |
| 357 | assert.deepStrictEqual(offsets, { serverOffset: 1, webOffset: 1 }); |
| 358 | }), |
| 359 | ); |
| 360 | |
| 361 | it.effect("keeps server offset stable for dev:web and only shifts web offset", () => |
| 362 | Effect.gen(function* () { |
| 363 | const taken = new Set([5733]); |
| 364 | const offsets = yield* resolveModePortOffsets({ |
| 365 | mode: "dev:web", |
| 366 | startOffset: 0, |
| 367 | hasExplicitServerPort: false, |
| 368 | hasExplicitDevUrl: false, |
| 369 | checkPortAvailability: (port) => Effect.succeed(!taken.has(port)), |
| 370 | }); |
| 371 | |
| 372 | assert.deepStrictEqual(offsets, { serverOffset: 0, webOffset: 1 }); |
| 373 | }), |
| 374 | ); |
| 375 | |
| 376 | it.effect("shifts only server offset for dev:server", () => |
| 377 | Effect.gen(function* () { |
| 378 | const taken = new Set([13773]); |
| 379 | const offsets = yield* resolveModePortOffsets({ |
| 380 | mode: "dev:server", |
| 381 | startOffset: 0, |
| 382 | hasExplicitServerPort: false, |
| 383 | hasExplicitDevUrl: false, |
| 384 | checkPortAvailability: (port) => Effect.succeed(!taken.has(port)), |
| 385 | }); |
| 386 | |
| 387 | assert.deepStrictEqual(offsets, { serverOffset: 1, webOffset: 1 }); |
| 388 | }), |
| 389 | ); |
| 390 | |
| 391 | it.effect("respects explicit dev-url override for dev:web", () => |
| 392 | Effect.gen(function* () { |
| 393 | const offsets = yield* resolveModePortOffsets({ |
| 394 | mode: "dev:web", |
| 395 | startOffset: 0, |
| 396 | hasExplicitServerPort: false, |
| 397 | hasExplicitDevUrl: true, |
| 398 | checkPortAvailability: () => Effect.succeed(false), |
| 399 | }); |
| 400 | |
| 401 | assert.deepStrictEqual(offsets, { serverOffset: 0, webOffset: 0 }); |
| 402 | }), |
| 403 | ); |
| 404 | }); |
| 405 | |
| 406 | describe("runDevRunnerWithInput", () => { |
| 407 | it.effect("preserves process spawn context and the exact platform cause", () => { |
| 408 | const cause = PlatformError.systemError({ |
| 409 | _tag: "NotFound", |
| 410 | module: "ChildProcess", |
| 411 | method: "spawn", |
| 412 | description: "vp was not found", |
| 413 | }); |
| 414 | const spawnerLayer = Layer.succeed( |
| 415 | ChildProcessSpawner.ChildProcessSpawner, |
| 416 | ChildProcessSpawner.make(() => Effect.fail(cause)), |
| 417 | ); |
| 418 | |
| 419 | return Effect.gen(function* () { |
| 420 | const error = yield* runDevRunnerWithInput(devServerInput).pipe( |
| 421 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), |
| 422 | Effect.provideService(HostProcess.Platform, "linux"), |
| 423 | Effect.flip, |
| 424 | ); |
| 425 | |
| 426 | if (error._tag !== "DevRunnerProcessError") { |
| 427 | assert.fail(`Unexpected error: ${error._tag}`); |
| 428 | } |
| 429 | assert.equal(error.operation, "spawn"); |
| 430 | assert.equal(error.mode, "dev:server"); |
| 431 | assert.equal(error.executable, "vp"); |
| 432 | assert.equal(error.argumentCount, 5); |
| 433 | assert.equal(error.shell, false); |
| 434 | assert.equal(error.cause, cause); |
| 435 | assert.ok(!error.message.includes(cause.message)); |
| 436 | assert.notProperty(error, "args"); |
| 437 | assert.notInclude(error.message, "secret-token-value"); |
| 438 | }); |
| 439 | }); |
| 440 | |
| 441 | it.effect.each([ |
| 442 | { built: "release", cargoFails: false, expectBuild: false }, |
| 443 | { built: "debug", cargoFails: false, expectBuild: false }, |
| 444 | { built: undefined, cargoFails: false, expectBuild: true }, |
| 445 | { built: undefined, cargoFails: true, expectBuild: true }, |
| 446 | ] as const)( |
| 447 | "builds the resource monitor only when it is missing: built=$built cargoFails=$cargoFails", |
| 448 | ({ built, cargoFails, expectBuild }) => |
| 449 | Effect.gen(function* () { |
| 450 | const fs = yield* FileSystem.FileSystem; |
| 451 | const spawned: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }> = |
| 452 | []; |
| 453 | const warnings: Array<string> = []; |
| 454 | const spawnerLayer = Layer.succeed( |
| 455 | ChildProcessSpawner.ChildProcessSpawner, |
| 456 | ChildProcessSpawner.make((command) => { |
| 457 | const spawn = command as unknown as { |
| 458 | readonly command: string; |
| 459 | readonly args: ReadonlyArray<string>; |
| 460 | }; |
| 461 | spawned.push({ command: spawn.command, args: spawn.args }); |
| 462 | return spawn.command === "cargo" && cargoFails |
| 463 | ? Effect.succeed(mockProcess(101)) |
| 464 | : Effect.succeed(mockProcess(0)); |
| 465 | }), |
| 466 | ); |
| 467 | |
| 468 | yield* runDevRunnerWithInput(devServerInput).pipe( |
| 469 | Effect.provide([ |
| 470 | emptyConfigLayer, |
| 471 | netServiceLayer, |
| 472 | spawnerLayer, |
| 473 | Logger.layer([ |
| 474 | Logger.make((options) => { |
| 475 | if (options.logLevel === "Warn") warnings.push(String(options.message)); |
| 476 | }), |
| 477 | ]), |
| 478 | ]), |
| 479 | Effect.provideService(FileSystem.FileSystem, { |
| 480 | ...fs, |
| 481 | exists: (file) => |
| 482 | NodePath.basename(file) === "t3-resource-monitor" |
| 483 | ? Effect.succeed( |
| 484 | built !== undefined && NodePath.basename(NodePath.dirname(file)) === built, |
| 485 | ) |
| 486 | : fs.exists(file), |
| 487 | }), |
| 488 | Effect.provideService(HostProcess.Platform, "linux"), |
| 489 | ); |
| 490 | |
| 491 | const cargo = spawned.filter((spawn) => spawn.command === "cargo"); |
| 492 | assert.lengthOf(cargo, expectBuild ? 1 : 0); |
| 493 | if (expectBuild) { |
| 494 | assert.includeMembers([...cargo[0]!.args], ["build", "--locked", "--release"]); |
| 495 | assert.isTrue(cargo[0]!.args.at(-1)!.endsWith("Cargo.toml")); |
| 496 | } |
| 497 | assert.lengthOf(warnings, cargoFails ? 1 : 0); |
| 498 | if (cargoFails) assert.include(warnings[0]!, "vp run build:resource-monitor"); |
| 499 | // A failed build never blocks dev. |
| 500 | assert.equal(spawned.at(-1)?.command, "vp"); |
| 501 | }), |
| 502 | ); |
| 503 | |
| 504 | it.effect("ignores an ambient T3CODE_HOME when no home is resolved", () => |
| 505 | Effect.gen(function* () { |
| 506 | const env = yield* createDevRunnerEnv({ |
| 507 | mode: "dev", |
| 508 | baseEnv: { T3CODE_HOME: "/home/user/.t3" }, |
| 509 | serverOffset: 0, |
| 510 | webOffset: 0, |
| 511 | t3Home: undefined, |
| 512 | browser: undefined, |
| 513 | autoBootstrapProjectFromCwd: undefined, |
| 514 | logWebSocketEvents: undefined, |
| 515 | host: undefined, |
| 516 | port: undefined, |
| 517 | devUrl: undefined, |
| 518 | }); |
| 519 | |
| 520 | assert.equal(env.T3CODE_HOME, undefined); |
| 521 | }), |
| 522 | ); |
| 523 | |
| 524 | it.effect("expands a home-relative T3 home the way the server does", () => |
| 525 | Effect.gen(function* () { |
| 526 | const path = yield* Path.Path; |
| 527 | const homeFor = (t3Home: string) => |
| 528 | createDevRunnerEnv({ |
| 529 | mode: "dev", |
| 530 | baseEnv: {}, |
| 531 | serverOffset: 0, |
| 532 | webOffset: 0, |
| 533 | t3Home, |
| 534 | browser: undefined, |
| 535 | autoBootstrapProjectFromCwd: undefined, |
| 536 | logWebSocketEvents: undefined, |
| 537 | host: undefined, |
| 538 | port: undefined, |
| 539 | devUrl: undefined, |
| 540 | }).pipe( |
| 541 | Effect.provideService(HostProcess.HomeDirectory, "/home/dev"), |
| 542 | Effect.map((env) => env.T3CODE_HOME), |
| 543 | ); |
| 544 | |
| 545 | assert.equal(yield* homeFor("~/t3-dev"), path.join("/home/dev", "t3-dev")); |
| 546 | assert.equal(yield* homeFor("~"), "/home/dev"); |
| 547 | assert.equal(yield* homeFor("~\\t3-dev"), path.join("/home/dev", "t3-dev")); |
| 548 | assert.equal(yield* homeFor(" ~/t3-dev "), path.join("/home/dev", "t3-dev")); |
| 549 | assert.equal(yield* homeFor("~someone/t3-dev"), path.resolve("~someone/t3-dev")); |
| 550 | assert.equal(yield* homeFor("relative/t3-dev"), path.resolve("relative/t3-dev")); |
| 551 | assert.equal(yield* homeFor("/tmp/t3-dev"), path.resolve("/tmp/t3-dev")); |
| 552 | // An explicit ~/.t3 would put state in the live userdata; leaving it implicit selects ~/.t3/dev. |
| 553 | assert.equal(yield* homeFor("~/.t3"), undefined); |
| 554 | assert.equal(yield* homeFor("/home/dev/.t3/"), undefined); |
| 555 | }), |
| 556 | ); |
| 557 | |
| 558 | it.effect("rejects a specific non-loopback --host for browser dev modes", () => { |
| 559 | const spawnerLayer = Layer.succeed( |
| 560 | ChildProcessSpawner.ChildProcessSpawner, |
| 561 | ChildProcessSpawner.make(() => Effect.succeed(mockProcess(0))), |
| 562 | ); |
| 563 | |
| 564 | return Effect.gen(function* () { |
| 565 | const error = yield* runDevRunnerWithInput({ |
| 566 | ...devServerInput, |
| 567 | mode: "dev", |
| 568 | port: undefined, |
| 569 | host: "192.168.1.10", |
| 570 | }).pipe( |
| 571 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), |
| 572 | Effect.provideService(HostProcess.Platform, "linux"), |
| 573 | Effect.flip, |
| 574 | ); |
| 575 | |
| 576 | if (error._tag !== "DevRunnerHostNotProxiableError") { |
| 577 | assert.fail(`Unexpected error: ${error._tag}`); |
| 578 | } |
| 579 | assert.equal(error.mode, "dev"); |
| 580 | assert.equal(error.host, "192.168.1.10"); |
| 581 | assert.include(error.message, "0.0.0.0"); |
| 582 | assert.include(error.message, "--share"); |
| 583 | }); |
| 584 | }); |
| 585 | |
| 586 | // A shared origin means a remote browser, where unbundled dev's |
| 587 | // per-module waterfall pays a tailnet round trip per import level. The |
| 588 | // runner defaults bundled dev on for the spawned stack, but only |
| 589 | // defaults: an explicit T3CODE_BUNDLED_DEV (even "0") must pass through. |
| 590 | describe("--share spawn wiring", () => { |
| 591 | const shareSpawned = (input: { |
| 592 | readonly ambientBundledDev?: string | undefined; |
| 593 | readonly built?: boolean; |
| 594 | readonly host?: string; |
| 595 | }) => |
| 596 | Effect.gen(function* () { |
| 597 | let captured: Record<string, string | undefined> | undefined; |
| 598 | let vpArgs: ReadonlyArray<string> = []; |
| 599 | const tailscaleCalls: Array<ReadonlyArray<string>> = []; |
| 600 | const spawnerLayer = Layer.succeed( |
| 601 | ChildProcessSpawner.ChildProcessSpawner, |
| 602 | ChildProcessSpawner.make((command) => { |
| 603 | const spawned = command as unknown as { |
| 604 | readonly command: string; |
| 605 | readonly args: ReadonlyArray<string>; |
| 606 | readonly options?: { readonly env?: Record<string, string | undefined> }; |
| 607 | }; |
| 608 | if (spawned.command === "vp") { |
| 609 | captured = spawned.options?.env; |
| 610 | vpArgs = spawned.args; |
| 611 | return Effect.succeed(mockProcess(0)); |
| 612 | } |
| 613 | tailscaleCalls.push(spawned.args); |
| 614 | return Effect.succeed( |
| 615 | ChildProcessSpawner.makeHandle({ |
| 616 | pid: ChildProcessSpawner.ProcessId(2), |
| 617 | exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 618 | isRunning: Effect.succeed(false), |
| 619 | kill: () => Effect.void, |
| 620 | unref: Effect.succeed(Effect.void), |
| 621 | stdin: Sink.drain, |
| 622 | stdout: spawned.args.includes("status") |
| 623 | ? Stream.make( |
| 624 | new TextEncoder().encode( |
| 625 | JSON.stringify({ Self: { DNSName: "host.example.ts.net." } }), |
| 626 | ), |
| 627 | ) |
| 628 | : Stream.empty, |
| 629 | stderr: Stream.empty, |
| 630 | all: Stream.empty, |
| 631 | getInputFd: () => Sink.drain, |
| 632 | getOutputFd: () => Stream.empty, |
| 633 | }), |
| 634 | ); |
| 635 | }), |
| 636 | ); |
| 637 | |
| 638 | yield* runDevRunnerWithInput({ |
| 639 | ...devServerInput, |
| 640 | mode: "dev", |
| 641 | port: undefined, |
| 642 | share: true, |
| 643 | built: input.built ?? false, |
| 644 | host: input.host, |
| 645 | }).pipe( |
| 646 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), |
| 647 | Effect.provideService(HostProcess.Platform, "linux"), |
| 648 | Effect.provideService( |
| 649 | HostProcess.Environment, |
| 650 | input.ambientBundledDev === undefined |
| 651 | ? {} |
| 652 | : { T3CODE_BUNDLED_DEV: input.ambientBundledDev }, |
| 653 | ), |
| 654 | ); |
| 655 | |
| 656 | return { env: captured, vpArgs, tailscaleCalls }; |
| 657 | }); |
| 658 | |
| 659 | it.effect("keeps an explicit T3CODE_BUNDLED_DEV=0 opt-out", () => |
| 660 | Effect.gen(function* () { |
| 661 | const { env } = yield* shareSpawned({ ambientBundledDev: "0" }); |
| 662 | assert.equal(env?.T3CODE_BUNDLED_DEV, "0"); |
| 663 | }), |
| 664 | ); |
| 665 | |
| 666 | it.effect("--built serves the backend on the web port's tailnet URL", () => |
| 667 | Effect.gen(function* () { |
| 668 | const { env, vpArgs, tailscaleCalls } = yield* shareSpawned({ built: true }); |
| 669 | assert.include(vpArgs, "dev:built"); |
| 670 | assert.equal(env?.T3CODE_BUILT_DEV, "1"); |
| 671 | assert.equal(env?.T3CODE_BUNDLED_DEV, undefined); |
| 672 | assert.equal(env?.VITE_DEV_SERVER_URL, `https://host.example.ts.net:${env?.PORT}/`); |
| 673 | const serve = tailscaleCalls.find((args) => args.includes("--bg")); |
| 674 | assert.deepEqual(serve?.slice(-2), [ |
| 675 | `--https=${env?.PORT}`, |
| 676 | `http://127.0.0.1:${env?.T3CODE_PORT}`, |
| 677 | ]); |
| 678 | }), |
| 679 | ); |
| 680 | |
| 681 | it.effect("--built --host ::1 targets the backend's IPv6 loopback", () => |
| 682 | Effect.gen(function* () { |
| 683 | const { env, tailscaleCalls } = yield* shareSpawned({ built: true, host: "::1" }); |
| 684 | const serve = tailscaleCalls.find((args) => args.includes("--bg")); |
| 685 | assert.equal(serve?.at(-1), `http://[::1]:${env?.T3CODE_PORT}`); |
| 686 | }), |
| 687 | ); |
| 688 | |
| 689 | // Node resolves localhost to ::1 first, so a 127.0.0.1 target can miss the backend. |
| 690 | it.effect("--built --host localhost keeps the backend's localhost name", () => |
| 691 | Effect.gen(function* () { |
| 692 | const { env, tailscaleCalls } = yield* shareSpawned({ built: true, host: "localhost" }); |
| 693 | const serve = tailscaleCalls.find((args) => args.includes("--bg")); |
| 694 | assert.equal(serve?.at(-1), `http://localhost:${env?.T3CODE_PORT}`); |
| 695 | }), |
| 696 | ); |
| 697 | |
| 698 | it.effect("without --built keeps Vite behind the tailnet URL", () => |
| 699 | Effect.gen(function* () { |
| 700 | const { env, vpArgs, tailscaleCalls } = yield* shareSpawned({}); |
| 701 | assert.include(vpArgs, "dev"); |
| 702 | assert.notInclude(vpArgs, "dev:built"); |
| 703 | assert.equal(env?.T3CODE_BUNDLED_DEV, "1"); |
| 704 | const serve = tailscaleCalls.find((args) => args.includes("--bg")); |
| 705 | assert.equal(serve?.at(-1), `http://localhost:${env?.PORT}`); |
| 706 | }), |
| 707 | ); |
| 708 | }); |
| 709 | |
| 710 | it.effect.each([ |
| 711 | { mode: "dev", share: false }, |
| 712 | { mode: "dev:web", share: true }, |
| 713 | { mode: "dev:desktop", share: true }, |
| 714 | ] as const)("rejects --built for $mode with share=$share", ({ mode, share }) => { |
| 715 | const spawnerLayer = Layer.succeed( |
| 716 | ChildProcessSpawner.ChildProcessSpawner, |
| 717 | ChildProcessSpawner.make(() => Effect.succeed(mockProcess(0))), |
| 718 | ); |
| 719 | |
| 720 | return Effect.gen(function* () { |
| 721 | const error = yield* runDevRunnerWithInput({ |
| 722 | ...devServerInput, |
| 723 | mode, |
| 724 | port: undefined, |
| 725 | share, |
| 726 | built: true, |
| 727 | }).pipe( |
| 728 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), |
| 729 | Effect.provideService(HostProcess.Platform, "linux"), |
| 730 | Effect.flip, |
| 731 | ); |
| 732 | |
| 733 | assert.equal(error._tag, "DevRunnerBuiltNeedsShareError"); |
| 734 | }); |
| 735 | }); |
| 736 | |
| 737 | it.effect("reports non-zero exits without manufacturing a cause", () => { |
| 738 | const spawnerLayer = Layer.succeed( |
| 739 | ChildProcessSpawner.ChildProcessSpawner, |
| 740 | ChildProcessSpawner.make(() => Effect.succeed(mockProcess(17))), |
| 741 | ); |
| 742 | |
| 743 | return Effect.gen(function* () { |
| 744 | const error = yield* runDevRunnerWithInput(devServerInput).pipe( |
| 745 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), |
| 746 | Effect.provideService(HostProcess.Platform, "linux"), |
| 747 | Effect.flip, |
| 748 | ); |
| 749 | |
| 750 | if (error._tag !== "DevRunnerProcessExitError") { |
| 751 | assert.fail(`Unexpected error: ${error._tag}`); |
| 752 | } |
| 753 | assert.equal(error.mode, "dev:server"); |
| 754 | assert.equal(error.executable, "vp"); |
| 755 | assert.equal(error.argumentCount, 5); |
| 756 | assert.equal(error.shell, false); |
| 757 | assert.equal(error.exitCode, 17); |
| 758 | assert.ok(!("cause" in error)); |
| 759 | assert.notProperty(error, "args"); |
| 760 | assert.notInclude(error.message, "secret-token-value"); |
| 761 | }); |
| 762 | }); |
| 763 | |
| 764 | describe("t3 home precedence", () => { |
| 765 | const makeWorktree = Effect.acquireRelease( |
| 766 | Effect.sync(() => { |
| 767 | const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-devrunner-")); |
| 768 | NodeFS.writeFileSync( |
| 769 | NodePath.join(root, ".git"), |
| 770 | "gitdir: /elsewhere/.git/worktrees/x\n", |
| 771 | ); |
| 772 | return root; |
| 773 | }), |
| 774 | (root) => Effect.sync(() => NodeFS.rmSync(root, { recursive: true, force: true })), |
| 775 | ); |
| 776 | |
| 777 | const spawnedHome = (input: { |
| 778 | readonly t3Home: string | undefined; |
| 779 | readonly cwd: string; |
| 780 | readonly ambientHome: string | undefined; |
| 781 | }) => |
| 782 | Effect.gen(function* () { |
| 783 | let captured: Record<string, string | undefined> | undefined; |
| 784 | const spawnerLayer = Layer.succeed( |
| 785 | ChildProcessSpawner.ChildProcessSpawner, |
| 786 | ChildProcessSpawner.make((command) => { |
| 787 | captured = ( |
| 788 | command as { |
| 789 | readonly options?: { readonly env?: Record<string, string | undefined> }; |
| 790 | } |
| 791 | ).options?.env; |
| 792 | return Effect.succeed(mockProcess(0)); |
| 793 | }), |
| 794 | ); |
| 795 | |
| 796 | yield* runDevRunnerWithInput({ ...devServerInput, t3Home: input.t3Home }).pipe( |
| 797 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), |
| 798 | Effect.provideService(HostProcess.Platform, "linux"), |
| 799 | Effect.provideService(HostProcess.WorkingDirectory, input.cwd), |
| 800 | // No ~/.t3 here, so these runs never seed from this machine's install. |
| 801 | Effect.provideService(HostProcess.HomeDirectory, NodePath.join(input.cwd, "home")), |
| 802 | Effect.provideService( |
| 803 | HostProcess.Environment, |
| 804 | input.ambientHome === undefined ? {} : { T3CODE_HOME: input.ambientHome }, |
| 805 | ), |
| 806 | ); |
| 807 | |
| 808 | return captured?.T3CODE_HOME; |
| 809 | }); |
| 810 | |
| 811 | it.effect("prefers an explicit --home-dir over the worktree default", () => |
| 812 | Effect.gen(function* () { |
| 813 | const path = yield* Path.Path; |
| 814 | const root = yield* makeWorktree; |
| 815 | const home = yield* spawnedHome({ |
| 816 | t3Home: "/tmp/explicit-home", |
| 817 | cwd: root, |
| 818 | ambientHome: "/home/user/.t3", |
| 819 | }); |
| 820 | assert.equal(home, path.resolve("/tmp/explicit-home")); |
| 821 | }).pipe(Effect.scoped), |
| 822 | ); |
| 823 | |
| 824 | it.effect("treats a blank --home-dir as unset rather than as a selection", () => |
| 825 | Effect.gen(function* () { |
| 826 | const path = yield* Path.Path; |
| 827 | const root = yield* makeWorktree; |
| 828 | const home = yield* spawnedHome({ |
| 829 | t3Home: " ", |
| 830 | cwd: root, |
| 831 | ambientHome: "/home/user/.t3", |
| 832 | }); |
| 833 | assert.equal(home, path.join(path.resolve(root), ".t3")); |
| 834 | }).pipe(Effect.scoped), |
| 835 | ); |
| 836 | |
| 837 | it.effect("prefers the worktree .t3 over an ambient T3CODE_HOME", () => |
| 838 | Effect.gen(function* () { |
| 839 | const path = yield* Path.Path; |
| 840 | const root = yield* makeWorktree; |
| 841 | const home = yield* spawnedHome({ |
| 842 | t3Home: undefined, |
| 843 | cwd: root, |
| 844 | ambientHome: "/home/user/.t3", |
| 845 | }); |
| 846 | assert.equal(home, path.join(path.resolve(root), ".t3")); |
| 847 | }).pipe(Effect.scoped), |
| 848 | ); |
| 849 | |
| 850 | it.effect("seeds a worktree database from ~/.t3 only when it has none", () => |
| 851 | Effect.gen(function* () { |
| 852 | const path = yield* Path.Path; |
| 853 | const root = yield* makeWorktree; |
| 854 | const home = path.join(root, "home"); |
| 855 | NodeFS.mkdirSync(path.join(home, ".t3", "userdata"), { recursive: true }); |
| 856 | NodeFS.writeFileSync(path.join(home, ".t3", "userdata", "statev2.sqlite"), ""); |
| 857 | |
| 858 | const spawnedArgs = (seed: boolean | undefined) => |
| 859 | Effect.gen(function* () { |
| 860 | const spawned: Array<ReadonlyArray<string>> = []; |
| 861 | const spawnerLayer = Layer.succeed( |
| 862 | ChildProcessSpawner.ChildProcessSpawner, |
| 863 | ChildProcessSpawner.make((command) => { |
| 864 | spawned.push( |
| 865 | (command as unknown as { readonly args: ReadonlyArray<string> }).args, |
| 866 | ); |
| 867 | return Effect.succeed(mockProcess(0)); |
| 868 | }), |
| 869 | ); |
| 870 | yield* runDevRunnerWithInput({ ...devServerInput, t3Home: undefined, seed }).pipe( |
| 871 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), |
| 872 | Effect.provideService(HostProcess.Platform, "linux"), |
| 873 | Effect.provideService(HostProcess.WorkingDirectory, root), |
| 874 | Effect.provideService(HostProcess.HomeDirectory, home), |
| 875 | Effect.provideService(HostProcess.Environment, {}), |
| 876 | ); |
| 877 | return spawned.filter((args) => |
| 878 | args.some((arg) => arg.endsWith("migrate-dev-db.ts")), |
| 879 | ); |
| 880 | }); |
| 881 | |
| 882 | assert.deepStrictEqual(yield* spawnedArgs(false), []); |
| 883 | const [seedArgs] = yield* spawnedArgs(undefined); |
| 884 | assert.includeMembers([...(seedArgs ?? [])], ["--base-dir", path.join(root, ".t3")]); |
| 885 | |
| 886 | // Either database means the worktree already has data. |
| 887 | for (const name of ["state.sqlite", "statev2.sqlite"]) { |
| 888 | const file = path.join(root, ".t3", "userdata", name); |
| 889 | NodeFS.mkdirSync(path.dirname(file), { recursive: true }); |
| 890 | NodeFS.writeFileSync(file, ""); |
| 891 | assert.deepStrictEqual(yield* spawnedArgs(undefined), []); |
| 892 | NodeFS.rmSync(file); |
| 893 | } |
| 894 | }).pipe(Effect.scoped), |
| 895 | ); |
| 896 | |
| 897 | it.effect.each([ |
| 898 | { sourceBytes: 500 * 1024 * 1024 - 1, shouldSeed: true }, |
| 899 | { sourceBytes: 500 * 1024 * 1024, shouldSeed: true }, |
| 900 | { sourceBytes: 500 * 1024 * 1024 + 1, shouldSeed: false }, |
| 901 | ])( |
| 902 | "checks source size before seeding an empty worktree: $sourceBytes bytes", |
| 903 | ({ sourceBytes, shouldSeed }) => |
| 904 | Effect.gen(function* () { |
| 905 | const fs = yield* FileSystem.FileSystem; |
| 906 | const path = yield* Path.Path; |
| 907 | const root = yield* makeWorktree; |
| 908 | const home = path.join(root, "home"); |
| 909 | const source = path.join(home, ".t3", "userdata", "statev2.sqlite"); |
| 910 | NodeFS.mkdirSync(path.dirname(source), { recursive: true }); |
| 911 | NodeFS.writeFileSync(source, ""); |
| 912 | const spawned: Array<ReadonlyArray<string>> = []; |
| 913 | const warnings: Array<string> = []; |
| 914 | const spawnerLayer = Layer.succeed( |
| 915 | ChildProcessSpawner.ChildProcessSpawner, |
| 916 | ChildProcessSpawner.make((command) => { |
| 917 | spawned.push((command as unknown as { readonly args: ReadonlyArray<string> }).args); |
| 918 | return Effect.succeed(mockProcess(0)); |
| 919 | }), |
| 920 | ); |
| 921 | |
| 922 | yield* runDevRunnerWithInput({ ...devServerInput, t3Home: undefined }).pipe( |
| 923 | Effect.provide([ |
| 924 | emptyConfigLayer, |
| 925 | netServiceLayer, |
| 926 | spawnerLayer, |
| 927 | Logger.layer([ |
| 928 | Logger.make((options) => { |
| 929 | if (options.logLevel === "Warn") warnings.push(String(options.message)); |
| 930 | }), |
| 931 | ]), |
| 932 | ]), |
| 933 | Effect.provideService(FileSystem.FileSystem, { |
| 934 | ...fs, |
| 935 | stat: (file) => |
| 936 | fs |
| 937 | .stat(file) |
| 938 | .pipe( |
| 939 | Effect.map((info) => |
| 940 | file === source ? { ...info, size: ByteSize.bytes(sourceBytes) } : info, |
| 941 | ), |
| 942 | ), |
| 943 | }), |
| 944 | Effect.provideService(HostProcess.Platform, "linux"), |
| 945 | Effect.provideService(HostProcess.WorkingDirectory, root), |
| 946 | Effect.provideService(HostProcess.HomeDirectory, home), |
| 947 | Effect.provideService(HostProcess.Environment, {}), |
| 948 | ); |
| 949 | |
| 950 | assert.equal( |
| 951 | spawned.some((args) => args.some((arg) => arg.endsWith("migrate-dev-db.ts"))), |
| 952 | shouldSeed, |
| 953 | ); |
| 954 | assert.deepStrictEqual(spawned.at(-1), [ |
| 955 | "run", |
| 956 | "--filter=t3", |
| 957 | "dev", |
| 958 | ...devServerInput.runArgs, |
| 959 | ]); |
| 960 | if (shouldSeed) { |
| 961 | assert.deepStrictEqual(warnings, []); |
| 962 | } else { |
| 963 | assert.lengthOf(warnings, 1); |
| 964 | assert.include(warnings[0]!, source); |
| 965 | assert.include(warnings[0]!, "skipping automatic database seeding"); |
| 966 | assert.include(warnings[0]!, "vp run migrate-dev-db"); |
| 967 | } |
| 968 | }).pipe(Effect.scoped), |
| 969 | ); |
| 970 | |
| 971 | it.effect("falls back to an ambient T3CODE_HOME outside a worktree", () => |
| 972 | Effect.gen(function* () { |
| 973 | const path = yield* Path.Path; |
| 974 | const home = yield* spawnedHome({ |
| 975 | t3Home: undefined, |
| 976 | cwd: NodeOS.tmpdir(), |
| 977 | ambientHome: "/home/user/.t3", |
| 978 | }); |
| 979 | assert.equal(home, path.resolve("/home/user/.t3")); |
| 980 | }), |
| 981 | ); |
| 982 | }); |
| 983 | }); |
| 984 | }); |