UX refactor (#52)

* feat(ltl): bound fields on AlwaysFormula and named thunks

Add StepBound/Duration/Deadline to AlwaysFormula as the dual of bounded
Eventually, give ThunkFormula a Name for stable identity, add ThunkNamed,
and surface both in describe() and MarshalJSON.

* feat(ltl): negation normal form pass

nnf/pushNot rewrite a formula so every Not wraps only a Thunk or Error
leaf, dualizing Always<->Eventually and preserving bounds.

* feat(ltl): NNF in NewEvaluator, bounded-always, Finalize, collapse

Apply nnf on construction, reduce bounded Always symmetric to bounded
Eventually (vacuous holds once the window closes), add Finalize to
resolve undischarged liveness obligations to Violated at run end, and
collapse structurally-identical pending obligations.

* test(ltl): property-based NNF laws

Lock double-negation identity, Always/Eventually duality with bound
preservation, leaf pushdown, and not(always true) reaching Violated.

* test(ltl): Finalize, bounded eventually, latch, collapse

Property tests for monotonic violation latch and eventually-within
violating iff n consecutive false, plus Finalize and collapse cases.

* feat(inspect): within clause on always residual node

A negated bounded eventually serializes as a bounded always; render its
bound instead of dropping it.

* feat(ltl): witness violations and (bool,error) predicate thunks

* test(ltl): migrate thunk call sites to (bool,error)

* feat(ltl): flag thrown-predicate witnesses with IsError

* refactor(verifier): replace predicate err side-channel with violation witness

* test(verifier): witness API for thrown predicates

* feat(trace): witnesses map and skipped-verification marker on Step

* feat(runner): thread violation witnesses, finalize, skip marker into trace

* test(ltl): lock violation witness reason, IsError, and step

* test(verifier): finalize surfaces unmet eventually with witness

* fix(ltl): eliminate implies and bounded-always false-negatives

Rewrite a -> b to (not a) or b in NNF so a pending temporal antecedent
can no longer defer the whole implication and drop a consequent that was
false at the current step. Carry a pending inner past a bounded-Always
window close instead of dropping it to holds, so a deferred obligation is
resolved by a later step or Finalize.

* test(ltl): lock implies and bounded-always false-negative regressions

* fix(web-runtime): seed PRNG for reproducible runs and align weighted pick

* feat(testrun): inject seed into web bundle via SANDERLING_SEED define

* test: cover web-runtime seeded PRNG, weighted pick, and seed define wiring

* test(spec): add Go math/rand/v2 PCG oracle and golden fixture

* feat(spec): bit-exact PCG port of Go math/rand/v2

* test(spec): assert pcg.ts matches the PCG golden fixture

* feat(spec): shared input corpus and press-key pools

* feat(spec): action-tree types and Host interface

* feat(spec): verb support matrix and warn-once helper

* feat(spec): deterministic shared action picker

* test(spec): verb matrix and warn-once semantics

* test(spec): picker draw-order and determinism

* refactor(spec): actions.ts returns pure GeneratorNode data trees

* refactor(spec): wire from() sampling through the picker rng

* feat(spec): shared runtime-entry installs next-action over pick.ts

* feat(spec): export LongPress/Scroll/longPresses/scrolls factories

* test(spec): assert data-tree shapes for action factories

* test(spec): runtime-entry serializeAction wire-contract round-trip

* refactor(spec): bridge data-tree nodes to the legacy goja picker tags

* fix(spec): web runtime walks the spec's globalThis.actions data tree

* test(spec): tolerate legacy bridge fields on builtin nodes

* refactor(spec): installRuntime accepts a lazy root resolver

The web bundle imports the runtime before the spec, so the action root
on globalThis.actions only exists after the spec evaluates. Accept a
function form so the goja and web hosts resolve the root per tick.

* refactor(spec): web-runtime becomes the WEB Host, delegates to shared picker

Delete the duplicate picker (resolveGenerator/pickWeighted/randomTap/
randomInput/randomSwipe/randomPressKey/pickFromArray, the mulberry32 PRNG,
and the snake_case serializeAction) plus the __sanderling__ action factory
binds. web-runtime now implements Host (platform/seedHi/seedLo from the
injected 64-bit seed via BigInt, queryCandidates over the live DOM with a
per-tick cache, reportUnsupported) and calls installRuntime so both engines
run pick.ts over the same Pcg. Swipe/longPress/scroll follow the verbs.ts
matrix instead of silently returning null. Keeps the DOM helpers (selector
translation, queryElement, elementHandle, buildState, sanitize, extractors)
and the global locking. Net -214 lines (741 -> 527).

* test(spec): cover the WEB Host surface and seed precision

Replace the deleted-picker tests with Host coverage: platform()==web,
seedHi() parsing a 64-bit seed without Number precision loss, seedLo()==0,
reportUnsupported warning, the installed next-action/extractor globals, and
queryCandidates verb routing + per-tick caching over a querySelectorAll stub.

* refactor(spec): picker emits native selector + scroll endpoints, setup precedence

* feat(spec): goja runtime entry wires the shared picker over the Go host

* feat(bundler): optional RuntimeFile prepends a runtime-entry import via stdin

* feat(testrun): bundle the goja runtime entry so the verifier runs the shared picker

* refactor(spec): drop the legacy goja bridge fields from action factories

* feat(spec): serialize selector-only string targets for the runner to re-resolve

* refactor(verifier): one DecodeAction reads the unified flat wire contract

* refactor(verifier): goja host + shared picker replace the duplicate Go picker

* refactor(runner): decode V8 actions via the unified DecodeAction; wire goja runtime

* test(verifier): author specs through the shared picker path

* test(runner): bundle authored specs with the goja runtime entry

* feat(verifier): collect unsupported verbs for the run report

* refactor(runner): collapse WebDriver forks behind ActionSource/ExtractorSource

* feat(testrun): surface unsupported verbs in run report

* test(verifier): cross-runtime goja/node parity gate on the shared picker

* test(verifier): unsupported verbs collected deduped in first-seen order

* test(runner): summary reports no unsupported verbs on a clean run

* test(spec): golden-fixture cross-runtime parity gate for the node picker

Replace the env-driven parity harness with a shared scenario module and a
committed golden the node picker asserts independently. The goja side asserts
the same golden, so neither runtime invokes the other at test time.

* test(verifier): assert goja picker against the same cross-runtime golden

Drop the node-subprocess coupling: the goja side now installs a stub
__sanderlingHost__ with the fixed candidate list and asserts the committed
golden, matching pkg/spec/test/parity.test.ts.

* refactor(spec): rename pressKey generator export to pressKeys

* refactor(spec): update barrel re-exports for pressKeys

* test(spec): update pressKeys generator export name

* docs(spec): rename pressKey generator to pressKeys

* refactor(spec): extract samplerRng into shared sampler-rng module

* feat(spec): add fluent seeded value generators (strings/integers/emails/edgeCaseText)

* test(spec): cover fluent value generators determinism and chaining

* refactor(bundler): inject globalThis trailer from spec named exports

* refactor(bundler): reuse registration trailer in web bundler

* test(bundler): cover named-export globalThis registration

* feat(spec): add named() to Extracted handle type

* feat(web-runtime): named() and cross-extractor read guard

* feat(verifier): named() and cross-extractor read guard in goja

* test(verifier): cross-extractor read guard and named()

* test(web-runtime): export runtime and extractors for tests

* test(web-runtime): named() and cross-extractor read guard

* refactor(folio): drop manual globalThis trailer (bundler injects it)

* refactor(folio): seed txn amounts via integers().between(1,500)

* refactor(folio-web): drop manual globalThis trailer (bundler injects it)

* fix(folio-web): seed card/txn-type selection via from().generate() for reproducible runs

* refactor(folio-web): weight valid generators against edgeCaseText for names/amounts

* refactor(folio-web): name extractors so violation witnesses are readable

* fix(web-runtime): propagate extractor getter throws and unpoison locked global

Stop swallowing getter errors in evaluateExtractors so the cross-extractor read guard aborts loudly, matching goja's PushSnapshot. Make the __sanderling__ lock configurable (still non-writable) so a shared test process can reinstall a fake.

* test(spec): install fake runtime via defineProperty to survive locked global

* test(web-runtime): assert uncaught cross-extractor read aborts evaluateExtractors

* feat(runner): add MaxSteps bound to Options

* test(runner): MaxSteps stops after exactly N steps

* test(driverpb): drop proto getter round-trip tautology

* test(sidecar): drop stub-mode placeholder tautology tests

* test(mock): drop default-field-value assertion test

* test(ltl): drop Verdict.String tautology tests

* refactor(runner): extract RenderSummary for snapshot testing

* test(runner): golden snapshots for trace stream and violation summary

* feat(web-runtime): capture uncaught errors into state.exceptions

* test(integration): add throwing and counter web fixtures

* test(integration): add specs for the web fixtures

* test(integration): drive web fixtures through the real pipeline in headless Chrome

* chore(make): add test-browser target for the Chrome-driven suite

* ci: run the Chrome-driven browser suite in a separate job

* refactor(test): relocate browser suite to test/browser

* refactor(permissions): delete dead internal/permissions package

* refactor(test): rename package to browser_test

* refactor(sidecarassets): rename internal/sidecar to internal/sidecarassets

* chore(make): point test-browser at test/browser

* docs(decisions): record internal/permissions deletion

* refactor(doctor): use sidecarassets package

* refactor(testrun): use sidecarassets package

* fix(test): resolve testdata relative to browser_test.go

* refactor(verifier): remove dead __sanderlingIndex compat alias

* refactor(bundler): use encoding/json for JS string literals

* docs(action-space): use vendor-neutral native driver wording

* refactor(hierarchy): scrub backend tool name from comments

* refactor(driver): scrub backend tool name from comments

* refactor(driver): add DoubleTap and DoubleTapSelector to DeviceDriver

* refactor(sidecar): implement DoubleTap with the sub-100ms inter-tap gap

* refactor(chrome): implement DoubleTap as two taps with the gap

* refactor(mock): record DoubleTap and DoubleTapSelector actions

* refactor(runner): delegate double-tap to driver, drop gesture timing

* test(runner): assert double-tap delegates to driver DoubleTap

* docs(cmd): add package docs to CLI and developer tools

* docs(driver): add package docs to driver interface and chrome backend

* docs(driver): add package docs to mock and sidecar backends

* docs(platform): add package docs to android and ios device prep

* docs: add package docs to bundler and inspect

* docs(ltl): add package doc to temporal logic evaluator

* docs: add package docs to runner and testrun pipeline

* docs: add package docs to trace and verifier

* docs(sidecarassets): add package doc for embedded JAR loader

* fix(chrome): add disable-dev-shm-usage so Chrome starts in CI

* test(chrome): gate real-Chrome driver tests behind the browser tag

* chore(make): run chrome driver tests in the browser job

* fix(web-runtime): guard global error listeners for non-browser hosts

The module registered window error/unhandledrejection listeners at top
level, which threw under Node (the spec-api test runner) where
globalThis.addEventListener is absent. Register only when the API exists;
the real browser run is unaffected.

* ci(browser): re-enable unprivileged user namespaces for headless Chrome

ubuntu-latest moved to 24.04, whose AppArmor restriction on unprivileged
user namespaces stops headless Chrome from opening its DevTools socket
even with --no-sandbox, surfacing as the driver's 'websocket url timeout'.
Relax the sysctl for the job and add a direct launch check so a future
breakage shows Chrome's own stderr rather than an opaque driver timeout.

* ci(browser): pin stable Chrome for the driver tests

setup-chrome's default latest pulled a dev Chromium (150) whose remote
debugging socket never came up under chromedp, while plain --dump-dom
worked. Pin the stable channel, which the driver is tested against.

* feat(defaults): add scroll and rebalance action weights

Use relative-integer weights (taps/typing co-primary 100, scrolls 50,
swipes 25, doubleTaps 10); the picker normalizes by their total. Adds
scrolls to defaultActions as a first-class reveal behavior.

* feat(defaults): trim scroll action weight wiring

* fix(build): point sidecar jar ignore and embed paths at sidecarassets

* test(defaults): drop stale longPresses re-export assertion

longPresses is opt-in vocabulary, no longer re-exported from
defaults/actions.ts since e0d3b20; its builtin resolution is already
covered by api.test.ts. Trim the defaults test to scrolls, which is an
actual default export.

* fix(chrome): raise DevTools websocket read timeout to 60s

Chrome cold-start on a loaded CI runner can exceed chromedp's 20s
default for reading the DevTools websocket URL, flaking the browser
tests with "websocket url timeout reached". Give launch more headroom.
This commit is contained in:
pj authored and GitHub committed 2026-06-02 09:52:53 +05:30
1 parent 88db9653e5
commit c5bb176be8
98 files changed
+6898 -2227

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
// Shared action-generator tree and host interface for W2 approach B.
//
// Both engines (the goja verifier and the V8 web runtime) walk ONE tree with
// ONE picker (pick.ts), drawing through the shared PCG (pcg.ts). This module
// only declares the data shapes; pick.ts holds the traversal and the parity
// contract for draw order.
//
// Element references never cross the V8/host boundary: builtin generators emit
// an ActionDescriptor whose target is already a resolved Point (plus an
// optional selector on native), so the host serializer never has to chase a
// live element handle.
import type {
AccessibilityElement,
AttrSelector,
Direction,
Point,
SelectorPath,
} from "./types.ts";
// Target is any target shape an action may carry before serialization. Author
// specs supply a string/selector/element; builtin generators resolve to a
// {x, y} Point (so no element handle crosses the V8/host boundary).
export type Target = string | AttrSelector | SelectorPath | AccessibilityElement | Point;
// BuiltinVerb names a leaf generator backed by host-enumerated candidates
// rather than author-supplied actions.
export type BuiltinVerb =
| "taps"
| "doubleTaps"
| "longPresses"
| "scrolls"
| "typing"
| "swipes"
| "waitOnce"
| "pressKeys";
// ActionDescriptor is the camelCase author shape an action takes before the
// engine serializes it to the wire format. Mirrors the factory return shapes
// in actions.ts (Tap/DoubleTap/LongPress {on}; Scroll {direction; in?};
// InputText {into; text}; Swipe {from; to; durationMillis?}; PressKey {key};
// Wait {durationMillis}).
export type ActionDescriptor =
| { kind: "Tap"; on: Target }
| { kind: "DoubleTap"; on: Target }
| { kind: "LongPress"; on: Target }
// Scroll carries the author target (in) for spec-written scrolls; the builtin
// generator additionally pre-computes the swipe from/to so the wire contract
// carries a real gesture rather than a zero-length one.
| { kind: "Scroll"; direction: Direction; in?: Target; from?: Target; to?: Target }
| { kind: "InputText"; into: Target; text: string }
| { kind: "Swipe"; from: Target; to: Target; durationMillis?: number }
| { kind: "PressKey"; key: string }
| { kind: "Wait"; durationMillis: number };
// GeneratorNode is a node in the action-generator tree.
// weighted: probabilistic choice over child nodes, scanned ascending.
// actions: author callback returning a list to pick uniformly from.
// builtin: host-backed leaf identified by a verb.
export type GeneratorNode =
| { kind: "weighted"; branches: ReadonlyArray<readonly [number, GeneratorNode]> }
| { kind: "actions"; generate: () => ActionDescriptor[] }
| { kind: "builtin"; verb: BuiltinVerb };
// Candidate is one host-enumerated target for a builtin verb. The host
// resolves geometry (and a native selector) so no element handle crosses into
// the picker. width/height let swipe/scroll size a gesture off the element.
export interface Candidate {
x: number;
y: number;
selector?: string;
width?: number;
height?: number;
}
// Host is the platform backing the picker draws against. In this foundation
// workflow only the interface is defined and exercised against a stub; the
// goja and DOM implementations land in the rewire workflow.
export interface Host {
platform(): "android" | "ios" | "web";
// queryCandidates returns the host-enumerated targets for a verb, in a
// deterministic order. The picker indexes into this list with the PCG, so
// the order is part of the parity contract.
queryCandidates(verb: BuiltinVerb): Candidate[];
// reportUnsupported is invoked at most once per verb@platform (see verbs.ts)
// when a verb has no support on this platform.
reportUnsupported(verb: BuiltinVerb): void;
seedHi(): bigint;
seedLo(): bigint;
}
+48 -45
View File
@@ -1,7 +1,12 @@
// Author-facing action factories. Each returns plain GeneratorNode /
// ActionDescriptor DATA (see action-tree.ts); the shared picker (pick.ts) walks
// the tree and the runtime entry (runtime-entry.ts) serializes the result. No
// factory forwards to globalThis.__sanderling__ anymore: the same data tree
// drives both the goja verifier and the V8 web runtime.
import type {
AccessibilityElement,
Action,
ActionGenerator,
Direction,
DoubleTapAction,
InputTextAction,
@@ -16,16 +21,24 @@ import type {
WaitAction,
WeightedEntry,
} from "./types.ts";
import type { ActionDescriptor, BuiltinVerb, GeneratorNode } from "./action-tree.ts";
import { getSamplerRng } from "./sampler-rng.ts";
export function actions(generator: () => Action[]): ActionGenerator {
return globalThis.__sanderling__.actions(generator);
export { setSamplerRng } from "./sampler-rng.ts";
function builtinNode(verb: BuiltinVerb): GeneratorNode {
return { kind: "builtin", verb };
}
export function actions(generator: () => Action[]): GeneratorNode {
return { kind: "actions", generate: generator as () => ActionDescriptor[] };
}
export function whenRoute(
routeExtractor: { readonly current: string | null },
routes: string | readonly string[],
body: () => Action[],
): ActionGenerator {
): GeneratorNode {
const allowed = typeof routes === "string" ? [routes] : routes;
return actions(() => {
const current = routeExtractor.current;
@@ -34,38 +47,45 @@ export function whenRoute(
});
}
export function weighted(...entries: WeightedEntry[]): ActionGenerator {
return globalThis.__sanderling__.weighted(...entries);
export function weighted(...entries: WeightedEntry[]): GeneratorNode {
return { kind: "weighted", branches: entries };
}
export function from<T>(items: readonly T[]): Sampler<T> {
return globalThis.__sanderling__.from(items);
return {
generate(): T {
if (items.length <= 1) return items[0] as T;
const rng = getSamplerRng();
const index = rng ? rng.intN(items.length) : 0;
return items[index] as T;
},
};
}
export function Tap(parameters: { on: string | AccessibilityElement }): TapAction {
return globalThis.__sanderling__.tap(parameters);
return { kind: "Tap", on: parameters.on };
}
export function DoubleTap(parameters: { on: string | AccessibilityElement }): DoubleTapAction {
return globalThis.__sanderling__.doubleTap(parameters);
return { kind: "DoubleTap", on: parameters.on };
}
export function LongPress(parameters: { on: string | AccessibilityElement }): LongPressAction {
return globalThis.__sanderling__.longPress(parameters);
return { kind: "LongPress", on: parameters.on };
}
export function Scroll(parameters: {
direction: Direction;
in?: string | AccessibilityElement;
}): ScrollAction {
return globalThis.__sanderling__.scroll(parameters);
return { kind: "Scroll", direction: parameters.direction, in: parameters.in };
}
export function InputText(parameters: {
into: string | AccessibilityElement;
text: string;
}): InputTextAction {
return globalThis.__sanderling__.inputText(parameters);
return { kind: "InputText", into: parameters.into, text: parameters.text };
}
export function Swipe(parameters: {
@@ -73,44 +93,27 @@ export function Swipe(parameters: {
to: Point | AccessibilityElement;
durationMillis?: number;
}): SwipeAction {
return globalThis.__sanderling__.swipe(parameters);
return {
kind: "Swipe",
from: parameters.from,
to: parameters.to,
durationMillis: parameters.durationMillis,
};
}
export function PressKey(parameters: { key: Key }): PressKeyAction {
return globalThis.__sanderling__.pressKey(parameters);
return { kind: "PressKey", key: parameters.key };
}
export function Wait(parameters: { durationMillis: number }): WaitAction {
return globalThis.__sanderling__.wait(parameters);
return { kind: "Wait", durationMillis: parameters.durationMillis };
}
function builtinGenerator(
name:
| "taps"
| "doubleTaps"
| "longPresses"
| "scrolls"
| "typing"
| "swipes"
| "waitOnce"
| "pressKeys",
): ActionGenerator {
return new Proxy({} as ActionGenerator, {
get(_target, property) {
const runtime = globalThis.__sanderling__[name] as unknown as Record<
string | symbol,
unknown
>;
return runtime[property];
},
});
}
export const taps: ActionGenerator = builtinGenerator("taps");
export const doubleTaps: ActionGenerator = builtinGenerator("doubleTaps");
export const longPresses: ActionGenerator = builtinGenerator("longPresses");
export const scrolls: ActionGenerator = builtinGenerator("scrolls");
export const typing: ActionGenerator = builtinGenerator("typing");
export const swipes: ActionGenerator = builtinGenerator("swipes");
export const waitOnce: ActionGenerator = builtinGenerator("waitOnce");
export const pressKey: ActionGenerator = builtinGenerator("pressKeys");
export const taps: GeneratorNode = builtinNode("taps");
export const doubleTaps: GeneratorNode = builtinNode("doubleTaps");
export const longPresses: GeneratorNode = builtinNode("longPresses");
export const scrolls: GeneratorNode = builtinNode("scrolls");
export const typing: GeneratorNode = builtinNode("typing");
export const swipes: GeneratorNode = builtinNode("swipes");
export const waitOnce: GeneratorNode = builtinNode("waitOnce");
export const pressKeys: GeneratorNode = builtinNode("pressKeys");
+47
View File
@@ -0,0 +1,47 @@
// Single source of truth for the data both action-generator engines draw from:
// the goja verifier (internal/verifier/worker.go) and the V8 web runtime
// (web-runtime.ts). The entries and their ORDER are load-bearing: both engines
// index into these arrays with the shared PCG, so any reordering shifts every
// downstream pick for a given seed and breaks cross-engine reproducibility.
// INPUT_CORPUS is the edge-case string pool the typing builtin draws from to
// stress field parsing: empty, single char, overflow length, unicode, blank /
// whitespace, numeric boundaries, and common injection payloads. It must stay
// byte-for-byte identical to worker.go inputCorpus.
export const INPUT_CORPUS: readonly string[] = [
"",
"a",
"a".repeat(4096),
"🙂🔥💸",
" ",
"\t\n",
"-1",
"999999999999999999999",
"0.0000001",
"1e10",
"'; DROP TABLE--",
"<script>alert(1)</script>",
"../../etc/passwd",
"%s%n",
"NaN",
];
// NATIVE_PRESS_KEYS is the key pool the native (goja) press-key builtin may
// emit. Exploration stays gentle: only "back" is in play today, because
// "home"/"menu" navigate away from the app under test. Kept as an array so the
// matrix can declare native support over the full set without the picker
// hardcoding a single value.
export const NATIVE_PRESS_KEYS: readonly string[] = ["back"];
// WEB_PRESS_KEYS is the key pool the web press-key builtin draws from. Only
// keys with meaningful in-page semantics are included; "back"/"home" would not
// navigate a browser tab the way they navigate a native app.
export const WEB_PRESS_KEYS: readonly string[] = [
"enter",
"tab",
"escape",
"up",
"down",
"left",
"right",
];
+7 -19
View File
@@ -1,24 +1,12 @@
import { doubleTaps, swipes, taps, typing, weighted } from "../actions.ts";
import { doubleTaps, scrolls, swipes, taps, typing, weighted } from "../actions.ts";
import type { ActionGenerator } from "../types.ts";
export { doubleTaps, pressKey, swipes, taps, typing, waitOnce } from "../actions.ts";
// longPresses and scrolls are opt-in generators: not part of defaultActions,
// but authors can include them in their own weighted() set.
export { longPresses, scrolls } from "../actions.ts";
export { doubleTaps, scrolls, swipes, taps, typing } from "../actions.ts";
// A broad exploration generator: tap things, type edge-case values into fields,
// and swipe. Layer it under targeted depth flows so the fuzzer wanders the whole
// app while still driving the paths an author wrote. It deliberately omits the
// hardware back key: backing out past the app's root screen exits the app under
// test, and exploration must stay within the app.
//
// doubleTaps fires the same target twice ~50ms apart inside one step. Real users
// double-tap (image zoom, like-to-favorite, play/pause); without this the fuzzer
// can never produce sub-100ms event spacing, so race-window bugs (debounce gaps,
// in-flight guards, init races) stay unreachable.
export const defaultActions: ActionGenerator = weighted(
[45, taps],
[15, doubleTaps],
[25, typing],
[15, swipes],
[100, taps],
[100, typing],
[50, scrolls],
[25, swipes],
[10, doubleTaps],
);
+23
View File
@@ -0,0 +1,23 @@
// Goja-side runtime entry for the native verifier (internal/verifier).
//
// Go installs globalThis.__sanderlingHost__ (platform/seed/queryCandidates/
// reportUnsupported, implemented over the hierarchy tree in bindings.go) before
// the spec evaluates. This module bundles AFTER the spec, reads that host, and
// wires the shared picker via installRuntime so the goja verifier and the V8 web
// runtime run the SAME pick.ts over the SAME Pcg.
//
// Extractors are driven by Go (bindExtract + PushSnapshot advance current/
// previous against the Go-built `state`), so the extractor callback is a no-op
// here; Go never invokes __sanderlingExtractors__ on native.
import { installRuntime } from "./runtime-entry.ts";
import type { GeneratorNode, Host } from "./action-tree.ts";
const host = (globalThis as { __sanderlingHost__?: Host }).__sanderlingHost__;
if (!host) throw new Error("goja runtime: __sanderlingHost__ not installed");
installRuntime(
host,
() => (globalThis as { actions?: GeneratorNode }).actions ?? null,
() => ({}),
);
+6 -1
View File
@@ -33,14 +33,18 @@ export { always, eventually, next, now } from "./ltl.ts";
export {
DoubleTap,
InputText,
LongPress,
PressKey,
Scroll,
Swipe,
Tap,
Wait,
actions,
doubleTaps,
from,
pressKey,
longPresses,
pressKeys,
scrolls,
swipes,
taps,
typing,
@@ -48,3 +52,4 @@ export {
weighted,
whenRoute,
} from "./actions.ts";
export { edgeCaseText, emails, integers, strings } from "./values.ts";
+96
View File
@@ -0,0 +1,96 @@
// Bit-exact port of Go's math/rand/v2 PCG source wrapped in rand.Rand.
//
// This MUST match `rand.New(rand.NewPCG(hi, lo))` draw-for-draw, because every
// random decision the action picker makes is shared between the goja verifier
// and the V8 web runtime. Divergence here breaks reproducibility across engines.
//
// The golden fixture in test/fixtures/pcg-golden.json is generated by
// internal/_oracle/main.go and pins the exact sequences asserted in pcg.test.ts.
const MASK64 = (1n << 64n) - 1n;
const MUL_HI = 2549297995355413924n;
const MUL_LO = 4865540595714422341n;
const INC_HI = 6364136223846793005n;
const INC_LO = 1442695040888963407n;
const CHEAP_MUL = 0xda942042e4dd58b5n;
const POW53 = 1n << 53n;
// mul64 returns the 128-bit product of two uint64 values split into high and
// low 64-bit halves, matching Go's math/bits.Mul64.
function mul64(a: bigint, b: bigint): { hi: bigint; lo: bigint } {
const product = a * b;
return { hi: (product >> 64n) & MASK64, lo: product & MASK64 };
}
export class Pcg {
private hi: bigint;
private lo: bigint;
constructor(hi: bigint, lo: bigint) {
this.hi = hi & MASK64;
this.lo = lo & MASK64;
}
// next advances the 128-bit LCG state and returns the new (hi, lo) pair.
private next(): { hi: bigint; lo: bigint } {
// 128-bit multiply of state by the 128-bit multiplier, keeping 128 bits.
let { hi, lo } = mul64(this.lo, MUL_LO);
hi = (hi + this.hi * MUL_LO + this.lo * MUL_HI) & MASK64;
// 128-bit add of the increment with carry propagation.
const loSum = lo + INC_LO;
const carry = loSum >> 64n;
lo = loSum & MASK64;
hi = (hi + INC_HI + carry) & MASK64;
this.lo = lo;
this.hi = hi;
return { hi, lo };
}
// uint64 mirrors PCG.Uint64: the DXSM output permutation over next().
uint64(): bigint {
let { hi, lo } = this.next();
hi ^= hi >> 32n;
hi = (hi * CHEAP_MUL) & MASK64;
hi ^= hi >> 48n;
hi = (hi * (lo | 1n)) & MASK64;
return hi & MASK64;
}
// float64 mirrors rand.Rand.Float64: float64(Uint64()<<11>>11) / (1<<53),
// which keeps the low 53 bits and divides by 2^53.
float64(): number {
const bits = this.uint64() & (POW53 - 1n);
return Number(bits) / Number(POW53);
}
// intN mirrors rand.Rand.IntN via uint64n (Lemire with a rejection threshold).
// The host arch is 64-bit, so the 32-bit fast path never applies.
intN(n: number | bigint): number {
const bound = typeof n === "bigint" ? n : BigInt(n);
if (bound <= 0n) {
throw new Error("invalid argument to intN");
}
return Number(this.uint64n(bound));
}
private uint64n(n: bigint): bigint {
if ((n & (n - 1n)) === 0n) {
// n is a power of two: mask the low bits.
return this.uint64() & (n - 1n);
}
let { hi, lo } = mul64(this.uint64(), n);
if (lo < n) {
// Go computes thresh := -n % n in uint64 math, where -n wraps to 2^64 - n.
const threshold = ((1n << 64n) - n) % n;
while (lo < threshold) {
({ hi, lo } = mul64(this.uint64(), n));
}
}
return hi;
}
}
+248
View File
@@ -0,0 +1,248 @@
// The shared deterministic action picker for W2 approach B.
//
// walk() traverses a GeneratorNode tree, and nextAction() wraps it with the
// 16-attempt retry that matches worker.go NextAction. Both engines (the goja
// verifier and the V8 web runtime) run THIS code, drawing through the shared
// Pcg, so a given seed yields an identical action stream on every platform.
//
// PARITY CONTRACT - draw order.
// Every random decision goes through the Pcg in a FIXED, pinned order. Changing
// this order shifts the stream for a seed and breaks cross-engine
// reproducibility, so treat it as load-bearing:
//
// weighted node: ONE float64() draw, then an ASCENDING cumulative scan over
// max(0, weight). (matches worker.go pickWeighted.)
// actions node: the generator runs FIRST (any from(...).generate() inside
// draws intN(itemCount) for >1 items, nothing otherwise),
// THEN if the returned list has >1 entry, ONE intN(len) draw;
// a 0- or 1-element list draws nothing. (pickFromResult.)
// builtin node, per verb, in this exact sequence:
// taps/doubleTaps/longPresses: intN(candidateCount) [1 draw]
// typing: intN(candidateCount), intN(corpusLength)
// swipes: intN(candidateCount),
// 200 + intN(401) magnitude, intN(4) direction
// scrolls: intN(candidateCount), intN(4) direction
// pressKeys: intN(keyCount)
// waitOnce: no draw
//
// Builtin targets are resolved to a {x, y} Point by the host BEFORE the picker
// sees them, so no element handle crosses into this module.
import type { Pcg } from "./pcg.ts";
import type {
ActionDescriptor,
BuiltinVerb,
GeneratorNode,
Host,
} from "./action-tree.ts";
import type { Direction, Point } from "./types.ts";
import { INPUT_CORPUS, NATIVE_PRESS_KEYS, WEB_PRESS_KEYS } from "./corpus.ts";
import { setSamplerRng } from "./sampler-rng.ts";
import { supports, warnUnsupportedOnce } from "./verbs.ts";
// SWIPE_MIN_MAGNITUDE / SWIPE_MAGNITUDE_SPAN reproduce worker.go's
// `200 + rng.IntN(401)` swipe distance in pixels (200..600 inclusive).
const SWIPE_MIN_MAGNITUDE = 200;
const SWIPE_MAGNITUDE_SPAN = 401;
const SWIPE_DURATION_MILLIS = 250;
const DIRECTIONS: readonly Direction[] = ["up", "down", "left", "right"];
const MAX_RETRIES = 16;
// nextAction resolves an action for the current step, retrying walk() up to 16
// times when it yields null (matches worker.go NextAction). Returns null when
// every attempt comes up empty.
export function nextAction(
root: GeneratorNode,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
const action = walk(root, rng, host);
if (action !== null) return action;
}
return null;
}
// walk resolves a single node to an ActionDescriptor or null.
export function walk(
node: GeneratorNode,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
switch (node.kind) {
case "weighted":
return walkWeighted(node.branches, rng, host);
case "actions": {
// Expose the picker's rng to from(...).generate() calls inside the
// generator so author sampling shares this single deterministic stream.
setSamplerRng(rng);
try {
return walkActions(node.generate(), rng);
} finally {
setSamplerRng(null);
}
}
case "builtin":
return walkBuiltin(node.verb, rng, host);
}
}
function walkWeighted(
branches: ReadonlyArray<readonly [number, GeneratorNode]>,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
let total = 0;
for (const [weight] of branches) total += Math.max(0, weight);
if (total <= 0) return null;
const draw = rng.float64() * total;
let cumulative = 0;
for (const [weight, child] of branches) {
cumulative += Math.max(0, weight);
if (draw < cumulative) return walk(child, rng, host);
}
// Floating-point slack: draw can equal total. Fall to the last branch,
// matching worker.go's trailing `return generators[length-1]`.
const last = branches[branches.length - 1];
return last ? walk(last[1], rng, host) : null;
}
function walkActions(
generated: ActionDescriptor[],
rng: Pcg,
): ActionDescriptor | null {
if (generated.length === 0) return null;
if (generated.length === 1) return generated[0] ?? null;
return generated[rng.intN(generated.length)] ?? null;
}
function walkBuiltin(
verb: BuiltinVerb,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
if (!supports(verb, host.platform())) {
warnUnsupportedOnce(host, verb);
return null;
}
if (verb === "waitOnce") {
return { kind: "Wait", durationMillis: 500 };
}
if (verb === "pressKeys") {
return walkPressKey(rng, host);
}
const candidates = host.queryCandidates(verb);
if (candidates.length === 0) return null;
const picked = candidates[rng.intN(candidates.length)];
if (!picked) return null;
const point: Point = { x: picked.x, y: picked.y };
switch (verb) {
case "taps":
return tapDescriptor("Tap", point, picked.selector);
case "doubleTaps":
return tapDescriptor("DoubleTap", point, picked.selector);
case "longPresses":
return tapDescriptor("LongPress", point, picked.selector);
case "typing": {
const text = INPUT_CORPUS[rng.intN(INPUT_CORPUS.length)] ?? "";
return { kind: "InputText", into: withSelector(point, picked.selector), text };
}
case "swipes":
return buildSwipe(point, rng);
case "scrolls": {
const direction = DIRECTIONS[rng.intN(DIRECTIONS.length)] ?? "down";
return buildScroll(point, direction, picked, rng);
}
}
}
// withSelector attaches a native selector to a resolved Point so the runner can
// re-resolve the target by id/text. The web host omits it (point-only).
function withSelector(point: Point, selector?: string): Point {
if (selector === undefined) return point;
return { ...point, selector } as Point;
}
function tapDescriptor(
kind: "Tap" | "DoubleTap" | "LongPress",
point: Point,
selector?: string,
): ActionDescriptor {
return { kind, on: withSelector(point, selector) } as ActionDescriptor;
}
// buildScroll lowers a scroll to a swipe over the container, matching
// worker.go's geometry: the gesture drags opposite the named content motion,
// magnitude 40% of the container extent. Missing width/height (web root) yields
// a zero-length endpoint, which the runner re-derives from container bounds.
function buildScroll(
from: Point,
direction: Direction,
candidate: { width?: number; height?: number },
_rng: Pcg,
): ActionDescriptor {
const width = candidate.width ?? 0;
const height = candidate.height ?? 0;
let toX = from.x;
let toY = from.y;
switch (direction) {
case "down":
toY = from.y - Math.trunc((4 * height) / 10);
break;
case "up":
toY = from.y + Math.trunc((4 * height) / 10);
break;
case "left":
toX = from.x + Math.trunc((4 * width) / 10);
break;
case "right":
toX = from.x - Math.trunc((4 * width) / 10);
break;
}
return {
kind: "Scroll",
direction,
in: from,
from,
to: { x: Math.max(0, toX), y: Math.max(0, toY) },
} as ActionDescriptor;
}
function walkPressKey(rng: Pcg, host: Host): ActionDescriptor | null {
const keys = host.platform() === "web" ? WEB_PRESS_KEYS : NATIVE_PRESS_KEYS;
if (keys.length === 0) return null;
const key = keys[rng.intN(keys.length)] ?? keys[0];
if (key === undefined) return null;
return { kind: "PressKey", key };
}
function buildSwipe(from: Point, rng: Pcg): ActionDescriptor {
const magnitude = SWIPE_MIN_MAGNITUDE + rng.intN(SWIPE_MAGNITUDE_SPAN);
let toX = from.x;
let toY = from.y;
switch (rng.intN(4)) {
case 0:
toY = from.y - magnitude;
break;
case 1:
toY = from.y + magnitude;
break;
case 2:
toX = from.x - magnitude;
break;
case 3:
toX = from.x + magnitude;
break;
}
return {
kind: "Swipe",
from,
to: { x: Math.max(0, toX), y: Math.max(0, toY) },
durationMillis: SWIPE_DURATION_MILLIS,
};
}
+155
View File
@@ -0,0 +1,155 @@
// The single next-action entry shared by both engines (goja verifier and V8 web
// runtime). installRuntime wires the spec's root generator, the shared Pcg, and
// a Host into the globals the host invokes each tick:
// __sanderlingNextAction__() -> one serialized action (unified wire contract)
// __sanderlingExtractors__() -> the engine's extractor snapshot
//
// Both engines call THIS picker over the SAME Pcg, so a given seed yields an
// identical action stream by construction.
import { Pcg } from "./pcg.ts";
import { nextAction, walk } from "./pick.ts";
import type { ActionDescriptor, GeneratorNode, Host } from "./action-tree.ts";
import type { Point } from "./types.ts";
// SerializedAction is the flat, camelCase wire shape JS emits and Go decodes
// (ONE decoder on each side). Builtin targets are already resolved to a Point,
// and serializeAction collapses author targets that carry {x, y}; a target that
// does not resolve to coordinates drops the action (returns null).
export type SerializedAction =
| { kind: "Tap" | "DoubleTap" | "LongPress"; x: number; y: number; selector?: string }
| { kind: "InputText"; x: number; y: number; text: string; selector?: string }
| { kind: "Swipe"; fromX: number; fromY: number; toX: number; toY: number; durationMillis: number }
| {
kind: "Scroll";
direction: string;
fromX: number;
fromY: number;
toX: number;
toY: number;
durationMillis: number;
}
| { kind: "PressKey"; key: string }
| { kind: "Wait"; durationMillis: number };
const DEFAULT_SWIPE_DURATION = 250;
// pointOf resolves a target to {x, y, selector?}. Builtins and resolved ax
// elements carry numeric x/y; a bare selector string carries no geometry, so it
// serializes with (0, 0) and the selector, leaving the native runner to
// re-resolve coordinates by id/text. A target with neither shape (null, an
// unrecognized object) returns undefined so the action is dropped.
function pointOf(target: unknown): (Point & { selector?: string }) | undefined {
if (typeof target === "string") {
return target.length > 0 ? { x: 0, y: 0, selector: target } : undefined;
}
if (!target || typeof target !== "object") return undefined;
const obj = target as Record<string, unknown>;
if (typeof obj.x === "number" && typeof obj.y === "number") {
const point: Point & { selector?: string } = { x: obj.x, y: obj.y };
// The picker's builtin candidates carry `selector`; a goja ax element
// carries it under the runtime tag. Either lets the runner re-resolve.
const selector = obj.selector ?? obj.__sanderlingSelector;
if (typeof selector === "string" && selector.length > 0) point.selector = selector;
return point;
}
return undefined;
}
export function serializeAction(action: ActionDescriptor | null): SerializedAction | null {
if (!action) return null;
switch (action.kind) {
case "Tap":
case "DoubleTap":
case "LongPress": {
const point = pointOf(action.on);
if (!point) return null;
const out: SerializedAction = { kind: action.kind, x: point.x, y: point.y };
if (point.selector) out.selector = point.selector;
return out;
}
case "InputText": {
const point = pointOf(action.into);
if (!point) return null;
const out: SerializedAction = { kind: "InputText", x: point.x, y: point.y, text: action.text };
if (point.selector) out.selector = point.selector;
return out;
}
case "Swipe": {
const from = pointOf(action.from);
const to = pointOf(action.to);
if (!from || !to) return null;
return {
kind: "Swipe",
fromX: from.x,
fromY: from.y,
toX: to.x,
toY: to.y,
durationMillis: action.durationMillis ?? DEFAULT_SWIPE_DURATION,
};
}
case "Scroll": {
const from = pointOf(action.from) ?? pointOf(action.in);
if (!from) return null;
// The builtin generator pre-computes `to`; an author Scroll without it
// collapses to a zero-length gesture the runner re-derives from bounds.
const to = pointOf(action.to) ?? from;
return {
kind: "Scroll",
direction: action.direction,
fromX: from.x,
fromY: from.y,
toX: to.x,
toY: to.y,
durationMillis: DEFAULT_SWIPE_DURATION,
};
}
case "PressKey":
return { kind: "PressKey", key: action.key };
case "Wait":
return { kind: "Wait", durationMillis: action.durationMillis };
}
}
// installRuntime defines the next-action and extractor globals for one engine.
// root is the generator the spec assigned; pass a function when the spec runs
// AFTER this call (the web bundle imports the runtime before the spec, so the
// root only exists on globalThis.actions once the spec has evaluated).
// evaluateExtractors is the engine's snapshot of the spec's extract() handles.
//
// Setup precedence: when the spec assigned globalThis.setup, it is walked ONCE
// per tick first; if it yields an action that wins, otherwise the call falls
// through to the action root's 16-attempt retry. This matches the native
// verifier's prior NextAction precedence and applies on both engines.
export function installRuntime(
host: Host,
root: GeneratorNode | null | (() => GeneratorNode | null),
evaluateExtractors: () => Record<number, unknown>,
): void {
const rng = new Pcg(host.seedHi(), host.seedLo());
const resolveRoot = typeof root === "function" ? root : () => root;
const resolveSetup = () =>
(globalThis as { setup?: GeneratorNode }).setup ?? null;
defineLockedGlobal("__sanderlingExtractors__", () => evaluateExtractors());
defineLockedGlobal("__sanderlingNextAction__", () => {
// resolveRoot runs first: on web it also resets the per-tick candidate
// cache, which setup's walk below must see fresh.
const current = resolveRoot();
const setup = resolveSetup();
if (setup) {
const setupAction = walk(setup, rng, host);
if (setupAction) return serializeAction(setupAction);
}
if (!current) return null;
return serializeAction(nextAction(current, rng, host));
});
}
function defineLockedGlobal(name: string, value: unknown): void {
Object.defineProperty(globalThis, name, {
value,
writable: false,
configurable: false,
enumerable: false,
});
}
+17
View File
@@ -0,0 +1,17 @@
// samplerRng is the picker's Pcg while it evaluates an `actions` node's
// generator (set by pick.ts walkActions). Author sampling, from(...).generate()
// and the values.ts generators, draws from it so every sample shares the single
// deterministic stream. It is null outside a walk; eager spec-time generate()
// calls then fall back to a fixed deterministic default.
import type { Pcg } from "./pcg.ts";
let samplerRng: Pcg | null = null;
export function setSamplerRng(rng: Pcg | null): void {
samplerRng = rng;
}
export function getSamplerRng(): Pcg | null {
return samplerRng;
}
+10 -33
View File
@@ -125,6 +125,7 @@ export interface WebState extends State {
export interface Extracted<T> {
readonly current: T;
readonly previous: T | undefined;
named(name: string): Extracted<T>;
}
export interface Point {
@@ -175,10 +176,10 @@ export type Key =
| "left"
| "right";
export interface ActionGenerator {
readonly __sanderlingActionGenerator: true;
generate(): Action[];
}
// ActionGenerator is a node in the action-generator tree (see action-tree.ts).
// Author specs treat it as an opaque handle they compose with weighted(); the
// shared picker walks the underlying GeneratorNode.
export type ActionGenerator = GeneratorNode;
export interface Formula {
readonly __sanderlingFormula: true;
@@ -196,45 +197,21 @@ export interface Sampler<T> {
generate(): T;
}
// SanderlingRuntime is the host-bound surface that stays on
// globalThis.__sanderling__: extract plus the LTL formula constructors. Action
// factories no longer live here; they return plain data trees (see actions.ts).
export interface SanderlingRuntime {
extract: <T>(getter: (state: State) => T, name?: string) => Extracted<T>;
always: (predicateOrFormula: (() => boolean) | Formula) => Formula;
now: (predicate: () => boolean) => Formula;
next: (predicate: () => boolean) => Formula;
eventually: (predicate: () => boolean) => EventuallyFormula;
actions: (generator: () => Action[]) => ActionGenerator;
weighted: (...entries: WeightedEntry[]) => ActionGenerator;
from: <T>(items: readonly T[]) => Sampler<T>;
tap: (parameters: { on: string | AccessibilityElement }) => TapAction;
doubleTap: (parameters: { on: string | AccessibilityElement }) => DoubleTapAction;
longPress: (parameters: { on: string | AccessibilityElement }) => LongPressAction;
scroll: (parameters: {
direction: Direction;
in?: string | AccessibilityElement;
}) => ScrollAction;
inputText: (parameters: {
into: string | AccessibilityElement;
text: string;
}) => InputTextAction;
swipe: (parameters: {
from: Point | AccessibilityElement;
to: Point | AccessibilityElement;
durationMillis?: number;
}) => SwipeAction;
pressKey: (parameters: { key: Key }) => PressKeyAction;
wait: (parameters: { durationMillis: number }) => WaitAction;
taps: ActionGenerator;
doubleTaps: ActionGenerator;
longPresses: ActionGenerator;
scrolls: ActionGenerator;
typing: ActionGenerator;
swipes: ActionGenerator;
waitOnce: ActionGenerator;
pressKeys: ActionGenerator;
}
export type WeightedEntry = readonly [number, ActionGenerator];
import type { GeneratorNode } from "./action-tree.ts";
declare global {
// eslint-disable-next-line no-var
var __sanderling__: SanderlingRuntime;
+104
View File
@@ -0,0 +1,104 @@
// Author-facing, fluent, seeded value generators. Each builder is itself a
// Sampler<T> (it has .generate()), so `integers().between(0, 9)` is usable
// anywhere a Sampler is expected and chains read left to right.
//
// Every draw goes through the shared samplerRng (sampler-rng.ts) that from()
// uses, so generators are deterministic AND identical across the goja verifier
// and the V8 web runtime. Outside an actions() walk samplerRng is null and each
// generator returns a fixed deterministic default (mirroring from()'s index 0),
// so module-load-time calls never throw and never use an unseeded source.
import type { Pcg } from "./pcg.ts";
import type { Sampler } from "./types.ts";
import { getSamplerRng } from "./sampler-rng.ts";
import { INPUT_CORPUS } from "./corpus.ts";
const ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const DIGITS = "0123456789";
const ALPHANUMERIC = ALPHA + DIGITS;
function draw(rng: Pcg | null, bound: number): number {
return rng && bound > 1 ? rng.intN(bound) : 0;
}
class StringBuilder implements Sampler<string> {
private minLength = 1;
private maxLength = 16;
private charset = ALPHANUMERIC;
length(min: number, max: number): this {
this.minLength = min;
this.maxLength = max;
return this;
}
alpha(): this {
this.charset = ALPHA;
return this;
}
generate(): string {
const rng = getSamplerRng();
const span = this.maxLength - this.minLength + 1;
const count = this.minLength + draw(rng, span);
let result = "";
for (let i = 0; i < count; i++) {
result += this.charset[draw(rng, this.charset.length)];
}
return result;
}
}
class IntegerBuilder implements Sampler<number> {
private minValue = 0;
private maxValue = 2 ** 31 - 1;
between(min: number, max: number): this {
this.minValue = min;
this.maxValue = max;
return this;
}
generate(): number {
const rng = getSamplerRng();
const span = this.maxValue - this.minValue + 1;
return this.minValue + draw(rng, span);
}
}
class EmailBuilder implements Sampler<string> {
private host = "example.com";
domain(domain: string): this {
this.host = domain;
return this;
}
generate(): string {
const local = new StringBuilder().length(3, 8).alpha().generate();
return `${local}@${this.host}`;
}
}
class EdgeCaseTextBuilder implements Sampler<string> {
generate(): string {
const rng = getSamplerRng();
return INPUT_CORPUS[draw(rng, INPUT_CORPUS.length)] ?? "";
}
}
export function strings(): StringBuilder {
return new StringBuilder();
}
export function integers(): IntegerBuilder {
return new IntegerBuilder();
}
export function emails(): EmailBuilder {
return new EmailBuilder();
}
export function edgeCaseText(): EdgeCaseTextBuilder {
return new EdgeCaseTextBuilder();
}
+58
View File
@@ -0,0 +1,58 @@
// Verb support matrix and a warn-once helper, shared by both engines.
//
// The matrix is the single declared decision about which builtin verbs each
// platform supports. The picker (pick.ts) consults it before drawing so an
// unsupported verb is surfaced once (via host.reportUnsupported) rather than
// silently producing a null action every tick.
import type { BuiltinVerb, Host } from "./action-tree.ts";
export type Platform = "android" | "ios" | "web";
// VERB_SUPPORT declares, per verb, the platforms that can execute it.
// Tap/DoubleTap/InputText/Wait: native + web.
// PressKey: native (full key set incl. back/home) + web (enter/tab/escape/arrows).
// Swipe/LongPress/Scroll: native yes; web yes (the chrome driver supports
// pointer drags / long-press / wheel), declared here rather than silently
// no-op as the older web-runtime did.
const VERB_SUPPORT: Record<BuiltinVerb, ReadonlyArray<Platform>> = {
taps: ["android", "ios", "web"],
doubleTaps: ["android", "ios", "web"],
typing: ["android", "ios", "web"],
waitOnce: ["android", "ios", "web"],
pressKeys: ["android", "ios", "web"],
swipes: ["android", "ios", "web"],
longPresses: ["android", "ios", "web"],
scrolls: ["android", "ios", "web"],
};
// supports reports whether a verb is executable on a platform.
export function supports(verb: BuiltinVerb, platform: Platform): boolean {
return VERB_SUPPORT[verb].includes(platform);
}
// A warn-once registry keyed by `${verb}@${platform}`. Module-level so the
// "warn at most once" guarantee holds across every picker walk in a run.
const warnedKeys = new Set<string>();
function warnKey(verb: BuiltinVerb, platform: Platform): string {
return `${verb}@${platform}`;
}
// warnUnsupportedOnce calls host.reportUnsupported(verb) at most once per
// verb@platform for the lifetime of the process. Returns true on the first
// (reporting) call for a key, false on subsequent suppressed calls. The picker
// uses the boolean only for testability; behavior is the single report.
export function warnUnsupportedOnce(host: Host, verb: BuiltinVerb): boolean {
const key = warnKey(verb, host.platform());
if (warnedKeys.has(key)) return false;
warnedKeys.add(key);
host.reportUnsupported(verb);
return true;
}
// resetWarnings clears the warn-once registry. Test-only: production never
// resets, so a verb is reported once per process.
export function resetWarnings(): void {
warnedKeys.clear();
}
+228 -300
View File
@@ -2,36 +2,75 @@
// V8-side runtime for `sanderling test --platform web`.
//
// The user spec is bundled with this file as the first import so that
// globalThis.__sanderling__ is installed before the spec evaluates. The host
// invokes window.__sanderlingExtractors__() and window.__sanderlingNextAction__()
// over CDP each tick. LTL predicates are intentionally stubbed: properties
// run host-side in goja, which loads its own bundle of the same spec.
// This file is the WEB Host. It installs globalThis.__sanderling__ (extract +
// LTL formula binds) before the spec evaluates, implements the Host interface
// (platform/seed/queryCandidates/reportUnsupported) over the live DOM, and then
// delegates ALL action generation to the shared picker via installRuntime
// (runtime-entry.ts -> pick.ts). The goja verifier runs the SAME picker over the
// SAME Pcg, so a given seed yields an identical action stream by construction.
//
// Element references never cross V8/host. Action targets that reference an
// AccessibilityElement collapse to `{x, y}` from getBoundingClientRect()
// before serialization.
// The host invokes window.__sanderlingExtractors__() and
// window.__sanderlingNextAction__() over CDP each tick. LTL predicates are
// stubbed: properties run host-side in goja, which loads its own bundle.
//
// Element references never cross V8/host. queryCandidates resolves each element
// to a {x, y} Point via getBoundingClientRect before the picker sees it.
import { installRuntime } from "./runtime-entry.ts";
import type { BuiltinVerb, Candidate, Host } from "./action-tree.ts";
interface Handle {
current: unknown;
previous: unknown;
readonly current: unknown;
readonly previous: unknown;
named(name: string): Handle;
}
interface ExtractorEntry {
getter: (state: unknown) => unknown;
handle: Handle;
name: string;
}
interface ActionGeneratorHandle {
__sanderlingActionGenerator: true;
__sanderlingKind: string;
generate?: () => unknown;
entries?: readonly [number, ActionGeneratorHandle][];
currentValue: unknown;
previousValue: unknown;
}
const extractors: ExtractorEntry[] = [];
let actionsRoot: ActionGeneratorHandle | null = null;
// extracting is true only while an extractor getter is running. The current/
// previous accessors consult it so a getter that reaches into another
// extractor's handle throws instead of reading a stale cross-extractor value.
let extracting = false;
function checkNotExtracting(slot: "current" | "previous"): void {
if (extracting) {
throw new Error(
`reading .${slot} of an extractor inside another extractor is not allowed; extractor getters may read only from the state argument`,
);
}
}
// SANDERLING_SEED is the host-computed 64-bit seed, injected as a decimal
// string via the bundle define. We parse it into a BigInt without ever going
// through a JS Number (which loses precision above 2^53), matching the goja
// side's rand.NewPCG(seed, 0): hi = seed, lo = 0.
function injectedSeed(): string | undefined {
try {
return process.env.SANDERLING_SEED;
} catch {
return undefined;
}
}
function seedBigInt(): bigint {
const raw = injectedSeed();
if (!raw) return 0n;
try {
return BigInt(raw);
} catch {
return 0n;
}
}
const SEED_HI = seedBigInt();
function noopFormula(): unknown {
const formula: Record<string, unknown> = { __sanderlingFormula: true };
@@ -277,6 +316,38 @@ function buildAx(): unknown {
};
}
// Uncaught errors and unhandled rejections are buffered here as they fire, so
// the default noUncaughtExceptions property can observe them. Without this the
// web state.exceptions would always be empty and a page that throws would
// silently pass.
interface CapturedException {
class: string;
message: string;
stackTrace: string;
unixMillis: number;
}
const capturedExceptions: CapturedException[] = [];
function recordException(error: unknown): void {
const asError = error instanceof Error ? error : undefined;
capturedExceptions.push({
class: asError?.name ?? "Error",
message: asError?.message ?? String(error),
stackTrace: asError?.stack ?? "",
unixMillis: Date.now(),
});
}
if (typeof globalThis.addEventListener === "function") {
globalThis.addEventListener("error", (event: ErrorEvent) => {
recordException(event.error ?? event.message);
});
globalThis.addEventListener("unhandledrejection", (event: PromiseRejectionEvent) => {
recordException(event.reason);
});
}
function buildState(): unknown {
return {
snapshots: {},
@@ -286,91 +357,42 @@ function buildState(): unknown {
lastAction: null,
time: 0,
logs: [],
exceptions: [],
exceptions: capturedExceptions.slice(),
};
}
const runtime = {
extract<T>(getter: (state: unknown) => T, name?: string): Handle {
const handle: Handle = { current: undefined, previous: undefined };
const resolvedName = name && name.length > 0 ? name : `extractor_${extractors.length}`;
extractors.push({
const entry: ExtractorEntry = {
getter: getter as (s: unknown) => unknown,
handle,
handle: undefined as unknown as Handle,
name: resolvedName,
});
currentValue: undefined,
previousValue: undefined,
};
const handle: Handle = {
get current() {
checkNotExtracting("current");
return entry.currentValue;
},
get previous() {
checkNotExtracting("previous");
return entry.previousValue;
},
named(name: string): Handle {
entry.name = name;
return handle;
},
};
entry.handle = handle;
extractors.push(entry);
return handle;
},
always: noopFormula,
now: noopFormula,
next: noopFormula,
eventually: noopFormula,
actions(generator: () => unknown): ActionGeneratorHandle {
const handle: ActionGeneratorHandle = {
__sanderlingActionGenerator: true,
__sanderlingKind: "actions",
generate: generator,
};
if (!actionsRoot) actionsRoot = handle;
return handle;
},
weighted(...entries: [number, ActionGeneratorHandle][]): ActionGeneratorHandle {
const handle: ActionGeneratorHandle = {
__sanderlingActionGenerator: true,
__sanderlingKind: "weighted",
entries,
};
actionsRoot = handle;
return handle;
},
from<T>(items: readonly T[]): { generate: () => T | undefined } {
return {
generate(): T | undefined {
if (items.length === 0) return undefined;
return items[Math.floor(Math.random() * items.length)];
},
};
},
tap(p: { on: unknown }): unknown {
return { kind: "Tap", on: p.on };
},
doubleTap(p: { on: unknown }): unknown {
return { kind: "DoubleTap", on: p.on };
},
longPress(): unknown {
// Why: web has no long-press gesture for v1; the factory returns null so LongPress() calls no-op.
return null;
},
scroll(): unknown {
// Why: web has no native scroll gesture for v1; the factory returns null so Scroll() calls no-op.
return null;
},
inputText(p: { into: unknown; text: string }): unknown {
return { kind: "InputText", into: p.into, text: p.text };
},
swipe(_p: { from: unknown; to: unknown; durationMillis?: number }): unknown {
// Why: web has no swipe gesture; the factory returns null so Swipe() calls in specs no-op.
return null;
},
pressKey(p: { key: string }): unknown {
if (!WEB_PRESS_KEYS.includes(p.key)) {
throw new Error(
`pressKey: unsupported key ${JSON.stringify(p.key)} (allowed: ${WEB_PRESS_KEYS.join(", ")})`,
);
}
return { kind: "PressKey", key: p.key };
},
wait(p: { durationMillis: number }): unknown {
return { kind: "Wait", durationMillis: p.durationMillis };
},
taps: { __sanderlingActionGenerator: true, __sanderlingKind: "taps" } as ActionGeneratorHandle,
doubleTaps: { __sanderlingActionGenerator: true, __sanderlingKind: "doubleTaps" } as ActionGeneratorHandle,
longPresses: { __sanderlingActionGenerator: true, __sanderlingKind: "longPresses" } as ActionGeneratorHandle,
scrolls: { __sanderlingActionGenerator: true, __sanderlingKind: "scrolls" } as ActionGeneratorHandle,
typing: { __sanderlingActionGenerator: true, __sanderlingKind: "typing" } as ActionGeneratorHandle,
swipes: { __sanderlingActionGenerator: true, __sanderlingKind: "swipes" } as ActionGeneratorHandle,
waitOnce: { __sanderlingActionGenerator: true, __sanderlingKind: "waitOnce" } as ActionGeneratorHandle,
pressKeys: { __sanderlingActionGenerator: true, __sanderlingKind: "pressKey" } as ActionGeneratorHandle,
};
// Lock the runtime globals so a misbehaving (or malicious) page script can't
@@ -378,11 +400,16 @@ const runtime = {
// the host invoking the extractor/next-action callbacks.
defineLockedGlobal("__sanderling__", runtime);
// writable:false stops a page script from shadowing the runtime via plain
// assignment (the realistic in-page threat). configurable:true is required so
// unit tests sharing one process can reinstall a fake via defineProperty; a
// non-configurable lock would poison globalThis.__sanderling__ for every later
// test in the run.
function defineLockedGlobal(name: string, value: unknown): void {
Object.defineProperty(globalThis, name, {
value,
writable: false,
configurable: false,
configurable: true,
enumerable: false,
});
}
@@ -393,14 +420,19 @@ function evaluateExtractors(): Record<number, unknown> {
for (let i = 0; i < extractors.length; i++) {
const entry = extractors[i];
if (!entry) continue;
entry.handle.previous = entry.handle.current;
entry.previousValue = entry.currentValue;
// Let getter throws propagate, matching the goja side, where a getter
// error aborts PushSnapshot rather than yielding undefined. Swallowing
// here would silence the cross-extractor read guard (and every other
// author error) on web only, breaking cross-engine parity.
let value: unknown;
extracting = true;
try {
value = entry.getter(state);
} catch {
value = undefined;
} finally {
extracting = false;
}
entry.handle.current = value;
entry.currentValue = value;
result[i] = sanitize(value);
}
return result;
@@ -435,96 +467,21 @@ function sanitizeAt(value: unknown, depth: number, seen: WeakSet<object>): unkno
return out;
}
function pickWeighted(handle: ActionGeneratorHandle): ActionGeneratorHandle | null {
const entries = handle.entries ?? [];
if (entries.length === 0) return null;
let total = 0;
for (const [weight] of entries) total += Math.max(0, weight);
if (total <= 0) return null;
let pick = Math.random() * total;
for (const [weight, generator] of entries) {
pick -= Math.max(0, weight);
if (pick <= 0) return generator;
}
return entries[entries.length - 1]?.[1] ?? null;
}
function resolveGenerator(handle: ActionGeneratorHandle): unknown {
switch (handle.__sanderlingKind) {
case "actions": {
const generated = handle.generate?.();
return pickFromArray(generated);
}
case "weighted": {
const inner = pickWeighted(handle);
if (!inner) return null;
return resolveGenerator(inner);
}
case "taps":
return randomTap("Tap");
case "doubleTaps":
return randomTap("DoubleTap");
case "typing":
return randomInput();
case "swipes":
return randomSwipe();
case "longPresses":
case "scrolls":
// Why: web has no long-press or native scroll gesture for v1; both no-op.
return null;
case "waitOnce":
return { kind: "Wait", durationMillis: 500 };
case "pressKey":
return randomPressKey();
default:
return null;
}
}
// Per-tick cache so the 16-attempt retry loop in __sanderlingNextAction__
// doesn't re-walk the DOM and re-flush layout on every iteration.
let randomTapCandidates: HTMLElement[] | null = null;
function randomTap(kind: "Tap" | "DoubleTap"): unknown {
if (!randomTapCandidates) {
randomTapCandidates = Array.from(
document.querySelectorAll<HTMLElement>(
'a, button, input, select, textarea, [role="button"], [onclick]',
),
).filter((element) => {
if ((element as HTMLButtonElement).disabled) return false;
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
});
}
const candidates = randomTapCandidates;
if (candidates.length === 0) return null;
const picked = candidates[Math.floor(Math.random() * candidates.length)];
if (!picked) return null;
const rect = picked.getBoundingClientRect();
return {
kind,
on: {
x: Math.round(rect.left + rect.width / 2),
y: Math.round(rect.top + rect.height / 2),
},
};
}
// Per-tick cache mirroring randomTapCandidates, reset each NextAction tick.
let randomInputCandidates: HTMLElement[] | null = null;
// Per-verb DOM selector sets. The tappable set backs taps/doubleTaps/longPresses
// (every tappable element is also a valid long-press target); the editable set
// backs typing; swipes/scrolls target the scrollable element.
const TAPPABLE_SELECTOR = 'a, button, input, select, textarea, [role="button"], [onclick]';
const EDITABLE_SELECTOR = "input, textarea, [contenteditable]";
const NON_TEXT_INPUT_TYPES = [
"button", "submit", "checkbox", "radio", "range", "color", "file", "image", "reset",
];
// Edge-case input pool mirroring the goja-side inputCorpus: empty, whitespace,
// overflow length, unicode, numeric boundaries, and common injection payloads.
const WEB_INPUT_CORPUS = [
"", "a", "a".repeat(4096), "🙂🔥💸", " ", "\t\n", "-1",
"999999999999999999999", "0.0000001", "1e10", "'; DROP TABLE--",
"<script>alert(1)</script>", "../../etc/passwd", "%s%n", "NaN",
];
function isVisible(element: HTMLElement): boolean {
if ((element as HTMLButtonElement).disabled) return false;
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
}
function isEditableElement(element: HTMLElement): boolean {
if (element.isContentEditable) return true;
@@ -537,141 +494,112 @@ function isEditableElement(element: HTMLElement): boolean {
return false;
}
function randomInput(): unknown {
if (!randomInputCandidates) {
randomInputCandidates = Array.from(
document.querySelectorAll<HTMLElement>("input, textarea, [contenteditable]"),
).filter((element) => {
if (!isEditableElement(element)) return false;
if ((element as HTMLInputElement).disabled) return false;
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
});
}
const candidates = randomInputCandidates;
if (candidates.length === 0) return null;
const picked = candidates[Math.floor(Math.random() * candidates.length)];
if (!picked) return null;
const rect = picked.getBoundingClientRect();
const text = WEB_INPUT_CORPUS[Math.floor(Math.random() * WEB_INPUT_CORPUS.length)];
function pointOf(element: Element): Candidate {
const rect = element.getBoundingClientRect();
return {
kind: "InputText",
into: {
x: Math.round(rect.left + rect.width / 2),
y: Math.round(rect.top + rect.height / 2),
},
text,
x: Math.round(rect.left + rect.width / 2),
y: Math.round(rect.top + rect.height / 2),
width: Math.round(rect.width),
height: Math.round(rect.height),
};
}
function randomSwipe(): unknown {
// Why: web has no swipe gesture; pointer drags into empty divs are noise.
return null;
function tappableCandidates(): Candidate[] {
return Array.from(document.querySelectorAll<HTMLElement>(TAPPABLE_SELECTOR))
.filter(isVisible)
.map(pointOf);
}
const WEB_PRESS_KEYS = ["enter", "tab", "escape", "up", "down", "left", "right"];
function randomPressKey(): unknown {
// Why: only emit keys with meaningful browser semantics; "back"/"home" don't navigate.
const key = WEB_PRESS_KEYS[Math.floor(Math.random() * WEB_PRESS_KEYS.length)];
return { kind: "PressKey", key };
function editableCandidates(): Candidate[] {
return Array.from(document.querySelectorAll<HTMLElement>(EDITABLE_SELECTOR))
.filter((element) => isEditableElement(element) && isVisible(element))
.map(pointOf);
}
function pickFromArray(value: unknown): unknown {
if (!value) return null;
if (!Array.isArray(value)) return value;
if (value.length === 0) return null;
return value[Math.floor(Math.random() * value.length)];
}
function serializeAction(action: unknown): unknown {
if (!action || typeof action !== "object") return null;
const obj = action as Record<string, unknown>;
switch (obj.kind) {
case "Tap": {
const point = pointOf(obj.on);
if (!point) {
console.warn("[sanderling] Tap target did not resolve to coordinates");
return null;
}
return { kind: "Tap", x: point.x, y: point.y };
// Scrollable candidates back swipe/scroll: elements that overflow their box,
// plus the scrolling root so a page-level scroll always has a target.
function scrollableCandidates(): Candidate[] {
const root = document.scrollingElement ?? document.documentElement;
const candidates: Candidate[] = root ? [pointOf(root)] : [];
for (const element of Array.from(document.querySelectorAll<HTMLElement>("*"))) {
if (element === root) continue;
if (element.scrollHeight <= element.clientHeight && element.scrollWidth <= element.clientWidth) {
continue;
}
case "DoubleTap": {
const point = pointOf(obj.on);
if (!point) {
console.warn("[sanderling] DoubleTap target did not resolve to coordinates");
return null;
}
return { kind: "DoubleTap", x: point.x, y: point.y };
}
case "InputText": {
const point = pointOf(obj.into);
if (!point) {
console.warn("[sanderling] InputText target did not resolve to coordinates");
return null;
}
return {
kind: "InputText",
x: point.x,
y: point.y,
text: obj.text ?? "",
};
}
case "Swipe": {
const from = pointOf(obj.from);
const to = pointOf(obj.to);
if (!from || !to) {
console.warn("[sanderling] Swipe endpoints did not resolve to coordinates");
return null;
}
return {
kind: "Swipe",
from_x: from.x,
from_y: from.y,
to_x: to.x,
to_y: to.y,
duration_millis: obj.durationMillis ?? 250,
};
}
case "LongPress":
case "Scroll":
// Why: web has no long-press or native scroll gesture for v1; drop the action.
return null;
case "PressKey":
return { kind: "PressKey", key: obj.key };
case "Wait":
return { kind: "Wait", duration_millis: obj.durationMillis ?? 0 };
default:
return null;
if (!isVisible(element)) continue;
candidates.push(pointOf(element));
}
return candidates;
}
function pointOf(value: unknown): { x: number; y: number } | undefined {
if (!value || typeof value !== "object") return undefined;
const obj = value as Record<string, unknown>;
if (typeof obj.x === "number" && typeof obj.y === "number") {
return { x: obj.x, y: obj.y };
}
return undefined;
// Per-tick candidate cache: the picker's 16-attempt retry re-queries the same
// verb, so we avoid re-walking the DOM and re-flushing layout within one tick.
// installRuntime resets it before each __sanderlingNextAction__ invocation.
const candidateCache = new Map<BuiltinVerb, Candidate[]>();
function resetCandidateCache(): void {
candidateCache.clear();
}
defineLockedGlobal("__sanderlingExtractors__", function (): Record<number, unknown> {
return evaluateExtractors();
});
const host: Host = {
platform: () => "web",
seedHi: () => SEED_HI,
// lo = 0 matches the goja side's rand.NewPCG(seed, 0).
seedLo: () => 0n,
queryCandidates(verb: BuiltinVerb): Candidate[] {
const cached = candidateCache.get(verb);
if (cached) return cached;
let candidates: Candidate[];
switch (verb) {
case "taps":
case "doubleTaps":
case "longPresses":
candidates = tappableCandidates();
break;
case "typing":
candidates = editableCandidates();
break;
case "swipes":
case "scrolls":
candidates = scrollableCandidates();
break;
default:
candidates = [];
}
candidateCache.set(verb, candidates);
return candidates;
},
reportUnsupported(verb: BuiltinVerb): void {
console.warn(`[sanderling] verb ${verb} is unsupported on web`);
},
};
defineLockedGlobal("__sanderlingNextAction__", function (): unknown {
if (!actionsRoot) return null;
// Reset per-tick caches so each invocation gets a fresh DOM scan.
randomTapCandidates = null;
randomInputCandidates = null;
// Match the goja runtime: retry up to 16 times when a weighted entry's
// generator returns []. Otherwise on routes where most generators are
// gated to other pages, ~80% of ticks would emit no action.
for (let attempt = 0; attempt < 16; attempt++) {
const action = serializeAction(resolveGenerator(actionsRoot));
if (action !== null) return action;
}
return null;
});
// The spec assigns its action root to globalThis.actions, and it runs AFTER
// this module (the web bundle imports the runtime first). Resolve the root
// lazily so installRuntime captures it once the spec has evaluated. The root
// resolver runs once per __sanderlingNextAction__ tick (before the retry loop),
// so it is also where we reset the per-tick candidate cache.
installRuntime(
host,
() => {
resetCandidateCache();
return (globalThis as { actions?: import("./action-tree.ts").GeneratorNode }).actions ?? null;
},
evaluateExtractors,
);
// Test-only exports. The IIFE bundle the host installs has no export surface,
// so these are stripped from production output; they only exist for unit tests.
export const __testing__ = {
host,
seedBigInt,
tappableCandidates,
editableCandidates,
scrollableCandidates,
resetCandidateCache,
runtime,
extractors,
evaluateExtractors,
};
export {};
+126 -135
View File
@@ -4,38 +4,46 @@ import { test } from "node:test";
import {
DoubleTap,
InputText,
LongPress,
PressKey,
Scroll,
Swipe,
Tap,
Wait,
actions,
always,
doubleTaps,
eventually,
extract,
from,
keyedBy,
longPresses,
next,
now,
pressKey,
pressKeys,
scrolls,
swipes,
taps,
typing,
waitOnce,
weighted,
whenRoute,
} from "../src/index.ts";
import { setSamplerRng } from "../src/actions.ts";
import { Pcg } from "../src/pcg.ts";
import type { GeneratorNode } from "../src/action-tree.ts";
import type {
AccessibilityElement,
Action,
ActionGenerator,
EventuallyFormula,
Extracted,
Formula,
Sampler,
State,
SanderlingRuntime,
WeightedEntry,
} from "../src/types.ts";
// extract() and the LTL constructors stay host-bound on __sanderling__; the
// action factories now return plain data and need no runtime. This fake records
// the host-bound calls so the forwarding tests can assert them.
interface RecordedRuntime extends SanderlingRuntime {
extracts: Array<(state: State) => unknown>;
extractNames: Array<string | undefined>;
@@ -48,13 +56,10 @@ interface RecordedRuntime extends SanderlingRuntime {
orCalls: number;
andCalls: number;
notCalls: number;
actionGenerators: Array<() => Action[]>;
weightedCalls: WeightedEntry[][];
fromCalls: unknown[][];
}
function makeChainableFormula(record: RecordedRuntime): Formula {
const formula: Formula = {
return {
__sanderlingFormula: true,
implies(other: Formula): Formula {
record.impliesCalls++;
@@ -76,7 +81,6 @@ function makeChainableFormula(record: RecordedRuntime): Formula {
return makeChainableFormula(record);
},
};
return formula;
}
function makeChainableEventually(record: RecordedRuntime): EventuallyFormula {
@@ -103,11 +107,8 @@ function installFakeRuntime(): RecordedRuntime {
orCalls: 0,
andCalls: 0,
notCalls: 0,
actionGenerators: [] as Array<() => Action[]>,
weightedCalls: [] as WeightedEntry[][],
fromCalls: [] as unknown[][],
};
const runtime = {
const runtime: SanderlingRuntime = {
extract: <T>(getter: (state: State) => T, name?: string): Extracted<T> => {
calls.extracts.push(getter as (state: State) => unknown);
calls.extractNames.push(name);
@@ -129,42 +130,17 @@ function installFakeRuntime(): RecordedRuntime {
calls.eventuallyPredicates.push(predicate);
return makeChainableEventually(recorded);
},
actions: (generator: () => Action[]): ActionGenerator => {
calls.actionGenerators.push(generator);
return { __sanderlingActionGenerator: true, generate: generator };
},
weighted: (...entries: WeightedEntry[]): ActionGenerator => {
calls.weightedCalls.push(entries);
return { __sanderlingActionGenerator: true, generate: () => [] };
},
from: <T>(items: readonly T[]): Sampler<T> => {
calls.fromCalls.push(items as unknown[]);
return { generate: () => items[0] as T };
},
tap: ({ on }) => ({ kind: "Tap", on }),
doubleTap: ({ on }) => ({ kind: "DoubleTap", on }),
longPress: ({ on }) => ({ kind: "LongPress", on }),
scroll: ({ direction, in: container }) => ({ kind: "Scroll", direction, in: container }),
inputText: ({ into, text }) => ({ kind: "InputText", into, text }),
swipe: ({ from: fromPoint, to, durationMillis }) => ({
kind: "Swipe",
from: fromPoint,
to,
durationMillis,
}),
pressKey: ({ key }) => ({ kind: "PressKey", key }),
wait: ({ durationMillis }) => ({ kind: "Wait", durationMillis }),
taps: { __sanderlingActionGenerator: true, generate: () => [] },
doubleTaps: { __sanderlingActionGenerator: true, generate: () => [] },
longPresses: { __sanderlingActionGenerator: true, generate: () => [] },
scrolls: { __sanderlingActionGenerator: true, generate: () => [] },
typing: { __sanderlingActionGenerator: true, generate: () => [] },
swipes: { __sanderlingActionGenerator: true, generate: () => [] },
waitOnce: { __sanderlingActionGenerator: true, generate: () => [] },
pressKeys: { __sanderlingActionGenerator: true, generate: () => [] },
} satisfies SanderlingRuntime;
};
const recorded = Object.assign(runtime, calls) as RecordedRuntime;
globalThis.__sanderling__ = recorded;
// web-runtime.ts locks __sanderling__ as non-writable (configurable) when it
// is imported earlier in the same bun-test process, so a plain assignment
// would throw. defineProperty installs (and reinstalls) the fake regardless.
Object.defineProperty(globalThis, "__sanderling__", {
value: recorded,
writable: false,
configurable: true,
enumerable: false,
});
return recorded;
}
@@ -202,14 +178,6 @@ test("always wraps a predicate into a formula via the runtime", () => {
assert.equal(formula.__sanderlingFormula, true);
});
test("always accepts a formula handle", () => {
const runtime = installFakeRuntime();
const inner = now(() => true);
const wrapped = always(inner);
assert.equal(runtime.alwaysArgs.at(-1), inner);
assert.equal(wrapped.__sanderlingFormula, true);
});
test("now/next/eventually forward predicates", () => {
const runtime = installFakeRuntime();
const p1 = () => true;
@@ -240,20 +208,30 @@ test("formula chaining exposes implies/or/and/not", () => {
assert.equal(runtime.notCalls, 1);
});
test("Tap returns a TapAction with the supplied selector", () => {
installFakeRuntime();
const action = Tap({ on: "id:login_continue" });
assert.deepEqual(action, { kind: "Tap", on: "id:login_continue" });
test("Tap returns a TapAction descriptor", () => {
assert.deepEqual(Tap({ on: "id:login_continue" }), {
kind: "Tap",
on: "id:login_continue",
});
});
test("DoubleTap returns a DoubleTapAction with the supplied selector", () => {
installFakeRuntime();
const action = DoubleTap({ on: "id:save" });
assert.deepEqual(action, { kind: "DoubleTap", on: "id:save" });
test("DoubleTap returns a DoubleTapAction descriptor", () => {
assert.deepEqual(DoubleTap({ on: "id:save" }), { kind: "DoubleTap", on: "id:save" });
});
test("LongPress returns a LongPressAction descriptor", () => {
assert.deepEqual(LongPress({ on: "id:row" }), { kind: "LongPress", on: "id:row" });
});
test("Scroll returns a ScrollAction descriptor", () => {
assert.deepEqual(Scroll({ direction: "down", in: "id:list" }), {
kind: "Scroll",
direction: "down",
in: "id:list",
});
});
test("Tap accepts an AccessibilityElement", () => {
installFakeRuntime();
const element: AccessibilityElement = {
id: "login_continue",
find: () => undefined,
@@ -264,63 +242,92 @@ test("Tap accepts an AccessibilityElement", () => {
assert.equal(action.on, element);
});
test("InputText returns an InputTextAction", () => {
installFakeRuntime();
const action = InputText({ into: "id:phone", text: "+1234567890" });
assert.deepEqual(action, { kind: "InputText", into: "id:phone", text: "+1234567890" });
});
test("Swipe returns a SwipeAction with the supplied endpoints", () => {
installFakeRuntime();
const action = Swipe({ from: { x: 10, y: 20 }, to: { x: 30, y: 40 }, durationMillis: 400 });
assert.deepEqual(action, {
kind: "Swipe",
from: { x: 10, y: 20 },
to: { x: 30, y: 40 },
durationMillis: 400,
test("InputText returns an InputTextAction descriptor", () => {
assert.deepEqual(InputText({ into: "id:phone", text: "+1234567890" }), {
kind: "InputText",
into: "id:phone",
text: "+1234567890",
});
});
test("PressKey returns a PressKeyAction", () => {
installFakeRuntime();
const action = PressKey({ key: "back" });
assert.deepEqual(action, { kind: "PressKey", key: "back" });
test("Swipe returns a SwipeAction descriptor", () => {
assert.deepEqual(
Swipe({ from: { x: 10, y: 20 }, to: { x: 30, y: 40 }, durationMillis: 400 }),
{ kind: "Swipe", from: { x: 10, y: 20 }, to: { x: 30, y: 40 }, durationMillis: 400 },
);
});
test("Wait returns a WaitAction", () => {
installFakeRuntime();
const action = Wait({ durationMillis: 500 });
assert.deepEqual(action, { kind: "Wait", durationMillis: 500 });
test("PressKey returns a PressKeyAction descriptor", () => {
assert.deepEqual(PressKey({ key: "back" }), { kind: "PressKey", key: "back" });
});
test("actions wraps a generator into the runtime's ActionGenerator", () => {
const runtime = installFakeRuntime();
test("Wait returns a WaitAction descriptor", () => {
assert.deepEqual(Wait({ durationMillis: 500 }), { kind: "Wait", durationMillis: 500 });
});
test("actions returns an actions node carrying the generator", () => {
const generator = () => [Tap({ on: "id:x" })];
const wrapped = actions(generator);
assert.equal(runtime.actionGenerators[0], generator);
assert.equal(wrapped.__sanderlingActionGenerator, true);
const node = actions(generator);
assert.equal(node.kind, "actions");
assert.equal((node as { generate: unknown }).generate, generator);
});
test("weighted forwards weighted entries to the runtime", () => {
const runtime = installFakeRuntime();
const entries: WeightedEntry[] = [
test("weighted returns a weighted node carrying the branches", () => {
const node = weighted([80, taps], [20, swipes]);
assert.equal(node.kind, "weighted");
assert.deepEqual((node as { branches: unknown }).branches, [
[80, taps],
[20, swipes],
]);
});
test("builtin factories return builtin nodes carrying their verb", () => {
const cases: Array<[GeneratorNode, string]> = [
[taps, "taps"],
[doubleTaps, "doubleTaps"],
[longPresses, "longPresses"],
[scrolls, "scrolls"],
[typing, "typing"],
[swipes, "swipes"],
[waitOnce, "waitOnce"],
[pressKeys, "pressKeys"],
];
weighted(...entries);
assert.deepEqual(runtime.weightedCalls[0], entries);
for (const [node, verb] of cases) {
assert.equal(node.kind, "builtin");
assert.equal((node as { verb: string }).verb, verb);
}
});
test("from forwards items to the runtime", () => {
const runtime = installFakeRuntime();
const sampler = from(["a", "b", "c"]);
assert.deepEqual(runtime.fromCalls[0], ["a", "b", "c"]);
assert.equal(sampler.generate(), "a");
test("from with a single item returns it without drawing", () => {
const sampler = from(["only"]);
setSamplerRng(new Pcg(42n, 0n));
try {
assert.equal(sampler.generate(), "only");
} finally {
setSamplerRng(null);
}
});
function elementWithChildren(
cells: Record<string, string>,
): AccessibilityElement {
test("from draws intN(len) from the active picker rng", () => {
const items = ["a", "b", "c"];
const sampler = from(items);
const rng = new Pcg(42n, 0n);
const oracle = new Pcg(42n, 0n);
setSamplerRng(rng);
try {
const index = oracle.intN(items.length);
assert.equal(sampler.generate(), items[index]);
} finally {
setSamplerRng(null);
}
});
test("from falls back to the first item outside a picker walk", () => {
setSamplerRng(null);
assert.equal(from(["a", "b", "c"]).generate(), "a");
});
function elementWithChildren(cells: Record<string, string>): AccessibilityElement {
return {
find: selector => {
if (typeof selector === "string" || Array.isArray(selector)) return undefined;
@@ -346,58 +353,42 @@ test("keyedBy joins testTag-resolved texts with a stable delimiter", () => {
});
test("keyedBy returns empty string for an undefined element", () => {
installFakeRuntime();
assert.equal(keyedBy(undefined, ["TxnDate"]), "");
});
test("keyedBy substitutes empty strings for missing children", () => {
installFakeRuntime();
const row = elementWithChildren({ TxnDate: "2026-04-26" });
assert.equal(
keyedBy(row, ["TxnDate", "TxnNote", "TxnAmount"]),
"2026-04-26\x1f\x1f",
);
assert.equal(keyedBy(row, ["TxnDate", "TxnNote", "TxnAmount"]), "2026-04-26\x1f\x1f");
});
test("whenRoute returns [] when current route does not match", () => {
installFakeRuntime();
test("whenRoute body is skipped when the current route does not match", () => {
const route = { current: "home" as string | null };
let bodyCalled = false;
const generator = whenRoute(route, "ledger", () => {
const node = whenRoute(route, "ledger", () => {
bodyCalled = true;
return [Tap({ on: "id:x" })];
});
assert.deepEqual(generator.generate(), []);
assert.equal(node.kind, "actions");
assert.deepEqual((node as { generate: () => unknown }).generate(), []);
assert.equal(bodyCalled, false);
});
test("whenRoute calls body when current route matches", () => {
installFakeRuntime();
test("whenRoute runs the body when the current route matches", () => {
const route = { current: "ledger" as string | null };
const generator = whenRoute(route, "ledger", () => [Tap({ on: "id:x" })]);
const result = generator.generate();
const node = whenRoute(route, "ledger", () => [Tap({ on: "id:x" })]);
const result = (node as { generate: () => Array<{ kind: string }> }).generate();
assert.equal(result.length, 1);
assert.equal(result[0]?.kind, "Tap");
});
test("whenRoute accepts an array of allowed routes", () => {
installFakeRuntime();
const route = { current: "add-account" as string | null };
const generator = whenRoute(route, ["home", "add-account"], () => [Tap({ on: "id:x" })]);
assert.equal(generator.generate().length, 1);
const node = whenRoute(route, ["home", "add-account"], () => [Tap({ on: "id:x" })]);
assert.equal((node as { generate: () => unknown[] }).generate().length, 1);
});
test("whenRoute returns [] for null route", () => {
installFakeRuntime();
test("whenRoute body is skipped for a null route", () => {
const route = { current: null as string | null };
const generator = whenRoute(route, ["home"], () => [Tap({ on: "id:x" })]);
assert.deepEqual(generator.generate(), []);
});
test("default generators proxy through to the runtime", () => {
installFakeRuntime();
assert.equal(taps.__sanderlingActionGenerator, true);
assert.equal(swipes.__sanderlingActionGenerator, true);
assert.equal(waitOnce.__sanderlingActionGenerator, true);
assert.equal(pressKey.__sanderlingActionGenerator, true);
const node = whenRoute(route, ["home"], () => [Tap({ on: "id:x" })]);
assert.deepEqual((node as { generate: () => unknown }).generate(), []);
});
+23 -93
View File
@@ -2,99 +2,29 @@ import assert from "node:assert/strict";
import { test } from "node:test";
import type {
Action,
ActionGenerator,
EventuallyFormula,
Extracted,
Formula,
Sampler,
State,
SanderlingRuntime,
WeightedEntry,
} from "../src/types.ts";
interface RecordedRuntime extends SanderlingRuntime {
currentState: State;
extractors: Array<(state: State) => unknown>;
alwaysArgs: Array<(() => boolean) | Formula>;
lastPredicate: (() => boolean) | undefined;
}
function installRuntime(initialState: State): RecordedRuntime {
const extractors: Array<(state: State) => unknown> = [];
const extracted: Array<{ value: unknown }> = [];
const alwaysArgs: Array<(() => boolean) | Formula> = [];
let lastPredicate: (() => boolean) | undefined;
const runtime = {
extract: <T>(getter: (state: State) => T): Extracted<T> => {
extractors.push(getter as (state: State) => unknown);
const slot = { value: getter(state.currentState) };
extracted.push(slot);
return {
get current(): T {
return slot.value as T;
},
previous: undefined,
};
},
always: (predicateOrFormula: (() => boolean) | Formula): Formula => {
alwaysArgs.push(predicateOrFormula);
if (typeof predicateOrFormula === "function") {
lastPredicate = predicateOrFormula;
}
return { __sanderlingFormula: true } as Formula;
},
// The defaults bundle still calls extract() and the LTL constructors, which
// stay host-bound on __sanderling__. Action factories now return plain data
// trees, so the fake runtime only needs the extract + formula surface.
function installRuntime(initialState: State): void {
const state = { current: initialState };
const runtime: SanderlingRuntime = {
extract: <T>(getter: (s: State) => T): Extracted<T> => ({
current: getter(state.current),
previous: undefined,
}),
always: () => ({ __sanderlingFormula: true } as Formula),
now: () => ({ __sanderlingFormula: true } as Formula),
next: () => ({ __sanderlingFormula: true } as Formula),
eventually: () => ({ __sanderlingFormula: true } as EventuallyFormula),
actions: (generator: () => Action[]): ActionGenerator => ({
__sanderlingActionGenerator: true,
generate: generator,
}),
weighted: (..._entries: WeightedEntry[]): ActionGenerator => ({
__sanderlingActionGenerator: true,
generate: () => [],
}),
from: <T>(_items: readonly T[]): Sampler<T> => ({ generate: () => _items[0] as T }),
tap: ({ on }) => ({ kind: "Tap", on }),
doubleTap: ({ on }) => ({ kind: "DoubleTap", on }),
longPress: ({ on }) => ({ kind: "LongPress", on }),
scroll: ({ direction, in: container }) => ({ kind: "Scroll", direction, in: container }),
inputText: ({ into, text }) => ({ kind: "InputText", into, text }),
swipe: (p) => ({ kind: "Swipe", from: p.from, to: p.to, durationMillis: p.durationMillis }),
pressKey: ({ key }) => ({ kind: "PressKey", key }),
wait: ({ durationMillis }) => ({ kind: "Wait", durationMillis }),
taps: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
doubleTaps: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
longPresses: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
scrolls: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
typing: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
swipes: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
waitOnce: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
pressKeys: { __sanderlingActionGenerator: true, generate: () => [] } as ActionGenerator,
} satisfies SanderlingRuntime;
const state = { currentState: initialState };
const recorded = Object.assign(runtime, {
currentState: initialState,
extractors,
alwaysArgs,
get lastPredicate() {
return lastPredicate;
},
}) as unknown as RecordedRuntime;
globalThis.__sanderling__ = recorded;
// Re-bind state ref so subsequent extract() calls read the up-to-date state.
Object.defineProperty(recorded, "currentState", {
get() {
return state.currentState;
},
set(next: State) {
state.currentState = next;
},
});
return recorded;
};
globalThis.__sanderling__ = runtime;
}
const emptyState: State = {
@@ -116,29 +46,29 @@ test("defaults bundle exports formulas tagged as LTL properties", async () => {
assert.equal(defaults.noLogcatErrors.__sanderlingFormula, true);
});
test("defaults bundle exports defaultActions as a valid ActionGenerator", async () => {
test("defaults bundle exports defaultActions as a weighted node", async () => {
installRuntime(emptyState);
const defaults = await import("../src/defaults/actions.ts");
assert.equal(defaults.defaultActions.__sanderlingActionGenerator, true);
assert.equal(typeof defaults.defaultActions.generate, "function");
assert.equal(defaults.defaultActions.kind, "weighted");
});
test("typing resolves as a valid ActionGenerator", async () => {
test("typing resolves as a builtin node", async () => {
installRuntime(emptyState);
const { typing } = await import("../src/defaults/actions.ts");
assert.equal(typing.__sanderlingActionGenerator, true);
assert.equal(typing.kind, "builtin");
assert.equal((typing as { verb: string }).verb, "typing");
});
test("longPresses and scrolls resolve as valid ActionGenerators", async () => {
test("scrolls resolves as a builtin node", async () => {
installRuntime(emptyState);
const { longPresses, scrolls } = await import("../src/defaults/actions.ts");
assert.equal(longPresses.__sanderlingActionGenerator, true);
assert.equal(scrolls.__sanderlingActionGenerator, true);
const { scrolls } = await import("../src/defaults/actions.ts");
assert.equal(scrolls.kind, "builtin");
assert.equal((scrolls as { verb: string }).verb, "scrolls");
});
test("defaults barrel re-exports both properties and actions", async () => {
installRuntime(emptyState);
const barrel = await import("../src/defaults/index.ts");
assert.equal(barrel.defaultActions.__sanderlingActionGenerator, true);
assert.equal(barrel.defaultActions.kind, "weighted");
assert.equal(barrel.noUncaughtExceptions.__sanderlingFormula, true);
});
+127
View File
@@ -0,0 +1,127 @@
[
{
"kind": "Tap",
"x": 250,
"y": 260,
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 150,
"y": 160,
"selector": "id:beta"
},
{
"kind": "Tap",
"x": 50,
"y": 60,
"selector": "id:alpha"
},
{
"kind": "InputText",
"x": 150,
"y": 160,
"text": "a",
"selector": "id:beta"
},
{
"kind": "InputText",
"x": 50,
"y": 60,
"text": "\t\n",
"selector": "id:alpha"
},
{
"kind": "Tap",
"x": 250,
"y": 260,
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 150,
"y": 160,
"selector": "id:beta"
},
{
"kind": "Tap",
"x": 250,
"y": 260,
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 150,
"y": 160,
"selector": "id:beta"
},
{
"kind": "Tap",
"x": 250,
"y": 260,
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 250,
"y": 260,
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 150,
"y": 160,
"selector": "id:beta"
},
{
"kind": "Tap",
"x": 250,
"y": 260,
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 50,
"y": 60,
"selector": "id:alpha"
},
{
"kind": "InputText",
"x": 150,
"y": 160,
"text": "999999999999999999999",
"selector": "id:beta"
},
{
"kind": "InputText",
"x": 150,
"y": 160,
"text": "-1",
"selector": "id:beta"
},
{
"kind": "InputText",
"x": 250,
"y": 260,
"text": "-1",
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 50,
"y": 60,
"selector": "id:alpha"
},
{
"kind": "Tap",
"x": 250,
"y": 260,
"selector": "id:gamma"
},
{
"kind": "Tap",
"x": 50,
"y": 60,
"selector": "id:alpha"
}
]
File diff suppressed because it is too large. Load diff
+52
View File
@@ -0,0 +1,52 @@
// Shared cross-runtime parity scenario: the FIXED seed, FIXED candidate list,
// and FIXED action root that both the node test (parity.test.ts) and the goja
// test (internal/verifier/parity_test.go) drive. Each side runs the SAME
// pick.ts over the SAME Pcg and asserts the SAME committed golden
// (fixtures/parity-golden.json); matching one golden on both sides proves the
// two engines agree without either invoking the other.
//
// The candidate ORDER and the per-tick PCG draw order are the parity contract.
// The weighted root mixes a tap branch (1 candidate draw) with a typing branch
// (1 candidate draw + 1 corpus draw), so a tick exercises weighted selection, a
// builtin, and the input corpus together; reordering candidates or adding or
// dropping a draw on either side shifts the stream and fails the golden.
import { Pcg } from "../src/pcg.ts";
import { nextAction } from "../src/pick.ts";
import { serializeAction, type SerializedAction } from "../src/runtime-entry.ts";
import { taps, typing, weighted } from "../src/actions.ts";
import type { BuiltinVerb, Candidate, GeneratorNode, Host } from "../src/action-tree.ts";
export const PARITY_SEED_HI = 0x9e3779b97f4a7c15n;
export const PARITY_STEPS = 20;
export const PARITY_CANDIDATES: Candidate[] = [
{ x: 50, y: 60, selector: "id:alpha", width: 100, height: 40 },
{ x: 150, y: 160, selector: "id:beta", width: 120, height: 48 },
{ x: 250, y: 260, selector: "id:gamma", width: 80, height: 32 },
];
// A 3:1 weighted split over taps and typing. The stub host returns the same
// candidate list for every verb so the only variables are the draw order and
// the JS engine's number/bigint behavior.
export const PARITY_ROOT: GeneratorNode = weighted([3, taps], [1, typing]);
const HOST: Host = {
platform: () => "android",
queryCandidates: (_verb: BuiltinVerb) => PARITY_CANDIDATES,
reportUnsupported: () => {},
seedHi: () => PARITY_SEED_HI,
seedLo: () => 0n,
};
// runParity emits the parity scenario's action stream from a fresh Pcg. It
// drives pick.ts directly (not installRuntime, whose globals are locked once)
// so the caller can run it repeatedly to assert determinism.
export function runParity(): (SerializedAction | null)[] {
const rng = new Pcg(PARITY_SEED_HI, 0n);
const stream: (SerializedAction | null)[] = [];
for (let i = 0; i < PARITY_STEPS; i++) {
stream.push(serializeAction(nextAction(PARITY_ROOT, rng, HOST)));
}
return stream;
}
+24
View File
@@ -0,0 +1,24 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { test } from "node:test";
import { PARITY_STEPS, runParity } from "./parity-harness.ts";
const goldenPath = fileURLToPath(new URL("./fixtures/parity-golden.json", import.meta.url));
const golden = JSON.parse(readFileSync(goldenPath, "utf8"));
// The node side of the W2 acceptance gate: the shared picker's stream for the
// fixed parity scenario must match the committed golden byte-for-byte. The goja
// test asserts the SAME golden, so a match on both sides proves cross-runtime
// parity. A drift here means pick.ts, the corpus, or the Pcg changed.
test("node picker matches the cross-runtime golden", () => {
const stream = runParity();
assert.equal(stream.length, PARITY_STEPS);
assert.deepEqual(stream, golden);
});
// Determinism: a second run from the same seed yields the identical stream.
test("node picker is deterministic across repeated runs", () => {
assert.deepEqual(runParity(), runParity());
});
+58
View File
@@ -0,0 +1,58 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import { Pcg } from "../src/pcg.ts";
const here = dirname(fileURLToPath(import.meta.url));
const fixturePath = join(here, "fixtures", "pcg-golden.json");
interface GoldenCase {
seed: { hi: string; lo: string };
uint64: string[];
float64: number[];
intN: Record<string, number[]>;
}
const cases: GoldenCase[] = JSON.parse(readFileSync(fixturePath, "utf8"));
function seedLabel(c: GoldenCase): string {
return `seed(${c.seed.hi}, ${c.seed.lo})`;
}
test("golden fixture has the expected shape", () => {
assert.ok(cases.length >= 5, "expected several seed cases");
for (const c of cases) {
assert.equal(c.uint64.length, 40);
assert.equal(c.float64.length, 40);
for (const draws of Object.values(c.intN)) {
assert.equal(draws.length, 40);
}
}
});
for (const c of cases) {
test(`${seedLabel(c)} uint64 sequence is bit-exact`, () => {
const pcg = new Pcg(BigInt(c.seed.hi), BigInt(c.seed.lo));
c.uint64.forEach((want, i) => {
assert.equal(pcg.uint64(), BigInt(want), `uint64 draw ${i}`);
});
});
test(`${seedLabel(c)} float64 sequence is bit-exact`, () => {
const pcg = new Pcg(BigInt(c.seed.hi), BigInt(c.seed.lo));
c.float64.forEach((want, i) => {
assert.equal(pcg.float64(), want, `float64 draw ${i}`);
});
});
for (const [n, expected] of Object.entries(c.intN)) {
test(`${seedLabel(c)} intN(${n}) sequence is bit-exact`, () => {
const pcg = new Pcg(BigInt(c.seed.hi), BigInt(c.seed.lo));
expected.forEach((want, i) => {
assert.equal(pcg.intN(Number(n)), want, `intN(${n}) draw ${i}`);
});
});
}
}
+335
View File
@@ -0,0 +1,335 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { Pcg } from "../src/pcg.ts";
import { nextAction, walk } from "../src/pick.ts";
import { INPUT_CORPUS, NATIVE_PRESS_KEYS, WEB_PRESS_KEYS } from "../src/corpus.ts";
import { resetWarnings } from "../src/verbs.ts";
import type {
ActionDescriptor,
BuiltinVerb,
Candidate,
GeneratorNode,
Host,
} from "../src/action-tree.ts";
type Platform = "android" | "ios" | "web";
// stubHost returns a fixed candidate list for every verb and records each
// reportUnsupported call so warn-once semantics are observable.
function stubHost(
platform: Platform,
candidates: Candidate[],
): Host & { unsupported: BuiltinVerb[] } {
const unsupported: BuiltinVerb[] = [];
return {
unsupported,
platform: () => platform,
queryCandidates: () => candidates,
reportUnsupported: (verb) => {
unsupported.push(verb);
},
seedHi: () => 0n,
seedLo: () => 0n,
};
}
const POINTS: Candidate[] = [
{ x: 10, y: 20, selector: "id:a", width: 100, height: 200 },
{ x: 30, y: 40, selector: "id:b", width: 100, height: 200 },
{ x: 50, y: 60, selector: "id:c", width: 100, height: 200 },
];
function builtin(verb: BuiltinVerb): GeneratorNode {
return { kind: "builtin", verb };
}
test("taps draws one candidate index and targets its point", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const rng = new Pcg(42n, 0n);
const oracle = new Pcg(42n, 0n);
const expectedIndex = oracle.intN(POINTS.length);
const action = walk(builtin("taps"), rng, host);
assert.deepEqual(action, {
kind: "Tap",
on: {
x: POINTS[expectedIndex]!.x,
y: POINTS[expectedIndex]!.y,
selector: POINTS[expectedIndex]!.selector,
},
});
});
test("typing draws candidate index then corpus index, in that order", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const rng = new Pcg(42n, 0n);
const oracle = new Pcg(42n, 0n);
const candidateIndex = oracle.intN(POINTS.length);
const corpusIndex = oracle.intN(INPUT_CORPUS.length);
const action = walk(builtin("typing"), rng, host) as ActionDescriptor & {
kind: "InputText";
};
assert.equal(action.kind, "InputText");
assert.deepEqual(action.into, {
x: POINTS[candidateIndex]!.x,
y: POINTS[candidateIndex]!.y,
selector: POINTS[candidateIndex]!.selector,
});
assert.equal(action.text, INPUT_CORPUS[corpusIndex]);
});
test("swipes draw candidate, magnitude (200+intN(401)), then direction", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const rng = new Pcg(7n, 0n);
const oracle = new Pcg(7n, 0n);
const candidateIndex = oracle.intN(POINTS.length);
const magnitude = 200 + oracle.intN(401);
const direction = oracle.intN(4);
const from = { x: POINTS[candidateIndex]!.x, y: POINTS[candidateIndex]!.y };
const expectedTo = { x: from.x, y: from.y };
switch (direction) {
case 0:
expectedTo.y = Math.max(0, from.y - magnitude);
break;
case 1:
expectedTo.y = Math.max(0, from.y + magnitude);
break;
case 2:
expectedTo.x = Math.max(0, from.x - magnitude);
break;
case 3:
expectedTo.x = Math.max(0, from.x + magnitude);
break;
}
const action = walk(builtin("swipes"), rng, host) as ActionDescriptor & {
kind: "Swipe";
};
assert.equal(action.kind, "Swipe");
assert.deepEqual(action.from, from);
assert.deepEqual(action.to, expectedTo);
assert.equal(action.durationMillis, 250);
});
test("scrolls draw candidate index then direction", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const rng = new Pcg(99n, 0n);
const oracle = new Pcg(99n, 0n);
const candidateIndex = oracle.intN(POINTS.length);
const directionIndex = oracle.intN(4);
const directions = ["up", "down", "left", "right"] as const;
const action = walk(builtin("scrolls"), rng, host) as ActionDescriptor & {
kind: "Scroll";
};
assert.equal(action.kind, "Scroll");
assert.equal(action.direction, directions[directionIndex]);
assert.deepEqual(action.in, {
x: POINTS[candidateIndex]!.x,
y: POINTS[candidateIndex]!.y,
});
});
test("doubleTaps and longPresses draw exactly one candidate index", () => {
resetWarnings();
for (const verb of ["doubleTaps", "longPresses"] as const) {
const host = stubHost("android", POINTS);
const rng = new Pcg(123n, 0n);
const oracle = new Pcg(123n, 0n);
const index = oracle.intN(POINTS.length);
const action = walk(builtin(verb), rng, host);
const kind = verb === "doubleTaps" ? "DoubleTap" : "LongPress";
assert.deepEqual(action, {
kind,
on: {
x: POINTS[index]!.x,
y: POINTS[index]!.y,
selector: POINTS[index]!.selector,
},
});
}
});
test("waitOnce emits a 500ms wait and draws nothing", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const rng = new Pcg(42n, 0n);
const before = rng.float64();
const after = rng.float64();
const fresh = new Pcg(42n, 0n);
fresh.float64();
const action = walk(builtin("waitOnce"), fresh, host);
const afterWait = fresh.float64();
assert.deepEqual(action, { kind: "Wait", durationMillis: 500 });
// The wait consumed no draw, so the stream is identical with or without it.
assert.equal(afterWait, after);
assert.notEqual(before, after);
});
test("pressKeys on native draws from NATIVE_PRESS_KEYS", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const rng = new Pcg(42n, 0n);
const oracle = new Pcg(42n, 0n);
const index = oracle.intN(NATIVE_PRESS_KEYS.length);
const action = walk(builtin("pressKeys"), rng, host);
assert.deepEqual(action, { kind: "PressKey", key: NATIVE_PRESS_KEYS[index] });
});
test("pressKeys on web draws from WEB_PRESS_KEYS", () => {
resetWarnings();
const host = stubHost("web", POINTS);
const rng = new Pcg(2024n, 0n);
const oracle = new Pcg(2024n, 0n);
const index = oracle.intN(WEB_PRESS_KEYS.length);
const action = walk(builtin("pressKeys"), rng, host);
assert.deepEqual(action, { kind: "PressKey", key: WEB_PRESS_KEYS[index] });
});
test("empty candidate list yields null without drawing", () => {
resetWarnings();
const host = stubHost("android", []);
const rng = new Pcg(42n, 0n);
assert.equal(walk(builtin("taps"), rng, host), null);
// No draw consumed: next float64 matches a fresh stream's first draw.
assert.equal(rng.float64(), new Pcg(42n, 0n).float64());
});
test("weighted: one float64 draw, ascending cumulative scan", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const tap: GeneratorNode = builtin("taps");
const wait: GeneratorNode = builtin("waitOnce");
// total = 3; draw = float64()*3 decides the branch by ascending cumulative.
const node: GeneratorNode = {
kind: "weighted",
branches: [
[1, tap],
[2, wait],
],
};
const rng = new Pcg(42n, 0n);
const oracle = new Pcg(42n, 0n);
const draw = oracle.float64() * 3;
const expectFirst = draw < 1; // cumulative after branch 0 is 1.
const action = walk(node, rng, host);
if (expectFirst) {
assert.equal((action as ActionDescriptor).kind, "Tap");
} else {
assert.deepEqual(action, { kind: "Wait", durationMillis: 500 });
}
});
test("weighted: a zero-weight branch is never selected", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const poison: GeneratorNode = {
kind: "actions",
generate: () => {
throw new Error("zero-weight branch must never be walked");
},
};
const node: GeneratorNode = {
kind: "weighted",
branches: [
[0, poison],
[1, builtin("waitOnce")],
],
};
// Drive many seeds: the zero-weight branch must never be entered.
for (let seed = 1; seed <= 200; seed++) {
const action = walk(node, new Pcg(BigInt(seed), 0n), host);
assert.deepEqual(action, { kind: "Wait", durationMillis: 500 });
}
});
test("weighted: all-zero (or negative) weights return null", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const node: GeneratorNode = {
kind: "weighted",
branches: [
[0, builtin("waitOnce")],
[-5, builtin("taps")],
],
};
assert.equal(walk(node, new Pcg(42n, 0n), host), null);
});
test("actions: single-element list draws nothing", () => {
resetWarnings();
const only: ActionDescriptor = { kind: "Wait", durationMillis: 10 };
const node: GeneratorNode = { kind: "actions", generate: () => [only] };
const rng = new Pcg(42n, 0n);
const action = walk(node, rng, stubHost("android", POINTS));
assert.deepEqual(action, only);
// No draw consumed for a singleton list.
assert.equal(rng.float64(), new Pcg(42n, 0n).float64());
});
test("actions: multi-element list draws one intN(len) index", () => {
resetWarnings();
const list: ActionDescriptor[] = [
{ kind: "Wait", durationMillis: 1 },
{ kind: "Wait", durationMillis: 2 },
{ kind: "Wait", durationMillis: 3 },
];
const node: GeneratorNode = { kind: "actions", generate: () => [...list] };
const rng = new Pcg(42n, 0n);
const oracle = new Pcg(42n, 0n);
const index = oracle.intN(list.length);
assert.deepEqual(walk(node, rng, stubHost("android", POINTS)), list[index]);
});
test("actions: empty list yields null", () => {
resetWarnings();
const node: GeneratorNode = { kind: "actions", generate: () => [] };
assert.equal(walk(node, new Pcg(42n, 0n), stubHost("android", POINTS)), null);
});
test("corpus reproducibility: same seed yields the same typed text", () => {
resetWarnings();
const host = stubHost("android", POINTS);
const first = walk(builtin("typing"), new Pcg(2024n, 0n), host) as ActionDescriptor & {
kind: "InputText";
};
const second = walk(builtin("typing"), new Pcg(2024n, 0n), host) as ActionDescriptor & {
kind: "InputText";
};
assert.equal(first.text, second.text);
assert.ok(INPUT_CORPUS.includes(first.text));
});
test("nextAction retries up to 16 times then returns null", () => {
resetWarnings();
// An empty weighted tree always returns null; nextAction must give up, not loop.
const empty: GeneratorNode = { kind: "weighted", branches: [] };
assert.equal(nextAction(empty, new Pcg(42n, 0n), stubHost("android", POINTS)), null);
});
test("nextAction returns the first non-null walk result", () => {
resetWarnings();
let calls = 0;
// First two walks yield null (empty list), third yields an action. nextAction
// should return on the third attempt.
const node: GeneratorNode = {
kind: "actions",
generate: () => {
calls++;
return calls >= 3 ? [{ kind: "Wait", durationMillis: 5 }] : [];
},
};
const action = nextAction(node, new Pcg(42n, 0n), stubHost("android", POINTS));
assert.deepEqual(action, { kind: "Wait", durationMillis: 5 });
assert.equal(calls, 3);
});
+181
View File
@@ -0,0 +1,181 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { serializeAction } from "../src/runtime-entry.ts";
import type { ActionDescriptor } from "../src/action-tree.ts";
// decodeWire mirrors the Go decodeAction reading the unified flat contract: a
// field rename (fromX vs from_x) would make these assertions fail here, the
// same way it would silently no-op web actions in production.
const POINT = { x: 12, y: 34 };
test("Tap serializes to the flat point contract", () => {
assert.deepEqual(serializeAction({ kind: "Tap", on: POINT }), {
kind: "Tap",
x: 12,
y: 34,
});
});
test("Tap carries a resolved selector when present", () => {
const target = { x: 1, y: 2, selector: "id:save" };
assert.deepEqual(serializeAction({ kind: "Tap", on: target }), {
kind: "Tap",
x: 1,
y: 2,
selector: "id:save",
});
});
test("DoubleTap and LongPress share the point contract", () => {
assert.deepEqual(serializeAction({ kind: "DoubleTap", on: POINT }), {
kind: "DoubleTap",
x: 12,
y: 34,
});
assert.deepEqual(serializeAction({ kind: "LongPress", on: POINT }), {
kind: "LongPress",
x: 12,
y: 34,
});
});
test("InputText carries x, y and text", () => {
assert.deepEqual(serializeAction({ kind: "InputText", into: POINT, text: "hi" }), {
kind: "InputText",
x: 12,
y: 34,
text: "hi",
});
});
test("Swipe emits camelCase fromX/fromY/toX/toY/durationMillis", () => {
const action: ActionDescriptor = {
kind: "Swipe",
from: { x: 5, y: 6 },
to: { x: 7, y: 8 },
durationMillis: 300,
};
assert.deepEqual(serializeAction(action), {
kind: "Swipe",
fromX: 5,
fromY: 6,
toX: 7,
toY: 8,
durationMillis: 300,
});
});
test("Swipe defaults durationMillis to 250", () => {
const action = serializeAction({
kind: "Swipe",
from: { x: 0, y: 0 },
to: { x: 0, y: 1 },
}) as { durationMillis: number };
assert.equal(action.durationMillis, 250);
});
test("Scroll emits direction plus from/to point and duration", () => {
assert.deepEqual(serializeAction({ kind: "Scroll", direction: "down", in: POINT }), {
kind: "Scroll",
direction: "down",
fromX: 12,
fromY: 34,
toX: 12,
toY: 34,
durationMillis: 250,
});
});
test("PressKey and Wait pass through their fields", () => {
assert.deepEqual(serializeAction({ kind: "PressKey", key: "enter" }), {
kind: "PressKey",
key: "enter",
});
assert.deepEqual(serializeAction({ kind: "Wait", durationMillis: 500 }), {
kind: "Wait",
durationMillis: 500,
});
});
test("builtin Scroll carries the pre-computed to endpoint", () => {
assert.deepEqual(
serializeAction({
kind: "Scroll",
direction: "down",
in: { x: 100, y: 200 },
from: { x: 100, y: 200 },
to: { x: 100, y: 120 },
}),
{
kind: "Scroll",
direction: "down",
fromX: 100,
fromY: 200,
toX: 100,
toY: 120,
durationMillis: 250,
},
);
});
test("a string target serializes selector-only for the runner to re-resolve", () => {
assert.deepEqual(serializeAction({ kind: "Tap", on: "id:save" }), {
kind: "Tap",
x: 0,
y: 0,
selector: "id:save",
});
});
test("an empty target or null action is dropped", () => {
assert.equal(serializeAction({ kind: "Tap", on: "" }), null);
assert.equal(serializeAction(null), null);
});
// Wire round-trip: serialize -> JSON -> parse asserts every field the Go
// decodeAction reads survives by its exact camelCase name. A rename (fromX vs
// from_x) would silently turn web/native actions into no-ops; this catches it.
test("serialized actions JSON round-trip with the decoder's field names", () => {
const cases: ActionDescriptor[] = [
{ kind: "Tap", on: { x: 1, y: 2, selector: "id:a" } as never },
{ kind: "InputText", into: { x: 3, y: 4 } as never, text: "x" },
{ kind: "Swipe", from: { x: 5, y: 6 }, to: { x: 7, y: 8 }, durationMillis: 9 },
{ kind: "Scroll", direction: "up", in: { x: 1, y: 1 } as never },
{ kind: "PressKey", key: "enter" },
{ kind: "Wait", durationMillis: 10 },
];
for (const descriptor of cases) {
const wire = serializeAction(descriptor);
assert.ok(wire, `expected ${descriptor.kind} to serialize`);
const decoded = JSON.parse(JSON.stringify(wire)) as Record<string, unknown>;
assert.equal(decoded.kind, wire!.kind);
switch (wire!.kind) {
case "Tap":
assert.equal(typeof decoded.x, "number");
assert.equal(typeof decoded.y, "number");
assert.equal(decoded.selector, "id:a");
break;
case "InputText":
assert.equal(typeof decoded.x, "number");
assert.equal(decoded.text, "x");
break;
case "Swipe":
assert.equal(decoded.fromX, 5);
assert.equal(decoded.toY, 8);
assert.equal(decoded.durationMillis, 9);
break;
case "Scroll":
assert.equal(decoded.direction, "up");
assert.equal(typeof decoded.fromX, "number");
assert.equal(typeof decoded.toX, "number");
break;
case "PressKey":
assert.equal(decoded.key, "enter");
break;
case "Wait":
assert.equal(decoded.durationMillis, 10);
break;
}
}
});
+75
View File
@@ -0,0 +1,75 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { Pcg } from "../src/pcg.ts";
import { setSamplerRng } from "../src/sampler-rng.ts";
import { edgeCaseText, emails, integers, strings } from "../src/values.ts";
import { INPUT_CORPUS } from "../src/corpus.ts";
import type { Sampler } from "../src/types.ts";
function withRng<T>(seed: bigint, body: () => T): T {
setSamplerRng(new Pcg(seed, 0n));
try {
return body();
} finally {
setSamplerRng(null);
}
}
test("same seed yields an identical draw sequence", () => {
const drawAll = () => [
strings().generate(),
integers().between(0, 999).generate(),
emails().domain("folio.app").generate(),
edgeCaseText().generate(),
];
assert.deepEqual(withRng(7n, drawAll), withRng(7n, drawAll));
});
test("a different seed yields a different sequence", () => {
const drawAll = () => [strings().generate(), integers().generate()];
assert.notDeepEqual(withRng(1n, drawAll), withRng(2n, drawAll));
});
test("integers().between constrains the output range", () => {
withRng(42n, () => {
for (let i = 0; i < 200; i++) {
const value = integers().between(5, 9).generate();
assert.ok(Number.isInteger(value));
assert.ok(value >= 5 && value <= 9, `out of range: ${value}`);
}
});
});
test("strings().length(min, max).alpha() constrains length and charset", () => {
withRng(99n, () => {
for (let i = 0; i < 200; i++) {
const value = strings().length(3, 6).alpha().generate();
assert.ok(value.length >= 3 && value.length <= 6, `bad length: ${value}`);
assert.match(value, /^[A-Za-z]*$/);
}
});
});
test("emails().domain sets the host and edgeCaseText draws from the corpus", () => {
withRng(3n, () => {
const email = emails().domain("folio.app").generate();
assert.match(email, /^[A-Za-z]+@folio\.app$/);
assert.ok(INPUT_CORPUS.includes(edgeCaseText().generate()));
});
});
test("a builder is itself a Sampler<T>", () => {
withRng(11n, () => {
const sampler: Sampler<number> = integers().between(0, 9);
const value = sampler.generate();
assert.ok(value >= 0 && value <= 9);
});
});
test("outside a walk generators return a fixed deterministic default", () => {
setSamplerRng(null);
assert.equal(integers().between(10, 20).generate(), 10);
assert.equal(strings().length(4, 8).alpha().generate(), "aaaa");
assert.equal(emails().domain("folio.app").generate(), "[email protected]");
assert.equal(edgeCaseText().generate(), INPUT_CORPUS[0]);
});
+72
View File
@@ -0,0 +1,72 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { supports, warnUnsupportedOnce, resetWarnings } from "../src/verbs.ts";
import type { BuiltinVerb, Host } from "../src/action-tree.ts";
const ALL_VERBS: BuiltinVerb[] = [
"taps",
"doubleTaps",
"longPresses",
"scrolls",
"typing",
"swipes",
"waitOnce",
"pressKeys",
];
function countingHost(platform: "android" | "ios" | "web"): Host & { calls: BuiltinVerb[] } {
const calls: BuiltinVerb[] = [];
return {
calls,
platform: () => platform,
queryCandidates: () => [],
reportUnsupported: (verb) => {
calls.push(verb);
},
seedHi: () => 0n,
seedLo: () => 0n,
};
}
test("every verb is supported on native platforms", () => {
for (const verb of ALL_VERBS) {
assert.equal(supports(verb, "android"), true, `android ${verb}`);
assert.equal(supports(verb, "ios"), true, `ios ${verb}`);
}
});
test("web supports the full builtin verb set (declared, not silently no-op)", () => {
for (const verb of ALL_VERBS) {
assert.equal(supports(verb, "web"), true, `web ${verb}`);
}
});
test("warn-once reports a verb exactly once per platform", () => {
resetWarnings();
const host = countingHost("web");
assert.equal(warnUnsupportedOnce(host, "swipes"), true);
assert.equal(warnUnsupportedOnce(host, "swipes"), false);
assert.equal(warnUnsupportedOnce(host, "swipes"), false);
assert.deepEqual(host.calls, ["swipes"]);
});
test("warn-once keys by verb@platform independently", () => {
resetWarnings();
const web = countingHost("web");
const android = countingHost("android");
warnUnsupportedOnce(web, "scrolls");
warnUnsupportedOnce(android, "scrolls");
warnUnsupportedOnce(web, "scrolls");
// Same verb on two platforms: one report each, suppressed on repeat.
assert.deepEqual(web.calls, ["scrolls"]);
assert.deepEqual(android.calls, ["scrolls"]);
});
test("resetWarnings re-arms reporting", () => {
resetWarnings();
const host = countingHost("web");
warnUnsupportedOnce(host, "longPresses");
resetWarnings();
warnUnsupportedOnce(host, "longPresses");
assert.deepEqual(host.calls, ["longPresses", "longPresses"]);
});
+185
View File
@@ -0,0 +1,185 @@
import assert from "node:assert/strict";
import { test } from "node:test";
// The web runtime is the WEB Host: it parses the injected seed, reports its
// platform, and delegates action generation to the shared picker. These tests
// guard the Host surface and the seed-precision contract. DOM candidate
// enumeration is exercised against a minimal querySelectorAll stub.
// A 64-bit seed that loses precision as a JS Number must survive as a BigInt.
process.env.SANDERLING_SEED = "9007199254740993";
const { __testing__ } = await import("../src/web-runtime.ts");
const { host } = __testing__;
test("platform is web", () => {
assert.equal(host.platform(), "web");
});
test("seedHi parses the injected 64-bit seed without Number precision loss", () => {
assert.equal(host.seedHi(), 9007199254740993n);
});
test("seedLo is 0 to match goja rand.NewPCG(seed, 0)", () => {
assert.equal(host.seedLo(), 0n);
});
test("reportUnsupported warns once via console.warn", () => {
const original = console.warn;
const messages: string[] = [];
console.warn = (m: string) => messages.push(m);
try {
host.reportUnsupported("swipes");
} finally {
console.warn = original;
}
assert.equal(messages.length, 1);
assert.match(messages[0]!, /swipes/);
});
// installRuntime locked the next-action and extractor globals at import.
test("installRuntime defined the host-invoked globals", () => {
const g = globalThis as Record<string, unknown>;
assert.equal(typeof g.__sanderlingNextAction__, "function");
assert.equal(typeof g.__sanderlingExtractors__, "function");
assert.equal(typeof g.__sanderling__, "object");
});
// A button and a text input, each with a deterministic bounding box, exercise
// the per-verb selector routing without a full DOM.
function fakeElement(tag: string, rect: { x: number; y: number; w: number; h: number }) {
return {
tagName: tag.toUpperCase(),
disabled: false,
isContentEditable: false,
type: tag === "input" ? "text" : "",
scrollHeight: 0,
clientHeight: 0,
scrollWidth: 0,
clientWidth: 0,
getBoundingClientRect: () => ({
left: rect.x,
top: rect.y,
width: rect.w,
height: rect.h,
right: rect.x + rect.w,
bottom: rect.y + rect.h,
}),
};
}
function withFakeDocument(map: Record<string, unknown[]>, run: () => void) {
const g = globalThis as Record<string, unknown>;
const original = g.document;
g.document = {
querySelectorAll: (selector: string) => map[selector] ?? [],
scrollingElement: null,
documentElement: null,
};
try {
run();
} finally {
g.document = original;
}
}
test("queryCandidates routes taps to the tappable selector set", () => {
const button = fakeElement("button", { x: 10, y: 20, w: 40, h: 8 });
withFakeDocument(
{ 'a, button, input, select, textarea, [role="button"], [onclick]': [button] },
() => {
__testing__.resetCandidateCache();
const candidates = host.queryCandidates("taps");
assert.equal(candidates.length, 1);
assert.deepEqual({ x: candidates[0]!.x, y: candidates[0]!.y }, { x: 30, y: 24 });
},
);
});
test("queryCandidates routes typing to editable inputs only", () => {
const input = fakeElement("input", { x: 0, y: 0, w: 100, h: 20 });
withFakeDocument({ "input, textarea, [contenteditable]": [input] }, () => {
__testing__.resetCandidateCache();
const candidates = host.queryCandidates("typing");
assert.equal(candidates.length, 1);
assert.deepEqual({ x: candidates[0]!.x, y: candidates[0]!.y }, { x: 50, y: 10 });
});
});
test("queryCandidates caches within a tick until reset", () => {
const first = fakeElement("button", { x: 0, y: 0, w: 10, h: 10 });
withFakeDocument(
{ 'a, button, input, select, textarea, [role="button"], [onclick]': [first] },
() => {
__testing__.resetCandidateCache();
const a = host.queryCandidates("taps");
const b = host.queryCandidates("taps");
assert.equal(a, b);
},
);
});
// evaluateExtractors builds State, which references document and window.
const emptyDocument = {
querySelector: () => null,
querySelectorAll: () => [],
};
function withState(run: () => void) {
const g = globalThis as Record<string, unknown>;
const originalDocument = g.document;
const originalWindow = g.window;
g.document = emptyDocument;
g.window = {};
try {
run();
} finally {
g.document = originalDocument;
g.window = originalWindow;
}
}
test("named() sets the extractor's display name", () => {
const handle = __testing__.runtime.extract(() => "home").named("route");
const entry = __testing__.extractors.find((e) => e.handle === handle);
assert.equal(entry?.name, "route");
});
test("reading another extractor's current inside a getter throws", () => {
const first = __testing__.runtime.extract(() => 1);
let caught: Error | undefined;
__testing__.runtime.extract(() => {
try {
return first.current;
} catch (error) {
caught = error as Error;
return undefined;
}
});
withState(() => __testing__.evaluateExtractors());
assert.match(
caught?.message ?? "",
/inside another extractor is not allowed/,
);
});
// An uncaught cross-extractor read must abort evaluateExtractors loudly, exactly
// like goja's PushSnapshot. If the getter throw were swallowed, web would
// silently yield undefined and the guard would be a no-op for real authors.
test("an uncaught cross-extractor read aborts evaluateExtractors", () => {
__testing__.extractors.length = 0;
const first = __testing__.runtime.extract(() => 1);
__testing__.runtime.extract(() => first.previous);
let thrown: Error | undefined;
withState(() => {
try {
__testing__.evaluateExtractors();
} catch (error) {
thrown = error as Error;
}
});
assert.match(
thrown?.message ?? "",
/inside another extractor is not allowed/,
);
});