Files
pages/internal/server/productivity_browser_test.go
T
Anya Tokugawa 6ba30bc9f4
Build and publish container images / images (push) Successful in 12m38s
Release container images / release (push) Successful in 23s
Unify organizer navigation and add personal widget dashboard
2026-11-04 00:05:16 +09:00

85 lines
3.0 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"} {
for _, size := range []string{"desktop", "mobile"} {
t.Run(view+"-"+size, func(t *testing.T) {
s, token, domain, actor := taskFixture(t)
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": "/"}[view]
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Authorization", "Bearer "+token)
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"
}
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)
}
})
}
}
}