feat: Phase 4 — spotify_radio, and a bound on logs/runs.jsonl
spotify_radio opens a track's own context menu and starts its song radio.
The labels came from a fresh uiautomator dump on the phone, not from a guess:
the entry is "Go to song radio" and it sits below the fold, so the recipe
scrolls before it matches.
Recipe format:
- menu_row "{query}" finds the row like tap_row, then presses the action
button inside that row's own subtree (ui.Row.Action), so the More options
button of any other row is never a candidate. It records the row as {row}.
- a step can name its own failure: wait ... else "reason". The named reason
comes first and the screen's labels still follow (FR-8).
- scroll up|down moves the current list.
- playing with no title waits for the media session to be PLAYING. A radio
changes track every few minutes, so a title match would fail on a working
radio; spotify_play still passes the title it wants.
ui:
- pickBestMatch ignores a match with no usable bounds. An element scrolled out
of its list reports [0,0][0,0], and tapping its "centre" would be a blind tap
in the corner. A wait now only succeeds once the element is on screen.
Run log (FR-10): appendRunLog rotates runs.jsonl when a write would pass
ARTEMIS_LOG_MAX_BYTES (default 1 MiB), keeping ARTEMIS_LOG_KEEP (default 5)
files. History is bounded, never deleted wholesale, and the per-entry cap of
6 screen labels of 60 characters is unchanged.
Live evidence in docs/phase-4-song-radio.md: ok:true in 56.6s, the menu that
opened belonged to the pressed row, the station name "Honky Tonk Women Radio"
on the phone, and the radio playing past its seed track (Layla) per the media
session and dumpsys audio. Both new failure modes were demonstrated failing
with distinct reasons. The mini-bar's transport label stays "Play" while
playing on this build, which is why playback is confirmed from the media
session.
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -46,9 +46,12 @@ type Options struct {
|
||||
// returns an error silently: the returned RunResult always says what happened.
|
||||
func Run(r *adb.Runner, rec *Recipe, opts Options) *RunResult {
|
||||
started := time.Now()
|
||||
params := opts.Params
|
||||
if params == nil {
|
||||
params = map[string]string{}
|
||||
// The caller's map is copied: a run adds its own values to it (the row a
|
||||
// tap_row or menu_row step chose, under "row"), and the caller's map must
|
||||
// not change because of a run.
|
||||
params := make(map[string]string, len(opts.Params)+1)
|
||||
for name, value := range opts.Params {
|
||||
params[name] = value
|
||||
}
|
||||
res := &RunResult{Task: rec.Task, Params: params}
|
||||
defer func() {
|
||||
@@ -111,7 +114,7 @@ func Run(r *adb.Runner, rec *Recipe, opts Options) *RunResult {
|
||||
// A wait step with "retry N" presses the step above again: on a
|
||||
// busy phone a tap can be delivered but never acted on, or dropped.
|
||||
if attempt >= step.Retry {
|
||||
return fail(fmt.Errorf("step %d (%s %s) failed: %s", i+1, step.Verb, strings.Join(step.Args, " "), sr.Error))
|
||||
return fail(fmt.Errorf("step %d (%s %s) failed: %s", i+1, step.Verb, strings.Join(step.Args, " "), stepFailure(sr, step)))
|
||||
}
|
||||
if i == 0 {
|
||||
return fail(fmt.Errorf("step %d: retry needs a step above it to repeat", i+1))
|
||||
@@ -218,7 +221,15 @@ func executeStep(r *adb.Runner, rec *Recipe, step Step, index int, params map[st
|
||||
case "playing":
|
||||
// The screen is not a trustworthy place to read playback: Spotify's
|
||||
// transport button keeps the label "Play" while music is playing.
|
||||
// Ask the media session instead, and confirm the track title.
|
||||
// Ask the media session instead. An extra text argument also
|
||||
// confirms the playing title, which is how spotify_play proves it
|
||||
// started the right song. With no text, only the state is checked:
|
||||
// a radio plays a different track every few minutes, so a title
|
||||
// match would fail on a radio that is working perfectly.
|
||||
title := ""
|
||||
if len(args) >= 1 {
|
||||
title = args[0]
|
||||
}
|
||||
pkg := rec.App
|
||||
if len(args) == 2 {
|
||||
pkg = args[1]
|
||||
@@ -235,14 +246,22 @@ func executeStep(r *adb.Runner, rec *Recipe, step Step, index int, params map[st
|
||||
}
|
||||
last = state
|
||||
if state.Playing() && state.Title != "" {
|
||||
matched, total := ui.MatchesText(state.Title, args[0])
|
||||
if title == "" {
|
||||
sr.Detail = fmt.Sprintf("%s is playing %q", pkg, state.Title)
|
||||
return nil
|
||||
}
|
||||
matched, total := ui.MatchesText(state.Title, title)
|
||||
if total > 0 && matched*2 >= total {
|
||||
sr.Detail = fmt.Sprintf("%s is playing %q", pkg, state.Title)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if time.Now().After(checkDeadline) {
|
||||
return fmt.Errorf("%s is not playing %q within %s (%s)", pkg, args[0], timeout, last)
|
||||
want := fmt.Sprintf("playing %q", title)
|
||||
if title == "" {
|
||||
want = "playing"
|
||||
}
|
||||
return fmt.Errorf("%s is not %s within %s (%s)", pkg, want, timeout, last)
|
||||
}
|
||||
time.Sleep(ui.PollInterval)
|
||||
}
|
||||
@@ -279,7 +298,20 @@ func executeStep(r *adb.Runner, rec *Recipe, step Step, index int, params map[st
|
||||
return nil
|
||||
|
||||
case "tap_row":
|
||||
return tapRow(r, rec, args[0], timeout, w, h, &sr)
|
||||
title, err := tapRow(r, rec, args[0], timeout, w, h, &sr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params["row"] = title
|
||||
return nil
|
||||
|
||||
case "menu_row":
|
||||
title, err := tapRowMenu(r, rec, args[0], timeout, w, h, &sr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params["row"] = title
|
||||
return nil
|
||||
|
||||
case "type":
|
||||
if err := ui.Type(r, args[0]); err != nil {
|
||||
@@ -288,6 +320,17 @@ func executeStep(r *adb.Runner, rec *Recipe, step Step, index int, params map[st
|
||||
sr.Detail = fmt.Sprintf("typed %d characters", len(args[0]))
|
||||
return nil
|
||||
|
||||
case "scroll":
|
||||
// Scroll the current list. ui.Scroll prefers a scrollable node from
|
||||
// the dump, so a swipe stays inside the list rather than landing on
|
||||
// a toolbar. A recipe uses it when a menu's entries sit below the
|
||||
// fold and are not on screen yet.
|
||||
if err := ui.Scroll(r, args[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
sr.Detail = "scrolled " + strings.ToLower(args[0])
|
||||
return nil
|
||||
|
||||
case "key":
|
||||
return sendKey(r, args[0], &sr)
|
||||
|
||||
@@ -355,8 +398,9 @@ func tapByText(r *adb.Runner, rec *Recipe, query string, exact bool, timeout tim
|
||||
|
||||
// tapRow taps the list row that best matches the query. It retries the dump
|
||||
// until a matching row is on screen, then taps a point derived from that row's
|
||||
// own bounds.
|
||||
func tapRow(r *adb.Runner, rec *Recipe, query string, timeout time.Duration, w, h int, sr *StepResult) error {
|
||||
// own bounds. It returns the chosen row's title, which later steps can use as
|
||||
// the "row" value.
|
||||
func tapRow(r *adb.Runner, rec *Recipe, query string, timeout time.Duration, w, h int, sr *StepResult) (string, error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
var lastErr error
|
||||
for time.Now().Before(deadline) {
|
||||
@@ -365,11 +409,11 @@ func tapRow(r *adb.Runner, rec *Recipe, query string, timeout time.Duration, w,
|
||||
row, rerr := hier.BestRow(query, rec.RowID, w, h)
|
||||
if rerr == nil {
|
||||
if err := ui.Tap(r, row.Centre.X, row.Centre.Y); err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
sr.Detail = fmt.Sprintf("tapped row %q at (%d,%d) from %s", row.Title, row.Centre.X, row.Centre.Y,
|
||||
strings.Join(row.Texts, " / "))
|
||||
return nil
|
||||
return row.Title, nil
|
||||
}
|
||||
lastErr = rerr
|
||||
} else {
|
||||
@@ -380,7 +424,64 @@ func tapRow(r *adb.Runner, rec *Recipe, query string, timeout time.Duration, w,
|
||||
if lastErr == nil {
|
||||
lastErr = fmt.Errorf("no result row matches %q", query)
|
||||
}
|
||||
return describeFailure(r, rec, lastErr)
|
||||
return "", describeFailure(r, rec, lastErr)
|
||||
}
|
||||
|
||||
// tapRowMenu opens the More options menu of the row that matches query.
|
||||
//
|
||||
// The button is looked up *inside the chosen row's own subtree*, so the menu
|
||||
// always belongs to the row the query matched; the More options button of any
|
||||
// other row is never a candidate. The button carries no text of its own on the
|
||||
// real screen (its label sits on a child), so the search covers each candidate
|
||||
// button's subtree. It returns the chosen row's title, which later steps can use
|
||||
// as the "row" value to check that the menu that opened is that row's.
|
||||
func tapRowMenu(r *adb.Runner, rec *Recipe, query string, timeout time.Duration, w, h int, sr *StepResult) (string, error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
var lastErr error
|
||||
for time.Now().Before(deadline) {
|
||||
hier, err := ui.Dump(r)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
time.Sleep(ui.PollInterval)
|
||||
continue
|
||||
}
|
||||
row, rerr := hier.BestRow(query, rec.RowID, w, h)
|
||||
if rerr != nil {
|
||||
lastErr = rerr
|
||||
time.Sleep(ui.PollInterval)
|
||||
continue
|
||||
}
|
||||
button, berr := row.Action(rec.RowMenu)
|
||||
if berr != nil {
|
||||
return "", describeFailure(r, rec, berr)
|
||||
}
|
||||
cx, cy, cerr := button.Center()
|
||||
if cerr != nil {
|
||||
return "", cerr
|
||||
}
|
||||
if err := ui.Tap(r, cx, cy); err != nil {
|
||||
return "", err
|
||||
}
|
||||
sr.Detail = fmt.Sprintf("opened the %q menu of row %q at (%d,%d)", rec.RowMenu, row.Title, cx, cy)
|
||||
return row.Title, nil
|
||||
}
|
||||
if lastErr == nil {
|
||||
lastErr = fmt.Errorf("no result row matches %q", query)
|
||||
}
|
||||
return "", describeFailure(r, rec, lastErr)
|
||||
}
|
||||
|
||||
// stepFailure is the message for a failed step. A step's own "else" reason
|
||||
// comes first, because it names the real problem, and the technical detail
|
||||
// follows so the screen Artemis saw is still in the record (FR-8).
|
||||
func stepFailure(sr StepResult, step Step) string {
|
||||
if step.Reason == "" {
|
||||
return sr.Error
|
||||
}
|
||||
if sr.Error == "" {
|
||||
return step.Reason
|
||||
}
|
||||
return step.Reason + ": " + sr.Error
|
||||
}
|
||||
|
||||
func sendKey(r *adb.Runner, name string, sr *StepResult) error {
|
||||
|
||||
Reference in New Issue
Block a user