@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/src/feeds.ts ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The Test Environment's **Input feeds** (#318): a script replayed to each subscriber, and a hub a
3
+ * test pushes into.
4
+ */
5
+ import type * as InputFeed from "@wardic/core/InputFeed"
6
+ import * as Arr from "effect/Array"
7
+ import * as Effect from "effect/Effect"
8
+ import { constVoid } from "effect/Function"
9
+
10
+ /**
11
+ * A fixed script as a feed: each subscriber hears every event as it subscribes, then the end. A
12
+ * Stream over it (`InputFeed.toStream`) emits the script and ends, as `Stream.fromIterable` did; an
13
+ * empty one ends at once. Not shared: each subscriber gets the whole script (tests address one
14
+ * surface, and a consumer may read it twice).
15
+ */
16
+ export const script =
17
+ <A>(events: ReadonlyArray<A>): InputFeed.InputFeed<A> =>
18
+ (emit, end) => {
19
+ Arr.forEach(events, emit)
20
+ end()
21
+ return constVoid
22
+ }
23
+
24
+ /** A live feed a test pushes into, and the push. */
25
+ export interface Hub<A> {
26
+ readonly feed: InputFeed.InputFeed<A>
27
+ readonly push: (event: A) => Effect.Effect<void>
28
+ }
29
+
30
+ /**
31
+ * A live feed: every subscriber hears each event pushed after it subscribed, in the order they
32
+ * subscribed. `push` doesn't yield (ADR-0032): a subscriber handles the event inside the push, as an
33
+ * Environment's consumers handle a real event inside its dispatch.
34
+ */
35
+ export const hub = <A>(): Hub<A> => {
36
+ let subscribers: ReadonlyArray<(event: A) => void> = []
37
+ return {
38
+ feed: (emit) => {
39
+ subscribers = Arr.append(subscribers, emit)
40
+ return () => {
41
+ subscribers = Arr.filter(subscribers, (s) => s !== emit)
42
+ }
43
+ },
44
+ push: (event) => Effect.sync(() => Arr.forEach(subscribers, (emit) => emit(event))),
45
+ }
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @wardic/test — headless testing Layers.
3
+ *
4
+ * Provide `TestLive` (or `testLayer(frames)`) in place of `@wardic/dom`'s
5
+ * `DomLive` to run animation code without a page. The Clock is deterministic
6
+ * (replays a `dt` sequence), so primitive output is collectable frame-by-frame.
7
+ * Nothing is drawn on a page: an app's components are tested with `DomLive`
8
+ * under the test runner's fake timers, as https://wardic.dev/guides/testing shows.
9
+ */
10
+ // The testing guide: #558.
11
+ export const VERSION = "1.0.0-rc.10"
12
+
13
+ export * as TestClock from "./clock.ts"
14
+ export * as TestRenderer from "./renderer.ts"
15
+ export * as TestProbe from "./probe.ts"
16
+ export * as TestInput from "./input.ts"
17
+ export { testLayer, TestLive } from "./layer.ts"
package/src/input.ts ADDED
@@ -0,0 +1,130 @@
1
+ import type * as InputFeed from "@wardic/core/InputFeed"
2
+ import * as InputSource from "@wardic/core/InputSource"
3
+ import * as Bool from "effect/Boolean"
4
+ import * as Effect from "effect/Effect"
5
+ import * as Predicate from "effect/Predicate"
6
+ import * as Layer from "effect/Layer"
7
+ import * as UndefinedOr from "effect/UndefinedOr"
8
+ import * as Option from "effect/Option"
9
+ import * as Feeds from "./feeds.ts"
10
+
11
+ /**
12
+ * An InputSource that replays fixed scripts of events, as **Input feeds** that end after their
13
+ * script: a Stream over one ends too. `pointer(target)` replays the same `pointerEvents`,
14
+ * `focus(target)` the same `focusEvents` and `keyboard(target)` the same `keyEvents` to each
15
+ * subscriber (each ignores `target` — tests address a single surface). The first positional arg is
16
+ * pointer events for back-compat; focus and key events are optional, defaulting to empty so existing
17
+ * call sites emit nothing on them. Each script plays whole when its feed is subscribed, so scripts of
18
+ * two kinds don't interleave: a test that needs them to pushes them ({@link pushable}). Called with no
19
+ * args it yields an InputSource that emits nothing — for tests that don't exercise gestures. It adopts
20
+ * no pointer: `adoptPointer` delivers nothing.
21
+ */
22
+ // Input feeds: #318. adoptPointer: #431.
23
+ export const layer = (
24
+ pointerEvents: ReadonlyArray<InputSource.PointerLikeEvent> = [],
25
+ focusEvents: ReadonlyArray<InputSource.FocusLikeEvent> = [],
26
+ keyEvents: ReadonlyArray<InputSource.KeyboardLikeEvent> = [],
27
+ ): Layer.Layer<InputSource.InputSource> =>
28
+ Layer.succeed(InputSource.InputSource, {
29
+ pointer: () => Feeds.script(pointerEvents),
30
+ keyboard: () => Feeds.script(keyEvents),
31
+ focus: () => Feeds.script(focusEvents),
32
+ adoptPointer: () => Option.none(),
33
+ })
34
+
35
+ /**
36
+ * A push-based InputSource for tests that need to interleave pointer events with
37
+ * other effects (e.g. advancing the Clock between a release and a re-grab). Unlike
38
+ * {@link layer} — which replays a fixed script that drains immediately — this
39
+ * form lets the test `push` events one at a time, on its own schedule.
40
+ *
41
+ * Backed by a hub (`feeds.ts`) so that every subscription to `pointer(target)`
42
+ * hears every event: a consumer may subscribe to `input.pointer(target)` more than once
43
+ * (a binding's drag track reads the drag fold and the grabs). There is no replay buffer,
44
+ * so only events pushed *after* a subscription is live reach it.
45
+ *
46
+ * `push` doesn't yield: a subscriber handles the event inside the push, as an
47
+ * Environment's consumers handle a real event inside its dispatch, so a pipeline that hops
48
+ * through Effect's scheduler on its way to its sink hasn't got there when `push` returns,
49
+ * and its test sees it.
50
+ *
51
+ * Returns `{ layer, push }`. Resolve it inside an `Effect.gen`, provide `layer`,
52
+ * and drive the surface with `push`:
53
+ *
54
+ * @example
55
+ * const input = yield* TestInput.pushable()
56
+ * yield* Stream.runForEach(drag(undefined), …).pipe(Effect.fork…) // subscribe
57
+ * yield* input.push(ev("down", 0, 0, 0))
58
+ *
59
+ * Returns `pushFocus` and `pushKey` alongside `push` so focus/blur transitions and keys can be
60
+ * driven on the same schedule; `focus(target)` and `keyboard(target)` mirror `pointer(target)`, each
61
+ * with its own hub.
62
+ *
63
+ * `adoptPointer(target, raw)` takes a pointer event as its `raw`: a pressed one is pushed on
64
+ * the pointer hub as an adopted down, inside the call, as the DOM's is. The hub holds no pointer, so
65
+ * the test pushes that pointer's later events itself.
66
+ *
67
+ * `options.focusWithin` is the source's `focusWithin`, for a test of a **Keyboard press** that
68
+ * holds its keys only while focus is within its target. Without it the source says nothing of focus,
69
+ * and a press hears every key pushed.
70
+ *
71
+ * `pushTo(target, event)` and `pushFocusTo(target, event)` feed `pointerAmong` and `focusAmong`, the feeds
72
+ * over many targets: each subscriber hears the event with the target its `memberOf` names `target`, or
73
+ * nothing when it names none. `push` and `pushFocus` reach neither, nor these the per-target feeds.
74
+ */
75
+ // The hub: #318. No yield: ADR-0032. Keys: #427. adoptPointer: #431. focusWithin: #460. The feeds over
76
+ // many targets: #665's PR 5.
77
+ export const pushable = (options?: { readonly focusWithin?: ((target: unknown) => boolean) | undefined }): Effect.Effect<{
78
+ readonly layer: Layer.Layer<InputSource.InputSource>
79
+ readonly push: (event: InputSource.PointerLikeEvent) => Effect.Effect<void>
80
+ readonly pushFocus: (event: InputSource.FocusLikeEvent) => Effect.Effect<void>
81
+ readonly pushKey: (event: InputSource.KeyboardLikeEvent) => Effect.Effect<void>
82
+ readonly pushTo: (target: unknown, event: InputSource.PointerLikeEvent) => Effect.Effect<void>
83
+ readonly pushFocusTo: (target: unknown, event: InputSource.FocusLikeEvent) => Effect.Effect<void>
84
+ }> =>
85
+ Effect.sync(() => {
86
+ const pointer = Feeds.hub<InputSource.PointerLikeEvent>()
87
+ const focus = Feeds.hub<InputSource.FocusLikeEvent>()
88
+ const keys = Feeds.hub<InputSource.KeyboardLikeEvent>()
89
+ const pointerAmong = Feeds.hub<InputSource.TargetEvent<InputSource.PointerLikeEvent>>()
90
+ const focusAmong = Feeds.hub<InputSource.TargetEvent<InputSource.FocusLikeEvent>>()
91
+ return {
92
+ layer: Layer.succeed(InputSource.InputSource, {
93
+ pointer: () => pointer.feed,
94
+ keyboard: () => keys.feed,
95
+ focus: () => focus.feed,
96
+ focusWithin: UndefinedOr.map(options, (o) => o.focusWithin),
97
+ adoptPointer: (_target, raw) => {
98
+ if (isPressedPointer(raw)) {
99
+ const down: InputSource.PointerLikeEvent = { ...raw, type: "down", adopted: true }
100
+ Effect.runSync(pointer.push(down))
101
+ return Option.some(down)
102
+ }
103
+ return Option.none()
104
+ },
105
+ pointerAmong: among(pointerAmong),
106
+ focusAmong: among(focusAmong),
107
+ }),
108
+ push: pointer.push,
109
+ pushFocus: focus.push,
110
+ pushKey: keys.push,
111
+ pushTo: (target, event) => pointerAmong.push({ target, event }),
112
+ pushFocusTo: (target, event) => focusAmong.push({ target, event }),
113
+ }
114
+ })
115
+
116
+ /**
117
+ * A feed over many targets on `hub`, for a subscriber's `memberOf`: each pushed event with the target
118
+ * `memberOf` names its pushed one, none when it names none.
119
+ */
120
+ const among =
121
+ <A>(hub: Feeds.Hub<InputSource.TargetEvent<A>>) =>
122
+ (memberOf: (node: unknown) => Option.Option<unknown>): InputFeed.InputFeed<InputSource.TargetEvent<A>> =>
123
+ (emit, end) =>
124
+ hub.feed((pushed) => {
125
+ Option.map(memberOf(pushed.target), (target) => emit({ target, event: pushed.event }))
126
+ }, end)
127
+
128
+ /** Whether `raw` is a pointer event with a button pressed: one the pushable InputSource adopts (#431). */
129
+ const isPressedPointer = (raw: unknown): raw is InputSource.PointerLikeEvent =>
130
+ Bool.and(Predicate.hasProperty(raw, "pointerId"), Predicate.hasProperty(raw, "pressed")) && (raw as InputSource.PointerLikeEvent).pressed
package/src/layer.ts ADDED
@@ -0,0 +1,80 @@
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 DragActivity from "@wardic/core/DragActivity"
5
+ import * as FrameLoop from "@wardic/core/FrameLoop"
6
+ import type * as InputSource from "@wardic/core/InputSource"
7
+ import * as InterpolatorRegistry from "@wardic/core/InterpolatorRegistry"
8
+ import * as PressClaims from "@wardic/core/PressClaims"
9
+ import type * as Probe from "@wardic/core/Probe"
10
+ import * as ReducedMotion from "@wardic/core/ReducedMotion"
11
+ import * as RefreshFrameSource from "@wardic/core/RefreshFrameSource"
12
+ import type * as Renderer from "@wardic/core/Renderer"
13
+ import type * as ScrollSource from "@wardic/core/ScrollSource"
14
+ import { constUndefined, constVoid } from "effect/Function"
15
+ import * as Layer from "effect/Layer"
16
+ import * as AtomRegistry from "effect/reactivity/AtomRegistry"
17
+ import * as TestClock from "./clock.ts"
18
+ import * as TestInput from "./input.ts"
19
+ import * as TestProbe from "./probe.ts"
20
+ import * as TestRenderer from "./renderer.ts"
21
+
22
+ /**
23
+ * The headless Environment's Refresh frame source: it never calls back, as its Clock never ticks
24
+ * on its own, so a flush gate built on it never flushes by itself. A test that fires frames
25
+ * provides `TestClock.pushable()`'s layer over it (ADR-0031, decision 6).
26
+ */
27
+ const stillFrames: RefreshFrameSource.Shape = { requestFrame: constUndefined, cancelFrame: constVoid }
28
+
29
+ /**
30
+ * The full headless environment: deterministic Clock (driven by `frames`), a Refresh frame
31
+ * source that never calls back, recording Renderer, static Probe and ScrollSource, silent
32
+ * InputSource, core's real interpolator registry, a fresh AtomRegistry for reading Probe atoms
33
+ * (`viewport`, `prefersReducedMotion`), and a fresh Drag activity, so one test's
34
+ * drag never blocks another's hover or press, and core's Frame loop over that
35
+ * Clock. Provide this in place of `DomLive` to test animation code without a
36
+ * page. It draws nothing on one: an element rendered under it keeps its Starting style, so an
37
+ * app's components are tested with `DomLive` under the test runner's fake timers instead
38
+ * (https://wardic.dev/guides/testing).
39
+ */
40
+ // The Drag activity: ADR-0019. The Frame loop: ADR-0027. The testing guide: #558.
41
+ export const testLayer = (
42
+ frames: ReadonlyArray<number> = [],
43
+ ): Layer.Layer<
44
+ | Clock.Clock
45
+ | RefreshFrameSource.RefreshFrameSource
46
+ | FrameLoop.FrameLoop
47
+ | Renderer.Renderer
48
+ | Probe.Probe
49
+ | ScrollSource.ScrollSource
50
+ | InputSource.InputSource
51
+ | InterpolatorRegistry.InterpolatorRegistry
52
+ | ReducedMotion.PositionalKeys
53
+ | DefaultTransition.DefaultTransition
54
+ | Announcer.Announcer
55
+ | AtomRegistry.AtomRegistry
56
+ > =>
57
+ FrameLoop.layer.pipe(
58
+ Layer.provideMerge(
59
+ Layer.mergeAll(
60
+ TestClock.layer(frames),
61
+ RefreshFrameSource.layer(stillFrames),
62
+ TestRenderer.layer,
63
+ TestProbe.layer(),
64
+ TestInput.layer(),
65
+ InterpolatorRegistry.layer,
66
+ // The transforms only: the Test Environment names no box keys (ADR-0036).
67
+ ReducedMotion.transformsOnly,
68
+ // No opinion on a key's Default transition: every key on one spring (ADR-0051).
69
+ DefaultTransition.layerOneSpring,
70
+ // No assistive technology to reach headless (ADR-0037).
71
+ Announcer.layerSilent,
72
+ AtomRegistry.layer,
73
+ Layer.sync(DragActivity.DragActivity, DragActivity.make),
74
+ Layer.sync(PressClaims.PressClaims, PressClaims.make),
75
+ ),
76
+ ),
77
+ )
78
+
79
+ /** `testLayer()` with an empty clock — for tests that drive their own frames. It never ticks on its own. */
80
+ export const TestLive = testLayer()
package/src/probe.ts ADDED
@@ -0,0 +1,94 @@
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.ts"
10
+
11
+ const ZERO_BOUNDS: Probe.Bounds = { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0 }
12
+ const VIEWPORT = { width: 1024, height: 768 }
13
+ /** A page that doesn't scroll: no range, and progress 1, as Motion reads a container with none. */
14
+ const STILL_SCROLL: ScrollSource.Scroll = { x: { current: 0, length: 0, progress: 1 }, y: { current: 0, length: 0, progress: 1 } }
15
+
16
+ /** A scripted in-view boolean as the Probe reports it, with no platform record. */
17
+ const toIntersection = (intersecting: boolean): Probe.Intersection => ({ intersecting })
18
+
19
+ /** Options for the static test Probe. */
20
+ // visible: #443 (`pageVisible` until #610). scroll: #234, a `ScrollSource` of its own since #518.
21
+ // intersection, an Input feed: #318.
22
+ export interface Options {
23
+ readonly bounds?: Probe.Bounds
24
+ readonly viewport?: { readonly width: number; readonly height: number }
25
+ readonly prefersReducedMotion?: boolean
26
+ /** Whether the Environment is shown: `true` when left out. */
27
+ readonly visible?: boolean
28
+ /** Every container's scroll, as the `ScrollSource` gives it: a page that doesn't scroll when left out. */
29
+ readonly scroll?: ScrollSource.Scroll
30
+ /**
31
+ * Script of intersection (in-view) booleans replayed by `observeIntersection`, an **Input feed**
32
+ * that ends after it. Mirrors the InputSource test harness: omitted (or empty) means it
33
+ * emits nothing and ends.
34
+ */
35
+ readonly intersection?: ReadonlyArray<boolean>
36
+ }
37
+
38
+ /**
39
+ * A static Probe: fixed bounds/viewport. `observeIntersection` replays the
40
+ * optional `intersection` script to each subscriber (empty by default, so it
41
+ * emits nothing); `observeResize` stays empty. With it, a `ScrollSource`
42
+ * whose every container reads `scroll`, as the Probe's `scroll` did until #518.
43
+ */
44
+ export const layer = (options: Options = {}): Layer.Layer<Probe.Probe | ScrollSource.ScrollSource> => {
45
+ const { intersection = [], scroll = STILL_SCROLL } = options
46
+ return Layer.mergeAll(
47
+ Layer.succeed(Probe.Probe, {
48
+ ...fixed(options),
49
+ observeIntersection: () => Feeds.script(Arr.map(intersection, toIntersection)),
50
+ }),
51
+ scrollLayer(Atom.make(scroll)),
52
+ )
53
+ }
54
+
55
+ /** Both Probes' fixed part: the bounds, the viewport, the preference and the page's visibility, each defaulted, and no resizes. */
56
+ const fixed = ({ bounds = ZERO_BOUNDS, viewport = VIEWPORT, prefersReducedMotion = false, visible = true }: Options) => ({
57
+ measure: () => Effect.sync(() => bounds),
58
+ viewport: Atom.make(viewport),
59
+ prefersReducedMotion: Atom.make(prefersReducedMotion),
60
+ visible: Atom.make(visible),
61
+ observeResize: () => Stream.empty,
62
+ })
63
+
64
+ /** One scroll Atom for every container, target and offset (#234). */
65
+ const scrollLayer = (scroll: Atom.Atom<ScrollSource.Scroll>): Layer.Layer<ScrollSource.ScrollSource> =>
66
+ Layer.succeed(ScrollSource.ScrollSource, { scroll: () => Effect.succeed(scroll) })
67
+
68
+ /**
69
+ * A push-based Probe for tests that need to interleave intersection
70
+ * (in-view/out-of-view) transitions with other effects on their own schedule —
71
+ * mirroring {@link TestInput.pushable}. `observeIntersection(target)` subscribes
72
+ * to a hub, so every subscriber sees the same transitions and only
73
+ * events pushed *after* a subscription is live reach it. `pushIntersection`
74
+ * doesn't yield: a subscriber handles the transition inside the push,
75
+ * as `TestInput.pushable`'s do. `pushScroll` sets the scroll every container reads,
76
+ * through the Environment's registry, as a scroll event would.
77
+ * measure/viewport/prefersReducedMotion/observeResize behave as in {@link layer}.
78
+ */
79
+ // The hub: #318. No yield: ADR-0032. Scroll: #234.
80
+ export const pushable = (options: Options = {}): Effect.Effect<{
81
+ readonly layer: Layer.Layer<Probe.Probe | ScrollSource.ScrollSource>
82
+ readonly pushIntersection: (inView: boolean) => Effect.Effect<void>
83
+ readonly pushScroll: (scroll: ScrollSource.Scroll) => Effect.Effect<void, never, AtomRegistry.AtomRegistry>
84
+ }> =>
85
+ Effect.sync(() => {
86
+ const intersections = Feeds.hub<Probe.Intersection>()
87
+ const { scroll = STILL_SCROLL } = options
88
+ const scrollAtom = Atom.make(scroll)
89
+ return {
90
+ layer: Layer.mergeAll(Layer.succeed(Probe.Probe, { ...fixed(options), observeIntersection: () => intersections.feed }), scrollLayer(scrollAtom)),
91
+ pushIntersection: (inView: boolean) => intersections.push(toIntersection(inView)),
92
+ pushScroll: (next: ScrollSource.Scroll) => AtomRegistry.AtomRegistry.use((registry) => Effect.sync(() => registry.set(scrollAtom, next))),
93
+ }
94
+ })
@@ -0,0 +1,63 @@
1
+ import * as Renderer from "@wardic/core/Renderer"
2
+ import { constVoid } from "effect/Function"
3
+ import * as Layer from "effect/Layer"
4
+
5
+ /** A single recorded `project` call. */
6
+ export interface ProjectCall {
7
+ readonly values: Record<string, unknown>
8
+ }
9
+
10
+ /** A single recorded `commit` call. */
11
+ export interface CommitCall {
12
+ readonly target: unknown
13
+ readonly patch: Renderer.Patch
14
+ }
15
+
16
+ /**
17
+ * A Renderer that records every `project` and `commit` invocation into
18
+ * separate arrays instead of touching an environment — lets a test assert
19
+ * both the pure projection step and the imperative writes that would have
20
+ * been issued.
21
+ *
22
+ * Defaults:
23
+ * - `project` is identity (`(values) => values as Patch`).
24
+ * - `commit` pushes onto `commitCalls`.
25
+ */
26
+ export const make = (): {
27
+ readonly service: {
28
+ readonly project: (values: Record<string, unknown>) => Renderer.Patch
29
+ readonly commit: (target: unknown, patch: Renderer.Patch) => void
30
+ }
31
+ readonly projectCalls: ReadonlyArray<ProjectCall>
32
+ readonly commitCalls: ReadonlyArray<CommitCall>
33
+ } => {
34
+ const projectCalls: Array<ProjectCall> = []
35
+ const commitCalls: Array<CommitCall> = []
36
+ return {
37
+ service: {
38
+ project: (values: Record<string, unknown>): Renderer.Patch => {
39
+ projectCalls.push({ values })
40
+ return values as Renderer.Patch
41
+ },
42
+ commit: (target: unknown, patch: Renderer.Patch): void => {
43
+ commitCalls.push({ target, patch })
44
+ },
45
+ },
46
+ projectCalls,
47
+ commitCalls,
48
+ }
49
+ }
50
+
51
+ /**
52
+ * A Renderer that draws nothing and keeps nothing: `project` hands its values back as the patch, and
53
+ * `commit` drops it. A test that reads the calls makes its own with {@link make}.
54
+ */
55
+ // #618 (decision 7): this was one recording renderer made at module load, whose calls every test in a
56
+ // file added to and nothing read.
57
+ const DISCARDING: Renderer.Renderer["Service"] = {
58
+ project: (values) => values as Renderer.Patch,
59
+ commit: constVoid,
60
+ }
61
+
62
+ /** The {@link DISCARDING} Renderer's Layer. */
63
+ export const layer: Layer.Layer<Renderer.Renderer> = Layer.succeed(Renderer.Renderer, DISCARDING)