119 lines
4.2 KiB
Go
119 lines
4.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestProductivityChromium(t *testing.T) {
|
|
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
|
t.Skip("opt-in Chromium")
|
|
}
|
|
for _, view := range []string{"contacts", "settings", "report", "tasks", "home", "editor"} {
|
|
sizes := []string{"desktop", "mobile"}
|
|
if view == "editor" {
|
|
sizes = append(sizes, "laptop", "mobile-small", "english")
|
|
}
|
|
for _, size := range sizes {
|
|
t.Run(view+"-"+size, func(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
if view == "settings" {
|
|
project, err := s.store.SaveTask(context.Background(), domain, actor, newTaskRecord("project", "Saved default project"), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := s.store.TaskPreferences(context.Background(), domain, actor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p.ProjectUUID = project.UUID
|
|
if _, err = s.store.SaveTaskPreferences(context.Background(), domain, actor, p, time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if size == "english" {
|
|
if err := s.store.SetConfigInDomain(domain, "locale", "en"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
s.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { s.renderSearchHome(w, r, "search") })
|
|
item := newTaskRecord("task", "Browser work task")
|
|
item.Status = "done"
|
|
item.Estimate = 60
|
|
item.Complexity = 3
|
|
if _, e := s.store.SaveTask(context.Background(), domain, actor, item, 0); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
repo, e := s.calendars()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = repo.Create(context.Background(), domain, actor, "Command calendar", "local"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
script, e := os.ReadFile("internal/server/testdata/productivity-browser.js")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
path := map[string]string{"contacts": "/v/plan/contacts", "settings": "/-/sys/config?panel=organizer", "report": "/v/plan/dashboard", "tasks": "/v/plan/tasks", "home": "/", "editor": "/v/plan/task/-/new"}[view]
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
// Simulate a saved project outside the first project list page.
|
|
if view == "settings" && size == "desktop" && r.URL.Path == "/_/api/v1/extension/organizer.tasks/items" && r.URL.Query().Get("kind") == "project" {
|
|
writeEventJSON(w, 200, map[string]any{"items": []any{}, "next": "fixture"})
|
|
return
|
|
}
|
|
if r.Method == "GET" && r.URL.RequestURI() == path {
|
|
rec := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
|
for k, v := range rec.Header() {
|
|
w.Header()[k] = v
|
|
}
|
|
w.WriteHeader(rec.Code)
|
|
// The home template uses nonce CSP. Reuse its already generated nonce
|
|
// only in this synthetic fixture; production HTML is never rewritten.
|
|
nonce := ""
|
|
if csp := rec.Header().Get("Content-Security-Policy"); strings.Contains(csp, "'nonce-") {
|
|
nonce = strings.SplitN(strings.SplitN(csp, "'nonce-", 2)[1], "'", 2)[0]
|
|
}
|
|
w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script nonce=\""+nonce+"\">"+string(script)+"</script></body>", 1)))
|
|
return
|
|
}
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
}))
|
|
defer server.Close()
|
|
width, height := "1440", "1000"
|
|
if size == "mobile" {
|
|
width, height = "390", "844"
|
|
}
|
|
if size == "laptop" {
|
|
width, height = "1024", "768"
|
|
}
|
|
if size == "mobile-small" {
|
|
width, height = "320", "568"
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
|
defer cancel()
|
|
shot := ""
|
|
if prefix := os.Getenv("PAGESWIKI_PRODUCTIVITY_SCREENSHOT"); prefix != "" {
|
|
shot = prefix + "-" + view + "-" + size + ".png"
|
|
}
|
|
command := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", strings.Replace(server.URL, "127.0.0.1", "localhost", 1)+path, t.TempDir(), shot, width, height)
|
|
if view == "report" && size == "desktop" {
|
|
command.Env = append(os.Environ(), "PAGESWIKI_DASHBOARD_DRAG=1")
|
|
}
|
|
out, e := command.CombinedOutput()
|
|
if e != nil {
|
|
t.Fatalf("%v: %s", e, out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|