@highalch/story 0.0.1 → 0.0.3
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.
- package/README.md +82 -13
- package/dist/{steps-DZBFsrQr.mjs → atoms-H-2NrC_q.mjs} +19 -1
- package/dist/index.d.mts +10 -2
- package/dist/index.mjs +3 -3
- package/dist/{run-DMO8Dcmq.mjs → run-DNZqa3xX.mjs} +1 -1
- package/dist/screen.d.mts +9 -4
- package/dist/screen.mjs +22 -7
- package/dist/xstate.d.mts +23 -7
- package/dist/xstate.mjs +107 -24
- package/package.json +12 -7
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ mounted React component without a browser.
|
|
|
12
12
|
| `@highalch/story/screen` | Screen steps: a mounted component's frames vs reviewed baselines; needs takumi/happy-dom |
|
|
13
13
|
|
|
14
14
|
Peer dependencies: `@highalch/db` and its peers; optional `xstate` and `@xstate/effect` (for
|
|
15
|
-
`/xstate`), `react-dom`, `happy-dom
|
|
15
|
+
`/xstate`), `react-dom`, `happy-dom`, `takumi-js` 2.14.0 and `@effect/atom-react` (for `/screen`).
|
|
16
16
|
|
|
17
17
|
A story tells a frontend's behaviour like Foldkit's: given a state, when something happens, then
|
|
18
18
|
the next state and what was asked for. There is **one runner** and one vocabulary: data steps (a
|
|
@@ -104,18 +104,19 @@ machine introduced last), as data steps take `{ as?: scope }`:
|
|
|
104
104
|
| ----------------------------------------------------------- | -------------------------------------------------------------- | --------------------------------------------------------------------------- |
|
|
105
105
|
| `given(machine, snapshot \| (read) => snapshot, { name? })` | enters from `{ state?, context?, input? }`; works hostless | — |
|
|
106
106
|
| `start(machine, { input?, name? })` | — | enters as an actor (`createEffectActor`) on the step's client; needs a host |
|
|
107
|
+
| `mounted(atoms, { name? })` | — | enters as the actor a mounted component reads through these atoms |
|
|
107
108
|
| `when(event \| (read) => event, { machine? })` | XState's pure `transition`; nothing runs | sent to the actor; the commands it starts wait for a settling step |
|
|
108
109
|
| `then({ state, context? }, { machine? })` | `snapshot.matches(state)`, context compared partially | the same, on the actor's snapshot |
|
|
109
110
|
| `asked(command, input?, { machine? })` | the last transition's spawn actions of `invoke(command)` logic | the commands the story's `Runner` ran for it since its last event |
|
|
110
111
|
| `askedNothing({ machine? })` | the last transition spawned none | it ran none since its last event |
|
|
111
|
-
| `done(command, output?, { machine? })` | feeds `xstate.done.actor
|
|
112
|
-
| `failed(command, error, { machine? })` | feeds `xstate.error.actor
|
|
112
|
+
| `done(command, output?, { machine? })` | feeds `xstate.done.actor` for the command it asked for last | an error: its commands settle by themselves (`Story.settled`) |
|
|
113
|
+
| `failed(command, error, { machine? })` | feeds `xstate.error.actor` with the typed error | an error: assert the real rejection with `Story.rejected` |
|
|
113
114
|
|
|
114
115
|
`(read) => …` computes a snapshot or an event from what the story's sources show when the step runs
|
|
115
116
|
(`read(source, { as? })` returns its rows, opening it first), so a stepped machine can be asked
|
|
116
117
|
what it _would_ do with the rows a collection shows now. It needs a host.
|
|
117
118
|
|
|
118
|
-
## Pure or
|
|
119
|
+
## Pure, running or mounted: one explicit choice
|
|
119
120
|
|
|
120
121
|
A machine enters a story once, and how it enters decides what every later step means for it;
|
|
121
122
|
nothing switches modes silently.
|
|
@@ -131,6 +132,13 @@ nothing switches modes silently.
|
|
|
131
132
|
together: what is shown while a command is in flight, how it reacts to typed rejections, that
|
|
132
133
|
rows roll back. Needs a host.
|
|
133
134
|
|
|
135
|
+
- **`Machine.mounted` — running, as a component reads it.** The actor the story's atom registry
|
|
136
|
+
holds for the atoms `createActorAtoms` (from `@xstate/effect/atom`) made, which is the one a
|
|
137
|
+
component under `Screen.render` reads through the same atoms. From then on it is a running
|
|
138
|
+
machine: `when` sends to it, `then` and `asked` read it, and its commands run on the story's
|
|
139
|
+
client, recorded. The story keeps the actor alive until it ends, so the step may come before or
|
|
140
|
+
after the render. Needs a host. See [Machines in a screen](#machines-in-a-screen).
|
|
141
|
+
|
|
134
142
|
```ts
|
|
135
143
|
story(
|
|
136
144
|
Story.given(n1, new NoteCreated({ note: ada })),
|
|
@@ -172,7 +180,7 @@ const editor = setupEffect({ actors: { rename: invoke(notes.rename) }, schemas }
|
|
|
172
180
|
input: ({ context }) => ({ NoteId: context.NoteId, title: context.draft }),
|
|
173
181
|
onDone: () => ({ target: "Saved" }),
|
|
174
182
|
onError: ({ event }) => ({
|
|
175
|
-
target: isTagged(event.error, "NoteMissing") ? "Stale" : "Failed",
|
|
183
|
+
target: isTagged(rejection(event.error), "NoteMissing") ? "Stale" : "Failed",
|
|
176
184
|
}),
|
|
177
185
|
},
|
|
178
186
|
},
|
|
@@ -180,8 +188,10 @@ const editor = setupEffect({ actors: { rename: invoke(notes.rename) }, schemas }
|
|
|
180
188
|
});
|
|
181
189
|
```
|
|
182
190
|
|
|
183
|
-
It is `
|
|
184
|
-
|
|
191
|
+
It is `createEffectLogic` over `command.effect(input)`: the invoked actor's output is the
|
|
192
|
+
`Settled` value. Its error reaches `onError` as `@xstate/effect`'s `EffectFailure` around the
|
|
193
|
+
Effect's cause; `rejection(event.error)` (from `@highalch/db/xstate`) answers the typed rejection
|
|
194
|
+
inside, the original instance. The logic is marked with its command,
|
|
185
195
|
which is what makes "the command it asked for" observable: XState 6's pure `transition` returns,
|
|
186
196
|
for each invoked actor, an `@xstate.spawn` executable action holding the actor `logic`, its `id`
|
|
187
197
|
and resolved `input`; the story reads the mark from `logic`, and names the command by the key it
|
|
@@ -240,9 +250,9 @@ a browser and compared with reviewed baselines. A story then yields a filmstrip:
|
|
|
240
250
|
pending, settled or rolled back, retrying.
|
|
241
251
|
|
|
242
252
|
```tsx
|
|
243
|
-
import * as
|
|
253
|
+
import * as Viewport from "@highalch/story/screen";
|
|
244
254
|
|
|
245
|
-
const
|
|
255
|
+
const Screen = Viewport.make({
|
|
246
256
|
css, // the compiled stylesheet(s) the markup is painted with
|
|
247
257
|
width: 360,
|
|
248
258
|
height: 200,
|
|
@@ -254,18 +264,25 @@ const screen = Screen.make({
|
|
|
254
264
|
|
|
255
265
|
await story.with({ name: "rename is rejected" })(
|
|
256
266
|
Story.given(n1, new NoteCreated({ note: ada })),
|
|
257
|
-
|
|
267
|
+
Screen.render(<NoteList account="acc-1" />), // mounted; frame 2
|
|
258
268
|
Story.when(notes.rename, { NoteId: "n1", title: "locked" }), // frame 3: pending
|
|
259
269
|
Story.rejected(new NoteLocked({ title: "locked" })), // frame 4: rolled back
|
|
260
270
|
);
|
|
261
271
|
```
|
|
262
272
|
|
|
263
|
-
- `render(element, { every? })` mounts the element with `react-dom/client` under the package's
|
|
273
|
+
- `Screen.render(element, { every? })` mounts the element with `react-dom/client` under the package's
|
|
264
274
|
`Provider` with the story's client (of the step's scope), so stock `useLiveQuery` reads story
|
|
265
|
-
data
|
|
275
|
+
data, and under the story's atom registry, so atoms read there are the story's; React is
|
|
276
|
+
flushed with `act`. After this step and every later one, once the world is quiet
|
|
266
277
|
(command replies still held, so a pending row stays pending) and React has flushed, the DOM is
|
|
267
|
-
serialised and painted. `every: false` captures only at `shot(label?)` steps. One screen per
|
|
278
|
+
serialised and painted. `every: false` captures only at `Screen.shot(label?)` steps. One screen per
|
|
268
279
|
story; it is unmounted when the story ends, passed or failed.
|
|
280
|
+
- `Screen.check(label, (container) => …)` asserts on the mounted DOM: it is given the container
|
|
281
|
+
the element is mounted in, once the world is quiet and React has flushed, so it sees what a
|
|
282
|
+
frame taken now would paint. Use it for what a picture does not say: that the title is an `h1`
|
|
283
|
+
(`container.querySelector("h1")`), that a button is disabled, what a control's value is. The
|
|
284
|
+
container is an ordinary DOM node, so a testing library's queries (`within(container)`) work on
|
|
285
|
+
it; do not use such a library's own `render`, which mounts outside the story's client.
|
|
269
286
|
- A frame is named by the story's name and the step: `<story>/<step>-<label>.png`. A screen story
|
|
270
287
|
needs `story.with({ name })`; without one, and in a hostless story, `render` fails naming
|
|
271
288
|
itself. Screen steps compose with machine steps (a running editor's frames) like any other.
|
|
@@ -280,6 +297,58 @@ await story.with({ name: "rename is rejected" })(
|
|
|
280
297
|
the story ends, so other tests keep their environment. Only this entry imports `takumi-js`,
|
|
281
298
|
`happy-dom` and `react-dom` (optional peer dependencies), checked by `vp run check:patterns`.
|
|
282
299
|
|
|
300
|
+
### Machines in a screen
|
|
301
|
+
|
|
302
|
+
A component reads its machine through atoms with stock `@effect/atom-react` hooks, and a story
|
|
303
|
+
drives that same actor with machine steps. There are no click or type steps: the story sends the
|
|
304
|
+
event, and the frame shows what the component renders for the machine's state.
|
|
305
|
+
|
|
306
|
+
```tsx
|
|
307
|
+
// The application: one editor actor per note, as atoms.
|
|
308
|
+
const runtime = Atom.runtime(Layer.empty);
|
|
309
|
+
|
|
310
|
+
export const editing = Atom.family((NoteId: string) =>
|
|
311
|
+
createActorAtoms(runtime, editor, { input: { NoteId } }),
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
export function NoteEditor({ NoteId }: { NoteId: string }) {
|
|
315
|
+
const atoms = editing(NoteId);
|
|
316
|
+
const result = useAtomValue(atoms.snapshot);
|
|
317
|
+
const send = useAtomSet(atoms.send);
|
|
318
|
+
// render from result; call send from handlers
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// The story: a frame of every state the editor passes through.
|
|
322
|
+
await story.with({ name: "editor is rejected" })(
|
|
323
|
+
Story.given(n1, new NoteCreated({ note: ada })),
|
|
324
|
+
Machine.mounted(editing("n1")),
|
|
325
|
+
Screen.render(<NoteEditor NoteId="n1" />), // Idle
|
|
326
|
+
Machine.when({ type: "Edit", title: "locked" }), // Editing
|
|
327
|
+
Machine.when({ type: "Save" }), // Saving
|
|
328
|
+
Story.rejected(new NoteLocked({ title: "locked" })), // Failed
|
|
329
|
+
Machine.then({ state: "Failed", context: { problem: "NoteLocked" } }),
|
|
330
|
+
);
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
- Each story run has its own atom registry (`Story.registry(stage)` in a step of your own), made
|
|
334
|
+
on first use and disposed when the story ends. `Screen.render` mounts the element under it, and
|
|
335
|
+
`Machine.mounted` reads the actor from it, so the component and the story see one actor and no
|
|
336
|
+
actor outlives its story.
|
|
337
|
+
- Loading `@highalch/story/xstate` adds one layer to `Atom.runtime` (`addGlobalLayer`): in a
|
|
338
|
+
story's registry it provides the `Runner`, so the commands of every machine there run on the
|
|
339
|
+
story's client and are recorded, whether or not a machine step introduced the machine. In any
|
|
340
|
+
other registry it runs commands as `invoke` does without a runner. Runtimes made by another
|
|
341
|
+
factory (`Atom.context()`) are not covered.
|
|
342
|
+
- A command is recorded as its machine's when the story introduced that machine with
|
|
343
|
+
`Machine.mounted`; two actors of one machine are told apart by the input of the command each
|
|
344
|
+
invokes. A command of a machine the story never introduced is recorded under `mounted`, and
|
|
345
|
+
`Story.settled` and `Story.rejected` still assert it.
|
|
346
|
+
- Put `Machine.mounted` before `Screen.render` when the first frame matters: the step waits until
|
|
347
|
+
the actor has started, so the first frame shows its state and not the component's own loading
|
|
348
|
+
branch.
|
|
349
|
+
- A stepped machine (`Machine.given`) has no actor, so it has no frame: a screen shows running
|
|
350
|
+
machines only.
|
|
351
|
+
|
|
283
352
|
**What the painter does and does not do.** Takumi paints HTML and CSS (flexbox, borders, radii,
|
|
284
353
|
colours, text) and runs no script. Form controls are not painted, so `input`, `textarea` and
|
|
285
354
|
`select` are serialised as `span`s with their attributes, `data-control` naming the control, and
|
|
@@ -6,6 +6,7 @@ import { getLiveQueryStatusFlags } from "@tanstack/db";
|
|
|
6
6
|
import { connectionOf, fromLive, keyOf } from "@highalch/db/internal";
|
|
7
7
|
import * as Domain from "@alchemy.run/domain";
|
|
8
8
|
import { RpcClientDefect, RpcClientError } from "effect/rpc/RpcClientError";
|
|
9
|
+
import { AtomRegistry } from "effect/reactivity";
|
|
9
10
|
// src/stage.ts
|
|
10
11
|
/** A failed expectation inside a step: what it was about, what it expected and what it saw. */
|
|
11
12
|
var Mismatch = class extends Error {
|
|
@@ -947,4 +948,21 @@ const alchemy = (from) => {
|
|
|
947
948
|
});
|
|
948
949
|
throw new Error(`alchemy(${from._tag}): only Story.given and Story.when run on a HighAlch story`);
|
|
949
950
|
};
|
|
950
|
-
|
|
951
|
+
// src/atoms.ts
|
|
952
|
+
const key = Symbol("@highalch/story/atoms");
|
|
953
|
+
const stages = /* @__PURE__ */ new WeakMap();
|
|
954
|
+
/**
|
|
955
|
+
* The story's atom registry (`effect/reactivity`), made on first use and disposed when the story
|
|
956
|
+
* ends: a screen mounts its component under it, and a machine step reads the actor a component
|
|
957
|
+
* reads through its atoms from it, so both see one actor. Each story run has its own, so no
|
|
958
|
+
* actor outlives its story.
|
|
959
|
+
*/
|
|
960
|
+
const registryOf = (stage) => stage.slot(key, () => {
|
|
961
|
+
const registry = AtomRegistry.make();
|
|
962
|
+
stages.set(registry, stage);
|
|
963
|
+
stage.end(async () => registry.dispose());
|
|
964
|
+
return { registry };
|
|
965
|
+
}).registry;
|
|
966
|
+
/** The story run a registry belongs to, if it is a story's. */
|
|
967
|
+
const stageOf = (registry) => Option.fromUndefinedOr(stages.get(registry));
|
|
968
|
+
export { Observed as A, Stage as B, settle as C, state as D, staged as E, plain as F, retryTransport as I, Wire as L, describeDefect as M, json as N, step as O, macrotask as P, transport as R, resume as S, shows as T, recover as _, as as a, reload as b, fail as c, historyOf as d, hold as f, partial as g, open as h, alchemy as i, World as j, when as k, flatten as l, mute as m, stageOf as n, check as o, idle as p, advance as r, drop as s, registryOf as t, given as u, rejected as v, settled as w, remote as x, release as y, Mismatch as z };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { $ as Hosting, A as staged, B as Options, C as release, D as settle, E as resume, F as Stage, G as World, H as Run, I as Client, J as retryTransport, K as json, L as Entry, M as step, N as when, O as settled, P as Mismatch, Q as Host, R as Observation, S as rejected, T as remote, U as Shown, V as Outcome, W as Source, X as transport, Y as Wire, Z as ClientOf, _ as hold, a as ShownRow, b as open, c as advance, d as check, et as Memory, f as drop, g as historyOf, h as given, i as ReadState, j as state, k as shows, l as alchemy, m as flatten, n as Callable, nt as memory, o as Step, p as fail, q as plain, r as Expected, s as Steps, t as Acting, tt as SessionOf, u as as, v as idle, w as reload, x as recover, y as mute, z as Observed } from "./steps-CvFOQHum.mjs";
|
|
2
2
|
import { a as make, c as invariants, d as rejectedRestores, f as subsetsAgree, i as StoryOptions, l as noFlickerBack, n as Story, o as Invariant, r as StoryFailure, s as Moment, t as StepReport, u as nothingPending } from "./run-C2SdDPWE.mjs";
|
|
3
|
-
import { Data } from "effect";
|
|
3
|
+
import { Data, Option } from "effect";
|
|
4
4
|
import { ErrorsOf, Live as Live$1, Source as Source$1, TaggedFailure } from "@highalch/db";
|
|
5
5
|
import { CollectionStatus, HealthNode } from "@tanstack/db";
|
|
6
|
+
import { AtomRegistry } from "effect/reactivity";
|
|
7
|
+
/**
|
|
8
|
+
* The story's atom registry (`effect/reactivity`), made on first use and disposed when the story
|
|
9
|
+
* ends: a screen mounts its component under it, and a machine step reads the actor a component
|
|
10
|
+
* reads through its atoms from it, so both see one actor. Each story run has its own, so no
|
|
11
|
+
* actor outlives its story.
|
|
12
|
+
*/
|
|
13
|
+
declare const registryOf: (stage: Stage) => AtomRegistry.AtomRegistry;
|
|
6
14
|
/**
|
|
7
15
|
* The constructors of every state `match` gives a live query result: `Loading()`,
|
|
8
16
|
* `Retrying({ error, attempt, data })`, `Ready({ data, feed, retry })`, `Missing()`,
|
|
@@ -39,4 +47,4 @@ type ViewResult<Row> = {
|
|
|
39
47
|
};
|
|
40
48
|
/** `view(customers.list(input), state)`: a live query result over that source in `state`. */
|
|
41
49
|
export declare const view: <Row extends object>(source: Source$1<Row>, state: ViewState<NoInfer<Row>, Extract<ErrorsOf<Array<NoInfer<Row>>>, TaggedFailure>>) => ViewResult<Row>;
|
|
42
|
-
export { type Acting, type Callable, type Client, type ClientOf, type Entry, type Expected, type Host, type Hosting, type Invariant, type Memory, Mismatch, type Moment, type Observation, Observed, type Options, type Outcome, type ReadState, type Run, type Story as Runner, type SessionOf, type Shown, type ShownRow, type Source, type Stage, type Step, type StepReport, type Steps, StoryFailure, type StoryOptions, type ViewResult, type ViewState, Wire, World, advance, alchemy, as, check, drop, fail, flatten, given, historyOf, hold, idle, invariants, json, make, memory, mute, noFlickerBack, nothingPending, open, plain, recover, rejected, rejectedRestores, release, reload, remote, resume, retryTransport, settle, settled, shows, staged, state, step, subsetsAgree, transport, when };
|
|
50
|
+
export { type Acting, type Callable, type Client, type ClientOf, type Entry, type Expected, type Host, type Hosting, type Invariant, type Memory, Mismatch, type Moment, type Observation, Observed, type Options, type Outcome, type ReadState, type Run, type Story as Runner, type SessionOf, type Shown, type ShownRow, type Source, type Stage, type Step, type StepReport, type Steps, StoryFailure, type StoryOptions, type ViewResult, type ViewState, Wire, World, advance, alchemy, as, check, drop, fail, flatten, given, historyOf, hold, idle, invariants, json, make, memory, mute, noFlickerBack, nothingPending, open, plain, recover, registryOf as registry, rejected, rejectedRestores, release, reload, remote, resume, retryTransport, settle, settled, shows, staged, state, step, subsetsAgree, transport, when };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as make, t as StoryFailure } from "./run-
|
|
2
|
-
import { C as
|
|
1
|
+
import { n as make, t as StoryFailure } from "./run-DNZqa3xX.mjs";
|
|
2
|
+
import { A as Observed, C as settle, D as state, E as staged, F as plain, I as retryTransport, L as Wire, N as json, O as step, R as transport, S as resume, T as shows, _ as recover, a as as, b as reload, c as fail, d as historyOf, f as hold, h as open, i as alchemy, j as World, k as when, l as flatten, m as mute, o as check, p as idle, r as advance, s as drop, t as registryOf, u as given, v as rejected, w as settled, x as remote, y as release, z as Mismatch } from "./atoms-H-2NrC_q.mjs";
|
|
3
3
|
import * as InMemory from "@alchemy.run/domain/InMemory";
|
|
4
4
|
import * as Model from "@alchemy.run/domain/Model";
|
|
5
5
|
import { Cause, Context, Effect, Exit, Layer, ManagedRuntime, Option, Predicate } from "effect";
|
|
@@ -318,4 +318,4 @@ const view = (source, state) => {
|
|
|
318
318
|
health: nodeOf(id, "error", null, problem(error, 1))
|
|
319
319
|
};
|
|
320
320
|
};
|
|
321
|
-
export { Live, Mismatch, Observed, Settled, StoryFailure, Wire, World, advance, alchemy, as, check, drop, fail, flatten, given, historyOf, hold, idle, invariants, json, make, memory, mute, noFlickerBack, nothingPending, open, plain, recover, rejected, rejectedRestores, release, reload, remote, resume, retryTransport, settle, settled, shows, staged, state, step, subsetsAgree, transport, view, when };
|
|
321
|
+
export { Live, Mismatch, Observed, Settled, StoryFailure, Wire, World, advance, alchemy, as, check, drop, fail, flatten, given, historyOf, hold, idle, invariants, json, make, memory, mute, noFlickerBack, nothingPending, open, plain, recover, registryOf as registry, rejected, rejectedRestores, release, reload, remote, resume, retryTransport, settle, settled, shows, staged, state, step, subsetsAgree, transport, view, when };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { B as Stage, M as describeDefect, j as World, l as flatten, z as Mismatch } from "./atoms-H-2NrC_q.mjs";
|
|
2
2
|
import { Option } from "effect";
|
|
3
3
|
// src/run.ts
|
|
4
4
|
/**
|
package/dist/screen.d.mts
CHANGED
|
@@ -31,20 +31,25 @@ type Frame = {
|
|
|
31
31
|
readonly differing?: number;
|
|
32
32
|
};
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* A viewport for stories: `const Screen = Viewport.make(options)` gives a screen of that size,
|
|
35
|
+
* whose `Screen.render(element)` is a step that mounts a React
|
|
35
36
|
* element under the package's `Provider` with the story's client (stock `useLiveQuery` reads the
|
|
36
37
|
* story's data) in a happy-dom document, and captures a frame after it and after every later
|
|
37
38
|
* step: once the world is quiet (command replies still held) and React has flushed, the DOM is
|
|
38
39
|
* serialised (current control values) and painted by takumi's WASM renderer with `css`, then
|
|
39
|
-
* compared pixel by pixel with its baseline. `shot(label?)` captures a frame on demand (with
|
|
40
|
-
* `render(element, { every: false })`, only there).
|
|
40
|
+
* compared pixel by pixel with its baseline. `Screen.shot(label?)` captures a frame on demand (with
|
|
41
|
+
* `Screen.render(element, { every: false })`, only there). `Screen.check(label, run)` asserts on
|
|
42
|
+
* the mounted DOM.
|
|
41
43
|
*/
|
|
42
44
|
export declare const make: (options: Options) => {
|
|
43
45
|
render: (element: ReactElement, settings?: {
|
|
44
46
|
readonly every?: boolean;
|
|
45
47
|
}) => Step;
|
|
46
48
|
shot: (label?: string) => Step;
|
|
49
|
+
check: (label: string, run: (container: HTMLElement) => void | Promise<void>) => Step;
|
|
47
50
|
};
|
|
51
|
+
/** A screen of a viewport: its `render`, `shot` and `check` steps (`const Screen = Viewport.make(…)`). */
|
|
52
|
+
type Screen = ReturnType<typeof make>;
|
|
48
53
|
/** An image as 8-bit RGBA pixels, row by row. */
|
|
49
54
|
type Pixels = {
|
|
50
55
|
readonly width: number;
|
|
@@ -68,4 +73,4 @@ type Difference = {
|
|
|
68
73
|
* the difference image then has the actual image's size.
|
|
69
74
|
*/
|
|
70
75
|
export declare const compare: (actual: Pixels, expected: Pixels) => Difference;
|
|
71
|
-
export type { Difference, Frame, Options, Pixels, Place, Status };
|
|
76
|
+
export type { Difference, Frame, Options, Pixels, Place, Screen, Status };
|
package/dist/screen.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { E as staged, P as macrotask, t as registryOf, z as Mismatch } from "./atoms-H-2NrC_q.mjs";
|
|
2
2
|
import { Option, Predicate, Schema } from "effect";
|
|
3
3
|
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { dirname, join, relative } from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { RegistryContext } from "@effect/atom-react";
|
|
6
7
|
import { act, createElement } from "react";
|
|
7
8
|
import { createRoot } from "react-dom/client";
|
|
8
9
|
import { render } from "takumi-js";
|
|
@@ -374,13 +375,15 @@ const index = async (output) => {
|
|
|
374
375
|
await write(join(output, "index.html"), `<!doctype html><meta charset="utf-8"><title>Story screens</title><style>body{font:14px system-ui;margin:16px}</style><h1>Story screens</h1><ul>${items.join("")}</ul>`);
|
|
375
376
|
};
|
|
376
377
|
/**
|
|
377
|
-
*
|
|
378
|
+
* A viewport for stories: `const Screen = Viewport.make(options)` gives a screen of that size,
|
|
379
|
+
* whose `Screen.render(element)` is a step that mounts a React
|
|
378
380
|
* element under the package's `Provider` with the story's client (stock `useLiveQuery` reads the
|
|
379
381
|
* story's data) in a happy-dom document, and captures a frame after it and after every later
|
|
380
382
|
* step: once the world is quiet (command replies still held) and React has flushed, the DOM is
|
|
381
383
|
* serialised (current control values) and painted by takumi's WASM renderer with `css`, then
|
|
382
|
-
* compared pixel by pixel with its baseline. `shot(label?)` captures a frame on demand (with
|
|
383
|
-
* `render(element, { every: false })`, only there).
|
|
384
|
+
* compared pixel by pixel with its baseline. `Screen.shot(label?)` captures a frame on demand (with
|
|
385
|
+
* `Screen.render(element, { every: false })`, only there). `Screen.check(label, run)` asserts on
|
|
386
|
+
* the mounted DOM.
|
|
384
387
|
*/
|
|
385
388
|
const make = (options) => {
|
|
386
389
|
const baselines = pathOf(options.baselines);
|
|
@@ -483,7 +486,7 @@ const make = (options) => {
|
|
|
483
486
|
};
|
|
484
487
|
screens.mounted = Option.some(mounted);
|
|
485
488
|
stage.end(finish(stage, mounted, label));
|
|
486
|
-
await flush(() => root.render(createElement(Provider, { client: client.db }, element)));
|
|
489
|
+
await flush(() => root.render(createElement(RegistryContext.Provider, { value: registryOf(stage) }, createElement(Provider, { client: client.db }, element))));
|
|
487
490
|
stage.after(async (done) => {
|
|
488
491
|
if (mounted.every) await capture(stage, mounted, done, label);
|
|
489
492
|
});
|
|
@@ -491,15 +494,27 @@ const make = (options) => {
|
|
|
491
494
|
};
|
|
492
495
|
/** Captures a frame now, labelled `label` (for `render(element, { every: false })`). */
|
|
493
496
|
const shot = (label = "shot") => staged(`shot ${label}`, async (stage) => {
|
|
494
|
-
const mounted = Option.getOrThrowWith(stage.slot(screensKey, () => ({ mounted: Option.none() })).mounted, () => new Mismatch("nothing is rendered yet: render(element) first"));
|
|
497
|
+
const mounted = Option.getOrThrowWith(stage.slot(screensKey, () => ({ mounted: Option.none() })).mounted, () => new Mismatch("nothing is rendered yet: Screen.render(element) first"));
|
|
495
498
|
if (!mounted.every) await capture(stage, mounted, {
|
|
496
499
|
step: stage.step,
|
|
497
500
|
label
|
|
498
501
|
}, label);
|
|
499
502
|
});
|
|
503
|
+
/**
|
|
504
|
+
* Checks the mounted DOM: `run` is given the container the element is mounted in, once the
|
|
505
|
+
* world is quiet and React has flushed, so it sees what a frame taken now would paint. Query it
|
|
506
|
+
* as any DOM (`container.querySelector("h1")`, or a testing library's `within(container)`);
|
|
507
|
+
* what `run` throws fails the step.
|
|
508
|
+
*/
|
|
509
|
+
const check = (label, run) => staged(`check ${label}`, async (stage) => {
|
|
510
|
+
const mounted = Option.getOrThrowWith(stage.slot(screensKey, () => ({ mounted: Option.none() })).mounted, () => new Mismatch("nothing is rendered yet: Screen.render(element) first"));
|
|
511
|
+
await quiet(stage.hosted(label));
|
|
512
|
+
await run(mounted.container);
|
|
513
|
+
});
|
|
500
514
|
return {
|
|
501
515
|
render: render$1,
|
|
502
|
-
shot
|
|
516
|
+
shot,
|
|
517
|
+
check
|
|
503
518
|
};
|
|
504
519
|
};
|
|
505
520
|
export { compare, decode, encode, make };
|
package/dist/xstate.d.mts
CHANGED
|
@@ -2,7 +2,9 @@ import { W as Source$1, o as Step, s as Steps } from "./steps-CvFOQHum.mjs";
|
|
|
2
2
|
import { n as Story, o as Invariant } from "./run-C2SdDPWE.mjs";
|
|
3
3
|
import { TaggedFailure } from "@highalch/db";
|
|
4
4
|
import { Answer, Input } from "@highalch/db/internal";
|
|
5
|
-
import {
|
|
5
|
+
import { AsyncResult, Atom } from "effect/reactivity";
|
|
6
|
+
import { Invocable, Invocable as Invocable$1, Marked, Marked as Marked$1, Runner, commandOf, invoke, mark, rejection } from "@highalch/db/xstate";
|
|
7
|
+
import { EffectActor } from "@xstate/effect";
|
|
6
8
|
import { AnyStateMachine, EventFromLogic, EventObject, MachineContext, StateValue } from "xstate";
|
|
7
9
|
/** Which machine a step addresses: the one of this name, else the one introduced last. */
|
|
8
10
|
type Addressed = {
|
|
@@ -51,6 +53,18 @@ declare const given: (machine: AnyStateMachine, snapshot?: FromShown<Snapshot>,
|
|
|
51
53
|
declare const start: (machine: AnyStateMachine, options?: Named & {
|
|
52
54
|
readonly input?: Input;
|
|
53
55
|
}) => Step;
|
|
56
|
+
/** The atoms of an actor, as `createActorAtoms` gives them: the story reads `actor`. */
|
|
57
|
+
type Atoms<M extends AnyStateMachine = AnyStateMachine> = {
|
|
58
|
+
readonly actor: Atom.Atom<AsyncResult.AsyncResult<EffectActor<M>, unknown>>;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* The machine enters the story **running, as a mounted component reads it**: the actor the
|
|
62
|
+
* story's registry holds for these atoms (`createActorAtoms` from `@xstate/effect/atom`), which
|
|
63
|
+
* is the one a component under `Screen.render` reads through the same atoms. Before or after the
|
|
64
|
+
* render: the story keeps the actor alive until it ends. Its commands run on the story's client
|
|
65
|
+
* (of the step's scope) and are recorded, as a started machine's are. Needs a hosted story.
|
|
66
|
+
*/
|
|
67
|
+
declare const mounted: <M extends AnyStateMachine>(atoms: Atoms<M>, options?: Named) => Step;
|
|
54
68
|
/**
|
|
55
69
|
* The machine receives an event (or one computed from what the sources show: `(read) => event`).
|
|
56
70
|
* Stepped: its next snapshot and the commands it would ask for; nothing runs. Running: sent to
|
|
@@ -70,25 +84,27 @@ declare const asked: <I extends object>(command: Invocable$1<I, unknown, unknown
|
|
|
70
84
|
declare const askedNothing: (options?: Addressed) => Step;
|
|
71
85
|
/**
|
|
72
86
|
* Stepped only: the command the machine asked for last settled with this output (its `Settled`
|
|
73
|
-
* value); the machine takes `xstate.done.actor
|
|
87
|
+
* value); the machine takes `xstate.done.actor`.
|
|
74
88
|
*/
|
|
75
89
|
declare const done: (command: Marked$1, output?: Answer, options?: Addressed) => Step;
|
|
76
90
|
/**
|
|
77
91
|
* Stepped only: the command the machine asked for last failed with this typed error; the machine
|
|
78
|
-
* takes `xstate.error.actor
|
|
92
|
+
* takes `xstate.error.actor` with the error inside an `EffectFailure`, as a running command fails.
|
|
79
93
|
*/
|
|
80
94
|
declare const failed: (command: Marked$1, error: TaggedFailure, options?: Addressed) => Step;
|
|
81
95
|
/**
|
|
82
96
|
* Machine steps, run by the one story runner beside data steps, in any order. A machine enters a
|
|
83
|
-
* story one of
|
|
84
|
-
* against the story's client, needs a host)
|
|
97
|
+
* story one of three ways, chosen once: `given` (stepped purely, works hostless), `start`
|
|
98
|
+
* (running against the story's client, needs a host) or `mounted` (running, the actor a mounted
|
|
99
|
+
* component reads through its atoms). `when`, `then`, `asked` and `askedNothing` mean the
|
|
85
100
|
* same in both; `done`/`failed` feed results to a stepped machine only. Each takes an optional
|
|
86
|
-
* `{ machine }` naming the machine (default: the one introduced last); `given`/`start` take
|
|
101
|
+
* `{ machine }` naming the machine (default: the one introduced last); `given`/`start`/`mounted` take
|
|
87
102
|
* `{ name }` (default: the machine's `id`).
|
|
88
103
|
*/
|
|
89
104
|
export declare const Machine: {
|
|
90
105
|
readonly given: typeof given;
|
|
91
106
|
readonly start: typeof start;
|
|
107
|
+
readonly mounted: typeof mounted;
|
|
92
108
|
readonly when: typeof when;
|
|
93
109
|
readonly then: typeof then;
|
|
94
110
|
readonly asked: typeof asked;
|
|
@@ -142,4 +158,4 @@ export declare const pathsOf: <M extends AnyStateMachine>(model: M, events: Read
|
|
|
142
158
|
* own report. Deterministic: the same model and options run the same paths in the same order.
|
|
143
159
|
*/
|
|
144
160
|
export declare const explore: <M extends AnyStateMachine>(exploration: Exploration<M>) => Promise<Explored>;
|
|
145
|
-
export { type Addressed, type Expected, type Exploration, type Explored, type FromShown, type Invocable, type Marked, type Named, type Path, type Read, Runner, type Snapshot, commandOf, invoke, mark };
|
|
161
|
+
export { type Addressed, type Atoms, type Expected, type Exploration, type Explored, type FromShown, type Invocable, type Marked, type Named, type Path, type Read, Runner, type Snapshot, commandOf, invoke, mark, rejection };
|
package/dist/xstate.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as StoryFailure } from "./run-
|
|
3
|
-
import { Effect, Exit, Option, Predicate, Scope } from "effect";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { E as staged, N as json, g as partial, l as flatten, n as stageOf, t as registryOf, z as Mismatch } from "./atoms-H-2NrC_q.mjs";
|
|
2
|
+
import { t as StoryFailure } from "./run-DNZqa3xX.mjs";
|
|
3
|
+
import { Cause, Effect, Exit, Layer, Option, Predicate, Scope } from "effect";
|
|
4
|
+
import { Atom, AtomRegistry } from "effect/reactivity";
|
|
5
|
+
import { Runner, Runner as Runner$1, commandOf, commandOf as commandOf$1, invoke, mark, rejection } from "@highalch/db/xstate";
|
|
6
|
+
import { EffectFailure, createEffectActor } from "@xstate/effect";
|
|
6
7
|
import { initialTransition, transition } from "xstate";
|
|
7
8
|
import { getShortestPaths, getSimplePaths } from "xstate/graph";
|
|
8
9
|
// src/xstate/machine.ts
|
|
@@ -24,7 +25,7 @@ const resolved = async (value, stage, label) => {
|
|
|
24
25
|
/** The name a machine's `setup({ actors })` registers this command's logic under, if any. */
|
|
25
26
|
const sourceOf = (machine, command) => Option.fromUndefinedOr(Object.entries(machine.sources.actors ?? {}).find(([, logic]) => Option.contains(commandOf$1(logic), command))?.[0]);
|
|
26
27
|
const nameOf = (machine, command) => Option.getOrElse(sourceOf(machine, command), () => "a command");
|
|
27
|
-
const isSpawn = (action) => action.type === "@xstate.spawn" && Predicate.hasProperty(action, "logic") && Predicate.hasProperty(action, "id") && Predicate.isString(action.id);
|
|
28
|
+
const isSpawn = (action) => action.type === "@xstate.spawn" && Predicate.hasProperty(action, "logic") && Predicate.hasProperty(action, "id") && Predicate.isString(action.id) && Predicate.hasProperty(action, "actor");
|
|
28
29
|
const spawnedBy = (actions) => actions.flatMap((action) => {
|
|
29
30
|
if (!isSpawn(action)) return [];
|
|
30
31
|
return Option.match(commandOf$1(action.logic), {
|
|
@@ -32,6 +33,7 @@ const spawnedBy = (actions) => actions.flatMap((action) => {
|
|
|
32
33
|
onSome: (command) => [{
|
|
33
34
|
command,
|
|
34
35
|
id: action.id,
|
|
36
|
+
session: action.actor.sessionId,
|
|
35
37
|
input: action.input
|
|
36
38
|
}]
|
|
37
39
|
});
|
|
@@ -61,7 +63,10 @@ const askedOf = (stage, machine, label) => {
|
|
|
61
63
|
}]
|
|
62
64
|
}));
|
|
63
65
|
};
|
|
64
|
-
const where = (machine) =>
|
|
66
|
+
const where = (machine) => {
|
|
67
|
+
if (machine.mode === "running" && machine.mounted) return `${machine.name} (mounted)`;
|
|
68
|
+
return `${machine.name} (${machine.mode})`;
|
|
69
|
+
};
|
|
65
70
|
const describe = (snapshot) => `${json(snapshot.value)} ${json(snapshot.context)}`;
|
|
66
71
|
const listed = (machine, asked) => {
|
|
67
72
|
if (asked.length === 0) return "nothing";
|
|
@@ -95,12 +100,12 @@ const prefix = (options) => Option.match(Option.fromUndefinedOr(options.machine)
|
|
|
95
100
|
});
|
|
96
101
|
const isCallable = (command) => Predicate.hasProperty(command, "with") && Predicate.isFunction(command.with);
|
|
97
102
|
/**
|
|
98
|
-
* The story's runner of a running machine's commands: on the client of the
|
|
99
|
-
* recorded as the
|
|
103
|
+
* The story's runner of a running machine's commands: on the client of the owner's scope,
|
|
104
|
+
* recorded as the owner's (`asked` reads them; `settled`/`rejected` assert the last one).
|
|
100
105
|
*/
|
|
101
|
-
const runnerOf = (world,
|
|
102
|
-
const
|
|
103
|
-
return world.start(
|
|
106
|
+
const runnerOf = (world, owner) => Runner$1.of({ run: (command, input) => Effect.promise(() => {
|
|
107
|
+
const { name, label, scope } = owner(command, input);
|
|
108
|
+
return world.start(label, scope, byOf(name), input, (client, onCommitted) => {
|
|
104
109
|
if (!isCallable(command)) throw new Error("invoke(command) takes a declared HighAlch command");
|
|
105
110
|
return command.with({ client })(input, { onCommitted });
|
|
106
111
|
}, Option.some(command));
|
|
@@ -110,6 +115,49 @@ const runnerOf = (world, machine) => Runner$1.of({ run: (command, input) => Effe
|
|
|
110
115
|
const value = Option.flatten(Option.liftPredicate(Predicate.hasProperty(outcome, "value") ? outcome.value : Option.none(), Option.isOption));
|
|
111
116
|
return Effect.succeed(Option.getOrUndefined(value));
|
|
112
117
|
})) });
|
|
118
|
+
const ownerOf = (running, command) => ({
|
|
119
|
+
name: running.name,
|
|
120
|
+
label: `${running.name}: ${nameOf(running.machine, command)}`,
|
|
121
|
+
scope: running.scope
|
|
122
|
+
});
|
|
123
|
+
/** The name commands run under when no mounted machine of the story is seen to invoke them. */
|
|
124
|
+
const unnamed = "mounted";
|
|
125
|
+
/** Whether an actor has a child that was given exactly this input (the command it invokes now). */
|
|
126
|
+
const invokes = (running, input) => Object.values(running.actor.getSnapshot().children ?? {}).some((child) => {
|
|
127
|
+
const snapshot = Predicate.hasProperty(child, "getSnapshot") ? Reflect.apply(child.getSnapshot, child, []) : void 0;
|
|
128
|
+
return Predicate.hasProperty(snapshot, "input") && snapshot.input === input;
|
|
129
|
+
});
|
|
130
|
+
/**
|
|
131
|
+
* The mounted machine a command of the story's registry is run for: the one whose actor invokes
|
|
132
|
+
* a child with this very input; else the only mounted machine that registers the command.
|
|
133
|
+
* A command of an actor the story never introduced (`Machine.mounted`) is recorded as `mounted`.
|
|
134
|
+
*/
|
|
135
|
+
const mountedOwner = (stage, world, command, input) => {
|
|
136
|
+
const candidates = [...machinesOf(stage).entered.values()].filter((each) => each.mode === "running" && each.mounted);
|
|
137
|
+
const registering = candidates.filter((each) => Option.isSome(sourceOf(each.machine, command)));
|
|
138
|
+
const found = Option.orElse(Option.fromUndefinedOr(candidates.find((each) => invokes(each, input))), () => Option.liftPredicate(registering[0], () => registering.length === 1));
|
|
139
|
+
return Option.match(Option.flatMap(found, Option.fromUndefinedOr), {
|
|
140
|
+
onNone: () => ({
|
|
141
|
+
name: unnamed,
|
|
142
|
+
label: `${unnamed}: a command`,
|
|
143
|
+
scope: world.scope
|
|
144
|
+
}),
|
|
145
|
+
onSome: (running) => ownerOf(running, command)
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* In a story's registry, every atom runtime runs its machines' commands on the story's client,
|
|
150
|
+
* recorded: a layer of `Atom.runtime` that provides the `Runner`. In any other registry (an
|
|
151
|
+
* application's) it runs the command as `invoke` does without a runner. Added once, when this
|
|
152
|
+
* entry is loaded, so a component mounted before any machine step is covered too.
|
|
153
|
+
*/
|
|
154
|
+
Atom.runtime.addGlobalLayer(Layer.effect(Runner$1, Effect.map(Effect.service(AtomRegistry.AtomRegistry), (registry) => Runner$1.of({ run: (command, input) => Option.match(stageOf(registry), {
|
|
155
|
+
onNone: () => command.effect(input),
|
|
156
|
+
onSome: (stage) => {
|
|
157
|
+
const world = stage.hosted("a mounted machine's command");
|
|
158
|
+
return runnerOf(world, (asked, given) => mountedOwner(stage, world, asked, given)).run(command, input);
|
|
159
|
+
}
|
|
160
|
+
}) }))));
|
|
113
161
|
/**
|
|
114
162
|
* The machine enters the story **stepped purely**: from this snapshot (its initial state and
|
|
115
163
|
* context when omitted, for `input`), every later step is XState's pure `transition`. Nothing
|
|
@@ -157,9 +205,10 @@ const start = (machine, options = {}) => {
|
|
|
157
205
|
let entered = Option.none();
|
|
158
206
|
const current = () => Option.getOrThrowWith(entered, () => /* @__PURE__ */ new Error(`${name} invoked a command while starting`));
|
|
159
207
|
const create = createEffectActor(machine, { input: options.input });
|
|
160
|
-
const actor = Effect.runSync(create.pipe(Effect.provideService(Runner$1, runnerOf(world, current)), Effect.provideService(Scope.Scope, scope)));
|
|
208
|
+
const actor = Effect.runSync(create.pipe(Effect.provideService(Runner$1, runnerOf(world, (command) => ownerOf(current(), command))), Effect.provideService(Scope.Scope, scope)));
|
|
161
209
|
const running = {
|
|
162
210
|
mode: "running",
|
|
211
|
+
mounted: false,
|
|
163
212
|
name,
|
|
164
213
|
machine,
|
|
165
214
|
actor,
|
|
@@ -176,6 +225,36 @@ const start = (machine, options = {}) => {
|
|
|
176
225
|
});
|
|
177
226
|
};
|
|
178
227
|
/**
|
|
228
|
+
* The machine enters the story **running, as a mounted component reads it**: the actor the
|
|
229
|
+
* story's registry holds for these atoms (`createActorAtoms` from `@xstate/effect/atom`), which
|
|
230
|
+
* is the one a component under `Screen.render` reads through the same atoms. Before or after the
|
|
231
|
+
* render: the story keeps the actor alive until it ends. Its commands run on the story's client
|
|
232
|
+
* (of the step's scope) and are recorded, as a started machine's are. Needs a hosted story.
|
|
233
|
+
*/
|
|
234
|
+
const mounted = (atoms, options = {}) => {
|
|
235
|
+
const label = `enter ${options.name ?? "a machine"} as mounted`;
|
|
236
|
+
return staged(label, async (stage) => {
|
|
237
|
+
const world = stage.hosted(label);
|
|
238
|
+
await world.connect();
|
|
239
|
+
const registry = registryOf(stage);
|
|
240
|
+
const release = registry.mount(atoms.actor);
|
|
241
|
+
stage.end(async () => release());
|
|
242
|
+
const actor = await Effect.runPromise(AtomRegistry.getResult(registry, atoms.actor, { suspendOnWaiting: true })).catch((error) => {
|
|
243
|
+
throw new Mismatch("the atoms' actor did not start: its runtime failed to build", [String(error)]);
|
|
244
|
+
});
|
|
245
|
+
enter(stage, {
|
|
246
|
+
mode: "running",
|
|
247
|
+
mounted: true,
|
|
248
|
+
name: options.name ?? actor.logic.id,
|
|
249
|
+
machine: actor.logic,
|
|
250
|
+
actor,
|
|
251
|
+
scope: world.scope,
|
|
252
|
+
since: world.next()
|
|
253
|
+
});
|
|
254
|
+
await world.settle(false);
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
179
258
|
* The machine receives an event (or one computed from what the sources show: `(read) => event`).
|
|
180
259
|
* Stepped: its next snapshot and the commands it would ask for; nothing runs. Running: sent to
|
|
181
260
|
* the actor, which reacts and starts the commands it asks for on the story's client; their replies
|
|
@@ -250,21 +329,23 @@ const answer = (machine, command, outcome, value) => {
|
|
|
250
329
|
if (!spawned) throw new Mismatch(`${where(machine)} never asked for ${nameOf(machine.machine, command)}`, [`asked so far: ${listed(machine, machine.invoked)}`]);
|
|
251
330
|
const event = Option.match(Option.liftPredicate(outcome, (tag) => tag === "done"), {
|
|
252
331
|
onSome: () => ({
|
|
253
|
-
type:
|
|
332
|
+
type: "xstate.done.actor",
|
|
254
333
|
output: value,
|
|
255
|
-
actorId: spawned.id
|
|
334
|
+
actorId: spawned.id,
|
|
335
|
+
sessionId: spawned.session
|
|
256
336
|
}),
|
|
257
337
|
onNone: () => ({
|
|
258
|
-
type:
|
|
259
|
-
error: value,
|
|
260
|
-
actorId: spawned.id
|
|
338
|
+
type: "xstate.error.actor",
|
|
339
|
+
error: new EffectFailure(Cause.fail(value)),
|
|
340
|
+
actorId: spawned.id,
|
|
341
|
+
sessionId: spawned.session
|
|
261
342
|
})
|
|
262
343
|
});
|
|
263
344
|
return stepped(machine, event);
|
|
264
345
|
};
|
|
265
346
|
/**
|
|
266
347
|
* Stepped only: the command the machine asked for last settled with this output (its `Settled`
|
|
267
|
-
* value); the machine takes `xstate.done.actor
|
|
348
|
+
* value); the machine takes `xstate.done.actor`.
|
|
268
349
|
*/
|
|
269
350
|
const done = (command, output, options = {}) => staged(`${prefix(options)}command done`, async (stage) => {
|
|
270
351
|
const machine = steppedOnly(find(stage, options), "Machine.done");
|
|
@@ -272,7 +353,7 @@ const done = (command, output, options = {}) => staged(`${prefix(options)}comman
|
|
|
272
353
|
});
|
|
273
354
|
/**
|
|
274
355
|
* Stepped only: the command the machine asked for last failed with this typed error; the machine
|
|
275
|
-
* takes `xstate.error.actor
|
|
356
|
+
* takes `xstate.error.actor` with the error inside an `EffectFailure`, as a running command fails.
|
|
276
357
|
*/
|
|
277
358
|
const failed = (command, error, options = {}) => staged(`${prefix(options)}command failed with ${error._tag}`, async (stage) => {
|
|
278
359
|
const machine = steppedOnly(find(stage, options), "Machine.failed");
|
|
@@ -280,15 +361,17 @@ const failed = (command, error, options = {}) => staged(`${prefix(options)}comma
|
|
|
280
361
|
});
|
|
281
362
|
/**
|
|
282
363
|
* Machine steps, run by the one story runner beside data steps, in any order. A machine enters a
|
|
283
|
-
* story one of
|
|
284
|
-
* against the story's client, needs a host)
|
|
364
|
+
* story one of three ways, chosen once: `given` (stepped purely, works hostless), `start`
|
|
365
|
+
* (running against the story's client, needs a host) or `mounted` (running, the actor a mounted
|
|
366
|
+
* component reads through its atoms). `when`, `then`, `asked` and `askedNothing` mean the
|
|
285
367
|
* same in both; `done`/`failed` feed results to a stepped machine only. Each takes an optional
|
|
286
|
-
* `{ machine }` naming the machine (default: the one introduced last); `given`/`start` take
|
|
368
|
+
* `{ machine }` naming the machine (default: the one introduced last); `given`/`start`/`mounted` take
|
|
287
369
|
* `{ name }` (default: the machine's `id`).
|
|
288
370
|
*/
|
|
289
371
|
const Machine = {
|
|
290
372
|
given,
|
|
291
373
|
start,
|
|
374
|
+
mounted,
|
|
292
375
|
when,
|
|
293
376
|
then,
|
|
294
377
|
asked,
|
|
@@ -402,4 +485,4 @@ const explore = async (exploration) => {
|
|
|
402
485
|
steps
|
|
403
486
|
};
|
|
404
487
|
};
|
|
405
|
-
export { Machine, Runner, commandOf, explore, invoke, mark, pathsOf };
|
|
488
|
+
export { Machine, Runner, commandOf, explore, invoke, mark, pathsOf, rejection };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@highalch/story",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Stories for HighAlch data and the XState machines over it: given, when, then, with screenshots painted without a browser.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"alchemy",
|
|
@@ -45,13 +45,14 @@
|
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@alchemy.run/domain": "2.0.0-beta.87",
|
|
48
|
-
"@
|
|
48
|
+
"@effect/atom-react": "4.1.4",
|
|
49
|
+
"@highalch/db": "0.0.3",
|
|
49
50
|
"@tanstack/db": "1.0.0-rc.1.5",
|
|
50
51
|
"@tanstack/react-db": "1.0.0-rc.1.5",
|
|
51
52
|
"@types/node": "^24",
|
|
52
53
|
"@types/react": "19.2.17",
|
|
53
54
|
"@types/react-dom": "19.2.3",
|
|
54
|
-
"@xstate/effect": "
|
|
55
|
+
"@xstate/effect": "1.0.0-beta.2",
|
|
55
56
|
"effect": "4.1.4",
|
|
56
57
|
"happy-dom": "20.11.1",
|
|
57
58
|
"react": "19.2.7",
|
|
@@ -59,21 +60,25 @@
|
|
|
59
60
|
"takumi-js": "2.14.0",
|
|
60
61
|
"typescript": "^7.0.2",
|
|
61
62
|
"vite-plus": "1.5.1",
|
|
62
|
-
"xstate": "6.0.0-
|
|
63
|
+
"xstate": "6.0.0-beta.3"
|
|
63
64
|
},
|
|
64
65
|
"peerDependencies": {
|
|
65
66
|
"@alchemy.run/domain": "2.0.0-beta.87",
|
|
66
|
-
"@
|
|
67
|
+
"@effect/atom-react": "^4.1.4",
|
|
68
|
+
"@highalch/db": "^0.0.3",
|
|
67
69
|
"@tanstack/db": "1.0.0-rc.1.5",
|
|
68
|
-
"@xstate/effect": "
|
|
70
|
+
"@xstate/effect": "1.0.0-beta.2",
|
|
69
71
|
"effect": "^4.1.4",
|
|
70
72
|
"happy-dom": "^20.11.1",
|
|
71
73
|
"react": "^19.2.7",
|
|
72
74
|
"react-dom": "^19.2.7",
|
|
73
75
|
"takumi-js": "^2.14.0",
|
|
74
|
-
"xstate": "6.0.0-
|
|
76
|
+
"xstate": "6.0.0-beta.3"
|
|
75
77
|
},
|
|
76
78
|
"peerDependenciesMeta": {
|
|
79
|
+
"@effect/atom-react": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
77
82
|
"@xstate/effect": {
|
|
78
83
|
"optional": true
|
|
79
84
|
},
|