#include "tray_bridge_model.h"

#include <QtTest/QtTest>

class TrayBridgeModelTest : public QObject {
    Q_OBJECT

private slots:
    void parsesDirectSnapshotLine();
    void parsesSnapshotEnvelope();
    void parsesErrorEnvelope();
    void parsesStateAndBrowseUrlEvents();
    void groupsPeersLikeWindowsTray();
};

void TrayBridgeModelTest::parsesDirectSnapshotLine()
{
    const BridgeStreamEvent event = parseBridgeStreamLine(
        QByteArrayLiteral("{\"backendState\":\"Running\",\"stateLabel\":\"已连接\",\"self\":{\"name\":\"alpha\"}}"));

    QCOMPARE(event.kind, BridgeStreamEvent::Snapshot);
    QCOMPARE(event.snapshot.value(QStringLiteral("backendState")).toString(), QStringLiteral("Running"));
    QCOMPARE(event.snapshot.value(QStringLiteral("stateLabel")).toString(), QStringLiteral("已连接"));
}

void TrayBridgeModelTest::parsesSnapshotEnvelope()
{
    const BridgeStreamEvent event = parseBridgeStreamLine(QByteArrayLiteral(
        "{\"type\":\"snapshot\",\"snapshot\":{\"backendState\":\"Starting\",\"prefs\":{\"wantRunning\":true}}}"));

    QCOMPARE(event.kind, BridgeStreamEvent::Snapshot);
    QCOMPARE(event.snapshot.value(QStringLiteral("backendState")).toString(), QStringLiteral("Starting"));
    QVERIFY(event.snapshot.value(QStringLiteral("prefs")).isObject());
}

void TrayBridgeModelTest::parsesErrorEnvelope()
{
    const BridgeStreamEvent event =
        parseBridgeStreamLine(QByteArrayLiteral("{\"type\":\"error\",\"message\":\"bridge failed\"}"));

    QCOMPARE(event.kind, BridgeStreamEvent::Error);
    QCOMPARE(event.errorMessage, QStringLiteral("bridge failed"));
}

void TrayBridgeModelTest::parsesStateAndBrowseUrlEvents()
{
    const BridgeStreamEvent stateEvent = parseBridgeStreamLine(
        QByteArrayLiteral("{\"type\":\"state\",\"state\":\"Running\",\"stateLabel\":\"已连接\"}"));
    QCOMPARE(stateEvent.kind, BridgeStreamEvent::State);
    QCOMPARE(stateEvent.stringPayload, QStringLiteral("Running"));

    const BridgeStreamEvent urlEvent = parseBridgeStreamLine(
        QByteArrayLiteral("{\"type\":\"browse-url\",\"url\":\"https://login.example.invalid/flow\"}"));
    QCOMPARE(urlEvent.kind, BridgeStreamEvent::BrowseURL);
    QCOMPARE(urlEvent.stringPayload, QStringLiteral("https://login.example.invalid/flow"));
}

void TrayBridgeModelTest::groupsPeersLikeWindowsTray()
{
    QJsonObject snapshot{
        {QStringLiteral("accountLabel"), QStringLiteral("alice@example.com")},
        {QStringLiteral("peers"),
         QJsonArray{
             QJsonObject{{QStringLiteral("name"), QStringLiteral("laptop")},
                         {QStringLiteral("ownerLabel"), QStringLiteral("alice@example.com")},
                         {QStringLiteral("ip"), QStringLiteral("100.64.0.2")}},
             QJsonObject{{QStringLiteral("name"), QStringLiteral("shared-box")},
                         {QStringLiteral("ownerLabel"), QStringLiteral("bob@example.com")},
                         {QStringLiteral("shared"), true},
                         {QStringLiteral("ip"), QStringLiteral("100.64.0.3")}},
             QJsonObject{{QStringLiteral("name"), QStringLiteral("tag-node")},
                         {QStringLiteral("tagged"), true},
                         {QStringLiteral("ip"), QStringLiteral("100.64.0.4")}},
         }},
    };

    const QList<PeerMenuSection> sections = buildPeerMenuSections(snapshot);

    QCOMPARE(sections.size(), 3);
    QCOMPARE(sections.at(0).title, QStringLiteral("我的其他设备"));
    QCOMPARE(sections.at(0).peers.at(0).toObject().value(QStringLiteral("name")).toString(), QStringLiteral("laptop"));
    QCOMPARE(sections.at(1).title, QStringLiteral("bob@example.com"));
    QCOMPARE(sections.at(2).title, QStringLiteral("标签节点"));
}

QTEST_MAIN(TrayBridgeModelTest)

#include "tray_bridge_model_test.moc"
