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.
This commit is contained in:
+28
-10
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -446,17 +447,34 @@ var _ = Describe("discordRPC", func() {
|
|||||||
longAlbum := strings.Repeat("B", 200)
|
longAlbum := strings.Repeat("B", 200)
|
||||||
longURL := "https://example.com/" + strings.Repeat("x", 237)
|
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 {
|
host.WebSocketMock.On("SendText", "testuser", mock.MatchedBy(func(msg string) bool {
|
||||||
// Text fields should be truncated to 128 runes (127 + "…")
|
var message struct {
|
||||||
truncatedName := strings.Repeat("N", 127) + "…"
|
D json.RawMessage `json:"d"`
|
||||||
truncatedTitle := strings.Repeat("T", 127) + "…"
|
}
|
||||||
truncatedArtist := strings.Repeat("A", 127) + "…"
|
if err := json.Unmarshal([]byte(msg), &message); err != nil {
|
||||||
truncatedAlbum := strings.Repeat("B", 127) + "…"
|
return false
|
||||||
return strings.Contains(msg, truncatedName) &&
|
}
|
||||||
strings.Contains(msg, truncatedTitle) &&
|
var presence presencePayload
|
||||||
strings.Contains(msg, truncatedArtist) &&
|
if err := json.Unmarshal(message.D, &presence); err != nil {
|
||||||
strings.Contains(msg, truncatedAlbum) &&
|
return false
|
||||||
!strings.Contains(msg, longURL) // URL should be omitted
|
}
|
||||||
|
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)
|
})).Return(nil)
|
||||||
|
|
||||||
err := r.sendActivity("client123", "testuser", "token123", activity{
|
err := r.sendActivity("client123", "testuser", "token123", activity{
|
||||||
|
|||||||
Reference in New Issue
Block a user