|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - builds a real worktree layout on disk. |
| 2 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | +import * as NodeFS from "node:fs"; |
| 4 | +import * as NodeOS from "node:os"; |
| 5 | +import * as NodePath from "node:path"; |
| 6 | +import * as NetService from "@t3tools/shared/Net"; |
| 7 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 8 | +import * as ConfigProvider from "effect/ConfigProvider"; |
| 9 | +import { assert, it } from "@effect/vitest"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as Layer from "effect/Layer"; |
| 12 | +import * as Sink from "effect/Sink"; |
| 13 | +import * as Stream from "effect/Stream"; |
| 14 | +import { ChildProcessSpawner } from "effect/process"; |
| 15 | + |
| 16 | +import { runDevRunnerWithInput } from "./dev-runner.ts"; |
| 17 | + |
| 18 | +const exitedProcess = ChildProcessSpawner.makeHandle({ |
| 19 | + pid: ChildProcessSpawner.ProcessId(1), |
| 20 | + exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 21 | + isRunning: Effect.succeed(false), |
| 22 | + kill: () => Effect.void, |
| 23 | + unref: Effect.succeed(Effect.void), |
| 24 | + stdin: Sink.drain, |
| 25 | + stdout: Stream.empty, |
| 26 | + stderr: Stream.empty, |
| 27 | + all: Stream.empty, |
| 28 | + getInputFd: () => Sink.drain, |
| 29 | + getOutputFd: () => Stream.empty, |
| 30 | +}); |
| 31 | + |
| 32 | +const makeWorktree = Effect.acquireRelease( |
| 33 | + Effect.sync(() => { |
| 34 | + const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-devrunner-fork-")); |
| 35 | + NodeFS.writeFileSync(NodePath.join(root, ".git"), "gitdir: /elsewhere/.git/worktrees/x\n"); |
| 36 | + return root; |
| 37 | + }), |
| 38 | + (root) => Effect.sync(() => NodeFS.rmSync(root, { recursive: true, force: true })), |
| 39 | +); |
| 40 | + |
| 41 | +/** The environment dev-runner hands Vite+ for one `dev:server` run. */ |
| 42 | +const spawnedEnv = (input: { |
| 43 | + readonly cwd: string; |
| 44 | + readonly t3Home?: string; |
| 45 | + readonly port?: number; |
| 46 | + readonly ambient?: Record<string, string>; |
| 47 | +}) => |
| 48 | + Effect.gen(function* () { |
| 49 | + let captured: Record<string, string | undefined> | undefined; |
| 50 | + const spawner = ChildProcessSpawner.make((command) => { |
| 51 | + captured = (command as { readonly options?: { readonly env?: Record<string, string> } }) |
| 52 | + .options?.env; |
| 53 | + return Effect.succeed(exitedProcess); |
| 54 | + }); |
| 55 | + yield* runDevRunnerWithInput({ |
| 56 | + mode: "dev:server", |
| 57 | + t3Home: input.t3Home, |
| 58 | + browser: undefined, |
| 59 | + autoBootstrapProjectFromCwd: undefined, |
| 60 | + logWebSocketEvents: undefined, |
| 61 | + host: undefined, |
| 62 | + port: input.port, |
| 63 | + devUrl: undefined, |
| 64 | + dryRun: false, |
| 65 | + share: false, |
| 66 | + seed: false, |
| 67 | + built: false, |
| 68 | + runArgs: [], |
| 69 | + }).pipe( |
| 70 | + Effect.provide( |
| 71 | + Layer.mergeAll( |
| 72 | + ConfigProvider.layer(ConfigProvider.fromEnv({ env: input.ambient ?? {} })), |
| 73 | + Layer.succeed(NetService.NetService, { |
| 74 | + canListenOnHost: () => Effect.succeed(true), |
| 75 | + isPortAvailableOnLoopback: () => Effect.succeed(true), |
| 76 | + hasListenerOnHost: () => Effect.succeed(false), |
| 77 | + reserveLoopbackPort: () => Effect.succeed(49_152), |
| 78 | + findAvailablePort: (port) => Effect.succeed(port), |
| 79 | + }), |
| 80 | + Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 81 | + ), |
| 82 | + ), |
| 83 | + Effect.provideService(HostProcess.Platform, "linux"), |
| 84 | + Effect.provideService(HostProcess.WorkingDirectory, input.cwd), |
| 85 | + Effect.provideService(HostProcess.HomeDirectory, input.cwd), |
| 86 | + Effect.provideService(HostProcess.Environment, input.ambient ?? {}), |
| 87 | + ); |
| 88 | + return captured; |
| 89 | + }); |
| 90 | + |
| 91 | +it.layer(NodeServices.layer)("dev-runner (fork)", (it) => { |
| 92 | + it.effect("a worktree ignores the live install's ambient T3CODE_PORT; --port still wins", () => |
| 93 | + Effect.gen(function* () { |
| 94 | + const cwd = yield* makeWorktree; |
| 95 | + const ambient = { T3CODE_PORT: "3773" }; |
| 96 | + assert.notEqual((yield* spawnedEnv({ cwd, ambient }))?.T3CODE_PORT, "3773"); |
| 97 | + assert.equal((yield* spawnedEnv({ cwd, ambient, port: 4321 }))?.T3CODE_PORT, "4321"); |
| 98 | + }).pipe(Effect.scoped), |
| 99 | + ); |
| 100 | + |
| 101 | + it.effect("keeps an ambient T3CODE_PORT outside a worktree", () => |
| 102 | + Effect.gen(function* () { |
| 103 | + const env = yield* spawnedEnv({ cwd: NodeOS.tmpdir(), ambient: { T3CODE_PORT: "3773" } }); |
| 104 | + assert.equal(env?.T3CODE_PORT, "3773"); |
| 105 | + }), |
| 106 | + ); |
| 107 | + it.effect("gives worktree dev Electron its own profile unless one is set", () => |
| 108 | + Effect.gen(function* () { |
| 109 | + const cwd = yield* makeWorktree; |
| 110 | + const own = yield* spawnedEnv({ cwd }); |
| 111 | + assert.equal(own?.T3CODE_DEV_ELECTRON_USER_DATA, NodePath.join(cwd, ".t3", "electron")); |
| 112 | + const preset = { T3CODE_DEV_ELECTRON_USER_DATA: "/tmp/mine" }; |
| 113 | + const kept = yield* spawnedEnv({ cwd, ambient: preset }); |
| 114 | + assert.equal(kept?.T3CODE_DEV_ELECTRON_USER_DATA, "/tmp/mine"); |
| 115 | + }).pipe(Effect.scoped), |
| 116 | + ); |
| 117 | +}); |
0 commit comments