Skip to content

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

test(cdptk): check the fromSurface rule per-GOOS instead of only the host's half

Darshan Jain6127e101a520
diff --git a/internal/cdptk/screenshot_rule_test.go b/internal/cdptk/screenshot_rule_test.go
new file mode 100644
index 00000000..0a2be0d7
--- /dev/null
+++ b/internal/cdptk/screenshot_rule_test.go
@@ -0,0 +1,50 @@
+package cdptk
+
+import (
+ "testing"
+
+ "github.com/chromedp/cdproto/page"
+)
+
+// The fromSurface rule is platform-dependent: Windows always composites a fresh
+// surface, because the read-the-view fast path returns a blank image there
+// (issue #619). The exported CaptureFromSurface reads runtime.GOOS, so a table
+// driven through it can only ever assert the rule for the machine running the
+// test — and it asserted the non-Windows answers, which is why it failed on
+// Windows while leaving the Windows clause untested everywhere else.
+//
+// captureFromSurface takes goos precisely so the whole rule can be checked from
+// any host. This is that table.
+func TestCaptureFromSurfaceRulePerGOOS(t *testing.T) {
+ clip := &page.Viewport{Width: 120, Height: 60, Scale: 1}
+
+ for _, tc := range []struct {
+ name string
+ goos string
+ beyondViewport bool
+ clip *page.Viewport
+ want bool
+ }{
+ {name: "linux plain capture keeps the fast read", goos: "linux", want: false},
+ {name: "darwin plain capture keeps the fast read", goos: "darwin", want: false},
+ {name: "windows always composites a surface", goos: "windows", want: true},
+
+ {name: "linux any clip needs the surface", goos: "linux", clip: clip, want: true},
+ {name: "darwin any clip needs the surface", goos: "darwin", clip: clip, want: true},
+ {name: "windows with a clip still needs it", goos: "windows", clip: clip, want: true},
+
+ {name: "linux beyond viewport needs it with no clip", goos: "linux", beyondViewport: true, want: true},
+ {name: "darwin beyond viewport needs it with no clip", goos: "darwin", beyondViewport: true, want: true},
+ {name: "windows beyond viewport needs it too", goos: "windows", beyondViewport: true, want: true},
+
+ {name: "linux both", goos: "linux", beyondViewport: true, clip: clip, want: true},
+ {name: "windows both", goos: "windows", beyondViewport: true, clip: clip, want: true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := captureFromSurface(tc.goos, tc.beyondViewport, tc.clip); got != tc.want {
+ t.Errorf("captureFromSurface(%q, %v, %+v) = %v, want %v",
+ tc.goos, tc.beyondViewport, tc.clip, got, tc.want)
+ }
+ })
+ }
+}
diff --git a/internal/cdptk/screenshot_test.go b/internal/cdptk/screenshot_test.go
index a6c7cc01..791581c6 100644
--- a/internal/cdptk/screenshot_test.go
+++ b/internal/cdptk/screenshot_test.go
@@ -8,6 +8,7 @@ import (
"go/token"
"path"
"path/filepath"
+ "runtime"
"strings"
"testing"
"time"
@@ -291,23 +292,34 @@ func TestAnnotationRectForNodeAppliesFrameOffset(t *testing.T) {
}
// The fromSurface rule had two implementations in packages that cannot import each other,
-// and they had already drifted once. This is the table that now stands for both.
+// and they had already drifted once. The rule itself is now tabled per-GOOS in the
+// in-package TestCaptureFromSurfaceRulePerGOOS, which is the only place it can be checked
+// whole: this exported wrapper reads runtime.GOOS, so from out here only the host's own
+// answers are knowable. This covers what is left — that the wrapper asks the rule about
+// the machine it is running on, rather than deciding anything itself.
+//
+// It used to assert want:false for a plain capture unconditionally, which is the
+// non-Windows answer. Windows always composites a surface (issue #619), so the row was
+// wrong on the one platform whose clause it was accidentally exercising.
func TestCaptureFromSurface(t *testing.T) {
+ clip := &page.Viewport{Width: 120, Height: 60, Scale: 1}
+ hostAlwaysComposites := runtime.GOOS == "windows"
+
for _, tc := range []struct {
name string
beyondViewport bool
clip *page.Viewport
want bool
}{
- {name: "plain capture keeps the fast read", beyondViewport: false, clip: nil, want: false},
- {name: "any clip needs the surface", beyondViewport: false, clip: &page.Viewport{Width: 120, Height: 60, Scale: 1}, want: true},
- {name: "a native-scale clip still needs it", beyondViewport: false, clip: &page.Viewport{Width: 120, Height: 60, Scale: 1}, want: true},
- {name: "beyond viewport needs it with no clip", beyondViewport: true, clip: nil, want: true},
- {name: "both", beyondViewport: true, clip: &page.Viewport{Width: 10, Height: 10, Scale: 1}, want: true},
+ {name: "plain capture keeps the fast read where the host allows it", want: hostAlwaysComposites},
+ {name: "any clip needs the surface", clip: clip, want: true},
+ {name: "beyond viewport needs it with no clip", beyondViewport: true, want: true},
+ {name: "both", beyondViewport: true, clip: clip, want: true},
} {
t.Run(tc.name, func(t *testing.T) {
if got := cdptk.CaptureFromSurface(tc.beyondViewport, tc.clip); got != tc.want {
- t.Errorf("cdptk.CaptureFromSurface(%v, %+v) = %v, want %v", tc.beyondViewport, tc.clip, got, tc.want)
+ t.Errorf("cdptk.CaptureFromSurface(%v, %+v) = %v, want %v (GOOS=%s)",
+ tc.beyondViewport, tc.clip, got, tc.want, runtime.GOOS)
}
})
}