using Newtonsoft.Json;
using Nostr.Client.Json;
using Nostr.Client.Messages;
using NostrRelay;

namespace BasicTests;

public class Tests
{
    private NostrEvent _testEvent = null!;

    [SetUp]
    public void Setup()
    {
        var json =
            "{\"id\":\"0552df8c6617c8919f1872ce8a0b2834f3f28100e4bf07be8b4aa262e9d59321\",\"pubkey\":\"27154fb873badf69c3ea83a0da6e65d6a150d2bf8f7320fc3314248d74645c64\",\"created_at\":1705062495,\"kind\":1,\"tags\":[[\"r\",\"https://www.coindesk.com/business/2024/01/12/bitcoin-miner-outflows-hit-six-year-highs-ahead-of-halving-sparking-mixed-signals/\"]],\"content\":\"‚Miner outflow has hit a multi-year high as tens of thousands of bitcoin (BTC), worth over $1 billion, have been sent to exchanges.‘\\n\\nhttps://www.coindesk.com/business/2024/01/12/bitcoin-miner-outflows-hit-six-year-highs-ahead-of-halving-sparking-mixed-signals/\",\"sig\":\"ba2523600bea47637bcced3b75ec446253dca184783a98ec807bf0a4fc90031152c225e7a39392bb3af23ceafc64aa67d9c10874e8ec6d2c1af268235a554d93\"}";

        _testEvent = JsonConvert.DeserializeObject<NostrEvent>(json, NostrSerializer.Settings)!;
    }

    [Test]
    public void TestNostrBuf()
    {
        var data = NostrBuf.Encode(_testEvent);
        Assert.That(data.Length, Is.EqualTo(NostrBuf.CalculateSize(_testEvent)));

        var ev = NostrBuf.Decode(data);
        Assert.That(ev.Id, Is.EqualTo(_testEvent.Id));
        Assert.That(ev.Pubkey, Is.EqualTo(_testEvent.Pubkey));
        Assert.That(ev.Sig, Is.EqualTo(_testEvent.Sig));
        Assert.That(ev.CreatedAt, Is.EqualTo(_testEvent.CreatedAt));
        Assert.That(ev.Kind, Is.EqualTo(_testEvent.Kind));
        Assert.That(ev.Tags!.Count, Is.EqualTo(_testEvent.Tags!.Count));
        for (var tx = 0; tx < ev.Tags.Count; tx++)
        {
            Assert.That(ev.Tags[tx].TagIdentifier, Is.EqualTo(_testEvent.Tags[tx].TagIdentifier));
            Assert.That(ev.Tags[tx].AdditionalData.Length, Is.EqualTo(_testEvent.Tags[tx].AdditionalData.Length));
            for (var ty = 0; ty < ev.Tags[tx].AdditionalData.Length; ty++)
            {
                Assert.That(ev.Tags[tx].AdditionalData[ty], Is.EqualTo(_testEvent.Tags[tx].AdditionalData[ty]));
            }
        }
    }
}
