@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 215 lines 7.96 KB
Raw
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 Cause from "effect/Cause";
5 import * as Effect from "effect/Effect";
6 import * as Exit from "effect/Exit";
7 import * as Layer from "effect/Layer";
8 import * as Schema from "effect/Schema";
9
10 class Deposited extends Domain.Event("Deposited", { amount: Schema.Number }) {}
11 class Deposit extends Domain.Command("Deposit", {
12 target: Schema.String,
13 input: { amount: Schema.Number },
14 events: [Deposited],
15 }) {}
16 class Wallet extends Domain.Aggregate("Wallet", {
17 id: Schema.String,
18 state: Schema.Struct({ balance: Schema.Number }),
19 initial: { balance: 0 },
20 commands: [Deposit],
21 decide: { Deposit: (_, cmd) => [new Deposited({ amount: cmd.amount })] },
22 evolve: { Deposited: (s, e) => ({ balance: s.balance + e.amount }) },
23 }) {}
24
25 class Sent extends Domain.Event("Sent", { to: Schema.String, amount: Schema.Number }) {}
26 class Send extends Domain.Command("Send", {
27 target: Schema.String,
28 input: { to: Schema.String, amount: Schema.Number },
29 events: [Sent],
30 }) {}
31 class Payment extends Domain.Aggregate("Payment", {
32 id: Schema.String,
33 state: Schema.Struct({}),
34 initial: {},
35 commands: [Send],
36 decide: { Send: (_, { to, amount }) => [new Sent({ to, amount })] },
37 evolve: { Sent: (s) => s },
38 }) {}
39
40 class Balance extends Domain.View("Balance", {
41 from: [Wallet],
42 key: Wallet,
43 state: Schema.Struct({ balance: Schema.Number }),
44 initial: { balance: 0 },
45 evolve: { Deposited: (s, e) => ({ balance: s.balance + e.amount }) },
46 }) {}
47
48 class Card extends Domain.Event("Card", { amount: Schema.Number }) {}
49 class WalletCard extends Domain.Stream("WalletCard", { key: Wallet, entries: [Card] }) {}
50 class WalletCards extends Domain.Projection("WalletCards", {
51 from: [Wallet],
52 key: Wallet,
53 on: [Deposited],
54 writes: [WalletCard],
55 }) {}
56
57 class Bank extends Domain.Model("Bank", {
58 aggregates: [Wallet, Payment],
59 views: [Balance],
60 projections: [WalletCards],
61 streams: [WalletCard],
62 }) {}
63
64 // Payments are keyed by payment id, not by wallet: routing them needs keyOf.
65 class Received extends Domain.View("Received", {
66 from: [Payment],
67 key: Wallet,
68 state: Schema.Struct({ total: Schema.Number }),
69 evolve: { Sent: (s, e) => ({ total: (s?.total ?? 0) + e.amount }) },
70 }) {}
71 // Keyed by a branch id rather than an aggregate: a wallet's events name no branch.
72 const BranchId = Schema.String.pipe(Schema.brand("BranchId"));
73 class BranchTotals extends Domain.View("BranchTotals", {
74 from: [Wallet],
75 key: BranchId,
76 state: Schema.Struct({ total: Schema.Number }),
77 initial: { total: 0 },
78 evolve: { Deposited: (s, e) => ({ total: s.total + e.amount }) },
79 }) {}
80 // Keyed by branch id, so it cannot write a stream keyed by wallet.
81 class BranchCard extends Domain.Stream("BranchCard", { key: Wallet, entries: [Card] }) {}
82 class BranchCards extends Domain.Projection("BranchCards", {
83 from: [Wallet],
84 key: BranchId,
85 keyOf: { Deposited: () => BranchId.make("main") },
86 on: [Deposited],
87 writes: [BranchCard],
88 }) {}
89 class AlsoCards extends Domain.Projection("AlsoCards", {
90 from: [Wallet],
91 key: Wallet,
92 on: [Deposited],
93 writes: [WalletCard],
94 }) {}
95 class Broken extends Domain.Model("Broken", {
96 aggregates: [Wallet, Payment],
97 views: [Received, BranchTotals],
98 projections: [WalletCards, AlsoCards, BranchCards],
99 streams: [WalletCard, BranchCard],
100 }) {}
101
102 // Notify listens for an event its source never emits, and Audit reads a view
103 // the model does not host. Ledger's initial state does not fit its schema.
104 class Notify extends Domain.Policy("Notify", { from: Payment, on: [Deposited] }) {}
105 class Audit extends Domain.Policy("Audit", { from: Balance, on: [Deposited] }) {}
106 class Ledger extends Domain.Aggregate("Ledger", {
107 id: Schema.String,
108 state: Schema.Struct({ entries: Schema.Number }),
109 initial: { entries: "none" } as unknown as { entries: number },
110 commands: [],
111 decide: {},
112 evolve: {},
113 }) {}
114 class AlsoBroken extends Domain.Model("AlsoBroken", {
115 aggregates: [Wallet, Payment, Ledger],
116 policies: [Notify, Audit],
117 }) {}
118
119 const AccountId = Schema.String.pipe(Schema.brand("AccountId"));
120 const CustomerId = Schema.String.pipe(Schema.brand("CustomerId"));
121 class Opened extends Domain.Event("Opened") {}
122 class OpenAccount extends Domain.Command("OpenAccount", { target: AccountId, events: [Opened] }) {}
123 // Addresses a customer, so it does not belong on Account.
124 class OpenForCustomer extends Domain.Command("OpenForCustomer", {
125 target: CustomerId,
126 events: [Opened],
127 }) {}
128 // A plain string target accepts ids that are not an AccountId.
129 class OpenAny extends Domain.Command("OpenAny", { target: Schema.String, events: [Opened] }) {}
130
131 describe("Domain verify", { tags: ["unit", "local"] }, () => {
132 it("finds no problem in a correct model", () => {
133 expect(Domain.verify(Bank)).toEqual([]);
134 });
135
136 it("returns every definition problem at once", () => {
137 expect(Domain.verify(Broken).map((problem) => problem.message)).toEqual([
138 "Received declares state without initial",
139 "Received consumes 'Sent' from Payment, which is not keyed by Wallet: add keyOf.Sent",
140 "BranchTotals consumes 'Deposited' from Wallet, which is not keyed by its key schema: add keyOf.Deposited",
141 "WalletCard has two writers, WalletCards and AlsoCards: a stream key has exactly one",
142 "Projection BranchCards is keyed by a key schema, so it cannot write BranchCard, keyed by Wallet",
143 ]);
144 });
145
146 it("goes on past a problem to find the next one", () => {
147 expect(Domain.verify(AlsoBroken).map((problem) => problem.message)).toEqual([
148 expect.stringContaining("Ledger.initial does not match its state schema"),
149 "Policy Notify: none of Payment emits 'Deposited'",
150 "Policy Audit consumes Balance, which is not in the Domain",
151 ]);
152 });
153
154 it("reports the problem that building the model's Layer fails on", () => {
155 const [first] = Domain.verify(Broken);
156 const exit = Effect.runSyncExit(
157 Layer.build(
158 Broken.layer(
159 Layer.mergeAll(
160 WalletCards.toLayer(Effect.succeed(() => Effect.void)),
161 AlsoCards.toLayer(Effect.succeed(() => Effect.void)),
162 BranchCards.toLayer(Effect.succeed(() => Effect.void)),
163 ),
164 ),
165 ).pipe(Effect.scoped, Effect.provide(InMemory)) as Effect.Effect<unknown>,
166 );
167 expect(Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined).toMatchObject({
168 _tag: "DefinitionError",
169 message: first!.message,
170 });
171 });
172
173 describe("command target", { tags: ["unit", "local"] }, () => {
174 it("a command addresses its aggregate's id", () => {
175 class Account extends Domain.Aggregate("Account", {
176 id: AccountId,
177 state: Schema.Struct({}),
178 initial: {},
179 commands: [OpenAccount],
180 decide: { OpenAccount: () => [new Opened()] },
181 evolve: { Opened: (s) => s },
182 }) {}
183 expect(Account.ref("a-1").id).toBe("a-1");
184 });
185
186 it("a command whose target is another id schema is a type error", () => {
187 class Account extends Domain.Aggregate("Account", {
188 id: AccountId,
189 state: Schema.Struct({}),
190 initial: {},
191 commands: [
192 OpenAccount,
193 // @ts-expect-error OpenForCustomer's target must be this aggregate's id schema
194 OpenForCustomer,
195 ],
196 decide: { OpenAccount: () => [new Opened()], OpenForCustomer: () => [new Opened()] },
197 evolve: { Opened: (s) => s },
198 }) {}
199 expect(Account.aggregateName).toBe("Account");
200 });
201
202 it("a command whose target is wider than the id is a type error", () => {
203 class Account extends Domain.Aggregate("Account", {
204 id: AccountId,
205 state: Schema.Struct({}),
206 initial: {},
207 // @ts-expect-error OpenAny's target must be this aggregate's id schema
208 commands: [OpenAny],
209 decide: { OpenAny: () => [new Opened()] },
210 evolve: { Opened: (s) => s },
211 }) {}
212 expect(Account.aggregateName).toBe("Account");
213 });
214 });
215 });