| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- // Package integration exercises real engines against a deterministic local site.
- package integration
- import (
- "bytes"
- "context"
- "encoding/json"
- "errors"
- "image/png"
- "net/http/httptest"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "time"
- "clusterflow.dev/cfbrowser"
- "clusterflow.dev/cfbrowser/api"
- "clusterflow.dev/cfbrowser/driver/cdp"
- native "clusterflow.dev/cfbrowser/driver/native"
- "clusterflow.dev/cfbrowser/internal/testsite"
- "clusterflow.dev/cfbrowser/policy"
- )
- type harness struct {
- m *cfbrowser.Manager
- owner api.Owner
- s api.SessionInfo
- p api.PageInfo
- server *httptest.Server
- }
- func engines(t *testing.T, fn func(*testing.T, api.Driver)) {
- t.Helper()
- if os.Getenv("CFBROWSER_INTEGRATION") != "1" {
- t.Skip("set CFBROWSER_INTEGRATION=1 to run real browser tests")
- }
- engine := os.Getenv("CFBROWSER_TEST_ENGINE")
- if engine != "" && engine != "both" && engine != "native" && engine != "cdp" {
- t.Fatalf("invalid CFBROWSER_TEST_ENGINE: %s", engine)
- }
- if engine != "native" {
- t.Run("cdp", func(t *testing.T) { fn(t, &cdp.Driver{}) })
- }
- if engine == "native" || engine == "both" || os.Getenv("CFBROWSER_NATIVE") == "1" {
- t.Run("native", func(t *testing.T) {
- fn(t, &native.Driver{WorkerPath: os.Getenv("CFBROWSER_WORKER"), Diagnostics: os.Stderr})
- })
- }
- }
- func setup(t *testing.T, driver api.Driver) *harness {
- t.Helper()
- server := httptest.NewServer(testsite.Handler())
- t.Cleanup(server.Close)
- m, e := cfbrowser.New(cfbrowser.Config{Executable: os.Getenv("CFBROWSER_EXECUTABLE"), NoSandbox: os.Getenv("CFBROWSER_TEST_NO_SANDBOX") == "1", Timeout: 30 * time.Second, Policy: policy.URLPolicy{AllowOrigins: []string{server.URL}}}, driver)
- if e != nil {
- t.Fatal(e)
- }
- t.Cleanup(func() {
- if e := m.Close(); e != nil {
- t.Error(e)
- }
- })
- o := api.Owner{EnvironmentID: "ssh1", ThreadID: "thread1", RunID: t.Name(), SSHSessionID: "connection1"}
- ctx := context.Background()
- s, e := m.CreateSession(ctx, o)
- if e != nil {
- t.Fatal(e)
- }
- p, e := m.NewPage(ctx, o, s.ID)
- if e != nil {
- t.Fatal(e)
- }
- if _, e = m.Navigate(ctx, o, s.ID, p.PageID, server.URL, "goto"); e != nil {
- t.Fatal(e)
- }
- return &harness{m: m, owner: o, s: s, p: p, server: server}
- }
- func (h *harness) snapshot(t *testing.T) api.Snapshot {
- t.Helper()
- s, e := h.m.Snapshot(context.Background(), h.owner, h.s.ID, h.p.PageID, api.SnapshotRequest{})
- if e != nil {
- t.Fatal(e)
- }
- return s
- }
- func ref(t *testing.T, s api.Snapshot, role, name string) string {
- t.Helper()
- for _, e := range s.Elements {
- if e.Role == role && e.Name == name {
- return e.Ref
- }
- }
- t.Fatalf("missing %s %s in %+v", role, name, s)
- return ""
- }
- func (h *harness) act(t *testing.T, s api.Snapshot, role, name, action, value string) {
- t.Helper()
- r, e := h.m.Act(context.Background(), h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: ref(t, s, role, name), Action: action, Value: value})
- if e != nil || r.Effect != "dispatched" {
- t.Fatalf("%s %s: %+v %v", action, name, r, e)
- }
- }
- func TestRealDynamicWorkflow(t *testing.T) {
- engines(t, func(t *testing.T, d api.Driver) {
- h := setup(t, d)
- ctx := context.Background()
- s := h.snapshot(t)
- encoded, _ := json.Marshal(s)
- if strings.Contains(string(encoded), "hidden-secret") {
- t.Fatal("password leaked into snapshot")
- }
- h.act(t, s, "textbox", "Query", "fill", "La2O3 科学 \"\\ test")
- capabilities, _ := h.m.Capabilities(ctx)
- if capabilities.Supports("trustedInput") {
- h.act(t, s, "textbox", "Query", "type", "X")
- h.act(t, s, "textbox", "Query", "press", "Backspace")
- h.act(t, s, "button", "Search", "hover", "")
- }
- h.act(t, s, "button", "Search", "click", "")
- if e := h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "Results for La2O3 科学", TimeoutMS: 3000}); e != nil {
- t.Fatal(e)
- }
- caps, e := h.m.Capabilities(ctx)
- if e != nil {
- t.Fatal(e)
- }
- want := "trusted=false"
- if caps.Supports("trustedInput") {
- want = "trusted=true"
- }
- if e = h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: want, TimeoutMS: 1000}); e != nil {
- t.Fatal("input trust contract mismatch", e)
- }
- table, e := h.m.Read(ctx, h.owner, h.s.ID, h.p.PageID, api.ReadRequest{Mode: "table"})
- if e != nil || len(table.Rows) != 2 || table.Rows[1][0] != "La2O3" {
- t.Fatal(table, e)
- }
- s = h.snapshot(t)
- h.act(t, s, "checkbox", "Enabled", "check", "")
- h.act(t, s, "checkbox", "Enabled", "uncheck", "")
- h.act(t, s, "combobox", "Format", "select", "xyz")
- small, e := h.m.Snapshot(ctx, h.owner, h.s.ID, h.p.PageID, api.SnapshotRequest{MaxBytes: 1024})
- if e != nil {
- t.Fatal(e)
- }
- b, _ := json.Marshal(small)
- if len(b) > 1024 {
- t.Fatal("wire budget exceeded")
- }
- if caps.Supports("screenshotViewport") {
- f, e := h.m.Capture(ctx, h.owner, h.s.ID, h.p.PageID)
- if e != nil || f.Size < 100 {
- t.Fatal(f, e)
- }
- dest := t.TempDir()
- if _, e = h.m.Export(ctx, h.owner, h.s.ID, f.ID, dest, "shot.png"); e != nil {
- t.Fatal(e)
- }
- if _, e = os.Stat(filepath.Join(dest, "shot.png")); e != nil {
- t.Fatal(e)
- }
- }
- })
- }
- func TestRealStaleRefsCancellationAndScope(t *testing.T) {
- engines(t, func(t *testing.T, d api.Driver) {
- h := setup(t, d)
- s := h.snapshot(t)
- search := ref(t, s, "button", "Search")
- h.act(t, s, "button", "Replace Search", "click", "")
- _, e := h.m.Act(context.Background(), h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: search, Action: "click"})
- var typed *api.Error
- if !errors.As(e, &typed) || typed.Code != "BROWSER_STALE_REF" {
- t.Fatal("replaced element was accepted", e)
- }
- other := h.owner
- other.RunID = "child-run"
- if _, e = h.m.Snapshot(context.Background(), other, h.s.ID, h.p.PageID, api.SnapshotRequest{}); e == nil {
- t.Fatal("cross-run page access")
- }
- ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
- defer cancel()
- started := time.Now()
- e = h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "never-appears"})
- if e == nil || time.Since(started) > 2*time.Second {
- t.Fatal("wait cancellation failed", e)
- }
- if _, e = h.m.Navigate(context.Background(), h.owner, h.s.ID, h.p.PageID, h.server.URL+"/cookie", "goto"); e != nil {
- t.Fatal(e)
- }
- _, e = h.m.Act(context.Background(), h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: search, Action: "click"})
- if e == nil {
- t.Fatal("navigation did not invalidate ref")
- }
- })
- }
- func TestRealCookieIsolationAndSessionLifecycle(t *testing.T) {
- engines(t, func(t *testing.T, d api.Driver) {
- h := setup(t, d)
- ctx := context.Background()
- p2, e := h.m.NewPage(ctx, h.owner, h.s.ID)
- if e != nil {
- t.Fatal(e)
- }
- if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, h.server.URL+"/cookie", "goto"); e != nil {
- t.Fatal(e)
- }
- read, e := h.m.Read(ctx, h.owner, h.s.ID, p2.PageID, api.ReadRequest{})
- if e != nil || !strings.Contains(read.Text, "cookie=active") {
- t.Fatal("same session lost cookie", read, e)
- }
- if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, h.server.URL, "goto"); e != nil {
- t.Fatal(e)
- }
- if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, "", "back"); e != nil {
- t.Fatal(e)
- }
- if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, "", "forward"); e != nil {
- t.Fatal(e)
- }
- if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, "", "reload"); e != nil {
- t.Fatal(e)
- }
- if e = h.m.ClosePage(ctx, h.owner, h.s.ID, p2.PageID); e != nil {
- t.Fatal(e)
- }
- other := h.owner
- other.RunID = "run2"
- s2, e := h.m.CreateSession(ctx, other)
- if e != nil {
- t.Fatal(e)
- }
- p3, e := h.m.NewPage(ctx, other, s2.ID)
- if e != nil {
- t.Fatal(e)
- }
- if _, e = h.m.Navigate(ctx, other, s2.ID, p3.PageID, h.server.URL+"/cookie", "goto"); e != nil {
- t.Fatal(e)
- }
- read, e = h.m.Read(ctx, other, s2.ID, p3.PageID, api.ReadRequest{})
- if e != nil || !strings.Contains(read.Text, "cookie=empty") {
- t.Fatal("cookie crossed session boundary", read, e)
- }
- if e = h.m.CloseSession(other, s2.ID); e != nil {
- t.Fatal(e)
- }
- if _, e = h.m.Read(ctx, other, s2.ID, p3.PageID, api.ReadRequest{}); e == nil {
- t.Fatal("closed session remained callable")
- }
- })
- }
- func TestRealAuthenticatedAndBlobDownloads(t *testing.T) {
- engines(t, func(t *testing.T, d api.Driver) {
- h := setup(t, d)
- caps, _ := h.m.Capabilities(context.Background())
- if !caps.Supports("downloads") {
- t.Skip("backend explicitly reports downloads unsupported")
- }
- s := h.snapshot(t)
- h.act(t, s, "link", "Download CIF", "click", "")
- h.act(t, s, "button", "Download blob", "click", "")
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- var files []api.FileInfo
- for {
- var e error
- files, e = h.m.Files(ctx, h.owner, h.s.ID)
- if e != nil {
- t.Fatal(e)
- }
- if len(files) >= 2 {
- break
- }
- select {
- case <-ctx.Done():
- t.Fatal("downloads did not complete")
- case <-time.After(50 * time.Millisecond):
- }
- }
- dest := t.TempDir()
- contents := map[string]string{}
- for _, f := range files {
- if _, e := h.m.Export(ctx, h.owner, h.s.ID, f.ID, dest, f.Name); e != nil {
- t.Fatal(e)
- }
- b, e := os.ReadFile(filepath.Join(dest, f.Name))
- if e != nil {
- t.Fatal(e)
- }
- contents[f.Name] = string(b)
- }
- if !strings.Contains(contents["sample.cif"], "data_La2O3") || contents["blob.txt"] != "blob-data" {
- t.Fatalf("incorrect downloads: %+v", contents)
- }
- })
- }
- func TestRealOpenShadowDOM(t *testing.T) {
- engines(t, func(t *testing.T, d api.Driver) {
- h := setup(t, d)
- ctx := context.Background()
- caps, _ := h.m.Capabilities(ctx)
- if !caps.Supports("openShadowDOM") {
- t.Skip("openShadowDOM unsupported")
- }
- if _, err := h.m.Navigate(ctx, h.owner, h.s.ID, h.p.PageID, h.server.URL+"/shadow", "goto"); err != nil {
- t.Fatal(err)
- }
- s := h.snapshot(t)
- for _, e := range s.Elements {
- if e.Name == "Closed secret button" {
- t.Fatal("closed shadow root exposed")
- }
- }
- h.act(t, s, "textbox", "Shadow Query", "fill", "La2O3")
- h.act(t, s, "button", "Shadow Search", "click", "")
- if err := h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "Shadow result La2O3", TimeoutMS: 2000}); err != nil {
- t.Fatal(err)
- }
- if err := h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "Shadow visible text", TimeoutMS: 2000}); err != nil {
- t.Fatal(err)
- }
- r, err := h.m.Read(ctx, h.owner, h.s.ID, h.p.PageID, api.ReadRequest{})
- if err != nil || (!strings.Contains(r.Text, "Shadow visible text") || !strings.Contains(r.Text, "Direct shadow text")) {
- t.Fatal(r, err)
- }
- table, err := h.m.Read(ctx, h.owner, h.s.ID, h.p.PageID, api.ReadRequest{Mode: "table"})
- if err != nil || len(table.Rows) != 1 || table.Rows[0][0] != "La2O3 shadow" {
- t.Fatal(table, err)
- }
- target := ref(t, s, "button", "Shadow Search")
- h.act(t, s, "button", "Replace shadow", "click", "")
- _, err = h.m.Act(ctx, h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: target, Action: "click"})
- var typed *api.Error
- if !errors.As(err, &typed) || typed.Code != "BROWSER_STALE_REF" {
- t.Fatal("replaced shadow ref accepted", err)
- }
- })
- }
- func TestRealBoundedFullPageCapture(t *testing.T) {
- engines(t, func(t *testing.T, d api.Driver) {
- h := setup(t, d)
- ctx := context.Background()
- caps, _ := h.m.Capabilities(ctx)
- if !caps.Supports("screenshotFullPage") {
- t.Skip("full-page capture unsupported on this backend")
- }
- if _, err := h.m.Navigate(ctx, h.owner, h.s.ID, h.p.PageID, h.server.URL+"/long", "goto"); err != nil {
- t.Fatal(err)
- }
- f, err := h.m.CapturePage(ctx, h.owner, h.s.ID, h.p.PageID, true)
- if err != nil {
- t.Fatal(err)
- }
- dest := t.TempDir()
- if _, err = h.m.Export(ctx, h.owner, h.s.ID, f.ID, dest, "full.png"); err != nil {
- t.Fatal(err)
- }
- data, err := os.ReadFile(filepath.Join(dest, "full.png"))
- if err != nil {
- t.Fatal(err)
- }
- img, err := png.Decode(bytes.NewReader(data))
- if err != nil || img.Bounds().Dy() < 2200 {
- t.Fatal("full document was not captured", err)
- }
- top := img.At(10, 100)
- bottom := img.At(10, img.Bounds().Dy()-10)
- if top == bottom {
- t.Fatal("blank capture")
- }
- if _, err = h.m.Navigate(ctx, h.owner, h.s.ID, h.p.PageID, h.server.URL+"/long?huge=1", "goto"); err != nil {
- t.Fatal(err)
- }
- _, err = h.m.CapturePage(ctx, h.owner, h.s.ID, h.p.PageID, true)
- var typed *api.Error
- if !errors.As(err, &typed) || typed.Code != "BROWSER_RESULT_LIMIT" {
- t.Fatal("oversized full page accepted", err)
- }
- })
- }
|