|
| 1 | +import { expect, it } from "@effect/vitest"; |
| 2 | +import * as Deferred from "effect/Deferred"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Exit from "effect/Exit"; |
| 5 | +import * as Fiber from "effect/Fiber"; |
| 6 | +import * as DesktopShutdown from "./DesktopShutdown.ts"; |
| 7 | + |
| 8 | +it.effect("quitting interrupts pending hydration and skips subsequent startup work", () => |
| 9 | + Effect.gen(function* () { |
| 10 | + const shutdown = yield* DesktopShutdown.DesktopShutdown; |
| 11 | + const started = yield* Deferred.make<void>(); |
| 12 | + const hydrationResult = yield* Deferred.make<void>(); |
| 13 | + let released = false; |
| 14 | + let configured = false; |
| 15 | + const startup = Effect.scoped( |
| 16 | + Effect.gen(function* () { |
| 17 | + const hydration = yield* Effect.forkScoped( |
| 18 | + Effect.acquireUseRelease( |
| 19 | + Deferred.succeed(started, undefined), |
| 20 | + () => Deferred.await(hydrationResult), |
| 21 | + () => |
| 22 | + Effect.sync(() => { |
| 23 | + released = true; |
| 24 | + }), |
| 25 | + ), |
| 26 | + ); |
| 27 | + yield* Fiber.join(hydration); |
| 28 | + configured = true; |
| 29 | + }), |
| 30 | + ); |
| 31 | + const running = yield* shutdown.runUntilRequested(startup).pipe(Effect.forkChild); |
| 32 | + yield* Deferred.await(started); |
| 33 | + yield* shutdown.request; |
| 34 | + yield* Fiber.join(running); |
| 35 | + expect(released).toBe(true); |
| 36 | + expect(configured).toBe(false); |
| 37 | + expect(yield* shutdown.isComplete).toBe(false); |
| 38 | + yield* shutdown.markComplete; |
| 39 | + yield* shutdown.awaitComplete; |
| 40 | + expect(yield* shutdown.isComplete).toBe(true); |
| 41 | + }).pipe(Effect.scoped, Effect.provide(DesktopShutdown.layer)), |
| 42 | +); |
| 43 | + |
| 44 | +it.effect("returns the completed startup result", () => |
| 45 | + Effect.gen(function* () { |
| 46 | + const shutdown = yield* DesktopShutdown.DesktopShutdown; |
| 47 | + expect(yield* shutdown.runUntilRequested(Effect.succeed("ready"))).toBe("ready"); |
| 48 | + expect(yield* shutdown.isComplete).toBe(false); |
| 49 | + }).pipe(Effect.scoped, Effect.provide(DesktopShutdown.layer)), |
| 50 | +); |
| 51 | + |
| 52 | +it.effect("preserves startup failure before a quit request", () => |
| 53 | + Effect.gen(function* () { |
| 54 | + const shutdown = yield* DesktopShutdown.DesktopShutdown; |
| 55 | + const result = yield* shutdown |
| 56 | + .runUntilRequested(Effect.fail("startup failed")) |
| 57 | + .pipe(Effect.exit); |
| 58 | + expect(result).toEqual(Exit.fail("startup failed")); |
| 59 | + }).pipe(Effect.provide(DesktopShutdown.layer)), |
| 60 | +); |
0 commit comments