Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(handlers): batch action errors carry the --dialog-action remedy once (PIN-005)

Luigi Agosti50bc6a37e496
diff --git a/internal/bridge/errors.go b/internal/bridge/errors.go
index a7f192b8..548b35e8 100644
--- a/internal/bridge/errors.go
+++ b/internal/bridge/errors.go
@@ -14,13 +14,11 @@ func (e *TabLimitError) Error() string {
return fmt.Sprintf("tab limit reached (%d/%d)", e.Current, e.Max)
}
-// ErrDialogBlocking is returned when a click action is blocked by a
-// JavaScript dialog (alert/confirm/prompt) and no --dialog-action was provided.
type ErrDialogBlocking struct {
DialogType string
DialogMessage string
}
func (e *ErrDialogBlocking) Error() string {
- return fmt.Sprintf("click blocked by JavaScript dialog (%s: %q) — use --dialog-action accept|dismiss", e.DialogType, e.DialogMessage)
+ return fmt.Sprintf("click blocked by JavaScript dialog (%s: %q)", e.DialogType, e.DialogMessage)
}
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index cf5ca59d..72113cbd 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -143,7 +143,7 @@ func (h *Handlers) mapDialogBlockingError(err error, kind, tabID string) (string
func (h *Handlers) dialogAwareActionError(err error, kind, tabID, fallback string) string {
if message, _, ok := h.mapDialogBlockingError(err, kind, tabID); ok {
- return message
+ return dialogBlockedStepError(message)
}
return fallback
}
diff --git a/internal/handlers/dialog_blocked.go b/internal/handlers/dialog_blocked.go
index 8d0e493d..af0a95c3 100644
--- a/internal/handlers/dialog_blocked.go
+++ b/internal/handlers/dialog_blocked.go
@@ -13,11 +13,14 @@ const dialogBlockedCode = "dialog_blocked"
const dialogBlockedHint = "a JavaScript dialog is open on this tab and blocks every page interaction until it is answered. It belongs to this tab, so retrying, activating the tab or opening a fresh one cannot clear it. Use pinchtab dialog dismiss to cancel it instead of accepting, or pass --dialog-action accept|dismiss on the action that opens the dialog."
-// Accepting is the remedy because it is the answer that lets the blocked interaction
-// proceed; dismissing is the other choice a reader may want and lives in the hint. The
-// old value offered both as "accept|dismiss", which a shell reads as a pipeline.
var dialogBlockedRemedy = remedy.Declare("pinchtab dialog accept")
+const dialogBlockedStepRemedy = "answer it with pinchtab dialog accept or pinchtab dialog dismiss, or pass --dialog-action accept on the action that opens the dialog"
+
+func dialogBlockedStepError(message string) string {
+ return message + "; " + dialogBlockedStepRemedy
+}
+
func pendingTabDialog(b bridge.BridgeAPI, tabID string) *bridge.DialogState {
if b == nil || tabID == "" {
return nil
@@ -48,9 +51,6 @@ func writeDialogBlocked(w http.ResponseWriter, tabID string, dialog *bridge.Dial
httpx.ErrorCode(w, http.StatusConflict, dialogBlockedCode, message, false, dialogBlockedDetails(tabID, dialog))
}
-// refuseIfDialogBlocked answers the request with dialog_blocked when a dialog is
-// pending on the tab. The lookup is in-memory, so it never touches the page it is
-// reporting on.
func (h *Handlers) refuseIfDialogBlocked(w http.ResponseWriter, tabID string) bool {
dialog := pendingTabDialog(h.Bridge, tabID)
if dialog == nil {
diff --git a/internal/handlers/dialog_blocked_step_test.go b/internal/handlers/dialog_blocked_step_test.go
new file mode 100644
index 00000000..c5a1e40d
--- /dev/null
+++ b/internal/handlers/dialog_blocked_step_test.go
@@ -0,0 +1,165 @@
+package handlers
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+type dialogRaisingBridge struct {
+ mockBridge
+ dialog *bridge.DialogState
+ err error
+}
+
+func (b *dialogRaisingBridge) ExecuteAction(context.Context, string, bridge.ActionRequest) (map[string]any, error) {
+ b.GetDialogManager().SetPending("tab1", b.dialog)
+ return nil, b.err
+}
+
+var dialogBlockedBranches = []struct {
+ name string
+ err func(*bridge.DialogState) error
+ message string
+}{
+ {"click timeout", func(*bridge.DialogState) error { return context.DeadlineExceeded }, "timed out"},
+ {"dialog blocking", func(d *bridge.DialogState) error {
+ return &bridge.ErrDialogBlocking{DialogType: d.Type, DialogMessage: d.Message}
+ }, "click blocked"},
+}
+
+func TestBatchAndMacroStepsCarryTheDialogRemedyExactlyOnce(t *testing.T) {
+ dialog := &bridge.DialogState{Type: "confirm", Message: "leave page?"}
+ for _, branch := range dialogBlockedBranches {
+ for _, tc := range []struct{ name, path, body string }{
+ {"batch", "/actions", `{"tabId":"tab1","actions":[{"kind":"click"}]}`},
+ {"macro", "/macro", `{"tabId":"tab1","steps":[{"kind":"click"}]}`},
+ } {
+ t.Run(branch.name+"/"+tc.name, func(t *testing.T) {
+ b := &dialogRaisingBridge{dialog: dialog, err: branch.err(dialog)}
+ h := New(b, &config.RuntimeConfig{AllowMacro: true}, nil, nil, nil)
+ req := httptest.NewRequest(http.MethodPost, tc.path, bytes.NewReader([]byte(tc.body)))
+ req.Header.Set("Content-Type", "application/json")
+ rec := httptest.NewRecorder()
+ if tc.path == "/macro" {
+ h.HandleMacro(rec, req)
+ } else {
+ h.HandleActions(rec, req)
+ }
+ if rec.Code != http.StatusOK {
+ t.Fatalf("%s answered %d: %s", tc.path, rec.Code, rec.Body.String())
+ }
+ var envelope batchEnvelope
+ if err := json.Unmarshal(rec.Body.Bytes(), &envelope); err != nil {
+ t.Fatalf("decode: %v: %s", err, rec.Body.String())
+ }
+ if len(envelope.Results) != 1 || envelope.Results[0].Success {
+ t.Fatalf("want one failed result: %s", rec.Body.String())
+ }
+ got := envelope.Results[0].Error
+ if !strings.Contains(got, branch.message) || !strings.Contains(got, dialog.Message) {
+ t.Fatalf("step error does not name the condition: %q", got)
+ }
+ if n := strings.Count(got, dialogBlockedStepRemedy); n != 1 {
+ t.Fatalf("remedy sentence appears %d times, want 1: %q", n, got)
+ }
+ if strings.Contains(got, "accept|dismiss") {
+ t.Fatalf("step error uses the pipeline form: %q", got)
+ }
+ })
+ }
+ }
+}
+
+func TestSingleActionDialogBlockedMessageCarriesNoRemedy(t *testing.T) {
+ dialog := &bridge.DialogState{Type: "alert", Message: "saved"}
+ for _, branch := range dialogBlockedBranches {
+ t.Run(branch.name, func(t *testing.T) {
+ b := &dialogRaisingBridge{dialog: dialog, err: branch.err(dialog)}
+ h := New(b, &config.RuntimeConfig{}, nil, nil, nil)
+ rec := httptest.NewRecorder()
+ body := bytes.NewReader([]byte(`{"kind":"click","tabId":"tab1"}`))
+ h.HandleAction(rec, httptest.NewRequest(http.MethodPost, "/action", body))
+ decodeDialogBlocked(t, rec, dialog)
+ var failure dialogBlockedFailure
+ if err := json.Unmarshal(rec.Body.Bytes(), &failure); err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(failure.Error, branch.message) {
+ t.Fatalf("message does not name the condition: %q", failure.Error)
+ }
+ for _, remedy := range []string{dialogBlockedStepRemedy, "--dialog-action", "pinchtab dialog"} {
+ if strings.Contains(failure.Error, remedy) {
+ t.Fatalf("message carries the remedy %q: %q", remedy, failure.Error)
+ }
+ }
+ if failure.Details.Hint != dialogBlockedHint {
+ t.Fatalf("hint = %q, want %q", failure.Details.Hint, dialogBlockedHint)
+ }
+ })
+ }
+}
+
+func TestErrDialogBlockingCarriesNoRemedy(t *testing.T) {
+ got := (&bridge.ErrDialogBlocking{DialogType: "prompt", DialogMessage: "name?"}).Error()
+ for _, remedy := range []string{"--dialog-action", "pinchtab dialog", dialogBlockedStepRemedy} {
+ if strings.Contains(got, remedy) {
+ t.Fatalf("bridge error carries the remedy %q: %q", remedy, got)
+ }
+ }
+ if !strings.Contains(got, `prompt: "name?"`) {
+ t.Fatalf("bridge error does not name the dialog: %q", got)
+ }
+}
+
+func TestDialogActionRemedyLiteralHasOneOwnerBesideTheFlagHelp(t *testing.T) {
+ const literal = "dialog-action accept"
+ allowed := map[string]bool{
+ "internal/handlers/dialog_blocked.go": true,
+ "cmd/pinchtab/cmd_cli_register.go": true,
+ }
+ root := filepath.Join("..", "..")
+ found := map[string]bool{}
+ for _, dir := range []string{"internal", "cmd"} {
+ err := filepath.WalkDir(filepath.Join(root, dir), func(path string, d os.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
+ return err
+ }
+ f, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
+ if err != nil {
+ return err
+ }
+ ast.Inspect(f, func(n ast.Node) bool {
+ if lit, ok := n.(*ast.BasicLit); ok && lit.Kind == token.STRING && strings.Contains(lit.Value, literal) {
+ rel, _ := filepath.Rel(root, path)
+ found[filepath.ToSlash(rel)] = true
+ }
+ return true
+ })
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ if !found["internal/handlers/dialog_blocked.go"] {
+ t.Fatalf("dialog_blocked.go no longer owns the %q remedy: %v", literal, found)
+ }
+ for file := range found {
+ if !allowed[file] {
+ t.Errorf("%s spells the %q remedy; dialog_blocked.go is the one owner", file, literal)
+ }
+ }
+}