The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Domain from "@alchemy.run/domain"; |
| 2 | import { InMemory } from "@alchemy.run/domain/InMemory"; |
| 3 | import { Story } from "@alchemy.run/domain/Story"; |
| 4 | import { describe, expect, it } from "@alchemy.run/test"; |
| 5 | import * as DateTime from "effect/DateTime"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | |
| 10 | // A hold that expires an hour after it is placed, unless it is released first. |
| 11 | class HoldPlaced extends Domain.Event("HoldPlaced") {} |
| 12 | class HoldReleased extends Domain.Event("HoldReleased") {} |
| 13 | class HoldExpired extends Domain.Event("HoldExpired") {} |
| 14 | |
| 15 | class PlaceHold extends Domain.Command("PlaceHold", { |
| 16 | target: Schema.String, |
| 17 | events: [HoldPlaced], |
| 18 | }) {} |
| 19 | class ReleaseHold extends Domain.Command("ReleaseHold", { |
| 20 | target: Schema.String, |
| 21 | events: [HoldReleased], |
| 22 | }) {} |
| 23 | /** |
| 24 | * Records the expiry with no guard on state: only cancelling the schedule |
| 25 | * keeps a released hold from expiring. |
| 26 | */ |
| 27 | class ExpireHold extends Domain.Command("ExpireHold", { |
| 28 | target: Schema.String, |
| 29 | events: [HoldExpired], |
| 30 | }) {} |
| 31 | |
| 32 | class Hold extends Domain.Aggregate("Hold", { |
| 33 | id: Schema.String, |
| 34 | state: Schema.Struct({ status: Schema.Literals(["none", "held", "released", "expired"]) }), |
| 35 | initial: { status: "none" }, |
| 36 | commands: [PlaceHold, ReleaseHold, ExpireHold], |
| 37 | decide: { |
| 38 | PlaceHold: () => [new HoldPlaced()], |
| 39 | ReleaseHold: () => [new HoldReleased()], |
| 40 | ExpireHold: () => [new HoldExpired()], |
| 41 | }, |
| 42 | evolve: { |
| 43 | HoldPlaced: () => ({ status: "held" as const }), |
| 44 | HoldReleased: () => ({ status: "released" as const }), |
| 45 | HoldExpired: () => ({ status: "expired" as const }), |
| 46 | }, |
| 47 | }) {} |
| 48 | |
| 49 | /** The schedule key of a hold's expiry. */ |
| 50 | const expiryOf = (id: string) => `expire:${id}`; |
| 51 | |
| 52 | class HoldDeadline extends Domain.Policy("HoldDeadline", { |
| 53 | from: Hold, |
| 54 | on: [HoldPlaced, HoldReleased], |
| 55 | }) {} |
| 56 | const HoldDeadlineLive = HoldDeadline.toLayer( |
| 57 | Effect.gen(function* () { |
| 58 | const holds = yield* Hold; |
| 59 | return Effect.fn(function* ({ source, event }) { |
| 60 | if (event._tag === "HoldPlaced") { |
| 61 | yield* holds |
| 62 | .schedule(source, new ExpireHold(), { after: "1 hour", commandId: expiryOf(source.id) }) |
| 63 | // a redelivery after the expiry ran: nothing to arm |
| 64 | .pipe(Effect.catchTag("AlreadyExecuted", () => Effect.void)); |
| 65 | } else { |
| 66 | yield* holds.unschedule(source, expiryOf(source.id)).pipe( |
| 67 | // the hold expired first, and the release is recorded after it; a |
| 68 | // redelivery, so the expiry is already cancelled; or the hold was |
| 69 | // never placed, so there's no expiry |
| 70 | Effect.catchTag( |
| 71 | ["AlreadyExecuted", "AlreadyCancelled", "NotScheduled"], |
| 72 | () => Effect.void, |
| 73 | ), |
| 74 | ); |
| 75 | } |
| 76 | }); |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | class Holds extends Domain.Model("Holds", { |
| 81 | aggregates: [Hold], |
| 82 | policies: [HoldDeadline], |
| 83 | }) {} |
| 84 | |
| 85 | const story = Story.make(Holds, { layer: HoldDeadlineLive }); |
| 86 | const HoldsLive = Holds.layer(HoldDeadlineLive).pipe(Layer.provide(InMemory)); |
| 87 | const h1 = Hold.ref("h-1"); |
| 88 | const h2 = Hold.ref("h-2"); |
| 89 | |
| 90 | describe("Domain schedule", { tags: ["unit", "local"] }, () => { |
| 91 | it.effect("a scheduled command fires when it is due", () => |
| 92 | story( |
| 93 | Story.clock("2026-01-01T00:00:00Z"), |
| 94 | Story.when(h1, new PlaceHold()), |
| 95 | Story.then(h1, new HoldPlaced()), |
| 96 | Story.clock("2026-01-01T01:00:00Z"), |
| 97 | Story.then(h1, new HoldExpired()), |
| 98 | ), |
| 99 | ); |
| 100 | |
| 101 | it.effect("an unscheduled command never fires", () => |
| 102 | story( |
| 103 | Story.clock("2026-01-01T00:00:00Z"), |
| 104 | Story.when(h1, new PlaceHold()), |
| 105 | Story.when(h1, new ReleaseHold()), |
| 106 | Story.then(h1, new HoldPlaced(), new HoldReleased()), |
| 107 | Story.clock("2026-01-01T02:00:00Z"), |
| 108 | Story.then(h1), |
| 109 | Story.state(h1, (s) => expect(s.status).toBe("released")), |
| 110 | ), |
| 111 | ); |
| 112 | |
| 113 | it.effect("unscheduling cancels only that instance's command", () => |
| 114 | story( |
| 115 | Story.clock("2026-01-01T00:00:00Z"), |
| 116 | Story.when(h1, new PlaceHold()), |
| 117 | Story.when(h2, new PlaceHold()), |
| 118 | Story.when(h1, new ReleaseHold()), |
| 119 | Story.then(h1, new HoldPlaced(), new HoldReleased()), |
| 120 | Story.then(h2, new HoldPlaced()), |
| 121 | Story.clock("2026-01-01T01:00:00Z"), |
| 122 | Story.then(h1), |
| 123 | Story.then(h2, new HoldExpired()), |
| 124 | ), |
| 125 | ); |
| 126 | |
| 127 | it.effect("a release after the command fired is still recorded", () => |
| 128 | story( |
| 129 | Story.clock("2026-01-01T00:00:00Z"), |
| 130 | Story.when(h1, new PlaceHold()), |
| 131 | Story.clock("2026-01-01T01:00:00Z"), |
| 132 | Story.then(h1, new HoldPlaced(), new HoldExpired()), |
| 133 | Story.when(h1, new ReleaseHold()), |
| 134 | Story.then(h1, new HoldReleased()), |
| 135 | ), |
| 136 | ); |
| 137 | |
| 138 | it.live("an unscheduled command never fires on the real clock", () => |
| 139 | Effect.gen(function* () { |
| 140 | const holds = yield* Hold; |
| 141 | yield* holds.schedule("h-3", new ExpireHold(), { after: "20 millis", commandId: "x" }); |
| 142 | yield* holds.schedule("h-4", new ExpireHold(), { after: "20 millis", commandId: "x" }); |
| 143 | yield* holds.unschedule("h-3", "x"); |
| 144 | yield* Effect.sleep("100 millis"); |
| 145 | expect((yield* holds.state("h-3")).status).toBe("none"); |
| 146 | expect((yield* holds.state("h-4")).status).toBe("expired"); |
| 147 | }).pipe(Effect.provide(HoldsLive)), |
| 148 | ); |
| 149 | |
| 150 | it.live("schedule returns the command id and when it is due, to cancel it by", () => |
| 151 | Effect.gen(function* () { |
| 152 | const holds = yield* Hold; |
| 153 | const before = yield* Effect.sync(() => Date.now()); |
| 154 | const receipt = yield* holds.schedule("h-9", new ExpireHold(), { after: "1 hour" }); |
| 155 | const at = DateTime.toEpochMillis(receipt.at); |
| 156 | expect(at).toBeGreaterThanOrEqual(before + 3_600_000); |
| 157 | expect(at).toBeLessThan(before + 3_600_000 + 1_000); |
| 158 | yield* holds.unschedule("h-9", receipt.commandId); |
| 159 | const fixed = yield* holds.schedule("h-9", new ExpireHold(), { |
| 160 | at: DateTime.makeUnsafe("2030-01-01T00:00:00Z"), |
| 161 | commandId: "x", |
| 162 | }); |
| 163 | expect(fixed).toEqual({ commandId: "x", at: DateTime.makeUnsafe("2030-01-01T00:00:00Z") }); |
| 164 | }).pipe(Effect.provide(HoldsLive)), |
| 165 | ); |
| 166 | |
| 167 | it.live("unscheduling a command that ran fails with AlreadyExecuted", () => |
| 168 | Effect.gen(function* () { |
| 169 | const holds = yield* Hold; |
| 170 | yield* holds.schedule("h-5", new ExpireHold(), { after: "20 millis", commandId: "x" }); |
| 171 | yield* Effect.sleep("100 millis"); |
| 172 | const error = yield* Effect.flip(holds.unschedule("h-5", "x")); |
| 173 | expect(error).toEqual( |
| 174 | new Domain.AlreadyExecuted({ aggregate: "Hold", id: "h-5", commandId: "x" }), |
| 175 | ); |
| 176 | }).pipe(Effect.provide(HoldsLive)), |
| 177 | ); |
| 178 | |
| 179 | it.live("scheduling a command id that was already sent fails with AlreadyExecuted", () => |
| 180 | Effect.gen(function* () { |
| 181 | const holds = yield* Hold; |
| 182 | yield* holds.send("h-6", new PlaceHold(), { commandId: "x" }); |
| 183 | const error = yield* Effect.flip( |
| 184 | holds.schedule("h-6", new ExpireHold(), { after: "1 hour", commandId: "x" }), |
| 185 | ); |
| 186 | expect(error._tag).toBe("AlreadyExecuted"); |
| 187 | }).pipe(Effect.provide(HoldsLive)), |
| 188 | ); |
| 189 | |
| 190 | it.live("unscheduling again fails with AlreadyCancelled, until it is scheduled again", () => |
| 191 | Effect.gen(function* () { |
| 192 | const holds = yield* Hold; |
| 193 | yield* holds.schedule("h-7", new ExpireHold(), { after: "1 hour", commandId: "x" }); |
| 194 | yield* holds.unschedule("h-7", "x"); |
| 195 | expect((yield* Effect.flip(holds.unschedule("h-7", "x")))._tag).toBe("AlreadyCancelled"); |
| 196 | yield* holds.schedule("h-7", new ExpireHold(), { after: "1 hour", commandId: "x" }); |
| 197 | yield* holds.unschedule("h-7", "x"); |
| 198 | }).pipe(Effect.provide(HoldsLive)), |
| 199 | ); |
| 200 | |
| 201 | it.live("unscheduling an id that was never scheduled fails with NotScheduled", () => |
| 202 | Effect.gen(function* () { |
| 203 | const holds = yield* Hold; |
| 204 | const error = yield* Effect.flip(holds.unschedule("h-8", "never")); |
| 205 | expect(error).toEqual( |
| 206 | new Domain.NotScheduled({ aggregate: "Hold", id: "h-8", commandId: "never" }), |
| 207 | ); |
| 208 | }).pipe(Effect.provide(HoldsLive)), |
| 209 | ); |
| 210 | }); |