| 1 | package workspacestate |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "maps" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestSettleImportedWorkspaceConflictMatchesOnlyPublishedSourceVersion(t *testing.T) { |
| 13 | s := NewStore(filepath.Join(t.TempDir(), "state.json")) |
| 14 | mapping := SourceMapping{SourceKey: "source:head", Path: "/source.jsonl", HeadID: "head", Format: "legacy", Fingerprint: "version", SessionID: "imported", WorkspaceID: "global"} |
| 15 | identity := State{SourceMappings: map[string]SourceMapping{mapping.SourceKey: mapping}} |
| 16 | aliases := identity.SourceKeys(mapping.SourceKey) |
| 17 | if len(aliases) != 2 { |
| 18 | t.Fatalf("fixture requires a durable and normalized source identity: %v", aliases) |
| 19 | } |
| 20 | base := RecoveryEntry{SourceKey: mapping.SourceKey, Path: mapping.Path, HeadID: mapping.HeadID, Format: mapping.Format, |
| 21 | Fingerprint: mapping.Fingerprint, Reason: "workspace_conflict", Status: "pending", WorkspaceRoot: "/previous-workspace"} |
| 22 | variants := map[string]func(*RecoveryEntry){ |
| 23 | "matched": func(e *RecoveryEntry) {}, |
| 24 | "normalized-alias": func(e *RecoveryEntry) { e.SourceKey = aliases[1] }, |
| 25 | "other-source": func(e *RecoveryEntry) { e.SourceKey = "other-source:head" }, |
| 26 | "other-head-key": func(e *RecoveryEntry) { e.SourceKey = "source:other-head"; e.HeadID = "other-head" }, |
| 27 | "other-head": func(e *RecoveryEntry) { e.HeadID = "other-head" }, |
| 28 | "other-version": func(e *RecoveryEntry) { e.Fingerprint = "new-version" }, |
| 29 | "unknown-version": func(e *RecoveryEntry) { e.Fingerprint = "" }, |
| 30 | "other-format": func(e *RecoveryEntry) { e.Format = "legacy-trash" }, |
| 31 | "changed-source": func(e *RecoveryEntry) { e.Reason = "source_changed_after_adoption" }, |
| 32 | "alternate-head": func(e *RecoveryEntry) { e.Reason = "alternate_head" }, |
| 33 | "unreadable": func(e *RecoveryEntry) { e.Reason = "metadata_unreadable" }, |
| 34 | "already-restored": func(e *RecoveryEntry) { e.Status = "restored"; e.SessionID = "previous-target" }, |
| 35 | } |
| 36 | if err := s.mutate(t.Context(), func(state *State) error { |
| 37 | state.Workspaces["global"] = Workspace{ID: "global", Root: t.TempDir(), SessionIDs: []string{"imported"}} |
| 38 | state.SessionStates["imported"] = SessionState{Lifecycle: Active} |
| 39 | state.SourceMappings[mapping.SourceKey] = mapping |
| 40 | for id, change := range variants { |
| 41 | entry := base |
| 42 | entry.ID = id |
| 43 | change(&entry) |
| 44 | state.RecoveryEntries[id] = entry |
| 45 | } |
| 46 | return nil |
| 47 | }); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | before, err := s.Load(t.Context()) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if err := s.SettleImportedWorkspaceConflict(t.Context(), mapping.SourceKey, mapping.Fingerprint, mapping.SessionID); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | after, err := NewStore(s.path).Load(t.Context()) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | for id, old := range before.RecoveryEntries { |
| 62 | want := old |
| 63 | if id == "matched" || id == "normalized-alias" { |
| 64 | want.Status, want.SessionID = "restored", mapping.SessionID |
| 65 | } |
| 66 | if got := after.RecoveryEntries[id]; !reflect.DeepEqual(got, want) { |
| 67 | t.Errorf("entry %s = %+v, want %+v", id, got, want) |
| 68 | } |
| 69 | } |
| 70 | if err := s.SettleImportedWorkspaceConflict(t.Context(), mapping.SourceKey, mapping.Fingerprint, mapping.SessionID); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | again, err := s.Load(t.Context()) |
| 74 | if err != nil || again.Generation != after.Generation { |
| 75 | t.Fatalf("repeat changed settled state: generation %d -> %d, err=%v", after.Generation, again.Generation, err) |
| 76 | } |
| 77 | for _, args := range [][3]string{ |
| 78 | {"unpublished-source", mapping.Fingerprint, mapping.SessionID}, |
| 79 | {mapping.SourceKey, "changed-version", mapping.SessionID}, |
| 80 | {mapping.SourceKey, "", mapping.SessionID}, |
| 81 | {mapping.SourceKey, mapping.Fingerprint, "other-target"}, |
| 82 | } { |
| 83 | if err := s.SettleImportedWorkspaceConflict(t.Context(), args[0], args[1], args[2]); !errors.Is(err, ErrMutationConflict) { |
| 84 | t.Fatalf("unpublished identity %v accepted: %v", args, err) |
| 85 | } |
| 86 | } |
| 87 | final, err := s.Load(t.Context()) |
| 88 | if err != nil || final.Generation != after.Generation { |
| 89 | t.Fatalf("failed settlement changed registry: generation %d -> %d, err=%v", after.Generation, final.Generation, err) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestSettleImportedWorkspaceConflictPreservesAmbiguousAlias(t *testing.T) { |
| 94 | s := NewStore(filepath.Join(t.TempDir(), "state.json")) |
| 95 | first := SourceMapping{SourceKey: "old-key", Path: "/source.jsonl", HeadID: "head", Format: "legacy", Fingerprint: "version", SessionID: "first", WorkspaceID: "global"} |
| 96 | second := first |
| 97 | second.SourceKey, second.SessionID = "other-old-key", "second" |
| 98 | identity := State{SourceMappings: map[string]SourceMapping{first.SourceKey: first, second.SourceKey: second}} |
| 99 | alias := identity.SourceKeys(first.SourceKey)[1] |
| 100 | if err := s.mutate(t.Context(), func(state *State) error { |
| 101 | state.Workspaces["global"] = Workspace{ID: "global", Root: t.TempDir(), SessionIDs: []string{"first", "second"}} |
| 102 | state.SessionStates["first"], state.SessionStates["second"] = SessionState{Lifecycle: Active}, SessionState{Lifecycle: Active} |
| 103 | state.SourceMappings[first.SourceKey], state.SourceMappings[second.SourceKey] = first, second |
| 104 | state.RecoveryEntries["ambiguous"] = RecoveryEntry{ID: "ambiguous", SourceKey: alias, HeadID: first.HeadID, Format: first.Format, |
| 105 | Fingerprint: first.Fingerprint, Reason: "workspace_conflict", Status: "pending"} |
| 106 | return nil |
| 107 | }); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | before, err := s.Load(t.Context()) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if err := s.SettleImportedWorkspaceConflict(t.Context(), first.SourceKey, first.Fingerprint, first.SessionID); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | if err := s.SettleImportedWorkspaceConflict(t.Context(), alias, first.Fingerprint, first.SessionID); !errors.Is(err, ErrMutationConflict) { |
| 118 | t.Fatalf("ambiguous import identity accepted: %v", err) |
| 119 | } |
| 120 | after, err := s.Load(t.Context()) |
| 121 | if err != nil || after.Generation != before.Generation || after.RecoveryEntries["ambiguous"].Status != "pending" { |
| 122 | t.Fatalf("ambiguous recovery was changed: %+v, err=%v", after.RecoveryEntries, err) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestSettleReviewedImportWorkspaceConflictUsesExactVersion(t *testing.T) { |
| 127 | for _, lifecycle := range []string{Active, Archived, Deleted} { |
| 128 | t.Run(lifecycle, func(t *testing.T) { |
| 129 | s := NewStore(filepath.Join(t.TempDir(), "state.json")) |
| 130 | base := SourceMapping{SourceKey: "old-key", Path: "/source.jsonl", HeadID: "head", Format: "legacy", Fingerprint: "v1", SessionID: "original", WorkspaceID: "global"} |
| 131 | reviewed := base |
| 132 | reviewed.SourceKey, reviewed.Fingerprint, reviewed.SessionID = base.SourceKey+":review:v2", "v2", "reviewed" |
| 133 | identity := State{SourceMappings: map[string]SourceMapping{base.SourceKey: base, reviewed.SourceKey: reviewed}} |
| 134 | alias := identity.SourceKeys(base.SourceKey)[1] |
| 135 | entries := map[string]RecoveryEntry{} |
| 136 | for id, change := range map[string]func(*RecoveryEntry){ |
| 137 | "base": func(e *RecoveryEntry) {}, |
| 138 | "base-alias": func(e *RecoveryEntry) { e.SourceKey = alias }, |
| 139 | "review-key": func(e *RecoveryEntry) { e.SourceKey = reviewed.SourceKey }, |
| 140 | "old-version": func(e *RecoveryEntry) { e.Fingerprint = "v1" }, |
| 141 | "unknown-version": func(e *RecoveryEntry) { e.Fingerprint = "" }, |
| 142 | "other-head": func(e *RecoveryEntry) { e.HeadID = "other-head" }, |
| 143 | "other-source": func(e *RecoveryEntry) { e.SourceKey = "other-source" }, |
| 144 | "other-review": func(e *RecoveryEntry) { e.SourceKey = base.SourceKey + ":review:v3" }, |
| 145 | "other-reason": func(e *RecoveryEntry) { e.Reason = "source_changed_after_adoption" }, |
| 146 | } { |
| 147 | entry := RecoveryEntry{ID: id, SourceKey: base.SourceKey, HeadID: base.HeadID, Format: "legacy", Fingerprint: "v2", Reason: "workspace_conflict", Status: "pending"} |
| 148 | change(&entry) |
| 149 | entries[id] = entry |
| 150 | } |
| 151 | if err := s.mutate(t.Context(), func(state *State) error { |
| 152 | members := []string{"original"} |
| 153 | if lifecycle != Deleted { |
| 154 | members = append(members, "reviewed") |
| 155 | } |
| 156 | state.Workspaces["global"] = Workspace{ID: "global", Root: t.TempDir(), SessionIDs: members} |
| 157 | state.SessionStates["original"], state.SessionStates["reviewed"] = SessionState{Lifecycle: Active}, SessionState{Lifecycle: lifecycle} |
| 158 | state.SourceMappings[base.SourceKey], state.SourceMappings[reviewed.SourceKey] = base, reviewed |
| 159 | maps.Copy(state.RecoveryEntries, entries) |
| 160 | return nil |
| 161 | }); err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | before, err := s.Load(t.Context()) |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | for range 2 { |
| 169 | if err := s.SettleImportedWorkspaceConflict(t.Context(), reviewed.SourceKey, reviewed.Fingerprint, reviewed.SessionID); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | } |
| 173 | after, err := s.Load(t.Context()) |
| 174 | if err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | for id, entry := range before.RecoveryEntries { |
| 178 | if lifecycle != Deleted && (id == "base" || id == "base-alias" || id == "review-key") { |
| 179 | entry.Status, entry.SessionID = "restored", reviewed.SessionID |
| 180 | } |
| 181 | if got := after.RecoveryEntries[id]; !reflect.DeepEqual(got, entry) { |
| 182 | t.Errorf("%s = %+v, want %+v", id, got, entry) |
| 183 | } |
| 184 | } |
| 185 | if !reflect.DeepEqual(before.SourceMappings, after.SourceMappings) || !reflect.DeepEqual(before.SessionStates, after.SessionStates) || !reflect.DeepEqual(before.Workspaces, after.Workspaces) { |
| 186 | t.Fatal("settlement changed an existing adoption or lifecycle") |
| 187 | } |
| 188 | wantGeneration := before.Generation |
| 189 | if lifecycle != Deleted { |
| 190 | wantGeneration++ |
| 191 | } |
| 192 | if after.Generation != wantGeneration { |
| 193 | t.Fatalf("non-idempotent settlement: generation=%d want=%d", after.Generation, wantGeneration) |
| 194 | } |
| 195 | }) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func TestSettleReviewedImportWorkspaceConflictPreservesAmbiguousBaseAlias(t *testing.T) { |
| 200 | s := NewStore(filepath.Join(t.TempDir(), "state.json")) |
| 201 | first := SourceMapping{SourceKey: "old-key:review:version", Path: "/source.jsonl", HeadID: "head", Format: "legacy", Fingerprint: "version", SessionID: "first", WorkspaceID: "global"} |
| 202 | second := first |
| 203 | second.SourceKey, second.SessionID = "other-old-key:review:version", "second" |
| 204 | identity := State{SourceMappings: map[string]SourceMapping{first.SourceKey: first, second.SourceKey: second}} |
| 205 | alias := identity.SourceKeys(first.SourceKey)[1] |
| 206 | if err := s.mutate(t.Context(), func(state *State) error { |
| 207 | state.Workspaces["global"] = Workspace{ID: "global", Root: t.TempDir(), SessionIDs: []string{"first", "second"}} |
| 208 | state.SessionStates["first"], state.SessionStates["second"] = SessionState{Lifecycle: Active}, SessionState{Lifecycle: Active} |
| 209 | state.SourceMappings[first.SourceKey], state.SourceMappings[second.SourceKey] = first, second |
| 210 | state.RecoveryEntries["ambiguous"] = RecoveryEntry{ID: "ambiguous", SourceKey: strings.TrimSuffix(alias, ":review:version"), HeadID: first.HeadID, Format: first.Format, |
| 211 | Fingerprint: first.Fingerprint, Reason: "workspace_conflict", Status: "pending"} |
| 212 | return nil |
| 213 | }); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | before, err := s.Load(t.Context()) |
| 217 | if err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | if err := s.SettleImportedWorkspaceConflict(t.Context(), first.SourceKey, first.Fingerprint, first.SessionID); err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | if err := s.SettleImportedWorkspaceConflict(t.Context(), alias, first.Fingerprint, first.SessionID); !errors.Is(err, ErrMutationConflict) { |
| 224 | t.Fatalf("ambiguous import identity accepted: %v", err) |
| 225 | } |
| 226 | after, err := s.Load(t.Context()) |
| 227 | if err != nil || after.Generation != before.Generation || after.RecoveryEntries["ambiguous"].Status != "pending" { |
| 228 | t.Fatalf("ambiguous recovery was changed: %+v, err=%v", after.RecoveryEntries, err) |
| 229 | } |
| 230 | } |
| 231 |