package cdp import ( "context" "os" "path/filepath" "testing" "clusterflow.dev/cfbrowser/api" "github.com/chromedp/cdproto/browser" ) func TestDiscoveryAndCapabilities(t *testing.T) { if _, e := FindExecutable(filepath.Join(t.TempDir(), "missing")); e == nil { t.Fatal("missing executable accepted") } path, err := os.Executable() if err != nil { t.Fatal(err) } if _, e := FindExecutable(path); e != nil { t.Fatal(e) } c, e := (&Driver{}).Probe(context.Background()) if e != nil || !c.Supports("trustedInput") || c.Supports("crossOriginFrames") { t.Fatal(c, e) } ctx, cancel := context.WithCancel(context.Background()) cancel() if _, e = (&Driver{}).Probe(ctx); e == nil { t.Fatal("canceled probe succeeded") } if _, e = (&Driver{}).Open(context.Background(), api.EngineOptions{Executable: "/missing/browser"}); e == nil { t.Fatal("missing browser started") } } func TestCanceledCommandsAndUnsupportedOperations(t *testing.T) { p := &enginePage{} ctx, cancel := context.WithCancel(context.Background()) cancel() if e := p.run(ctx); e != context.Canceled { t.Fatal(e) } if _, e := p.Navigate(context.Background(), "", "bad"); e == nil { t.Fatal("invalid navigation accepted") } if e := p.Input(context.Background(), "unknown", "", 0, 0); e == nil { t.Fatal("unknown input accepted") } if e := p.Input(context.Background(), "press", "UnknownKey", 0, 0); e == nil { t.Fatal("invalid key accepted") } } func TestDownloadEventState(t *testing.T) { s := &session{downloads: map[string]api.Download{}, dir: t.TempDir(), limit: 1000} s.downloadEvent(&browser.EventDownloadWillBegin{GUID: "id", SuggestedFilename: "data.cif"}) s.downloadEvent(&browser.EventDownloadProgress{GUID: "id", ReceivedBytes: 20, State: browser.DownloadProgressStateCompleted}) s.downloadEvent(&browser.EventDownloadProgress{GUID: "missing", ReceivedBytes: 2}) d := s.Downloads() if len(d) != 1 || d[0].State != "completed" || d[0].Bytes != 20 { t.Fatal(d) } }