High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(select): return 4xx option_not_found with details.available when a select value matches no option (PIN-404)
Luigi Agosti27c5e1bb04b5
diff --git a/internal/bridge/cdpops/element_ops.go b/internal/bridge/cdpops/element_ops.goindex a9b8ccaa..a53b0a3f 100644--- a/internal/bridge/cdpops/element_ops.go+++ b/internal/bridge/cdpops/element_ops.go@@ -8,6 +8,25 @@ import ("github.com/chromedp/chromedp")+// SelectOption is one <option> of a <select>, as its value attribute and its+// trimmed visible text.+type SelectOption struct {+ Value string `json:"value"`+ Text string `json:"text"`+}++// NoOptionMatchError reports a <select> whose requested value matched no option.+// It carries every option so the caller can pick one without re-inspecting, and+// so a handler can classify the miss as a client error rather than a server fault.+type NoOptionMatchError struct {+ Value string+ Available []SelectOption+}++func (e *NoOptionMatchError) Error() string {+ return fmt.Sprintf("no option matched %q by value or visible text", e.Value)+}+func FillByNodeID(ctx context.Context, nodeID int64, value string) error {return chromedp.Run(ctx,chromedp.ActionFunc(func(ctx context.Context) error {@@ -179,7 +198,11 @@ func SelectByNodeID(ctx context.Context, nodeID int64, value string) error {return fmt.Errorf("parse select result: %w", err)}if !outcome.OK {- return fmt.Errorf("%s", outcome.Error)+ available := make([]SelectOption, len(outcome.Available))+ for i, o := range outcome.Available {+ available[i] = SelectOption{Value: o.Value, Text: o.Text}+ }+ return &NoOptionMatchError{Value: value, Available: available}}return nil}),diff --git a/internal/handlers/action_select_test.go b/internal/handlers/action_select_test.gonew file mode 100644index 00000000..d106cd3b--- /dev/null+++ b/internal/handlers/action_select_test.go@@ -0,0 +1,75 @@+package handlers++import (+ "encoding/json"+ "net/http"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/bridge"+ bridgecdpops "github.com/pinchtab/pinchtab/internal/bridge/cdpops"+)++// A select whose value matches no option is a client input error, not a server+// fault: the element was found and resolved. It must classify as 4xx, carry the+// non-retryable option_not_found code, and hand back the real options so the+// caller can pick one without re-inspecting.+func TestSelectWithNoMatchingOptionReturns4xxWithAvailable(t *testing.T) {+ available := []bridgecdpops.SelectOption{+ {Value: "red", Text: "Red"},+ {Value: "green", Text: "Green"},+ }+ mb := &mockBridge{+ availableActions: []string{bridge.ActionSelect},+ executeActionErr: &bridgecdpops.NoOptionMatchError{Value: "Purple", Available: available},+ }+ w, failure := postActionBody(t, mb, `{"kind":"select","nodeId":42,"value":"Purple","tabId":"tab1"}`)++ if w.Code < 400 || w.Code >= 500 {+ t.Fatalf("status = %d, want 4xx; a resolved element with an unmatched value is a client error, not 500\nbody: %s", w.Code, w.Body.String())+ }+ if failure.Code != "option_not_found" {+ t.Errorf("code = %q, want option_not_found", failure.Code)+ }+ if failure.Retryable {+ t.Errorf("retryable = true, want false; retrying the identical value can never match an option")+ }++ var resp struct {+ Details struct {+ Available []bridgecdpops.SelectOption `json:"available"`+ Hint string `json:"hint"`+ } `json:"details"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatalf("decode body: %v\nbody: %s", err, w.Body.String())+ }+ if len(resp.Details.Available) != len(available) {+ t.Fatalf("details.available = %v, want the %d real options", resp.Details.Available, len(available))+ }+ for i, want := range available {+ if resp.Details.Available[i] != want {+ t.Errorf("details.available[%d] = %+v, want %+v", i, resp.Details.Available[i], want)+ }+ }+ for _, want := range available {+ if !strings.Contains(resp.Details.Hint, want.Value) {+ t.Errorf("details.hint = %q, want it to name option %q so the CLI renders it", resp.Details.Hint, want.Value)+ }+ }+}++// A genuinely missing element still returns the element-not-found status: the+// new option-not-found branch sits after the ErrTargetNotFound branch and keys+// on a distinct type, so it must not intercept the not-found path.+func TestSelectOnMissingElementStillNotFound(t *testing.T) {+ mb := &mockBridge{+ availableActions: []string{bridge.ActionSelect},+ executeActionErr: ErrTargetNotFound,+ }+ w, _ := postActionBody(t, mb, `{"kind":"select","nodeId":42,"value":"Red","tabId":"tab1"}`)++ if w.Code != http.StatusNotFound {+ t.Fatalf("status = %d, want 404 for a missing element\nbody: %s", w.Code, w.Body.String())+ }+}diff --git a/internal/handlers/actions.go b/internal/handlers/actions.goindex a8e81c0a..d244ecc2 100644--- a/internal/handlers/actions.go+++ b/internal/handlers/actions.go@@ -16,6 +16,7 @@ import ("github.com/pinchtab/pinchtab/internal/activity""github.com/pinchtab/pinchtab/internal/api/types""github.com/pinchtab/pinchtab/internal/bridge"+ bridgecdpops "github.com/pinchtab/pinchtab/internal/bridge/cdpops""github.com/pinchtab/pinchtab/internal/browserops""github.com/pinchtab/pinchtab/internal/browsers""github.com/pinchtab/pinchtab/internal/config"@@ -117,6 +118,34 @@ func writeTargetNotFound(w http.ResponseWriter, err error, rr *recovery.Recoveryhttpx.ErrorCode(w, http.StatusNotFound, "ref_not_found", err.Error(), false, details)}+// writeNoOptionMatch classifies a select whose value matched no option as a+// client error: the element was found and resolved, so retrying the identical+// request can never succeed. details.available carries the structured option+// list, and details.hint renders it through the CLI's generic guidance path so+// the caller sees the choices without re-inspecting the page.+func writeNoOptionMatch(w http.ResponseWriter, kind string, err error, noOption *bridgecdpops.NoOptionMatchError) {+ httpx.ErrorCode(w, http.StatusUnprocessableEntity, "option_not_found",+ fmt.Sprintf("action %s: %v", kind, err), false, map[string]any{+ "available": noOption.Available,+ "hint": availableOptionsHint(noOption.Available),+ })+}++func availableOptionsHint(options []bridgecdpops.SelectOption) string {+ if len(options) == 0 {+ return "the <select> has no options"+ }+ rendered := make([]string, len(options))+ for i, o := range options {+ if o.Text == "" || o.Text == o.Value {+ rendered[i] = fmt.Sprintf("%q", o.Value)+ continue+ }+ rendered[i] = fmt.Sprintf("%q (%s)", o.Value, o.Text)+ }+ return "available options: " + strings.Join(rendered, ", ")+}+// actionFailureIsRetryable answers the only question the flag promises: could repeating the// IDENTICAL request plausibly succeed. It used to be !submitClick, which says whether the// caller declared a submit and nothing about the failure, so every unresolvable ref and@@ -660,6 +689,11 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {writeTargetNotFound(w, actionErr, recoveryResult)return}+ var noOption *bridgecdpops.NoOptionMatchError+ if errors.As(actionErr, &noOption) {+ writeNoOptionMatch(w, req.Kind, actionErr, noOption)+ return+ }dispatchMayHaveLanded := submitClickvar details map[string]anyif dispatchMayHaveLanded {