Skip to content

Commit 0631c01

Browse files
committed
test(SitHelperTest): add unit tests for player sitting functionality and edge cases
1 parent bc91a3f commit 0631c01

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/**
2+
* Copyright 2025 OneLiteFeather Network
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.onelitefeather.titan.common.helper;
17+
18+
import net.minestom.server.coordinate.Pos;
19+
import net.minestom.server.coordinate.Vec;
20+
import net.minestom.server.entity.Player;
21+
import net.minestom.server.instance.Instance;
22+
import net.minestom.testing.Env;
23+
import net.minestom.testing.extension.MicrotusExtension;
24+
import net.onelitefeather.titan.common.config.AppConfig;
25+
import net.onelitefeather.titan.common.utils.Tags;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.Disabled;
28+
import org.junit.jupiter.api.DisplayName;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.ExtendWith;
31+
32+
import java.util.Collections;
33+
import java.util.UUID;
34+
35+
@ExtendWith(MicrotusExtension.class)
36+
class SitHelperTest {
37+
38+
@DisplayName("Test if player can sit at a specific location")
39+
@Test
40+
void testSitPlayer(Env env) {
41+
// Create a real instance and player
42+
Instance instance = env.createFlatInstance();
43+
Player player = env.createPlayer(instance);
44+
45+
// Set player position
46+
Pos playerPos = new Pos(0, 64, 0);
47+
player.teleport(playerPos);
48+
49+
// Create a custom AppConfig with a specific sit offset
50+
Vec sitOffset = new Vec(0, 0.3, 0);
51+
AppConfig customConfig = AppConfig.builder()
52+
.sitOffset(sitOffset)
53+
.build();
54+
55+
// Make the player sit
56+
Pos sitLocation = new Pos(0, 64, 0);
57+
SitHelper.sitPlayer(player, sitLocation, customConfig);
58+
59+
// Verify that the player is sitting
60+
Assertions.assertTrue(SitHelper.isSitting(player), "Player should be sitting");
61+
62+
// Verify that the player has the SIT_PLAYER tag with the original position
63+
Assertions.assertTrue(player.hasTag(Tags.SIT_PLAYER), "Player should have SIT_PLAYER tag");
64+
Assertions.assertEquals(playerPos, player.getTag(Tags.SIT_PLAYER), "SIT_PLAYER tag should contain the original position");
65+
66+
// Verify that the player has the SIT_ARROW tag
67+
Assertions.assertTrue(player.hasTag(Tags.SIT_ARROW), "Player should have SIT_ARROW tag");
68+
69+
// Verify that the arrow entity exists in the instance
70+
UUID arrowUuid = player.getTag(Tags.SIT_ARROW);
71+
Assertions.assertNotNull(instance.getEntityByUuid(arrowUuid), "Arrow entity should exist in the instance");
72+
}
73+
74+
@DisplayName("Test if player can be removed from sitting position")
75+
@Test
76+
void testRemovePlayer(Env env) {
77+
// Create a real instance and player
78+
Instance instance = env.createFlatInstance();
79+
Player player = env.createPlayer(instance);
80+
81+
// Set player position
82+
Pos playerPos = new Pos(0, 64, 0);
83+
player.teleport(playerPos);
84+
85+
// Create a custom AppConfig with a specific sit offset
86+
Vec sitOffset = new Vec(0, 0.3, 0);
87+
AppConfig customConfig = AppConfig.builder()
88+
.sitOffset(sitOffset)
89+
.build();
90+
91+
// Make the player sit
92+
Pos sitLocation = new Pos(0, 64, 0);
93+
SitHelper.sitPlayer(player, sitLocation, customConfig);
94+
95+
// Verify that the player is sitting
96+
Assertions.assertTrue(SitHelper.isSitting(player), "Player should be sitting");
97+
98+
// Get the arrow UUID before removing the player
99+
UUID arrowUuid = player.getTag(Tags.SIT_ARROW);
100+
101+
// Remove the player from the sitting position
102+
SitHelper.removePlayer(player);
103+
104+
// Verify that the player is no longer sitting
105+
Assertions.assertFalse(SitHelper.isSitting(player), "Player should not be sitting");
106+
107+
// Verify that the player no longer has the SIT_ARROW tag
108+
Assertions.assertFalse(player.hasTag(Tags.SIT_ARROW), "Player should not have SIT_ARROW tag");
109+
110+
// Verify that the arrow entity no longer exists in the instance
111+
Assertions.assertNull(instance.getEntityByUuid(arrowUuid), "Arrow entity should not exist in the instance");
112+
}
113+
114+
@DisplayName("Test if isSitting correctly identifies if a player is sitting")
115+
@Test
116+
void testIsSitting(Env env) {
117+
// Create a real instance and player
118+
Instance instance = env.createFlatInstance();
119+
Player player = env.createPlayer(instance);
120+
121+
// Initially, the player should not be sitting
122+
Assertions.assertFalse(SitHelper.isSitting(player), "Player should not be sitting initially");
123+
124+
// Set player position
125+
Pos playerPos = new Pos(0, 64, 0);
126+
player.teleport(playerPos);
127+
128+
// Create a custom AppConfig with a specific sit offset
129+
Vec sitOffset = new Vec(0, 0.3, 0);
130+
AppConfig customConfig = AppConfig.builder()
131+
.sitOffset(sitOffset)
132+
.build();
133+
134+
// Make the player sit
135+
Pos sitLocation = new Pos(0, 64, 0);
136+
SitHelper.sitPlayer(player, sitLocation, customConfig);
137+
138+
// Now the player should be sitting
139+
Assertions.assertTrue(SitHelper.isSitting(player), "Player should be sitting after sitPlayer");
140+
141+
// Remove the player from the sitting position
142+
SitHelper.removePlayer(player);
143+
144+
// The player should no longer be sitting
145+
Assertions.assertFalse(SitHelper.isSitting(player), "Player should not be sitting after removePlayer");
146+
}
147+
148+
@DisplayName("Test edge case: trying to sit a player who is already sitting")
149+
@Test
150+
void testSitPlayerAlreadySitting(Env env) {
151+
// Create a real instance and player
152+
Instance instance = env.createFlatInstance();
153+
Player player = env.createPlayer(instance);
154+
155+
// Set player position
156+
Pos playerPos = new Pos(0, 64, 0);
157+
player.teleport(playerPos);
158+
159+
// Create a custom AppConfig with a specific sit offset
160+
Vec sitOffset = new Vec(0, 0.3, 0);
161+
AppConfig customConfig = AppConfig.builder()
162+
.sitOffset(sitOffset)
163+
.build();
164+
165+
// Make the player sit
166+
Pos sitLocation = new Pos(0, 64, 0);
167+
SitHelper.sitPlayer(player, sitLocation, customConfig);
168+
169+
// Get the arrow UUID after the first sit
170+
UUID firstArrowUuid = player.getTag(Tags.SIT_ARROW);
171+
172+
// Try to make the player sit again at a different location
173+
Pos newSitLocation = new Pos(1, 64, 1);
174+
SitHelper.sitPlayer(player, newSitLocation, customConfig);
175+
176+
// Get the arrow UUID after the second sit
177+
UUID secondArrowUuid = player.getTag(Tags.SIT_ARROW);
178+
179+
// Verify that the player is still sitting
180+
Assertions.assertTrue(SitHelper.isSitting(player), "Player should still be sitting");
181+
182+
// Verify that a new arrow entity was created (the UUIDs should be different)
183+
Assertions.assertNotEquals(firstArrowUuid, secondArrowUuid, "A new arrow entity should have been created");
184+
185+
// Verify that the first arrow entity no longer exists in the instance
186+
Assertions.assertNull(instance.getEntityByUuid(firstArrowUuid), "First arrow entity should not exist in the instance");
187+
188+
// Verify that the second arrow entity exists in the instance
189+
Assertions.assertNotNull(instance.getEntityByUuid(secondArrowUuid), "Second arrow entity should exist in the instance");
190+
}
191+
192+
@DisplayName("Test edge case: trying to remove a player who is not sitting")
193+
@Test
194+
void testRemovePlayerNotSitting(Env env) {
195+
// Create a real instance and player
196+
Instance instance = env.createFlatInstance();
197+
Player player = env.createPlayer(instance);
198+
199+
// Verify that the player is not sitting
200+
Assertions.assertFalse(SitHelper.isSitting(player), "Player should not be sitting");
201+
202+
// Try to remove the player from the sitting position
203+
SitHelper.removePlayer(player);
204+
205+
// Verify that the player is still not sitting
206+
Assertions.assertFalse(SitHelper.isSitting(player), "Player should still not be sitting");
207+
}
208+
209+
@DisplayName("Test edge case: trying to sit a player with a null instance")
210+
@Disabled
211+
@Test
212+
void testSitPlayerNullInstance(Env env) {
213+
// Create a player without an instance
214+
Player player = env.createPlayer(null);
215+
216+
// Create a custom AppConfig with a specific sit offset
217+
Vec sitOffset = new Vec(0, 0.3, 0);
218+
AppConfig customConfig = AppConfig.builder()
219+
.sitOffset(sitOffset)
220+
.build();
221+
222+
// Try to make the player sit
223+
Pos sitLocation = new Pos(0, 64, 0);
224+
SitHelper.sitPlayer(player, sitLocation, customConfig);
225+
226+
// Verify that the player is not sitting
227+
Assertions.assertFalse(SitHelper.isSitting(player), "Player should not be sitting");
228+
}
229+
}

0 commit comments

Comments
 (0)