Services, people, worlds and stand-ins
The shop fixture in Rehearse’s repository sets its scene with all four blocks.
base: mainservices: api: up: node packages/api/server.mjs --port {{port}} port: auto ready: http://127.0.0.1:{{port}}/health isolate: { env: SHOP_STATE }signIn: { command: node people.mjs, service: api }people: owner: { describe: Runs the shop, default: true } member: { describe: A customer with an account }worlds: empty: { describe: No items } stocked: describe: Three items in stock extends: empty add: node seed.mjs {{api_url}} default: truestandins: payments: replaces: The payment provider by: packages/api/server.mjs (its fake provider) services: [api] env: { PAYMENTS: fake } real: { env: { PAYMENTS: sandbox } }A repository says in rehearse.yaml what it has. Four blocks set the scene a scenario runs in:
| Block | What it is | Why |
|---|---|---|
services |
What comes up: a server, an app, a daemon | A scenario needs the product running |
people |
Who you sign in as | The same request means different things to different people |
worlds |
Named starting states that build on each other | A scenario starts from a known state, on both sides |
standins |
Fakes for what can’t run locally | So the product can run, and every result says what was fake |
Every block is optional. A repository of React components may declare no services at all. A command-line tool may declare only file scenarios. Declare what you have.
Every field is listed in the rehearse.yaml reference. This page explains what each block does.
Services
Section titled “Services”A service is something Rehearse brings up before a scenario runs and stops after it.
services: api: up: node packages/api/server.mjs --port {{port}} port: auto ready: http://127.0.0.1:{{port}}/health timeout: 20 isolate: { env: SHOP_STATE }Up, ready, down
Section titled “Up, ready, down”| Step | What Rehearse does |
|---|---|
| up | Runs up in the checkout (or in cwd, relative to the checkout), in its own process group, with its output in .rehearse/logs/. |
| ready | Waits until the service is ready, checking every quarter second, for timeout seconds (60 by default). |
| down | Runs down if you gave one, then ends the process group, then removes the service’s scratch folder. |
ready takes one of three forms:
| Form | Ready when |
|---|---|
ready: http://127.0.0.1:{{port}}/health |
the URL answers with a status under 500 |
ready: pnpm drive snapshot --json |
the command exits 0 |
ready: { log: 'listening on' } |
the service’s log matches the regular expression |
A string that starts with http:// or https:// is a URL; any other string is a command. Without ready, a service
with a port is ready when something answers on that port, and a service without a port is ready at once.
If the service exits before it is ready, or isn’t ready in time, Rehearse stops it and says so with the last lines of its log:
api stopped before it was ready (exit 1): … Its log: …/.rehearse/logs/api@4f20e84ecf.logport: auto picks a free port on 127.0.0.1. The service gets it as {{port}} and as the PORT variable. Its URL is
http://127.0.0.1:{{port}} unless you give url. A fixed port that something already answers on is refused, so two
copies never talk to each other by mistake.
Other commands reach a service by name: {{api_url}} and {{api_port}}. {{url}} is the default service’s URL (the
service named api, else the first with a URL, unless defaults.service says otherwise).
Isolate
Section titled “Isolate”The base and the head can each have the service up. isolate: { env: SHOP_STATE } gives each copy its own scratch
folder, through that environment variable, so they never share a database file or a socket. Every service also gets
its scratch folder as REHEARSE_SCRATCH and as {{scratch}} in its up, down and env. A service with its
own driver can use it to keep each copy’s driver state apart:
env: { DRIVE_DIR: '{{scratch}}/drive' }Daemons
Section titled “Daemons”Some up commands start a daemon and exit 0. That is fine: Rehearse keeps the service as running, asks its ready
again before it uses it later, and stops it with its down command. Give such a service a down.
The registry
Section titled “The registry”Rehearse keeps what it brought up in .rehearse/services/, one file per service and checkout. Later commands find it
there. Four commands work with it.
rehearse up # brings every service up, builds the default world, and leaves them runningrehearse services # what can come up, and what is up nowrehearse down # stops what Rehearse brought up in this checkoutrehearse down --all # in every checkoutrehearse up says where each service answers and which world it built.
$ rehearse upapi is up at http://127.0.0.1:43639 (pid 438228)World: stocked.Stop with `rehearse down`.A scenario run while a service is already up uses it, if it was built with the same world. If it was built with another world, the scenario isn’t run, and Rehearse says which world is up and how to stop it.
One set at a time
Section titled “One set at a time”A machine runs one set of services at a time by default. Before Rehearse brings services up in one checkout, it stops those it brought up in any other checkout:
The budget allows 1 set of services at once: stopping those in …/shop/.rehearse/checkouts/c68f00d9431fSet budget.services: 2 to allow two. A comparison runs the base, then the head, never both at once.
Interrupted runs
Section titled “Interrupted runs”If you stop Rehearse with Ctrl-C (or it gets SIGTERM), it stops every service that command brought up before it
ends. Services you asked to keep, with rehearse up or rehearse run --keep, stay up until rehearse down.
People
Section titled “People”A person is someone you can sign in as. The manifest names each person, and the command that signs them in.
signIn: { command: node people.mjs, service: api }people: owner: { describe: Runs the shop, default: true } member: { describe: A customer with an account }signIn is a command that prints everyone’s token as JSON, in the sign-in shape. The shop’s people.mjs prints this
object, indented here.
{ "people": { "owner": { "token": "local-owner-0001", "role": "owner" }, "member": { "token": "local-member-0002", "role": "member" } }, "variables": { "shop": "main-street" }}A person’s value is a token, or an object with a token and other fields. Each other field becomes a variable named
<person>_<field> (owner_role). variables adds more. service: api means the sign-in needs api up first.
rehearse people says who each person is, and hides each token.
$ rehearse peopleowner (default) — local: Runs the shop · token (16 characters, hidden; --json prints it)member — local: A customer with an account · token (17 characters, hidden; --json prints it)variables: owner_role, member_role, shopA person signIn doesn’t cover can have their own token: { command: … } or token: { env: … }. A person a browser
signs in as needs browser: (a header, storage values, cookies, or sign-in steps): see
sign in in the flows guide.
real says how real a person is: local (a sign-in the local services trust, the default), stand-in (a fake
identity) or real (a real account).
The names you, nobody and api are reserved: they are Rehearse’s own. nobody sends a call signed in as no one:
a call with no Authorization header is sent as nobody. api is the service’s address, {{api}}. you is kept for
whoever runs Rehearse.
Rehearse replaces every token with {{name}} in what it keeps and prints: observations, the cache, the stage’s
records and every line it writes. rehearse people prints tokens only with --json.
Worlds
Section titled “Worlds”A world is a named starting state. A world can extend another and add to it.
worlds: empty: { describe: No items } stocked: describe: Three items in stock extends: empty add: node seed.mjs {{api_url}} default: truerehearse worlds lists each world with the chain it is built from.
$ rehearse worldsempty — No itemsstocked (default) — Three items in stock built from empty → stockedTo build a world, Rehearse runs each world in its chain in order, from the root. Each world has two commands.
| Field | When it runs |
|---|---|
before |
Before any service comes up, such as writing files a service reads at start. No service is up yet, so {{port}}, {{url}} and {{scratch}} aren’t filled. |
add |
Once services are up, such as seeding through the API |
Rehearse builds a world when it brings services up fresh for it: for rehearse up, a run or a comparison,
rehearse drive start, and the first call you send from the stage. A world whose chain has an add needs every
service up.
A world’s commands see {{world}}, {{checkout}}, {{manifest}}, and each running service’s {{<name>_url}} and
{{<name>_port}}. They run in the checkout, with up to 5 minutes each.
A scenario starts from its own world, else the default world (default: true, or defaults.world). --world <name>
runs it from another.
Worlds in TypeScript
Section titled “Worlds in TypeScript”defineWorld writes the same chain in code:
import { defineWorld, worldMain } from '@kurvball/rehearse/worlds';
const empty = defineWorld({ name: 'empty', add: async () => {} });const stocked = defineWorld({ name: 'stocked', extends: 'empty', add: async ({ url }) => { await fetch(`${url}/items`, { method: 'POST', body: JSON.stringify({ name: 'Pen' }) }); },});
await worldMain([empty, stocked]);The manifest’s add runs that file with the world’s name and the service’s address.
worlds: stocked: { add: 'node worlds.ts stocked --url {{api_url}}' }worldMain reads the world’s name and --url, builds the chain root first, and prints
{"v": 1, "world": "stocked", "built": ["empty", "stocked"]}.
Without --url, worldMain reads $REHEARSE_URL. Rehearse sets it to the default service’s URL, once that service
is up, whenever it builds a world: for a run, a comparison, rehearse up, drive start or the stage. --url, as
above, names the URL outright.
Stand-ins
Section titled “Stand-ins”A stand-in is a fake for something that can’t run locally: a payment provider, a model, an identity provider.
standins: payments: replaces: The payment provider by: packages/api/server.mjs (its fake provider) fidelity: fake services: [api] env: { PAYMENTS: fake } real: { env: { PAYMENTS: sandbox } }A stand-in is an environment preset on the services it names (every service when services is left out). Rehearse sets its env on
those services while it is used.
| Field | Says |
|---|---|
replaces |
What it stands in for |
by |
What plays it: a file, a package, a command |
fidelity |
fake (written by hand, the default), recorded (replays real answers) or local (the real thing, run locally) |
real |
The environment that uses the real dependency instead |
--real payments on run, compare or up uses real.env instead. A stand-in without real can’t be swapped for
the real one locally, and Rehearse says so.
The text of a comparison ends with the stand-ins it ran with.
Stand-ins: payments (fake, for The payment provider).- Scenarios: what runs in this scene.
- Comparing base and head: how two runs become a verdict.
- rehearse.yaml reference: every field and its default.