Page MenuHomePhorge

matrix-event-test.cpp
No OneTemporary

Size
4 KB
Referenced Files
None
Subscribers
None

matrix-event-test.cpp

/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <kazv-defs.hpp>
#include <QtTest>
#include <QSignalSpy>
#include <lager/state.hpp>
#include <testfixtures/factory.hpp>
#include <matrix-room-timeline.hpp>
#include <matrix-sdk.hpp>
#include <matrix-room-list.hpp>
#include <matrix-room.hpp>
#include <matrix-event-list.hpp>
#include <matrix-event.hpp>
#include "test-model.hpp"
#include "test-utils.hpp"
using namespace Kazv;
using namespace Kazv::Factory;
class MatrixEventTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testEdits();
void testEncryptedEdits();
};
void MatrixEventTest::testEdits()
{
auto r = makeRoom(withRoomTimeline({
makeEvent(withEventId("$0")),
makeEvent(withEventId("$1") | withEventRelationship("moe.kazv.mxc.some-rel", "$0") | withEventKV("/origin_server_ts"_json_pointer, 1720751684500)),
// this one is invalid because there is no m.new_content
makeEvent(withEventId("$2") | withEventContent(json{{"body", "first"}}) | withEventRelationship("m.replace", "$1") | withEventKV("/origin_server_ts"_json_pointer, 1720751684700)),
makeEvent(withEventId("$3") | withEventContent(json{{"m.new_content", {{"body", "second"}}}}) | withEventRelationship("m.replace", "$1") | withEventKV("/origin_server_ts"_json_pointer, 1720751684800)),
makeEvent(withEventId("$4") | withEventContent(json{{"m.new_content", {{"body", "third"}}}}) | withEventRelationship("m.replace", "$1") | withEventKV("/origin_server_ts"_json_pointer, 1720751684900)),
}));
auto model = SdkModel{makeClient(withRoom(r))};
std::unique_ptr<MatrixSdk> sdk{makeTestSdk(model)};
auto roomList = toUniquePtr(sdk->roomList());
auto room = toUniquePtr(roomList->room(QString::fromStdString(r.roomId)));
auto timeline = toUniquePtr(room->timeline());
auto event = toUniquePtr(timeline->at(3));
QCOMPARE(event->eventId(), "$1");
auto history = toUniquePtr(event->history());
// three versions including the original one
QCOMPARE(history->count(), 3);
QCOMPARE(toUniquePtr(history->at(0))->underlyingEvent(), event->underlyingEvent());
auto v1 = toUniquePtr(history->at(1));
QCOMPARE(v1->content()["body"], QStringLiteral("second"));
QCOMPARE(v1->eventId(), QStringLiteral("$3"));
QCOMPARE(v1->underlyingEvent().originServerTs(), 1720751684800);
QCOMPARE(v1->relationType(), QStringLiteral("moe.kazv.mxc.some-rel"));
auto v2 = toUniquePtr(history->at(2));
QCOMPARE(v2->content()["body"], QStringLiteral("third"));
QCOMPARE(v2->eventId(), QStringLiteral("$4"));
QCOMPARE(v2->underlyingEvent().originServerTs(), 1720751684900);
QCOMPARE(v2->relationType(), QStringLiteral("moe.kazv.mxc.some-rel"));
}
void MatrixEventTest::testEncryptedEdits()
{
auto r = makeRoom();
auto events = EventList{
makeEvent(withEventId("$0") | withEventType("m.room.encrypted")).setDecryptedJson(json{
{"room_id", r.roomId},
{"type", "m.room.message"},
{"content", {{"body", "first"}}},
}, Event::Decrypted),
makeEvent(withEventId("$1") | withEventType("m.room.encrypted") | withEventRelationship("m.replace", "$0")).setDecryptedJson(json{
{"room_id", r.roomId},
{"type", "m.room.message"},
{"content", {{"m.new_content", {{"body", "second"}}}}},
}, Event::Decrypted),
};
withRoomTimeline(events)(r);
auto model = SdkModel{makeClient(withRoom(r))};
std::unique_ptr<MatrixSdk> sdk{makeTestSdk(model)};
auto roomList = toUniquePtr(sdk->roomList());
auto room = toUniquePtr(roomList->room(QString::fromStdString(r.roomId)));
auto timeline = toUniquePtr(room->timeline());
auto event = toUniquePtr(timeline->at(1));
QCOMPARE(event->eventId(), "$0");
auto history = toUniquePtr(event->history());
// three versions including the original one
QCOMPARE(history->count(), 2);
QCOMPARE(toUniquePtr(history->at(0))->underlyingEvent(), event->underlyingEvent());
auto v1 = toUniquePtr(history->at(1));
QCOMPARE(v1->sender(), QString::fromStdString(events[1].sender()));
QCOMPARE(v1->content()["body"], QStringLiteral("second"));
QCOMPARE(v1->eventId(), QStringLiteral("$1"));
QCOMPARE(v1->relationType(), QStringLiteral());
}
QTEST_MAIN(MatrixEventTest)
#include "matrix-event-test.moc"

File Metadata

Mime Type
text/x-c
Expires
Tue, Jun 24, 6:30 AM (23 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
234726
Default Alt Text
matrix-event-test.cpp (4 KB)

Event Timeline