Files
pages/internal/server/tasks_browser_test.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
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.
2026-09-27 14:44:43 +09:00

120 lines
3.9 KiB
Go

package server
import (
"context"
"github.com/google/uuid"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"pageswiki/internal/store"
"strings"
"testing"
"time"
)
func TestTasksChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
for _, size := range []string{"desktop", "laptop", "mobile", "mobile-small"} {
t.Run(size, func(t *testing.T) {
s, token, domain, actor := taskFixture(t)
page, err := s.store.CreatePageAtPathInDomain(domain, "/projects/browser")
if err != nil {
t.Fatal(err)
}
project := newTaskRecord("project", "Browser project")
project.PageUUID, project.NotesSubpath = page, "notes"
project, err = s.store.SaveTask(context.Background(), domain, actor, project, 0)
if err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/e/-/", s.handleSemanticEditor)
s.mux.HandleFunc("/projects/", s.handleSemanticRoute)
repo, err := s.calendars()
if err != nil {
t.Fatal(err)
}
if _, err = repo.Create(context.Background(), domain, actor, "Browser focus", "focus"); err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/_/api/v1/notifications", s.handleNotificationsAPI)
s.mux.HandleFunc("/_/api/v1/notifications/", s.handleNotificationsAPI)
script, err := os.ReadFile("internal/server/testdata/tasks-browser.js")
if err != nil {
t.Fatal(err)
}
script = []byte(strings.ReplaceAll(string(script), "__TASK_PROJECT_UUID__", project.UUID))
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Authorization", "Bearer "+token)
if r.URL.Path == "/__notification-fixture" && r.Method == "POST" {
tx, e := s.store.DB.Begin()
if e != nil {
http.Error(w, "fixture failed", 500)
return
}
_, _, e = s.store.CreateNotificationTx(tx, store.Notification{DomainUUID: domain, RecipientUUID: actor, EventUUID: uuid.NewString(), Kind: "fixture", Message: "Browser notification fixture"})
if e != nil {
tx.Rollback()
http.Error(w, "fixture failed", 500)
return
}
if e = tx.Commit(); e != nil {
http.Error(w, "fixture failed", 500)
return
}
w.WriteHeader(204)
return
}
if r.URL.Path == "/__task-reminder-fixture" && r.Method == "POST" {
var fire int64
if err := s.store.DB.QueryRow(`SELECT MAX(fire_at_ms) FROM organizer_reminders`).Scan(&fire); err != nil {
http.Error(w, err.Error(), 500)
return
}
if _, _, err := s.store.DeliverTaskReminders(r.Context(), time.UnixMilli(fire), 100); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(204)
return
}
if r.Method == "GET" && r.URL.Path == "/v/plan/today" {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+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"
}
screenshot := ""
if base := os.Getenv("PAGESWIKI_TASK_SCREENSHOT"); base != "" {
screenshot = base + "-" + size + ".png"
}
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
target := strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + "/v/plan/today"
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", target, t.TempDir(), screenshot, width, height)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("task browser %v: %s", err, out)
}
})
}
}