Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/boost/stones_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down
28 changes: 28 additions & 0 deletions src/boost/stones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
}
21 changes: 12 additions & 9 deletions src/boost/teamwork_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/boost/teamwork_pages_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading