@earnestjs/xstate 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ import type { StatePath, TraversalOptions } from "@xstate/graph";
2
+ import type { ActorLogic, EventObject, Snapshot } from "xstate";
3
+ /**
4
+ * A path generator for `TestModel.getPaths` from `@xstate/graph`: `model.getPaths(earnestPaths)`
5
+ * gives one shortest path to every state reachable from the start, found by the breadth-first
6
+ * `search` of `@earnestjs/model`, in place of `model.getShortestPaths()`. Each path has the shape
7
+ * XState's own shortest paths have: a first step with the start snapshot and the event
8
+ * `xstate.init`, then one step per event with the snapshot after it, and `weight`, the number of
9
+ * events. States are told apart by `serializeState`, and `events`, `fromState`, `input`,
10
+ * `stopWhen` and `toState` work as they do for `getShortestPaths`. It takes the events and the
11
+ * `serializeState` that `getPaths` passes on: called on its own with neither, it sends no event
12
+ * and keys a snapshot by `JSON.stringify`. A finite `limit` is the most distinct states the
13
+ * search may hold, a whole number that is not negative; when the search would hold more, it
14
+ * throws an error that names the limit and the count, and gives no paths. Paths come in the
15
+ * order the search reached their states, which may differ from XState's order.
16
+ */
17
+ export declare const earnestPaths: <TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TInput>(logic: ActorLogic<TSnapshot, TEvent, TInput>, options: TraversalOptions<TSnapshot, TEvent, TInput>) => StatePath<TSnapshot, TEvent>[];
package/dist/paths.js ADDED
@@ -0,0 +1,49 @@
1
+ import { search } from "@earnestjs/model";
2
+ import { stepping } from "./machine.js";
3
+ const toStatePath = ({ start, steps, }) => {
4
+ const initStep = {
5
+ state: start.snapshot,
6
+ event: { type: "xstate.init" },
7
+ };
8
+ return {
9
+ state: (steps.at(-1)?.state ?? start).snapshot,
10
+ steps: [initStep, ...steps.map(({ move, state }) => ({ state: state.snapshot, event: move }))],
11
+ weight: steps.length,
12
+ };
13
+ };
14
+ /**
15
+ * A path generator for `TestModel.getPaths` from `@xstate/graph`: `model.getPaths(earnestPaths)`
16
+ * gives one shortest path to every state reachable from the start, found by the breadth-first
17
+ * `search` of `@earnestjs/model`, in place of `model.getShortestPaths()`. Each path has the shape
18
+ * XState's own shortest paths have: a first step with the start snapshot and the event
19
+ * `xstate.init`, then one step per event with the snapshot after it, and `weight`, the number of
20
+ * events. States are told apart by `serializeState`, and `events`, `fromState`, `input`,
21
+ * `stopWhen` and `toState` work as they do for `getShortestPaths`. It takes the events and the
22
+ * `serializeState` that `getPaths` passes on: called on its own with neither, it sends no event
23
+ * and keys a snapshot by `JSON.stringify`. A finite `limit` is the most distinct states the
24
+ * search may hold, a whole number that is not negative; when the search would hold more, it
25
+ * throws an error that names the limit and the count, and gives no paths. Paths come in the
26
+ * order the search reached their states, which may differ from XState's order.
27
+ */
28
+ export const earnestPaths = (logic, options) => {
29
+ const { limit } = options;
30
+ const isBounded = limit !== undefined && limit !== Infinity;
31
+ if (isBounded && !(Number.isInteger(limit) && limit >= 0)) {
32
+ throw new Error(`limit must be a whole number that is not negative, or Infinity, not ${limit}`);
33
+ }
34
+ const found = search({
35
+ ...stepping(logic, options),
36
+ ...(isBounded ? { maxStates: limit } : {}),
37
+ });
38
+ if (found.reason !== null) {
39
+ throw new Error(`earnestPaths stopped at the ${found.reason} after ${found.states} states, before it reached every state; raise limit to search them all`);
40
+ }
41
+ const paths = [];
42
+ for (const trail of found.reached()) {
43
+ const path = toStatePath(trail);
44
+ if (options.toState?.(path.state) ?? true) {
45
+ paths.push(path);
46
+ }
47
+ }
48
+ return paths;
49
+ };
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "@earnestjs/xstate",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.1",
4
+ "description": "Earnest's breadth-first state search for XState: a path generator for model-based tests and a check of rules and stuck states",
5
+ "homepage": "https://earnest.cpeaustriajc.dev",
6
+ "license": "LGPL-3.0-or-later",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/cpeaustriajc/earnest.git",
10
+ "directory": "packages/xstate"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "README.md",
15
+ "LICENSE",
16
+ "LICENSE.LESSER"
17
+ ],
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "publishConfig": {
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "access": "public",
33
+ "provenance": true
34
+ },
35
+ "scripts": {
36
+ "build": "tsrs -p tsconfig.build.json",
37
+ "prepack": "node --eval \"fs.rmSync('dist', { recursive: true, force: true })\" && bun run build && node scripts/publish-manifest.ts apply",
38
+ "postpack": "node scripts/publish-manifest.ts restore",
39
+ "typecheck": "tsrs --noEmit",
40
+ "format": "oxfmt --check",
41
+ "format:fix": "oxfmt",
42
+ "test": "vitest run"
43
+ },
44
+ "dependencies": {
45
+ "@earnestjs/model": "0.2.1"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^26.6.6",
49
+ "@xstate/graph": "^3.0.4",
50
+ "oxfmt": "^0.79.0",
51
+ "tsrs": "^7.1.0-beta.tsrs4",
52
+ "vitest": "^5.1.1",
53
+ "xstate": "^5.33.4"
54
+ },
55
+ "peerDependencies": {
56
+ "@xstate/graph": "^3.0.4",
57
+ "xstate": "^5.19.4"
58
+ },
59
+ "engines": {
60
+ "node": ">=22.18"
61
+ }
62
+ }