Changeset View
Changeset View
Standalone View
Standalone View
src/tests/client/sync-test.cpp
| Show All 11 Lines | |||||
| #include <boost/asio.hpp> | #include <boost/asio.hpp> | ||||
| #include <zug/into_vector.hpp> | #include <zug/into_vector.hpp> | ||||
| #include <asio-promise-handler.hpp> | #include <asio-promise-handler.hpp> | ||||
| #include <cursorutil.hpp> | #include <cursorutil.hpp> | ||||
| #include <sdk-model.hpp> | #include <sdk-model.hpp> | ||||
| #include <client/client.hpp> | #include <client/client.hpp> | ||||
| #include <client/actions/sync.hpp> | |||||
| #include "client-test-util.hpp" | #include "client-test-util.hpp" | ||||
| #include "factory.hpp" | #include "factory.hpp" | ||||
| using namespace Kazv::Factory; | using namespace Kazv::Factory; | ||||
| // The example response is adapted from https://matrix.org/docs/spec/client_server/latest | // The example response is adapted from https://matrix.org/docs/spec/client_server/latest | ||||
| static json syncResponseJson = R"({ | static json syncResponseJson = R"({ | ||||
| "next_batch": "s72595_4483_1934", | "next_batch": "s72595_4483_1934", | ||||
| ▲ Show 20 Lines • Show All 215 Lines • ▼ Show 20 Lines | "join": { | ||||
| ], | ], | ||||
| "limited": false | "limited": false | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| })"_json; | })"_json; | ||||
| static auto addNotificationsJson = R"({ | |||||
| "next_batch": "some-example-value", | |||||
| "account_data": { | |||||
| "events": [ | |||||
| { | |||||
| "type": "m.push_rules", | |||||
| "content": { | |||||
| "global": { | |||||
| "override": [{ | |||||
| "rule_id": "moe.kazv.mxc.catch_all", | |||||
| "default": true, | |||||
| "enabled": true, | |||||
| "conditions": [], | |||||
| "actions": ["notify"] | |||||
| }] | |||||
| } | |||||
| } | |||||
| } | |||||
| ] | |||||
| }, | |||||
| "rooms": { | |||||
| "join": { | |||||
| "!exampleroomid:example.com": { | |||||
| "timeline": { | |||||
| "events": [ | |||||
| { | |||||
| "content": { "example": "foo" }, | |||||
| "event_id": "$example:example.com", | |||||
| "sender": "@example:example.org", | |||||
| "origin_server_ts": 1432735824653, | |||||
| "type": "m.room.message" | |||||
| }, | |||||
| { | |||||
| "content": { "example": "foo2" }, | |||||
| "event_id": "$example2:example.com", | |||||
| "sender": "@example:example.org", | |||||
| "origin_server_ts": 1432735824953, | |||||
| "type": "m.room.message" | |||||
| } | |||||
| ], | |||||
| "limited": false | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| })"_json; | |||||
| static auto receiptJson = R"({ | |||||
| "type": "m.receipt", | |||||
| "content": { | |||||
| "$example:example.com": { | |||||
| "m.read": { | |||||
| "@bob:example.com": { | |||||
| "ts": 1432735824653 | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| })"_json; | |||||
| static auto addAndRemoveNotificationsJson = [](auto a, auto r) { | |||||
| a["rooms"]["join"]["!exampleroomid:example.com"]["ephemeral"] = { | |||||
| {"events", { | |||||
| r, | |||||
| }} | |||||
| }; | |||||
| return a; | |||||
| }(addNotificationsJson, receiptJson); | |||||
| static auto removeNotificationsJson = [](auto r) { | |||||
| auto j = R"({ | |||||
| "next_batch": "some-example-value", | |||||
| "rooms": { | |||||
| "join": { | |||||
| "!exampleroomid:example.com": { | |||||
| "timeline": { | |||||
| "events": [], | |||||
| "limited": false | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| })"_json; | |||||
| j["rooms"]["join"]["!exampleroomid:example.com"]["ephemeral"] = { | |||||
| {"events", { | |||||
| r, | |||||
| }} | |||||
| }; | |||||
| return j; | |||||
| }(receiptJson); | |||||
| TEST_CASE("use sync response to update client model", "[client][sync]") | TEST_CASE("use sync response to update client model", "[client][sync]") | ||||
| { | { | ||||
| using namespace Kazv::CursorOp; | using namespace Kazv::CursorOp; | ||||
| boost::asio::io_context io; | boost::asio::io_context io; | ||||
| AsioPromiseHandler ph{io.get_executor()}; | AsioPromiseHandler ph{io.get_executor()}; | ||||
| auto store = createTestClientStore(ph); | auto store = createTestClientStore(ph); | ||||
| ▲ Show 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | TEST_CASE("Sync should remove already sent local echo", "[client][sync]") | ||||
| io.run(); | io.run(); | ||||
| auto r = client.room("!exampleroomid:example.com"); | auto r = client.room("!exampleroomid:example.com"); | ||||
| auto localEchoes = +r.localEchoes(); | auto localEchoes = +r.localEchoes(); | ||||
| REQUIRE(localEchoes.size() == 1); | REQUIRE(localEchoes.size() == 1); | ||||
| REQUIRE(localEchoes[0].txnId == "some-other-txnid"); | REQUIRE(localEchoes[0].txnId == "some-other-txnid"); | ||||
| } | |||||
| TEST_CASE("updating local notifications", "[client][sync]") | |||||
| { | |||||
| ClientModel m = makeClient( | |||||
| withRoom(makeRoom( | |||||
| withRoomId("!exampleroomid:example.com")))); | |||||
| WHEN("the receipt for the current user did not change") { | |||||
| auto resp = makeResponse( | |||||
| "Sync", | |||||
| withResponseJsonBody(addNotificationsJson) | |||||
| | withResponseDataKV("is", "incremental") | |||||
| ); | |||||
| auto [next, _] = processResponse(m, SyncResponse{resp}); | |||||
| auto room = next.roomList.rooms.at("!exampleroomid:example.com"); | |||||
| REQUIRE(room.unreadNotificationEventIds | |||||
| == immer::flex_vector<std::string>{ | |||||
| "$example:example.com", | |||||
| "$example2:example.com" | |||||
| }); | |||||
| THEN("it changed later") { | |||||
| auto resp = makeResponse( | |||||
| "Sync", | |||||
| withResponseJsonBody(removeNotificationsJson) | |||||
| | withResponseDataKV("is", "incremental") | |||||
| ); | |||||
| auto [nextNext, _] = processResponse(next, SyncResponse{resp}); | |||||
| auto room = nextNext.roomList.rooms.at("!exampleroomid:example.com"); | |||||
| REQUIRE(room.unreadNotificationEventIds | |||||
| == immer::flex_vector<std::string>{ | |||||
| "$example2:example.com" | |||||
| }); | |||||
| } | |||||
| } | |||||
| WHEN("the receipt for the current user changed") { | |||||
| auto resp = makeResponse( | |||||
| "Sync", | |||||
| withResponseJsonBody(addAndRemoveNotificationsJson) | |||||
| | withResponseDataKV("is", "incremental") | |||||
| ); | |||||
| auto [next, _] = processResponse(m, SyncResponse{resp}); | |||||
| auto room = next.roomList.rooms.at("!exampleroomid:example.com"); | |||||
| REQUIRE(room.unreadNotificationEventIds | |||||
| == immer::flex_vector<std::string>{ | |||||
| "$example2:example.com" | |||||
| }); | |||||
| } | |||||
| } | } | ||||