|
| 1 | +import { assert, describe, it } from "@effect/vitest" |
| 2 | +import { Effect, Schema } from "effect" |
| 3 | +import { Machine } from "../../src/index.js" |
| 4 | + |
| 5 | +describe("local compound target selection", () => { |
| 6 | + it.effect("captures and plans local.with from the compound scope", () => |
| 7 | + Effect.gen(function*() { |
| 8 | + const State = Schema.TaggedUnion({ |
| 9 | + Search: { query: Schema.String }, |
| 10 | + Idle: {}, |
| 11 | + Updated: {} |
| 12 | + }) |
| 13 | + const Events = Schema.TaggedUnion({ |
| 14 | + UpdateQuery: { query: Schema.String }, |
| 15 | + Reset: {} |
| 16 | + }) |
| 17 | + const states = Machine.defineStates({ |
| 18 | + search: { |
| 19 | + schema: State.cases.Search, |
| 20 | + initial: "Idle", |
| 21 | + states: { |
| 22 | + Idle: {}, |
| 23 | + Updated: {} |
| 24 | + } |
| 25 | + } |
| 26 | + }) |
| 27 | + const machine = Machine.make({ |
| 28 | + states: states.states, |
| 29 | + events: Machine.events(Events), |
| 30 | + initial: { |
| 31 | + target: (to) => to.search.initial(), |
| 32 | + resolve: ({ target }) => target.from({ query: "" }, (search) => search.Idle.from()) |
| 33 | + } |
| 34 | + }).handle({ |
| 35 | + search: { |
| 36 | + on: { |
| 37 | + UpdateQuery: Machine.transition({ |
| 38 | + target: (to) => to.local.with(), |
| 39 | + resolve: ({ event, target }) => target.from({ query: event.query }, (search) => search.Updated.from()), |
| 40 | + reenter: true |
| 41 | + }) |
| 42 | + }, |
| 43 | + states: { |
| 44 | + Idle: {}, |
| 45 | + Updated: { |
| 46 | + on: { |
| 47 | + Reset: Machine.transition({ |
| 48 | + target: (to) => to.local.Idle(), |
| 49 | + resolve: ({ target }) => target.from() |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + assert.deepStrictEqual(Machine.transitionDefinitions(machine), [{ |
| 58 | + source: "search", |
| 59 | + trigger: { type: "event", event: "UpdateQuery" }, |
| 60 | + reenter: true, |
| 61 | + branches: [{ |
| 62 | + type: "direct", |
| 63 | + target: "search", |
| 64 | + selection: { path: "search", kind: "state", scope: "local" } |
| 65 | + }] |
| 66 | + }, { |
| 67 | + source: "search.Updated", |
| 68 | + trigger: { type: "event", event: "Reset" }, |
| 69 | + reenter: false, |
| 70 | + branches: [{ |
| 71 | + type: "direct", |
| 72 | + target: "search.Idle", |
| 73 | + selection: { path: "search.Idle", kind: "state", scope: "local" } |
| 74 | + }] |
| 75 | + }]) |
| 76 | + |
| 77 | + const initial = yield* Machine.planInitial(machine) |
| 78 | + const updated = yield* Machine.plan(machine, initial.state, Events.cases.UpdateQuery.make({ query: "next" })) |
| 79 | + |
| 80 | + assert.deepStrictEqual(updated.next, { |
| 81 | + path: "search", |
| 82 | + value: State.cases.Search.make({ query: "next" }), |
| 83 | + state: { |
| 84 | + path: "search.Updated", |
| 85 | + value: undefined |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + const reset = yield* Machine.plan(machine, updated.next, Events.cases.Reset.make({})) |
| 90 | + assert.deepStrictEqual(reset.next, { |
| 91 | + path: "search", |
| 92 | + value: State.cases.Search.make({ query: "next" }), |
| 93 | + state: { |
| 94 | + path: "search.Idle", |
| 95 | + value: undefined |
| 96 | + } |
| 97 | + }) |
| 98 | + })) |
| 99 | + |
| 100 | + it.effect("resolves local.with from a descendant invoke source", () => |
| 101 | + Effect.gen(function*() { |
| 102 | + const State = Schema.TaggedUnion({ |
| 103 | + Search: { query: Schema.String }, |
| 104 | + Searching: {}, |
| 105 | + Updated: {} |
| 106 | + }) |
| 107 | + const states = Machine.defineStates({ |
| 108 | + search: { |
| 109 | + schema: State.cases.Search, |
| 110 | + initial: "Searching", |
| 111 | + states: { |
| 112 | + Searching: {}, |
| 113 | + Updated: {} |
| 114 | + } |
| 115 | + } |
| 116 | + }) |
| 117 | + const machine = Machine.make({ |
| 118 | + states: states.states, |
| 119 | + events: Machine.events(), |
| 120 | + initial: { |
| 121 | + target: (to) => to.search.initial(), |
| 122 | + resolve: ({ target }) => target.from({ query: "pending" }, (search) => search.Searching.from()) |
| 123 | + } |
| 124 | + }).handle({ |
| 125 | + search: { |
| 126 | + states: { |
| 127 | + Searching: { |
| 128 | + invoke: Machine.invoke({ |
| 129 | + id: "search", |
| 130 | + effect: () => Effect.succeed("resolved"), |
| 131 | + onDone: Machine.transition({ |
| 132 | + target: (to) => to.local.with(), |
| 133 | + resolve: ({ output, target }) => target.from({ query: output }, (search) => search.Updated.from()) |
| 134 | + }) |
| 135 | + }) |
| 136 | + }, |
| 137 | + Updated: {} |
| 138 | + } |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + assert.deepStrictEqual(Machine.transitionDefinitions(machine), [{ |
| 143 | + source: "search.Searching", |
| 144 | + trigger: { type: "invoke", id: "search", outcome: "done" }, |
| 145 | + reenter: false, |
| 146 | + branches: [{ |
| 147 | + type: "direct", |
| 148 | + target: "search", |
| 149 | + selection: { path: "search", kind: "state", scope: "local" } |
| 150 | + }] |
| 151 | + }]) |
| 152 | + |
| 153 | + const ref = yield* Machine.start(machine) |
| 154 | + for (let index = 0; index < 5; index += 1) yield* Effect.yieldNow |
| 155 | + |
| 156 | + assert.deepStrictEqual(yield* ref.state, { |
| 157 | + path: "search", |
| 158 | + value: State.cases.Search.make({ query: "resolved" }), |
| 159 | + state: { |
| 160 | + path: "search.Updated", |
| 161 | + value: undefined |
| 162 | + } |
| 163 | + }) |
| 164 | + })) |
| 165 | + |
| 166 | + it("does not install local.with for a schema-less compound scope", () => { |
| 167 | + const Event = Schema.TaggedUnion({ Advance: {} }) |
| 168 | + const states = Machine.defineStates({ |
| 169 | + flow: { |
| 170 | + initial: "Idle", |
| 171 | + states: { |
| 172 | + Idle: {}, |
| 173 | + Updated: {} |
| 174 | + } |
| 175 | + } |
| 176 | + }) |
| 177 | + |
| 178 | + Machine.make({ |
| 179 | + states: states.states, |
| 180 | + events: Machine.events(Event), |
| 181 | + initial: { |
| 182 | + target: (to) => to.flow.initial(), |
| 183 | + resolve: ({ target }) => target.from((flow) => flow.Idle.from()) |
| 184 | + } |
| 185 | + }).handle({ |
| 186 | + flow: { |
| 187 | + states: { |
| 188 | + Idle: { |
| 189 | + on: { |
| 190 | + Advance: Machine.transition({ |
| 191 | + target: (to) => { |
| 192 | + assert.notProperty(to.local, "with") |
| 193 | + return to.local.Updated() |
| 194 | + }, |
| 195 | + resolve: ({ target }) => target.from() |
| 196 | + }) |
| 197 | + } |
| 198 | + }, |
| 199 | + Updated: {} |
| 200 | + } |
| 201 | + } |
| 202 | + }) |
| 203 | + }) |
| 204 | +}) |
0 commit comments