Files
sanderling/internal/ltl/formula_test.go
T
pj 7493945251 feat: LTL operators, sampling, and default generators (#17)
* feat(ltl): add Now/Next/Eventually/Implies/Or/And/Not formulas

Replace the fold-with-latch evaluator with a residual-formula reducer.
Each Observe() instantiates a fresh obligation from the root (stripping
an outer Always), reduces each pending obligation against current state,
latches Violated on first failure, and surfaces Pending verdicts for
deferred obligations. Existing Always/Pure/Thunk tests continue to pass.

* feat(ltl): support relative duration for eventually().within()

* feat(proto): add Swipe, PressKey, RecentLogs RPCs

* feat(verifier,runner): formula handles, new action kinds, rich state

- verifier: add formula-spec registry; bindNow/bindNext/bindEventually with
  chainable .implies/.or/.and/.not and .within(n,unit) on eventually; bindFrom
  for uniform sampling. bindAlways keeps accepting plain predicates.
- verifier: store lastTree, lastAction, step time, logs, exceptions on the
  Verifier; SnapshotInput replaces the (snapshots, tree) pair. stateObject now
  produces state.lastAction/time/logs/exceptions matching the TS State type.
- verifier: make taps/swipes/waitOnce/pressKey built-in generators actually
  fire; taps picks a clickable, enabled element from the last hierarchy.
- agent: add exceptions field to Message wire format.
- driver: add Swipe/PressKey/RecentLogs to Driver interface; wire maestro
  client and mock driver. LogEntry exposed for runner consumption.
- runner: apply Swipe/PressKey/Wait actions; collect logcat and exceptions;
  pass lastAction and step time into PushSnapshot.

* feat(spec-api): LTL operators, new actions, richer State

- ltl.ts exports now/next/eventually; always overload accepts a Formula
- types.ts: Formula gains implies/or/and/not; EventuallyFormula adds .within;
  State gains lastAction/time/logs/exceptions; Swipe/PressKey/Wait action types
- actions.ts: Swipe/PressKey/Wait/from constructors; waitOnce + pressKey
  default generators
- tests exercise the chaining, sampling, and new actions through a recorded
  fake runtime

* feat(sidecar): add swipe, pressKey, recentLogs RPC handlers

* feat(sdk-android): capture uncaught exceptions

Install a default uncaught handler on Uatu.start, chained with any
existing handler so Android's crash reporter still runs. Expose
Uatu.reportError for callers to forward caught throwables. A bounded
circular buffer (default 50) drains into each STATE message's new
exceptions field. Protocol.kt serializes/deserializes the field,
matching the Go wire format added to internal/agent/protocol.go.

* feat(spec-api): add @uatu/spec/defaults/properties bundle

* feat(sample-app): exercise new LTL operators + defaults

spec.ts now imports eventually/next/now/from from @uatu/spec and
noUncaughtExceptions from @uatu/spec/defaults/properties. It declares
three properties that exercise the new surface:

- accountCountNonNegative: plain always() safety
- addAccountAdvances: always(now(x).implies(next(y)))
- eventuallyLoggedIn: eventually(p).within(30, "seconds")
- noUncaughtExceptions: imported default

The weighted actions root uses from() for random phone/name sampling
and entries for taps/swipes/waitOnce/pressKey built-ins.

SampleApplication gains a debug hook gated on the system property
uatu.inject_error so the e2e run can synthesize an Uatu.reportError and
verify noUncaughtExceptions violates.

cmd/uatu/test_run.go adds a subpath alias so specs importing
"@uatu/spec/defaults/properties" resolve against the in-tree source
when running from the uatu checkout. The spec-integration tests swap
the old click-counter fixtures for the new login hierarchy.

* feat(trace): record swipe/key/wait details + exceptions

trace.Step gains an Exceptions array so the trace captures the
class/message/stackTrace for each SDK-reported throwable in a step.
trace.Action gains FromX/FromY/ToX/ToY/Key/DurationMillis so the full
payload of Swipe/PressKey/Wait actions is visible in trace.jsonl.

sample-app's debug error hook now gates on ApplicationInfo.DEBUGGABLE
instead of a system property (adb setprop fails on non-rooted
emulators).
2026-04-20 02:19:39 +07:00

194 lines
6.2 KiB
Go

package ltl
import (
"strings"
"testing"
"time"
)
func TestDescribe_NowNextEventually(t *testing.T) {
now := Always(Now(Pure(true)))
if got := Describe(now); !strings.Contains(got, "Now") || !strings.Contains(got, "Always") {
t.Errorf("Describe(Always(Now(Pure(true)))) = %q", got)
}
next := Always(Next(Pure(false)))
if got := Describe(next); !strings.Contains(got, "Next") {
t.Errorf("Describe next = %q", got)
}
ev := Always(EventuallyWithinSteps(Pure(true), 3))
if got := Describe(ev); !strings.Contains(got, "Eventually") || !strings.Contains(got, "steps=3") {
t.Errorf("Describe eventually = %q", got)
}
}
func TestDescribe_ImpliesOrAndNot(t *testing.T) {
implies := Implies(Pure(true), Pure(false))
if got := Describe(implies); !strings.Contains(got, "Implies") {
t.Errorf("Describe implies = %q", got)
}
or := Or(Pure(true), Pure(false))
if got := Describe(or); !strings.Contains(got, "Or") {
t.Errorf("Describe or = %q", got)
}
and := And(Pure(true), Pure(false))
if got := Describe(and); !strings.Contains(got, "And") {
t.Errorf("Describe and = %q", got)
}
not := Not(Pure(true))
if got := Describe(not); !strings.Contains(got, "Not") {
t.Errorf("Describe not = %q", got)
}
}
func TestAlways_Now_ViolatesImmediately(t *testing.T) {
evaluator := NewEvaluator(Always(Now(Pure(false))))
if got := evaluator.Observe(); got != VerdictViolated {
t.Errorf("step 1: got %v, want violated", got)
}
}
func TestAlways_Next_PendingThenViolated(t *testing.T) {
y := true
evaluator := NewEvaluator(Always(Next(Thunk(func() bool { return y }))))
if got := evaluator.Observe(); got != VerdictPending {
t.Errorf("step 1: got %v, want pending", got)
}
y = false
if got := evaluator.Observe(); got != VerdictViolated {
t.Errorf("step 2: got %v, want violated", got)
}
}
func TestAlways_Next_StaysPendingWhileInnerHolds(t *testing.T) {
evaluator := NewEvaluator(Always(Next(Thunk(func() bool { return true }))))
for index := range 3 {
if got := evaluator.ObserveAt(time.Unix(int64(index), 0)); got != VerdictPending {
t.Errorf("step %d: got %v, want pending", index+1, got)
}
}
}
func TestAlways_NowImpliesEventuallyWithin_ViolatesWhenYLate(t *testing.T) {
// always(now(() => x).implies(eventually(() => y).within(3, "steps")))
// x = true only at step 1; y = true only at step 4.
xValues := []bool{true, false, false, false, false}
yValues := []bool{false, false, false, true, true}
step := 0
predX := Thunk(func() bool { return xValues[step] })
predY := Thunk(func() bool { return yValues[step] })
formula := Always(Implies(Now(predX), EventuallyWithinSteps(predY, 3)))
evaluator := NewEvaluator(formula)
verdicts := make([]Verdict, 0, 5)
for range 5 {
verdicts = append(verdicts, evaluator.Observe())
step++
}
// Step 1: X true, eventually(Y, 3) spawned pending. Pending.
// Step 2: pending eventually decrements (Y false). Pending.
// Step 3: eventually bound exhausted (Y still false). Violated.
if verdicts[0] != VerdictPending {
t.Errorf("step 1: got %v, want pending", verdicts[0])
}
if verdicts[1] != VerdictPending {
t.Errorf("step 2: got %v, want pending", verdicts[1])
}
if verdicts[2] != VerdictViolated {
t.Errorf("step 3: got %v, want violated", verdicts[2])
}
}
func TestAlways_NowImpliesEventuallyWithin_HoldsWhenYInBound(t *testing.T) {
// Same formula, y = true at step 3 (within the 3-step bound).
xValues := []bool{true, false, false}
yValues := []bool{false, false, true}
step := 0
predX := Thunk(func() bool { return xValues[step] })
predY := Thunk(func() bool { return yValues[step] })
formula := Always(Implies(Now(predX), EventuallyWithinSteps(predY, 3)))
evaluator := NewEvaluator(formula)
verdicts := make([]Verdict, 0, 3)
for range 3 {
verdicts = append(verdicts, evaluator.Observe())
step++
}
if verdicts[0] != VerdictPending {
t.Errorf("step 1: got %v, want pending", verdicts[0])
}
if verdicts[1] != VerdictPending {
t.Errorf("step 2: got %v, want pending", verdicts[1])
}
if verdicts[2] != VerdictHolds {
t.Errorf("step 3: got %v, want holds", verdicts[2])
}
}
func TestEventually_DeadlineViolation(t *testing.T) {
base := time.Unix(0, 0)
deadline := base.Add(1 * time.Second)
formula := Always(EventuallyBefore(Pure(false), deadline))
evaluator := NewEvaluator(formula)
// Well before deadline: pending.
if got := evaluator.ObserveAt(base.Add(100 * time.Millisecond)); got != VerdictPending {
t.Errorf("pre-deadline: got %v, want pending", got)
}
// At or past deadline: violated.
if got := evaluator.ObserveAt(base.Add(2 * time.Second)); got != VerdictViolated {
t.Errorf("post-deadline: got %v, want violated", got)
}
}
func TestEventually_RelativeDurationResolvesOnFirstReduce(t *testing.T) {
base := time.Unix(0, 0)
// One-shot Eventually (not wrapped in Always) with a 1s relative deadline.
evaluator := NewEvaluator(EventuallyWithin(Pure(false), 1*time.Second))
if got := evaluator.ObserveAt(base); got != VerdictPending {
t.Errorf("creation step: got %v, want pending", got)
}
if got := evaluator.ObserveAt(base.Add(500 * time.Millisecond)); got != VerdictPending {
t.Errorf("mid-window: got %v, want pending", got)
}
if got := evaluator.ObserveAt(base.Add(2 * time.Second)); got != VerdictViolated {
t.Errorf("past-window: got %v, want violated", got)
}
}
func TestOr_OneBranchHolds(t *testing.T) {
evaluator := NewEvaluator(Always(Or(Pure(false), Pure(true))))
if got := evaluator.Observe(); got != VerdictHolds {
t.Errorf("or(false,true): got %v, want holds", got)
}
}
func TestAnd_OneBranchViolatesLatches(t *testing.T) {
evaluator := NewEvaluator(Always(And(Pure(true), Pure(false))))
if got := evaluator.Observe(); got != VerdictViolated {
t.Errorf("and(true,false): got %v, want violated", got)
}
}
func TestNot_InvertsPure(t *testing.T) {
holds := NewEvaluator(Always(Not(Pure(false))))
if got := holds.Observe(); got != VerdictHolds {
t.Errorf("not(false): got %v, want holds", got)
}
violates := NewEvaluator(Always(Not(Pure(true))))
if got := violates.Observe(); got != VerdictViolated {
t.Errorf("not(true): got %v, want violated", got)
}
}
func TestVerdict_StringPending(t *testing.T) {
if got := VerdictPending.String(); got != "pending" {
t.Errorf("VerdictPending.String() = %q", got)
}
}