@wardic/test 0.0.0-stage → 1.0.0-rc.10

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/dist/input.js ADDED
@@ -0,0 +1,108 @@
1
+ import * as InputSource from "@wardic/core/InputSource";
2
+ import * as Bool from "effect/Boolean";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Predicate from "effect/Predicate";
5
+ import * as Layer from "effect/Layer";
6
+ import * as UndefinedOr from "effect/UndefinedOr";
7
+ import * as Option from "effect/Option";
8
+ import * as Feeds from "./feeds.js";
9
+ /**
10
+ * An InputSource that replays fixed scripts of events, as **Input feeds** that end after their
11
+ * script: a Stream over one ends too. `pointer(target)` replays the same `pointerEvents`,
12
+ * `focus(target)` the same `focusEvents` and `keyboard(target)` the same `keyEvents` to each
13
+ * subscriber (each ignores `target` — tests address a single surface). The first positional arg is
14
+ * pointer events for back-compat; focus and key events are optional, defaulting to empty so existing
15
+ * call sites emit nothing on them. Each script plays whole when its feed is subscribed, so scripts of
16
+ * two kinds don't interleave: a test that needs them to pushes them ({@link pushable}). Called with no
17
+ * args it yields an InputSource that emits nothing — for tests that don't exercise gestures. It adopts
18
+ * no pointer: `adoptPointer` delivers nothing.
19
+ */
20
+ // Input feeds: #318. adoptPointer: #431.
21
+ export const layer = (pointerEvents = [], focusEvents = [], keyEvents = []) => Layer.succeed(InputSource.InputSource, {
22
+ pointer: () => Feeds.script(pointerEvents),
23
+ keyboard: () => Feeds.script(keyEvents),
24
+ focus: () => Feeds.script(focusEvents),
25
+ adoptPointer: () => Option.none(),
26
+ });
27
+ /**
28
+ * A push-based InputSource for tests that need to interleave pointer events with
29
+ * other effects (e.g. advancing the Clock between a release and a re-grab). Unlike
30
+ * {@link layer} — which replays a fixed script that drains immediately — this
31
+ * form lets the test `push` events one at a time, on its own schedule.
32
+ *
33
+ * Backed by a hub (`feeds.ts`) so that every subscription to `pointer(target)`
34
+ * hears every event: a consumer may subscribe to `input.pointer(target)` more than once
35
+ * (a binding's drag track reads the drag fold and the grabs). There is no replay buffer,
36
+ * so only events pushed *after* a subscription is live reach it.
37
+ *
38
+ * `push` doesn't yield: a subscriber handles the event inside the push, as an
39
+ * Environment's consumers handle a real event inside its dispatch, so a pipeline that hops
40
+ * through Effect's scheduler on its way to its sink hasn't got there when `push` returns,
41
+ * and its test sees it.
42
+ *
43
+ * Returns `{ layer, push }`. Resolve it inside an `Effect.gen`, provide `layer`,
44
+ * and drive the surface with `push`:
45
+ *
46
+ * @example
47
+ * const input = yield* TestInput.pushable()
48
+ * yield* Stream.runForEach(drag(undefined), …).pipe(Effect.fork…) // subscribe
49
+ * yield* input.push(ev("down", 0, 0, 0))
50
+ *
51
+ * Returns `pushFocus` and `pushKey` alongside `push` so focus/blur transitions and keys can be
52
+ * driven on the same schedule; `focus(target)` and `keyboard(target)` mirror `pointer(target)`, each
53
+ * with its own hub.
54
+ *
55
+ * `adoptPointer(target, raw)` takes a pointer event as its `raw`: a pressed one is pushed on
56
+ * the pointer hub as an adopted down, inside the call, as the DOM's is. The hub holds no pointer, so
57
+ * the test pushes that pointer's later events itself.
58
+ *
59
+ * `options.focusWithin` is the source's `focusWithin`, for a test of a **Keyboard press** that
60
+ * holds its keys only while focus is within its target. Without it the source says nothing of focus,
61
+ * and a press hears every key pushed.
62
+ *
63
+ * `pushTo(target, event)` and `pushFocusTo(target, event)` feed `pointerAmong` and `focusAmong`, the feeds
64
+ * over many targets: each subscriber hears the event with the target its `memberOf` names `target`, or
65
+ * nothing when it names none. `push` and `pushFocus` reach neither, nor these the per-target feeds.
66
+ */
67
+ // The hub: #318. No yield: ADR-0032. Keys: #427. adoptPointer: #431. focusWithin: #460. The feeds over
68
+ // many targets: #665's PR 5.
69
+ export const pushable = (options) => Effect.sync(() => {
70
+ const pointer = Feeds.hub();
71
+ const focus = Feeds.hub();
72
+ const keys = Feeds.hub();
73
+ const pointerAmong = Feeds.hub();
74
+ const focusAmong = Feeds.hub();
75
+ return {
76
+ layer: Layer.succeed(InputSource.InputSource, {
77
+ pointer: () => pointer.feed,
78
+ keyboard: () => keys.feed,
79
+ focus: () => focus.feed,
80
+ focusWithin: UndefinedOr.map(options, (o) => o.focusWithin),
81
+ adoptPointer: (_target, raw) => {
82
+ if (isPressedPointer(raw)) {
83
+ const down = { ...raw, type: "down", adopted: true };
84
+ Effect.runSync(pointer.push(down));
85
+ return Option.some(down);
86
+ }
87
+ return Option.none();
88
+ },
89
+ pointerAmong: among(pointerAmong),
90
+ focusAmong: among(focusAmong),
91
+ }),
92
+ push: pointer.push,
93
+ pushFocus: focus.push,
94
+ pushKey: keys.push,
95
+ pushTo: (target, event) => pointerAmong.push({ target, event }),
96
+ pushFocusTo: (target, event) => focusAmong.push({ target, event }),
97
+ };
98
+ });
99
+ /**
100
+ * A feed over many targets on `hub`, for a subscriber's `memberOf`: each pushed event with the target
101
+ * `memberOf` names its pushed one, none when it names none.
102
+ */
103
+ const among = (hub) => (memberOf) => (emit, end) => hub.feed((pushed) => {
104
+ Option.map(memberOf(pushed.target), (target) => emit({ target, event: pushed.event }));
105
+ }, end);
106
+ /** Whether `raw` is a pointer event with a button pressed: one the pushable InputSource adopts (#431). */
107
+ const isPressedPointer = (raw) => Bool.and(Predicate.hasProperty(raw, "pointerId"), Predicate.hasProperty(raw, "pressed")) && raw.pressed;
108
+ //# sourceMappingURL=input.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input.js","sourceRoot":"","sources":["../src/input.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,WAAW,MAAM,0BAA0B,CAAA;AACvD,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAA;AACtC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,SAAS,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,WAAW,MAAM,oBAAoB,CAAA;AACjD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAC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IAClB,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;AACxF,CAAC,EAAE,GAAG,CAAC,CAAA;AAEX,0GAA0G;AAC1G,MAAM,gBAAgB,GAAG,CAAC,GAAY,EAAuC,EAAE,CAC7E,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,WAAW,CAAC,GAAG,EAAE,WAAW,CAAC,EAAE,SAAS,CAAC,WAAW,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,IAAK,GAAoC,CAAC,OAAO,CAAA"}
@@ -0,0 +1,28 @@
1
+ import * as Announcer from "@wardic/core/Announcer";
2
+ import type * as Clock from "@wardic/core/Clock";
3
+ import * as DefaultTransition from "@wardic/core/DefaultTransition";
4
+ import * as FrameLoop from "@wardic/core/FrameLoop";
5
+ import type * as InputSource from "@wardic/core/InputSource";
6
+ import * as InterpolatorRegistry from "@wardic/core/InterpolatorRegistry";
7
+ import type * as Probe from "@wardic/core/Probe";
8
+ import * as ReducedMotion from "@wardic/core/ReducedMotion";
9
+ import * as RefreshFrameSource from "@wardic/core/RefreshFrameSource";
10
+ import type * as Renderer from "@wardic/core/Renderer";
11
+ import type * as ScrollSource from "@wardic/core/ScrollSource";
12
+ import * as Layer from "effect/Layer";
13
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry";
14
+ /**
15
+ * The full headless environment: deterministic Clock (driven by `frames`), a Refresh frame
16
+ * source that never calls back, recording Renderer, static Probe and ScrollSource, silent
17
+ * InputSource, core's real interpolator registry, a fresh AtomRegistry for reading Probe atoms
18
+ * (`viewport`, `prefersReducedMotion`), and a fresh Drag activity, so one test's
19
+ * drag never blocks another's hover or press, and core's Frame loop over that
20
+ * Clock. Provide this in place of `DomLive` to test animation code without a
21
+ * page. It draws nothing on one: an element rendered under it keeps its Starting style, so an
22
+ * app's components are tested with `DomLive` under the test runner's fake timers instead
23
+ * (https://wardic.dev/guides/testing).
24
+ */
25
+ export declare const testLayer: (frames?: ReadonlyArray<number>) => Layer.Layer<Clock.Clock | RefreshFrameSource.RefreshFrameSource | FrameLoop.FrameLoop | Renderer.Renderer | Probe.Probe | ScrollSource.ScrollSource | InputSource.InputSource | InterpolatorRegistry.InterpolatorRegistry | ReducedMotion.PositionalKeys | DefaultTransition.DefaultTransition | Announcer.Announcer | AtomRegistry.AtomRegistry>;
26
+ /** `testLayer()` with an empty clock — for tests that drive their own frames. It never ticks on its own. */
27
+ export declare const TestLive: Layer.Layer<Announcer.Announcer | AtomRegistry.AtomRegistry | Clock.Clock | DefaultTransition.DefaultTransition | FrameLoop.FrameLoop | InputSource.InputSource | InterpolatorRegistry.InterpolatorRegistry | ReducedMotion.PositionalKeys | Probe.Probe | RefreshFrameSource.RefreshFrameSource | Renderer.Renderer | ScrollSource.ScrollSource, never, never>;
28
+ //# sourceMappingURL=layer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layer.d.ts","sourceRoot":"","sources":["../src/layer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAA;AACnD,OAAO,KAAK,KAAK,KAAK,MAAM,oBAAoB,CAAA;AAChD,OAAO,KAAK,iBAAiB,MAAM,gCAAgC,CAAA;AAEnE,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAA;AACnD,OAAO,KAAK,KAAK,WAAW,MAAM,0BAA0B,CAAA;AAC5D,OAAO,KAAK,oBAAoB,MAAM,mCAAmC,CAAA;AAEzE,OAAO,KAAK,KAAK,KAAK,MAAM,oBAAoB,CAAA;AAChD,OAAO,KAAK,aAAa,MAAM,4BAA4B,CAAA;AAC3D,OAAO,KAAK,kBAAkB,MAAM,iCAAiC,CAAA;AACrE,OAAO,KAAK,KAAK,QAAQ,MAAM,uBAAuB,CAAA;AACtD,OAAO,KAAK,KAAK,YAAY,MAAM,2BAA2B,CAAA;AAE9D,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAA;AAa9D;;;;;;;;;;GAUG;AAEH,eAAO,MAAM,SAAS,YACZ,aAAa,CAAC,MAAM,CAAC,KAC5B,KAAK,CAAC,KAAK,CACV,KAAK,CAAC,KAAK,GACX,kBAAkB,CAAC,kBAAkB,GACrC,SAAS,CAAC,SAAS,GACnB,QAAQ,CAAC,QAAQ,GACjB,KAAK,CAAC,KAAK,GACX,YAAY,CAAC,YAAY,GACzB,WAAW,CAAC,WAAW,GACvB,oBAAoB,CAAC,oBAAoB,GACzC,aAAa,CAAC,cAAc,GAC5B,iBAAiB,CAAC,iBAAiB,GACnC,SAAS,CAAC,SAAS,GACnB,YAAY,CAAC,YAAY,CAsB1B,CAAA;AAEH,4GAA4G;AAC5G,eAAO,MAAM,QAAQ,iWAAc,CAAA"}
package/dist/layer.js ADDED
@@ -0,0 +1,43 @@
1
+ import * as Announcer from "@wardic/core/Announcer";
2
+ import * as DefaultTransition from "@wardic/core/DefaultTransition";
3
+ import * as DragActivity from "@wardic/core/DragActivity";
4
+ import * as FrameLoop from "@wardic/core/FrameLoop";
5
+ import * as InterpolatorRegistry from "@wardic/core/InterpolatorRegistry";
6
+ import * as PressClaims from "@wardic/core/PressClaims";
7
+ import * as ReducedMotion from "@wardic/core/ReducedMotion";
8
+ import * as RefreshFrameSource from "@wardic/core/RefreshFrameSource";
9
+ import { constUndefined, constVoid } from "effect/Function";
10
+ import * as Layer from "effect/Layer";
11
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry";
12
+ import * as TestClock from "./clock.js";
13
+ import * as TestInput from "./input.js";
14
+ import * as TestProbe from "./probe.js";
15
+ import * as TestRenderer from "./renderer.js";
16
+ /**
17
+ * The headless Environment's Refresh frame source: it never calls back, as its Clock never ticks
18
+ * on its own, so a flush gate built on it never flushes by itself. A test that fires frames
19
+ * provides `TestClock.pushable()`'s layer over it (ADR-0031, decision 6).
20
+ */
21
+ const stillFrames = { requestFrame: constUndefined, cancelFrame: constVoid };
22
+ /**
23
+ * The full headless environment: deterministic Clock (driven by `frames`), a Refresh frame
24
+ * source that never calls back, recording Renderer, static Probe and ScrollSource, silent
25
+ * InputSource, core's real interpolator registry, a fresh AtomRegistry for reading Probe atoms
26
+ * (`viewport`, `prefersReducedMotion`), and a fresh Drag activity, so one test's
27
+ * drag never blocks another's hover or press, and core's Frame loop over that
28
+ * Clock. Provide this in place of `DomLive` to test animation code without a
29
+ * page. It draws nothing on one: an element rendered under it keeps its Starting style, so an
30
+ * app's components are tested with `DomLive` under the test runner's fake timers instead
31
+ * (https://wardic.dev/guides/testing).
32
+ */
33
+ // The Drag activity: ADR-0019. The Frame loop: ADR-0027. The testing guide: #558.
34
+ export const testLayer = (frames = []) => FrameLoop.layer.pipe(Layer.provideMerge(Layer.mergeAll(TestClock.layer(frames), RefreshFrameSource.layer(stillFrames), TestRenderer.layer, TestProbe.layer(), TestInput.layer(), InterpolatorRegistry.layer,
35
+ // The transforms only: the Test Environment names no box keys (ADR-0036).
36
+ ReducedMotion.transformsOnly,
37
+ // No opinion on a key's Default transition: every key on one spring (ADR-0051).
38
+ DefaultTransition.layerOneSpring,
39
+ // No assistive technology to reach headless (ADR-0037).
40
+ Announcer.layerSilent, AtomRegistry.layer, Layer.sync(DragActivity.DragActivity, DragActivity.make), Layer.sync(PressClaims.PressClaims, PressClaims.make))));
41
+ /** `testLayer()` with an empty clock — for tests that drive their own frames. It never ticks on its own. */
42
+ export const TestLive = testLayer();
43
+ //# sourceMappingURL=layer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layer.js","sourceRoot":"","sources":["../src/layer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAA;AAEnD,OAAO,KAAK,iBAAiB,MAAM,gCAAgC,CAAA;AACnE,OAAO,KAAK,YAAY,MAAM,2BAA2B,CAAA;A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YAAY,EAAE,YAAY,CAAC,IAAI,CAAC,EACxD,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,EAAE,WAAW,CAAC,IAAI,CAAC,CACtD,CACF,CACF,CAAA;AAEH,4GAA4G;AAC5G,MAAM,CAAC,MAAM,QAAQ,GAAG,SAAS,EAAE,CAAA"}
@@ -0,0 +1,48 @@
1
+ import * as Probe from "@wardic/core/Probe";
2
+ import * as ScrollSource from "@wardic/core/ScrollSource";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Layer from "effect/Layer";
5
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry";
6
+ /** Options for the static test Probe. */
7
+ export interface Options {
8
+ readonly bounds?: Probe.Bounds;
9
+ readonly viewport?: {
10
+ readonly width: number;
11
+ readonly height: number;
12
+ };
13
+ readonly prefersReducedMotion?: boolean;
14
+ /** Whether the Environment is shown: `true` when left out. */
15
+ readonly visible?: boolean;
16
+ /** Every container's scroll, as the `ScrollSource` gives it: a page that doesn't scroll when left out. */
17
+ readonly scroll?: ScrollSource.Scroll;
18
+ /**
19
+ * Script of intersection (in-view) booleans replayed by `observeIntersection`, an **Input feed**
20
+ * that ends after it. Mirrors the InputSource test harness: omitted (or empty) means it
21
+ * emits nothing and ends.
22
+ */
23
+ readonly intersection?: ReadonlyArray<boolean>;
24
+ }
25
+ /**
26
+ * A static Probe: fixed bounds/viewport. `observeIntersection` replays the
27
+ * optional `intersection` script to each subscriber (empty by default, so it
28
+ * emits nothing); `observeResize` stays empty. With it, a `ScrollSource`
29
+ * whose every container reads `scroll`, as the Probe's `scroll` did until #518.
30
+ */
31
+ export declare const layer: (options?: Options) => Layer.Layer<Probe.Probe | ScrollSource.ScrollSource>;
32
+ /**
33
+ * A push-based Probe for tests that need to interleave intersection
34
+ * (in-view/out-of-view) transitions with other effects on their own schedule —
35
+ * mirroring {@link TestInput.pushable}. `observeIntersection(target)` subscribes
36
+ * to a hub, so every subscriber sees the same transitions and only
37
+ * events pushed *after* a subscription is live reach it. `pushIntersection`
38
+ * doesn't yield: a subscriber handles the transition inside the push,
39
+ * as `TestInput.pushable`'s do. `pushScroll` sets the scroll every container reads,
40
+ * through the Environment's registry, as a scroll event would.
41
+ * measure/viewport/prefersReducedMotion/observeResize behave as in {@link layer}.
42
+ */
43
+ export declare const pushable: (options?: Options) => Effect.Effect<{
44
+ readonly layer: Layer.Layer<Probe.Probe | ScrollSource.ScrollSource>;
45
+ readonly pushIntersection: (inView: boolean) => Effect.Effect<void>;
46
+ readonly pushScroll: (scroll: ScrollSource.Scroll) => Effect.Effect<void, never, AtomRegistry.AtomRegistry>;
47
+ }>;
48
+ //# sourceMappingURL=probe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"probe.d.ts","sourceRoot":"","sources":["../src/probe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,oBAAoB,CAAA;AAC3C,OAAO,KAAK,YAAY,MAAM,2BAA2B,CAAA;AAEzD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAGrC,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAA;AAW9D,yCAAyC;AAGzC,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,MAAM,CAAA;IAC9B,QAAQ,CAAC,QAAQ,CAAC,EAAE;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;IACvE,QAAQ,CAAC,oBAAoB,CAAC,EAAE,OAAO,CAAA;IACvC,8DAA8D;IAC9D,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;IAC1B,0GAA0G;IAC1G,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC,MAAM,CAAA;IACrC;;;;OAIG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAC/C;AAED;;;;;GAKG;AACH,eAAO,MAAM,KAAK,aAAa,OAAO,KAAQ,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,GAAG,YAAY,CAAC,YAAY,CAShG,CAAA;AAeD;;;;;;;;;;GAUG;AAEH,eAAO,MAAM,QAAQ,aAAa,OAAO,KAAQ,MAAM,CAAC,MAAM,CAAC;IAC7D,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,GAAG,YAAY,CAAC,YAAY,CAAC,CAAA;IACpE,QAAQ,CAAC,gBAAgB,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACnE,QAAQ,CAAC,UAAU,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,YAAY,CAAC,YAAY,CAAC,CAAA;CAC5G,CAUG,CAAA"}
package/dist/probe.js ADDED
@@ -0,0 +1,61 @@
1
+ import * as Probe from "@wardic/core/Probe";
2
+ import * as ScrollSource from "@wardic/core/ScrollSource";
3
+ import * as Arr from "effect/Array";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Layer from "effect/Layer";
6
+ import * as Stream from "effect/Stream";
7
+ import * as Atom from "effect/reactivity/Atom";
8
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry";
9
+ import * as Feeds from "./feeds.js";
10
+ const ZERO_BOUNDS = { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0 };
11
+ const VIEWPORT = { width: 1024, height: 768 };
12
+ /** A page that doesn't scroll: no range, and progress 1, as Motion reads a container with none. */
13
+ const STILL_SCROLL = { x: { current: 0, length: 0, progress: 1 }, y: { current: 0, length: 0, progress: 1 } };
14
+ /** A scripted in-view boolean as the Probe reports it, with no platform record. */
15
+ const toIntersection = (intersecting) => ({ intersecting });
16
+ /**
17
+ * A static Probe: fixed bounds/viewport. `observeIntersection` replays the
18
+ * optional `intersection` script to each subscriber (empty by default, so it
19
+ * emits nothing); `observeResize` stays empty. With it, a `ScrollSource`
20
+ * whose every container reads `scroll`, as the Probe's `scroll` did until #518.
21
+ */
22
+ export const layer = (options = {}) => {
23
+ const { intersection = [], scroll = STILL_SCROLL } = options;
24
+ return Layer.mergeAll(Layer.succeed(Probe.Probe, {
25
+ ...fixed(options),
26
+ observeIntersection: () => Feeds.script(Arr.map(intersection, toIntersection)),
27
+ }), scrollLayer(Atom.make(scroll)));
28
+ };
29
+ /** Both Probes' fixed part: the bounds, the viewport, the preference and the page's visibility, each defaulted, and no resizes. */
30
+ const fixed = ({ bounds = ZERO_BOUNDS, viewport = VIEWPORT, prefersReducedMotion = false, visible = true }) => ({
31
+ measure: () => Effect.sync(() => bounds),
32
+ viewport: Atom.make(viewport),
33
+ prefersReducedMotion: Atom.make(prefersReducedMotion),
34
+ visible: Atom.make(visible),
35
+ observeResize: () => Stream.empty,
36
+ });
37
+ /** One scroll Atom for every container, target and offset (#234). */
38
+ const scrollLayer = (scroll) => Layer.succeed(ScrollSource.ScrollSource, { scroll: () => Effect.succeed(scroll) });
39
+ /**
40
+ * A push-based Probe for tests that need to interleave intersection
41
+ * (in-view/out-of-view) transitions with other effects on their own schedule —
42
+ * mirroring {@link TestInput.pushable}. `observeIntersection(target)` subscribes
43
+ * to a hub, so every subscriber sees the same transitions and only
44
+ * events pushed *after* a subscription is live reach it. `pushIntersection`
45
+ * doesn't yield: a subscriber handles the transition inside the push,
46
+ * as `TestInput.pushable`'s do. `pushScroll` sets the scroll every container reads,
47
+ * through the Environment's registry, as a scroll event would.
48
+ * measure/viewport/prefersReducedMotion/observeResize behave as in {@link layer}.
49
+ */
50
+ // The hub: #318. No yield: ADR-0032. Scroll: #234.
51
+ export const pushable = (options = {}) => Effect.sync(() => {
52
+ const intersections = Feeds.hub();
53
+ const { scroll = STILL_SCROLL } = options;
54
+ const scrollAtom = Atom.make(scroll);
55
+ return {
56
+ layer: Layer.mergeAll(Layer.succeed(Probe.Probe, { ...fixed(options), observeIntersection: () => intersections.feed }), scrollLayer(scrollAtom)),
57
+ pushIntersection: (inView) => intersections.push(toIntersection(inView)),
58
+ pushScroll: (next) => AtomRegistry.AtomRegistry.use((registry) => Effect.sync(() => registry.set(scrollAtom, next))),
59
+ };
60
+ });
61
+ //# sourceMappingURL=probe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"probe.js","sourceRoot":"","sources":["../src/probe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,oBAAoB,CAAA;AAC3C,OAAO,KAAK,YAAY,MAAM,2BAA2B,CAAA;AACzD,OAAO,KAAK,GAAG,MAAM,cAAc,CAAA;AACnC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAA;AAC9C,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAA;AAC9D,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AAEnC,MAAM,WAAW,GAAiB,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAA;AAC3G,MAAM,QAAQ,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAA;AAC7C,mGAAmG;AACnG,MAAM,YAAY,GAAwB,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC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W,GAAG,CAAC,MAAsC,EAA0C,EAAE,CACrG,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;AAEpF;;;;;;;;;;GAUG;AACH,mDAAmD;AACnD,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,OAAO,GAAY,EAAE,EAI3C,EAAE,CACH,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;IACf,MAAM,aAAa,GAAG,KAAK,CAAC,GAAG,EAAsB,CAAA;IACrD,MAAM,EAAE,MAAM,GAAG,YAAY,EAAE,GAAG,OAAO,CAAA;IACzC,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACpC,OAAO;QACL,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,EAAE,mBAAmB,EAAE,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,EAAE,WAAW,CAAC,UAAU,CAAC,CAAC;QAChJ,gBAAgB,EAAE,CAAC,MAAe,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QACjF,UAAU,EAAE,CAAC,IAAyB,EAAE,EAAE,CAAC,YAAY,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC;KAC1I,CAAA;AACH,CAAC,CAAC,CAAA"}
@@ -0,0 +1,32 @@
1
+ import * as Renderer from "@wardic/core/Renderer";
2
+ import * as Layer from "effect/Layer";
3
+ /** A single recorded `project` call. */
4
+ export interface ProjectCall {
5
+ readonly values: Record<string, unknown>;
6
+ }
7
+ /** A single recorded `commit` call. */
8
+ export interface CommitCall {
9
+ readonly target: unknown;
10
+ readonly patch: Renderer.Patch;
11
+ }
12
+ /**
13
+ * A Renderer that records every `project` and `commit` invocation into
14
+ * separate arrays instead of touching an environment — lets a test assert
15
+ * both the pure projection step and the imperative writes that would have
16
+ * been issued.
17
+ *
18
+ * Defaults:
19
+ * - `project` is identity (`(values) => values as Patch`).
20
+ * - `commit` pushes onto `commitCalls`.
21
+ */
22
+ export declare const make: () => {
23
+ readonly service: {
24
+ readonly project: (values: Record<string, unknown>) => Renderer.Patch;
25
+ readonly commit: (target: unknown, patch: Renderer.Patch) => void;
26
+ };
27
+ readonly projectCalls: ReadonlyArray<ProjectCall>;
28
+ readonly commitCalls: ReadonlyArray<CommitCall>;
29
+ };
30
+ /** The {@link DISCARDING} Renderer's Layer. */
31
+ export declare const layer: Layer.Layer<Renderer.Renderer>;
32
+ //# sourceMappingURL=renderer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderer.d.ts","sourceRoot":"","sources":["../src/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,uBAAuB,CAAA;AAEjD,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAErC,wCAAwC;AACxC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACzC;AAED,uCAAuC;AACvC,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;IACxB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAA;CAC/B;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,IAAI,QAAO;IACtB,QAAQ,CAAC,OAAO,EAAE;QAChB,QAAQ,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,QAAQ,CAAC,KAAK,CAAA;QACrE,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,KAAK,IAAI,CAAA;KAClE,CAAA;IACD,QAAQ,CAAC,YAAY,EAAE,aAAa,CAAC,WAAW,CAAC,CAAA;IACjD,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,UAAU,CAAC,CAAA;CAiBhD,CAAA;AAaD,+CAA+C;AAC/C,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAgD,CAAA"}
@@ -0,0 +1,43 @@
1
+ import * as Renderer from "@wardic/core/Renderer";
2
+ import { constVoid } from "effect/Function";
3
+ import * as Layer from "effect/Layer";
4
+ /**
5
+ * A Renderer that records every `project` and `commit` invocation into
6
+ * separate arrays instead of touching an environment — lets a test assert
7
+ * both the pure projection step and the imperative writes that would have
8
+ * been issued.
9
+ *
10
+ * Defaults:
11
+ * - `project` is identity (`(values) => values as Patch`).
12
+ * - `commit` pushes onto `commitCalls`.
13
+ */
14
+ export const make = () => {
15
+ const projectCalls = [];
16
+ const commitCalls = [];
17
+ return {
18
+ service: {
19
+ project: (values) => {
20
+ projectCalls.push({ values });
21
+ return values;
22
+ },
23
+ commit: (target, patch) => {
24
+ commitCalls.push({ target, patch });
25
+ },
26
+ },
27
+ projectCalls,
28
+ commitCalls,
29
+ };
30
+ };
31
+ /**
32
+ * A Renderer that draws nothing and keeps nothing: `project` hands its values back as the patch, and
33
+ * `commit` drops it. A test that reads the calls makes its own with {@link make}.
34
+ */
35
+ // #618 (decision 7): this was one recording renderer made at module load, whose calls every test in a
36
+ // file added to and nothing read.
37
+ const DISCARDING = {
38
+ project: (values) => values,
39
+ commit: constVoid,
40
+ };
41
+ /** The {@link DISCARDING} Renderer's Layer. */
42
+ export const layer = Layer.succeed(Renderer.Renderer, DISCARDING);
43
+ //# sourceMappingURL=renderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderer.js","sourceRoot":"","sources":["../src/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,uBAAuB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAarC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,GAOlB,EAAE;IACF,MAAM,YAAY,GAAuB,EAAE,CAAA;IAC3C,MAAM,WAAW,GAAsB,EAAE,CAAA;IACzC,OAAO;QACL,OAAO,EAAE;YACP,OAAO,EAAE,CAAC,MAA+B,EAAkB,EAAE;gBAC3D,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,CAAA;gBAC7B,OAAO,MAAwB,CAAA;YACjC,CAAC;YACD,MAAM,EAAE,CAAC,MAAe,EAAE,KAAqB,EAAQ,EAAE;gBACvD,WAAW,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAA;YACrC,CAAC;SACF;QACD,YAAY;QACZ,WAAW;KACZ,CAAA;AACH,CAAC,CAAA;AAED;;;GAGG;AACH,sGAAsG;AACtG,kCAAkC;AAClC,MAAM,UAAU,GAAiC;IAC/C,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,MAAwB;IAC7C,MAAM,EAAE,SAAS;CAClB,CAAA;AAED,+CAA+C;AAC/C,MAAM,CAAC,MAAM,KAAK,GAAmC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@wardic/test",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0-rc.10",
4
+ "description": "Headless testing Layers for Wardic: a clock that replays the frames you give it, a renderer that records what it's asked to draw, and input you push, so animation code runs without a browser.",
5
+ "keywords": [
6
+ "animation",
7
+ "testing",
8
+ "effect"
9
+ ],
10
+ "homepage": "https://wardic.dev",
11
+ "bugs": "https://github.com/wardiclabs/wardic/issues",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/wardiclabs/wardic.git",
15
+ "directory": "packages/test"
16
+ },
17
+ "license": "MIT",
18
+ "author": {
19
+ "name": "The Wardic contributors",
20
+ "email": "hello@wardic.dev",
21
+ "url": "https://wardic.dev"
22
+ },
23
+ "files": [
24
+ "dist/**/*.js",
25
+ "dist/**/*.d.ts",
26
+ "dist/**/*.map",
27
+ "src",
28
+ "README.md",
29
+ "LICENSE",
30
+ "THIRD_PARTY_NOTICES.md"
31
+ ],
32
+ "type": "module",
33
+ "sideEffects": false,
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "exports": {
38
+ "./package.json": "./package.json",
39
+ ".": {
40
+ "@wardic/source": "./src/index.ts",
41
+ "types": "./dist/index.d.ts",
42
+ "default": "./dist/index.js"
43
+ }
44
+ },
45
+ "scripts": {
46
+ "build": "../../node_modules/typescript-7/bin/tsc --build",
47
+ "typecheck": "../../node_modules/typescript-7/bin/tsc --build",
48
+ "typecheck:tests": "../../node_modules/typescript-7/bin/tsc -p tsconfig.test.json"
49
+ },
50
+ "dependencies": {
51
+ "@wardic/core": "1.0.0-rc.10"
52
+ },
53
+ "peerDependencies": {
54
+ "effect": "^4.1.1"
55
+ }
56
+ }
package/src/clock.ts ADDED
@@ -0,0 +1,136 @@
1
+ import * as Clock from "@wardic/core/Clock"
2
+ import * as RefreshFrameSource from "@wardic/core/RefreshFrameSource"
3
+ import * as Arr from "effect/Array"
4
+ import * as Effect from "effect/Effect"
5
+ import { constVoid } from "effect/Function"
6
+ import * as Layer from "effect/Layer"
7
+ import * as MutableRef from "effect/MutableRef"
8
+ import * as Predicate from "effect/Predicate"
9
+ import * as PubSub from "effect/PubSub"
10
+ import * as Stream from "effect/Stream"
11
+ import * as UndefinedOr from "effect/UndefinedOr"
12
+
13
+ /**
14
+ * A deterministic Clock for frame-level tests. `ticks` replays the supplied `dt`
15
+ * sequence (in seconds) exactly once, so a primitive's per-frame output can be
16
+ * collected with `Stream.runCollect` and asserted. Its `now` is the time those
17
+ * ticks have reached, in ms, from 0: the furthest any subscriber has
18
+ * reached, so two subscribers replaying the same frames don't run it twice as
19
+ * fast. A live Clock (rAF-driven) is `@wardic/dom`'s.
20
+ */
21
+ // `now`: #460.
22
+ export const make = (frames: ReadonlyArray<number> = []): Clock.Clock["Service"] => {
23
+ const time = MutableRef.make(0)
24
+ // Each frame with the time it reaches, in ms from 0 (#618, decision 7: each subscriber added its
25
+ // frames to one sum).
26
+ const reached = Arr.zip(frames, Arr.drop(Arr.scan(frames, 0, (at, dt) => at + dt * 1000), 1))
27
+ return {
28
+ ticks: Stream.map(Stream.fromIterable(reached), ([dt, at]) => {
29
+ MutableRef.update(time, (t) => Math.max(t, at))
30
+ return dt
31
+ }),
32
+ tick: (_t: number) => Effect.sync(() => {}),
33
+ now: () => MutableRef.get(time),
34
+ }
35
+ }
36
+
37
+ /** Clock Layer that emits `frames` (dt values, seconds) then completes. */
38
+ export const layer = (frames: ReadonlyArray<number> = []): Layer.Layer<Clock.Clock> =>
39
+ // A Clock per build: one Layer built twice shares no time (#618).
40
+ Layer.sync(Clock.Clock, () => make(frames))
41
+
42
+ /**
43
+ * Convenience: `n` frames of a fixed `dt` (default 1/60s) — e.g. `frames(30)`
44
+ * for half a second at 60fps.
45
+ */
46
+ export const frames = (count: number, dt = 1 / 60): ReadonlyArray<number> =>
47
+ Array.from({ length: count }, () => dt)
48
+
49
+ /**
50
+ * The Refresh frames `pushable`'s `advance` fires: a `RefreshFrameSource` that keeps the
51
+ * contract's order rules, as `requestAnimationFrame` does (ADR-0031, decision 4). `fire` runs the
52
+ * callbacks requested before it, in request order. One requested while they run waits for the
53
+ * next `fire`, and one cancelled before its turn never runs.
54
+ */
55
+ const makeFrames = () => {
56
+ const pending = new Map<number, (timestampMs: number) => void>()
57
+ const nextHandle = MutableRef.make(1)
58
+ const source: RefreshFrameSource.Shape = {
59
+ requestFrame: (callback) => {
60
+ const handle = MutableRef.getAndIncrement(nextHandle)
61
+ pending.set(handle, callback)
62
+ return handle
63
+ },
64
+ cancelFrame: (handle) => {
65
+ if (Predicate.isNumber(handle)) pending.delete(handle)
66
+ },
67
+ }
68
+ /** Run the callbacks requested so far at timestamp `t` (ms). */
69
+ const fire = (t: number): void =>
70
+ Arr.forEach(Arr.fromIterable(pending.keys()), (handle) =>
71
+ UndefinedOr.match(pending.get(handle), {
72
+ onUndefined: constVoid,
73
+ onDefined: (callback) => {
74
+ pending.delete(handle)
75
+ callback(t)
76
+ },
77
+ }))
78
+ return { source, fire }
79
+ }
80
+
81
+ /**
82
+ * A push-based Clock for tests that need to advance frames *on demand* —
83
+ * interleaved with pointer input — rather than replaying a fixed sequence. The
84
+ * static {@link layer} drains its whole `ticks` stream as fast as the consumer
85
+ * pulls, which makes it impossible to assert "advance a few frames, do
86
+ * something, advance more". This form lets the test `advance` one dt at a time.
87
+ *
88
+ * Backed by a `PubSub` (broadcast, no replay) so a consumer only sees ticks
89
+ * pushed *after* it subscribes — matching a live rAF clock where motion starts
90
+ * from the moment you subscribe. It has no Cold start: its ticks are the advances.
91
+ *
92
+ * Its `layer` also provides the Environment's `RefreshFrameSource`, and one `advance`
93
+ * is one Refresh frame in the web's order: it runs the frames requested so far (a flush gate's
94
+ * flush of a write made outside a tick), in request order, then publishes `dt` (the Clock's
95
+ * tick, at whose end a Frame loop over this Clock calls its `afterTick` hooks: the gate's
96
+ * flush). A frame requested during that `advance` waits for the next one. The frames'
97
+ * timestamps start at 0 and advance by `dt`, and the Clock's `now` is the last one's: a test's
98
+ * advances drive velocities too.
99
+ *
100
+ * It doesn't yield: a subscriber waiting on the ticks handles the tick inside the
101
+ * publish, as every subscriber does inside a real Refresh frame's callback, so a consumer that
102
+ * hops through Effect's scheduler on its way to its sink hasn't got there when `advance`
103
+ * returns, and its test sees it.
104
+ *
105
+ * @example
106
+ * const clock = yield* TestClock.pushable()
107
+ * // … fork a stream that consumes Clock.ticks, providing clock.layer …
108
+ * yield* clock.advance() // one 1/60s frame
109
+ * yield* clock.advance(1 / 30) // a custom dt
110
+ */
111
+ // The RefreshFrameSource: ADR-0031. The gate's flush at the tick's end: ADR-0033. `now`: #460. No
112
+ // yield: ADR-0032.
113
+ export const pushable: () => Effect.Effect<{
114
+ readonly layer: Layer.Layer<Clock.Clock | RefreshFrameSource.RefreshFrameSource>
115
+ readonly advance: (dt?: number) => Effect.Effect<void>
116
+ }> = Effect.fnUntraced(
117
+ function* () {
118
+ const pubsub = yield* PubSub.unbounded<number>()
119
+ const frames = makeFrames()
120
+ const time = MutableRef.make(0)
121
+ return {
122
+ layer: Layer.merge(
123
+ Layer.succeed(Clock.Clock, {
124
+ ticks: Stream.fromPubSub(pubsub),
125
+ tick: (_t: number) => Effect.sync(() => {}),
126
+ now: () => MutableRef.get(time),
127
+ }),
128
+ RefreshFrameSource.layer(frames.source),
129
+ ),
130
+ advance: (dt = 1 / 60) =>
131
+ Effect.sync(() => frames.fire(MutableRef.updateAndGet(time, (t) => t + dt * 1000))).pipe(
132
+ Effect.andThen(PubSub.publish(pubsub, dt)),
133
+ Effect.asVoid,
134
+ ),
135
+ }
136
+ })