Skip to content

In memory

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.

packages/server/test/bankInMemory.ts
import * as Domain from "@alchemy.run/domain";
import * as Layer from "effect/Layer";
import { Bank, BankHandlers } from "@bank/domain/Bank.model";
import { MemoryLedgerStore } from "@bank/domain/Ledger/LedgerStore";
import { SiftFraudCheck } from "../src/Fraud/SiftFraudCheck.ts";
import { MfaChallengeLive } from "../src/MfaChallenge.ts";
import { SessionLive } from "../src/Session.ts";
import { StripePayments } from "../src/Settlement/StripePayments.ts";
import { SessionKeyTest } from "./session.ts";
// The model, its external systems, and the middleware its Apis declare: what a Worker would serve.
export const BankInMemory = Layer.mergeAll(SessionLive, MfaChallengeLive).pipe(
Layer.provideMerge(Bank.layer(BankHandlers)),
Layer.provide(Layer.mergeAll(Domain.InMemory, SiftFraudCheck, StripePayments, MemoryLedgerStore)),
Layer.provideMerge(SessionKeyTest),
);

Policies and projections run for real, and views catch up just after each command, as they do on Cloudflare. A decision records on every instance it reads in one step.

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.

packages/server/test/inMemory.test.ts
import { expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import { CustomerApi } from "@bank/domain/CustomerApi.api";
import { customer } from "@bank/domain/Actor";
import { BankInMemory } from "./bankInMemory.ts";
import { register } from "./flows.ts";
import { signedInAs } from "./session.ts";
it.live("a customer registers and deposits", () =>
Effect.gen(function* () {
const sam = yield* CustomerApi.clientInProcess.pipe(Effect.provide(signedInAs(customer("c-1"))));
const accountId = yield* register(sam, "c-1", "sam");
const { reply } = yield* sam.DepositMoney({ accountId, amount: 100 });
expect(reply.balance).toBe(100);
}).pipe(Effect.scoped, Effect.provide(BankInMemory)),
);

Each test that provides BankInMemory gets a fresh, empty bank.

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.