aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/tests/simulation/reply_to_ping_with_pong.rs
blob: f77bf4bfc0d0052bd5e5f0538cadb12a4f87344a (plain)
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
use std::sync::Arc;

use azalea_client::{
    packet::{config::SendConfigPacketEvent, game::SendGamePacketEvent},
    test_utils::prelude::*,
};
use azalea_protocol::packets::{
    ConnectionProtocol,
    config::{
        self, ClientboundFinishConfiguration, ClientboundRegistryData, ServerboundConfigPacket,
    },
    game::{self, ServerboundGamePacket},
};
use azalea_registry::identifier::Identifier;
use bevy_ecs::observer::On;
use parking_lot::Mutex;
use simdnbt::owned::{NbtCompound, NbtTag};

#[test]
fn reply_to_ping_with_pong() {
    let _lock = init();

    let mut simulation = Simulation::new(ConnectionProtocol::Configuration);

    let reply_count = Arc::new(Mutex::new(0));
    let reply_count_clone = reply_count.clone();
    simulation
        .app
        .add_observer(move |send_config_packet: On<SendConfigPacketEvent>| {
            if send_config_packet.sent_by == simulation.entity
                && let ServerboundConfigPacket::Pong(packet) = &send_config_packet.packet
            {
                assert_eq!(packet.id, 321);
                *reply_count_clone.lock() += 1;
            }
        });

    simulation.receive_packet(config::ClientboundPing { id: 321 });
    simulation.tick();
    assert_eq!(*reply_count.lock(), 1);

    // move into game state and test ClientboundPing there

    simulation.receive_packet(ClientboundRegistryData {
        registry_id: Identifier::new("minecraft:dimension_type"),
        entries: vec![(
            Identifier::new("minecraft:overworld"),
            Some(NbtCompound::from_values(vec![
                ("height".into(), NbtTag::Int(384)),
                ("min_y".into(), NbtTag::Int(-64)),
            ])),
        )]
        .into_iter()
        .collect(),
    });

    simulation.receive_packet(ClientboundFinishConfiguration);
    simulation.tick();

    let reply_count = Arc::new(Mutex::new(0));
    let reply_count_clone = reply_count.clone();
    simulation
        .app
        .add_observer(move |send_game_packet: On<SendGamePacketEvent>| {
            if send_game_packet.sent_by == simulation.entity
                && let ServerboundGamePacket::Pong(packet) = &send_game_packet.packet
            {
                assert_eq!(packet.id, 123);
                *reply_count_clone.lock() += 1;
            }
        });

    simulation.tick();
    simulation.receive_packet(game::ClientboundPing { id: 123 });
    simulation.tick();

    assert_eq!(*reply_count.lock(), 1);
}