Files
purpur-fantasy-stick/src/nativeTest/java/games/dmg/fantasystick/RecipeDiscoveryNativeTest.java
T
dmg a58c192358
Release / release (push) Failing after 8m34s
feat(crafting): discover character sheet recipe
2026-09-20 15:29:21 -04:00

101 lines
5.6 KiB
Java

package games.dmg.fantasystick;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.doAnswer;
import games.dmg.fantasystick.items.CharacterSheets;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import org.bukkit.event.player.PlayerJoinEvent;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class RecipeDiscoveryNativeTest {
@TempDir Path data;
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
@Test void packagedRegistrationDiscoversOnlineAndJoinPlayersOnceWithoutMintingAndRetainsKnowledge() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
var online = new FantasyStickLifecycleNativeTest.Actor(data);
f.players.put(online.core.id, online.player);
long records = records(data);
f.plugin.setEnabled(true);
var recipe = (org.bukkit.inventory.ShapelessRecipe) f.recipes.get(CharacterSheets.RECIPE);
assertNotNull(recipe);
assertEquals(org.bukkit.inventory.recipe.CraftingBookCategory.MISC, recipe.getCategory());
assertEquals(java.util.Set.of(CharacterSheets.RECIPE), online.knownRecipes);
assertEquals(List.of(List.of(CharacterSheets.RECIPE)), online.recipeAwards);
assertEquals(records, records(data));
assertTrue(new CharacterSheets().identify(recipe.getResult()).isEmpty());
var joining = new FantasyStickLifecycleNativeTest.Actor(data);
f.players.put(joining.core.id, joining.player);
f.dispatcher.accept(new PlayerJoinEvent(joining.player, "joined"));
f.dispatcher.accept(new PlayerJoinEvent(joining.player, "joined again"));
assertEquals(List.of(List.of(CharacterSheets.RECIPE)), joining.recipeAwards);
assertEquals(records + 1, records(data));
f.plugin.setEnabled(false);
f.dispatcher.accept(new PlayerJoinEvent(joining.player, "disabled"));
assertEquals(List.of(List.of(CharacterSheets.RECIPE)), joining.recipeAwards);
assertEquals(java.util.Set.of(CharacterSheets.RECIPE), joining.knownRecipes);
f.plugin.setEnabled(true);
assertEquals(List.of(List.of(CharacterSheets.RECIPE)), online.recipeAwards);
assertEquals(List.of(List.of(CharacterSheets.RECIPE)), joining.recipeAwards);
}
}
@Test void playerFailureIsLoggedIsolatedAndRetryableButRegistrationFailureAwardsNobody() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data.resolve("player-failure"))) {
var failing = new FantasyStickLifecycleNativeTest.Actor(data.resolve("player-failure"));
var healthy = new FantasyStickLifecycleNativeTest.Actor(data.resolve("player-failure"));
var fail = new AtomicBoolean(true);
doAnswer(call -> {
if (fail.getAndSet(false)) throw new IllegalStateException("simulated discovery failure");
var requested = ((java.util.Collection<?>) call.getArgument(0)).stream()
.map(org.bukkit.NamespacedKey.class::cast).toList();
failing.recipeAwards.add(requested); failing.knownRecipes.addAll(requested); return requested.size();
}).when(failing.player).discoverRecipes(anyCollection());
f.players.put(failing.core.id, failing.player); f.players.put(healthy.core.id, healthy.player);
var logs = new java.util.ArrayList<LogRecord>();
var handler = new Handler() {
@Override public void publish(LogRecord record) { logs.add(record); }
@Override public void flush() { }
@Override public void close() { }
};
f.plugin.getLogger().addHandler(handler);
try {
f.plugin.setEnabled(true);
assertTrue(f.recipes.containsKey(CharacterSheets.RECIPE));
assertTrue(failing.knownRecipes.isEmpty());
assertEquals(java.util.Set.of(CharacterSheets.RECIPE), healthy.knownRecipes);
assertTrue(logs.stream().anyMatch(log -> log.getMessage().contains("discover character-sheet recipe")));
f.dispatcher.accept(new PlayerJoinEvent(failing.player, "retry"));
assertEquals(java.util.Set.of(CharacterSheets.RECIPE), failing.knownRecipes);
} finally { f.plugin.getLogger().removeHandler(handler); }
}
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data.resolve("registration-failure"))) {
var online = new FantasyStickLifecycleNativeTest.Actor(data.resolve("registration-failure"));
f.players.put(online.core.id, online.player); f.failRegistrationAt = "recipe";
assertThrows(IllegalStateException.class, () -> f.plugin.setEnabled(true));
assertTrue(online.knownRecipes.isEmpty());
assertTrue(online.recipeAwards.isEmpty());
assertTrue(f.recipes.isEmpty());
}
}
private static long records(Path directory) throws Exception {
var characters = directory.resolve("characters");
if (!java.nio.file.Files.isDirectory(characters)) return 0;
try (var files = java.nio.file.Files.list(characters)) {
return files.filter(file -> file.getFileName().toString().endsWith(".yml")).count();
}
}
}