| 22 | } |
| 23 | |
| 24 | func TestState_SaveAndLoadRoundtrip(t *testing.T) { |
| 25 | dir := t.TempDir() |
| 26 | now := time.Now().Truncate(time.Second) |
| 27 | |
| 28 | original := &SyncState{ |
| 29 | Source: "github", |
| 30 | LastSync: now, |
| 31 | Tasks: map[string]TaskState{ |
| 32 | "GH-1": { |
| 33 | ExternalID: "GH-1", |
| 34 | LocalID: "001", |
| 35 | FilePath: "tasks/github/001-fix-bug.md", |
| 36 | ExternalHash: "abc123", |
| 37 | LocalHash: "def456", |
| 38 | LastSynced: now, |
| 39 | }, |
| 40 | "GH-2": { |
| 41 | ExternalID: "GH-2", |
| 42 | LocalID: "002", |
| 43 | FilePath: "tasks/github/002-add-feature.md", |
| 44 | ExternalHash: "ghi789", |
| 45 | LocalHash: "jkl012", |
| 46 | LastSynced: now, |
| 47 | }, |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | if err := SaveState(dir, "github", original); err != nil { |
| 52 | t.Fatalf("failed to save state: %v", err) |
| 53 | } |
| 54 | |
| 55 | loaded, err := LoadState(dir, "github") |
| 56 | if err != nil { |
| 57 | t.Fatalf("failed to load state: %v", err) |
| 58 | } |
| 59 | |
| 60 | if loaded.Source != original.Source { |
| 61 | t.Errorf("source mismatch: got %q, want %q", loaded.Source, original.Source) |
| 62 | } |
| 63 | if len(loaded.Tasks) != len(original.Tasks) { |
| 64 | t.Fatalf("task count mismatch: got %d, want %d", len(loaded.Tasks), len(original.Tasks)) |
| 65 | } |
| 66 | |
| 67 | for id, orig := range original.Tasks { |
| 68 | got, ok := loaded.Tasks[id] |
| 69 | if !ok { |
| 70 | t.Errorf("missing task %q", id) |
| 71 | continue |
| 72 | } |
| 73 | if got.LocalID != orig.LocalID { |
| 74 | t.Errorf("task %s: local_id mismatch: got %q, want %q", id, got.LocalID, orig.LocalID) |
| 75 | } |
| 76 | if got.FilePath != orig.FilePath { |
| 77 | t.Errorf("task %s: file_path mismatch: got %q, want %q", id, got.FilePath, orig.FilePath) |
| 78 | } |
| 79 | if got.ExternalHash != orig.ExternalHash { |
| 80 | t.Errorf("task %s: external_hash mismatch", id) |
| 81 | } |