Skip to main content

State Machines

WEFA uses XState 5 for all complex user flows. Machines make transitions explicit, testable, and easy for collaborators to extend.

XState 5

WEFA uses XState 5 with the setup() API, not createMachine() from v4. The APIs are different.

Machines Overview

Auth Machine

Restores sessions from localStorage on init. Storage keys: wefa_auth_mode, wefa_username, wefa_credential, wefa_rp_id.

Game Session Machine

The game session machine is generic — it injects a GameRulesEngine via machine input rather than containing game-specific logic.

Testing Pattern

import { createActor } from 'xstate';
import { gameSessionMachine } from './gameSessionMachine';

test('transitions from lobby to setup on SELECT_GAME', () => {
const actor = createActor(gameSessionMachine, {
input: { engine: ticTacToeEngine },
});
actor.start();
actor.send({ type: 'SELECT_GAME', gameType: 'tic-tac-toe' });
expect(actor.getSnapshot().value).toBe('setup');
});

All machines require transition coverage for all states.

Key Files

  • src/machines/authMachine.ts
  • src/machines/onboardingMachine.ts
  • src/machines/plantCareMachine.ts
  • src/machines/creatureCareMachine.ts
  • src/machines/creatureEvolveMachine.ts
  • src/machines/creatureGeneratorMachine.ts
  • src/machines/gameSessionMachine.ts