From 6ce5b65f1bdc716cbbfab18f3dc09646afc2d8a0 Mon Sep 17 00:00:00 2001 From: Michael McCarty Date: Tue, 1 Dec 2026 14:35:34 -0800 Subject: [PATCH] fix(boost): Clear components on close and test it Update stones and teamwork page handlers to properly handle the "close" component action by: - Deleting the relevant page cache entry immediately - Sending an immediate message update with components cleared (stones sets ComponentsV1 and ClearComponents) - Returning early to prevent further rendering Remove the old stones "close" early-return behavior and the teamwork drawButtons/DeferUpdate flow that left stale components. Add unit tests to verify that: - Close clears the corresponding cache entry - Stones close response sets ClearComponents=true and ComponentsV1=true - Teamwork close responds with a message update response --- src/boost/stones_pages.go | 12 +++++++++--- src/boost/stones_test.go | 28 ++++++++++++++++++++++++++++ src/boost/teamwork_pages.go | 21 ++++++++++++--------- src/boost/teamwork_pages_test.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 src/boost/teamwork_pages_test.go diff --git a/src/boost/stones_pages.go b/src/boost/stones_pages.go index 6f2bbefb..e13f2863 100644 --- a/src/boost/stones_pages.go +++ b/src/boost/stones_pages.go @@ -265,6 +265,15 @@ func HandleStonesPage(client dc.Client, e *dc.ComponentEvent) { stonesCacheMutex.Lock() delete(stonesCacheMap, reaction[1]) stonesCacheMutex.Unlock() + + if err := e.Update(dc.Message{ + Content: e.MessageContent(), + ComponentsV1: true, + ClearComponents: true, + }); err != nil { + log.Println(err) + } + return } err := e.DeferUpdate() @@ -281,9 +290,6 @@ func HandleStonesPage(client dc.Client, e *dc.ComponentEvent) { if len(reaction) == 3 && reaction[2] == "toggle" { toggle = true } - if len(reaction) == 3 && reaction[2] == "close" { - return - } sendStonesPage(client, e, false, reaction[1], refresh, links, toggle) } diff --git a/src/boost/stones_test.go b/src/boost/stones_test.go index 02f62d15..bd491613 100644 --- a/src/boost/stones_test.go +++ b/src/boost/stones_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" + "github.com/mkmccarty/TokenTimeBoostBot/src/dc/dctest" "github.com/mkmccarty/TokenTimeBoostBot/src/ei" "google.golang.org/protobuf/proto" ) @@ -405,3 +406,30 @@ func TestRenderCoopStatusStones_PartialAndFullHabsWithColleggtibles(t *testing.T t.Errorf("PrivateFarm details expected LR 4.906 and SR 5.055, got: %s", row) } } + +func TestHandleStonesPage_CloseClearsComponentsAndCache(t *testing.T) { + uuidStr := "test-stones-close-uuid" + stonesCacheMutex.Lock() + stonesCacheMap[uuidStr] = stonesCache{uuidStr: uuidStr} + stonesCacheMutex.Unlock() + + event := dctest.ComponentButtonEvent("fd_stones#" + uuidStr + "#close") + HandleStonesPage(nil, event) + + stonesCacheMutex.Lock() + _, stillExists := stonesCacheMap[uuidStr] + stonesCacheMutex.Unlock() + if stillExists { + t.Errorf("expected stones cache entry %q to be deleted on close", uuidStr) + } + + if event.LastResponse == nil { + t.Fatalf("expected Close button to send a message update response, got nil") + } + if !event.LastResponse.ClearComponents { + t.Errorf("expected Close response to set ClearComponents=true") + } + if !event.LastResponse.ComponentsV1 { + t.Errorf("expected Close response to set ComponentsV1=true") + } +} diff --git a/src/boost/teamwork_pages.go b/src/boost/teamwork_pages.go index 8dc3ab7c..6e1fd45a 100644 --- a/src/boost/teamwork_pages.go +++ b/src/boost/teamwork_pages.go @@ -202,27 +202,30 @@ func HandleTeamworkPage(e *dc.ComponentEvent) { toggle := false reaction := strings.Split(e.CustomID(), "#") + if len(reaction) == 3 && reaction[2] == "close" { + delete(teamworkCacheMap, reaction[1]) + if err := e.Update(dc.Message{ + Ephemeral: e.MessageIsEphemeral(), + Components: e.MessageComponentsWithoutActionRows(), + }); err != nil { + log.Println(err) + } + return + } + err := e.DeferUpdate() if err != nil { log.Println(err) return } - drawButtons := true if len(reaction) == 3 && reaction[2] == "refresh" { refresh = true } if len(reaction) == 3 && reaction[2] == "toggle" { toggle = true } - if len(reaction) == 3 && reaction[2] == "close" { - drawButtons = false - } - sendTeamworkPage(e, false, reaction[1], refresh, toggle, drawButtons) - - if !drawButtons { - delete(teamworkCacheMap, reaction[1]) - } + sendTeamworkPage(e, false, reaction[1], refresh, toggle, true) } // getTeamworkComponents returns the components for the token value diff --git a/src/boost/teamwork_pages_test.go b/src/boost/teamwork_pages_test.go new file mode 100644 index 00000000..76c8f575 --- /dev/null +++ b/src/boost/teamwork_pages_test.go @@ -0,0 +1,28 @@ +package boost + +import ( + "testing" + + "github.com/mkmccarty/TokenTimeBoostBot/src/dc/dctest" +) + +func TestHandleTeamworkPage_CloseClearsCacheAndResponds(t *testing.T) { + uuidStr := "test-teamwork-close-uuid" + teamworkCacheMap[uuidStr] = teamworkCache{ + uuidStr: uuidStr, + page: 1, + previousPage: 0, + pages: 2, + names: []string{"PlayerA", "PlayerB"}, + } + + event := dctest.ComponentButtonEvent("fd_teamwork#" + uuidStr + "#close") + HandleTeamworkPage(event) + + if _, stillExists := teamworkCacheMap[uuidStr]; stillExists { + t.Errorf("expected teamwork cache entry %q to be deleted on close", uuidStr) + } + if event.LastResponse == nil { + t.Fatalf("expected Close button to send a message update response, got nil") + } +}