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
| 1 | --- |
| 2 | title: In memory |
| 3 | description: Run a domain model in a single process, for tests and for apps that don't need Cloudflare. |
| 4 | --- |
| 5 | |
| 6 | `Domain.InMemory` runs your whole model in the current process: aggregates, views and policies, with nothing to deploy. Swap it for `DomainCloudflare.layerDurableObjects()` and nothing else changes. |
| 7 | |
| 8 | ## Host the model in memory |
| 9 | |
| 10 | ```typescript |
| 11 | // packages/server/test/bankInMemory.ts |
| 12 | import * as Domain from "@alchemy.run/domain"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import { Bank, BankHandlers } from "@bank/domain/Bank.model"; |
| 15 | import { MemoryLedgerStore } from "@bank/domain/Ledger/LedgerStore"; |
| 16 | import { SiftFraudCheck } from "../src/Fraud/SiftFraudCheck.ts"; |
| 17 | import { MfaChallengeLive } from "../src/MfaChallenge.ts"; |
| 18 | import { SessionLive } from "../src/Session.ts"; |
| 19 | import { StripePayments } from "../src/Settlement/StripePayments.ts"; |
| 20 | import { SessionKeyTest } from "./session.ts"; |
| 21 | |
| 22 | // The model, its external systems, and the middleware its Apis declare: what a Worker would serve. |
| 23 | export const BankInMemory = Layer.mergeAll(SessionLive, MfaChallengeLive).pipe( |
| 24 | Layer.provideMerge(Bank.layer(BankHandlers)), |
| 25 | Layer.provide(Layer.mergeAll(Domain.InMemory, SiftFraudCheck, StripePayments, MemoryLedgerStore)), |
| 26 | Layer.provideMerge(SessionKeyTest), |
| 27 | ); |
| 28 | ``` |
| 29 | |
| 30 | Policies and projections run for real, and views catch up just after each command, as they do on Cloudflare. |
| 31 | |
| 32 | ## Call it through an Api |
| 33 | |
| 34 | `clientInProcess` calls an Api in the same process, through its real middleware. Provide the in-memory model, and sign in with `signedInAs` from [Testing an Api](/domain/testing-apis#sign-sessions-in-tests). |
| 35 | |
| 36 | ```typescript |
| 37 | // packages/server/test/inMemory.test.ts |
| 38 | import { expect, it } from "@effect/vitest"; |
| 39 | import * as Effect from "effect/Effect"; |
| 40 | import { CustomerApi } from "@bank/domain/CustomerApi.api"; |
| 41 | import { customer } from "@bank/domain/Actor"; |
| 42 | import { BankInMemory } from "./bankInMemory.ts"; |
| 43 | import { register } from "./flows.ts"; |
| 44 | import { signedInAs } from "./session.ts"; |
| 45 | |
| 46 | it.live("a customer registers and deposits", () => |
| 47 | Effect.gen(function* () { |
| 48 | const sam = yield* CustomerApi.clientInProcess.pipe(Effect.provide(signedInAs(customer("c-1")))); |
| 49 | const accountId = yield* register(sam, "c-1", "sam"); |
| 50 | const { reply } = yield* sam.DepositMoney(accountId, { amount: 100 }); |
| 51 | expect(reply.balance).toBe(100); |
| 52 | }).pipe(Effect.scoped, Effect.provide(BankInMemory)), |
| 53 | ); |
| 54 | ``` |
| 55 | |
| 56 | Each test that provides `BankInMemory` gets a fresh, empty bank. |
| 57 | |
| 58 | ## Keep stories separate |
| 59 | |
| 60 | Stories run on their own in-memory model with a controlled clock and stubbed external systems, so you don't provide `Domain.InMemory` to them. See [Story](/domain/story). |
| 61 | |
| 62 | :::note |
| 63 | Test more of an Api in [Testing an Api](/domain/testing-apis). |
| 64 | ::: |