High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
test: pin the fromSurface decision itself, which a headless capture cannot distinguish (PIN-051)
Luigi Agostid0bd74524c70
diff --git a/internal/browsers/chrome/instance.go b/internal/browsers/chrome/instance.goindex 47ed2247..7fd3b304 100644--- a/internal/browsers/chrome/instance.go+++ b/internal/browsers/chrome/instance.go@@ -51,18 +51,7 @@ func (i *Instance) CaptureScreenshot(ctx context.Context, format string, qualitycdpFormat = page.CaptureScreenshotFormatJpeg}- // fromSurface=false reads the renderer's current view directly instead- // of waiting for a 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.- // In headless Chrome the flag is a no-op.- //- // It also discards the clip: reading the view returns the whole- // viewport whatever region was asked for. So a clipped capture has to- // pay the surface path, and the BringToFront above is what keeps that- // affordable. Screencast polls with a nil clip and keeps the fast read.- shot := page.CaptureScreenshot().WithFormat(cdpFormat).WithFromSurface(clip != nil)+ shot := page.CaptureScreenshot().WithFormat(cdpFormat).WithFromSurface(captureFromSurface(clip))if clip != nil {shot = shot.WithClip(&page.Viewport{X: clip.X,@@ -85,6 +74,25 @@ func (i *Instance) CaptureScreenshot(ctx context.Context, format string, qualityreturn buf, nil}+// captureFromSurface reports whether a capture has to go through the+// compositor surface. Only a clipped one does.+//+// fromSurface=false reads the renderer's current view directly instead of+// waiting for a 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. In headless Chrome the flag is a+// no-op, which is why only this predicate, not a captured image, can pin the+// choice.+//+// Reading the view also discards the clip and returns the whole viewport+// whatever region was asked for, so a clipped capture pays the surface path and+// the BringToFront above is what keeps that affordable. Screencast polls with a+// nil clip and keeps the fast read.+func captureFromSurface(clip *cdptk.ScreenshotClip) bool {+ return clip != nil+}+func (i *Instance) Evaluate(ctx context.Context, expression string, result any, opts browsers.EvalOpts) error {var chromedpOpts []chromedp.EvaluateOptionif opts.AwaitPromise {diff --git a/internal/browsers/chrome/instance_from_surface_test.go b/internal/browsers/chrome/instance_from_surface_test.gonew file mode 100644index 00000000..1e077f44--- /dev/null+++ b/internal/browsers/chrome/instance_from_surface_test.go@@ -0,0 +1,32 @@+package chrome++import (+ "testing"++ "github.com/pinchtab/pinchtab/internal/cdptk"+)++// The browser-level clip tests cannot pin this choice on their own: in headless+// Chrome fromSurface is a no-op, so an always-true "simplification" produces+// byte-identical images and passes them. This is the guard that fails instead —+// the unclipped screencast poll must keep the direct view read.+func TestCaptureFromSurfaceOnlyForClippedCaptures(t *testing.T) {+ tests := []struct {+ name string+ clip *cdptk.ScreenshotClip+ want bool+ }{+ {name: "no clip keeps the fast view read", clip: nil, want: false},+ {name: "clip needs the compositor surface", clip: &cdptk.ScreenshotClip{Width: 120, Height: 60, Scale: 1}, want: true},+ {name: "offset clip needs the compositor surface", clip: &cdptk.ScreenshotClip{X: 40, Y: 60, Width: 120, Height: 60, Scale: 1}, want: true},+ {name: "zero-sized clip is still a clip", clip: &cdptk.ScreenshotClip{}, want: true},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := captureFromSurface(tt.clip); got != tt.want {+ t.Errorf("captureFromSurface(%+v) = %v, want %v", tt.clip, got, tt.want)+ }+ })+ }+}