package native import ( "context" "encoding/json" "os" "runtime" "testing" "time" "clusterflow.dev/cfbrowser/api" "clusterflow.dev/cfbrowser/protocol" ) func init() { if os.Getenv("CFBROWSER_TEST_HELPER") != "1" { return } err := protocol.Serve(context.Background(), os.Stdin, os.Stdout, func(ctx context.Context, method string, raw json.RawMessage) (any, error) { switch method { case "configure": if os.Getenv("CFBROWSER_TEST_MODE") == "reject" { return false, nil } if os.Getenv("CFBROWSER_TEST_MODE") == "crash" { os.Exit(3) } return true, nil case "session.features": if os.Getenv("CFBROWSER_TEST_MODE") == "old" { return nil, api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "old worker") } return map[string]int{"featureVersion": protocol.FeatureVersion}, nil case "page.new": return 1, nil case "page.navigate": return "https://example.org", nil case "page.evaluate": return json.RawMessage(`{"answer":42}`), nil case "page.close": return true, nil } return nil, api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "unknown method") }) if err != nil { os.Exit(2) } os.Exit(0) } func TestCapabilitiesAndStartupValidation(t *testing.T) { d := &Driver{} c, e := d.Probe(context.Background()) if e != nil || c.Supports("trustedInput") != (runtime.GOOS == "windows") || c.Supports("downloads") { t.Fatal(c, e) } if c.Engine != nativeEngine() || c.Supports("isolatedWorld") != nativeSupported() { t.Fatal(c) } if _, e = d.Open(context.Background(), api.EngineOptions{}); e == nil { t.Fatal("missing worker accepted") } ctx, cancel := context.WithCancel(context.Background()) cancel() if _, e = d.Probe(ctx); e == nil { t.Fatal("canceled probe accepted") } if _, e = d.Open(ctx, api.EngineOptions{}); e == nil { t.Fatal("canceled open accepted") } } func TestWorkerStartupFailure(t *testing.T) { if !nativeSupported() { t.Skip("unsupported native platform") } t.Setenv("CFBROWSER_TEST_HELPER", "1") path, e := os.Executable() if e != nil { t.Fatal(e) } for _, mode := range []string{"reject", "crash", "old"} { t.Run(mode, func(t *testing.T) { t.Setenv("CFBROWSER_TEST_MODE", mode) ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() if s, e := (&Driver{WorkerPath: path}).Open(ctx, api.EngineOptions{}); e == nil { _ = s.Close() t.Fatal("worker startup failure accepted") } }) } } func TestWorkerClientLifecycle(t *testing.T) { if !nativeSupported() { t.Skip("unsupported native platform") } t.Setenv("CFBROWSER_TEST_HELPER", "1") path, e := os.Executable() if e != nil { t.Fatal(e) } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() s, e := (&Driver{WorkerPath: path}).Open(ctx, api.EngineOptions{NoSandbox: true}) if e != nil { t.Fatal(e) } defer s.Close() p, e := s.NewPage(ctx) if e != nil { t.Fatal(e) } if v, e := p.Navigate(ctx, "", "goto"); e != nil || v != "https://example.org" { t.Fatal(v, e) } if v, e := p.Evaluate(ctx, "ignored"); e != nil || string(v) != `{"answer":42}` { t.Fatal(string(v), e) } if e = p.Input(ctx, "click", "", 0, 0); e == nil { t.Fatal("native trusted input claimed") } if _, e = p.Capture(ctx); e == nil { t.Fatal("native capture claimed") } if len(s.Downloads()) != 0 { t.Fatal("fake downloads") } if e = p.Close(); e != nil { t.Fatal(e) } if e = p.Close(); e != nil { t.Fatal(e) } if e = s.Close(); e != nil { t.Fatal(e) } }