Skip to content

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

fix: harden cli errors and windows screenshots

Luigi Agosti52074eb5c179
diff --git a/internal/cdptk/screenshot.go b/internal/cdptk/screenshot.go
index 9919c967..1ff9d0db 100644
--- a/internal/cdptk/screenshot.go
+++ b/internal/cdptk/screenshot.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log/slog"
+ "runtime"
"strings"
"github.com/chromedp/cdproto/page"
@@ -123,7 +124,9 @@ func ClipForNode(ctx context.Context, nodeID int64, css1x bool) (*ScreenshotClip
// fresh compositor surface frame. On idle pages in headed browsers (e.g. Cloak) the
// surface stops swapping frames, so the default fromSurface=true blocks until the action
// deadline (~30s) — stalling one-shot screenshots and polling screencast alike. That is
-// why the nil-clip fast path matters and must be preserved.
+// why the nil-clip fast path matters and must be preserved outside Windows. Windows
+// headless Chrome with software rendering can return an unpainted white view through
+// that path, so it always requests a freshly composited surface.
//
// Anything that changes the captured region needs the page recomposited, which only
// happens with fromSurface=true: capture-beyond-viewport, a render-time rescale, and the
@@ -140,7 +143,11 @@ func ClipForNode(ctx context.Context, nodeID int64, css1x bool) (*ScreenshotClip
// The nil-clip pair matches on dimensions, which is why an unclipped browser test cannot
// pin this choice; hence the table test beside this function.
func CaptureFromSurface(beyondViewport bool, clip *page.Viewport) bool {
- return beyondViewport || clip != nil
+ return captureFromSurface(runtime.GOOS, beyondViewport, clip)
+}
+
+func captureFromSurface(goos string, beyondViewport bool, clip *page.Viewport) bool {
+ return goos == "windows" || beyondViewport || clip != nil
}
// captureRefusal is the CDP error a renderer returns when it will not serve the
diff --git a/internal/cdptk/screenshot_fallback_test.go b/internal/cdptk/screenshot_fallback_test.go
index 9787e21e..4efbc90e 100644
--- a/internal/cdptk/screenshot_fallback_test.go
+++ b/internal/cdptk/screenshot_fallback_test.go
@@ -11,6 +11,12 @@ import (
"github.com/pinchtab/pinchtab/internal/srccensus"
)
+func TestWindowsViewportCaptureUsesSurface(t *testing.T) {
+ if !captureFromSurface("windows", false, nil) {
+ t.Fatal("Windows viewport capture used the read-the-view path")
+ }
+}
+
// The property is that a REFUSAL of the read-the-view fast path is handled, whatever the
// clip — not that an unclipped capture works. A browser fixture cannot pin this: the
// refusal depends on whether the renderer's view is available, which no test can withhold
diff --git a/internal/cli/actions/actions_screenshot.go b/internal/cli/actions/actions_screenshot.go
index b2601fc3..df41c340 100644
--- a/internal/cli/actions/actions_screenshot.go
+++ b/internal/cli/actions/actions_screenshot.go
@@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"net/url"
+ "path/filepath"
+ "strings"
"time"
"github.com/pinchtab/pinchtab/internal/cli"
@@ -38,7 +40,11 @@ func Screenshot(client *http.Client, base, token string, cmd *cobra.Command) {
annotate, _ := cmd.Flags().GetBool("annotate")
format, _ := cmd.Flags().GetString("format")
if format == "" {
- format = "jpeg"
+ if strings.EqualFold(filepath.Ext(outFile), ".png") {
+ format = "png"
+ } else {
+ format = "jpeg"
+ }
}
if v, _ := cmd.Flags().GetString("quality"); v != "" {
params.Set("quality", v)
diff --git a/internal/cli/actions/actions_screenshot_test.go b/internal/cli/actions/actions_screenshot_test.go
index 5027b365..0805e1d0 100644
--- a/internal/cli/actions/actions_screenshot_test.go
+++ b/internal/cli/actions/actions_screenshot_test.go
@@ -43,3 +43,19 @@ func TestScreenshot(t *testing.T) {
t.Errorf("unexpected content: %s", string(data))
}
}
+
+func TestScreenshotInfersPNGFromOutputExtension(t *testing.T) {
+ m := newMockServer()
+ m.response = "FAKEPNGDATA"
+ defer m.close()
+
+ outFile := filepath.Join(t.TempDir(), "screenshot.png")
+ cmd := &cobra.Command{}
+ cmd.Flags().String("output", outFile, "")
+ cmd.Flags().String("format", "", "")
+ Screenshot(m.server.Client(), m.base(), "", cmd)
+
+ if !strings.Contains(m.lastQuery, "format=png") {
+ t.Fatalf("query = %q, want format=png inferred from output extension", m.lastQuery)
+ }
+}
diff --git a/tests/e2e/scenarios/cli/files-basic.sh b/tests/e2e/scenarios/cli/files-basic.sh
index 0b4c4c7a..c03c49b0 100644
--- a/tests/e2e/scenarios/cli/files-basic.sh
+++ b/tests/e2e/scenarios/cli/files-basic.sh
@@ -5,16 +5,38 @@ GROUP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${GROUP_DIR}/../../helpers/cli.sh"
start_test "pinchtab screenshot"
+
+FIRST_SCREENSHOT=/tmp/e2e-screenshot-first.png
+SECOND_SCREENSHOT=/tmp/e2e-screenshot-second.png
+rm -f "$FIRST_SCREENSHOT" "$SECOND_SCREENSHOT"
+
pt_ok nav "${FIXTURES_URL}/index.html"
-pt_ok screenshot -o /tmp/e2e-screenshot-test.jpg
-if [ -f /tmp/e2e-screenshot-test.jpg ]; then
- echo -e " ${GREEN}✓${NC} screenshot file created"
- ((ASSERTIONS_PASSED++)) || true
- rm -f /tmp/e2e-screenshot-test.jpg
+pt_ok screenshot -o "$FIRST_SCREENSHOT"
+pt_ok nav "${FIXTURES_URL}/buttons.html"
+pt_ok screenshot -o "$SECOND_SCREENSHOT"
+
+assert_file_exists "$FIRST_SCREENSHOT" "first screenshot file created"
+assert_file_exists "$SECOND_SCREENSHOT" "second screenshot file created"
+
+PNG_MAGIC=""
+if [ -f "$FIRST_SCREENSHOT" ]; then
+ PNG_MAGIC=$(od -An -tx1 -N8 "$FIRST_SCREENSHOT" | tr -d ' \n')
+fi
+if [ "$PNG_MAGIC" = "89504e470d0a1a0a" ]; then
+ pass_assert ".png output contains PNG bytes"
else
- echo -e " ${RED}✗${NC} screenshot file not created"
- ((ASSERTIONS_FAILED++)) || true
+ fail_assert ".png output contains PNG bytes"
fi
+
+if [ ! -f "$FIRST_SCREENSHOT" ] || [ ! -f "$SECOND_SCREENSHOT" ]; then
+ fail_assert "different pages produce different viewport screenshots"
+elif cmp -s "$FIRST_SCREENSHOT" "$SECOND_SCREENSHOT"; then
+ fail_assert "different pages produce different viewport screenshots"
+else
+ pass_assert "different pages produce different viewport screenshots"
+fi
+
+rm -f "$FIRST_SCREENSHOT" "$SECOND_SCREENSHOT"
end_test
start_test "pinchtab pdf"
diff --git a/tests/e2e/scenarios/cli/system-extended.sh b/tests/e2e/scenarios/cli/system-extended.sh
index 2017c227..ae013a9d 100644
--- a/tests/e2e/scenarios/cli/system-extended.sh
+++ b/tests/e2e/scenarios/cli/system-extended.sh
@@ -4,6 +4,17 @@
GROUP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${GROUP_DIR}/../../helpers/cli.sh"
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab transport failure exits non-zero"
+
+ORIGINAL_E2E_SERVER="$E2E_SERVER"
+E2E_SERVER="http://127.0.0.1:1"
+pt health
+E2E_SERVER="$ORIGINAL_E2E_SERVER"
+assert_exit_code 1 "connection failure is reported to automation"
+
+end_test
+
# ─────────────────────────────────────────────────────────────────
start_test "pinchtab daemon (non-interactive shows status)"