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

main
ts 55 lines 2.65 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import { customer } from "@bank/domain/Actor";
3 import { Bank, BankHandlers } from "@bank/domain/Bank.model";
4 import { CustomerId } from "@bank/domain/Customer/Customer.id";
5 import { CustomerDashboard } from "@bank/domain/Customer/CustomerDashboard.view";
6 import { CustomerApi } from "@bank/domain/CustomerApi.api";
7 import { MemoryLedgerStore } from "@bank/domain/Ledger/LedgerStore";
8 import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
9 import { expect, it } from "@effect/vitest";
10 import * as Effect from "effect/Effect";
11 import * as HttpServer from "effect/http/HttpServer";
12 import * as Layer from "effect/Layer";
13 import * as Option from "effect/Option";
14 import * as Stream from "effect/Stream";
15 import { SiftFraudCheck } from "../src/Fraud/SiftFraudCheck.ts";
16 import { MfaChallengeLive } from "../src/MfaChallenge.ts";
17 import { SessionLive } from "../src/Session.ts";
18 import { StripePayments } from "../src/Settlement/StripePayments.ts";
19 import { register } from "./flows.ts";
20 import { SessionKeyTest, signedInAs } from "./session.ts";
21
22 // The Worker's HTTP surface (`yield* CustomerApi`, its `fetch`) on a real HTTP server, over the in-memory platform.
23 const Ports = Layer.mergeAll(SiftFraudCheck, StripePayments, MemoryLedgerStore);
24 const World = Layer.mergeAll(SessionLive, MfaChallengeLive).pipe(
25 Layer.provideMerge(
26 Bank.layer(BankHandlers).pipe(Layer.provide(Layer.mergeAll(Ports, Domain.InMemory))),
27 ),
28 Layer.provideMerge(SessionKeyTest),
29 );
30
31 // A test server on a random port, plus an HttpClient pointed at it.
32 const Served = Layer.unwrap(Effect.map(CustomerApi, (api) => HttpServer.serve()(api.fetch))).pipe(
33 Layer.provideMerge(NodeHttpServer.layerTest),
34 Layer.provide(World),
35 );
36
37 // The test server serves the Api at its root; its HttpClient already targets the server.
38 const Client = Layer.mergeAll(Domain.protocolHttp(""), signedInAs(customer("c-1"))).pipe(
39 Layer.provide(SessionKeyTest),
40 );
41
42 it.live("the CustomerApi round-trips over HTTP, including a streamed watch", () =>
43 Effect.gen(function* () {
44 const sam = yield* CustomerApi.client;
45 const accountId = yield* register(sam, "c-1", "sam");
46 yield* sam.DepositMoney(accountId, { amount: 70 });
47 const dashboard = yield* sam.CustomerDashboard.watch(CustomerId.make("c-1")).pipe(
48 Stream.map(({ state }) => state),
49 Stream.filter((d) => d !== null && CustomerDashboard.computed.totalBalance(d) === 70),
50 Stream.runHead,
51 );
52 expect(accountId as string).toBe("c-1-main");
53 expect(Option.getOrThrow(dashboard)).toMatchObject({ name: "sam" });
54 }).pipe(Effect.provide(Client.pipe(Layer.provideMerge(Served))), Effect.scoped),
55 );