Files
sanderling/internal/runner/element_extractor_trace_test.go
T
pj 11f72a722a follow-ups from the pr #73 review (#77)
* ci(folio): run gradle on jdk 21 for the metro plugin

the metro gradle plugin folio builds with publishes org.gradle.jvm.version 21
and java 21 class files, so every leg failed at the folio build on a 17
runtime. local builds pass on jdk 25, which is why only ci saw it.

* fix(build): clean pkg/spec/dist, not the dead spec-api path

* chore: point stale spec-api comments at pkg/spec

* fix(spec): publish src so an installed package carries the runtime entries

* fix(testrun): alias the installed spec package so one module graph loads

* fix(spec): export Direction, ScrollAction and LongPressAction from the entry

* docs(spec): cut the package readme to a description and doc links

* docs: say how the cli and spec package versions relate

* fix(verifier): report whether the last action was confirmed applied

Both hosts get applied: true when the runner saw the dispatch succeed and
applied: null when it could not, so an unconfirmed action stops arriving at
the spec as no action at all.

* fix(runner): an apply error leaves the action's fate unknown, not undone

A deadline that fires after the tap was dispatched leaves the effect
committed. Reporting nil made the spec see an effect with no action to cause
it, which is how the counting property convicts a healthy app.

* fix(release): stage the sidecar jar at the renamed embed path

* test(replay-ui): trace fixtures for the vacuity counts

one real green run, one run that rendered nothing, one that judges every property at least once.

* ci(replay-ui): count the steps each property judged

the exit code says no property returned false; it does not say any property was ever evaluated. this reads the trace and reports judged vs declined per property, and fails when the step page never rendered.

* test(replay-ui): cover the summary script from make test

* ci(replay-ui): summarise through the vacuity script

* docs(ci): explain the replay-ui judged/declined counts

* fix(verifier): encode element-valued extractors into the trace

An ax element exports with its find/findAll host functions attached, and
json.Marshal refuses the whole value over them: json: unsupported type:
func(goja.FunctionCall) goja.Value. The encoding failed, curr stayed nil,
and the goja hosts (ios, android) recorded null for every element-valued
extractor in both the per-step diff and the violation witness.

Apply the web host's sanitize rule before marshaling, so one rule encodes
an element on both hosts.

* test(verifier): pin element encoding to one rule on both hosts

* test(runner): assert an element reaches trace.jsonl and its witness

* feat(spec): give state.lastAction an applied field

Three states, not two: no action is a null lastAction, applied: true is an
action the runner confirmed, applied: null is one it dispatched and never
learned the fate of.

* fix(folio): do not attribute an effect to an unconfirmed action

submitChangesBalanceByTypedAmount and createdAccountHasNonZeroBalance both
convict by pinning an effect on the last action, so both decline unless the
runner saw it applied. The fixtures now say which fate they mean.

* test(folio): an unconfirmed submit belongs in the window

The count is an upper bound on the submits a window holds, so the tap that may
have landed counts and committedTransactionsExceedSubmits has nothing to
convict on.

* test(runner): a tap that lands under a failed apply is not a double submit

Drives the real folio counting predicates through the runner against a device
that commits the tap and then times out. The double-submit case is the control:
without it a green proves only that the property never fired.

* test(verifier): pin the three lastAction states on both hosts

The web page is handed the same applied field the goja object exposes, so a
property cannot read one thing on native and another on web.

* docs(spec-language): document the three lastAction states
2026-08-15 15:51:33 +05:30

122 lines
4.1 KiB
Go

package runner
import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/priyanshujain/sanderling/internal/trace"
)
// elementExtractorSpec reads a live ax element, the shape every field and
// button in examples/folio/sanderling/spec.ts is extracted with. The property
// is false the moment the field is on screen, so the run records a witness
// whose only interesting content is that element.
const elementExtractorSpec = `
import { actions, always, extract } from "@sanderling/spec";
const amountField = extract("amountField", s => s.ax.find({ "resource-id": "TxnAmountField" }));
globalThis.properties = {
noAmountField: always(() => amountField.current === undefined),
};
globalThis.actions = actions(() => []);
`
const amountFieldTreeJSON = `{
"attributes": {"resource-id": "root", "bounds": "[0,0,400,800]"},
"enabled": true,
"children": [
{"attributes": {"resource-id": "TxnAmountField", "text": "199", "bounds": "[0,100,400,160]"},
"editable": true, "enabled": true, "children": []}
]
}`
// TestRunner_TraceRecordsElementValuedExtractors is the guard on the artifact a
// person opens to decide whether a conviction is real. An element-valued
// extractor used to reach the trace as null on the goja hosts (ios, android):
// its exported value carries the element's find/findAll host functions, which
// json.Marshal refuses, so the encoding failed and both the per-step diff and
// the witness recorded nothing. A witness that reads null for the field the
// property fired on describes a state the property could not have fired in,
// which is worse than a blank.
func TestRunner_TraceRecordsElementValuedExtractors(t *testing.T) {
state := newHarnessWithSpec(t, elementExtractorSpec)
state.mock.HierarchyJSON = amountFieldTreeJSON
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
summary, err := Run(ctx, Options{
Duration: time.Hour,
IdleTimeout: 20 * time.Millisecond,
MaxSteps: 2,
Driver: state.mock,
Verifier: state.verifier,
TraceWriter: state.writer,
})
if err != nil {
t.Fatalf("Run: %v", err)
}
if !containsProperty(summary.Violations, "noAmountField") {
t.Fatalf("noAmountField did not violate, so the element never reached a predicate: %v",
summary.Violations)
}
file, err := os.Open(filepath.Join(state.writer.Directory(), "trace.jsonl"))
if err != nil {
t.Fatal(err)
}
defer file.Close()
type traceLine struct {
Step int `json:"step"`
ExtractorChanges map[string]trace.ExtractorChange `json:"extractor_changes"`
Witnesses map[string]trace.Witness `json:"witnesses"`
}
changes, witnesses := 0, 0
scanner := bufio.NewScanner(file)
scanner.Buffer(make([]byte, 0, 64*1024), 8*1024*1024)
for scanner.Scan() {
var line traceLine
if err := json.Unmarshal(scanner.Bytes(), &line); err != nil {
t.Fatalf("trace line decode: %v", err)
}
if change, ok := line.ExtractorChanges["amountField"]; ok {
changes++
assertAmountField(t, fmt.Sprintf("step %d extractor_changes", line.Step), change.Curr)
}
for name, witness := range line.Witnesses {
witnesses++
assertAmountField(t, fmt.Sprintf("step %d %s witness", line.Step, name),
witness.Extractors["amountField"])
}
}
if err := scanner.Err(); err != nil {
t.Fatalf("scan trace: %v", err)
}
if changes == 0 {
t.Error("amountField never appears in extractor_changes; the element the run read is not in the trace")
}
if witnesses == 0 {
t.Error("no witness reached the trace; nothing was compared")
}
}
// assertAmountField reads the recorded element the way a person opening the
// trace would: the field's text is the number the property was judged on.
func assertAmountField(t *testing.T, where string, recorded json.RawMessage) {
t.Helper()
var element struct {
Text string `json:"text"`
}
if err := json.Unmarshal(recorded, &element); err != nil {
t.Fatalf("%s: decode %s: %v", where, recorded, err)
}
if element.Text != "199" {
t.Errorf("%s: recorded element is %s, want its text to read 199", where, recorded)
}
}