Changeset View
Changeset View
Standalone View
Standalone View
src/tests/client/room/read-receipt-test.cpp
| /* | /* | ||||
| * This file is part of libkazv. | * This file is part of libkazv. | ||||
| * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe> | * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe> | ||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | * SPDX-License-Identifier: AGPL-3.0-or-later | ||||
| */ | */ | ||||
| #include <libkazv-config.hpp> | #include <libkazv-config.hpp> | ||||
| #include <boost/asio.hpp> | #include <boost/asio.hpp> | ||||
| #include <catch2/catch_test_macros.hpp> | #include <catch2/catch_test_macros.hpp> | ||||
| #include <catch2/matchers/catch_matchers_range_equals.hpp> | #include <catch2/matchers/catch_matchers_range_equals.hpp> | ||||
| #include <lager/event_loop/boost_asio.hpp> | |||||
| #include <asio-promise-handler.hpp> | #include <asio-promise-handler.hpp> | ||||
| #include <room/room-model.hpp> | #include <room/room-model.hpp> | ||||
| #include <sdk-model.hpp> | |||||
| #include <client/client.hpp> | |||||
| #include <cprjobhandler.hpp> | |||||
| #include <lagerstoreeventemitter.hpp> | |||||
| #include <testfixtures/factory.hpp> | #include <testfixtures/factory.hpp> | ||||
| #include "client-test-util.hpp" | #include "client-test-util.hpp" | ||||
| using namespace Kazv; | using namespace Kazv; | ||||
| using namespace Kazv::Factory; | using namespace Kazv::Factory; | ||||
| ▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | TEST_CASE("Update a receipt for some user", "[client][room][receipt]") | ||||
| auto r = client.room(room.roomId); | auto r = client.room(room.roomId); | ||||
| auto readers1 = r.eventReaders(lager::make_constant<std::string>("$1435641916114394fHBLK:matrix.org")).make().get(); | auto readers1 = r.eventReaders(lager::make_constant<std::string>("$1435641916114394fHBLK:matrix.org")).make().get(); | ||||
| auto expected1 = immer::flex_vector<EventReader>{}; | auto expected1 = immer::flex_vector<EventReader>{}; | ||||
| REQUIRE(readers1 == expected1); | REQUIRE(readers1 == expected1); | ||||
| auto readers2 = r.eventReaders(lager::make_constant<std::string>("$123")).make().get(); | auto readers2 = r.eventReaders(lager::make_constant<std::string>("$123")).make().get(); | ||||
| auto expected2 = immer::flex_vector<EventReader>{{"@rikj:jki.re", 1796451550450}}; | auto expected2 = immer::flex_vector<EventReader>{{"@rikj:jki.re", 1796451550450}}; | ||||
| REQUIRE(readers2 == expected2); | REQUIRE(readers2 == expected2); | ||||
| } | |||||
| TEST_CASE("Posting receipts", "[client][room][receipt]") | |||||
| { | |||||
| auto r = makeRoom(); | |||||
| auto m = makeClient(withRoom(r)); | |||||
| boost::asio::io_context io; | |||||
| SingleTypePromiseInterface<EffectStatus> sgph{AsioPromiseHandler{io.get_executor()}}; | |||||
| auto jh = Kazv::CprJobHandler{io.get_executor()}; | |||||
| auto ee = Kazv::LagerStoreEventEmitter(lager::with_boost_asio_event_loop{io.get_executor()}); | |||||
| auto sdk = Kazv::makeSdk( | |||||
| SdkModel{m}, | |||||
| jh, | |||||
| ee, | |||||
| Kazv::AsioPromiseHandler{io.get_executor()}, | |||||
| zug::identity | |||||
| ); | |||||
| auto ctx = sdk.context(); | |||||
| auto postReceiptCalled = 0; | |||||
| std::string postReceiptRoomId; | |||||
| std::string postReceiptEventId; | |||||
| auto mockContext = typename Client::ContextT([&sgph, &postReceiptCalled, &postReceiptRoomId, &postReceiptEventId](const auto &action) { | |||||
| if (std::holds_alternative<PostReceiptAction>(action)) { | |||||
| ++postReceiptCalled; | |||||
| postReceiptRoomId = std::get<PostReceiptAction>(action).roomId; | |||||
| postReceiptEventId = std::get<PostReceiptAction>(action).eventId; | |||||
| return sgph.createResolved(EffectStatus(true, json::object())); | |||||
| } | |||||
| throw std::runtime_error{"unhandled action"}; | |||||
| }, sgph, lager::deps<>{}); | |||||
| auto client = Client(Client::InEventLoopTag{}, mockContext, sdk.context()); | |||||
| auto room = client.room(r.roomId); | |||||
| room.postReceipt("$1") | |||||
| .then([&io](auto) { | |||||
| io.stop(); | |||||
| }); | |||||
| io.run(); | |||||
| REQUIRE(postReceiptCalled == 1); | |||||
| REQUIRE(postReceiptRoomId == r.roomId); | |||||
| REQUIRE(postReceiptEventId == "$1"); | |||||
| } | } | ||||