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 { describe, expect, it } from "@alchemy.run/test"; |
| 4 | import * as Context from "effect/Context"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { Activity } from "effect/workflow"; |
| 10 | |
| 11 | // One aggregate and one view definition, used by two independent models. |
| 12 | class Opened extends Domain.Event("Opened", { owner: Schema.String }) {} |
| 13 | class Open extends Domain.Command("Open", { |
| 14 | target: Schema.String, |
| 15 | input: { owner: Schema.String }, |
| 16 | events: [Opened], |
| 17 | }) {} |
| 18 | class Account extends Domain.Aggregate("Account", { |
| 19 | id: Schema.String, |
| 20 | state: Schema.Struct({ owner: Schema.NullOr(Schema.String) }), |
| 21 | initial: { owner: null }, |
| 22 | commands: [Open], |
| 23 | decide: { Open: (_, { owner }) => [new Opened({ owner })] }, |
| 24 | evolve: { Opened: (_, { owner }) => ({ owner }) }, |
| 25 | }) {} |
| 26 | class Owners extends Domain.View("Owners", { |
| 27 | from: [Account], |
| 28 | key: Account, |
| 29 | state: Schema.Struct({ owner: Schema.String }), |
| 30 | initial: { owner: "" }, |
| 31 | evolve: { Opened: (_, { owner }) => ({ owner }) }, |
| 32 | }) {} |
| 33 | |
| 34 | class Billing extends Domain.Model("Billing", { aggregates: [Account], views: [Owners] }) {} |
| 35 | class Audit extends Domain.Model("Audit", { aggregates: [Account], views: [Owners] }) {} |
| 36 | |
| 37 | // One saga definition, run by two independent models. |
| 38 | class OpenAccount extends Domain.Saga("OpenAccount", { |
| 39 | key: Schema.String, |
| 40 | input: { owner: Schema.String }, |
| 41 | }) {} |
| 42 | const OpenAccountLive = OpenAccount.toLayer( |
| 43 | Effect.gen(function* () { |
| 44 | const accounts = yield* Account; |
| 45 | return Effect.fn(function* ({ key, owner }) { |
| 46 | yield* Activity.make({ |
| 47 | name: "open", |
| 48 | execute: accounts.send(key, new Open({ owner })).pipe(Effect.asVoid), |
| 49 | }); |
| 50 | }); |
| 51 | }), |
| 52 | ); |
| 53 | class Sales extends Domain.Model("Sales", { aggregates: [Account], sagas: [OpenAccount] }) {} |
| 54 | class Support extends Domain.Model("Support", { aggregates: [Account], sagas: [OpenAccount] }) {} |
| 55 | |
| 56 | describe("Domain model isolation", () => { |
| 57 | it.live("two models with the same definitions on one platform keep separate data", () => |
| 58 | Effect.gen(function* () { |
| 59 | const platform = yield* Layer.build(InMemory); |
| 60 | const billing = yield* Layer.build(Billing.layer(Layer.empty)).pipe(Effect.provide(platform)); |
| 61 | const audit = yield* Layer.build(Audit.layer(Layer.empty)).pipe(Effect.provide(platform)); |
| 62 | |
| 63 | const receipt = yield* Context.get(billing, Account).send("a-1", new Open({ owner: "sam" })); |
| 64 | const atLeast = { stream: receipt.stream, version: receipt.version }; |
| 65 | |
| 66 | expect(yield* Context.get(billing, Account).state("a-1")).toEqual({ owner: "sam" }); |
| 67 | expect(yield* Context.get(billing, Owners).query(Account.ref("a-1"), { atLeast })).toEqual( |
| 68 | Option.some({ owner: "sam" }), |
| 69 | ); |
| 70 | expect(yield* Context.get(audit, Account).state("a-1")).toEqual({ owner: null }); |
| 71 | expect(yield* Context.get(audit, Owners).query(Account.ref("a-1"))).toEqual(Option.none()); |
| 72 | }).pipe(Effect.scoped), |
| 73 | ); |
| 74 | |
| 75 | it.live("two models running the same saga keep separate runs", () => |
| 76 | Effect.gen(function* () { |
| 77 | const platform = yield* Layer.build(InMemory); |
| 78 | const sales = yield* Layer.build(Sales.layer(OpenAccountLive)).pipe(Effect.provide(platform)); |
| 79 | const support = yield* Layer.build(Support.layer(OpenAccountLive)).pipe( |
| 80 | Effect.provide(platform), |
| 81 | ); |
| 82 | |
| 83 | // the same key in both models: two runs, each with its own input |
| 84 | const salesRun = yield* Context.get(sales, OpenAccount).start("s-1", { owner: "sam" }); |
| 85 | const supportRun = yield* Context.get(support, OpenAccount).start("s-1", { owner: "alex" }); |
| 86 | yield* salesRun.result; |
| 87 | yield* supportRun.result; |
| 88 | |
| 89 | expect(yield* Context.get(sales, Account).state("s-1")).toEqual({ owner: "sam" }); |
| 90 | expect(yield* Context.get(support, Account).state("s-1")).toEqual({ owner: "alex" }); |
| 91 | }).pipe(Effect.scoped), |
| 92 | ); |
| 93 | |
| 94 | it.effect("hosting two models with the same name on one platform fails", () => |
| 95 | Effect.gen(function* () { |
| 96 | class Duplicate extends Domain.Model("Billing", { aggregates: [Account] }) {} |
| 97 | const platform = yield* Layer.build(InMemory); |
| 98 | yield* Layer.build(Billing.layer(Layer.empty)).pipe(Effect.provide(platform)); |
| 99 | const exit = yield* Layer.build(Duplicate.layer(Layer.empty)).pipe( |
| 100 | Effect.provide(platform), |
| 101 | Effect.exit, |
| 102 | ); |
| 103 | expect(String(exit)).toContain( |
| 104 | "Model Billing is hosted twice on one platform: give each model its own name", |
| 105 | ); |
| 106 | }).pipe(Effect.scoped), |
| 107 | ); |
| 108 | }); |