From bb7d6aaa5e5be4d89e93d94bb97e38e50971ff88 Mon Sep 17 00:00:00 2001 From: deluan Date: Wed, 4 Mar 2026 12:44:24 -0500 Subject: [PATCH] test: use JSON unmarshalling for truncation test assertions Replace fragile strings.Contains checks with proper JSON unmarshalling to validate truncated fields by their exact values and verify URL fields are correctly omitted. --- rpc_test.go | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/rpc_test.go b/rpc_test.go index ba5270d..352a281 100644 --- a/rpc_test.go +++ b/rpc_test.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "errors" "strings" @@ -446,17 +447,34 @@ var _ = Describe("discordRPC", func() { longAlbum := strings.Repeat("B", 200) longURL := "https://example.com/" + strings.Repeat("x", 237) + truncatedName := strings.Repeat("N", 127) + "…" + truncatedTitle := strings.Repeat("T", 127) + "…" + truncatedArtist := strings.Repeat("A", 127) + "…" + truncatedAlbum := strings.Repeat("B", 127) + "…" + host.WebSocketMock.On("SendText", "testuser", mock.MatchedBy(func(msg string) bool { - // Text fields should be truncated to 128 runes (127 + "…") - truncatedName := strings.Repeat("N", 127) + "…" - truncatedTitle := strings.Repeat("T", 127) + "…" - truncatedArtist := strings.Repeat("A", 127) + "…" - truncatedAlbum := strings.Repeat("B", 127) + "…" - return strings.Contains(msg, truncatedName) && - strings.Contains(msg, truncatedTitle) && - strings.Contains(msg, truncatedArtist) && - strings.Contains(msg, truncatedAlbum) && - !strings.Contains(msg, longURL) // URL should be omitted + var message struct { + D json.RawMessage `json:"d"` + } + if err := json.Unmarshal([]byte(msg), &message); err != nil { + return false + } + var presence presencePayload + if err := json.Unmarshal(message.D, &presence); err != nil { + return false + } + if len(presence.Activities) != 1 { + return false + } + act := presence.Activities[0] + return act.Name == truncatedName && + act.Details == truncatedTitle && + act.State == truncatedArtist && + act.Assets.LargeText == truncatedAlbum && + act.DetailsURL == "" && + act.StateURL == "" && + act.Assets.LargeURL == "" && + act.Assets.SmallURL == "" })).Return(nil) err := r.sendActivity("client123", "testuser", "token123", activity{