import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; import * as KeepAwake from "./KeepAwake.ts"; describe("KeepAwake.holdWhileWanted", () => { it.effect("keeps one hold across a gap shorter than the grace and releases after it", () => Effect.gen(function* () { const wanted = yield* Queue.unbounded(); const transitions = yield* Queue.unbounded<"held" | "released">(); const hold = Effect.acquireRelease(Queue.offer(transitions, "held"), () => Queue.offer(transitions, "released"), ).pipe(Effect.andThen(Effect.never), Effect.scoped); yield* KeepAwake.holdWhileWanted(Stream.fromQueue(wanted), hold, "60 seconds").pipe( Effect.forkScoped, ); yield* Queue.offer(wanted, true); expect(yield* Queue.take(transitions)).toBe("held"); yield* Queue.offer(wanted, false); yield* TestClock.adjust("30 seconds"); yield* Queue.offer(wanted, true); yield* TestClock.adjust("120 seconds"); expect(yield* Queue.size(transitions)).toBe(0); yield* Queue.offer(wanted, false); yield* TestClock.adjust("120 seconds"); expect(yield* Queue.take(transitions)).toBe("released"); expect(yield* Queue.size(transitions)).toBe(0); }), ); });