package server import ( "context" "fmt" "io" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) // Real third-party interoperability, on a disposable fixture only. Nothing is // fetched from production or from external calendar subscriptions. func TestTaskDAVVdirsyncer(t *testing.T) { bin := os.Getenv("PAGESWIKI_VDIRSYNCER") if bin == "" { t.Skip("opt-in vdirsyncer and sibling todo executable") } s, _, domain, actor := taskFixture(t) ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() credential, password, err := s.store.CreateTaskDAVCredential(ctx, domain, actor, "Integration fixture", true, 1) if err != nil { t.Fatal(err) } native, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Native task"), 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 DAV %s %s Depth=%q: %d %s request=%s", r.Method, r.URL.Path, r.Header.Get("Depth"), 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() local := filepath.Join(dir, "tasks") if err = os.MkdirAll(filepath.Join(local, "default"), 0700); err != nil { t.Fatal(err) } config := fmt.Sprintf(`[general] status_path = %q [pair tasks] a = "local" b = "remote" collections = ["default"] metadata = ["displayname"] [storage local] type = "filesystem" path = %q fileext = ".ics" [storage remote] type = "caldav" url = %q username = %q password = %q `, filepath.Join(dir, "status"), local, strings.Replace(server.URL, "127.0.0.1", "localhost", 1)+taskDAVRoot, credential.ID, password) configPath := filepath.Join(dir, "vdirsyncer.conf") if err = os.WriteFile(configPath, []byte(config), 0600); err != nil { t.Fatal(err) } run := func(executable string, args ...string) string { t.Helper() cmd := exec.CommandContext(ctx, executable, args...) cmd.Stdin = strings.NewReader("y\ny\ny\n") out, e := cmd.CombinedOutput() if e != nil { t.Fatalf("%s failed: %v\n%s", filepath.Base(executable), e, strings.ReplaceAll(string(out), password, "[REDACTED]")) } return string(out) } sync := func() { t.Helper(); run(bin, "-c", configPath, "sync") } run(bin, "-c", configPath, "discover") sync() run(bin, "-c", configPath, "metasync") files, err := filepath.Glob(filepath.Join(local, "default", "*.ics")) if err != nil || len(files) != 1 { t.Fatal(files, err) } bytes, err := os.ReadFile(files[0]) if err != nil || !strings.Contains(string(bytes), "SUMMARY:Native task") { t.Fatal("download", err) } // Exercise calendar-query as well as the default PROPFIND listing path. config += "item_types = [\"VTODO\"]\n" if err = os.WriteFile(configPath, []byte(config), 0600); err != nil { t.Fatal(err) } run(bin, "-c", configPath, "discover") todo := filepath.Join(filepath.Dir(bin), "todo") todoConfig := filepath.Join(dir, "todo.py") config = fmt.Sprintf("path = %q\ncache_path = %q\ndate_format = '%%Y-%%m-%%d'\ntime_format = '%%H:%%M'\ndefault_list = 'PagesWiki Tasks'\ndefault_due = 0\n", filepath.Join(local, "*"), filepath.Join(dir, "todo.sqlite3")) if err = os.WriteFile(todoConfig, []byte(config), 0600); err != nil { t.Fatal(err) } run(todo, "-c", todoConfig, "new", "--due", "2026-10-01", "CLI-created task") sync() items, err := s.store.TaskDAVList(ctx, domain, actor) if err != nil || len(items) != 2 { t.Fatal(len(items), err) } found := "" for _, v := range items { if v.Title == "CLI-created task" { found = v.UUID if v.DueOn != "2026-10-01" { t.Fatal(v.DueOn) } } } if found == "" { t.Fatal("upload missing") } // Native task has the first Todoman ID after scanning before creation. output := run(todo, "-c", todoConfig, "--porcelain", "list") if !strings.Contains(output, "CLI-created task") { t.Fatal(output) } run(todo, "-c", todoConfig, "done", "1") sync() got, err := s.store.GetTask(ctx, domain, actor, native.UUID) if err != nil || got.Status != "done" { t.Fatal("CLI completion", got.Status, err) } got.Title = "Changed by Web" if _, err = s.store.SaveTask(ctx, domain, actor, got, got.Version); err != nil { t.Fatal(err) } sync() bytes, err = os.ReadFile(files[0]) if err != nil || !strings.Contains(string(bytes), "SUMMARY:Changed by Web") { t.Fatal("Web change missing locally", err) } // Local deletion becomes an archive, not a physical task/history deletion. if err = os.Remove(files[0]); err != nil { t.Fatal(err) } sync() got, err = s.store.GetTask(ctx, domain, actor, native.UUID) if err != nil || !got.Archived { t.Fatal("CLI delete", err) } // A no-op sync must not create another revision. before, err := s.store.GetTask(ctx, domain, actor, found) if err != nil { t.Fatal(err) } sync() after, err := s.store.GetTask(ctx, domain, actor, found) if err != nil || before.Version != after.Version { t.Fatal("sync oscillation", before.Version, after.Version, err) } }