package store import ( "os" "path/filepath" "testing" ) func TestMigrate(t *testing.T) { tmpDir := t.TempDir() // Create old structure // WebDAV: files// if err := os.MkdirAll(filepath.Join(tmpDir, "files", "alice"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(tmpDir, "files", "alice", "test.txt"), []byte("test"), 0o644); err != nil { t.Fatal(err) } // CalDAV: /cal-/ if err := os.MkdirAll(filepath.Join(tmpDir, "bob", "cal-work"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(tmpDir, "bob", "cal-work", "evt.ics"), []byte("calendar"), 0o644); err != nil { t.Fatal(err) } // CardDAV: /card-/ if err := os.MkdirAll(filepath.Join(tmpDir, "bob", "card-contacts"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(tmpDir, "bob", "card-contacts", "vcard.vcf"), []byte("card"), 0o644); err != nil { t.Fatal(err) } // Create store and migrate st, err := NewStore(tmpDir) if err != nil { t.Fatal(err) } if err := st.Migrate(); err != nil { t.Fatal(err) } // Verify WebDAV: /files/ if _, err := os.Stat(filepath.Join(tmpDir, "alice", "files", "test.txt")); err != nil { t.Errorf("alice files not migrated: %v", err) } // Verify CalDAV: /calendars/ if _, err := os.Stat(filepath.Join(tmpDir, "bob", "calendars", "work", "evt.ics")); err != nil { t.Errorf("bob calendars not migrated: %v", err) } // Verify CardDAV: /addressbooks/ if _, err := os.Stat(filepath.Join(tmpDir, "bob", "addressbooks", "contacts", "vcard.vcf")); err != nil { t.Errorf("bob addressbooks not migrated: %v", err) } // Verify old structure is gone if _, err := os.Stat(filepath.Join(tmpDir, "files", "alice")); err == nil { t.Error("old files directory not removed") } if _, err := os.Stat(filepath.Join(tmpDir, "bob", "cal-work")); err == nil { t.Error("old cal- directory not removed") } if _, err := os.Stat(filepath.Join(tmpDir, "bob", "card-contacts")); err == nil { t.Error("old card- directory not removed") } } func TestMigrateIdempotent(t *testing.T) { tmpDir := t.TempDir() // Create new structure if err := os.MkdirAll(filepath.Join(tmpDir, "alice", "files"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(tmpDir, "alice", "files", "test.txt"), []byte("test"), 0o644); err != nil { t.Fatal(err) } st, err := NewStore(tmpDir) if err != nil { t.Fatal(err) } // Run migration twice if err := st.Migrate(); err != nil { t.Fatal(err) } if err := st.Migrate(); err != nil { t.Fatal(err) } // Verify data still there if _, err := os.Stat(filepath.Join(tmpDir, "alice", "files", "test.txt")); err != nil { t.Errorf("data not preserved: %v", err) } } func TestMigrateMissingDirectories(t *testing.T) { tmpDir := t.TempDir() st, err := NewStore(tmpDir) if err != nil { t.Fatal(err) } // Should not error on empty directory if err := st.Migrate(); err != nil { t.Errorf("unexpected error: %v", err) } }