effect-machine 0.3.0 → 0.3.2

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.
Files changed (61) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +18 -0
  2. package/dist/actor.d.ts +251 -0
  3. package/dist/actor.js +385 -0
  4. package/dist/cluster/entity-machine.d.ts +90 -0
  5. package/dist/cluster/entity-machine.js +74 -0
  6. package/dist/cluster/index.d.ts +3 -0
  7. package/dist/cluster/index.js +4 -0
  8. package/dist/cluster/to-entity.d.ts +64 -0
  9. package/dist/cluster/to-entity.js +53 -0
  10. package/dist/errors.d.ts +61 -0
  11. package/dist/errors.js +38 -0
  12. package/dist/index.d.ts +13 -0
  13. package/dist/index.js +14 -0
  14. package/dist/inspection.d.ts +125 -0
  15. package/dist/inspection.js +50 -0
  16. package/dist/internal/brands.d.ts +40 -0
  17. package/dist/internal/brands.js +0 -0
  18. package/dist/internal/inspection.d.ts +11 -0
  19. package/dist/internal/inspection.js +15 -0
  20. package/dist/internal/transition.d.ts +159 -0
  21. package/dist/internal/transition.js +235 -0
  22. package/dist/internal/utils.d.ts +52 -0
  23. package/dist/internal/utils.js +31 -0
  24. package/dist/machine.d.ts +271 -0
  25. package/dist/machine.js +317 -0
  26. package/{src/persistence/adapter.ts → dist/persistence/adapter.d.ts} +40 -72
  27. package/dist/persistence/adapter.js +27 -0
  28. package/dist/persistence/adapters/in-memory.d.ts +32 -0
  29. package/dist/persistence/adapters/in-memory.js +176 -0
  30. package/dist/persistence/index.d.ts +5 -0
  31. package/dist/persistence/index.js +6 -0
  32. package/dist/persistence/persistent-actor.d.ts +50 -0
  33. package/dist/persistence/persistent-actor.js +348 -0
  34. package/{src/persistence/persistent-machine.ts → dist/persistence/persistent-machine.d.ts} +28 -54
  35. package/dist/persistence/persistent-machine.js +24 -0
  36. package/dist/schema.d.ts +141 -0
  37. package/dist/schema.js +165 -0
  38. package/dist/slot.d.ts +128 -0
  39. package/dist/slot.js +99 -0
  40. package/dist/testing.d.ts +142 -0
  41. package/dist/testing.js +131 -0
  42. package/package.json +18 -7
  43. package/src/actor.ts +0 -1050
  44. package/src/cluster/entity-machine.ts +0 -201
  45. package/src/cluster/index.ts +0 -43
  46. package/src/cluster/to-entity.ts +0 -99
  47. package/src/errors.ts +0 -64
  48. package/src/index.ts +0 -105
  49. package/src/inspection.ts +0 -178
  50. package/src/internal/brands.ts +0 -51
  51. package/src/internal/inspection.ts +0 -18
  52. package/src/internal/transition.ts +0 -489
  53. package/src/internal/utils.ts +0 -80
  54. package/src/machine.ts +0 -836
  55. package/src/persistence/adapters/in-memory.ts +0 -294
  56. package/src/persistence/index.ts +0 -24
  57. package/src/persistence/persistent-actor.ts +0 -791
  58. package/src/schema.ts +0 -362
  59. package/src/slot.ts +0 -281
  60. package/src/testing.ts +0 -284
  61. package/tsconfig.json +0 -65
