5
5
6
6
"github.com/matrix-org/complement/internal/b"
7
7
"github.com/matrix-org/complement/internal/client"
8
+ "github.com/matrix-org/complement/internal/must"
8
9
"github.com/matrix-org/complement/runtime"
9
10
"github.com/tidwall/gjson"
10
11
)
@@ -23,30 +24,43 @@ func TestTxnInEvent(t *testing.T) {
23
24
// Create a room where we can send events.
24
25
roomID := c .CreateRoom (t , map [string ]interface {}{})
25
26
27
+ txnId := "abcdefg"
26
28
// Let's send an event, and wait for it to appear in the timeline.
27
- eventID := c .SendEventSynced (t , roomID , b.Event {
29
+ eventID := c .SendEventUnsyncedWithTxnID (t , roomID , b.Event {
28
30
Type : "m.room.message" ,
29
31
Content : map [string ]interface {}{
30
32
"msgtype" : "m.text" ,
31
33
"body" : "first" ,
32
34
},
33
- })
35
+ }, txnId )
34
36
35
37
// The transaction ID should be present on the GET /rooms/{roomID}/event/{eventID} response.
36
38
res := c .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "event" , eventID })
37
39
body := client .ParseJSON (t , res )
38
40
result := gjson .ParseBytes (body )
39
41
if ! result .Get ("unsigned.transaction_id" ).Exists () {
40
- t .Fatalf ("Event did not have a 'transaction_id' on the GET /rooms/%s/event/%s response" , roomID , eventID )
42
+ t .Fatalf ("Event did not have a 'unsigned.transaction_id' on the GET /rooms/%s/event/%s response" , roomID , eventID )
43
+ }
44
+
45
+ txnIdFromResult := result .Get ("unsigned.transaction_id" ).Str
46
+
47
+ if txnIdFromResult != txnId {
48
+ 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 )
41
49
}
42
50
}
43
51
44
52
45
- func mustHaveTransactionID (t * testing.T , roomID , eventID string ) client.SyncCheckOpt {
53
+ func mustHaveTransactionID (t * testing.T , roomID , eventID , expectedTxnId string ) client.SyncCheckOpt {
46
54
return client .SyncTimelineHas (roomID , func (r gjson.Result ) bool {
47
55
if r .Get ("event_id" ).Str == eventID {
48
56
if ! r .Get ("unsigned.transaction_id" ).Exists () {
49
- t .Fatalf ("Event %s in room %s should have a 'transaction_id', but it did not" , eventID , roomID )
57
+ t .Fatalf ("Event %s in room %s should have a 'unsigned.transaction_id', but it did not" , eventID , roomID )
58
+ }
59
+
60
+ txnIdFromSync := r .Get ("unsigned.transaction_id" ).Str
61
+
62
+ if txnIdFromSync != expectedTxnId {
63
+ t .Fatalf ("Event %s in room %s should have a 'unsigned.transaction_id' of %s but found %s" , eventID , roomID , expectedTxnId , txnIdFromSync )
50
64
}
51
65
52
66
return true
@@ -61,7 +75,7 @@ func mustNotHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncC
61
75
if r .Get ("event_id" ).Str == eventID {
62
76
res := r .Get ("unsigned.transaction_id" )
63
77
if res .Exists () {
64
- t .Fatalf ("Event %s in room %s should NOT have a 'transaction_id', but it did (%s)" , eventID , roomID , res .Str )
78
+ t .Fatalf ("Event %s in room %s should NOT have a 'unsigned. transaction_id', but it did (%s)" , eventID , roomID , res .Str )
65
79
}
66
80
67
81
return true
@@ -89,21 +103,22 @@ func TestTxnScopeOnLocalEcho(t *testing.T) {
89
103
// Create a room where we can send events.
90
104
roomID := c1 .CreateRoom (t , map [string ]interface {}{})
91
105
106
+ txnId := "abdefgh"
92
107
// Let's send an event, and wait for it to appear in the timeline.
93
- eventID := c1 .SendEventUnsynced (t , roomID , b.Event {
108
+ eventID := c1 .SendEventUnsyncedWithTxnID (t , roomID , b.Event {
94
109
Type : "m.room.message" ,
95
110
Content : map [string ]interface {}{
96
111
"msgtype" : "m.text" ,
97
112
"body" : "first" ,
98
113
},
99
- })
114
+ }, txnId )
100
115
101
116
// When syncing, we should find the event and it should have a transaction ID on the first client.
102
- c1 .MustSyncUntil (t , client.SyncReq {}, mustHaveTransactionID (t , roomID , eventID ))
117
+ c1 .MustSyncUntil (t , client.SyncReq {}, mustHaveTransactionID (t , roomID , eventID , txnId ))
103
118
104
119
// Create a second client, inheriting the first device ID.
105
120
c2 := deployment .Client (t , "hs1" , "" )
106
- c2 .UserID , c2 .AccessToken = c2 .LoginUserWithDeviceID (t , "alice" , "password" , c1 .DeviceID )
121
+ c2 .UserID , c2 .AccessToken , _ = c2 .LoginUser (t , "alice" , "password" , client . WithDeviceID ( c1 .DeviceID ) )
107
122
c2 .DeviceID = c1 .DeviceID
108
123
109
124
// When syncing, we should find the event and it should *not* have a transaction ID on the second client.
@@ -128,7 +143,7 @@ func TestTxnIdempotencyScopedToClientSession(t *testing.T) {
128
143
// Create a room where we can send events.
129
144
roomID := c1 .CreateRoom (t , map [string ]interface {}{})
130
145
131
- txnId := 1
146
+ txnId := "abcdef"
132
147
event := b.Event {
133
148
Type : "m.room.message" ,
134
149
Content : map [string ]interface {}{
@@ -141,16 +156,14 @@ func TestTxnIdempotencyScopedToClientSession(t *testing.T) {
141
156
142
157
// Create a second client, inheriting the first device ID.
143
158
c2 := deployment .Client (t , "hs1" , "" )
144
- c2 .UserID , c2 .AccessToken = c2 .LoginUserWithDeviceID (t , "alice" , "password" , c1 .DeviceID )
159
+ c2 .UserID , c2 .AccessToken , _ = c2 .LoginUser (t , "alice" , "password" , client . WithDeviceID ( c1 .DeviceID ) )
145
160
c2 .DeviceID = c1 .DeviceID
146
161
147
162
// send another event with the same txnId
148
163
eventID2 := c2 .SendEventUnsyncedWithTxnID (t , roomID , event , txnId )
149
164
150
165
// the two events should have different event IDs as they came from different clients
151
- if eventID1 == eventID2 {
152
- t .Fatalf ("Expected event IDs to be different from two clients sharing the same device ID" )
153
- }
166
+ must .NotEqualStr (t , eventID2 , eventID1 , "Expected eventID1 and eventID2 to be different from two clients sharing the same device ID" )
154
167
}
155
168
156
169
// TestTxnIdempotency tests that PUT requests idempotency follows required semantics
@@ -169,7 +182,7 @@ func TestTxnIdempotency(t *testing.T) {
169
182
roomID2 := c1 .CreateRoom (t , map [string ]interface {}{})
170
183
171
184
// choose a transaction ID
172
- txnId := 1
185
+ txnId := "abc"
173
186
event1 := b.Event {
174
187
Type : "m.room.message" ,
175
188
Content : map [string ]interface {}{
@@ -191,21 +204,15 @@ func TestTxnIdempotency(t *testing.T) {
191
204
// we send the identical event again and should get back the same event ID
192
205
eventID2 := c1 .SendEventUnsyncedWithTxnID (t , roomID1 , event1 , txnId )
193
206
194
- if eventID1 != eventID2 {
195
- t .Fatalf ("Expected event IDs to be the same, but they were not" )
196
- }
207
+ must .EqualStr (t , eventID2 , eventID1 , "Expected eventID1 and eventID2 to be the same, but they were not" )
197
208
198
209
// even if we change the content we should still get back the same event ID as transaction ID is the same
199
210
eventID3 := c1 .SendEventUnsyncedWithTxnID (t , roomID1 , event2 , txnId )
200
211
201
- if eventID1 != eventID3 {
202
- t .Fatalf ("Expected event IDs to be the same even with different content, but they were not" )
203
- }
212
+ must .EqualStr (t , eventID3 , eventID1 , "Expected eventID3 and eventID2 to be the same even with different content, but they were not" )
204
213
205
214
// if we change the room ID we should be able to use the same transaction ID
206
215
eventID4 := c1 .SendEventUnsyncedWithTxnID (t , roomID2 , event1 , txnId )
207
216
208
- if eventID4 == eventID3 {
209
- t .Fatalf ("Expected event IDs to be the different, but they were not" )
210
- }
217
+ must .NotEqualStr (t , eventID4 , eventID3 , "Expected eventID4 and eventID3 to be different, but they were not" )
211
218
}
0 commit comments