Files
sanderling/pkg/spec/test/web-runtime.test.ts
T
pj 26b49b379a fix ltl semantics and unify action enumeration (#71)
* fix(ltl): give every thunk a construction identity

Two distinct unnamed predicates both described as "Thunk(...)", so obligation
collapse merged their residuals and could drop a live violation. Identity is
assigned at construction and the fields are unexported, so a thunk cannot be
built without one.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(ltl): reduce a thrown-predicate residual instead of panicking

The verifier substitutes an ErrorFormula for the residual of a property whose
predicate threw, and that residual is fed back in on the next step. reduce had
no case for it, so the run crashed. It re-reports the same failure now.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(ltl): make a bounded always the dual of a bounded eventually

G<=n(f) and not F<=n(not f) disagreed on traces where the inner was still
pending when the window closed, so nnf's negation normal form was not semantics
preserving. Both sides now range over the observations at which their inner can
definitely resolve: the eventually keeps a pending inner as a disjunct, and the
always discharges vacuously at window close.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(ltl): arm a one-shot root once per run

A root that carries its own horizon is one obligation for the whole run, not one
per observation. Re-instantiating a top-level eventually monitored G F<=n(p)
instead of F<=n(p) and left one live obligation per step behind; a bounded
always restarted its window every step and never closed.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(verifier): stop wrapping a top-level eventually in always

`eventually(p).within(300, "seconds")` as a property meant "within 300 seconds
of every step", which spawned an obligation per step with its own resolved
deadline. A 553-step run carried 553 of them and serialized a 75 KB residual.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(ltl): serialize the resolved deadline of a bounded window

Two obligations spawned at different steps from one duration-bounded formula
differ only in the deadline the evaluator resolved for them, so they serialized
identically and the trace erased a distinction the evaluator makes. The authored
window stays in amount/unit; the resolved deadline rides alongside.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(verifier): split a witness's origin step from its detection step

A deferred obligation spans two steps: the one that armed it and the one whose
reduction failed. They were conflated under one index, so the extractor snapshot
(which is the detecting step's state) was reported against the origin step.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(runner): record a witness's detection step in the trace

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* feat(replay-ui): show the step a violation was detected at

The witness evidence is the detecting step's state, so say which step that is
and let a reader jump to it.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(verifier): record the extractor state the predicates actually read

On the web path extractor bodies are evaluated in V8 and injected here, but only
the goja value was replaced. The trace diff and the violation witness therefore
described a state no property ever saw.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* refactor(spec): one candidate producer over one target-eligibility rule

Both hosts routed verbs themselves and both policies enumerated their own
actions, and all four drifted. Web sent `swipes` to scrollable containers only,
so swipe-to-dismiss on a list row was reachable on native and unreachable on
web; the model policy folded gestures its own way and could not reach what the
seeded picker drew.

A host now reports facts about every element and never decides which verb may
act on it: targets.ts acceptsTarget owns that for both. pick.ts builtinCandidates
is the single enumeration, and the model policy reads it through
__sanderlingEnumerateBuiltin__ instead of reimplementing it in Go.

Gesture verbs change with it: scrolls stay vertical over scrollable containers,
swipes go free-form in all four directions from any element with real bounds.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(runner): name a builtin scroll by its drag origin

A builtin gesture carries endpoints and no selector, so every scroll rendered as
"Scroll down " in the prompt's recent-action memory and two scrollable regions
were indistinguishable.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(chrome): clear storage over cdp instead of scripting an opaque origin

Launch runs while the tab is still on about:blank, whose opaque origin denies
storage access, so localStorage.clear() threw SecurityError and every web run
died at launch. Storage.clearDataForOrigin needs no navigation. The exception
helper lands here because "Uncaught" is what hid this for so long.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(chrome): enable the swiftshader webgl fallback

Headless Chrome runs with --disable-gpu, and without this flag it refuses the
software WebGL backend: getContext returns null, so a canvas-rendered app paints
nothing and every screenshot is identical black.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* fix(web): resolve testTag through data-testid or id

Compose Multiplatform emits its testTag into the element id, which the native
table already accepts via the resource-id alias. The two web selector tables
were the only place that rejected it.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* test(spec): type-check the spec api as part of make test

The fake runtime in api.test.ts did not return a chainable handle from extract,
so the file had not type-checked since named() was added. Wiring the check into
make test stops it drifting again.

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J

* docs(manual): one-shot eventually and the gesture verbs

Claude-Session: https://claude.ai/code/session_01Fj4wJUikdABuMQEETwW55J
2026-08-12 18:06:04 +05:30

341 lines
12 KiB
TypeScript

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";
// cssEscape delegates to the browser's CSS.escape, absent in node. Install the
// WHATWG CSSOM escape algorithm so selector-builder tests exercise the real
// escaping production relies on, not a stub.
if (!(globalThis as { CSS?: unknown }).CSS) {
(globalThis as { CSS?: { escape(v: string): string } }).CSS = {
escape(value: string): string {
let out = "";
for (let i = 0; i < value.length; i++) {
const c = value.charCodeAt(i);
if (c === 0) {
out += "�";
} else if (
(c >= 0x1 && c <= 0x1f) ||
c === 0x7f ||
(i === 0 && c >= 0x30 && c <= 0x39) ||
(i === 1 && c >= 0x30 && c <= 0x39 && value.charCodeAt(0) === 0x2d)
) {
out += "\\" + c.toString(16) + " ";
} else if (i === 0 && c === 0x2d && value.length === 1) {
out += "\\" + value[i];
} else if (
c >= 0x80 ||
c === 0x2d ||
c === 0x5f ||
(c >= 0x30 && c <= 0x39) ||
(c >= 0x41 && c <= 0x5a) ||
(c >= 0x61 && c <= 0x7a)
) {
out += value[i];
} else {
out += "\\" + value[i];
}
}
return out;
},
};
}
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");
});
const { fakeElement, withFakeDocument } = await import("./web-dom-harness.ts");
// The host reports facts and never routes verbs: which of these a verb may act
// on is decided by the shared rule in src/targets.ts, exercised across both
// engines by host-parity.test.ts.
test("queryTargets reports the tappable selector set as clickable", () => {
const button = fakeElement({ tag: "button", x: 10, y: 20, width: 40, height: 8, clickable: true });
const plain = fakeElement({ tag: "div", x: 0, y: 0, width: 100, height: 100 });
withFakeDocument([button, plain], () => {
const targets = host.queryTargets();
assert.equal(targets.length, 2);
assert.equal(targets[0]!.clickable, true);
assert.deepEqual({ x: targets[0]!.x, y: targets[0]!.y }, { x: 30, y: 24 });
assert.equal(targets[1]!.clickable, false);
});
});
test("queryTargets reports only real text inputs as editable", () => {
const input = fakeElement({ tag: "input", x: 0, y: 0, width: 100, height: 20, editable: true });
const checkbox = fakeElement({ tag: "input", x: 0, y: 40, width: 20, height: 20, editable: true });
checkbox.type = "checkbox";
withFakeDocument([input, checkbox], () => {
const targets = host.queryTargets();
assert.equal(targets[0]!.editable, true);
assert.deepEqual({ x: targets[0]!.x, y: targets[0]!.y }, { x: 50, y: 10 });
assert.equal(targets[1]!.editable, false);
});
});
// A disabled control used to be dropped from every verb's candidates, because
// the web host folded `disabled` into its visibility check. It is a fact of its
// own now, so `taps` still skips it while `swipes` can still start on it, which
// is what the native host has always done.
test("queryTargets reports a disabled control rather than dropping it", () => {
const disabled = fakeElement({
tag: "button", x: 0, y: 0, width: 40, height: 20, clickable: true, disabled: true,
});
withFakeDocument([disabled], () => {
const targets = host.queryTargets();
assert.equal(targets.length, 1);
assert.equal(targets[0]!.clickable, true);
assert.equal(targets[0]!.enabled, false);
});
});
test("queryTargets caches within a tick until reset", () => {
const button = fakeElement({ tag: "button", x: 0, y: 0, width: 10, height: 10, clickable: true });
withFakeDocument([button], () => {
const first = host.queryTargets();
const second = host.queryTargets();
assert.equal(first, second);
__testing__.resetTargetCache();
assert.notEqual(host.queryTargets(), first);
});
});
// 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/,
);
});
// sanitize runs over every extractor's return value before it leaves the
// runtime. A user extractor that returns a page object reachable from
// document/window can be self-referential, carry functions, or nest deeply;
// without cycle, function, and depth guards extraction overflows the stack or
// emits non-serializable values. These exercise sanitize via the real path.
function sanitizeViaExtract(value: unknown): unknown {
__testing__.extractors.length = 0;
__testing__.runtime.extract(() => value);
let out: Record<number, unknown> = {};
withState(() => {
out = __testing__.evaluateExtractors();
});
return out[0];
}
test("sanitize breaks a self-referential cycle instead of overflowing", () => {
const cyclic: Record<string, unknown> = { name: "root" };
cyclic.self = cyclic;
const result = sanitizeViaExtract(cyclic) as Record<string, unknown>;
assert.equal(result.name, "root");
assert.equal(result.self, null);
});
test("sanitize drops function-valued properties", () => {
const result = sanitizeViaExtract({ keep: 1, fn: () => 7 }) as Record<string, unknown>;
assert.deepEqual(result, { keep: 1 });
});
test("sanitize drops a top-level function to undefined", () => {
assert.equal(sanitizeViaExtract(() => 7), undefined);
});
test("sanitize bounds recursion past its depth limit", () => {
let deep: Record<string, unknown> = { leaf: true };
for (let i = 0; i < 40; i++) deep = { next: deep };
// Walk to the depth cap; beyond it sanitize must yield null, not recurse on.
let node: unknown = sanitizeViaExtract(deep);
for (let i = 0; i < 32 && node && typeof node === "object"; i++) {
node = (node as Record<string, unknown>).next;
}
assert.equal(node, null);
});
test("sanitize preserves arrays and nested plain values", () => {
const result = sanitizeViaExtract({ items: [1, "two", { ok: true }] });
assert.deepEqual(result, { items: [1, "two", { ok: true }] });
});
// xpathStringLiteral builds XPath 1.0 string literals by hand (no escape
// syntax in XPath 1.0). A value carrying a quote that isn't wrapped or
// concat()-composed produces a malformed expression, so document.evaluate
// throws or, worse, matches the wrong node by truncating at the quote.
const { xpathStringLiteral } = __testing__;
test("xpathStringLiteral table: quote handling stays well-formed", () => {
const cases: Array<[string, string]> = [
["plain", '"plain"'],
['has"double', `'has"double'`],
["has'single", `"has'single"`],
[`both"and'`, `concat("both", '"', "and'")`],
[`"`, `'"'`],
];
for (const [input, want] of cases) {
assert.equal(xpathStringLiteral(input), want, input);
}
});
// selectorFromString routes a "kind:value" prefix; text becomes an XPath
// equality, everything else a CSS attribute selector. A value containing a
// colon must not be re-split, and a quote in a text value must reach the
// well-formed XPath literal rather than corrupting the predicate.
const { selectorFromString, selectorFromObject } = __testing__;
test("selectorFromString routes text to a normalize-space XPath", () => {
assert.deepEqual(selectorFromString("text:Hello"), {
xpath: `//*[normalize-space(text())="Hello"]`,
});
});
test("selectorFromString keeps colons in the value intact", () => {
// Only the first colon splits kind from value; the rest is the value.
assert.deepEqual(selectorFromString("text:a:b:c"), {
xpath: `//*[normalize-space(text())="a:b:c"]`,
});
});
test("selectorFromString text value with both quote kinds uses concat", () => {
assert.deepEqual(selectorFromString(`text:say "hi" o'clock`), {
xpath: `//*[normalize-space(text())=concat("say ", '"', "hi", '"', " o'clock")]`,
});
});
test("selectorFromObject escapes attribute values to prevent injection", () => {
// A quote in an id value, left unescaped, would close the attribute selector
// early and match a different element.
assert.deepEqual(selectorFromObject({ id: 'a"]' }), {
css: `[id="a\\"\\]"]`,
});
});
test("selectorFromObject maps known keys to their canonical attribute", () => {
assert.deepEqual(selectorFromObject({ testID: "submit" }), {
css: `[data-testid="submit"]`,
});
});
// Compose Multiplatform emits its testTag into `id`, which the native table
// already accepts via the resource-id alias. The web table must not be the one
// place that rejects it.
test("selectorFromObject resolves testTag through data-testid or id", () => {
assert.deepEqual(selectorFromObject({ testTag: "LoginSubmit" }), {
css: `:is([data-testid="LoginSubmit"], [id="LoginSubmit"])`,
});
});
// Multi-key selectors concatenate their parts into one compound, so the
// two-attribute testTag match has to stay a single compound piece.
test("selectorFromObject composes testTag with a second key", () => {
assert.deepEqual(selectorFromObject({ testTag: "Row", "aria-label": "first" }), {
css: `:is([data-testid="Row"], [id="Row"])[aria-label="first"]`,
});
});
test("selectorFromObject falls back to a literal attribute for unknown keys", () => {
assert.deepEqual(selectorFromObject({ "data-foo": "bar" }), {
css: `[data-foo="bar"]`,
});
});
test("selectorFromObject text-only selector becomes an XPath", () => {
assert.deepEqual(selectorFromObject({ text: "Go" }), {
xpath: `//*[normalize-space(text())="Go"]`,
});
});