package server import ( "context" "fmt" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "time" "github.com/google/uuid" "pageswiki/internal/store" ) // Real client interoperability uses only disposable, synthetic contacts. func TestContactDAVVdirsyncer(t *testing.T) { bin := os.Getenv("PAGESWIKI_VDIRSYNCER") if bin == "" { t.Skip("opt-in vdirsyncer executable") } s, _, domain, actor := taskFixture(t) ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() credential, password, err := s.store.CreateDAVCredential(ctx, domain, actor, "Fixture", true, 1, "contacts") if err != nil { t.Fatal(err) } native, err := s.store.SaveContact(ctx, domain, actor, store.Contact{UUID: uuid.NewString(), VCard: testCard}, true) if err != nil { t.Fatal(err) } server := httptest.NewServer(s.handler()) defer server.Close() dir := t.TempDir() local := filepath.Join(dir, "cards") if err = os.MkdirAll(filepath.Join(local, "contacts"), 0700); err != nil { t.Fatal(err) } config := fmt.Sprintf(`[general] status_path = %q [pair contacts] a = "local" b = "remote" collections = ["contacts"] [storage local] type = "filesystem" path = %q fileext = ".vcf" [storage remote] type = "carddav" url = %q username = %q password = %q `, filepath.Join(dir, "status"), local, strings.Replace(server.URL, "127.0.0.1", "localhost", 1)+contactDAV, credential.ID, password) configPath := filepath.Join(dir, "vdirsyncer.conf") if err = os.WriteFile(configPath, []byte(config), 0600); err != nil { t.Fatal(err) } run := func(action string, extra ...string) { t.Helper() cmd := exec.CommandContext(ctx, bin, append([]string{"-c", configPath, action}, extra...)...) cmd.Stdin = strings.NewReader("y\ny\ny\n") out, e := cmd.CombinedOutput() if e != nil { t.Fatalf("%s: %v\n%s", action, e, strings.ReplaceAll(string(out), password, "[REDACTED]")) } } run("discover") run("sync") files, err := filepath.Glob(filepath.Join(local, "contacts", "*.vcf")) if err != nil || len(files) != 1 { t.Fatal(files, err) } raw, err := os.ReadFile(files[0]) if err != nil || !strings.Contains(string(raw), "FN:田中") { t.Fatal("download", err) } if err = os.WriteFile(files[0], []byte(strings.Replace(string(raw), "FN:田中", "FN:Client edited", 1)), 0600); err != nil { t.Fatal(err) } run("sync") got, err := s.store.Contact(ctx, domain, actor, native.UUID) if err != nil || got.Name != "Client edited" || !strings.Contains(got.VCard, "X-PRIVATE:keep") { t.Fatal("client edit", got.Name, err) } got.Name = "Web edited" got, err = s.store.SaveContact(ctx, domain, actor, got, false) if err != nil { t.Fatal(err) } run("sync") raw, err = os.ReadFile(files[0]) if err != nil || !strings.Contains(string(raw), "FN:Web edited") { t.Fatal("web edit", err) } newPath := filepath.Join(local, "contacts", "client-new.vcf") newCard := strings.ReplaceAll(testCard, "urn:test:dav-contact", "urn:test:client-new") if err = os.WriteFile(newPath, []byte(newCard), 0600); err != nil { t.Fatal(err) } run("sync") items, _, err := s.store.Contacts(ctx, domain, actor, "", "") if err != nil || len(items) != 2 { t.Fatal("client create", len(items), err) } if err = os.Remove(newPath); err != nil { t.Fatal(err) } run("sync") items, _, err = s.store.Contacts(ctx, domain, actor, "", "") if err != nil || len(items) != 1 { t.Fatal("client delete", len(items), err) } got.Deleted = true if _, err = s.store.SaveContact(ctx, domain, actor, got, false); err != nil { t.Fatal(err) } // Emptying a whole book requires vdirsyncer's explicit safety override. run("sync", "--force-delete", "contacts/contacts") files, err = filepath.Glob(filepath.Join(local, "contacts", "*.vcf")) if err != nil || len(files) != 0 { t.Fatal("web delete", files, err) } }