Skip to content

Commit 8466640

Browse files
Invalidate stale Trash refreshes after restoring from another source
1 parent 381a06b commit 8466640

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

‎internal/tui/trash.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ func (v *mailView) trashRestored(msg trashRestoredMsg) tea.Cmd {
7575
if v.inThread && v.threadFromTrash && v.topicID == msg.topicID {
7676
v.ExitThread()
7777
}
78+
// A live read begun before the restore may still contain this topic, even
79+
// if the reader switched away and returns to Trash before the read arrives.
80+
v.liveRequestID++
7881
if v.currentSourceKind() != mail.KindTrash {
7982
return notify("Thread restored")
8083
}
81-
// A page or live head read begun before the restore may still contain this
82-
// topic. Ignore those answers so they cannot resurrect the restored row.
84+
// Navigation already invalidates pages; only cancel paging here in Trash.
8385
v.moreRequestID++
84-
v.liveRequestID++
8586
v.postingPaging.loading = false
8687
if index := postingIndexIn(v.postingList.postings, msg.topicID); index >= 0 {
8788
v.removePostingAt(index)

‎internal/tui/trash_test.go‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"strings"
8+
"sync/atomic"
89
"testing"
910

1011
hey "github.com/basecamp/hey-sdk/go/pkg/hey"
@@ -257,3 +258,74 @@ func TestPostingActionCannotRemoveTrashTopicWithSameNumericID(t *testing.T) {
257258
t.Fatal("posting completion affected the Trash topic instead of only the search result")
258259
}
259260
}
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

Comments
 (0)