Skip to content

Commit

Permalink
Refactor for legibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hughns committed Mar 9, 2023
1 parent 4424fb0 commit 67f7d14
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions tests/csapi/txnid_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package csapi_tests

import (
"fmt"
"testing"

"github.com/matrix-org/complement/internal/b"
Expand Down Expand Up @@ -38,30 +39,24 @@ func TestTxnInEvent(t *testing.T) {
res := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "event", eventID})
body := client.ParseJSON(t, res)
result := gjson.ParseBytes(body)
if !result.Get("unsigned.transaction_id").Exists() {
unsignedTxnId := result.Get("unsigned.transaction_id")
if !unsignedTxnId.Exists() {
t.Fatalf("Event did not have a 'unsigned.transaction_id' on the GET /rooms/%s/event/%s response", roomID, eventID)
}

txnIdFromResult:= result.Get("unsigned.transaction_id").Str

if txnIdFromResult != txnId {
t.Fatalf("Event did not have a 'unsigned.transaction_id' of %s on GET /rooms/%s/event/%s response. Found %s instead", txnId, eventID, roomID, txnIdFromResult)
}
must.EqualStr(t, unsignedTxnId.Str, txnId, fmt.Sprintf("Event did not have a 'unsigned.transaction_id' on GET /rooms/%s/event/%s response", eventID, roomID))
}


func mustHaveTransactionID(t *testing.T, roomID, eventID, expectedTxnId string) client.SyncCheckOpt {
return client.SyncTimelineHas(roomID, func(r gjson.Result) bool {
if r.Get("event_id").Str == eventID {
if !r.Get("unsigned.transaction_id").Exists() {
unsignedTxnId := r.Get("unsigned.transaction_id")
if !unsignedTxnId.Exists() {
t.Fatalf("Event %s in room %s should have a 'unsigned.transaction_id', but it did not", eventID, roomID)
}

txnIdFromSync := r.Get("unsigned.transaction_id").Str

if txnIdFromSync != expectedTxnId {
t.Fatalf("Event %s in room %s should have a 'unsigned.transaction_id' of %s but found %s", eventID, roomID, expectedTxnId, txnIdFromSync)
}
must.EqualStr(t, unsignedTxnId.Str, expectedTxnId, fmt.Sprintf("Event %s in room %s should have a 'unsigned.transaction_id'", eventID, roomID))

return true
}
Expand All @@ -73,9 +68,9 @@ func mustHaveTransactionID(t *testing.T, roomID, eventID, expectedTxnId string)
func mustNotHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt {
return client.SyncTimelineHas(roomID, func(r gjson.Result) bool {
if r.Get("event_id").Str == eventID {
res := r.Get("unsigned.transaction_id")
if res.Exists() {
t.Fatalf("Event %s in room %s should NOT have a 'unsigned.transaction_id', but it did (%s)", eventID, roomID, res.Str)
unsignedTxnId := r.Get("unsigned.transaction_id")
if unsignedTxnId.Exists() {
t.Fatalf("Event %s in room %s should NOT have a 'unsigned.transaction_id', but it did (%s)", eventID, roomID, unsignedTxnId.Str)
}

return true
Expand Down

0 comments on commit 67f7d14

Please sign in to comment.