Files
crystall-punk-14/Content.IntegrationTests/Tests/Minds/GhostRoleTests.cs

126 lines
5.0 KiB
C#
Raw Normal View History

2023-06-18 11:33:19 -07:00
#nullable enable
2023-06-21 14:11:58 +12:00
using System.Linq;
2023-06-18 11:33:19 -07:00
using Content.Server.Ghost.Roles;
using Content.Server.Ghost.Roles.Components;
using Content.Server.Players;
2023-08-25 18:50:46 +10:00
using Content.Shared.Ghost;
using Content.Shared.Mind;
using Content.Shared.Players;
2023-06-18 11:33:19 -07:00
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
namespace Content.IntegrationTests.Tests.Minds;
[TestFixture]
public sealed class GhostRoleTests
{
[TestPrototypes]
2023-06-18 11:33:19 -07:00
private const string Prototypes = @"
- type: entity
2023-06-21 14:11:58 +12:00
id: GhostRoleTestEntity
2023-06-18 11:33:19 -07:00
components:
- type: MindContainer
- type: GhostRole
- type: GhostTakeoverAvailable
";
/// <summary>
/// This is a simple test that just checks if a player can take a ghost roll and then regain control of their
/// original entity without encountering errors.
/// </summary>
[Test]
public async Task TakeRoleAndReturn()
{
2023-08-25 02:56:51 +02:00
await using var pair = await PoolManager.GetServerClient(new PoolSettings
{
DummyTicker = false,
Connected = true
});
2023-08-25 02:56:51 +02:00
var server = pair.Server;
var client = pair.Client;
2023-06-18 11:33:19 -07:00
var entMan = server.ResolveDependency<IEntityManager>();
var sPlayerMan = server.ResolveDependency<Robust.Server.Player.IPlayerManager>();
var conHost = client.ResolveDependency<IConsoleHost>();
var mindSystem = entMan.System<SharedMindSystem>();
var session = sPlayerMan.Sessions.Single();
var originalMindId = session.ContentData()!.Mind!.Value;
2023-06-18 11:33:19 -07:00
// Spawn player entity & attach
EntityUid originalMob = default;
await server.WaitPost(() =>
{
2023-06-21 14:11:58 +12:00
originalMob = entMan.SpawnEntity(null, MapCoordinates.Nullspace);
mindSystem.TransferTo(originalMindId, originalMob, true);
2023-06-18 11:33:19 -07:00
});
// Check player got attached.
await pair.RunTicksSync(10);
2023-06-21 14:11:58 +12:00
Assert.That(session.AttachedEntity, Is.EqualTo(originalMob));
var originalMind = entMan.GetComponent<MindComponent>(originalMindId);
2023-08-07 15:29:10 +12:00
Assert.That(originalMind.OwnedEntity, Is.EqualTo(originalMob));
Assert.That(originalMind.VisitingEntity, Is.Null);
2023-06-18 11:33:19 -07:00
// Use the ghost command
conHost.ExecuteCommand("ghost");
await pair.RunTicksSync(10);
2023-08-07 15:29:10 +12:00
var ghost = session.AttachedEntity;
Assert.That(entMan.HasComponent<GhostComponent>(ghost));
Assert.That(ghost, Is.Not.EqualTo(originalMob));
Assert.That(session.ContentData()?.Mind, Is.EqualTo(originalMindId));
2023-08-07 15:29:10 +12:00
Assert.That(originalMind.OwnedEntity, Is.EqualTo(originalMob));
Assert.That(originalMind.VisitingEntity, Is.EqualTo(ghost));
2023-06-18 11:33:19 -07:00
// Spawn ghost takeover entity.
EntityUid ghostRole = default;
2023-06-21 14:11:58 +12:00
await server.WaitPost(() => ghostRole = entMan.SpawnEntity("GhostRoleTestEntity", MapCoordinates.Nullspace));
2023-06-18 11:33:19 -07:00
// Take the ghost role
await server.WaitPost(() =>
{
var id = entMan.GetComponent<GhostRoleComponent>(ghostRole).Identifier;
entMan.EntitySysManager.GetEntitySystem<GhostRoleSystem>().Takeover(session, id);
});
// Check player got attached to ghost role.
await pair.RunTicksSync(10);
var newMindId = session.ContentData()!.Mind!.Value;
var newMind = entMan.GetComponent<MindComponent>(newMindId);
Assert.That(newMindId, Is.Not.EqualTo(originalMindId));
2023-06-21 14:11:58 +12:00
Assert.That(session.AttachedEntity, Is.EqualTo(ghostRole));
2023-08-07 15:29:10 +12:00
Assert.That(newMind.OwnedEntity, Is.EqualTo(ghostRole));
Assert.That(newMind.VisitingEntity, Is.Null);
2023-08-07 15:29:10 +12:00
// Original mind should be unaffected, but the ghost will have deleted itself.
Assert.That(originalMind.OwnedEntity, Is.EqualTo(originalMob));
Assert.That(originalMind.VisitingEntity, Is.Null);
2023-08-07 15:29:10 +12:00
Assert.That(entMan.Deleted(ghost));
2023-06-18 11:33:19 -07:00
// Ghost again.
conHost.ExecuteCommand("ghost");
await pair.RunTicksSync(10);
2023-08-07 15:29:10 +12:00
var otherGhost = session.AttachedEntity;
Assert.That(entMan.HasComponent<GhostComponent>(otherGhost));
Assert.That(otherGhost, Is.Not.EqualTo(originalMob));
Assert.That(otherGhost, Is.Not.EqualTo(ghostRole));
Assert.That(session.ContentData()?.Mind, Is.EqualTo(newMindId));
2023-08-07 15:29:10 +12:00
Assert.That(newMind.OwnedEntity, Is.EqualTo(ghostRole));
Assert.That(newMind.VisitingEntity, Is.EqualTo(session.AttachedEntity));
2023-06-18 11:33:19 -07:00
// Next, control the original entity again:
await server.WaitPost(() => mindSystem.SetUserId(originalMindId, session.UserId));
await pair.RunTicksSync(10);
2023-06-21 14:11:58 +12:00
Assert.That(session.AttachedEntity, Is.EqualTo(originalMob));
2023-08-07 15:29:10 +12:00
Assert.That(originalMind.OwnedEntity, Is.EqualTo(originalMob));
Assert.That(originalMind.VisitingEntity, Is.Null);
2023-08-07 15:29:10 +12:00
// the ghost-role mind is unaffected, though the ghost will have deleted itself
Assert.That(newMind.OwnedEntity, Is.EqualTo(ghostRole));
Assert.That(newMind.VisitingEntity, Is.Null);
2023-08-07 15:29:10 +12:00
Assert.That(entMan.Deleted(otherGhost));
2023-06-18 11:33:19 -07:00
2023-08-25 02:56:51 +02:00
await pair.CleanReturnAsync();
2023-06-18 11:33:19 -07:00
}
}