package/dist/schema.js ADDED
@@ -0,0 +1,165 @@
1
+ import { InvalidSchemaError, MissingMatchHandlerError } from "./errors.js";
2
+ import { Schema } from "effect";
3
+
4
+ //#region src/schema.ts
5
+ /**
6
+ * Schema-first State/Event definitions for effect-machine.
7
+ *
8
+ * MachineSchema provides a single source of truth that combines:
9
+ * - Schema for validation/serialization
10
+ * - Variant constructors (like Data.taggedEnum)
11
+ * - $is and $match helpers for pattern matching
12
+ * - Brand integration for compile-time safety
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { State, Event, Machine } from "effect-machine"
17
+ *
18
+ * // Define schema-first state
19
+ * const OrderState = State({
20
+ * Pending: { orderId: Schema.String },
21
+ * Shipped: { trackingId: Schema.String },
22
+ * })
23
+ *
24
+ * // Infer type from schema
25
+ * type OrderState = typeof OrderState.Type
26
+ *
27
+ * // Use constructors
28
+ * const pending = OrderState.Pending({ orderId: "123" })
29
+ *
30
+ * // Pattern match
31
+ * OrderState.$match(state, {
32
+ * Pending: (s) => `Order ${s.orderId} pending`,
33
+ * Shipped: (s) => `Shipped: ${s.trackingId}`,
34
+ * })
35
+ *
36
+ * // Use as Schema for persistence/cluster
37
+ * machine.pipe(Machine.persist({ stateSchema: OrderState, ... }))
38
+ * ```
39
+ *
40
+ * @module
41
+ */
42
+ /**
43
+ * Build a schema-first definition from a record of tag -> fields
44
+ */
45
+ const buildMachineSchema = (definition) => {
46
+ const variants = {};
47
+ const constructors = {};
48
+ for (const tag of Object.keys(definition)) {
49
+ const fields = definition[tag];
50
+ if (fields === void 0) continue;
51
+ variants[tag] = Schema.TaggedStruct(tag, fields);
52
+ const fieldNames = new Set(Object.keys(fields));
53
+ if (fieldNames.size > 0) {
54
+ const constructor = (args) => ({
55
+ ...args,
56
+ _tag: tag
57
+ });
58
+ constructor._tag = tag;
59
+ constructor.derive = (source, partial) => {
60
+ const result = { _tag: tag };
61
+ for (const key of fieldNames) if (key in source) result[key] = source[key];
62
+ if (partial !== void 0) for (const [key, value] of Object.entries(partial)) result[key] = value;
63
+ return result;
64
+ };
65
+ constructors[tag] = constructor;
66
+ } else constructors[tag] = {
67
+ _tag: tag,
68
+ derive: () => ({ _tag: tag })
69
+ };
70
+ }
71
+ const variantArray = Object.values(variants);
72
+ if (variantArray.length === 0) throw new InvalidSchemaError();
73
+ const unionSchema = variantArray.length === 1 ? variantArray[0] : Schema.Union(...variantArray);
74
+ const $is = (tag) => (u) => typeof u === "object" && u !== null && "_tag" in u && u._tag === tag;
75
+ const $match = (valueOrCases, maybeCases) => {
76
+ if (maybeCases !== void 0) {
77
+ const value = valueOrCases;
78
+ const handler = maybeCases[value._tag];
79
+ if (handler === void 0) throw new MissingMatchHandlerError({ tag: value._tag });
80
+ return handler(value);
81
+ }
82
+ const cases = valueOrCases;
83
+ return (value) => {
84
+ const handler = cases[value._tag];
85
+ if (handler === void 0) throw new MissingMatchHandlerError({ tag: value._tag });
86
+ return handler(value);
87
+ };
88
+ };
89
+ return {
90
+ schema: unionSchema,
91
+ variants,
92
+ constructors,
93
+ _definition: definition,
94
+ $is,
95
+ $match
96
+ };
97
+ };
98
+ /**
99
+ * Internal helper to create a machine schema (shared by State and Event).
100
+ * Builds the schema object with variants, constructors, $is, and $match.
101
+ */
102
+ const createMachineSchema = (definition) => {
103
+ const { schema, variants, constructors, _definition, $is, $match } = buildMachineSchema(definition);
104
+ return Object.assign(Object.create(schema), {
105
+ variants,
106
+ _definition,
107
+ $is,
108
+ $match,
109
+ ...constructors
110
+ });
111
+ };
112
+ /**
113
+ * Create a schema-first State definition.
114
+ *
115
+ * The schema's definition type D creates a unique brand, preventing
116
+ * accidental use of constructors from different state schemas
117
+ * (unless they have identical definitions).
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * const OrderState = MachineSchema.State({
122
+ * Pending: { orderId: Schema.String },
123
+ * Shipped: { trackingId: Schema.String },
124
+ * })
125
+ *
126
+ * type OrderState = typeof OrderState.Type
127
+ *
128
+ * // Construct
129
+ * const s = OrderState.Pending({ orderId: "123" })
130
+ *
131
+ * // Pattern match
132
+ * OrderState.$match(s, {
133
+ * Pending: (v) => v.orderId,
134
+ * Shipped: (v) => v.trackingId,
135
+ * })
136
+ *
137
+ * // Validate
138
+ * Schema.decodeUnknownSync(OrderState)(rawJson)
139
+ * ```
140
+ */
141
+ const State = (definition) => createMachineSchema(definition);
142
+ /**
143
+ * Create a schema-first Event definition.
144
+ *
145
+ * The schema's definition type D creates a unique brand, preventing
146
+ * accidental use of constructors from different event schemas
147
+ * (unless they have identical definitions).
148
+ *
149
+ * @example
150
+ * ```ts
151
+ * const OrderEvent = MachineSchema.Event({
152
+ * Ship: { trackingId: Schema.String },
153
+ * Cancel: {},
154
+ * })
155
+ *
156
+ * type OrderEvent = typeof OrderEvent.Type
157
+ *
158
+ * // Construct
159
+ * const e = OrderEvent.Ship({ trackingId: "abc" })
160
+ * ```
161
+ */
162
+ const Event = (definition) => createMachineSchema(definition);
163
+
164
+ //#endregion
165
+ export { Event, State };
package/dist/slot.d.ts ADDED
@@ -0,0 +1,128 @@
1
+ import { Context, Effect, Schema } from "effect";
2
+
3
+ //#region src/slot.d.ts
4
+ /** Schema fields definition (like Schema.Struct.Fields) */
5
+ type Fields = Record<string, Schema.Schema.All>;
6
+ /** Extract the encoded type from schema fields (used for parameters) */
7
+ type FieldsToParams<F extends Fields> = keyof F extends never ? void : Schema.Schema.Type<Schema.Struct<F>>;
8
+ /**
9
+ * A guard slot - callable function that returns Effect<boolean>.
10
+ */
11
+ interface GuardSlot<Name extends string, Params> {
12
+ readonly _tag: "GuardSlot";
13
+ readonly name: Name;
14
+ (params: Params): Effect.Effect<boolean>;
15
+ }
16
+ /**
17
+ * An effect slot - callable function that returns Effect<void>.
18
+ */
19
+ interface EffectSlot<Name extends string, Params> {
20
+ readonly _tag: "EffectSlot";
21
+ readonly name: Name;
22
+ (params: Params): Effect.Effect<void>;
23
+ }
24
+ /**
25
+ * Guard definition - name to schema fields mapping
26
+ */
27
+ type GuardsDef = Record<string, Fields>;
28
+ /**
29
+ * Effect definition - name to schema fields mapping
30
+ */
31
+ type EffectsDef = Record<string, Fields>;
32
+ /**
33
+ * Convert guard definitions to callable guard slots
34
+ */
35
+ type GuardSlots<D extends GuardsDef> = { readonly [K in keyof D & string]: GuardSlot<K, FieldsToParams<D[K]>> };
36
+ /**
37
+ * Convert effect definitions to callable effect slots
38
+ */
39
+ type EffectSlots<D extends EffectsDef> = { readonly [K in keyof D & string]: EffectSlot<K, FieldsToParams<D[K]>> };
40
+ /**
41
+ * Type for machine context - state, event, and self reference.
42
+ * Shared across all machines via MachineContextTag.
43
+ */
44
+ interface MachineContext<State, Event, Self> {
45
+ readonly state: State;
46
+ readonly event: Event;
47
+ readonly self: Self;
48
+ }
49
+ /**
50
+ * Shared Context tag for all machines.
51
+ * Single module-level tag instead of per-machine allocation.
52
+ * @internal
53
+ */
54
+ declare const MachineContextTag: Context.Tag<MachineContext<any, any, any>, MachineContext<any, any, any>>;
55
+ /**
56
+ * Guard handler implementation.
57
+ * Receives params and context, returns Effect<boolean>.
58
+ */
59
+ type GuardHandler<Params, Ctx, R = never> = (params: Params, ctx: Ctx) => boolean | Effect.Effect<boolean, never, R>;
60
+ /**
61
+ * Effect handler implementation.
62
+ * Receives params and context, returns Effect<void>.
63
+ */
64
+ type EffectHandler<Params, Ctx, R = never> = (params: Params, ctx: Ctx) => Effect.Effect<void, never, R>;
65
+ /**
66
+ * Handler types for all guards in a definition
67
+ */
68
+ type GuardHandlers<D extends GuardsDef, MachineCtx, R = never> = { readonly [K in keyof D & string]: GuardHandler<FieldsToParams<D[K]>, MachineCtx, R> };
69
+ /**
70
+ * Handler types for all effects in a definition
71
+ */
72
+ type EffectHandlers<D extends EffectsDef, MachineCtx, R = never> = { readonly [K in keyof D & string]: EffectHandler<FieldsToParams<D[K]>, MachineCtx, R> };
73
+ /**
74
+ * Guards schema - returned by Slot.Guards()
75
+ */
76
+ interface GuardsSchema<D extends GuardsDef> {
77
+ readonly _tag: "GuardsSchema";
78
+ readonly definitions: D;
79
+ /** Create callable guard slots (used by Machine internally) */
80
+ readonly _createSlots: (resolve: <N extends keyof D & string>(name: N, params: FieldsToParams<D[N]>) => Effect.Effect<boolean>) => GuardSlots<D>;
81
+ }
82
+ /**
83
+ * Effects schema - returned by Slot.Effects()
84
+ */
85
+ interface EffectsSchema<D extends EffectsDef> {
86
+ readonly _tag: "EffectsSchema";
87
+ readonly definitions: D;
88
+ /** Create callable effect slots (used by Machine internally) */
89
+ readonly _createSlots: (resolve: <N extends keyof D & string>(name: N, params: FieldsToParams<D[N]>) => Effect.Effect<void>) => EffectSlots<D>;
90
+ }
91
+ /**
92
+ * Create a guards schema with parameterized guard definitions.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * const MyGuards = Slot.Guards({
97
+ * canRetry: { max: Schema.Number },
98
+ * isValid: {},
99
+ * })
100
+ * ```
101
+ */
102
+ declare const Guards: <D extends GuardsDef>(definitions: D) => GuardsSchema<D>;
103
+ /**
104
+ * Create an effects schema with parameterized effect definitions.
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * const MyEffects = Slot.Effects({
109
+ * fetchData: { url: Schema.String },
110
+ * notify: { message: Schema.String },
111
+ * })
112
+ * ```
113
+ */
114
+ declare const Effects: <D extends EffectsDef>(definitions: D) => EffectsSchema<D>;
115
+ /** Extract guard definition type from GuardsSchema */
116
+ type GuardsDefOf<G> = G extends GuardsSchema<infer D> ? D : never;
117
+ /** Extract effect definition type from EffectsSchema */
118
+ type EffectsDefOf<E> = E extends EffectsSchema<infer D> ? D : never;
119
+ /** Extract guard slots type from GuardsSchema */
120
+ type GuardSlotsOf<G> = G extends GuardsSchema<infer D> ? GuardSlots<D> : never;
121
+ /** Extract effect slots type from EffectsSchema */
122
+ type EffectSlotsOf<E> = E extends EffectsSchema<infer D> ? EffectSlots<D> : never;
123
+ declare const Slot: {
124
+ readonly Guards: <D extends GuardsDef>(definitions: D) => GuardsSchema<D>;
125
+ readonly Effects: <D extends EffectsDef>(definitions: D) => EffectsSchema<D>;
126
+ };
127
+ //#endregion
128
+ export { EffectHandler, EffectHandlers, EffectSlot, EffectSlots, EffectSlotsOf, Effects, EffectsDef, EffectsDefOf, EffectsSchema, GuardHandler, GuardHandlers, GuardSlot, GuardSlots, GuardSlotsOf, Guards, GuardsDef, GuardsDefOf, GuardsSchema, MachineContext, MachineContextTag, Slot };
package/dist/slot.js ADDED
@@ -0,0 +1,99 @@
1
+ import { Context } from "effect";
2
+
3
+ //#region src/slot.ts
4
+ /**
5
+ * Slot module - schema-based, parameterized guards and effects.
6
+ *
7
+ * Guards and Effects are defined with schemas for their parameters,
8
+ * and provided implementations receive typed parameters plus machine context.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { Slot } from "effect-machine"
13
+ * import { Schema } from "effect"
14
+ *
15
+ * const MyGuards = Slot.Guards({
16
+ * canRetry: { max: Schema.Number },
17
+ * isValid: {}, // no params
18
+ * })
19
+ *
20
+ * const MyEffects = Slot.Effects({
21
+ * fetchData: { url: Schema.String },
22
+ * notify: { message: Schema.String },
23
+ * })
24
+ *
25
+ * // Used in handlers:
26
+ * .on(State.X, Event.Y, ({ guards, effects }) =>
27
+ * Effect.gen(function* () {
28
+ * if (yield* guards.canRetry({ max: 3 })) {
29
+ * yield* effects.fetchData({ url: "/api" })
30
+ * return State.Next
31
+ * }
32
+ * return state
33
+ * })
34
+ * )
35
+ * ```
36
+ *
37
+ * @module
38
+ */
39
+ /**
40
+ * Shared Context tag for all machines.
41
+ * Single module-level tag instead of per-machine allocation.
42
+ * @internal
43
+ */
44
+ const MachineContextTag = Context.GenericTag("@effect-machine/Context");
45
+ /**
46
+ * Generic slot schema factory. Used internally by Guards() and Effects().
47
+ * @internal
48
+ */
49
+ const createSlotSchema = (tag, slotTag, definitions) => ({
50
+ _tag: tag,
51
+ definitions,
52
+ _createSlots: (resolve) => {
53
+ const slots = {};
54
+ for (const name of Object.keys(definitions)) {
55
+ const slot = (params) => resolve(name, params);
56
+ Object.defineProperty(slot, "_tag", {
57
+ value: slotTag,
58
+ enumerable: true
59
+ });
60
+ Object.defineProperty(slot, "name", {
61
+ value: name,
62
+ enumerable: true
63
+ });
64
+ slots[name] = slot;
65
+ }
66
+ return slots;
67
+ }
68
+ });
69
+ /**
70
+ * Create a guards schema with parameterized guard definitions.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const MyGuards = Slot.Guards({
75
+ * canRetry: { max: Schema.Number },
76
+ * isValid: {},
77
+ * })
78
+ * ```
79
+ */
80
+ const Guards = (definitions) => createSlotSchema("GuardsSchema", "GuardSlot", definitions);
81
+ /**
82
+ * Create an effects schema with parameterized effect definitions.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * const MyEffects = Slot.Effects({
87
+ * fetchData: { url: Schema.String },
88
+ * notify: { message: Schema.String },
89
+ * })
90
+ * ```
91
+ */
92
+ const Effects = (definitions) => createSlotSchema("EffectsSchema", "EffectSlot", definitions);
93
+ const Slot = {
94
+ Guards,
95
+ Effects
96
+ };
97
+
98
+ //#endregion
99
+ export { Effects, Guards, MachineContextTag, Slot };
@@ -0,0 +1,142 @@
1
+ import { EffectsDef, GuardsDef, MachineContext } from "./slot.js";
2
+ import { AssertionError } from "./errors.js";
3
+ import { BuiltMachine, Machine, MachineRef } from "./machine.js";
4
+ import { Effect, SubscriptionRef } from "effect";
5
+
6
+ //#region src/testing.d.ts
7
+ /** Accept either Machine or BuiltMachine for testing utilities. */
8
+ type MachineInput<S, E, R, GD extends GuardsDef, EFD extends EffectsDef> = Machine<S, E, R, any, any, GD, EFD> | BuiltMachine<S, E, R>;
9
+ /**
10
+ * Result of simulating events through a machine
11
+ */
12
+ interface SimulationResult<S> {
13
+ readonly states: ReadonlyArray<S>;
14
+ readonly finalState: S;
15
+ }
16
+ /**
17
+ * Simulate a sequence of events through a machine without running an actor.
18
+ * Useful for testing state transitions in isolation.
19
+ * Does not run onEnter/spawn/background effects, but does run guard/effect slots
20
+ * within transition handlers.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const result = yield* simulate(
25
+ * fetcherMachine,
26
+ * [
27
+ * Event.Fetch({ url: "https://example.com" }),
28
+ * Event._Done({ data: { foo: "bar" } })
29
+ * ]
30
+ * )
31
+ *
32
+ * expect(result.finalState._tag).toBe("Success")
33
+ * expect(result.states).toHaveLength(3) // Idle -> Loading -> Success
34
+ * ```
35
+ */
36
+ declare const simulate: <S extends {
37
+ readonly _tag: string;
38
+ }, E extends {
39
+ readonly _tag: string;
40
+ }, R, GD extends GuardsDef = Record<string, never>, EFD extends EffectsDef = Record<string, never>>(input: MachineInput<S, E, R, GD, EFD>, events: readonly E[]) => Effect.Effect<{
41
+ states: S[];
42
+ finalState: S;
43
+ }, never, Exclude<R, MachineContext<S, E, MachineRef<E>>>>;
44
+ /**
45
+ * Assert that a machine can reach a specific state given a sequence of events
46
+ */
47
+ declare const assertReaches: <S extends {
48
+ readonly _tag: string;
49
+ }, E extends {
50
+ readonly _tag: string;
51
+ }, R, GD extends GuardsDef = Record<string, never>, EFD extends EffectsDef = Record<string, never>>(input: MachineInput<S, E, R, GD, EFD>, events: readonly E[], expectedTag: string) => Effect.Effect<S, AssertionError, Exclude<R, MachineContext<S, E, MachineRef<E>>>>;
52
+ /**
53
+ * Assert that a machine follows a specific path of state tags
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * yield* assertPath(
58
+ * machine,
59
+ * [Event.Start(), Event.Increment(), Event.Stop()],
60
+ * ["Idle", "Counting", "Counting", "Done"]
61
+ * )
62
+ * ```
63
+ */
64
+ declare const assertPath: <S extends {
65
+ readonly _tag: string;
66
+ }, E extends {
67
+ readonly _tag: string;
68
+ }, R, GD extends GuardsDef = Record<string, never>, EFD extends EffectsDef = Record<string, never>>(input: MachineInput<S, E, R, GD, EFD>, events: readonly E[], expectedPath: readonly string[]) => Effect.Effect<{
69
+ states: S[];
70
+ finalState: S;
71
+ }, AssertionError, Exclude<R, MachineContext<S, E, MachineRef<E>>>>;
72
+ /**
73
+ * Assert that a machine never reaches a specific state given a sequence of events
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * // Verify error handling doesn't reach crash state
78
+ * yield* assertNeverReaches(
79
+ * machine,
80
+ * [Event.Error(), Event.Retry(), Event.Success()],
81
+ * "Crashed"
82
+ * )
83
+ * ```
84
+ */
85
+ declare const assertNeverReaches: <S extends {
86
+ readonly _tag: string;
87
+ }, E extends {
88
+ readonly _tag: string;
89
+ }, R, GD extends GuardsDef = Record<string, never>, EFD extends EffectsDef = Record<string, never>>(input: MachineInput<S, E, R, GD, EFD>, events: readonly E[], forbiddenTag: string) => Effect.Effect<{
90
+ states: S[];
91
+ finalState: S;
92
+ }, AssertionError, Exclude<R, MachineContext<S, E, MachineRef<E>>>>;
93
+ /**
94
+ * Create a controllable test harness for a machine
95
+ */
96
+ interface TestHarness<S, E, R> {
97
+ readonly state: SubscriptionRef.SubscriptionRef<S>;
98
+ readonly send: (event: E) => Effect.Effect<S, never, R>;
99
+ readonly getState: Effect.Effect<S>;
100
+ }
101
+ /**
102
+ * Options for creating a test harness
103
+ */
104
+ interface TestHarnessOptions<S, E> {
105
+ /**
106
+ * Called after each transition with the previous state, event, and new state.
107
+ * Useful for logging or spying on transitions.
108
+ */
109
+ readonly onTransition?: (from: S, event: E, to: S) => void;
110
+ }
111
+ /**
112
+ * Create a test harness for step-by-step testing.
113
+ * Does not run onEnter/spawn/background effects, but does run guard/effect slots
114
+ * within transition handlers.
115
+ *
116
+ * @example Basic usage
117
+ * ```ts
118
+ * const harness = yield* createTestHarness(machine)
119
+ * yield* harness.send(Event.Start())
120
+ * const state = yield* harness.getState
121
+ * ```
122
+ *
123
+ * @example With transition observer
124
+ * ```ts
125
+ * const transitions: Array<{ from: string; event: string; to: string }> = []
126
+ * const harness = yield* createTestHarness(machine, {
127
+ * onTransition: (from, event, to) =>
128
+ * transitions.push({ from: from._tag, event: event._tag, to: to._tag })
129
+ * })
130
+ * ```
131
+ */
132
+ declare const createTestHarness: <S extends {
133
+ readonly _tag: string;
134
+ }, E extends {
135
+ readonly _tag: string;
136
+ }, R, GD extends GuardsDef = Record<string, never>, EFD extends EffectsDef = Record<string, never>>(input: MachineInput<S, E, R, GD, EFD>, options?: TestHarnessOptions<S, E> | undefined) => Effect.Effect<{
137
+ state: SubscriptionRef.SubscriptionRef<S>;
138
+ send: (event: E) => Effect.Effect<S, never, Exclude<R, MachineContext<S, E, MachineRef<E>>>>;
139
+ getState: Effect.Effect<S, never, never>;
140
+ }, never, never>;
141
+ //#endregion
142
+ export { AssertionError, SimulationResult, TestHarness, TestHarnessOptions, assertNeverReaches, assertPath, assertReaches, createTestHarness, simulate };
@@ -0,0 +1,131 @@
1
+ import { AssertionError } from "./errors.js";
2
+ import { BuiltMachine } from "./machine.js";
3
+ import { executeTransition } from "./internal/transition.js";
4
+ import { Effect, SubscriptionRef } from "effect";
5
+
6
+ //#region src/testing.ts
7
+ /**
8
+ * Simulate a sequence of events through a machine without running an actor.
9
+ * Useful for testing state transitions in isolation.
10
+ * Does not run onEnter/spawn/background effects, but does run guard/effect slots
11
+ * within transition handlers.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const result = yield* simulate(
16
+ * fetcherMachine,
17
+ * [
18
+ * Event.Fetch({ url: "https://example.com" }),
19
+ * Event._Done({ data: { foo: "bar" } })
20
+ * ]
21
+ * )
22
+ *
23
+ * expect(result.finalState._tag).toBe("Success")
24
+ * expect(result.states).toHaveLength(3) // Idle -> Loading -> Success
25
+ * ```
26
+ */
27
+ const simulate = Effect.fn("effect-machine.simulate")(function* (input, events) {
28
+ const machine = input instanceof BuiltMachine ? input._inner : input;
29
+ const dummySelf = { send: Effect.fn("effect-machine.testing.simulate.send")((_event) => Effect.void) };
30
+ let currentState = machine.initial;
31
+ const states = [currentState];
32
+ for (const event of events) {
33
+ const result = yield* executeTransition(machine, currentState, event, dummySelf);
34
+ if (!result.transitioned) continue;
35
+ currentState = result.newState;
36
+ states.push(currentState);
37
+ if (machine.finalStates.has(currentState._tag)) break;
38
+ }
39
+ return {
40
+ states,
41
+ finalState: currentState
42
+ };
43
+ });
44
+ /**
45
+ * Assert that a machine can reach a specific state given a sequence of events
46
+ */
47
+ const assertReaches = Effect.fn("effect-machine.assertReaches")(function* (input, events, expectedTag) {
48
+ const result = yield* simulate(input, events);
49
+ if (result.finalState._tag !== expectedTag) return yield* new AssertionError({ message: `Expected final state "${expectedTag}" but got "${result.finalState._tag}". States visited: ${result.states.map((s) => s._tag).join(" -> ")}` });
50
+ return result.finalState;
51
+ });
52
+ /**
53
+ * Assert that a machine follows a specific path of state tags
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * yield* assertPath(
58
+ * machine,
59
+ * [Event.Start(), Event.Increment(), Event.Stop()],
60
+ * ["Idle", "Counting", "Counting", "Done"]
61
+ * )
62
+ * ```
63
+ */
64
+ const assertPath = Effect.fn("effect-machine.assertPath")(function* (input, events, expectedPath) {
65
+ const result = yield* simulate(input, events);
66
+ const actualPath = result.states.map((s) => s._tag);
67
+ if (actualPath.length !== expectedPath.length) return yield* new AssertionError({ message: `Path length mismatch. Expected ${expectedPath.length} states but got ${actualPath.length}.\nExpected: ${expectedPath.join(" -> ")}\nActual: ${actualPath.join(" -> ")}` });
68
+ for (let i = 0; i < expectedPath.length; i++) if (actualPath[i] !== expectedPath[i]) return yield* new AssertionError({ message: `Path mismatch at position ${i}. Expected "${expectedPath[i]}" but got "${actualPath[i]}".\nExpected: ${expectedPath.join(" -> ")}\nActual: ${actualPath.join(" -> ")}` });
69
+ return result;
70
+ });
71
+ /**
72
+ * Assert that a machine never reaches a specific state given a sequence of events
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * // Verify error handling doesn't reach crash state
77
+ * yield* assertNeverReaches(
78
+ * machine,
79
+ * [Event.Error(), Event.Retry(), Event.Success()],
80
+ * "Crashed"
81
+ * )
82
+ * ```
83
+ */
84
+ const assertNeverReaches = Effect.fn("effect-machine.assertNeverReaches")(function* (input, events, forbiddenTag) {
85
+ const result = yield* simulate(input, events);
86
+ const visitedIndex = result.states.findIndex((s) => s._tag === forbiddenTag);
87
+ if (visitedIndex !== -1) return yield* new AssertionError({ message: `Machine reached forbidden state "${forbiddenTag}" at position ${visitedIndex}.\nStates visited: ${result.states.map((s) => s._tag).join(" -> ")}` });
88
+ return result;
89
+ });
90
+ /**
91
+ * Create a test harness for step-by-step testing.
92
+ * Does not run onEnter/spawn/background effects, but does run guard/effect slots
93
+ * within transition handlers.
94
+ *
95
+ * @example Basic usage
96
+ * ```ts
97
+ * const harness = yield* createTestHarness(machine)
98
+ * yield* harness.send(Event.Start())
99
+ * const state = yield* harness.getState
100
+ * ```
101
+ *
102
+ * @example With transition observer
103
+ * ```ts
104
+ * const transitions: Array<{ from: string; event: string; to: string }> = []
105
+ * const harness = yield* createTestHarness(machine, {
106
+ * onTransition: (from, event, to) =>
107
+ * transitions.push({ from: from._tag, event: event._tag, to: to._tag })
108
+ * })
109
+ * ```
110
+ */
111
+ const createTestHarness = Effect.fn("effect-machine.createTestHarness")(function* (input, options) {
112
+ const machine = input instanceof BuiltMachine ? input._inner : input;
113
+ const dummySelf = { send: Effect.fn("effect-machine.testing.harness.send")((_event) => Effect.void) };
114
+ const stateRef = yield* SubscriptionRef.make(machine.initial);
115
+ return {
116
+ state: stateRef,
117
+ send: Effect.fn("effect-machine.testHarness.send")(function* (event) {
118
+ const currentState = yield* SubscriptionRef.get(stateRef);
119
+ const result = yield* executeTransition(machine, currentState, event, dummySelf);
120
+ if (!result.transitioned) return currentState;
121
+ const newState = result.newState;
122
+ yield* SubscriptionRef.set(stateRef, newState);
123
+ if (options?.onTransition !== void 0) options.onTransition(currentState, event, newState);
124
+ return newState;
125
+ }),
126
+ getState: SubscriptionRef.get(stateRef)
127
+ };
128
+ });
129
+
130
+ //#endregion
131
+ export { AssertionError, assertNeverReaches, assertPath, assertReaches, createTestHarness, simulate };