package integration import ( "clusterflow.dev/cfbrowser/api" "context" "encoding/json" "net/http" "net/http/httptest" "os" "strings" "testing" "time" ) func TestIsolatedWorldLifetime(t *testing.T) { engines(t, func(t *testing.T, driver api.Driver) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") _, _ = w.Write([]byte(`
`)) })) defer server.Close() ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() session, e := driver.Open(ctx, api.EngineOptions{Headless: true, NoSandbox: os.Getenv("CFBROWSER_TEST_NO_SANDBOX") == "1", Executable: os.Getenv("CFBROWSER_EXECUTABLE"), DownloadDir: t.TempDir(), ViewportWidth: 900, ViewportHeight: 600}) if e != nil { t.Fatal(e) } defer session.Close() page, e := session.NewPage(ctx) if e != nil { t.Fatal(e) } defer page.Close() if _, e := page.Navigate(ctx, server.URL, "goto"); e != nil { t.Fatal(e) } evaluate := func(script, want string) { t.Helper() v, e := page.Evaluate(ctx, script) if e != nil || string(v) != want { t.Fatalf("evaluate: %s, want %s, err %v", v, want, e) } } evaluate(`(()=>{globalThis.cfSecret=41;globalThis.cfNode=document.querySelector('button');return typeof globalThis.cfPageMarker})()`, `"undefined"`) evaluate(`(()=>{globalThis.cfNode.textContent='Changed';globalThis.cfSecret++;return globalThis.cfSecret})()`, `42`) evaluate(`(()=>{const s=document.createElement('script');s.textContent="document.body.dataset.secret=typeof window.cfSecret";document.body.appendChild(s);return document.body.dataset.secret})()`, `"undefined"`) evaluate(`globalThis.cfNode===document.querySelector('button')`, `true`) evaluate(`document.querySelector('button').textContent`, `"Changed"`) if _, e := page.Evaluate(ctx, `(()=>{throw new Error('expected')})()`); e == nil { t.Fatal("JavaScript exception accepted") } evaluate(`({answer:42})`, `{"answer":42}`) if _, e := page.Navigate(ctx, server.URL+"/new", "goto"); e != nil { t.Fatal(e) } evaluate(`typeof globalThis.cfSecret`, `"undefined"`) raw, e := page.Evaluate(ctx, `({width:innerWidth,height:innerHeight})`) var size struct{ Width, Height int } caps, _ := driver.Probe(ctx) if e != nil || json.Unmarshal(raw, &size) != nil || size.Width <= 0 || size.Height <= 0 || strings.HasPrefix(caps.Engine, "native-") && (size.Width != 900 || size.Height != 600) { t.Fatal("viewport mismatch", string(raw), e) } }) }