package server import ( "context" "io" "net/http" "net/http/httptest" "os" "os/exec" "pageswiki/internal/calendar" "path/filepath" "strings" "testing" "time" "github.com/google/uuid" ) func TestTaskDAVChroncal(t *testing.T) { testDAVChroncal(t, false) } func TestCommonDAVChroncal(t *testing.T) { testDAVChroncal(t, true) } func TestNamedTaskDAVChroncal(t *testing.T) { testDAVChroncal(t, true, true) } func TestNativeProjectTaskDAVChroncal(t *testing.T) { testDAVChroncal(t, true, false) } func testDAVChroncal(t *testing.T, common bool, named ...bool) { bin := os.Getenv("PAGESWIKI_CHRONCAL") if bin == "" { t.Skip("opt-in isolated chroncal CLI") } s, _, domain, actor := taskFixture(t) ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() scope := "tasks" if common { scope = "all" } credential, password, err := s.store.CreateDAVCredential(ctx, domain, actor, "chroncal fixture", true, 1, scope, common) if err != nil { t.Fatal(err) } var repo *calendar.Repository var cal calendar.Calendar var event calendar.Event if common { repo, err = s.calendars() if err != nil { t.Fatal(err) } cal, err = repo.Create(ctx, domain, actor, "Fixture Calendar") if err != nil { t.Fatal(err) } event, err = calendar.Build("", calendar.Event{UID: "native-fixture@example.test", Summary: "Native calendar meeting", Start: time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), End: time.Date(2026, 10, 1, 10, 0, 0, 0, time.UTC)}) if err != nil { t.Fatal(err) } event, err = repo.Change(ctx, domain, cal.ID, actor, event.UID, "*", &event) if err != nil { t.Fatal(err) } } projectID, listName := "", "PagesWiki Tasks" if len(named) == 1 { project := newTaskRecord("project", "Native task list") var e error if named[0] { project, e = s.store.CreateTaskDAVCollection(ctx, domain, actor, uuid.NewString(), "Named task list", credential.ID) } else { project, e = s.store.SaveTask(ctx, domain, actor, project, 0) } if e != nil { t.Fatal(e) } projectID, listName = project.UUID, project.Title } nativeRecord := newTaskRecord("task", "Chroncal native task") nativeRecord.ProjectUUID = projectID native, err := s.store.SaveTask(ctx, domain, actor, nativeRecord, 0) if err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, _ := io.ReadAll(r.Body) r.Body = io.NopCloser(strings.NewReader(string(body))) rec := httptest.NewRecorder() s.handler().ServeHTTP(rec, r) if rec.Code >= 400 { t.Logf("fixture %s %s depth=%s if-match=%q if-none-match=%q status=%d body=%s request=%s", r.Method, r.URL.Path, r.Header.Get("Depth"), r.Header.Get("If-Match"), r.Header.Get("If-None-Match"), rec.Code, rec.Body, body) } for k, v := range rec.Header() { w.Header()[k] = v } w.WriteHeader(rec.Code) w.Write(rec.Body.Bytes()) })) defer server.Close() dir := t.TempDir() run := func(args ...string) string { t.Helper() cmd := exec.CommandContext(ctx, bin, append([]string{"--allow-plaintext"}, args...)...) // Never load the user's chroncal database, settings, credentials or keyring. cmd.Env = []string{"PATH=" + os.Getenv("PATH"), "HOME=" + dir, "XDG_CONFIG_HOME=" + filepath.Join(dir, "config"), "XDG_DATA_HOME=" + filepath.Join(dir, "data"), "XDG_STATE_HOME=" + filepath.Join(dir, "state"), "XDG_CACHE_HOME=" + filepath.Join(dir, "cache"), "DBUS_SESSION_BUS_ADDRESS=unix:path=" + filepath.Join(dir, "no-keyring"), "CHRONCAL_TEST_PASSWORD=" + password} out, e := cmd.CombinedOutput() if e != nil { t.Fatalf("chroncal %s: %v\n%s", args[0], e, strings.ReplaceAll(string(out), password, "[REDACTED]")) } return string(out) } endpoint := strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + taskDAVRoot if common { endpoint = strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + calendarDAV } run("account", "add", "Fixture", "--server", endpoint, "--username", credential.ID, "--auth", "basic", "--allow-insecure", "--password-cmd", "printenv CHRONCAL_TEST_PASSWORD") if out := run("todo", "list", "--output", "json"); !strings.Contains(out, native.Title) { t.Fatal("native task not pulled", out) } if common { if out := run("event", "get", event.UID, "--output", "json"); !strings.Contains(out, event.Summary) { t.Fatal("calendar not pulled", out) } run("event", "add", "Chroncal event", "--calendar", cal.PrettyName, "--date", "2026-10-02", "--time", "09:00", "--duration", "1h") run("sync", "run", "--account", "Fixture") _, events, e := repo.DAVSnapshot(ctx, domain, cal.ID, actor, true) if e != nil || len(events) != 2 { t.Fatal("calendar create not synced", len(events), e) } for _, v := range events { if v.UID == event.UID { continue } run("event", "update", v.UID, "--title", "CLI renamed event") run("sync", "run", "--account", "Fixture") v, e = repo.Event(ctx, domain, cal.ID, actor, v.UID) if e != nil || v.Summary != "CLI renamed event" { t.Fatal("CLI update not synced", e) } updated, e := calendar.Build(v.Data, calendar.Event{UID: v.UID, Summary: "Web calendar rename", Start: time.Date(2026, 10, 2, 10, 0, 0, 0, time.UTC), End: time.Date(2026, 10, 2, 11, 0, 0, 0, time.UTC)}) if e != nil { t.Fatal(e) } if _, e = repo.Change(ctx, domain, cal.ID, actor, v.UID, v.ETag, &updated); e != nil { t.Fatal(e) } run("sync", "run", "--account", "Fixture") if out := run("event", "get", v.UID, "--output", "json"); !strings.Contains(out, updated.Summary) { t.Fatal("calendar change not pulled", out) } run("event", "delete", v.UID, "--yes") run("sync", "run", "--account", "Fixture") if _, e = repo.Event(ctx, domain, cal.ID, actor, v.UID); e != calendar.ErrMissing { t.Fatal("calendar delete not synced", e) } } } run("todo", "add", "Chroncal created task", "--calendar", listName, "--due", "2026-10-01") run("sync", "run", "--account", "Fixture") items, e := s.store.TaskDAVList(ctx, domain, actor, projectID) if e != nil { t.Fatal(e) } found := false for _, item := range items { if item.Title == "Chroncal created task" { found = true if item.DueOn != "2026-10-01" { t.Fatal(item.DueOn) } } } if !found { t.Fatal("CLI create not synced") } run("todo", "complete", native.UUID) run("sync", "run", "--account", "Fixture") current, e := s.store.GetTask(ctx, domain, actor, native.UUID) if e != nil || current.Status != "done" { t.Fatal("completion not synced", current.Status, e) } current.Title = "Web-side rename" if _, e = s.store.SaveTask(ctx, domain, actor, current, current.Version); e != nil { t.Fatal(e) } run("sync", "run", "--account", "Fixture") if out := run("todo", "get", native.UUID, "--output", "json"); !strings.Contains(out, current.Title) { t.Fatal("Web change not pulled", out) } run("todo", "delete", native.UUID, "--yes") run("sync", "run", "--account", "Fixture") current, e = s.store.GetTask(ctx, domain, actor, native.UUID) if e != nil || !current.Archived { t.Fatal("delete must archive", e) } before, e := s.store.TaskDAVList(ctx, domain, actor, projectID) if e != nil || len(before) != 1 { t.Fatal(e, len(before)) } run("sync", "run", "--account", "Fixture") after, e := s.store.TaskDAVList(ctx, domain, actor, projectID) if e != nil || len(after) != 1 || after[0].Version != before[0].Version { t.Fatal("no-op sync changed a version", e) } }