All writing
3 min read

A motion contract for pixi-reels

Unit tests check where symbols end up. Players notice how they got there. pixi-reels defines reel motion as fourteen laws, checks them in four orientations, and checks the checks by breaking the engine on purpose.

In short

  • Most reel bugs are in the motion before the stop, not in the final symbols, and example-based tests rarely catch them.
  • pixi-reels states reel motion as fourteen laws and checks each one over random geometry in all four orientations.
  • The laws found a defect in the engine that was already shipping, before the refactor started.
  • A law that only compares two layouts misses a bug that breaks both the same way. Each relative check needs an absolute one.

The problem

The server returns cherry, seven, bar and the reel shows cherry, seven, bar. The result is correct, but the motion before it can still be wrong: the strip stretches by a pixel, a wrap happens one symbol late, or the landing frame drifts off the grid after a long session. Players notice these errors even if they cannot name them.

pixi-reels 2.0 added an orientation axis: vertical, horizontal and reversed reels on one engine. About sixty files did coordinate math, and any of them could use the wrong axis. Before the refactor, I wrote the motion down as a contract (ADR 018).

Three kinds of check

CheckCatchesMisses
Golden tracesany change in existing behaviourbugs that are already in the recorded baseline
Lawsviolations of the intended behaviour, on random inputsbehaviour that is wrong but obeys every law
Typesaxis confusion, at compile timeanything about behaviour

Golden traces record behaviour, including its bugs. Laws describe intent. A refactor needs both. With traces only, it passes while it copies old defects onto the new axis.

The laws

Each law is a fast-check property. It runs 300 to 400 random cases (cell size 20 to 200 px, gaps 0 to 20 px, 1 to 6 visible cells) in all four orientation and direction combinations. Some of them:

  • Rigidity. Neighbouring symbols are always exactly one pitch apart. The strip does not stretch.
  • Order. Symbols are always sorted along the travel axis, so an array index equals a screen position.
  • Boundedness. No symbol leaves the strip, however long the session runs.
  • Inverse. advance(d) followed by advance(-d) restores the exact start state. This catches asymmetric wrap thresholds, a bug the engine had already had once.
  • Additivity. advance(a) then advance(b) equals advance(a + b). The frame rate cannot change the result.
  • Wrap count. A travel of d causes floor(d / pitch) wraps, plus or minus one. A missed wrap leaves the landing frame one symbol short.
  • Snap. After snapToGrid(), every symbol is exactly on its cell.
  • Feed edge. Forward motion adds new symbols at the start of the strip, reverse motion at the end, on every wrap.

What the laws found

The laws ran against the engine that was already shipping, not only against the new design. They found a defect in it. They also rejected the motion model the refactor first proposed. Both results came before any refactor code was written.

Checking the checks

A passing suite is useful only if it fails for the right bugs. I changed the engine on purpose in specific ways and recorded which change breaks which law. The table is in tests/contract/README.md.

One result was a gap. A law said that horizontal motion equals vertical motion, transposed. I put a transposition bug into the projection code that both orientations share. The law still passed, because both sides were wrong in the same way.

The same gap appeared twice more: in a test of block bounds, and in the debug overlay's travel arrow, where a mirrored arrow has the same bounds. The rule from this: every relative check needs at least one absolute check next to it.

Headless testing

These checks need an engine that runs without a GPU or a real clock. pixi-reels runs in Node with a fake ticker, and it exports the same harness for game teams:

import { createTestReelSet, expectGrid, captureEvents } from 'pixi-reels/testing';

const cells = [['a','a','a'], ['b','b','b'], ['c','c','c'], ['a','b','c'], ['c','b','a']];
const h = createTestReelSet({ reels: 5, visibleCells: 3, symbolIds: ['a', 'b', 'c'] });

const log = captureEvents(h.reelSet, ['spin:start', 'spin:complete']);
await h.spinAndLand(cells.map((visible) => ({ visible })));

expectGrid(h.reelSet, cells);
expect(log.map((e) => e.event)).toEqual(['spin:start', 'spin:complete']);

Using the approach elsewhere

The method does not depend on pixi-reels:

  1. Write the motion rules down as laws.
  2. Run them over random geometry in every orientation you support.
  3. Record golden traces of reference spins.
  4. Break the engine on purpose and confirm that the tests fail.