Skip to content
Get started

Test rules with Stories

View as Markdown

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.

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:

src/Round.test.ts
import { Schemas } from "@minekube/paper-types/schemas";
import { describe, expect, test } from "bun:test";
import { Component } from "effect/ui";
import { Story } from "effect/ui/testing";
import { Round, RoundMessage, ShowScore } from "./Round.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("…").

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:

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.

A Command declared with success answers with a message. In a Story you give that answer with resolve. The bot of the launcher example shows a chain of three:

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 });

Was this helpful?