mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 19:17:10 +00:00
* feat(runner): stop the step loop at the first violation on request
* feat(testrun): report violations as a typed error under exit-on-violation
* feat(cli): add --exit-on-violation and exit 2 when it fires
* docs(cli): document --exit-on-violation, --max-steps, and exit codes
* fix(web): enumerate and query across shadow roots in both producers
* test(chrome): compare both producers on a shadow-dom parity page
* test(browser): drive a canvas-under-shadow-root fixture end to end
* fix(web): select the focused field inside a shadow root before typing
* fix(web): report the pathname as the screen when there is no hash route
* fix(web): settle on dom quiescence instead of returning at body ready
* feat(replay-ui): add data-testid hooks the dogfood spec drives
* feat(replay-ui): add the dogfood spec sanderling runs against the replay ui
* fix(replay-ui): scope the screenshot property to the named state panel
* chore(make): add per-platform sanderling build targets
* ci: add dispatch workflows for folio and the replay ui
* docs: describe the dispatch workflows and how to read a failure
* ci(folio): give the ios leg its jdk, android sdk, just, and a clean app start
* refactor(web): use max for the settle budget
* ci: pin calibrated seeds, skip the flaky ios reinstall, bound every job
* ci: authenticate and pin the buf setup step
the anonymous release download hit the shared runner ip rate limit and
failed the job with 'socket hang up' after three retries.
* docs: record that canvas apps need a dom proxy to be text-fuzzable
* fix(ios): bound lifecycle rpcs and claim the target device
a launch the simulator rejects sent the xctest session into a recovery
chain that answered minutes late or never, and the rpc had no deadline,
so the run hung with no trace and no error. also take a per-udid flock:
a second run's reinstall lands under the first's live automation session
and wedges it.
* docs(ci): correct the ios hang wording and note the device lock
* refactor(verifier): derive the lastAction shape from one field list
both hosts must show a spec the same lastAction. one ordered list now
feeds the goja object and the json the web host installs, so they
cannot drift.
* fix(web): install lastAction in the page before extractors read it
state.lastAction was hardcoded null on web, so every property reading it
was silently vacuous: a correct property passed without ever firing.
* fix(web): carry element identity on actions and fix findAll on paths
an action's target was coordinates only, so a property matching on which
element was acted upon could never fire. ax.findAll([a,b]) also returned
nothing on web.
* fix(chrome): wait out a route transition before sampling facts
the tree stays byte-identical and quiet across a cross-fade, so both the
quiet timer and the unchanged-tree escape called it settled mid-flight
and extractors read two screens at once.
* fix: bound the pre-run app launch
launch happens before the runner starts, so --duration never covered it
and a wedged driver hung with no trace and no error.
* fix(folio): read balances from merged cards and treat unreadable as unknown
compose for web merges the whole accountcard subtree, so the balance
child never exists there and every card parsed as 0. the property then
compared 0 to 0 and fired on any submit, which is a false positive
generator. unknown is now null and null is vacuously true.
* test(folio): cover merged-card parsing and unknown balances
* ci(folio): make web an expect-the-bug leg
the web runtime can observe the double submit now, so the health gate
understates it. seed 1 finds it at step 109, 3 runs out of 3.
* docs(ci): explain why a submit tap landing on home is the bug
* fix(ios): read a StaticText's label as its text
AXValue was the only source for text, but a StaticText carries its
string in AXLabel, so nothing on screen had .text on ios: a spec reading
it saw everything on android and nothing here.
* docs(ci): correct the calibrated step ranges
* fix(folio): stop convicting on arithmetic float64 cannot hold
past 2^53 cents the gap between representable values is 128, so a real
1600-cent move reads back as something else and the equality is false
for a healthy submit as readily as a double one. also match parseCents:
a sign or an oversized amount is rejected, not read as an amount.
* test(folio): pin the safe-integer guard and its boundary
* docs: stop teaching the zero-default that caused a false alarm
* docs: write down the silent-vacuity failure modes
* feat(folio): tag the home total and the card transaction count
the total was the only untagged node on the screen, so the spec had to
sum cards and a clipped card broke the sum.
* fix(folio): read the app's own total and refuse contaminated windows
summing cards went null when one was clipped, and the null poisoned the
carrier for the rest of the run. the balance window also spanned every
transaction since the last home visit, so the property convicted on
deltas it could not attribute: the old web witness was 3.16x the typed
amount, not 2x.
* test(folio): pin the window rules and the count invariant
* fix(folio): never read a frame that shows two screens
android dumps a cross-fade with both screens in the tree. the route said
add-transaction while an unscoped find said home, so the oracle took a
half-rendered total as fresh and convicted on a tap that committed
nothing. one function now decides the route and returns null when the
frame is ambiguous.
* test(folio): cover transition frames, card readings and creation
* fix(folio): only disambiguate counts that came from merged text
the equal-length digit rule exists because web merges the card and an
account named -1 makes '12' ambiguous. a dedicated count node has
nothing to disambiguate, so applying it there threw away real evidence.
* ci(folio): pin the recalibrated seeds and drop android to a health gate
web 3 and ios 7 convict 3 runs out of 3 with an exactly 2x witness.
android convicts 2 in 5 because the same seed does not walk the same
trajectory there, so it proves the app runs instead.
* docs(ci): describe the two properties and why android cannot convict
* fix(android): wait out a route cross-fade before snapshotting
the dump could hold two screens at once, and the runner refuses to act
on such a tree, so a quarter of android steps applied no action and the
count varied per run: the same seed never walked the same trajectory.
the ios companion and the chrome driver already do this.
* ci(folio): let the android leg run far enough to see its conviction
* docs: only the repo owner merges
* ci(folio): a thrown predicate is not a conviction
exit 2 means the run recorded a violation, and a predicate that throws
is recorded as one too. so was newAccountBalanceIsZero, an unrelated
property in the same spec. the gate read the exit code and went green
with detection dead.
* ci: install idb-companion from its tap and stop interpolating inputs
idb-companion is not in homebrew-core, so the ios leg died before it
built anything. replay-ui expanded dispatch inputs into the shell.
* docs: correct the snippets and numbers that drifted from the code
* test(sidecar): pin that a slow read counts toward the stability streak
* fix(web): read the page's extractors only on steps that count
the page advances the spec's carriers when it evaluates, but the runner
applied the result only on non-transitional steps. a discarded step
moved the window forward anyway, so the next accepted pair bracketed two
transactions while counting one submit, and convicted a healthy app.
extractor errors now fail the run instead of leaving goja's values in
current against v8's in previous.
* fix(chrome): anchor the transition deadline when the dom goes quiet
it was anchored at script start, so a page that churned past the window
reached the check already expired and returned mid cross-fade. the
driver now publishes the idle timeout it needs, since the caller's 1s
could never spend the 800ms window.
* fix(web): fail on a partial extractor override
same mixed-producer hazard as the install error: some extractors hold
the page's value and the rest hold goja's, and a property comparing
across that split fires on a healthy app.
* docs: six of seven, the seventh is the stock property
* fix(folio): drop a name two cards answer to
homeTxnCountsOf keyed on the account name and let the last card win, so
two accounts the fuzzer named the same collapsed into one entry. a
reading that saw one Travel card and a later one that saw both then
subtracted two different accounts' counts, and
submitCommitsOneTransactionPerAction convicted a healthy app of
double-submitting. it is a gated property in folio-run.sh, so that reads
as "found the submit bug" over a card scrolling into view.
same rule createdAccountHasNonZeroBalance already applies: a name
nothing can attribute is no evidence. counted over every card, since an
unreadable twin spoils the identity too.
* perf(folio): read each frame once
every extractor asked routeOf, and routeOf does five ax.find calls. on
web each find walks the document and every shadow root beneath it, so
the spec cost 110 tree walks a step; homeCards was parsed four times
over. now 5 and once.
keyed on the identity of the state object because both hosts build a new
one per step and hand that one object to every getter, so it cannot
outlive its frame. holding the reference is what keeps that true rather
than likely.
* fix(web): keep an undefined reading's index through JSON
json has no undefined, so an extractor whose getter returned one had its
whole index dropped by JSON.stringify. that index then kept goja's
dump-derived value while its neighbours held the page's, and a property
comparing previous to current across the split fires on a healthy app.
folio has nine on(route, tag) extractors, so this was most extractors on
most steps.
each reading is wrapped in a {value} envelope: the drop now happens
inside the entry, and an absent value means the getter returned
undefined, which is what the goja host records for the same getter. a
json null would instead claim it returned null and x.current ===
undefined would answer differently on the two hosts.
* feat(verifier): report the registered extractor count
the web path needs it to check the page sent one reading per extractor.
* fix(runner): fail when the page reports fewer readings than extractors
the comment here already claimed a partial override was fatal. it was
not: the skipped check only catches indices outside the extractor list,
so a page reporting values for some extractors and not others left the
rest holding goja's reading of the dump with nothing said.
* test(browser): drive an undefined reading through the whole web path
four layers carry it: the page's envelope, the driver's unwrap, the
runner's count check and the verifier's decode. each has a unit test and
only a run proves they compose. goes red both ways, decoding an absent
value as null and dropping the envelope.
* fix(web): offer the aria roles a user activates
only role=button was in the tappable set, so link, checkbox, radio,
switch, tab, option, the menuitems and treeitem were invisible to the
enumeration however plain the control looked. the replay ui builds its
step rows as <li role="option">, and the spec dogfooding it had to
hand-write an action to reach them because no default verb could see a
single row.
both producers build the set from the same role list, since the parity
test compares them element by element.
* test(browser): tap a role-based control end to end
every control on the page is an <li role="option">, the shape the
replay ui gives its step rows, and the spec carries no action of its
own: the property firing is the evidence the default enumeration offered
a tap on one.
* fix(web): read aria-disabled as disabled
the enabled fact came off the disabled property, which only real form
controls have. it reads undefined on the role-based controls the
tappable set now covers, so every one of them looked enabled however
plainly it was marked otherwise, and the fuzzer would spend actions on
inert ones.
both producers answer the same two ways, and the parity fixture carries
a disabled row so the comparison covers it: reverting one side alone
names the element and the fact.
* docs(replay-ui): the enumeration reaches step rows now
the comment said role="option" is not in the tappable selector set,
which stopped being true a few commits ago. selectAStep stays, for the
reason the tab weight below it stays: one row among the page's clickable
elements is a thin chance, and both step-facing properties go vacuous on
a run that never selects one.
* test(runner): bound the last-action test by steps, not wall clock
100ms of wall clock against an assertion that two steps ran fatals under
load with "the web path never installed it", which reads as a
regression. every sibling test in the package uses a long duration and
MaxSteps.
* ci: run the kotlin tests in make test
RouteTransitionTest and the stability poll cover the android settle and
nothing in ci ran them. :sidecar:test needs no android sdk, checked by
running it with ANDROID_HOME pointed at nothing.
* fix(sidecar): measure the stability streak as observed quiet
parameterising pollUntilStable also moved the clock to the start of the
read that opened a run of identical snapshots, so a read's own duration
counted as quiet. the pre-existing caller polls a real uiautomator dump:
at 400ms a read, 750ms of required quiet became 250ms of observed quiet
and the poll settled in two reads instead of four.
the parameters stay, the semantics go back.
* test(sidecar): pin the transition cap by driving it
it asserted 1500 >= 700 + 300, two constants, which can only fail if
someone edits a constant. it now drives awaitSettledTree against a fade
that lands after 700ms and asserts it hands back the settled tree before
the cap. cut the cap to 1000 and it goes red.
* ci: pin buf-setup-action to a commit
it takes a token now, so a floating tag is a token handed to whatever
that tag moves to. note v1 there is a branch, not a tag, so the ref
lookup that resolves it is matching-refs/heads/v1.
* ci: declare least-privilege permissions
none of the three declared any, so each got the repository default.
release.yml and docs.yml already do this. all three only check out,
build, test and upload artifacts.
* ci: fail fast when a server never comes up
the readiness loops fell through silently after 30 tries, so a server
that never started surfaced as an opaque driver failure minutes later.
each now says what did not answer and on which port.
* ci(folio): a missing trace is not a verdict
with no trace the android gate ran its grep against ./trace.jsonl and
reported "never reached AddTransactionScreen, so it never got past
login", which is not what happened. the web and ios branches had the
same misdiagnosis on exit 0.
same class, one line up: the classifier's own failure was swallowed, so
with the evidence reader dead the gate printed a healthy run and exited
0.
* ci(replay-ui): skip a run directory with no trace
the summarise step is if: always(), and under github's bash -eo pipefail
an unmatched glob stays literal, the redirect fails, pipefail carries it
into the assignment and -e kills the step. so a failed fuzz run went red
twice, once for the real reason.
1026 lines
32 KiB
Go
1026 lines
32 KiB
Go
package ioscompanion
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/priyanshujain/sanderling/internal/driver"
|
|
"github.com/priyanshujain/sanderling/internal/driver/ioscompanion/transport"
|
|
)
|
|
|
|
// fakeCompanion is an in-package stand-in for transport.Companion. It records
|
|
// the order of calls and returns scripted results, so the driver's decision
|
|
// logic is testable without a live simulator.
|
|
type fakeCompanion struct {
|
|
// callsMutex guards calls: Snapshot captures the hierarchy and the
|
|
// screenshot concurrently.
|
|
callsMutex sync.Mutex
|
|
calls []string
|
|
|
|
accessibilityJSON string
|
|
accessibilityErr error
|
|
screenshotData []byte
|
|
describe transport.ScreenDescription
|
|
apps []transport.InstalledApp
|
|
|
|
// hidErr fires on the next SendHID then clears, letting a test inject one
|
|
// transient failure.
|
|
hidErr error
|
|
}
|
|
|
|
func (f *fakeCompanion) record(name string) {
|
|
f.callsMutex.Lock()
|
|
defer f.callsMutex.Unlock()
|
|
f.calls = append(f.calls, name)
|
|
}
|
|
|
|
func (f *fakeCompanion) recorded() []string {
|
|
f.callsMutex.Lock()
|
|
defer f.callsMutex.Unlock()
|
|
out := make([]string, len(f.calls))
|
|
copy(out, f.calls)
|
|
return out
|
|
}
|
|
|
|
func (f *fakeCompanion) AccessibilityInfo(context.Context) (string, error) {
|
|
f.record("accessibility")
|
|
return f.accessibilityJSON, f.accessibilityErr
|
|
}
|
|
|
|
func (f *fakeCompanion) Describe(context.Context) (transport.ScreenDescription, error) {
|
|
f.record("describe")
|
|
return f.describe, nil
|
|
}
|
|
|
|
func (f *fakeCompanion) SendHID(context.Context, ...transport.HIDEvent) error {
|
|
f.record("hid")
|
|
if f.hidErr != nil {
|
|
err := f.hidErr
|
|
f.hidErr = nil
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeCompanion) Screenshot(context.Context) ([]byte, string, error) {
|
|
f.record("screenshot")
|
|
return f.screenshotData, "", nil
|
|
}
|
|
|
|
func (f *fakeCompanion) Launch(_ context.Context, _ string, _ bool) error {
|
|
f.record("launch")
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeCompanion) Terminate(context.Context, string) error {
|
|
f.record("terminate")
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeCompanion) ListApps(context.Context) ([]transport.InstalledApp, error) {
|
|
f.record("listapps")
|
|
return f.apps, nil
|
|
}
|
|
|
|
func (f *fakeCompanion) Install(context.Context, string) error {
|
|
f.record("install")
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeCompanion) Uninstall(context.Context, string) error {
|
|
f.record("uninstall")
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeCompanion) Close() error {
|
|
f.record("close")
|
|
return nil
|
|
}
|
|
|
|
var _ transport.Companion = (*fakeCompanion)(nil)
|
|
|
|
// newTestDriver returns a Driver wired to companion with no child process and a
|
|
// no-op container reset, so tests never touch the filesystem or spawn anything.
|
|
func newTestDriver(companion transport.Companion) *Driver {
|
|
d := &Driver{
|
|
companion: companion,
|
|
bundleID: "com.example.app",
|
|
output: &bytes.Buffer{},
|
|
doubleTapGapMilliseconds: DefaultDoubleTapGapMilliseconds,
|
|
screenWidth: 390,
|
|
screenHeight: 844,
|
|
}
|
|
d.resetContainer = func(context.Context) error { return nil }
|
|
d.reinstallApp = func(context.Context) error { return nil }
|
|
d.grantPaste = func(context.Context) error { return nil }
|
|
return d
|
|
}
|
|
|
|
func samplePNG(t *testing.T, width, height int) []byte {
|
|
t.Helper()
|
|
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
img.Set(0, 0, color.RGBA{R: 1, A: 255})
|
|
var buffer bytes.Buffer
|
|
if err := png.Encode(&buffer, img); err != nil {
|
|
t.Fatalf("encode png: %v", err)
|
|
}
|
|
return buffer.Bytes()
|
|
}
|
|
|
|
func TestLaunchTerminatesFirstThenLaunches(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
d := newTestDriver(companion)
|
|
if err := d.Launch(context.Background(), "", false, nil); err != nil {
|
|
t.Fatalf("Launch: %v", err)
|
|
}
|
|
if companion.calls[0] != "terminate" {
|
|
t.Fatalf("first call = %q, want terminate", companion.calls[0])
|
|
}
|
|
if indexOf(companion.calls, "launch") < indexOf(companion.calls, "terminate") {
|
|
t.Fatalf("launch must follow terminate; got %v", companion.calls)
|
|
}
|
|
}
|
|
|
|
func TestLaunchGrantsPasteboardBeforeLaunch(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
d := newTestDriver(companion)
|
|
granted := false
|
|
d.grantPaste = func(context.Context) error { granted = true; return nil }
|
|
if err := d.Launch(context.Background(), "", false, nil); err != nil {
|
|
t.Fatalf("Launch: %v", err)
|
|
}
|
|
if !granted {
|
|
t.Fatal("Launch must grant pasteboard access so unicode input skips the permission prompt")
|
|
}
|
|
}
|
|
|
|
func TestLaunchContinuesWhenGrantFails(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
d := newTestDriver(companion)
|
|
d.grantPaste = func(context.Context) error { return errors.New("no privacy db") }
|
|
if err := d.Launch(context.Background(), "", false, nil); err != nil {
|
|
t.Fatalf("Launch must continue when the grant fails: %v", err)
|
|
}
|
|
if indexOf(companion.calls, "launch") < 0 {
|
|
t.Fatalf("app must still launch; got %v", companion.calls)
|
|
}
|
|
}
|
|
|
|
func TestLaunchClearStateReinstallsWithAppPath(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
d := newTestDriver(companion)
|
|
d.appPath = "/tmp/Sample.app"
|
|
reinstalls := 0
|
|
d.reinstallApp = func(context.Context) error { reinstalls++; return nil }
|
|
if err := d.Launch(context.Background(), "", true, nil); err != nil {
|
|
t.Fatalf("Launch: %v", err)
|
|
}
|
|
if reinstalls != 1 {
|
|
t.Fatalf("clear-state with app path must reinstall exactly once; got %d", reinstalls)
|
|
}
|
|
if indexOf(companion.calls, "launch") < indexOf(companion.calls, "terminate") {
|
|
t.Fatalf("launch must still follow terminate; got %v", companion.calls)
|
|
}
|
|
}
|
|
|
|
func TestLaunchClearStateFallbackWarnsOnce(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
output := &bytes.Buffer{}
|
|
d := newTestDriver(companion)
|
|
d.output = output
|
|
resets := 0
|
|
d.resetContainer = func(context.Context) error { resets++; return nil }
|
|
|
|
for i := 0; i < 2; i++ {
|
|
if err := d.Launch(context.Background(), "", true, nil); err != nil {
|
|
t.Fatalf("Launch %d: %v", i, err)
|
|
}
|
|
}
|
|
if resets != 2 {
|
|
t.Fatalf("resetContainer called %d times, want 2", resets)
|
|
}
|
|
warnings := strings.Count(output.String(), "resetting the data container only")
|
|
if warnings != 1 {
|
|
t.Fatalf("warning emitted %d times, want once", warnings)
|
|
}
|
|
for _, call := range companion.calls {
|
|
if call == "install" || call == "uninstall" {
|
|
t.Fatalf("fallback path must not install/uninstall; got %v", companion.calls)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLaunchRejectsEnvironment(t *testing.T) {
|
|
d := newTestDriver(&fakeCompanion{accessibilityJSON: "[]"})
|
|
err := d.Launch(context.Background(), "", false, map[string]string{"K": "V"})
|
|
if err == nil || !strings.Contains(err.Error(), "environment") {
|
|
t.Fatalf("Launch with env: err = %v, want unsupported-environment error", err)
|
|
}
|
|
}
|
|
|
|
func TestSnapshotPairsHierarchyAndScreenshot(t *testing.T) {
|
|
companion := &fakeCompanion{
|
|
accessibilityJSON: "[]",
|
|
screenshotData: samplePNG(t, 390, 844),
|
|
}
|
|
d := newTestDriver(companion)
|
|
_, image, err := d.Snapshot(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Snapshot: %v", err)
|
|
}
|
|
if image.Width != 390 || image.Height != 844 {
|
|
t.Fatalf("image dims = %dx%d, want 390x844", image.Width, image.Height)
|
|
}
|
|
// The two captures run concurrently (they ride different transports on
|
|
// the hybrid path), so both must happen but in no particular order.
|
|
calls := companion.recorded()
|
|
if indexOf(calls, "accessibility") < 0 || indexOf(calls, "screenshot") < 0 {
|
|
t.Fatalf("snapshot must capture hierarchy and screenshot; got %v", calls)
|
|
}
|
|
}
|
|
|
|
// blockingScreenshotCompanion holds its Screenshot until proceed closes, so a
|
|
// test can keep the screenshot leg in flight while the hierarchy leg recovers.
|
|
type blockingScreenshotCompanion struct {
|
|
fakeCompanion
|
|
proceed chan struct{}
|
|
}
|
|
|
|
func (b *blockingScreenshotCompanion) Screenshot(ctx context.Context) ([]byte, string, error) {
|
|
<-b.proceed
|
|
return b.fakeCompanion.Screenshot(ctx)
|
|
}
|
|
|
|
func TestSnapshotRestartDuringScreenshotDoesNotRace(t *testing.T) {
|
|
// The hierarchy leg drops its connection, forcing withRecovery to restart
|
|
// while the screenshot goroutine is still in flight. The restart reassigns
|
|
// d.companion the way respawnAndRedial does; the goroutine must keep
|
|
// working through the transport it captured rather than racing the field.
|
|
first := &blockingScreenshotCompanion{
|
|
fakeCompanion: fakeCompanion{
|
|
accessibilityErr: status.Error(codes.Unavailable, "companion gone"),
|
|
screenshotData: samplePNG(t, 390, 844),
|
|
},
|
|
proceed: make(chan struct{}),
|
|
}
|
|
replacement := &fakeCompanion{
|
|
accessibilityJSON: "[]",
|
|
screenshotData: samplePNG(t, 390, 844),
|
|
}
|
|
d := newTestDriver(first)
|
|
d.restart = func(context.Context) error {
|
|
d.companion = replacement
|
|
close(first.proceed)
|
|
return nil
|
|
}
|
|
_, image, err := d.Snapshot(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Snapshot should recover: %v", err)
|
|
}
|
|
if image.Width != 390 || image.Height != 844 {
|
|
t.Fatalf("image dims = %dx%d, want 390x844", image.Width, image.Height)
|
|
}
|
|
}
|
|
|
|
func TestScreenshotRejectsNonPNG(t *testing.T) {
|
|
d := newTestDriver(&fakeCompanion{screenshotData: []byte("not a png")})
|
|
if _, err := d.Screenshot(context.Background()); err == nil {
|
|
t.Fatal("Screenshot of non-PNG should error")
|
|
}
|
|
}
|
|
|
|
func TestInputTextFastPathSkipsFieldResolution(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
d := newTestDriver(companion)
|
|
// "abc" is fully mappable and under the paste threshold, so the fast
|
|
// keyboard path runs and never reads the accessibility dump for a field
|
|
// target.
|
|
if err := d.InputText(context.Background(), "abc"); err != nil {
|
|
t.Fatalf("InputText: %v", err)
|
|
}
|
|
if indexOf(companion.calls, "hid") < 0 {
|
|
t.Fatalf("fast path must send HID; got %v", companion.calls)
|
|
}
|
|
if indexOf(companion.calls, "accessibility") >= 0 {
|
|
t.Fatalf("fast path must not resolve a field via accessibility; got %v", companion.calls)
|
|
}
|
|
}
|
|
|
|
func TestInputTextPasteboardResolvesFieldFromLastTap(t *testing.T) {
|
|
// The dump carries one editable field whose frame contains the last tap.
|
|
dump := `[{"type":"TextField","AXUniqueId":"field-1","frame":{"x":10,"y":100,"width":200,"height":40}}]`
|
|
companion := &fakeCompanion{accessibilityJSON: dump, screenshotData: samplePNG(t, 10, 10)}
|
|
d := newTestDriver(companion)
|
|
// Tap inside the field so lastTap lands on it.
|
|
if err := d.Tap(context.Background(), 50, 120); err != nil {
|
|
t.Fatalf("Tap: %v", err)
|
|
}
|
|
field := d.resolveInputField(context.Background())
|
|
if field.identifier != "field-1" {
|
|
t.Fatalf("resolved field id = %q, want field-1", field.identifier)
|
|
}
|
|
if field.centerX != 110 || field.centerY != 120 {
|
|
t.Fatalf("field center = (%v,%v), want (110,120)", field.centerX, field.centerY)
|
|
}
|
|
}
|
|
|
|
func TestResolveInputFieldEmptyWhenTapOutsideAnyField(t *testing.T) {
|
|
dump := `[{"type":"TextField","AXUniqueId":"field-1","frame":{"x":10,"y":100,"width":200,"height":40}}]`
|
|
d := newTestDriver(&fakeCompanion{accessibilityJSON: dump})
|
|
if err := d.Tap(context.Background(), 5, 5); err != nil {
|
|
t.Fatalf("Tap: %v", err)
|
|
}
|
|
if field := d.resolveInputField(context.Background()); field != (fieldTarget{}) {
|
|
t.Fatalf("field = %+v, want empty", field)
|
|
}
|
|
}
|
|
|
|
func TestSupervisionRestartsOnceThenSucceeds(t *testing.T) {
|
|
companion := &fakeCompanion{
|
|
accessibilityJSON: "[]",
|
|
hidErr: status.Error(codes.Unavailable, "connection refused"),
|
|
}
|
|
d := newTestDriver(companion)
|
|
restarts := 0
|
|
d.restart = func(context.Context) error {
|
|
restarts++
|
|
return nil
|
|
}
|
|
// First SendHID fails with Unavailable; the recovery restarts once and the
|
|
// retry succeeds (hidErr cleared itself).
|
|
if err := d.Tap(context.Background(), 10, 10); err != nil {
|
|
t.Fatalf("Tap should recover: %v", err)
|
|
}
|
|
if restarts != 1 {
|
|
t.Fatalf("restarts = %d, want exactly 1", restarts)
|
|
}
|
|
}
|
|
|
|
func TestSupervisionDoesNotRestartOnNormalError(t *testing.T) {
|
|
companion := &fakeCompanion{hidErr: errors.New("bad argument")}
|
|
d := newTestDriver(companion)
|
|
restarts := 0
|
|
d.restart = func(context.Context) error { restarts++; return nil }
|
|
if err := d.Tap(context.Background(), 10, 10); err == nil {
|
|
t.Fatal("non-connection error should surface")
|
|
}
|
|
if restarts != 0 {
|
|
t.Fatalf("restarts = %d, want 0 on a normal error", restarts)
|
|
}
|
|
}
|
|
|
|
func TestSupervisionBudgetResetsBetweenIncidents(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
d := newTestDriver(companion)
|
|
restarts := 0
|
|
d.restart = func(context.Context) error { restarts++; return nil }
|
|
|
|
companion.hidErr = status.Error(codes.Unavailable, "drop one")
|
|
if err := d.Tap(context.Background(), 1, 1); err != nil {
|
|
t.Fatalf("first incident: %v", err)
|
|
}
|
|
companion.hidErr = status.Error(codes.Unavailable, "drop two")
|
|
if err := d.Tap(context.Background(), 2, 2); err != nil {
|
|
t.Fatalf("second incident: %v", err)
|
|
}
|
|
if restarts != 2 {
|
|
t.Fatalf("restarts = %d, want 2 (one per incident)", restarts)
|
|
}
|
|
}
|
|
|
|
func TestPressKeyEnterSupportedOthersUnsupported(t *testing.T) {
|
|
d := newTestDriver(&fakeCompanion{})
|
|
if err := d.PressKey(context.Background(), "enter"); err != nil {
|
|
t.Fatalf("PressKey enter: %v", err)
|
|
}
|
|
if err := d.PressKey(context.Background(), "return"); err != nil {
|
|
t.Fatalf("PressKey return: %v", err)
|
|
}
|
|
for _, key := range []string{"back", "home", "tab"} {
|
|
if err := d.PressKey(context.Background(), key); err == nil {
|
|
t.Fatalf("PressKey %q should be unsupported", key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWaitForIdleSettlesOnStableTree(t *testing.T) {
|
|
companion := &fakeCompanion{accessibilityJSON: "[]"}
|
|
d := newTestDriver(companion)
|
|
// A fake clock advances time only on Sleep, so the settle poll runs to its
|
|
// cap instantly instead of taking the real two seconds.
|
|
d.idleClock = &fakeClock{}
|
|
if err := d.WaitForIdle(context.Background(), time.Second); err != nil {
|
|
t.Fatalf("WaitForIdle: %v", err)
|
|
}
|
|
if indexOf(companion.calls, "accessibility") < 0 {
|
|
t.Fatalf("WaitForIdle must poll the hierarchy; got %v", companion.calls)
|
|
}
|
|
}
|
|
|
|
func TestForegroundAppPrefersAppUnderTest(t *testing.T) {
|
|
companion := &fakeCompanion{apps: []transport.InstalledApp{
|
|
{BundleID: "com.example.app", ProcessState: transport.ProcessStateRunning, InstallType: "user"},
|
|
{BundleID: "com.other.app", ProcessState: transport.ProcessStateRunning, InstallType: "user"},
|
|
}}
|
|
d := newTestDriver(companion)
|
|
got, err := d.ForegroundApp(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("ForegroundApp: %v", err)
|
|
}
|
|
if got != "com.example.app" {
|
|
t.Fatalf("ForegroundApp = %q, want com.example.app", got)
|
|
}
|
|
}
|
|
|
|
func TestForegroundAppFallsBackToOtherRunningUserApp(t *testing.T) {
|
|
companion := &fakeCompanion{apps: []transport.InstalledApp{
|
|
{BundleID: "com.example.app", ProcessState: transport.ProcessStateNotRunning, InstallType: "user"},
|
|
{BundleID: "com.other.app", ProcessState: transport.ProcessStateRunning, InstallType: "user"},
|
|
}}
|
|
d := newTestDriver(companion)
|
|
got, err := d.ForegroundApp(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("ForegroundApp: %v", err)
|
|
}
|
|
if got != "com.other.app" {
|
|
t.Fatalf("ForegroundApp = %q, want com.other.app", got)
|
|
}
|
|
}
|
|
|
|
func TestHealthReportsIOS(t *testing.T) {
|
|
d := newTestDriver(&fakeCompanion{})
|
|
health, err := d.Health(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Health: %v", err)
|
|
}
|
|
if !health.Ready || health.Platform != "ios" {
|
|
t.Fatalf("Health = %+v, want ready ios", health)
|
|
}
|
|
}
|
|
|
|
// The driver must satisfy DeviceDriver, ForegroundChecker, and TextReplacer
|
|
// (its InputText clears the field then types, so the runner skips its
|
|
// blackout-fragile pre-erase), but must NOT satisfy FocusedWindowChecker.
|
|
func TestCapabilityAssertions(t *testing.T) {
|
|
var instance any = newTestDriver(&fakeCompanion{})
|
|
if _, ok := instance.(driver.DeviceDriver); !ok {
|
|
t.Fatal("Driver must implement DeviceDriver")
|
|
}
|
|
if _, ok := instance.(driver.ForegroundChecker); !ok {
|
|
t.Fatal("Driver must implement ForegroundChecker")
|
|
}
|
|
replacer, ok := instance.(driver.TextReplacer)
|
|
if !ok {
|
|
t.Fatal("Driver must implement TextReplacer")
|
|
}
|
|
if !replacer.ReplacesTextOnInput() {
|
|
t.Fatal("ReplacesTextOnInput must report true")
|
|
}
|
|
if _, ok := instance.(driver.FocusedWindowChecker); ok {
|
|
t.Fatal("Driver must NOT implement FocusedWindowChecker")
|
|
}
|
|
}
|
|
|
|
func indexOf(slice []string, value string) int {
|
|
for i, item := range slice {
|
|
if item == value {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// TestNewChildOutlivesStartup proves the companion child is spawned under the
|
|
// driver-lifetime context, not the startup-scoped one: a startup context that
|
|
// is canceled when New returns would SIGTERM the child mid-run.
|
|
func TestNewChildOutlivesStartup(t *testing.T) {
|
|
// The legacy-only path keeps this test focused on the companion child;
|
|
// the hybrid default would also demand runner assets.
|
|
t.Setenv("SANDERLING_SIMULATOR_COMPANION", "legacy")
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
go func() {
|
|
for {
|
|
connection, acceptErr := listener.Accept()
|
|
if acceptErr != nil {
|
|
return
|
|
}
|
|
_ = connection.Close()
|
|
}
|
|
}()
|
|
|
|
var spawnContext context.Context
|
|
options := Options{
|
|
UniqueDeviceIdentifier: "FAKE-UDID",
|
|
pickAddress: func() (string, error) { return listener.Addr().String(), nil },
|
|
spawnChild: func(ctx context.Context, _ string) (*exec.Cmd, error) {
|
|
spawnContext = ctx
|
|
return &exec.Cmd{}, nil
|
|
},
|
|
dialCompanion: func(string) (transport.Companion, error) {
|
|
return &fakeCompanion{accessibilityJSON: "[]"}, nil
|
|
},
|
|
}
|
|
|
|
d, err := New(context.Background(), options)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case <-spawnContext.Done():
|
|
t.Fatal("spawn context canceled after New returned; child would receive SIGTERM mid-run")
|
|
default:
|
|
}
|
|
|
|
d.Close()
|
|
select {
|
|
case <-spawnContext.Done():
|
|
default:
|
|
t.Fatal("spawn context still alive after Close; child lifetime leaks")
|
|
}
|
|
}
|
|
|
|
// fakeTextEditingCompanion extends fakeCompanion with the optional TextEditor
|
|
// capability so routing through the native text path is testable.
|
|
type fakeTextEditingCompanion struct {
|
|
fakeCompanion
|
|
|
|
inputTexts []string
|
|
eraseCounts []int
|
|
pressedKeys []string
|
|
}
|
|
|
|
func (f *fakeTextEditingCompanion) InputText(_ context.Context, text string) error {
|
|
f.record("inputtext")
|
|
f.inputTexts = append(f.inputTexts, text)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeTextEditingCompanion) EraseText(_ context.Context, characterCount int) error {
|
|
f.record("erasetext")
|
|
f.eraseCounts = append(f.eraseCounts, characterCount)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeTextEditingCompanion) PressKey(_ context.Context, key string) error {
|
|
f.record("presskey")
|
|
f.pressedKeys = append(f.pressedKeys, key)
|
|
return nil
|
|
}
|
|
|
|
var _ transport.TextEditor = (*fakeTextEditingCompanion)(nil)
|
|
|
|
func TestInputTextRoutesThroughTextEditor(t *testing.T) {
|
|
companion := &fakeTextEditingCompanion{}
|
|
d := newTestDriver(companion)
|
|
if err := d.InputText(context.Background(), "héllo 🌟"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(companion.inputTexts) != 1 || companion.inputTexts[0] != "héllo 🌟" {
|
|
t.Fatalf("inputTexts = %v, want the typed text once", companion.inputTexts)
|
|
}
|
|
for _, call := range companion.calls {
|
|
if call == "hid" {
|
|
t.Fatal("text editor path must not compose HID streams")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEraseTextRoutesThroughTextEditor(t *testing.T) {
|
|
companion := &fakeTextEditingCompanion{}
|
|
d := newTestDriver(companion)
|
|
if err := d.EraseText(context.Background(), 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(companion.eraseCounts) != 1 || companion.eraseCounts[0] != 7 {
|
|
t.Fatalf("eraseCounts = %v, want [7]", companion.eraseCounts)
|
|
}
|
|
}
|
|
|
|
func TestPressKeyRoutesThroughTextEditor(t *testing.T) {
|
|
companion := &fakeTextEditingCompanion{}
|
|
d := newTestDriver(companion)
|
|
if err := d.PressKey(context.Background(), "enter"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(companion.pressedKeys) != 1 || companion.pressedKeys[0] != "enter" {
|
|
t.Fatalf("pressedKeys = %v, want [enter]", companion.pressedKeys)
|
|
}
|
|
for _, call := range companion.calls {
|
|
if call == "hid" {
|
|
t.Fatal("text editor path must not compose HID streams")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsConnectionErrorRecognizesUnavailableSentinel(t *testing.T) {
|
|
wrapped := errors.Join(errors.New("dial tcp: connection refused"), transport.ErrCompanionUnavailable)
|
|
if !isConnectionError(wrapped) {
|
|
t.Fatal("wrapped ErrCompanionUnavailable must count as a connection error")
|
|
}
|
|
if isConnectionError(errors.New("ordinary failure")) {
|
|
t.Fatal("ordinary errors must not count as connection errors")
|
|
}
|
|
}
|
|
|
|
// fakeRunnerCompanion stands in for the in-simulator runner half of the hybrid:
|
|
// it serves snapshots and native typing.
|
|
type fakeRunnerCompanion struct {
|
|
fakeCompanion
|
|
|
|
typed []struct {
|
|
text string
|
|
replace bool
|
|
}
|
|
}
|
|
|
|
func (f *fakeRunnerCompanion) TypeText(_ context.Context, text string, replace bool) error {
|
|
f.record("typetext")
|
|
f.typed = append(f.typed, struct {
|
|
text string
|
|
replace bool
|
|
}{text, replace})
|
|
return nil
|
|
}
|
|
|
|
var _ transport.TextTyper = (*fakeRunnerCompanion)(nil)
|
|
|
|
func newHybridTestDriver(legacy *fakeCompanion, runner *fakeRunnerCompanion) *Driver {
|
|
d := newTestDriver(legacy)
|
|
d.hybrid = true
|
|
d.runnerClient = runner
|
|
return d
|
|
}
|
|
|
|
func TestHybridInputTextClearsViaHIDThenTypes(t *testing.T) {
|
|
legacy := &fakeCompanion{accessibilityJSON: "[]"}
|
|
runner := &fakeRunnerCompanion{}
|
|
d := newHybridTestDriver(legacy, runner)
|
|
|
|
if err := d.InputText(context.Background(), "héllo 🌟"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(legacy.calls) != 1 || legacy.calls[0] != "hid" {
|
|
t.Fatalf("legacy calls = %v, want exactly the clear chord", legacy.calls)
|
|
}
|
|
if len(runner.typed) != 1 || runner.typed[0].text != "héllo 🌟" || runner.typed[0].replace {
|
|
t.Fatalf("typed = %+v, want the text appended without replace", runner.typed)
|
|
}
|
|
}
|
|
|
|
func TestHybridSnapshotsComeFromRunner(t *testing.T) {
|
|
legacy := &fakeCompanion{accessibilityJSON: `[{"type":"Application","frame":{"x":0,"y":0,"width":1,"height":1},"enabled":true}]`}
|
|
runner := &fakeRunnerCompanion{}
|
|
runner.accessibilityJSON = `[{"type":"Button","AXUniqueId":"FromRunner","frame":{"x":0,"y":0,"width":10,"height":10},"enabled":true}]`
|
|
d := newHybridTestDriver(legacy, runner)
|
|
|
|
hierarchy, err := d.Hierarchy(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(hierarchy, "FromRunner") {
|
|
t.Fatalf("hierarchy = %s, want runner content", hierarchy)
|
|
}
|
|
for _, call := range legacy.calls {
|
|
if call == "accessibility" {
|
|
t.Fatal("hybrid must not read snapshots from the legacy companion")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHybridLaunchSkipsPasteGrant(t *testing.T) {
|
|
legacy := &fakeCompanion{accessibilityJSON: "[]"}
|
|
runner := &fakeRunnerCompanion{}
|
|
d := newHybridTestDriver(legacy, runner)
|
|
granted := 0
|
|
d.grantPaste = func(context.Context) error { granted++; return nil }
|
|
|
|
if err := d.Launch(context.Background(), "", false, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if granted != 0 {
|
|
t.Fatalf("grantPaste called %d times, want 0 on the hybrid path", granted)
|
|
}
|
|
}
|
|
|
|
func TestHybridGesturesStayOnLegacyHID(t *testing.T) {
|
|
legacy := &fakeCompanion{accessibilityJSON: "[]"}
|
|
runner := &fakeRunnerCompanion{}
|
|
d := newHybridTestDriver(legacy, runner)
|
|
|
|
if err := d.DoubleTap(context.Background(), 10, 20); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(legacy.calls) != 1 || legacy.calls[0] != "hid" {
|
|
t.Fatalf("legacy calls = %v, want the double-tap HID stream", legacy.calls)
|
|
}
|
|
for _, call := range runner.calls {
|
|
if call == "hid" {
|
|
t.Fatal("runner must not receive gesture streams")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHybridRunnerErrorTriggersRestart(t *testing.T) {
|
|
legacy := &fakeCompanion{accessibilityJSON: "[]"}
|
|
runner := &fakeRunnerCompanion{}
|
|
runner.accessibilityErr = fmt.Errorf("read snapshot: %w", transport.ErrCompanionUnavailable)
|
|
d := newHybridTestDriver(legacy, runner)
|
|
restarts := 0
|
|
d.restart = func(context.Context) error {
|
|
restarts++
|
|
runner.accessibilityErr = nil
|
|
return nil
|
|
}
|
|
|
|
if _, err := d.Hierarchy(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if restarts != 1 {
|
|
t.Fatalf("restarts = %d, want 1", restarts)
|
|
}
|
|
}
|
|
|
|
func TestBindTestRunPortSubstitutesPlaceholder(t *testing.T) {
|
|
directory := t.TempDir()
|
|
source := filepath.Join(directory, "runner.xctestrun")
|
|
if err := os.WriteFile(source, []byte("<string>__COMPANION_PORT__</string>"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bound, err := bindTestRunPort(source, "27999")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if filepath.Dir(bound) != directory {
|
|
t.Fatalf("bound copy %s must sit next to the original", bound)
|
|
}
|
|
content, err := os.ReadFile(bound)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(content) != "<string>27999</string>" {
|
|
t.Fatalf("bound content = %s", content)
|
|
}
|
|
}
|
|
|
|
func TestBindTestRunPortRejectsMissingPlaceholder(t *testing.T) {
|
|
directory := t.TempDir()
|
|
source := filepath.Join(directory, "runner.xctestrun")
|
|
if err := os.WriteFile(source, []byte("<string>fixed</string>"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := bindTestRunPort(source, "27999"); err == nil {
|
|
t.Fatal("expected an error for a configuration without the placeholder")
|
|
}
|
|
}
|
|
|
|
func TestHybridMappableTextRidesOneHIDStream(t *testing.T) {
|
|
legacy := &fakeCompanion{accessibilityJSON: "[]"}
|
|
runner := &fakeRunnerCompanion{}
|
|
d := newHybridTestDriver(legacy, runner)
|
|
|
|
if err := d.InputText(context.Background(), "Travel 42"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(legacy.recorded()) != 1 || legacy.recorded()[0] != "hid" {
|
|
t.Fatalf("legacy calls = %v, want one combined chord-and-keystrokes stream", legacy.recorded())
|
|
}
|
|
if len(runner.typed) != 0 {
|
|
t.Fatalf("typed = %+v, want no native typing for mappable text", runner.typed)
|
|
}
|
|
}
|
|
|
|
func TestHybridUnicodeWaitsForClearedFieldBeforeTyping(t *testing.T) {
|
|
legacy := &fakeCompanion{accessibilityJSON: "[]"}
|
|
runner := &fakeRunnerCompanion{}
|
|
// The focused field still shows old content on the first read and is
|
|
// empty on the second; typing must come after the cleared read.
|
|
first := `[{"type":"TextField","AXUniqueId":"F","AXValue":"old","frame":{"x":0,"y":0,"width":100,"height":40},"enabled":true}]`
|
|
second := `[{"type":"TextField","AXUniqueId":"F","AXValue":"","frame":{"x":0,"y":0,"width":100,"height":40},"enabled":true}]`
|
|
reads := 0
|
|
d := newHybridTestDriver(legacy, runner)
|
|
d.mu.Lock()
|
|
d.lastTap.x, d.lastTap.y, d.lastTap.set = 50, 20, true
|
|
d.mu.Unlock()
|
|
// Swap the dump after the first read through a wrapper companion.
|
|
wrapped := &sequencedDumpCompanion{fakeRunnerCompanion: runner, dumps: []string{first, second}, reads: &reads}
|
|
d.runnerClient = wrapped
|
|
|
|
if err := d.InputText(context.Background(), "héllo 🌟"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reads < 2 {
|
|
t.Fatalf("reads = %d, want at least 2 (poll until cleared)", reads)
|
|
}
|
|
if len(wrapped.typed) != 1 || wrapped.typed[0].text != "héllo 🌟" || wrapped.typed[0].replace {
|
|
t.Fatalf("typed = %+v", wrapped.typed)
|
|
}
|
|
}
|
|
|
|
// sequencedDumpCompanion serves scripted dumps in order, repeating the last.
|
|
type sequencedDumpCompanion struct {
|
|
*fakeRunnerCompanion
|
|
dumps []string
|
|
reads *int
|
|
}
|
|
|
|
func (s *sequencedDumpCompanion) AccessibilityInfo(context.Context) (string, error) {
|
|
index := *s.reads
|
|
if index >= len(s.dumps) {
|
|
index = len(s.dumps) - 1
|
|
}
|
|
*s.reads++
|
|
return s.dumps[index], nil
|
|
}
|
|
|
|
// wedgedLifecycleCompanion never answers a lifecycle RPC, standing in for a
|
|
// runner whose XCTest session is stuck inside a rejected launch.
|
|
type wedgedLifecycleCompanion struct {
|
|
fakeCompanion
|
|
release chan struct{}
|
|
}
|
|
|
|
func (w *wedgedLifecycleCompanion) block(ctx context.Context) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-w.release:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (w *wedgedLifecycleCompanion) Launch(ctx context.Context, _ string, _ bool) error {
|
|
return w.block(ctx)
|
|
}
|
|
|
|
func (w *wedgedLifecycleCompanion) Terminate(ctx context.Context, _ string) error {
|
|
return w.block(ctx)
|
|
}
|
|
|
|
// TestLaunchBoundsWedgedLifecycleRPC proves the launch path carries its own
|
|
// deadline. Callers hand Launch an undeadlined context, so without one a runner
|
|
// that never answers hangs the run forever with nothing printed.
|
|
func TestLaunchBoundsWedgedLifecycleRPC(t *testing.T) {
|
|
previous := launchTimeout
|
|
launchTimeout = 100 * time.Millisecond
|
|
defer func() { launchTimeout = previous }()
|
|
|
|
companion := &wedgedLifecycleCompanion{release: make(chan struct{})}
|
|
defer close(companion.release)
|
|
d := newTestDriver(companion)
|
|
|
|
done := make(chan error, 1)
|
|
go func() { done <- d.Launch(context.Background(), "com.example.app", false, nil) }()
|
|
|
|
select {
|
|
case err := <-done:
|
|
if err == nil {
|
|
t.Fatal("wedged launch returned nil; a stuck runner must surface an error")
|
|
}
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("err = %v, want a deadline-exceeded error", err)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("Launch never returned: the lifecycle RPC is unbounded, so a stuck runner hangs the run forever")
|
|
}
|
|
}
|
|
|
|
// TestTerminateBoundsWedgedLifecycleRPC covers the same bound on the standalone
|
|
// terminate, which the runner calls mid-run on an equally stuck session.
|
|
func TestTerminateBoundsWedgedLifecycleRPC(t *testing.T) {
|
|
previous := launchTimeout
|
|
launchTimeout = 100 * time.Millisecond
|
|
defer func() { launchTimeout = previous }()
|
|
|
|
companion := &wedgedLifecycleCompanion{release: make(chan struct{})}
|
|
defer close(companion.release)
|
|
d := newTestDriver(companion)
|
|
|
|
done := make(chan error, 1)
|
|
go func() { done <- d.Terminate(context.Background()) }()
|
|
|
|
select {
|
|
case err := <-done:
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("err = %v, want a deadline-exceeded error", err)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("Terminate never returned: the lifecycle RPC is unbounded")
|
|
}
|
|
}
|
|
|
|
// TestLaunchLeavesATighterCallerDeadlineAlone confirms the bound narrows the
|
|
// caller's context and never widens it, so a caller that wants to give up
|
|
// sooner still does.
|
|
func TestLaunchLeavesATighterCallerDeadlineAlone(t *testing.T) {
|
|
previous := launchTimeout
|
|
launchTimeout = 30 * time.Second
|
|
defer func() { launchTimeout = previous }()
|
|
|
|
companion := &wedgedLifecycleCompanion{release: make(chan struct{})}
|
|
defer close(companion.release)
|
|
d := newTestDriver(companion)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
defer cancel()
|
|
start := time.Now()
|
|
if err := d.Launch(ctx, "com.example.app", false, nil); !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("err = %v, want a deadline-exceeded error", err)
|
|
}
|
|
if elapsed := time.Since(start); elapsed > 5*time.Second {
|
|
t.Fatalf("Launch took %v; the driver's bound overrode the caller's tighter deadline", elapsed)
|
|
}
|
|
}
|
|
|
|
// newLockTestOptions builds New options that dial a seamed companion, so the
|
|
// device-lock tests exercise New without spawning anything.
|
|
func newLockTestOptions(t *testing.T, udid string) Options {
|
|
t.Helper()
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { listener.Close() })
|
|
go func() {
|
|
for {
|
|
connection, acceptErr := listener.Accept()
|
|
if acceptErr != nil {
|
|
return
|
|
}
|
|
_ = connection.Close()
|
|
}
|
|
}()
|
|
return Options{
|
|
UniqueDeviceIdentifier: udid,
|
|
pickAddress: func() (string, error) { return listener.Addr().String(), nil },
|
|
spawnChild: func(context.Context, string) (*exec.Cmd, error) { return &exec.Cmd{}, nil },
|
|
dialCompanion: func(string) (transport.Companion, error) {
|
|
return &fakeCompanion{accessibilityJSON: "[]"}, nil
|
|
},
|
|
}
|
|
}
|
|
|
|
// TestNewRejectsConcurrentRunOnSameDevice proves a second run cannot claim a
|
|
// device the first is driving. Two runs interleave app lifecycle on one
|
|
// simulator: the second's reinstall lands under the first's automation session
|
|
// and wedges it. Failing fast names the contended device; the claim is released
|
|
// on Close so the next run is not locked out.
|
|
func TestNewRejectsConcurrentRunOnSameDevice(t *testing.T) {
|
|
t.Setenv("SANDERLING_SIMULATOR_COMPANION", "legacy")
|
|
udid := "LOCK-TEST-" + t.Name()
|
|
|
|
first, err := New(context.Background(), newLockTestOptions(t, udid))
|
|
if err != nil {
|
|
t.Fatalf("first New: %v", err)
|
|
}
|
|
|
|
_, err = New(context.Background(), newLockTestOptions(t, udid))
|
|
if err == nil {
|
|
t.Fatal("second run claimed a device the first still drives; concurrent runs corrupt each other's session")
|
|
}
|
|
if !strings.Contains(err.Error(), udid) {
|
|
t.Fatalf("err = %v, want it to name the contended device %s", err, udid)
|
|
}
|
|
|
|
first.Close()
|
|
third, err := New(context.Background(), newLockTestOptions(t, udid))
|
|
if err != nil {
|
|
t.Fatalf("device stayed locked after Close: %v", err)
|
|
}
|
|
third.Close()
|
|
}
|
|
|
|
// TestAcquireDeviceLockKeepsDistinctDevicesIndependent guards against a lock
|
|
// path that ignores the udid and serializes unrelated runs.
|
|
func TestAcquireDeviceLockKeepsDistinctDevicesIndependent(t *testing.T) {
|
|
first, err := acquireDeviceLock("LOCK-TEST-DEVICE-A")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer first.Close()
|
|
second, err := acquireDeviceLock("LOCK-TEST-DEVICE-B")
|
|
if err != nil {
|
|
t.Fatalf("a second device was refused while another was locked: %v", err)
|
|
}
|
|
defer second.Close()
|
|
}
|