Test your story
Drive a play by naming each reply, check every scene without playing it, and explore every combination of choices to reach each ending.
Last updated by
After reading this guide, you will know
- How to drive a scene with
createTestPlayand assert on its state - How
check()andexplore()find problems no single play reaches - How to replay the path
explore()reports for each ending
A branching story hides its bugs on the paths nobody played. @sdxc/novel/test runs a play
without a browser: createTestPlay follows the replies you name, check() reads every scene
without playing it, and explore() plays every combination of choices and reports each ending
with the shortest path to it.
npm add @sdxc/novelTest a scene
createTestPlay starts a play from the initial scene, or from scene with state. A fixed
seed replays every random draw, and persistent stands in for earlier playthroughs. A reply
that does not fit the current frame throws, so the test fails where the story went wrong:
import { createTestPlay } from "@sdxc/novel/test";
import { expect, test } from "vitest";
import { novel } from "~/app/initializers/novel";
import { SCENES } from "~/resources/novel/declarations";
test("a cocoa warms you up on the way to the counter", async () => {
let play = createTestPlay(novel, { seed: "cocoa" });
await play.advanceUntil("choice");
await play.choose("opening.cocoa");
expect(play.state.warmth).toBe(2);
expect(play.scene).toBe(SCENES.prologue.counter);
});
test("the counter answers a warm customer with a smile", async () => {
let play = createTestPlay(novel, {
scene: SCENES.prologue.counter,
state: { cups: 1, warmth: 2 },
});
while (play.ending === undefined) await play.advance();
expect(play.transcript).toContainEqual(
expect.objectContaining({ key: "counter.smile" }),
);
expect(play.ending?.key).toBe("endings.regular");
});advance() answers one line, advanceUntil(type) advances to the next frame of that type, and
transcript and events keep every line, option and event so far.
Check every scene
check() reads each mapped scene without playing a line. Errors break a play or a save: a key
used twice in a scene, a jump to a scene with no module, an undeclared asset or character.
Warnings may be intended, such as a scene the initial one never reaches:
import { check } from "@sdxc/novel/test";
import { expect, test } from "vitest";
import { novel } from "~/app/initializers/novel";
test("the story has no problems", async () => {
let report = await check(novel);
expect(report.ok).toBe(true);
expect(report.problems).toEqual([]);
});Warnings also name every extra a reader can never unlock: a sprite no scene draws
(unshown-sprite), a CG no scene shows (unshown-image), a track no scene plays
(unplayed-music), an entry no scene teaches (unlearned-entry), and a listed character no
scene introduces, gives a line or draws (unmet-character). Every branch of every mapped scene
counts. When an extra stays locked on purpose, filter it out by name:
let { problems } = await check(novel);
expect(
problems.filter(
(problem) => !("asset" in problem && problem.asset === "music.credits"),
),
).toEqual([]);Reach every ending
explore() plays every enabled option of every menu and reports each ending with its shortest
path, menus where every option is locked, and keys a catalog lacks. Each path is a seed and its
steps, so follow() replays it:
import { createTestPlay, explore } from "@sdxc/novel/test";
import { expect, test } from "vitest";
import { novel } from "~/app/initializers/novel";
test("every ending is reachable, and each path replays", async () => {
let report = await explore(novel);
expect(report.endings.map((ending) => ending.key).sort()).toEqual([
"endings.rain",
"endings.regular",
]);
expect(report.deadEndMenus).toEqual([]);
expect(report.missingMessages).toEqual({});
for (let ending of report.endings) {
let play = createTestPlay(novel, { seed: ending.seed });
await play.follow(ending.steps);
expect(play.ending?.key).toBe(ending.key);
}
});Loops and hubs are walked once per state, so the walk finishes. maxPaths and maxDepth cap it
for a novel with thousands of branches, and persistent explores what a second playthrough
unlocks:
let regulars = await explore(novel, {
persistent: { endings: ["endings.regular"] },
maxPaths: 5000,
});Where to go next
Add an interaction — the next guide: a minigame the story hands the screen to.
Saves, settings and unlocks — the previous guide, whose unlock the
persistentoption explores.@sdxc/novel— every field ofcreateTestPlay,check()andexplore().