|
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
7 | 7 | "strings" |
| 8 | + "sync/atomic" |
8 | 9 | "testing" |
9 | 10 |
|
10 | 11 | hey "github.com/basecamp/hey-sdk/go/pkg/hey" |
@@ -257,3 +258,74 @@ func TestPostingActionCannotRemoveTrashTopicWithSameNumericID(t *testing.T) { |
257 | 258 | t.Fatal("posting completion affected the Trash topic instead of only the search result") |
258 | 259 | } |
259 | 260 | } |
| 261 | + |
| 262 | +func TestRestoreAwayFromTrashInvalidatesEarlierLiveRefresh(t *testing.T) { |
| 263 | + var restored atomic.Bool |
| 264 | + v := trashView(t, func(w http.ResponseWriter, r *http.Request) { |
| 265 | + switch { |
| 266 | + case r.Method == http.MethodPut && r.URL.Path == "/topics/701/status/active.json": |
| 267 | + restored.Store(true) |
| 268 | + w.WriteHeader(http.StatusOK) |
| 269 | + case r.Method == http.MethodGet && r.URL.Path == "/topics/trash.json": |
| 270 | + w.Header().Set("Content-Type", "application/json") |
| 271 | + topics := []map[string]any{{"id": 702, "name": "Hotel receipt"}} |
| 272 | + if !restored.Load() { |
| 273 | + topics = append(topics, map[string]any{"id": 701, "name": "Conference travel plans"}) |
| 274 | + } |
| 275 | + _ = json.NewEncoder(w).Encode(map[string]any{"topics": topics}) |
| 276 | + default: |
| 277 | + t.Errorf("unexpected request %s %s", r.Method, r.URL) |
| 278 | + w.WriteHeader(http.StatusNotFound) |
| 279 | + } |
| 280 | + }) |
| 281 | + |
| 282 | + // Hold the old response until after a restore and a round trip through the Imbox. |
| 283 | + refresh := v.refreshBox(AnyBoxChanged) |
| 284 | + if refresh == nil { |
| 285 | + t.Fatal("Trash refresh did not start") |
| 286 | + } |
| 287 | + stale := refresh() |
| 288 | + restore := v.HandleContentKey(keyPress("i")) |
| 289 | + if restore == nil { |
| 290 | + t.Fatal("restore did not start") |
| 291 | + } |
| 292 | + v.switchBox(0) |
| 293 | + v.Update(currentPostingsLoaded(v, []mail.Posting{{ID: 701, TopicID: 900, Name: "Dinner reservation"}})) |
| 294 | + moreRequest := v.moreRequestID |
| 295 | + v.Update(restore()) |
| 296 | + if len(v.postingList.postings) != 1 || v.postingList.postings[0].TopicID != 900 { |
| 297 | + t.Fatal("restore changed the Imbox row with the same numeric ID") |
| 298 | + } |
| 299 | + if v.moreRequestID != moreRequest { |
| 300 | + t.Fatal("restore invalidated paging for the other source") |
| 301 | + } |
| 302 | + read := v.switchBox(v.trashIndex()) |
| 303 | + v.Update(read()) |
| 304 | + if got := postingIDs(v.postingList.postings); len(got) != 1 || postingIndexIn(v.postingList.postings, 702) < 0 { |
| 305 | + t.Fatalf("fresh Trash read = %v, want only the remaining topic", got) |
| 306 | + } |
| 307 | + v.Update(stale) |
| 308 | + if got := postingIDs(v.postingList.postings); len(got) != 1 || postingIndexIn(v.postingList.postings, 702) < 0 { |
| 309 | + t.Fatalf("stale refresh reinserted the restored topic: %v", got) |
| 310 | + } |
| 311 | +} |
| 312 | + |
| 313 | +func TestFailedRestoreAwayFromTrashKeepsLiveRefreshValid(t *testing.T) { |
| 314 | + v := trashView(t, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusForbidden) }) |
| 315 | + restore := v.HandleContentKey(keyPress("i")) |
| 316 | + oldLive := v.liveRequestID |
| 317 | + v.switchBox(0) |
| 318 | + v.Update(restore()) |
| 319 | + if v.liveRequestID != oldLive || v.pendingMutations != 0 || v.trashRestoreID != 0 { |
| 320 | + t.Fatal("failed restore invalidated the refresh or left the mutation pending") |
| 321 | + } |
| 322 | + v.switchBox(v.trashIndex()) |
| 323 | + v.Update(currentPostingsLoaded(v, []mail.Posting{{ID: 701, TopicID: 701}})) |
| 324 | + v.Update(postingsRefreshedMsg{ |
| 325 | + requestID: oldLive, sourceKind: mail.KindTrash, |
| 326 | + postings: []mail.Posting{{ID: 701, TopicID: 701}, {ID: 702, TopicID: 702}}, |
| 327 | + }) |
| 328 | + if len(v.postingList.postings) != 2 { |
| 329 | + t.Fatal("failed restore prevented an otherwise valid refresh") |
| 330 | + } |
| 331 | +} |
0 commit comments