|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + ProjectId, |
| 5 | + ThreadId, |
| 6 | + type OrchestrationProjectShell, |
| 7 | + type OrchestrationV2AppThread, |
| 8 | + type OrchestrationV2ThreadShellSnapshot, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import * as Deferred from "effect/Deferred"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as Fiber from "effect/Fiber"; |
| 13 | +import * as Layer from "effect/Layer"; |
| 14 | +import * as Option from "effect/Option"; |
| 15 | +import { expect } from "vite-plus/test"; |
| 16 | + |
| 17 | +import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; |
| 18 | +import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; |
| 19 | +import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; |
| 20 | +import * as ProcessRunner from "../processRunner.ts"; |
| 21 | +import * as ServerSettings from "../serverSettings.ts"; |
| 22 | +import * as TerminalManager from "../terminal/Manager.ts"; |
| 23 | +import { |
| 24 | + createManager, |
| 25 | + FakeProcessRunner, |
| 26 | + processResult, |
| 27 | +} from "../terminal/Manager.fork-test-harness.ts"; |
| 28 | +import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; |
| 29 | +import { makeSettledLaneReactor } from "../zmux/SettledLaneSessions.fork.ts"; |
| 30 | +import * as Binder from "../zmux/ZmuxSessionBinder.ts"; |
| 31 | +import * as ProjectService from "./ProjectService.ts"; |
| 32 | +import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts"; |
| 33 | + |
| 34 | +const threadId = ThreadId.make("thread:hook-lifecycle"); |
| 35 | +const projectId = ProjectId.make("project:hook-lifecycle"); |
| 36 | +const identity = { |
| 37 | + target: "fixture/feature", |
| 38 | + nativeId: "$22", |
| 39 | + serverId: "123:456", |
| 40 | + createdAt: 123, |
| 41 | +}; |
| 42 | + |
| 43 | +const harness = Effect.gen(function* () { |
| 44 | + let available = true; |
| 45 | + let settled = true; |
| 46 | + let busy = true; |
| 47 | + let beforeOpen: Effect.Effect<void> = Effect.void; |
| 48 | + const processRunner = new FakeProcessRunner(() => |
| 49 | + Effect.succeed( |
| 50 | + processResult({ |
| 51 | + stdout: available |
| 52 | + ? '{"workspace":"fixture","session":"feature","target":"fixture/feature","tmuxName":"zws_fixture__feature","nativeId":"$22","serverId":"123:456","createdAt":123,"state":"live","match":"worktree"}' |
| 53 | + : '{"status":"not-found"}', |
| 54 | + }), |
| 55 | + ), |
| 56 | + ); |
| 57 | + const fixture = yield* createManager(20, { |
| 58 | + terminalSessionMode: "zmux", |
| 59 | + shellResolver: () => "/bin/bash", |
| 60 | + subprocessInspector: () => |
| 61 | + Effect.succeed({ |
| 62 | + hasRunningSubprocess: busy, |
| 63 | + childCommand: busy ? "hook" : null, |
| 64 | + processIds: [], |
| 65 | + }), |
| 66 | + resolveOnlyForSettledThread: () => Effect.sync(() => settled), |
| 67 | + ensureZmuxSession: () => |
| 68 | + Effect.succeed({ |
| 69 | + status: "ensured", |
| 70 | + target: "fixture/feature", |
| 71 | + workspace: "fixture", |
| 72 | + session: "feature", |
| 73 | + }), |
| 74 | + }).pipe(Effect.provideService(ProcessRunner.ProcessRunner, processRunner.service)); |
| 75 | + const patched = new Set<number>(); |
| 76 | + const acknowledgeKills = () => { |
| 77 | + for (const process of fixture.ptyAdapter.processes) { |
| 78 | + if (patched.has(process.pid)) continue; |
| 79 | + patched.add(process.pid); |
| 80 | + const kill = process.kill.bind(process); |
| 81 | + process.kill = (signal) => { |
| 82 | + kill(signal); |
| 83 | + process.emitExit({ exitCode: 0, signal: 15 }); |
| 84 | + }; |
| 85 | + } |
| 86 | + }; |
| 87 | + const terminals = TerminalManager.TerminalManager.of({ |
| 88 | + ...fixture.manager, |
| 89 | + open: (input) => |
| 90 | + beforeOpen.pipe( |
| 91 | + Effect.andThen(fixture.manager.open(input)), |
| 92 | + Effect.tap(() => Effect.sync(acknowledgeKills)), |
| 93 | + ), |
| 94 | + }); |
| 95 | + const project = { |
| 96 | + id: projectId, |
| 97 | + workspaceRoot: fixture.baseDir, |
| 98 | + scripts: [ |
| 99 | + { |
| 100 | + id: "setup", |
| 101 | + name: "Setup", |
| 102 | + command: "printf setup", |
| 103 | + icon: "configure", |
| 104 | + runOnWorktreeCreate: true, |
| 105 | + }, |
| 106 | + { |
| 107 | + id: "hook", |
| 108 | + name: "Settle", |
| 109 | + command: "printf hook", |
| 110 | + icon: "configure", |
| 111 | + runOnWorktreeCreate: false, |
| 112 | + runOnSettle: true, |
| 113 | + }, |
| 114 | + ], |
| 115 | + } satisfies NonNullable<ProjectSetupScriptRunner.ProjectSetupScriptRunnerInput["project"]>; |
| 116 | + const thread = { |
| 117 | + id: threadId, |
| 118 | + projectId, |
| 119 | + worktreePath: process.cwd(), |
| 120 | + archivedAt: null, |
| 121 | + settledOverride: "settled", |
| 122 | + } as OrchestrationV2AppThread; |
| 123 | + const dependencies = Layer.mergeAll( |
| 124 | + Layer.succeed(TerminalManager.TerminalManager, terminals), |
| 125 | + ServerSettings.layerTest(), |
| 126 | + Layer.mock(ProjectService.ProjectService)({}), |
| 127 | + Layer.mock(ProjectStore.ProjectStoreV2)({ |
| 128 | + getShell: () => |
| 129 | + Effect.succeed( |
| 130 | + Option.some({ workspaceRoot: fixture.baseDir } as OrchestrationProjectShell), |
| 131 | + ), |
| 132 | + }), |
| 133 | + Layer.mock(ProjectionStore.ProjectionStoreV2)({ getThread: () => Effect.succeed(thread) }), |
| 134 | + Layer.mock(Orchestrator.OrchestratorV2)({ |
| 135 | + getShellSnapshot: () => |
| 136 | + Effect.succeed({ threads: [thread] } as unknown as OrchestrationV2ThreadShellSnapshot), |
| 137 | + }), |
| 138 | + Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({ |
| 139 | + resolve: ({ cwd }) => |
| 140 | + Effect.succeed({ repository: { rootPath: cwd } } as VcsDriverRegistry.VcsDriverHandle), |
| 141 | + }), |
| 142 | + Layer.mock(Binder.ZmuxSessionBinder)({ |
| 143 | + prepareUnbind: () => Effect.succeed({ status: "prepared", identity }), |
| 144 | + resolve: () => |
| 145 | + Effect.succeed({ status: "resolved", target: "fixture/main", match: "workspace-main" }), |
| 146 | + unbind: () => |
| 147 | + Effect.sync(() => { |
| 148 | + available = false; |
| 149 | + return { status: "unbound", target: identity.target } as const; |
| 150 | + }), |
| 151 | + }), |
| 152 | + ); |
| 153 | + const reactor = yield* makeSettledLaneReactor.pipe(Effect.provide(dependencies)); |
| 154 | + const runner = yield* ProjectSetupScriptRunner.make.pipe(Effect.provide(dependencies)); |
| 155 | + // The runner types once the shell's line editor starts, so the fake shell signals it. |
| 156 | + const runHook = (trigger: "setup" | "settle" = "settle") => |
| 157 | + Effect.gen(function* () { |
| 158 | + const run = yield* runner.runForThread({ |
| 159 | + threadId, |
| 160 | + projectId, |
| 161 | + project, |
| 162 | + worktreePath: process.cwd(), |
| 163 | + trigger, |
| 164 | + observeCompletion: {}, |
| 165 | + }); |
| 166 | + const shell = fixture.ptyAdapter.processes.at(-1)!; |
| 167 | + const typed = Promise.withResolvers<void>(); |
| 168 | + const write = shell.write.bind(shell); |
| 169 | + shell.write = (data) => { |
| 170 | + write(data); |
| 171 | + typed.resolve(); |
| 172 | + }; |
| 173 | + shell.emitData("fixture$ \x1b[?2004h"); |
| 174 | + yield* Effect.promise(() => typed.promise); |
| 175 | + return run; |
| 176 | + }); |
| 177 | + const finishHook = ( |
| 178 | + run: ProjectSetupScriptRunner.ProjectSetupScriptRunnerResult, |
| 179 | + exitCode: number, |
| 180 | + ) => |
| 181 | + Effect.gen(function* () { |
| 182 | + if (run.status !== "started" || !run.completion) |
| 183 | + return yield* Effect.die("hook did not start with observation"); |
| 184 | + const process = fixture.ptyAdapter.processes.at(-1)!; |
| 185 | + const sentinel = /(__T3_SETUP_DONE___\w+:)/.exec(process.writes.at(-1) ?? "")?.[1]; |
| 186 | + if (!sentinel) return yield* Effect.die("missing completion sentinel"); |
| 187 | + busy = false; |
| 188 | + const completion = yield* Effect.forkChild(run.completion); |
| 189 | + process.emitData(`\r\n${sentinel}${exitCode}\r\nfixture$ `); |
| 190 | + return yield* Fiber.join(completion); |
| 191 | + }); |
| 192 | + return { |
| 193 | + fixture, |
| 194 | + processRunner, |
| 195 | + runHook, |
| 196 | + finishHook, |
| 197 | + cleanup: reactor.reconcile(threadId), |
| 198 | + inventory: () => (available ? ["fixture/main", "fixture/feature"] : ["fixture/main"]), |
| 199 | + setSettled: (value: boolean) => { |
| 200 | + settled = value; |
| 201 | + }, |
| 202 | + setBeforeOpen: (effect: Effect.Effect<void>) => { |
| 203 | + beforeOpen = effect; |
| 204 | + }, |
| 205 | + openViewer: Effect.gen(function* () { |
| 206 | + settled = false; |
| 207 | + const release = yield* fixture.manager.attachStream( |
| 208 | + { |
| 209 | + threadId, |
| 210 | + terminalId: "viewer", |
| 211 | + cwd: process.cwd(), |
| 212 | + worktreePath: process.cwd(), |
| 213 | + cols: 100, |
| 214 | + rows: 24, |
| 215 | + }, |
| 216 | + () => Effect.void, |
| 217 | + ); |
| 218 | + acknowledgeKills(); |
| 219 | + settled = true; |
| 220 | + return release; |
| 221 | + }), |
| 222 | + }; |
| 223 | +}); |
| 224 | + |
| 225 | +it.layer(NodeServices.layer, { excludeTestServices: true })( |
| 226 | + "settle-hook session teardown", |
| 227 | + (it) => { |
| 228 | + it.effect( |
| 229 | + "cleanup can finish before the configured hook opens without preventing completion", |
| 230 | + () => |
| 231 | + Effect.gen(function* () { |
| 232 | + const h = yield* harness; |
| 233 | + const opening = yield* Deferred.make<void>(); |
| 234 | + const allowOpen = yield* Deferred.make<void>(); |
| 235 | + h.setBeforeOpen( |
| 236 | + Deferred.succeed(opening, undefined).pipe(Effect.andThen(Deferred.await(allowOpen))), |
| 237 | + ); |
| 238 | + const hook = yield* Effect.forkChild(h.runHook()); |
| 239 | + yield* Deferred.await(opening); |
| 240 | + yield* h.cleanup; |
| 241 | + expect(h.inventory()).toEqual(["fixture/main"]); |
| 242 | + yield* Deferred.succeed(allowOpen, undefined); |
| 243 | + const run = yield* Fiber.join(hook); |
| 244 | + expect(h.fixture.ptyAdapter.spawnInputs.at(-1)?.shell).toBe("/bin/bash"); |
| 245 | + expect(h.processRunner.inputs.filter((input) => input.command === "zmux")).toEqual([]); |
| 246 | + expect((yield* h.finishHook(run, 0)).exitCode).toBe(0); |
| 247 | + expect((yield* h.fixture.getEvents).some((event) => event.type === "closed")).toBe(true); |
| 248 | + }), |
| 249 | + ); |
| 250 | + |
| 251 | + it.effect( |
| 252 | + "cleanup after hook start releases the managed viewer but keeps hook observation alive", |
| 253 | + () => |
| 254 | + Effect.gen(function* () { |
| 255 | + const h = yield* harness; |
| 256 | + const releaseViewer = yield* h.openViewer; |
| 257 | + const run = yield* h.runHook(); |
| 258 | + const hook = h.fixture.ptyAdapter.processes.at(-1)!; |
| 259 | + const completion = |
| 260 | + run.status === "started" && run.completion |
| 261 | + ? yield* Effect.forkChild(run.completion) |
| 262 | + : yield* Effect.die("missing hook completion"); |
| 263 | + yield* h.cleanup; |
| 264 | + expect(h.inventory()).toEqual(["fixture/main"]); |
| 265 | + expect(h.fixture.ptyAdapter.processes[0]?.killSignals).toContain("SIGTERM"); |
| 266 | + expect(hook.killSignals).toEqual([]); |
| 267 | + expect(hook.hasDataListener()).toBe(true); |
| 268 | + expect(completion.pollUnsafe()).toBeUndefined(); |
| 269 | + // The observer is evaluated once; its completion effect is passed to the helper. |
| 270 | + if (run.status !== "started") return yield* Effect.die("missing hook"); |
| 271 | + expect( |
| 272 | + (yield* h.finishHook({ ...run, completion: Fiber.join(completion) }, 0)).exitCode, |
| 273 | + ).toBe(0); |
| 274 | + releaseViewer(); |
| 275 | + }), |
| 276 | + ); |
| 277 | + |
| 278 | + it.effect("failed hook remains inspectable after session removal", () => |
| 279 | + Effect.gen(function* () { |
| 280 | + const h = yield* harness; |
| 281 | + const run = yield* h.runHook(); |
| 282 | + yield* h.cleanup; |
| 283 | + expect((yield* h.finishHook(run, 7)).exitCode).toBe(7); |
| 284 | + expect(h.fixture.ptyAdapter.processes.at(-1)?.killSignals).toEqual([]); |
| 285 | + expect(h.inventory()).toEqual(["fixture/main"]); |
| 286 | + yield* h.fixture.manager.close({ threadId }); |
| 287 | + }), |
| 288 | + ); |
| 289 | + |
| 290 | + it.effect("setup scripts still attach to the configured managed session", () => |
| 291 | + Effect.gen(function* () { |
| 292 | + const h = yield* harness; |
| 293 | + h.setSettled(false); |
| 294 | + yield* h.runHook("setup"); |
| 295 | + expect(h.fixture.ptyAdapter.spawnInputs[0]?.args).toContain("open"); |
| 296 | + yield* h.fixture.manager.close({ threadId }); |
| 297 | + }), |
| 298 | + ); |
| 299 | + }, |
| 300 | +); |
0 commit comments