~ruther/NosSmooth

ref: 8dcf58cbe2cee392418cb457c78eddff0bf612b5 NosSmooth/Tests/NosSmooth.Core.Tests/Packets/InPacketSerializerTest.cs -rw-r--r-- 5.7 KiB
8dcf58cb — František Boháček chore: add DI dependency to Tests 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
//  InPacketSerializerTest.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NosCore.Packets.ServerPackets.Inventory;
using NosCore.Packets.ServerPackets.Visibility;
using NosCore.Shared.Enumerations;
using NosSmooth.Core.Packets;
using NosSmooth.Core.Packets.Converters;
using Xunit;

namespace NosSmooth.Core.Tests.Packets;

/// <summary>
/// Test class for <see cref="InPacketSerializerTest"/>.
/// </summary>
public class InPacketSerializerTest
{
    private readonly InPacketSerializer _inPacketSerializer;

    /// <summary>
    /// Initializes a new instance of the <see cref="InPacketSerializerTest"/> class.
    /// </summary>
    public InPacketSerializerTest()
    {
        var types = new List<Type>(new[]
        {
            typeof(InPacket),
            typeof(InAliveSubPacket),
            typeof(InNonPlayerSubPacket),
            typeof(InCharacterSubPacket),
            typeof(InItemSubPacket),
            typeof(InEquipmentSubPacket),
            typeof(UpgradeRareSubPacket),
            typeof(FamilySubPacket),
        });

        _inPacketSerializer = new ServiceCollection()
            .AddSingleton(
                p => new PacketSerializerProvider(types, types, p)
            )
            .AddSingleton<InPacketSerializer>()
            .BuildServiceProvider()
            .GetRequiredService<InPacketSerializer>();
    }

    /// <summary>
    /// Tests whether the serializer accepts to handle in packet.
    /// </summary>
    [Fact]
    public void AcceptsInPacket()
    {
        var shouldHandle = _inPacketSerializer.ShouldHandle(
            "in 1 dfrfgh - 1 79 2 6 2 1 0 106 2 -1.4480.4452.4468.4840.4132.-1.-1.-1.-1 100 100 0 -1 4 4 0 43 0 0 108 108 -1 - 26 0 0 0 0 99 0 0|0|0 0 0 10 80 0");
        Assert.True(shouldHandle);
    }

    /// <summary>
    /// Tests whether the serializer doesnt accept to handle non in packet.
    /// </summary>
    [Fact]
    public void DoesntAcceptNonInPacket()
    {
        var shouldHandle = _inPacketSerializer.ShouldHandle(
            "sr 5");
        Assert.False(shouldHandle);
    }

    /// <summary>
    /// Tests whether the result is successful when serializing player in packet.
    /// </summary>
    [Fact]
    public void SucceedsDeserializingPlayerIn()
    {
        var result = _inPacketSerializer.Deserialize(
            "in 1 dfrfgh - 1 79 2 6 2 1 0 106 2 -1.4480.4452.4468.4840.4132.-1.-1.-1.-1 50 98 0 -1 4 4 0 43 0 0 108 108 -1 - 26 0 0 0 0 99 0 0|0|0 0 0 10 80 0"
        );
        Assert.True(result.IsSuccess);
    }

    /// <summary>
    /// Tests whether the result is successful when serializing monster in packet.
    /// </summary>
    [Fact]
    public void SucceedsDeserializingMonsterIn()
    {
        var result = _inPacketSerializer.Deserialize(
            "in 2 334 1992 134 112 2 100 100 0 0 0 -1 1 0 -1 - 0 -1 0 0 0 0 0 0 0 0 0 0"
        );
        Assert.True(result.IsSuccess);
    }

    /// <summary>
    /// Tests whether the result of deserializing player is correct.
    /// </summary>
    [Fact]
    public void DeserializesPlayerInCorrectly()
    {
        var result = _inPacketSerializer.Deserialize(
            "in 1 dfrfgh - 55 79 2 6 2 1 0 106 2 -1.4480.4452.4468.4840.4132.-1.-1.-1.-1 50 95 0 -1 4 4 0 43 0 0 108 108 -1 - 26 0 0 0 0 99 0 0|0|0 0 0 10 80 0"
        ); // 55 is id, 50 hp, 95 mp

        Assert.True(result.IsSuccess);
        var inPacket = result.Entity;

        Assert.Equal(VisualType.Player, inPacket.VisualType);
        Assert.NotNull(inPacket.Name);
        Assert.Matches("dfrfgh", inPacket.Name);
        Assert.Equal(55, inPacket.VisualId);
        Assert.Equal(79, inPacket.PositionX);
        Assert.Equal(2, inPacket.PositionY);
        Assert.NotNull(inPacket.Direction);
        Assert.Equal(6, (byte)inPacket.Direction!);
        Assert.NotNull(inPacket.InCharacterSubPacket);
        var characterSubPacket = inPacket.InCharacterSubPacket!;
        Assert.Equal(AuthorityType.GameMaster, characterSubPacket.Authority);
        Assert.Equal(CharacterClassType.Archer, characterSubPacket.Class);
        Assert.NotNull(characterSubPacket.InAliveSubPacket);
        Assert.Equal(50, characterSubPacket.InAliveSubPacket!.Hp);
        Assert.Equal(95, characterSubPacket.InAliveSubPacket!.Mp);

        // TODO: check other things
    }

    /// <summary>
    /// Tests whether the result of deserializing monster is correct.
    /// </summary>
    [Fact]
    public void DeserializesMonsterInCorrectly()
    {
        var result = _inPacketSerializer.Deserialize(
            "in 2 334 1992 134 112 2 100 80 0 0 0 -1 1 0 -1 - 0 -1 0 0 0 0 0 0 0 0 0 0"
        );
        Assert.True(result.IsSuccess);
        var inPacket = result.Entity;
        Assert.Equal(VisualType.Npc, inPacket.VisualType);
        Assert.NotNull(inPacket.VNum);
        Assert.Equal(334, inPacket.VNum!.Value);
        Assert.Equal(1992, inPacket.VisualId);
        Assert.Equal(134, inPacket.PositionX);
        Assert.Equal(112, inPacket.PositionY);
        Assert.NotNull(inPacket.Direction);
        Assert.Equal(2, (byte)inPacket.Direction!);
        Assert.NotNull(inPacket.InNonPlayerSubPacket);
        var nonPlayerSubPacket = inPacket.InNonPlayerSubPacket!;
        Assert.NotNull(nonPlayerSubPacket.InAliveSubPacket);
        Assert.Equal(100, nonPlayerSubPacket.InAliveSubPacket!.Hp);
        Assert.Equal(80, nonPlayerSubPacket.InAliveSubPacket!.Mp);

        // TODO: check other things
    }
}
Do not follow this link