Test rules with Stories
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
Section titled “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:
import { Schemas } from "@minekube/paper/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("b2fd8387-c4d5-3f70-bdad-86ce5faea46a");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 withsuccesswith the message it would return. - Acknowledge the Commands of
initfirst. Ifinitissues a Command, expect and resolve it before the first message:Story.start(Launcher, {}).expectExact(PlaceSign.make()).resolve(PlaceSign, Component.noop). Story.starthas no end check.Story.makeis 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
Section titled “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:
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.invariantstates 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
Section titled “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 shows a chain of three:
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
Section titled “Related”- Recordings and replay: turn a real session into a test.
- Games as Effect components: why rules, Commands and Stories fit together.
Was this helpful?
Thanks for the feedback.