ModernUO/Projects/UOContent.Tests/Tests/Mobiles/AI/PetFriendMembershipTests.cs
Kamron Batman 00688a04cf
fix(ai): guard zero-int detection cooldown and duplicate pet friends (#2667)
Supersedes #2658 (rebased onto main; edits by maintainers were not enabled on the original branch). Credit to @MithrilHammer for both fixes and the regression tests; their three commits are kept as authored.

Addresses two items from #2627:

- The detect-hidden cooldown clamps its divisor to at least one. A creature whose effective intelligence reaches zero now uses the existing capped cooldown instead of throwing.
- Pet friends are unique. `AddPetFriend` ignores an existing friend, so one removal revokes the membership.

On top of that, `AddPetFriend` and `RemovePetFriend` now go through the generated `AddToFriends` / `RemoveFromFriends`, which mark the creature dirty. `Friends` goes back to null when the last friend is removed, and removing a non-friend no longer touches the list.

Duplicates already stored in saves are not migrated.

## Tests

`DetectHiddenTimerTests` (7 cases) drives the real AI timer and checks cooldown bounds and retry timing. `PetFriendMembershipTests` (3 cases) covers duplicate adds, removal, and re-adding without disturbing other friends. `UOContent.Tests`: 1158 passed, 2 skipped, 0 failed on Windows.
2026-09-23 10:42:38 -07:00

82 lines
1.9 KiB
C#

using Server;
using Server.Mobiles;
using Xunit;
namespace UOContent.Tests.Mobiles.AI;
[Collection("Sequential UOContent Tests")]
public class PetFriendMembershipTests
{
[Fact]
public void AddPetFriend_Twice_StoresOneMembership()
{
var pet = new PetTestStub();
var friend = new PlayerMobile();
try
{
pet.AddPetFriend(friend);
pet.AddPetFriend(friend);
Assert.Single(pet.Friends);
Assert.True(pet.IsPetFriend(friend));
}
finally
{
pet.Delete();
friend.Delete();
}
}
[Fact]
public void RemovePetFriend_AfterRepeatedAdd_RevokesMembership()
{
var pet = new PetTestStub();
var friend = new PlayerMobile();
try
{
pet.AddPetFriend(friend);
pet.AddPetFriend(friend);
pet.RemovePetFriend(friend);
Assert.False(pet.IsPetFriend(friend));
Assert.Null(pet.Friends);
}
finally
{
pet.Delete();
friend.Delete();
}
}
[Fact]
public void RemoveAndReadd_LeavesOtherFriendsUnchanged()
{
var pet = new PetTestStub();
var first = new PlayerMobile();
var second = new PlayerMobile();
try
{
pet.AddPetFriend(first);
pet.AddPetFriend(second);
pet.RemovePetFriend(first);
Assert.False(pet.IsPetFriend(first));
Assert.True(pet.IsPetFriend(second));
Assert.Single(pet.Friends);
pet.AddPetFriend(first);
Assert.True(pet.IsPetFriend(first));
Assert.True(pet.IsPetFriend(second));
Assert.Equal(2, pet.Friends.Count);
}
finally
{
pet.Delete();
first.Delete();
second.Delete();
}
}
}