The best way to control Claude Code, Codex and more from any computer or phone

main
ts 144 lines 5.07 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as InMemory from "@alchemy.run/domain/InMemory";
3 import { describe, expect, it } from "@alchemy.run/test";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Option from "effect/Option";
7 import * as Schema from "effect/Schema";
8
9 class Deposited extends Domain.Event("Deposited", { amount: Schema.Number }) {}
10 class Deposit extends Domain.Command("Deposit", {
11 target: Schema.String,
12 input: { amount: Schema.Number },
13 events: [Deposited],
14 }) {}
15 class Account extends Domain.Aggregate("Account", {
16 id: Schema.String,
17 state: Schema.Struct({ balance: Schema.Number }),
18 initial: { balance: 0 },
19 commands: [Deposit],
20 decide: { Deposit: (_, cmd) => [new Deposited({ amount: cmd.amount })] },
21 evolve: { Deposited: (s, e) => ({ balance: s.balance + e.amount }) },
22 }) {}
23
24 /** Every deposit to an account: keyed by the account, so a key's history is its own. */
25 class Deposits extends Domain.View("Deposits", {
26 from: [Account],
27 key: Account,
28 state: Schema.Struct({ amounts: Schema.Array(Schema.Number) }),
29 initial: { amounts: [] },
30 evolve: { Deposited: (s, e) => ({ amounts: [...s.amounts, e.amount] }) },
31 }) {}
32
33 /** Every deposit across every account: one key fed by every account. */
34 class Activity extends Domain.View("Activity", {
35 from: [Account],
36 key: Schema.String,
37 keyOf: { Deposited: () => "all" },
38 state: Schema.Struct({ count: Schema.Number }),
39 initial: { count: 0 },
40 evolve: { Deposited: (s) => ({ count: s.count + 1 }) },
41 }) {}
42
43 class Before extends Domain.Model("Bank", { aggregates: [Account] }) {}
44 class WithDeposits extends Domain.Model("Bank", { aggregates: [Account], views: [Deposits] }) {}
45 class WithActivity extends Domain.Model("Bank", { aggregates: [Account], views: [Activity] }) {}
46
47 /**
48 * An in-memory platform that outlives the models built on it, as storage
49 * outlives a deploy. It delivers only when drained, so a read before `settle`
50 * sees only what the read itself fetched.
51 */
52 const platform = () => {
53 const hosted = InMemory.make({ manual: true });
54 const settle: Effect.Effect<void> = Effect.gen(function* () {
55 while (true) {
56 yield* hosted.control.drain;
57 if (yield* hosted.control.idle) return;
58 yield* Effect.sleep("1 millis");
59 }
60 });
61 return {
62 settle,
63 deploy: <A, E, R>(
64 model: { readonly layer: (policies: any, options?: any) => Layer.Layer<any, any, any> },
65 effect: Effect.Effect<A, E, R>,
66 options?: Domain.Model.LayerOptions,
67 ) =>
68 effect.pipe(
69 Effect.tap(() => settle),
70 Effect.provide(model.layer(Layer.empty, options).pipe(Layer.provide(hosted.layer))),
71 ),
72 };
73 };
74
75 const deposit = (id: string, amount: number) =>
76 Effect.gen(function* () {
77 const accounts = yield* Account;
78 yield* accounts.send(Account.ref(id), new Deposit({ amount }));
79 });
80
81 const amounts = (id: string) =>
82 Effect.gen(function* () {
83 const view = yield* Deposits;
84 return Option.getOrUndefined(yield* view.query(Account.ref(id)))?.amounts;
85 });
86
87 const history = [deposit("a-1", 1), deposit("a-1", 2), deposit("a-2", 5)];
88
89 describe("Domain lazy views", () => {
90 it.live("a lazy view's key reads its history the first time it is read", () =>
91 Effect.gen(function* () {
92 const bank = platform();
93 yield* bank.deploy(Before, Effect.all(history));
94 const [complete, first, other, after] = yield* bank.deploy(
95 WithDeposits,
96 Effect.gen(function* () {
97 // Nothing delivered yet: the read fetches the key's own history.
98 const { complete } = yield* (yield* Deposits).history;
99 const first = yield* amounts("a-1");
100 const other = yield* amounts("a-2");
101 yield* deposit("a-1", 3);
102 yield* bank.settle;
103 return [complete, first, other, yield* amounts("a-1")] as const;
104 }),
105 { lazy: ["Deposits"] },
106 );
107 expect(complete).toBe(true);
108 expect(first).toEqual([1, 2]);
109 expect(other).toEqual([5]);
110 expect(after).toEqual([1, 2, 3]);
111 }),
112 );
113
114 it.live(
115 "an eager view is filled by the deploy: its history is complete once that is delivered",
116 () =>
117 Effect.gen(function* () {
118 const bank = platform();
119 yield* bank.deploy(Before, Effect.all(history));
120 const [before, after, amountsAfter] = yield* bank.deploy(
121 WithDeposits,
122 Effect.gen(function* () {
123 const view = yield* Deposits;
124 const before = (yield* view.history).complete;
125 yield* bank.settle;
126 return [before, (yield* view.history).complete, yield* amounts("a-1")] as const;
127 }),
128 );
129 expect(before).toBe(false);
130 expect(after).toBe(true);
131 expect(amountsAfter).toEqual([1, 2]);
132 }),
133 );
134
135 it.live("a view not keyed by its one source can't be lazy", () =>
136 Effect.gen(function* () {
137 const bank = platform();
138 const exit = yield* bank
139 .deploy(WithActivity, Effect.void, { lazy: ["Activity"] })
140 .pipe(Effect.exit);
141 expect(String(exit)).toContain("Activity can't be lazy");
142 }),
143 );
144 });