Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
126 lines
5.1 KiB
Go
126 lines
5.1 KiB
Go
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 := `<script>(async()=>{
|
|
await new Promise(resolve=>setTimeout(resolve,50));
|
|
const rows=[...document.querySelectorAll('.browser-row')],grid=document.getElementById('browser-grid');
|
|
const first=rows[0].getBoundingClientRect(),last=rows.at(-1).getBoundingClientRect(),pointerId=9;
|
|
grid.dispatchEvent(new PointerEvent('pointerdown',{bubbles:true,button:0,pointerId,clientX:first.left+1,clientY:first.top+1}));
|
|
grid.dispatchEvent(new PointerEvent('pointermove',{bubbles:true,button:0,pointerId,clientX:last.right-1,clientY:last.bottom-1}));
|
|
grid.dispatchEvent(new PointerEvent('pointerup',{bubbles:true,button:0,pointerId,clientX:last.right-1,clientY:last.bottom-1}));
|
|
const transfer=new DataTransfer();
|
|
transfer.setData('application/x-pageswiki-paths',JSON.stringify(rows.map(row=>({path:row.dataset.path,name:row.dataset.name,is_directory:false}))));
|
|
await navigatorDrop({preventDefault(){},stopPropagation(){},dataTransfer:transfer});
|
|
document.body.dataset.browserInteractions=selectedEntries().length===2&&document.querySelectorAll('.navigator-link').length===2?'ready':'failed';
|
|
})()</script>`
|
|
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(), "</body>", testScript+"</body>", 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</a>`,
|
|
`/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())
|
|
}
|
|
}
|