mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 11:07:10 +00:00
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).
This commit is contained in:
40 files changed
+2980
-262
No files matched your search
@@ -9,6 +9,10 @@
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
},
|
||||
"./defaults/properties": {
|
||||
"types": "./dist/defaults/properties.d.ts",
|
||||
"import": "./dist/defaults/properties.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
|
||||
+45
-11
@@ -3,7 +3,13 @@ import type {
|
||||
Action,
|
||||
ActionGenerator,
|
||||
InputTextAction,
|
||||
Key,
|
||||
Point,
|
||||
PressKeyAction,
|
||||
Sampler,
|
||||
SwipeAction,
|
||||
TapAction,
|
||||
WaitAction,
|
||||
WeightedEntry,
|
||||
} from "./types.ts";
|
||||
|
||||
@@ -15,22 +21,50 @@ export function weighted(...entries: WeightedEntry[]): ActionGenerator {
|
||||
return globalThis.__uatu__.weighted(...entries);
|
||||
}
|
||||
|
||||
export function from<T>(items: readonly T[]): Sampler<T> {
|
||||
return globalThis.__uatu__.from(items);
|
||||
}
|
||||
|
||||
export function Tap(parameters: { on: string | AccessibilityElement }): TapAction {
|
||||
return globalThis.__uatu__.tap(parameters);
|
||||
}
|
||||
|
||||
export function InputText(parameters: { into: string | AccessibilityElement; text: string }): InputTextAction {
|
||||
export function InputText(parameters: {
|
||||
into: string | AccessibilityElement;
|
||||
text: string;
|
||||
}): InputTextAction {
|
||||
return globalThis.__uatu__.inputText(parameters);
|
||||
}
|
||||
|
||||
export const taps: ActionGenerator = new Proxy({} as ActionGenerator, {
|
||||
get(_target, property) {
|
||||
return (globalThis.__uatu__.taps as unknown as Record<string | symbol, unknown>)[property];
|
||||
},
|
||||
});
|
||||
export function Swipe(parameters: {
|
||||
from: Point | AccessibilityElement;
|
||||
to: Point | AccessibilityElement;
|
||||
durationMillis?: number;
|
||||
}): SwipeAction {
|
||||
return globalThis.__uatu__.swipe(parameters);
|
||||
}
|
||||
|
||||
export const swipes: ActionGenerator = new Proxy({} as ActionGenerator, {
|
||||
get(_target, property) {
|
||||
return (globalThis.__uatu__.swipes as unknown as Record<string | symbol, unknown>)[property];
|
||||
},
|
||||
});
|
||||
export function PressKey(parameters: { key: Key }): PressKeyAction {
|
||||
return globalThis.__uatu__.pressKey(parameters);
|
||||
}
|
||||
|
||||
export function Wait(parameters: { durationMillis: number }): WaitAction {
|
||||
return globalThis.__uatu__.wait(parameters);
|
||||
}
|
||||
|
||||
function builtinGenerator(name: "taps" | "swipes" | "waitOnce" | "pressKeys"): ActionGenerator {
|
||||
return new Proxy({} as ActionGenerator, {
|
||||
get(_target, property) {
|
||||
const runtime = globalThis.__uatu__[name] as unknown as Record<
|
||||
string | symbol,
|
||||
unknown
|
||||
>;
|
||||
return runtime[property];
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export const taps: ActionGenerator = builtinGenerator("taps");
|
||||
export const swipes: ActionGenerator = builtinGenerator("swipes");
|
||||
export const waitOnce: ActionGenerator = builtinGenerator("waitOnce");
|
||||
export const pressKey: ActionGenerator = builtinGenerator("pressKeys");
|
||||
@@ -0,0 +1,21 @@
|
||||
import { always } from "../ltl.ts";
|
||||
import { extract } from "../extract.ts";
|
||||
import type { Formula } from "../types.ts";
|
||||
|
||||
const exceptionCount = extract<number>((state) => state.exceptions.length);
|
||||
|
||||
// Fails when the SDK captured an uncaught throwable or a Uatu.reportError
|
||||
// call surfaced one during the run.
|
||||
export const noUncaughtExceptions: Formula = always(
|
||||
() => exceptionCount.current === 0,
|
||||
);
|
||||
|
||||
const errorLogCount = extract<number>(
|
||||
(state) => state.logs.reduce((count, log) => count + (log.level === "E" ? 1 : 0), 0),
|
||||
);
|
||||
|
||||
// Fails when the runner's logcat fetch observed any error-level lines since
|
||||
// the previous step.
|
||||
export const noLogcatErrors: Formula = always(
|
||||
() => errorLogCount.current === 0,
|
||||
);
|
||||
@@ -3,15 +3,38 @@ export type {
|
||||
AccessibilityTree,
|
||||
Action,
|
||||
ActionGenerator,
|
||||
EventuallyFormula,
|
||||
ExceptionRecord,
|
||||
Extracted,
|
||||
Formula,
|
||||
InputTextAction,
|
||||
Key,
|
||||
LogEntry,
|
||||
Point,
|
||||
PressKeyAction,
|
||||
Sampler,
|
||||
Snapshots,
|
||||
State,
|
||||
SwipeAction,
|
||||
TapAction,
|
||||
UatuRuntime,
|
||||
WaitAction,
|
||||
WeightedEntry,
|
||||
} from "./types.ts";
|
||||
|
||||
export { extract } from "./extract.ts";
|
||||
export { always } from "./ltl.ts";
|
||||
export { Tap, InputText, actions, weighted, taps, swipes } from "./actions.ts";
|
||||
export { always, eventually, next, now } from "./ltl.ts";
|
||||
export {
|
||||
InputText,
|
||||
PressKey,
|
||||
Swipe,
|
||||
Tap,
|
||||
Wait,
|
||||
actions,
|
||||
from,
|
||||
pressKey,
|
||||
swipes,
|
||||
taps,
|
||||
waitOnce,
|
||||
weighted,
|
||||
} from "./actions.ts";
|
||||
+18
-3
@@ -1,5 +1,20 @@
|
||||
import type { Formula } from "./types.ts";
|
||||
import type { EventuallyFormula, Formula } from "./types.ts";
|
||||
|
||||
export function always(predicate: () => boolean): Formula {
|
||||
return globalThis.__uatu__.always(predicate);
|
||||
export function always(predicateOrFormula: (() => boolean) | Formula): Formula {
|
||||
return globalThis.__uatu__.always(predicateOrFormula);
|
||||
}
|
||||
|
||||
export function now(predicate: () => boolean): Formula {
|
||||
return globalThis.__uatu__.now(predicate);
|
||||
}
|
||||
|
||||
export function next(predicate: () => boolean): Formula {
|
||||
return globalThis.__uatu__.next(predicate);
|
||||
}
|
||||
|
||||
// An unbounded `eventually` never forces a violation within a finite run —
|
||||
// prefer `.within(n, unit)` when you want the verifier to fail a property
|
||||
// that stalls.
|
||||
export function eventually(predicate: () => boolean): EventuallyFormula {
|
||||
return globalThis.__uatu__.eventually(predicate);
|
||||
}
|
||||
@@ -4,6 +4,8 @@ export interface AccessibilityElement {
|
||||
id?: string;
|
||||
text?: string;
|
||||
bounds?: { left: number; top: number; right: number; bottom: number };
|
||||
x?: number;
|
||||
y?: number;
|
||||
}
|
||||
|
||||
export interface AccessibilityTree {
|
||||
@@ -11,9 +13,27 @@ export interface AccessibilityTree {
|
||||
findAll(selector: string): AccessibilityElement[];
|
||||
}
|
||||
|
||||
export interface LogEntry {
|
||||
unixMillis: number;
|
||||
level: string;
|
||||
tag: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface ExceptionRecord {
|
||||
class: string;
|
||||
message: string;
|
||||
stackTrace: string;
|
||||
unixMillis?: number;
|
||||
}
|
||||
|
||||
export interface State {
|
||||
snapshots: Snapshots;
|
||||
ax: AccessibilityTree;
|
||||
lastAction: Action | null;
|
||||
time: number;
|
||||
logs: readonly LogEntry[];
|
||||
exceptions: readonly ExceptionRecord[];
|
||||
}
|
||||
|
||||
export interface Extracted<T> {
|
||||
@@ -21,9 +41,41 @@ export interface Extracted<T> {
|
||||
readonly previous: T | undefined;
|
||||
}
|
||||
|
||||
export interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
export type TapAction = { kind: "Tap"; on: string | AccessibilityElement };
|
||||
export type InputTextAction = { kind: "InputText"; into: string | AccessibilityElement; text: string };
|
||||
export type Action = TapAction | InputTextAction;
|
||||
export type InputTextAction = {
|
||||
kind: "InputText";
|
||||
into: string | AccessibilityElement;
|
||||
text: string;
|
||||
};
|
||||
export type SwipeAction = {
|
||||
kind: "Swipe";
|
||||
from: Point | AccessibilityElement;
|
||||
to: Point | AccessibilityElement;
|
||||
durationMillis?: number;
|
||||
};
|
||||
export type PressKeyAction = { kind: "PressKey"; key: Key };
|
||||
export type WaitAction = { kind: "Wait"; durationMillis: number };
|
||||
export type Action =
|
||||
| TapAction
|
||||
| InputTextAction
|
||||
| SwipeAction
|
||||
| PressKeyAction
|
||||
| WaitAction;
|
||||
|
||||
export type Key =
|
||||
| "back"
|
||||
| "home"
|
||||
| "enter"
|
||||
| "tab"
|
||||
| "up"
|
||||
| "down"
|
||||
| "left"
|
||||
| "right";
|
||||
|
||||
export interface ActionGenerator {
|
||||
readonly __uatuActionGenerator: true;
|
||||
@@ -32,17 +84,45 @@ export interface ActionGenerator {
|
||||
|
||||
export interface Formula {
|
||||
readonly __uatuFormula: true;
|
||||
implies(other: Formula): Formula;
|
||||
or(other: Formula): Formula;
|
||||
and(other: Formula): Formula;
|
||||
not(): Formula;
|
||||
}
|
||||
|
||||
export interface EventuallyFormula extends Formula {
|
||||
within(amount: number, unit: "milliseconds" | "seconds" | "steps"): Formula;
|
||||
}
|
||||
|
||||
export interface Sampler<T> {
|
||||
generate(): T;
|
||||
}
|
||||
|
||||
export interface UatuRuntime {
|
||||
extract: <T>(getter: (state: State) => T) => Extracted<T>;
|
||||
always: (predicate: () => boolean) => Formula;
|
||||
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;
|
||||
inputText: (parameters: { into: string | AccessibilityElement; text: string }) => InputTextAction;
|
||||
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;
|
||||
swipes: ActionGenerator;
|
||||
waitOnce: ActionGenerator;
|
||||
pressKeys: ActionGenerator;
|
||||
}
|
||||
|
||||
export type WeightedEntry = readonly [number, ActionGenerator];
|
||||
|
||||
+188
-15
@@ -1,38 +1,127 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
import { Tap, InputText, actions, always, extract, swipes, taps, weighted } from "../src/index.ts";
|
||||
import {
|
||||
InputText,
|
||||
PressKey,
|
||||
Swipe,
|
||||
Tap,
|
||||
Wait,
|
||||
actions,
|
||||
always,
|
||||
eventually,
|
||||
extract,
|
||||
from,
|
||||
next,
|
||||
now,
|
||||
pressKey,
|
||||
swipes,
|
||||
taps,
|
||||
waitOnce,
|
||||
weighted,
|
||||
} from "../src/index.ts";
|
||||
import type {
|
||||
AccessibilityElement,
|
||||
Action,
|
||||
ActionGenerator,
|
||||
EventuallyFormula,
|
||||
Extracted,
|
||||
Formula,
|
||||
Sampler,
|
||||
State,
|
||||
UatuRuntime,
|
||||
WeightedEntry,
|
||||
} from "../src/types.ts";
|
||||
|
||||
function installFakeRuntime(): UatuRuntime & {
|
||||
interface RecordedRuntime extends UatuRuntime {
|
||||
extracts: Array<(state: State) => unknown>;
|
||||
alwaysPredicates: Array<() => boolean>;
|
||||
alwaysArgs: Array<(() => boolean) | Formula>;
|
||||
nowPredicates: Array<() => boolean>;
|
||||
nextPredicates: Array<() => boolean>;
|
||||
eventuallyPredicates: Array<() => boolean>;
|
||||
withinCalls: Array<{ amount: number; unit: string }>;
|
||||
impliesCalls: number;
|
||||
orCalls: number;
|
||||
andCalls: number;
|
||||
notCalls: number;
|
||||
actionGenerators: Array<() => Action[]>;
|
||||
weightedCalls: WeightedEntry[][];
|
||||
} {
|
||||
fromCalls: unknown[][];
|
||||
}
|
||||
|
||||
function makeChainableFormula(record: RecordedRuntime): Formula {
|
||||
const formula: Formula = {
|
||||
__uatuFormula: true,
|
||||
implies(other: Formula): Formula {
|
||||
record.impliesCalls++;
|
||||
void other;
|
||||
return makeChainableFormula(record);
|
||||
},
|
||||
or(other: Formula): Formula {
|
||||
record.orCalls++;
|
||||
void other;
|
||||
return makeChainableFormula(record);
|
||||
},
|
||||
and(other: Formula): Formula {
|
||||
record.andCalls++;
|
||||
void other;
|
||||
return makeChainableFormula(record);
|
||||
},
|
||||
not(): Formula {
|
||||
record.notCalls++;
|
||||
return makeChainableFormula(record);
|
||||
},
|
||||
};
|
||||
return formula;
|
||||
}
|
||||
|
||||
function makeChainableEventually(record: RecordedRuntime): EventuallyFormula {
|
||||
const base = makeChainableFormula(record);
|
||||
return {
|
||||
...base,
|
||||
within(amount, unit) {
|
||||
record.withinCalls.push({ amount, unit });
|
||||
return makeChainableFormula(record);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function installFakeRuntime(): RecordedRuntime {
|
||||
const calls = {
|
||||
extracts: [] as Array<(state: State) => unknown>,
|
||||
alwaysPredicates: [] as Array<() => boolean>,
|
||||
alwaysArgs: [] as Array<(() => boolean) | Formula>,
|
||||
nowPredicates: [] as Array<() => boolean>,
|
||||
nextPredicates: [] as Array<() => boolean>,
|
||||
eventuallyPredicates: [] as Array<() => boolean>,
|
||||
withinCalls: [] as Array<{ amount: number; unit: string }>,
|
||||
impliesCalls: 0,
|
||||
orCalls: 0,
|
||||
andCalls: 0,
|
||||
notCalls: 0,
|
||||
actionGenerators: [] as Array<() => Action[]>,
|
||||
weightedCalls: [] as WeightedEntry[][],
|
||||
fromCalls: [] as unknown[][],
|
||||
};
|
||||
const runtime: UatuRuntime = {
|
||||
const runtime = {
|
||||
extract: <T>(getter: (state: State) => T): Extracted<T> => {
|
||||
calls.extracts.push(getter as (state: State) => unknown);
|
||||
return { current: undefined as unknown as T, previous: undefined };
|
||||
},
|
||||
always: (predicate: () => boolean): Formula => {
|
||||
calls.alwaysPredicates.push(predicate);
|
||||
return { __uatuFormula: true };
|
||||
always: (predicateOrFormula: (() => boolean) | Formula): Formula => {
|
||||
calls.alwaysArgs.push(predicateOrFormula);
|
||||
return makeChainableFormula(recorded);
|
||||
},
|
||||
now: (predicate: () => boolean): Formula => {
|
||||
calls.nowPredicates.push(predicate);
|
||||
return makeChainableFormula(recorded);
|
||||
},
|
||||
next: (predicate: () => boolean): Formula => {
|
||||
calls.nextPredicates.push(predicate);
|
||||
return makeChainableFormula(recorded);
|
||||
},
|
||||
eventually: (predicate: () => boolean): EventuallyFormula => {
|
||||
calls.eventuallyPredicates.push(predicate);
|
||||
return makeChainableEventually(recorded);
|
||||
},
|
||||
actions: (generator: () => Action[]): ActionGenerator => {
|
||||
calls.actionGenerators.push(generator);
|
||||
@@ -42,13 +131,28 @@ function installFakeRuntime(): UatuRuntime & {
|
||||
calls.weightedCalls.push(entries);
|
||||
return { __uatuActionGenerator: 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 }),
|
||||
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: { __uatuActionGenerator: true, generate: () => [] },
|
||||
swipes: { __uatuActionGenerator: true, generate: () => [] },
|
||||
};
|
||||
globalThis.__uatu__ = runtime;
|
||||
return Object.assign(runtime, calls);
|
||||
waitOnce: { __uatuActionGenerator: true, generate: () => [] },
|
||||
pressKeys: { __uatuActionGenerator: true, generate: () => [] },
|
||||
} satisfies UatuRuntime;
|
||||
const recorded = Object.assign(runtime, calls) as RecordedRuntime;
|
||||
globalThis.__uatu__ = recorded;
|
||||
return recorded;
|
||||
}
|
||||
|
||||
test("extract forwards the getter to the runtime", () => {
|
||||
@@ -63,10 +167,48 @@ test("always wraps a predicate into a formula via the runtime", () => {
|
||||
const runtime = installFakeRuntime();
|
||||
const predicate = () => true;
|
||||
const formula = always(predicate);
|
||||
assert.equal(runtime.alwaysPredicates[0], predicate);
|
||||
assert.equal(runtime.alwaysArgs[0], predicate);
|
||||
assert.equal(formula.__uatuFormula, 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.__uatuFormula, true);
|
||||
});
|
||||
|
||||
test("now/next/eventually forward predicates", () => {
|
||||
const runtime = installFakeRuntime();
|
||||
const p1 = () => true;
|
||||
const p2 = () => false;
|
||||
const p3 = () => true;
|
||||
now(p1);
|
||||
next(p2);
|
||||
eventually(p3);
|
||||
assert.equal(runtime.nowPredicates[0], p1);
|
||||
assert.equal(runtime.nextPredicates[0], p2);
|
||||
assert.equal(runtime.eventuallyPredicates[0], p3);
|
||||
});
|
||||
|
||||
test("eventually().within forwards unit and amount", () => {
|
||||
const runtime = installFakeRuntime();
|
||||
eventually(() => true).within(3, "seconds");
|
||||
assert.deepEqual(runtime.withinCalls[0], { amount: 3, unit: "seconds" });
|
||||
});
|
||||
|
||||
test("formula chaining exposes implies/or/and/not", () => {
|
||||
const runtime = installFakeRuntime();
|
||||
const a = now(() => true);
|
||||
const b = now(() => false);
|
||||
a.implies(b).or(b).and(b).not();
|
||||
assert.equal(runtime.impliesCalls, 1);
|
||||
assert.equal(runtime.orCalls, 1);
|
||||
assert.equal(runtime.andCalls, 1);
|
||||
assert.equal(runtime.notCalls, 1);
|
||||
});
|
||||
|
||||
test("Tap returns a TapAction with the supplied selector", () => {
|
||||
installFakeRuntime();
|
||||
const action = Tap({ on: "id:login_continue" });
|
||||
@@ -87,6 +229,29 @@ test("InputText returns an InputTextAction", () => {
|
||||
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("PressKey returns a PressKeyAction", () => {
|
||||
installFakeRuntime();
|
||||
const action = PressKey({ key: "back" });
|
||||
assert.deepEqual(action, { kind: "PressKey", key: "back" });
|
||||
});
|
||||
|
||||
test("Wait returns a WaitAction", () => {
|
||||
installFakeRuntime();
|
||||
const action = Wait({ durationMillis: 500 });
|
||||
assert.deepEqual(action, { kind: "Wait", durationMillis: 500 });
|
||||
});
|
||||
|
||||
test("actions wraps a generator into the runtime's ActionGenerator", () => {
|
||||
const runtime = installFakeRuntime();
|
||||
const generator = () => [Tap({ on: "id:x" })];
|
||||
@@ -105,9 +270,17 @@ test("weighted forwards weighted entries to the runtime", () => {
|
||||
assert.deepEqual(runtime.weightedCalls[0], entries);
|
||||
});
|
||||
|
||||
test("taps and swipes proxy through to the runtime defaults", () => {
|
||||
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("default generators proxy through to the runtime", () => {
|
||||
installFakeRuntime();
|
||||
assert.equal(taps.__uatuActionGenerator, true);
|
||||
assert.equal(swipes.__uatuActionGenerator, true);
|
||||
assert.equal(typeof taps.generate, "function");
|
||||
assert.equal(waitOnce.__uatuActionGenerator, true);
|
||||
assert.equal(pressKey.__uatuActionGenerator, true);
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
import type {
|
||||
Action,
|
||||
ActionGenerator,
|
||||
EventuallyFormula,
|
||||
Extracted,
|
||||
Formula,
|
||||
Sampler,
|
||||
State,
|
||||
UatuRuntime,
|
||||
WeightedEntry,
|
||||
} from "../src/types.ts";
|
||||
|
||||
interface RecordedRuntime extends UatuRuntime {
|
||||
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 { __uatuFormula: true } as Formula;
|
||||
},
|
||||
now: () => ({ __uatuFormula: true } as Formula),
|
||||
next: () => ({ __uatuFormula: true } as Formula),
|
||||
eventually: () => ({ __uatuFormula: true } as EventuallyFormula),
|
||||
actions: (generator: () => Action[]): ActionGenerator => ({
|
||||
__uatuActionGenerator: true,
|
||||
generate: generator,
|
||||
}),
|
||||
weighted: (..._entries: WeightedEntry[]): ActionGenerator => ({
|
||||
__uatuActionGenerator: true,
|
||||
generate: () => [],
|
||||
}),
|
||||
from: <T>(_items: readonly T[]): Sampler<T> => ({ generate: () => _items[0] as T }),
|
||||
tap: ({ on }) => ({ kind: "Tap", on }),
|
||||
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: { __uatuActionGenerator: true, generate: () => [] } as ActionGenerator,
|
||||
swipes: { __uatuActionGenerator: true, generate: () => [] } as ActionGenerator,
|
||||
waitOnce: { __uatuActionGenerator: true, generate: () => [] } as ActionGenerator,
|
||||
pressKeys: { __uatuActionGenerator: true, generate: () => [] } as ActionGenerator,
|
||||
} satisfies UatuRuntime;
|
||||
|
||||
const state = { currentState: initialState };
|
||||
const recorded = Object.assign(runtime, {
|
||||
currentState: initialState,
|
||||
extractors,
|
||||
alwaysArgs,
|
||||
get lastPredicate() {
|
||||
return lastPredicate;
|
||||
},
|
||||
}) as unknown as RecordedRuntime;
|
||||
globalThis.__uatu__ = 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;
|
||||
}
|
||||
|
||||
const emptyState: State = {
|
||||
snapshots: {},
|
||||
ax: { find: () => undefined, findAll: () => [] },
|
||||
lastAction: null,
|
||||
time: 0,
|
||||
logs: [],
|
||||
exceptions: [],
|
||||
};
|
||||
|
||||
test("defaults bundle exports formulas tagged as LTL properties", async () => {
|
||||
installRuntime({
|
||||
...emptyState,
|
||||
logs: [{ unixMillis: 1, level: "W", tag: "X", message: "warn" }],
|
||||
});
|
||||
const defaults = await import("../src/defaults/properties.ts");
|
||||
assert.equal(defaults.noUncaughtExceptions.__uatuFormula, true);
|
||||
assert.equal(defaults.noLogcatErrors.__uatuFormula, true);
|
||||
});
|
||||
Reference in new issue
Block a user