# Test rules with Stories

> Test a Minekube game's rules in Bun with Effect UI Stories - send messages, check every Command they issue, and let Story.explore try every order of a set of messages.

The rules of a Minekube game are an Effect UI component that imports no Java. That is why you can test them in Bun, without a server, in milliseconds. A Story is the test tool for such a component.

## Write a Story

A Story starts a component, sends it messages and checks the Commands each step issues. This test comes with every project that `mk init` creates:

```ts title="src/Round.test.ts"

const PlayerId = Schemas["java.util.UUID"];
const alex = PlayerId.make("36532b5e-c442-3dbb-a24c-c7e55d0f979a");
const sam = PlayerId.make("77bfcc1c-1831-3a3c-84d6-63015aaf34af");

describe("Round rules", () => {
  test("each placed block scores a point for its player", () => {
    const story = Story.start(Round, {})
      .send(RoundMessage.PlacedBlock({ player: alex }))
      .expectExact(ShowScore.make({ player: alex, score: 1 }))
      .resolve(ShowScore, Component.noop)
      .send(RoundMessage.PlacedBlock({ player: alex }))
      .expectExact(ShowScore.make({ player: alex, score: 2 }))
      .resolve(ShowScore, Component.noop);
    expect(story.model.scores).toEqual({ [alex]: 2 });
  });
});
```

Run it with `bun test`.

- **A Story is the full account of what the component did.** Every step throws while an earlier Command is unresolved. Resolve a silent Command with `Component.noop`. Resolve a Command with `success` with the message it would return.
- **Acknowledge the Commands of `init` first.** If `init` issues a Command, expect and resolve it before the first message: `Story.start(Launcher, {}).expectExact(PlaceSign.make()).resolve(PlaceSign, Component.noop)`.
- **`Story.start` has no end check.** `Story.make` is scoped and also fails on anything left pending when the test ends.
- **Build checked ids** with `Schemas["java.util.UUID"].make("…")`.

## Try every order with `Story.explore`

A rule often has to hold no matter in which order players act. `Story.explore` sends every order of the given messages, up to a depth, and checks a rule at every step:

```ts title="src/Round.test.ts"
test("scores never mix up players, in any order", () => {
  Story.explore(Round, {}, {
    messages: [RoundMessage.PlacedBlock({ player: alex }), RoundMessage.PlacedBlock({ player: sam })],
    transition: ({ before, after, message }) => {
      for (const player of [alex, sam]) {
        const scored = message.player === player ? 1 : 0;
        expect(after.scores[player] ?? 0).toBe((before.scores[player] ?? 0) + scored);
      }
    },
    depth: 4,
  });
});
```

- `transition: ({ before, after, message }) => …` states a rule for every step.
- `invariant` states a rule for every Model the exploration reaches.
- Silent Commands settle by themselves, so you only describe the rule.

## Test a Command that returns a message

A Command declared with `success` answers with a message. In a Story you give that answer with `resolve`. The bot of the [launcher example](https://minekube.com/games/examples/launcher) shows a chain of three:

```ts title="client/LauncherBot.test.ts"
const story = Story.start(LauncherBot, {})
  .expectExact(FindSign.make())
  .resolve(FindSign, BotMessage.FoundSign({ sign }))
  .expectExact(WalkTo.make({ sign }))
  .resolve(WalkTo, BotMessage.Arrived())
  .expectExact(ClickSign.make({ sign }))
  .resolve(ClickSign, BotMessage.Launched({ rise: 13.6 }));
expect(story.model).toEqual({ sign, rise: 13.6 });
```

## Related

- [Recordings and replay](https://minekube.com/games/testing/recordings): turn a real session into a test.
- [Games as Effect components](https://minekube.com/games/write/effect-model): why rules, Commands and Stories fit together.
