diff --git a/src/nativeTest/java/games/dmg/fantasystick/FrostShardNativeTest.java b/src/nativeTest/java/games/dmg/fantasystick/FrostShardNativeTest.java index bea6020..82ec08d 100644 --- a/src/nativeTest/java/games/dmg/fantasystick/FrostShardNativeTest.java +++ b/src/nativeTest/java/games/dmg/fantasystick/FrostShardNativeTest.java @@ -674,6 +674,14 @@ class FrostShardNativeTest { } @Test void sourceExitRemovesOnlyOwnedSlowAndRestoresThePriorNativeLayer() throws Exception { + sourceExitWithNativeSlow(false); + } + + @Test void nativeTickAgingBeforeSourceExitStillRestoresOnlyTheOwnedPriorSlowLayer() throws Exception { + sourceExitWithNativeSlow(true); + } + + private void sourceExitWithNativeSlow(boolean ageBeforeExit) throws Exception { java.nio.file.Files.writeString(data.resolve("config.yml"), "frost-shard-cooldown-seconds: 0\n"); try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) { f.plugin.setEnabled(true); @@ -688,10 +696,21 @@ class FrostShardNativeTest { var target = mock(org.bukkit.entity.LivingEntity.class); when(target.getUniqueId()).thenReturn(java.util.UUID.randomUUID()); when(target.getLocation()).thenReturn(new org.bukkit.Location(a.world, 2, 65.6, 0)); + when(target.getPersistentDataContainer()).thenReturn( + new org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer( + new org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry())); var previous = new org.bukkit.potion.PotionEffect(org.bukkit.potion.PotionEffectType.SLOWNESS, 200, 1); var current = new java.util.concurrent.atomic.AtomicReference(previous); - when(target.getPotionEffect(org.bukkit.potion.PotionEffectType.SLOWNESS)).thenAnswer(call -> current.get()); - when(target.addPotionEffect(any())).thenAnswer(call -> { current.set(call.getArgument(0)); return true; }); + var installedAt = new java.util.concurrent.atomic.AtomicLong(System.nanoTime()); + when(target.getPotionEffect(org.bukkit.potion.PotionEffectType.SLOWNESS)).thenAnswer(call -> { + var effect = current.get(); if (effect == null || effect.isInfinite()) return effect; + long ticks = Math.max(0, (System.nanoTime() - installedAt.get()) / 50_000_000L); + int remaining = (int) Math.max(0, (long) effect.getDuration() - ticks); + return remaining == 0 ? null : effect.withDuration(remaining); + }); + when(target.addPotionEffect(any())).thenAnswer(call -> { + current.set(call.getArgument(0)); installedAt.set(System.nanoTime()); return true; + }); doAnswer(call -> { current.set(null); return null; }).when(target) .removePotionEffect(org.bukkit.potion.PotionEffectType.SLOWNESS); doAnswer(call -> { @@ -707,6 +726,15 @@ class FrostShardNativeTest { PartyBookNativeTest.drain(f); PartyBookNativeTest.drain(f); f.dispatcher.accept(new org.bukkit.event.entity.ProjectileHitEvent(projectile, target)); assertEquals(0, current.get().getAmplifier()); + if (ageBeforeExit) { + var applied = current.get(); Thread.sleep(200); + var aged = target.getPotionEffect(org.bukkit.potion.PotionEffectType.SLOWNESS); assertNotNull(aged); + assertTrue(aged.getDuration() < applied.getDuration(), + "The native test layer must report remaining duration after real ticks, not a frozen application snapshot"); + assertEquals(applied.getType(), aged.getType()); assertEquals(applied.getAmplifier(), aged.getAmplifier()); + assertEquals(applied.isAmbient(), aged.isAmbient()); assertEquals(applied.hasParticles(), aged.hasParticles()); + assertEquals(applied.hasIcon(), aged.hasIcon()); + } a.core.inventory.setItemInOffHand(null); f.changed(a); PartyBookNativeTest.drain(f); f.awaitCharacterAndReceipt(a); ((games.dmg.fantasystick.ability.AbilityEngine) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "abilityEngine")).sweep(); assertNotNull(current.get()); assertEquals(1, current.get().getAmplifier());