package server import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "os" "os/exec" "strings" "testing" "time" ) func browserNavigationRequest(s *Server, token, method string, body any) *httptest.ResponseRecorder { var encoded bytes.Buffer if body != nil { _ = json.NewEncoder(&encoded).Encode(body) } request := httptest.NewRequest(method, "http://localhost/_/api/v1/browser/navigation", &encoded) request.Header.Set("Authorization", "Bearer "+token) request.Header.Set("Content-Type", "application/json") response := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(response, request) return response } func TestBrowserMarqueeAndNavigatorDragChromium(t *testing.T) { if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" { t.Skip("opt-in Chromium") } s, token, domain := configFixture(t) s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI) s.mux.HandleFunc("/v/browser/", s.handleBrowser) for _, path := range []string{"/drag/alpha", "/drag/beta"} { if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil { t.Fatal(err) } } testScript := `` server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Host = "localhost" r.Header.Set("Authorization", "Bearer "+token) if strings.HasPrefix(r.URL.Path, "/static/") { s.mux.ServeHTTP(w, r) return } if r.URL.Path == "/v/browser/drag" { recorded := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(recorded, r) for key, values := range recorded.Header() { w.Header()[key] = values } w.WriteHeader(recorded.Code) _, _ = w.Write([]byte(strings.Replace(recorded.Body.String(), "", testScript+"", 1))) return } s.withAuthorization(s.mux).ServeHTTP(w, r) })) defer server.Close() ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() command := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=3000", server.URL+"/v/browser/drag") output, err := command.CombinedOutput() if body := string(output); err != nil || !strings.Contains(body, `data-browser-interactions="ready"`) { t.Fatalf("browser interactions: err=%v body=%s", err, body) } } func TestBrowserNavigatorAPIAndUI(t *testing.T) { s, token, domain := configFixture(t) s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI) s.mux.HandleFunc("/v/browser/", s.handleBrowser) if _, err := s.store.CreatePageAtPathInDomain(domain, "/docs/page"); err != nil { t.Fatal(err) } response := browserNavigationRequest(s, token, http.MethodPost, browserNavigationInput{Label: "Docs", Path: "/docs", IsDir: true}) if response.Code != http.StatusCreated { t.Fatalf("add navigator link: %d %s", response.Code, response.Body.String()) } var link struct { UUID string `json:"uuid"` } if err := json.Unmarshal(response.Body.Bytes(), &link); err != nil || link.UUID == "" { t.Fatalf("add response: %#v %v", link, err) } response = browserNavigationRequest(s, token, http.MethodPatch, browserNavigationInput{UUID: link.UUID, Label: "Documents"}) if response.Code != http.StatusOK { t.Fatalf("rename navigator link: %d %s", response.Code, response.Body.String()) } page := configRequest(s, token, http.MethodGet, "/v/browser/", nil) body := page.Body.String() for _, expected := range []string{ `data-navigation-id="` + link.UUID + `"`, `href="/v/browser/docs"`, `>Documents`, `/static/js/browser-interactions.js?v=1`, `id="selection-marquee"`, `id="browser-operation-stack"`, } { if page.Code != http.StatusOK || !strings.Contains(body, expected) { t.Fatalf("browser UI missing %q: status=%d body=%s", expected, page.Code, body) } } response = browserNavigationRequest(s, token, http.MethodDelete, browserNavigationInput{UUID: link.UUID}) if response.Code != http.StatusOK { t.Fatalf("delete navigator link: %d %s", response.Code, response.Body.String()) } }