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

main
ts 158 lines 6.02 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 NodeHttpServer from "@effect/platform-node/NodeHttpServer";
5 import * as Effect from "effect/Effect";
6 import * as HttpServer from "effect/http/HttpServer";
7 import * as Layer from "effect/Layer";
8 import * as Schema from "effect/Schema";
9 import * as Stream from "effect/Stream";
10
11 class Incremented extends Domain.Event("Incremented", { by: Schema.Number }) {}
12 class Increment extends Domain.Command("Increment", {
13 target: Schema.String,
14 input: { by: Schema.Number },
15 events: [Incremented],
16 }) {}
17 class Counter extends Domain.Aggregate("Counter", {
18 id: Schema.String,
19 state: Schema.Struct({ total: Schema.Number }),
20 initial: { total: 0 },
21 commands: [Increment],
22 decide: { Increment: (_, { by }) => [new Incremented({ by })] },
23 evolve: { Incremented: (s, e) => ({ total: s.total + e.by }) },
24 }) {}
25
26 class Totals extends Domain.View("Totals", {
27 from: [Counter],
28 key: Counter,
29 state: Schema.Struct({ total: Schema.Number }),
30 initial: { total: 0 },
31 evolve: { Incremented: (s, e) => ({ total: s.total + e.by }) },
32 }) {}
33
34 class Counters extends Domain.Model("Counters", { aggregates: [Counter], views: [Totals] }) {}
35 class CounterApi extends Domain.Api("CounterApi", {
36 model: Counters,
37 commands: ["Increment"],
38 views: ["Totals"],
39 }) {}
40
41 const Hosted = Counters.layer(Layer.empty).pipe(Layer.provide(InMemory));
42 const InProcess = Layer.effect(CounterApi.Client, CounterApi.clientInProcess).pipe(
43 Layer.provideMerge(Hosted),
44 );
45 // The Api's `fetch` on a real HTTP server, and a client over the wire protocol.
46 const Wire = CounterApi.layerClient.pipe(
47 Layer.provide(Domain.protocolHttp("")),
48 Layer.provideMerge(
49 Layer.unwrap(Effect.map(CounterApi, (api) => HttpServer.serve()(api.fetch))).pipe(
50 Layer.provideMerge(NodeHttpServer.layerTest),
51 ),
52 ),
53 Layer.provideMerge(Hosted),
54 );
55
56 /** Each read that takes `atLeast`, waiting for a version the view never reaches. */
57 const neverCaughtUp = Effect.gen(function* () {
58 const api = yield* CounterApi.Client;
59 const receipt = yield* api.Increment("c-3", { by: 1 });
60 const atLeast = { stream: receipt.stream, version: receipt.version + 1 };
61 return yield* Effect.all(
62 [
63 Effect.flip(api.Totals.snapshot("c-3", { atLeast })),
64 Effect.flip(api.Totals.get("c-3", { atLeast })),
65 Effect.flip(api.Totals.list("c-3", { atLeast })),
66 ],
67 { concurrency: "unbounded" },
68 );
69 });
70
71 /** A snapshot, two writes, then a feed that resumes after the snapshot's cursor. */
72 const snapshotThenFeed = Effect.gen(function* () {
73 const api = yield* CounterApi.Client;
74 const first = yield* api.Increment("c-1", { by: 1 });
75 const snapshot = yield* api.Totals.snapshot("c-1", {
76 atLeast: { stream: first.stream, version: first.version },
77 });
78 // Written after the snapshot, before the feed starts.
79 yield* api.Increment("c-1", { by: 2 });
80 const last = yield* api.Increment("c-1", { by: 3 });
81 const entries = yield* api.Totals.events("c-1", { after: snapshot.cursor }).pipe(
82 Stream.filter(Domain.Read.isEvent),
83 Stream.take(2),
84 Stream.runCollect,
85 );
86 const now = yield* api.Totals.get("c-1", {
87 atLeast: { stream: last.stream, version: last.version },
88 });
89 return { snapshot, entries, now };
90 });
91
92 describe("Domain View reads", { tags: ["unit", "local"] }, () => {
93 it.live("gives the state and the cursor it is valid at", () =>
94 Effect.gen(function* () {
95 const api = yield* CounterApi.Client;
96 expect(yield* api.Totals.snapshot("c-0")).toEqual({ state: null, cursor: 0 });
97 const receipt = yield* api.Increment("c-1", { by: 4 });
98 const snapshot = yield* api.Totals.snapshot("c-1", {
99 atLeast: { stream: receipt.stream, version: receipt.version },
100 });
101 const page = yield* api.Totals.list("c-1", { order: "desc", take: 1 });
102 expect(snapshot).toEqual({ state: { total: 4 }, cursor: 1 });
103 expect(page.entries[0]!.envelope.seq).toBe(snapshot.cursor);
104 }).pipe(Effect.provide(InProcess)),
105 );
106
107 it.live("writes between the snapshot and the feed arrive once each", () =>
108 Effect.gen(function* () {
109 const { snapshot, entries, now } = yield* snapshotThenFeed;
110 expect(snapshot).toEqual({ state: { total: 1 }, cursor: 1 });
111 expect(entries.map((entry) => entry.envelope.seq)).toEqual([2, 3]);
112 // The snapshot's state plus the feed is the current state: no gap, nothing twice.
113 expect(entries.at(-1)!.state).toEqual(now);
114 expect(snapshot.state!.total + entries.reduce((sum, e) => sum + e.event.by, 0)).toBe(
115 now!.total,
116 );
117 }).pipe(Effect.provide(InProcess)),
118 );
119
120 it.live("works over the wire", () =>
121 Effect.gen(function* () {
122 const { snapshot, entries } = yield* snapshotThenFeed;
123 expect(snapshot).toEqual({ state: { total: 1 }, cursor: 1 });
124 expect(entries.map((entry) => entry.envelope.seq)).toEqual([2, 3]);
125 }).pipe(Effect.scoped, Effect.provide(Wire)),
126 );
127
128 it.live("list waits for a write with atLeast, like get", () =>
129 Effect.gen(function* () {
130 const api = yield* CounterApi.Client;
131 const receipt = yield* api.Increment("c-2", { by: 5 });
132 const page = yield* api.Totals.list("c-2", {
133 atLeast: { stream: receipt.stream, version: receipt.version },
134 });
135 expect(page.entries.map((entry) => entry.state)).toEqual([{ total: 5 }]);
136 }).pipe(Effect.provide(InProcess)),
137 );
138
139 it.live("a read whose atLeast is never reached fails with ConsistencyTimeout", () =>
140 Effect.gen(function* () {
141 const [inProcess, overTheWire] = yield* Effect.all(
142 [
143 neverCaughtUp.pipe(Effect.provide(InProcess)),
144 neverCaughtUp.pipe(Effect.scoped, Effect.provide(Wire)),
145 ],
146 { concurrency: "unbounded" },
147 );
148 for (const error of [...inProcess, ...overTheWire]) {
149 expect(error).toMatchObject({
150 _tag: "ConsistencyTimeout",
151 view: "Totals",
152 key: "c-3",
153 version: 2,
154 });
155 }
156 }),
157 );
158 });