|
| 1 | +import type { KeepHostAwakeMode, OrchestrationV2DomainEvent } from "@t3tools/contracts"; |
| 2 | +import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts"; |
| 3 | +import { describe, expect, it } from "@effect/vitest"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 7 | +import * as Queue from "effect/Queue"; |
| 8 | +import * as Ref from "effect/Ref"; |
| 9 | +import * as Sink from "effect/Sink"; |
| 10 | +import * as Stream from "effect/Stream"; |
| 11 | + |
| 12 | +import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; |
| 13 | +import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; |
| 14 | +import * as ServerSettings from "../serverSettings.ts"; |
| 15 | +import * as HostWakeLock from "./HostWakeLock.ts"; |
| 16 | + |
| 17 | +const runUpdated = { type: "run.updated" } as OrchestrationV2DomainEvent; |
| 18 | +const turnItemUpdated = { type: "turn-item.updated" } as OrchestrationV2DomainEvent; |
| 19 | + |
| 20 | +const setup = Effect.fn("HostWakeLockTest.setup")(function* (input: { |
| 21 | + readonly mode: KeepHostAwakeMode; |
| 22 | + readonly working?: boolean; |
| 23 | + /** Helpers exit by themselves instead of holding until killed. */ |
| 24 | + readonly exitsEarly?: boolean; |
| 25 | +}) { |
| 26 | + const mode = yield* Ref.make(input.mode); |
| 27 | + const working = yield* Ref.make(input.working ?? false); |
| 28 | + const workingReads = yield* Ref.make(0); |
| 29 | + const spawns = yield* Ref.make(0); |
| 30 | + const spawned = yield* Queue.unbounded<void>(); |
| 31 | + const released = yield* Queue.unbounded<void>(); |
| 32 | + const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>(); |
| 33 | + const settingsChanges = yield* Queue.unbounded<KeepHostAwakeMode>(); |
| 34 | + |
| 35 | + const dependencies = Layer.mergeAll( |
| 36 | + Layer.mock(Orchestrator.OrchestratorV2)({ streamDomainEvents: Stream.fromQueue(events) }), |
| 37 | + Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 38 | + hasWorkingRuns: () => |
| 39 | + Ref.update(workingReads, (reads) => reads + 1).pipe(Effect.andThen(Ref.get(working))), |
| 40 | + }), |
| 41 | + Layer.mock(ServerSettings.ServerSettingsService)({ |
| 42 | + getSettings: Ref.get(mode).pipe( |
| 43 | + Effect.map((keepHostAwake) => ({ ...DEFAULT_SERVER_SETTINGS, keepHostAwake })), |
| 44 | + ), |
| 45 | + subscribeChanges: Effect.succeed( |
| 46 | + Stream.fromQueue(settingsChanges).pipe( |
| 47 | + Stream.map((keepHostAwake) => ({ ...DEFAULT_SERVER_SETTINGS, keepHostAwake })), |
| 48 | + ), |
| 49 | + ), |
| 50 | + }), |
| 51 | + Layer.succeed( |
| 52 | + ChildProcessSpawner.ChildProcessSpawner, |
| 53 | + ChildProcessSpawner.make(() => |
| 54 | + Effect.gen(function* () { |
| 55 | + yield* Ref.update(spawns, (count) => count + 1); |
| 56 | + yield* Effect.addFinalizer(() => Queue.offer(released, undefined)); |
| 57 | + yield* Queue.offer(spawned, undefined); |
| 58 | + return ChildProcessSpawner.makeHandle({ |
| 59 | + pid: ChildProcessSpawner.ProcessId(1), |
| 60 | + exitCode: |
| 61 | + input.exitsEarly === true |
| 62 | + ? Effect.succeed(ChildProcessSpawner.ExitCode(1)) |
| 63 | + : Effect.never, |
| 64 | + isRunning: Effect.succeed(true), |
| 65 | + kill: () => Effect.void, |
| 66 | + unref: Effect.succeed(Effect.void), |
| 67 | + stdin: Sink.drain, |
| 68 | + stdout: Stream.empty, |
| 69 | + stderr: Stream.empty, |
| 70 | + all: Stream.empty, |
| 71 | + getInputFd: () => Sink.drain, |
| 72 | + getOutputFd: () => Stream.empty, |
| 73 | + }); |
| 74 | + }), |
| 75 | + ), |
| 76 | + ), |
| 77 | + ); |
| 78 | + const service = yield* HostWakeLock.make.pipe(Effect.provide(dependencies)); |
| 79 | + yield* service.start().pipe(Effect.provide(dependencies)); |
| 80 | + |
| 81 | + return { |
| 82 | + events, |
| 83 | + released, |
| 84 | + spawned, |
| 85 | + spawns, |
| 86 | + working, |
| 87 | + workingReads, |
| 88 | + setMode: (next: KeepHostAwakeMode) => |
| 89 | + Ref.set(mode, next).pipe(Effect.andThen(Queue.offer(settingsChanges, next))), |
| 90 | + }; |
| 91 | +}); |
| 92 | + |
| 93 | +describe("HostWakeLock", () => { |
| 94 | + it.effect("holds one lock while runs work and releases it when they stop", () => |
| 95 | + Effect.scoped( |
| 96 | + Effect.gen(function* () { |
| 97 | + const host = yield* setup({ mode: "whileWorking" }); |
| 98 | + |
| 99 | + yield* Ref.set(host.working, true); |
| 100 | + yield* Queue.offer(host.events, runUpdated); |
| 101 | + yield* Queue.take(host.spawned); |
| 102 | + yield* Queue.offerAll(host.events, [runUpdated, runUpdated]); |
| 103 | + |
| 104 | + yield* Ref.set(host.working, false); |
| 105 | + yield* Queue.offer(host.events, runUpdated); |
| 106 | + yield* Queue.take(host.released); |
| 107 | + expect(yield* Ref.get(host.spawns)).toBe(1); |
| 108 | + }), |
| 109 | + ), |
| 110 | + ); |
| 111 | + |
| 112 | + it.effect("takes the lock at startup for runs that are already working", () => |
| 113 | + Effect.scoped( |
| 114 | + Effect.gen(function* () { |
| 115 | + const host = yield* setup({ mode: "whileWorking", working: true }); |
| 116 | + yield* Queue.take(host.spawned); |
| 117 | + }), |
| 118 | + ), |
| 119 | + ); |
| 120 | + |
| 121 | + it.effect("follows the setting without reading runs outside while-working mode", () => |
| 122 | + Effect.scoped( |
| 123 | + Effect.gen(function* () { |
| 124 | + const host = yield* setup({ mode: "off", working: true }); |
| 125 | + yield* Queue.offer(host.events, runUpdated); |
| 126 | + |
| 127 | + yield* host.setMode("always"); |
| 128 | + yield* Queue.take(host.spawned); |
| 129 | + |
| 130 | + yield* host.setMode("off"); |
| 131 | + yield* Queue.take(host.released); |
| 132 | + expect(yield* Ref.get(host.workingReads)).toBe(0); |
| 133 | + expect(yield* Ref.get(host.spawns)).toBe(1); |
| 134 | + }), |
| 135 | + ), |
| 136 | + ); |
| 137 | + |
| 138 | + it.effect("ignores events that cannot change whether a run is working", () => |
| 139 | + Effect.scoped( |
| 140 | + Effect.gen(function* () { |
| 141 | + const host = yield* setup({ mode: "whileWorking" }); |
| 142 | + yield* Ref.set(host.working, true); |
| 143 | + yield* Queue.offer(host.events, turnItemUpdated); |
| 144 | + |
| 145 | + yield* Queue.offer(host.events, runUpdated); |
| 146 | + yield* Queue.take(host.spawned); |
| 147 | + // One read at startup, one for the run event. |
| 148 | + expect(yield* Ref.get(host.workingReads)).toBe(2); |
| 149 | + }), |
| 150 | + ), |
| 151 | + ); |
| 152 | + |
| 153 | + it.effect("takes the lock again after the helper exits by itself", () => |
| 154 | + Effect.scoped( |
| 155 | + Effect.gen(function* () { |
| 156 | + const host = yield* setup({ mode: "always", exitsEarly: true }); |
| 157 | + yield* Queue.take(host.released); |
| 158 | + |
| 159 | + yield* Queue.offer(host.events, runUpdated); |
| 160 | + yield* Queue.take(host.released); |
| 161 | + expect(yield* Ref.get(host.spawns)).toBe(2); |
| 162 | + }), |
| 163 | + ), |
| 164 | + ); |
| 165 | + |
| 166 | + it.effect("releases the lock when the server shuts down", () => |
| 167 | + Effect.gen(function* () { |
| 168 | + const released = yield* Effect.scoped( |
| 169 | + Effect.gen(function* () { |
| 170 | + const host = yield* setup({ mode: "always" }); |
| 171 | + yield* Queue.take(host.spawned); |
| 172 | + return host.released; |
| 173 | + }), |
| 174 | + ); |
| 175 | + yield* Queue.take(released); |
| 176 | + }), |
| 177 | + ); |
| 178 | +}); |
0 commit comments