ProvenPin Install

XState v5 traffic light state machine: working versions (tested 2026-10-15)

Model a traffic light with XState v5 createMachine and createActor, covering transitions, guards and context updates offline.

passed2 passed · 0 failed2026-10-15 16:06 UTC
Status
passed
Last passed
2026-10-15 16:06 UTC
Tests
2 / 2
Language
TypeScript
Runtime
node v22.23.3
pinned versions3
xstate@5.33.2@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false
npm test
test environment
node v22.23.3
linux 6.17.0-1022-azure
x64
runner: github-actions 38764699834

The code

Exactly the files that passed. Your agent can pull them with get_recipe("xstate-v5-traffic-light") (install), or see them on GitHub.

package.json
{
  "name": "xstate-v5-traffic-light",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node --import tsx --test test/*.test.ts"
  },
  "dependencies": {"xstate":"5.33.2"},
  "devDependencies": {"@types/node":"26.6.5","tsx":"4.23.15"}
}
src/light.ts
import { createMachine, createActor, assign } from "xstate";

export type LightContext = { cycles: number };

export const lightMachine = createMachine({
  id: "light",
  initial: "red",
  types: {} as { context: LightContext; events: { type: "NEXT" } | { type: "EMERGENCY" } },
  context: { cycles: 0 },
  states: {
    red: {
      on: {
        NEXT: { target: "green", actions: assign({ cycles: ({ context }) => context.cycles + 1 }) },
        EMERGENCY: "flashing",
      },
    },
    green: {
      on: {
        NEXT: "yellow",
        EMERGENCY: "flashing",
      },
    },
    yellow: {
      on: {
        NEXT: "red",
        EMERGENCY: "flashing",
      },
    },
    flashing: {
      on: {
        NEXT: "red",
      },
    },
  },
});

export function startLight() {
  const actor = createActor(lightMachine);
  actor.start();
  return {
    value: () => actor.getSnapshot().value,
    cycles: () => actor.getSnapshot().context.cycles,
    next() {
      actor.send({ type: "NEXT" });
    },
    emergency() {
      actor.send({ type: "EMERGENCY" });
    },
    stop() {
      actor.stop();
    },
  };
}
test/light.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { startLight } from "../src/light.ts";

test("cycles red -> green -> yellow -> red", () => {
  const light = startLight();
  assert.equal(light.value(), "red");
  light.next();
  assert.equal(light.value(), "green");
  assert.equal(light.cycles(), 1);
  light.next();
  assert.equal(light.value(), "yellow");
  light.next();
  assert.equal(light.value(), "red");
  light.stop();
});

test("emergency jumps to flashing then recovers", () => {
  const light = startLight();
  light.next();
  light.emergency();
  assert.equal(light.value(), "flashing");
  light.next();
  assert.equal(light.value(), "red");
  light.stop();
});

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.