The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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 Effect from "effect/Effect"; |
| 6 | import * as Fiber from "effect/Fiber"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | |
| 12 | // Tours belong to an org. The org-wide views are keyed by the org id itself: |
| 13 | // there is no Org aggregate. |
| 14 | const OrgId = Schema.String.pipe(Schema.brand("OrgId")); |
| 15 | type OrgId = typeof OrgId.Type; |
| 16 | |
| 17 | class TourBooked extends Domain.Event("TourBooked", { orgId: OrgId, unit: Schema.String }) {} |
| 18 | class TourCancelled extends Domain.Event("TourCancelled", { orgId: OrgId }) {} |
| 19 | |
| 20 | class BookTour extends Domain.Command("BookTour", { |
| 21 | target: Schema.String, |
| 22 | input: { orgId: OrgId, unit: Schema.String }, |
| 23 | events: [TourBooked], |
| 24 | }) {} |
| 25 | class CancelTour extends Domain.Command("CancelTour", { |
| 26 | target: Schema.String, |
| 27 | input: { orgId: OrgId }, |
| 28 | events: [TourCancelled], |
| 29 | }) {} |
| 30 | |
| 31 | class Tour extends Domain.Aggregate("Tour", { |
| 32 | id: Schema.String, |
| 33 | state: Schema.Struct({}), |
| 34 | initial: {}, |
| 35 | commands: [BookTour, CancelTour], |
| 36 | decide: { |
| 37 | BookTour: (_, { orgId, unit }) => [new TourBooked({ orgId, unit })], |
| 38 | CancelTour: (_, { orgId }) => [new TourCancelled({ orgId })], |
| 39 | }, |
| 40 | evolve: { TourBooked: (s) => s, TourCancelled: (s) => s }, |
| 41 | }) {} |
| 42 | |
| 43 | class ScheduleChanged extends Domain.Event("ScheduleChanged", { open: Schema.Number }) {} |
| 44 | |
| 45 | /** One instance per org: the open tours, by tour id. */ |
| 46 | class OrgSchedule extends Domain.View("OrgSchedule", { |
| 47 | from: [Tour], |
| 48 | key: OrgId, |
| 49 | keyOf: { Tour: ({ event }) => event.orgId }, |
| 50 | state: Schema.Struct({ org: Schema.String, tours: Schema.Record(Schema.String, Schema.String) }), |
| 51 | initial: { org: "", tours: {} }, |
| 52 | evolve: { |
| 53 | // `key` is the org id itself, not a ref. |
| 54 | TourBooked: (s, e, { key, source }) => ({ |
| 55 | org: key, |
| 56 | tours: { ...s.tours, [source.id]: e.unit }, |
| 57 | }), |
| 58 | TourCancelled: (s, _, { source }) => { |
| 59 | const { [source.id]: _cancelled, ...tours } = s.tours; |
| 60 | return { ...s, tours }; |
| 61 | }, |
| 62 | }, |
| 63 | events: [ScheduleChanged], |
| 64 | emit: { |
| 65 | Tour: (_, { after }) => new ScheduleChanged({ open: Object.keys(after?.tours ?? {}).length }), |
| 66 | }, |
| 67 | }) {} |
| 68 | |
| 69 | /** Keyed by the same schema as its source, so it needs no `keyOf`. */ |
| 70 | class OrgBusy extends Domain.View("OrgBusy", { |
| 71 | from: [OrgSchedule], |
| 72 | key: OrgId, |
| 73 | state: Schema.Struct({ org: Schema.String, busy: Schema.Boolean }), |
| 74 | initial: { org: "", busy: false }, |
| 75 | evolve: { |
| 76 | ScheduleChanged: (_, e, { source }) => ({ org: source, busy: e.open >= 2 }), |
| 77 | }, |
| 78 | }) {} |
| 79 | |
| 80 | class FeedItem extends Domain.Event("FeedItem", { tour: Schema.String, unit: Schema.String }) {} |
| 81 | |
| 82 | /** One ordered log per org. */ |
| 83 | class OrgFeed extends Domain.Stream("OrgFeed", { key: OrgId, entries: [FeedItem] }) {} |
| 84 | |
| 85 | /** Writes each org's feed, one org at a time. */ |
| 86 | class OrgFeedWriter extends Domain.Projection("OrgFeedWriter", { |
| 87 | from: [Tour], |
| 88 | key: OrgId, |
| 89 | keyOf: { Tour: ({ event }) => event.orgId }, |
| 90 | on: [TourBooked], |
| 91 | writes: [OrgFeed], |
| 92 | }) {} |
| 93 | const OrgFeedWriterLive = OrgFeedWriter.toLayer( |
| 94 | Effect.gen(function* () { |
| 95 | const feed = yield* OrgFeed; |
| 96 | return Effect.fn(function* (batch) { |
| 97 | for (const input of batch) { |
| 98 | // `key` is the org id itself, not a ref. |
| 99 | const org: OrgId = input.key; |
| 100 | yield* feed.append( |
| 101 | input, |
| 102 | new FeedItem({ tour: input.source.id, unit: `${org}:${input.event.unit}` }), |
| 103 | ); |
| 104 | } |
| 105 | }); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | class Noted extends Domain.Event("Noted", { tour: Schema.String }) {} |
| 110 | class Note extends Domain.Command("Note", { |
| 111 | target: Schema.String, |
| 112 | input: { tour: Schema.String }, |
| 113 | events: [Noted], |
| 114 | }) {} |
| 115 | /** An org's bookings, as notes. Its ids are org ids. */ |
| 116 | class Board extends Domain.Aggregate("Board", { |
| 117 | id: Schema.String, |
| 118 | state: Schema.Struct({ notes: Schema.Array(Schema.String) }), |
| 119 | initial: { notes: [] }, |
| 120 | commands: [Note], |
| 121 | decide: { Note: (_, { tour }) => [new Noted({ tour })] }, |
| 122 | evolve: { Noted: (s, e) => ({ notes: [...s.notes, e.tour] }) }, |
| 123 | }) {} |
| 124 | |
| 125 | /** Runs in each org's order: a policy keyed by the org id. */ |
| 126 | class NoteBookings extends Domain.Policy("NoteBookings", { |
| 127 | from: Tour, |
| 128 | key: OrgId, |
| 129 | keyOf: { TourBooked: ({ event }) => event.orgId }, |
| 130 | on: [TourBooked], |
| 131 | }) {} |
| 132 | const NoteBookingsLive = NoteBookings.toLayer( |
| 133 | Effect.gen(function* () { |
| 134 | const boards = yield* Board; |
| 135 | return Effect.fn(function* ({ key, source }) { |
| 136 | const org: OrgId = key; |
| 137 | yield* boards.send(org, new Note({ tour: source.id })); |
| 138 | }); |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | class Tours extends Domain.Model("Tours", { |
| 143 | aggregates: [Tour, Board], |
| 144 | views: [OrgSchedule, OrgBusy], |
| 145 | projections: [OrgFeedWriter], |
| 146 | streams: [OrgFeed], |
| 147 | policies: [NoteBookings], |
| 148 | }) {} |
| 149 | |
| 150 | const handlers = Layer.mergeAll(OrgFeedWriterLive, NoteBookingsLive); |
| 151 | const story = Story.make(Tours, { layer: handlers }); |
| 152 | |
| 153 | // A Schema-keyed view is addressed by its value, not by a ref. |
| 154 | // @ts-expect-error a ref is not an OrgId |
| 155 | export const byRef = Story.view(OrgSchedule, Tour.ref("t-1"), Story.none); |
| 156 | // @ts-expect-error a ref is not an OrgId |
| 157 | export const streamedByRef = Story.streamed(OrgFeed, Tour.ref("t-1")); |
| 158 | const acme = OrgId.make("acme"); |
| 159 | const globex = OrgId.make("globex"); |
| 160 | |
| 161 | describe("Domain keyed by a schema", { tags: ["unit", "local"] }, () => { |
| 162 | it.effect("folds every aggregate's events into the org they name", () => |
| 163 | story( |
| 164 | Story.when(Tour.ref("t-1"), new BookTour({ orgId: acme, unit: "1A" })), |
| 165 | Story.then(Tour.ref("t-1"), new TourBooked({ orgId: acme, unit: "1A" })), |
| 166 | Story.when(Tour.ref("t-2"), new BookTour({ orgId: acme, unit: "2B" })), |
| 167 | Story.then(Tour.ref("t-2"), new TourBooked({ orgId: acme, unit: "2B" })), |
| 168 | Story.when(Tour.ref("t-3"), new BookTour({ orgId: globex, unit: "9Z" })), |
| 169 | Story.then(Tour.ref("t-3"), new TourBooked({ orgId: globex, unit: "9Z" })), |
| 170 | Story.when(Tour.ref("t-1"), new CancelTour({ orgId: acme })), |
| 171 | Story.then(Tour.ref("t-1"), new TourCancelled({ orgId: acme })), |
| 172 | Story.view(OrgSchedule, acme, { org: "acme", tours: { "t-2": "2B" } }), |
| 173 | Story.view(OrgSchedule, globex, { org: "globex", tours: { "t-3": "9Z" } }), |
| 174 | Story.view(OrgSchedule, OrgId.make("initech"), Story.none), |
| 175 | Story.emitted(OrgSchedule, acme, [{ open: 1 }, { open: 2 }, { open: 1 }]), |
| 176 | Story.then(Board.ref("acme"), new Noted({ tour: "t-1" }), new Noted({ tour: "t-2" })), |
| 177 | Story.then(Board.ref("globex"), new Noted({ tour: "t-3" })), |
| 178 | ), |
| 179 | ); |
| 180 | |
| 181 | it.effect("a view keyed by the same schema follows it without keyOf", () => |
| 182 | story( |
| 183 | Story.when(Tour.ref("t-1"), new BookTour({ orgId: acme, unit: "1A" })), |
| 184 | Story.then(Tour.ref("t-1"), new TourBooked({ orgId: acme, unit: "1A" })), |
| 185 | Story.view(OrgBusy, acme, { org: "acme", busy: false }), |
| 186 | Story.when(Tour.ref("t-2"), new BookTour({ orgId: acme, unit: "2B" })), |
| 187 | Story.then(Tour.ref("t-2"), new TourBooked({ orgId: acme, unit: "2B" })), |
| 188 | Story.view(OrgBusy, acme, { org: "acme", busy: true }), |
| 189 | Story.view(OrgBusy, globex, Story.none), |
| 190 | Story.then(Board.ref("acme"), new Noted({ tour: "t-1" }), new Noted({ tour: "t-2" })), |
| 191 | ), |
| 192 | ); |
| 193 | |
| 194 | it.effect("a projection keyed by the schema writes a stream keyed by it", () => |
| 195 | story( |
| 196 | Story.when(Tour.ref("t-1"), new BookTour({ orgId: acme, unit: "1A" })), |
| 197 | Story.then(Tour.ref("t-1"), new TourBooked({ orgId: acme, unit: "1A" })), |
| 198 | Story.when(Tour.ref("t-3"), new BookTour({ orgId: globex, unit: "9Z" })), |
| 199 | Story.then(Tour.ref("t-3"), new TourBooked({ orgId: globex, unit: "9Z" })), |
| 200 | Story.when(Tour.ref("t-2"), new BookTour({ orgId: acme, unit: "2B" })), |
| 201 | Story.then(Tour.ref("t-2"), new TourBooked({ orgId: acme, unit: "2B" })), |
| 202 | Story.streamed( |
| 203 | OrgFeed, |
| 204 | acme, |
| 205 | new FeedItem({ tour: "t-1", unit: "acme:1A" }), |
| 206 | new FeedItem({ tour: "t-2", unit: "acme:2B" }), |
| 207 | ), |
| 208 | Story.streamed(OrgFeed, globex, new FeedItem({ tour: "t-3", unit: "globex:9Z" })), |
| 209 | Story.then(Board.ref("acme"), new Noted({ tour: "t-1" }), new Noted({ tour: "t-2" })), |
| 210 | Story.then(Board.ref("globex"), new Noted({ tour: "t-3" })), |
| 211 | ), |
| 212 | ); |
| 213 | |
| 214 | it.effect("a policy keyed by the schema gets the value as its key", () => |
| 215 | story( |
| 216 | Story.when(Tour.ref("t-1"), new BookTour({ orgId: acme, unit: "1A" })), |
| 217 | Story.then(Tour.ref("t-1"), new TourBooked({ orgId: acme, unit: "1A" })), |
| 218 | Story.when(Tour.ref("t-2"), new BookTour({ orgId: acme, unit: "2B" })), |
| 219 | Story.then(Tour.ref("t-2"), new TourBooked({ orgId: acme, unit: "2B" })), |
| 220 | Story.then(Board.ref("acme"), new Noted({ tour: "t-1" }), new Noted({ tour: "t-2" })), |
| 221 | Story.state(Board.ref("acme"), { notes: ["t-1", "t-2"] }), |
| 222 | ), |
| 223 | ); |
| 224 | |
| 225 | it.live("is read, watched and listed by its key", () => |
| 226 | Effect.gen(function* () { |
| 227 | const tours = yield* Tour; |
| 228 | const schedules = yield* OrgSchedule; |
| 229 | const watched = yield* schedules.watch(acme).pipe( |
| 230 | Stream.filter(Option.isSome), |
| 231 | Stream.map((state) => Object.keys(state.value.tours).length), |
| 232 | Stream.take(2), |
| 233 | Stream.runCollect, |
| 234 | Effect.forkChild, |
| 235 | ); |
| 236 | yield* Effect.sleep("10 millis"); |
| 237 | const first = yield* tours.send("t-1", new BookTour({ orgId: acme, unit: "1A" })); |
| 238 | const second = yield* tours.send("t-2", new BookTour({ orgId: acme, unit: "2B" })); |
| 239 | expect(yield* Fiber.join(watched)).toEqual([1, 2]); |
| 240 | const state = yield* schedules.query(acme, { |
| 241 | atLeast: { stream: second.stream, version: second.version }, |
| 242 | }); |
| 243 | expect(Option.getOrNull(state)).toEqual({ org: "acme", tours: { "t-1": "1A", "t-2": "2B" } }); |
| 244 | const page = yield* schedules.list(acme, { take: 1 }); |
| 245 | expect(page.entries.map((entry) => entry.envelope.id)).toEqual(["acme"]); |
| 246 | expect(page.entries.map((entry) => entry.event.open)).toEqual([1]); |
| 247 | expect(first.stream).toBe("Tour/t-1"); |
| 248 | }).pipe(Effect.scoped, Effect.provide(Tours.layer(handlers).pipe(Layer.provide(InMemory)))), |
| 249 | ); |
| 250 | |
| 251 | it.live("is served through an Api by its key", () => |
| 252 | Effect.gen(function* () { |
| 253 | class TourApi extends Domain.Api("TourApi", { |
| 254 | model: Tours, |
| 255 | views: ["OrgSchedule"], |
| 256 | streams: ["OrgFeed"], |
| 257 | }) {} |
| 258 | const client = yield* TourApi.clientInProcess; |
| 259 | const tours = yield* Tour; |
| 260 | const receipt = yield* tours.send("t-1", new BookTour({ orgId: acme, unit: "1A" })); |
| 261 | const atLeast = { stream: receipt.stream, version: receipt.version }; |
| 262 | expect(yield* client.OrgSchedule.get(acme, { atLeast })).toEqual({ |
| 263 | org: "acme", |
| 264 | tours: { "t-1": "1A" }, |
| 265 | }); |
| 266 | expect(yield* client.OrgSchedule.get(globex)).toBeNull(); |
| 267 | const { entries } = yield* client.OrgSchedule.list(acme); |
| 268 | expect(entries.map((entry) => entry.event.open)).toEqual([1]); |
| 269 | const latest = yield* client.OrgFeed.peek(acme, { atLeast }); |
| 270 | expect(latest?.envelope.id).toBe("acme"); |
| 271 | expect(latest?.event).toMatchObject({ tour: "t-1", unit: "acme:1A" }); |
| 272 | }).pipe(Effect.scoped, Effect.provide(Tours.layer(handlers).pipe(Layer.provide(InMemory)))), |
| 273 | ); |
| 274 | }); |