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

main
ts 344 lines 10.4 KB
Raw
1 import { AccountId } from "@bank/domain/Account/Account.id";
2 import { StatementLine } from "@bank/domain/Account/Statement.view";
3 import { CustomerId } from "@bank/domain/Customer/Customer.id";
4 import { expect, test } from "@effect/vitest";
5 import * as Array from "effect/Array";
6 import * as Option from "effect/Option";
7 import { Command, given, message, model, story } from "foldkit/story";
8 import { modifyFields } from "foldkit/struct";
9 import {
10 Deliver,
11 init,
12 type Line,
13 Message,
14 type Model,
15 BankClient,
16 SaveIntents,
17 shownBalance,
18 update,
19 } from "../src/Bank.ts";
20
21 const earlier: Line = {
22 commandId: "earlier",
23 kind: "deposit",
24 amount: 5_000,
25 balance: Option.some(5_000),
26 ack: "Confirmed",
27 reason: Option.none(),
28 };
29
30 const account: Model = {
31 customerId: CustomerId.make("c-1"),
32 accountId: AccountId.make("c-1-main"),
33 commandIds: { session: "s", sent: 0 },
34 summaryCursor: Option.none(),
35 statementCursor: Option.none(),
36 feedState: "Live",
37 online: true,
38 balance: Option.some(5_000),
39 lines: [earlier],
40 error: Option.none(),
41 };
42
43 const saved = Command.resolve(SaveIntents, Message.CompletedSaveIntents());
44
45 const accepted = (commandId: string) =>
46 Message.CompletedDeliver({
47 outcomes: [{ commandId, result: "Accepted", reason: Option.none() }],
48 });
49
50 const deposited = (commandId: string, balance: number) =>
51 Message.ReceivedLine({
52 commandId,
53 line: StatementLine.make({ kind: "deposit", amount: 1_000, balance }),
54 cursor: 1,
55 });
56
57 const acks = (model: Model) => model.lines.map((l) => [l.commandId, l.ack]);
58
59 // VARIANT 1: online, with optimistic UI.
60
61 test("a deposit shows at once as pending; its event confirms it", () =>
62 story(
63 update,
64 given(account),
65 message(Message.ClickedDeposit()),
66 saved,
67 model((model) => {
68 expect(Option.getOrThrow(Array.head(model.lines))).toMatchObject({
69 commandId: "s:0",
70 ack: "Pending",
71 balance: Option.none(),
72 });
73 expect(shownBalance(model)).toEqual(Option.some(6_000));
74 }),
75 Command.resolve(Deliver, accepted("s:0")),
76 saved,
77 // Accepted is not acknowledged: still pending until the statement says so.
78 model((model) => expect(Option.getOrThrow(Array.head(model.lines)).ack).toBe("Pending")),
79 message(deposited("s:0", 6_000)),
80 saved,
81 model((model) => {
82 expect(acks(model)).toEqual([
83 ["s:0", "Confirmed"],
84 ["earlier", "Confirmed"],
85 ]);
86 expect(shownBalance(model)).toEqual(Option.some(6_000));
87 }),
88 ));
89
90 test("the event can arrive before the reply; the reply changes nothing", () =>
91 story(
92 update,
93 // The deposit is in flight: its line is pending, its reply not yet back.
94 given(
95 modifyFields(account, {
96 commandIds: () => ({ session: "s", sent: 1 }),
97 lines: () => [
98 { ...earlier, commandId: "s:0", amount: 1_000, balance: Option.none(), ack: "Pending" },
99 earlier,
100 ],
101 }),
102 ),
103 message(deposited("s:0", 6_000)),
104 saved,
105 message(accepted("s:0")),
106 saved,
107 model((model) => {
108 expect(Option.getOrThrow(Array.head(model.lines))).toMatchObject({
109 commandId: "s:0",
110 ack: "Confirmed",
111 balance: Option.some(6_000),
112 });
113 expect(shownBalance(model)).toEqual(Option.some(6_000));
114 }),
115 ));
116
117 test("a rejected withdrawal is marked rejected and no longer moves the balance", () =>
118 story(
119 update,
120 given(account),
121 message(Message.ClickedWithdraw()),
122 saved,
123 model((model) => expect(shownBalance(model)).toEqual(Option.some(4_000))),
124 Command.resolve(
125 Deliver,
126 Message.CompletedDeliver({
127 outcomes: [
128 { commandId: "s:0", result: "Rejected", reason: Option.some("InsufficientFunds") },
129 ],
130 }),
131 ),
132 saved,
133 model((model) => {
134 expect(Option.getOrThrow(Array.head(model.lines))).toMatchObject({
135 ack: "Rejected",
136 reason: Option.some("InsufficientFunds"),
137 });
138 expect(shownBalance(model)).toEqual(Option.some(5_000));
139 }),
140 ));
141
142 test("an event this browser did not ask for lands below its unacknowledged lines", () =>
143 story(
144 update,
145 given(account),
146 message(Message.ClickedDeposit()),
147 saved,
148 Command.resolve(Deliver, accepted("s:0")),
149 saved,
150 // A transfer arrives from elsewhere while the deposit is in flight.
151 message(
152 Message.ReceivedLine({
153 commandId: "transfer-t-1",
154 line: StatementLine.make({ kind: "transfer-in", amount: 2_000, balance: 7_000 }),
155 cursor: 1,
156 }),
157 ),
158 saved,
159 model((model) => {
160 expect(acks(model)).toEqual([
161 ["s:0", "Pending"],
162 ["transfer-t-1", "Confirmed"],
163 ["earlier", "Confirmed"],
164 ]);
165 expect(shownBalance(model)).toEqual(Option.some(8_000));
166 }),
167 ));
168
169 // VARIANT 2: offline-capable, recording intents.
170
171 test("offline, a deposit is queued; back online, it is sent and confirmed", () =>
172 story(
173 update,
174 given(modifyFields(account, { online: () => false })),
175 // Nothing is sent while offline: the intent is only kept.
176 message(Message.ClickedDeposit()),
177 saved,
178 model((model) => {
179 expect(Option.getOrThrow(Array.head(model.lines))).toMatchObject({
180 commandId: "s:0",
181 ack: "Queued",
182 });
183 expect(shownBalance(model)).toEqual(Option.some(6_000));
184 }),
185 message(Message.WentOnline()),
186 model((model) => expect(Option.getOrThrow(Array.head(model.lines)).ack).toBe("Pending")),
187 Command.resolve(Deliver, accepted("s:0")),
188 saved,
189 message(deposited("s:0", 6_000)),
190 saved,
191 model((model) =>
192 expect(Option.getOrThrow(Array.head(model.lines))).toMatchObject({ ack: "Confirmed" }),
193 ),
194 ));
195
196 test("a send that cannot reach the server is queued, and so is everything after it", () =>
197 story(
198 update,
199 given(account),
200 message(Message.ClickedDeposit()),
201 saved,
202 Command.resolve(
203 Deliver,
204 Message.CompletedDeliver({
205 outcomes: [{ commandId: "s:0", result: "Queued", reason: Option.none() }],
206 }),
207 ),
208 saved,
209 // A later withdrawal waits behind the queued deposit instead of overtaking it.
210 message(Message.ClickedWithdraw()),
211 saved,
212 model((model) =>
213 expect(acks(model)).toEqual([
214 ["s:1", "Queued"],
215 ["s:0", "Queued"],
216 ["earlier", "Confirmed"],
217 ]),
218 ),
219 ));
220
221 test("a queued withdrawal the account refuses on sync stops counting", () =>
222 story(
223 update,
224 given(modifyFields(account, { online: () => false })),
225 message(Message.ClickedWithdraw()),
226 saved,
227 model((model) => expect(shownBalance(model)).toEqual(Option.some(4_000))),
228 message(Message.WentOnline()),
229 Command.resolve(
230 Deliver,
231 Message.CompletedDeliver({
232 outcomes: [
233 { commandId: "s:0", result: "Rejected", reason: Option.some("InsufficientFunds") },
234 ],
235 }),
236 ),
237 saved,
238 model((model) => {
239 expect(Option.getOrThrow(Array.head(model.lines))).toMatchObject({
240 ack: "Rejected",
241 reason: Option.some("InsufficientFunds"),
242 });
243 expect(shownBalance(model)).toEqual(Option.some(5_000));
244 }),
245 ));
246
247 test("intents from an earlier page load are sent, oldest first, when the app boots", () => {
248 const bankInit = init({
249 customerId: CustomerId.make("c-1"),
250 session: "next",
251 online: true,
252 intents: [
253 { commandId: "old:0", kind: "deposit", amount: 1_000 },
254 { commandId: "old:1", kind: "withdrawal", amount: 500 },
255 ],
256 });
257 expect(acks(bankInit.model)).toEqual([
258 ["old:1", "Pending"],
259 ["old:0", "Pending"],
260 ]);
261 expect(bankInit.commands).toHaveLength(1);
262 });
263
264 test("an intent the server already applied is confirmed by the statement's history", () =>
265 story(
266 update,
267 given(
268 modifyFields(account, {
269 lines: () => [
270 { ...earlier, commandId: "old:0", amount: 1_000, balance: Option.none(), ack: "Pending" },
271 earlier,
272 ],
273 }),
274 ),
275 // The statement replays history; the earlier page load's deposit is in it.
276 message(deposited("old:0", 6_000)),
277 saved,
278 // Its resend is answered from the account's stored receipt: no second deposit.
279 message(accepted("old:0")),
280 saved,
281 model((model) => {
282 expect(Option.getOrThrow(Array.head(model.lines))).toMatchObject({
283 commandId: "old:0",
284 ack: "Confirmed",
285 });
286 expect(shownBalance(model)).toEqual(Option.some(6_000));
287 }),
288 ));
289
290 test("a new customer opens their account; it appears through the subscription", () =>
291 story(
292 update,
293 given(modifyFields(account, { balance: () => Option.none(), lines: () => [] })),
294 message(Message.ClickedRegister()),
295 Command.resolve(
296 BankClient.RegisterCustomer,
297 BankClient.Message.SucceededRegisterCustomer({
298 commandId: "s:0",
299 reply: undefined,
300 receipt: { stream: "Customer/c-1", version: 1 },
301 }),
302 ),
303 model((model) => expect(model.balance).toEqual(Option.none())),
304 message(Message.ReceivedBalance({ balance: Option.some(0), cursor: 1 })),
305 model((model) => expect(model.balance).toEqual(Option.some(0))),
306 ));
307
308 test("a denied line allows new intents and retries with the same id", () => {
309 const denied = modifyFields(account, {
310 lines: () => [
311 { ...earlier, commandId: "denied", ack: "Denied", reason: Option.some("MfaRequired") },
312 earlier,
313 ],
314 });
315 const deposit = update(denied, Message.ClickedDeposit());
316 expect(Option.getOrThrow(Array.head(deposit.model.lines)).ack).toBe("Pending");
317 const retry = update(denied, Message.ClickedRetryLine({ commandId: "denied" }));
318 expect(Option.getOrThrow(Array.head(retry.model.lines))).toMatchObject({
319 commandId: "denied",
320 ack: "Pending",
321 reason: Option.none(),
322 });
323 expect(retry.commands?.find((command) => command.name === "Deliver")?.args).toMatchObject({
324 intents: [{ commandId: "denied" }],
325 });
326 });
327
328 test("stale snapshot Flags fall back to empty state", () => {
329 const bankInit = init({
330 customerId: account.customerId,
331 session: "fresh",
332 online: false,
333 intents: [],
334 summary: { state: { oldBalance: 1 }, cursor: 2 },
335 });
336 expect(bankInit.model.balance).toEqual(Option.none());
337 expect(bankInit.model.summaryCursor).toEqual(Option.none());
338 });
339
340 test("a caught-up marker records the cursor and live status", () => {
341 const caughtUp = update(account, Message.CaughtUpStatement({ cursor: 5 }));
342 expect(caughtUp.model.statementCursor).toEqual(Option.some(5));
343 expect(caughtUp.model.feedState).toBe("Live");
344 });