@@ -7,20 +7,24 @@ package csapi_tests
77import (
88 "testing"
99
10+ "github.com/tidwall/gjson"
11+
1012 "github.com/matrix-org/complement/internal/b"
1113 "github.com/matrix-org/complement/internal/client"
1214 "github.com/matrix-org/complement/internal/match"
1315 "github.com/matrix-org/complement/internal/must"
1416)
1517
1618func TestPresence (t * testing.T ) {
17- deployment := Deploy (t , b .BlueprintAlice )
19+ deployment := Deploy (t , b .BlueprintOneToOneRoom )
1820 defer deployment .Destroy (t )
1921
20- authedClient := deployment .Client (t , "hs1" , "@alice:hs1" )
22+ alice := deployment .Client (t , "hs1" , "@alice:hs1" )
23+ bob := deployment .Client (t , "hs1" , "@bob:hs1" )
24+
2125 // sytest: GET /presence/:user_id/status fetches initial status
2226 t .Run ("GET /presence/:user_id/status fetches initial status" , func (t * testing.T ) {
23- res := authedClient .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" })
27+ res := alice .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" })
2428 must .MatchResponse (t , res , match.HTTPResponse {
2529 JSON : []match.JSON {
2630 match .JSONKeyPresent ("presence" ),
@@ -34,16 +38,56 @@ func TestPresence(t *testing.T) {
3438 "status_msg" : statusMsg ,
3539 "presence" : "online" ,
3640 })
37- res := authedClient .DoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" }, reqBody )
41+ res := alice .DoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" }, reqBody )
3842 must .MatchResponse (t , res , match.HTTPResponse {
3943 StatusCode : 200 ,
4044 })
41- res = authedClient .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" })
45+ res = alice .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" })
4246 must .MatchResponse (t , res , match.HTTPResponse {
4347 JSON : []match.JSON {
4448 match .JSONKeyPresent ("presence" ),
4549 match .JSONKeyEqual ("status_msg" , statusMsg ),
4650 },
4751 })
4852 })
53+ // sytest: Presence can be set from sync
54+ t .Run ("Presence can be set from sync" , func (t * testing.T ) {
55+ _ , bobSinceToken := bob .MustSync (t , client.SyncReq {TimeoutMillis : "0" })
56+
57+ alice .MustSync (t , client.SyncReq {TimeoutMillis : "0" , SetPresence : "unavailable" })
58+
59+ bob .MustSyncUntil (t , client.SyncReq {Since : bobSinceToken }, client .SyncPresenceHas (alice .UserID , b .Ptr ("unavailable" )))
60+ })
61+ // sytest: Presence changes are reported to local room members
62+ t .Run ("Presence changes are reported to local room members" , func (t * testing.T ) {
63+ _ , bobSinceToken := bob .MustSync (t , client.SyncReq {TimeoutMillis : "0" })
64+
65+ statusMsg := "Update for room members"
66+ alice .MustDoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" },
67+ client .WithJSONBody (t , map [string ]interface {}{
68+ "status_msg" : statusMsg ,
69+ "presence" : "online" ,
70+ }),
71+ )
72+
73+ bob .MustSyncUntil (t , client.SyncReq {Since : bobSinceToken },
74+ client .SyncPresenceHas (alice .UserID , b .Ptr ("online" ), func (ev gjson.Result ) bool {
75+ return ev .Get ("content.status_msg" ).Str == statusMsg
76+ }),
77+ )
78+ })
79+ // sytest: Presence changes to UNAVAILABLE are reported to local room members
80+ t .Run ("Presence changes to UNAVAILABLE are reported to local room members" , func (t * testing.T ) {
81+ _ , bobSinceToken := bob .MustSync (t , client.SyncReq {TimeoutMillis : "0" })
82+
83+ alice .MustDoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "presence" , "@alice:hs1" , "status" },
84+ client .WithJSONBody (t , map [string ]interface {}{
85+ "presence" : "unavailable" ,
86+ }),
87+ )
88+
89+ bob .MustSyncUntil (t , client.SyncReq {Since : bobSinceToken },
90+ client .SyncPresenceHas (alice .UserID , b .Ptr ("unavailable" )),
91+ )
92+ })
4993}
0 commit comments