Add interaction tests (#15251)

This commit is contained in:
Leon Friedrich
2023-04-15 07:41:25 +12:00
committed by GitHub
parent ffe946729f
commit 489660a6bb
36 changed files with 2354 additions and 32 deletions

View File

@@ -92,6 +92,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
Assert.That(interactUsing);
});
testInteractionSystem.ClearHandlers();
await pairTracker.CleanReturnAsync();
}
@@ -154,6 +155,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
Assert.That(interactUsing, Is.False);
});
testInteractionSystem.ClearHandlers();
await pairTracker.CleanReturnAsync();
}
@@ -214,6 +216,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
Assert.That(interactUsing);
});
testInteractionSystem.ClearHandlers();
await pairTracker.CleanReturnAsync();
}
@@ -275,6 +278,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
Assert.That(interactUsing, Is.False);
});
testInteractionSystem.ClearHandlers();
await pairTracker.CleanReturnAsync();
}
@@ -352,6 +356,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
Assert.That(interactUsing, Is.True);
});
testInteractionSystem.ClearHandlers();
await pairTracker.CleanReturnAsync();
}
@@ -367,6 +372,12 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
SubscribeLocalEvent<InteractUsingEvent>((e) => InteractUsingEvent?.Invoke(e));
SubscribeLocalEvent<InteractHandEvent>((e) => InteractHandEvent?.Invoke(e));
}
public void ClearHandlers()
{
InteractUsingEvent = null;
InteractHandEvent = null;
}
}
}

View File

@@ -0,0 +1,43 @@
namespace Content.IntegrationTests.Tests.Interaction;
// This partial class contains various constant prototype IDs common to interaction tests.
// Should make it easier to mass-change hard coded strings if prototypes get renamed.
public abstract partial class InteractionTest
{
protected const string PlayerEntity = "AdminObserver";
// Tiles
protected const string Floor = "FloorSteel";
protected const string FloorItem = "FloorTileItemSteel";
protected const string Plating = "Plating";
protected const string Lattice = "Lattice";
// Tools/steps
protected const string Wrench = "Wrench";
protected const string Screw = "Screwdriver";
protected const string Weld = "WelderExperimental";
protected const string Pry = "Crowbar";
protected const string Cut = "Wirecutter";
// Materials/stacks
protected const string Steel = "Steel";
protected const string Glass = "Glass";
protected const string RGlass = "ReinforcedGlass";
protected const string Plastic = "Plastic";
protected const string Cable = "Cable";
protected const string Rod = "MetalRod";
// Parts
protected const string Bin1 = "MatterBinStockPart";
protected const string Bin4 = "BluespaceMatterBinStockPart";
protected const string Cap1 = "CapacitorStockPart";
protected const string Cap4 = "QuadraticCapacitorStockPart";
protected const string Manipulator1 = "MicroManipulatorStockPart";
protected const string Manipulator4 = "FemtoManipulatorStockPart";
protected const string Laser1 = "MicroLaserStockPart";
protected const string Laser2 = "QuadUltraMicroLaserStockPart";
protected const string Battery1 = "PowerCellSmall";
protected const string Battery4 = "PowerCellHyper";
}

View File

@@ -0,0 +1,126 @@
#nullable enable
using System.Threading.Tasks;
using Content.Shared.Stacks;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
namespace Content.IntegrationTests.Tests.Interaction;
public abstract partial class InteractionTest
{
/// <summary>
/// Utility class for working with prototypes ids that may refer to stacks or entities.
/// </summary>
/// <remarks>
/// Intended to make tests easier by removing ambiguity around "SheetSteel1", "SheetSteel", and "Steel". All three
/// should be treated identically by interaction tests.
/// </remarks>
protected sealed class EntitySpecifier
{
/// <summary>
/// Either the stack or entity prototype for this entity. Stack prototypes take priority.
/// </summary>
public string Prototype;
/// <summary>
/// The quantity. If the entity has a stack component, this is the total stack quantity.
/// Otherwise this is the number of entities.
/// </summary>
/// <remarks>
/// If used for spawning and this number is larger than the max stack size, only a single stack will be spawned.
/// </remarks>
public int Quantity;
/// <summary>
/// If true, a check has been performed to see if the prototype ia an entity prototype with a stack component,
/// in which case the specifier was converted into a stack-specifier
/// </summary>
public bool Converted;
public EntitySpecifier(string prototype, int quantity, bool converted = false)
{
Assert.That(quantity > 0);
Prototype = prototype;
Quantity = quantity;
Converted = converted;
}
public static implicit operator EntitySpecifier(string prototype)
=> new(prototype, 1);
public static implicit operator EntitySpecifier((string, int) tuple)
=> new(tuple.Item1, tuple.Item2);
/// <summary>
/// Convert applicable entity prototypes into stack prototypes.
/// </summary>
public void ConvertToStack(IPrototypeManager protoMan, IComponentFactory factory)
{
if (Converted)
return;
Converted = true;
if (protoMan.HasIndex<StackPrototype>(Prototype))
return;
if (!protoMan.TryIndex<EntityPrototype>(Prototype, out var entProto))
{
Assert.Fail($"Unknown prototype: {Prototype}");
return;
}
if (entProto.TryGetComponent<StackComponent>(factory.GetComponentName(typeof(StackComponent)),
out var stackComp))
{
Prototype = stackComp.StackTypeId;
}
}
}
protected async Task<EntityUid> SpawnEntity(EntitySpecifier spec, EntityCoordinates coords)
{
EntityUid uid = default!;
if (ProtoMan.TryIndex<StackPrototype>(spec.Prototype, out var stackProto))
{
await Server.WaitPost(() =>
{
uid = SEntMan.SpawnEntity(stackProto.Spawn, coords);
Stack.SetCount(uid, spec.Quantity);
});
return uid;
}
if (!ProtoMan.TryIndex<EntityPrototype>(spec.Prototype, out var entProto))
{
Assert.Fail($"Unkown prototype: {spec.Prototype}");
return default;
}
if (entProto.TryGetComponent<StackComponent>(Factory.GetComponentName(typeof(StackComponent)),
out var stackComp))
{
return await SpawnEntity((stackComp.StackTypeId, spec.Quantity), coords);
}
Assert.That(spec.Quantity, Is.EqualTo(1), "SpawnEntity only supports returning a singular entity");
await Server.WaitPost(() => uid = SEntMan.SpawnEntity(spec.Prototype, coords));;
return uid;
}
/// <summary>
/// Convert an entity-uid to a matching entity specifier. Usefull when doing entity lookups & checking that the
/// right quantity of entities/materials werre produced.
/// </summary>
protected EntitySpecifier ToEntitySpecifier(EntityUid uid)
{
if (SEntMan.TryGetComponent(uid, out StackComponent? stack))
return new EntitySpecifier(stack.StackTypeId, stack.Count) {Converted = true};
var meta = SEntMan.GetComponent<MetaDataComponent>(uid);
Assert.NotNull(meta.EntityPrototype);
return new (meta.EntityPrototype.ID, 1) { Converted = true };
}
}

View File

@@ -0,0 +1,160 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Stacks;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.IntegrationTests.Tests.Interaction;
public abstract partial class InteractionTest
{
/// <summary>
/// Data structure for representing a collection of <see cref="EntitySpecifier"/>s.
/// </summary>
protected sealed class EntitySpecifierCollection
{
public Dictionary<string, int> Entities = new();
/// <summary>
/// If true, a check has been performed to see if the prototypes correspond to entity prototypes with a stack
/// component, in which case the specifier was converted into a stack-specifier
/// </summary>
public bool Converted;
public EntitySpecifierCollection()
{
Converted = true;
}
public EntitySpecifierCollection(IEnumerable<EntitySpecifier> ents)
{
Converted = true;
foreach (var ent in ents)
{
Add(ent);
}
}
public static implicit operator EntitySpecifierCollection(string prototype)
{
var result = new EntitySpecifierCollection();
result.Add(prototype, 1);
return result;
}
public static implicit operator EntitySpecifierCollection((string, int) tuple)
{
var result = new EntitySpecifierCollection();
result.Add(tuple.Item1, tuple.Item2);
return result;
}
public void Remove(EntitySpecifier spec)
=> Add(new EntitySpecifier(spec.Prototype, -spec.Quantity, spec.Converted));
public void Add(EntitySpecifier spec)
=> Add(spec.Prototype, spec.Quantity, spec.Converted);
public void Add(string id, int quantity, bool converted = false)
{
Converted &= converted;
if (!Entities.TryGetValue(id, out var existing))
{
if (quantity != 0)
Entities.Add(id, quantity);
return;
}
var newQuantity = quantity + existing;
if (newQuantity == 0)
Entities.Remove(id);
else
Entities[id] = newQuantity;
}
public void Add(EntitySpecifierCollection collection)
{
var converted = Converted && collection.Converted;
foreach (var (id, quantity) in collection.Entities)
{
Add(id, quantity);
}
Converted = converted;
}
public void Remove(EntitySpecifierCollection collection)
{
var converted = Converted && collection.Converted;
foreach (var (id, quantity) in collection.Entities)
{
Add(id, -quantity);
}
Converted = converted;
}
public EntitySpecifierCollection Clone()
{
return new EntitySpecifierCollection()
{
Entities = Entities.ShallowClone(),
Converted = Converted
};
}
/// <summary>
/// Convert applicable entity prototypes into stack prototypes.
/// </summary>
public void ConvertToStacks(IPrototypeManager protoMan, IComponentFactory factory)
{
if (Converted)
return;
HashSet<string> toRemove = new();
List<(string, int)> toAdd = new();
foreach (var (id, quantity) in Entities)
{
if (protoMan.HasIndex<StackPrototype>(id))
continue;
if (!protoMan.TryIndex<EntityPrototype>(id, out var entProto))
{
Assert.Fail($"Unknown prototype: {id}");
continue;
}
if (!entProto.TryGetComponent<StackComponent>(factory.GetComponentName(typeof(StackComponent)),
out var stackComp))
{
continue;
}
toRemove.Add(id);
toAdd.Add((stackComp.StackTypeId, quantity));
}
foreach (var id in toRemove)
{
Entities.Remove(id);
}
foreach (var (id, quantity) in toAdd)
{
Add(id, quantity);
}
Converted = true;
}
}
protected EntitySpecifierCollection ToEntityCollection(IEnumerable<EntityUid> entities)
{
var collection = new EntitySpecifierCollection(entities.Select(uid => ToEntitySpecifier(uid)));
Assert.That(collection.Converted);
return collection;
}
}

View File

@@ -0,0 +1,614 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Client.Construction;
using Content.Server.Construction.Components;
using Content.Server.Tools.Components;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Item;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Maths;
namespace Content.IntegrationTests.Tests.Interaction;
// This partial class defines various methods that are useful for performing & validating interactions
public abstract partial class InteractionTest
{
/// <summary>
/// Begin constructing an entity.
/// </summary>
protected async Task StartConstruction(string prototype, bool shouldSucceed = true)
{
var proto = ProtoMan.Index<ConstructionPrototype>(prototype);
Assert.That(proto.Type, Is.EqualTo(ConstructionType.Structure));
await Client.WaitPost(() =>
{
Assert.That(CConSys.TrySpawnGhost(proto, TargetCoords, Direction.South, out Target),
Is.EqualTo(shouldSucceed));
if (!shouldSucceed)
return;
var comp = CEntMan.GetComponent<ConstructionGhostComponent>(Target!.Value);
ConstructionGhostId = comp.GhostId;
});
await RunTicks(1);
}
/// <summary>
/// Craft an item.
/// </summary>
protected async Task CraftItem(string prototype, bool shouldSucceed = true)
{
Assert.That(ProtoMan.Index<ConstructionPrototype>(prototype).Type, Is.EqualTo(ConstructionType.Item));
// Please someone purge async construction code
Task<bool> task =default!;
await Server.WaitPost(() => task = SConstruction.TryStartItemConstruction(prototype, Player));
Task? tickTask = null;
while (!task.IsCompleted)
{
tickTask = PoolManager.RunTicksSync(PairTracker.Pair, 1);
await Task.WhenAny(task, tickTask);
}
if (tickTask != null)
await tickTask;
#pragma warning disable RA0004
Assert.That(task.Result, Is.EqualTo(shouldSucceed));
#pragma warning restore RA0004
await RunTicks(5);
}
/// <summary>
/// Spawn an entity entity and set it as the target.
/// </summary>
protected async Task SpawnTarget(string prototype)
{
await Server.WaitPost(() =>
{
Target = SEntMan.SpawnEntity(prototype, TargetCoords);
});
await RunTicks(5);
AssertPrototype(prototype);
}
/// <summary>
/// Spawn an entity in preparation for deconstruction
/// </summary>
protected async Task StartDeconstruction(string prototype)
{
await SpawnTarget(prototype);
Assert.That(SEntMan.TryGetComponent(Target, out ConstructionComponent? comp));
await Server.WaitPost(() => SConstruction.SetPathfindingTarget(Target!.Value, comp!.DeconstructionNode, comp));
await RunTicks(5);
}
/// <summary>
/// Drops and deletes the currently held entity.
/// </summary>
protected async Task DeleteHeldEntity()
{
if (Hands.ActiveHandEntity is {} held)
{
await Server.WaitPost(() =>
{
Assert.That(HandSys.TryDrop(Player, null, false, true, Hands));
SEntMan.DeleteEntity(held);
Logger.Debug($"Deleting held entity");
});
}
await RunTicks(1);
Assert.That(Hands.ActiveHandEntity == null);
}
/// <summary>
/// Place an entity prototype into the players hand. Deletes any currently held entity.
/// </summary>
/// <remarks>
/// Automatically enables welders.
/// </remarks>
protected async Task<EntityUid?> PlaceInHands(string? id, int quantity = 1, bool enableWelder = true)
=> await PlaceInHands(id == null ? null : (id, quantity), enableWelder);
/// <summary>
/// Place an entity prototype into the players hand. Deletes any currently held entity.
/// </summary>
/// <remarks>
/// Automatically enables welders.
/// </remarks>
protected async Task<EntityUid?> PlaceInHands(EntitySpecifier? entity, bool enableWelder = true)
{
if (Hands.ActiveHand == null)
{
Assert.Fail("No active hand");
return default;
}
await DeleteHeldEntity();
if (entity == null)
{
await RunTicks(1);
Assert.That(Hands.ActiveHandEntity == null);
return null;
}
// spawn and pick up the new item
EntityUid item = await SpawnEntity(entity, PlayerCoords);
WelderComponent? welder = null;
await Server.WaitPost(() =>
{
Assert.That(HandSys.TryPickup(Player, item, Hands.ActiveHand, false, false, false, Hands));
// turn on welders
if (enableWelder && SEntMan.TryGetComponent(item, out welder) && !welder.Lit)
Assert.That(ToolSys.TryTurnWelderOn(item, Player, welder));
});
await RunTicks(1);
Assert.That(Hands.ActiveHandEntity, Is.EqualTo(item));
if (enableWelder && welder != null)
Assert.That(welder.Lit);
return item;
}
/// <summary>
/// Pick up an entity. Defaults to just deleting the previously held entity.
/// </summary>
protected async Task Pickup(EntityUid? uid = null, bool deleteHeld = true)
{
uid ??= Target;
if (Hands.ActiveHand == null)
{
Assert.Fail("No active hand");
return;
}
if (deleteHeld)
await DeleteHeldEntity();
if (!SEntMan.TryGetComponent(uid, out ItemComponent? item))
{
Assert.Fail($"Entity {uid} is not an item");
return;
}
await Server.WaitPost(() =>
{
Assert.That(HandSys.TryPickup(Player, uid!.Value, Hands.ActiveHand, false, false, false, Hands, item));
});
await RunTicks(1);
Assert.That(Hands.ActiveHandEntity, Is.EqualTo(uid));
}
/// <summary>
/// Drops the currently held entity.
/// </summary>
protected async Task Drop()
{
if (Hands.ActiveHandEntity == null)
{
Assert.Fail("Not holding any entity to drop");
return;
}
await Server.WaitPost(() =>
{
Assert.That(HandSys.TryDrop(Player, handsComp: Hands));
});
await RunTicks(1);
Assert.IsNull(Hands.ActiveHandEntity);
}
/// <summary>
/// Use the currently held entity.
/// </summary>
protected async Task UseInHand()
{
if (Hands.ActiveHandEntity is not {} target)
{
Assert.Fail("Not holding any entity");
return;
}
await Server.WaitPost(() =>
{
InteractSys.UserInteraction(Player, SEntMan.GetComponent<TransformComponent>(target).Coordinates, target);
});
}
/// <summary>
/// Place an entity prototype into the players hand and interact with the given entity (or target position)
/// </summary>
protected async Task Interact(string? id, int quantity = 1, bool shouldSucceed = true, bool awaitDoAfters = true)
=> await Interact(id == null ? null : (id, quantity), shouldSucceed, awaitDoAfters);
/// <summary>
/// Place an entity prototype into the players hand and interact with the given entity (or target position)
/// </summary>
protected async Task Interact(EntitySpecifier? entity, bool shouldSucceed = true, bool awaitDoAfters = true)
{
// For every interaction, we will also examine the entity, just in case this breaks something, somehow.
// (e.g., servers attempt to assemble construction examine hints).
if (Target != null)
{
await Client.WaitPost(() => ExamineSys.DoExamine(Target.Value));
}
await PlaceInHands(entity);
if (Target == null || !Target.Value.IsClientSide())
{
await Server.WaitPost(() => InteractSys.UserInteraction(Player, TargetCoords, Target));
await RunTicks(1);
}
else
{
// The entity is client-side, so attempt to start construction
var ghost = CEntMan.GetComponent<ConstructionGhostComponent>(Target.Value);
await Client.WaitPost(() => CConSys.TryStartConstruction(ghost.GhostId));
await RunTicks(5);
}
if (awaitDoAfters)
await AwaitDoAfters(shouldSucceed);
await CheckTargetChange(shouldSucceed && awaitDoAfters);
}
/// <summary>
/// Wait for any currently active DoAfters to finish.
/// </summary>
protected async Task AwaitDoAfters(bool shouldSucceed = true, int maxExpected = 1)
{
if (!ActiveDoAfters.Any())
return;
// Generally expect interactions to only start one DoAfter.
Assert.That(ActiveDoAfters.Count(), Is.LessThanOrEqualTo(maxExpected));
// wait out the DoAfters.
var doAfters = ActiveDoAfters.ToList();
while (ActiveDoAfters.Any())
{
await RunTicks(10);
}
if (!shouldSucceed)
return;
foreach (var doAfter in doAfters)
{
Assert.That(!doAfter.Cancelled);
}
}
/// <summary>
/// Cancel any currently active DoAfters. Default arguments are such that it also checks that there is at least one
/// active DoAfter to cancel.
/// </summary>
protected async Task CancelDoAfters(int minExpected = 1, int maxExpected = 1)
{
Assert.That(ActiveDoAfters.Count(), Is.GreaterThanOrEqualTo(minExpected));
Assert.That(ActiveDoAfters.Count(), Is.LessThanOrEqualTo(maxExpected));
if (!ActiveDoAfters.Any())
return;
// Cancel all the do-afters
var doAfters = ActiveDoAfters.ToList();
await Server.WaitPost(() =>
{
foreach (var doAfter in doAfters)
{
DoAfterSys.Cancel(Player, doAfter.Index, DoAfters);
}
});
await RunTicks(1);
foreach (var doAfter in doAfters)
{
Assert.That(doAfter.Cancelled);
}
Assert.That(ActiveDoAfters.Count(), Is.EqualTo(0));
}
/// <summary>
/// Check if the test's target entity has changed. E.g., construction interactions will swap out entities while
/// a structure is being built.
/// </summary>
protected async Task CheckTargetChange(bool shouldSucceed)
{
EntityUid newTarget = default;
if (Target == null)
return;
var target = Target.Value;
await RunTicks(5);
if (target.IsClientSide())
{
Assert.That(CEntMan.Deleted(target), Is.EqualTo(shouldSucceed),
$"Construction ghost was {(shouldSucceed ? "not deleted" : "deleted")}.");
if (shouldSucceed)
{
Assert.That(CTestSystem.Ghosts.TryGetValue(ConstructionGhostId, out newTarget),
$"Failed to get construction entity from ghost Id");
await Client.WaitPost(() => Logger.Debug($"Construction ghost {ConstructionGhostId} became entity {newTarget}"));
Target = newTarget;
}
}
if (STestSystem.EntChanges.TryGetValue(Target.Value, out newTarget))
{
await Server.WaitPost(
() => Logger.Debug($"Construction entity {Target.Value} changed to {newTarget}"));
Target = newTarget;
}
if (Target != target)
await CheckTargetChange(shouldSucceed);
}
/// <summary>
/// Variant of <see cref="InteractUsing"/> that performs several interactions using different entities.
/// </summary>
protected async Task Interact(params EntitySpecifier?[] specifiers)
{
foreach (var spec in specifiers)
{
await Interact(spec);
}
}
#region Asserts
protected void AssertPrototype(string? prototype)
{
var meta = Comp<MetaDataComponent>();
Assert.That(meta.EntityPrototype?.ID, Is.EqualTo(prototype));
}
protected void AssertAnchored(bool anchored = true)
{
var sXform = SEntMan.GetComponent<TransformComponent>(Target!.Value);
var cXform = CEntMan.GetComponent<TransformComponent>(Target.Value);
Assert.That(sXform.Anchored, Is.EqualTo(anchored));
Assert.That(cXform.Anchored, Is.EqualTo(anchored));
}
protected void AssertDeleted(bool deleted = true)
{
Assert.That(SEntMan.Deleted(Target), Is.EqualTo(deleted));
Assert.That(CEntMan.Deleted(Target), Is.EqualTo(deleted));
}
/// <summary>
/// Assert whether or not the target has the given component.
/// </summary>
protected void AssertComp<T>(bool hasComp = true)
{
Assert.That(SEntMan.HasComponent<T>(Target), Is.EqualTo(hasComp));
}
/// <summary>
/// Check that the tile at the target position matches some prototype.
/// </summary>
protected async Task AssertTile(string? proto, EntityCoordinates? coords = null)
{
var targetTile = proto == null
? Tile.Empty
: new Tile(TileMan[proto].TileId);
Tile tile = Tile.Empty;
var pos = (coords ?? TargetCoords).ToMap(SEntMan, Transform);
await Server.WaitPost(() =>
{
if (MapMan.TryFindGridAt(pos, out var grid))
tile = grid.GetTileRef(coords ?? TargetCoords).Tile;
});
Assert.That(tile.TypeId, Is.EqualTo(targetTile.TypeId));
}
#endregion
#region Entity lookups
/// <summary>
/// Returns entities in an area around the target. Ignores the map, grid, player, target, and contained entities.
/// </summary>
protected async Task<HashSet<EntityUid>> DoEntityLookup(LookupFlags flags = LookupFlags.Uncontained)
{
var lookup = SEntMan.System<EntityLookupSystem>();
HashSet<EntityUid> entities = default!;
await Server.WaitPost(() =>
{
// Get all entities left behind by deconstruction
entities = lookup.GetEntitiesIntersecting(MapId, Box2.CentredAroundZero((10, 10)), flags);
var xformQuery = SEntMan.GetEntityQuery<TransformComponent>();
HashSet<EntityUid> toRemove = new();
foreach (var ent in entities)
{
var transform = xformQuery.GetComponent(ent);
if (ent == transform.MapUid
|| ent == transform.GridUid
|| ent == Player
|| ent == Target)
{
toRemove.Add(ent);
}
}
entities.ExceptWith(toRemove);
});
return entities;
}
/// <summary>
/// Performs an entity lookup and asserts that only the listed entities exist and that they are all present.
/// Ignores the grid, map, player, target and contained entities.
/// </summary>
protected async Task AssertEntityLookup(params EntitySpecifier[] entities)
{
var collection = new EntitySpecifierCollection(entities);
await AssertEntityLookup(collection);
}
/// <summary>
/// Performs an entity lookup and asserts that only the listed entities exist and that they are all present.
/// Ignores the grid, map, player, target and contained entities.
/// </summary>
protected async Task AssertEntityLookup(
EntitySpecifierCollection collection,
bool failOnMissing = true,
bool failOnExcess = true,
LookupFlags flags = LookupFlags.Uncontained)
{
var expected = collection.Clone();
var entities = await DoEntityLookup(flags);
var found = ToEntityCollection(entities);
expected.Remove(found);
expected.ConvertToStacks(ProtoMan, Factory);
if (expected.Entities.Count == 0)
return;
Assert.Multiple(() =>
{
foreach (var (proto, quantity) in expected.Entities)
{
if (quantity < 0 && failOnExcess)
Assert.Fail($"Unexpected entity/stack: {proto}, quantity: {-quantity}");
if (quantity > 0 && failOnMissing)
Assert.Fail($"Missing entity/stack: {proto}, quantity: {quantity}");
if (quantity == 0)
throw new Exception("Error in entity collection math.");
}
});
}
/// <summary>
/// Performs an entity lookup and attempts to find an entity matching the given entity specifier.
/// </summary>
/// <remarks>
/// This is used to check that an item-crafting attempt was successful. Ideally crafting items would just return the
/// entity or raise an event or something.
/// </remarks>
protected async Task<EntityUid> FindEntity(
EntitySpecifier spec,
LookupFlags flags = LookupFlags.Uncontained | LookupFlags.Contained,
bool shouldSucceed = true)
{
spec.ConvertToStack(ProtoMan, Factory);
var entities = await DoEntityLookup(flags);
foreach (var uid in entities)
{
var found = ToEntitySpecifier(uid);
if (spec.Prototype != found.Prototype)
continue;
if (found.Quantity >= spec.Quantity)
return uid;
// TODO combine stacks?
}
if (shouldSucceed)
Assert.Fail($"Could not find stack/entity with prototype {spec.Prototype}");
return default;
}
#endregion
/// <summary>
/// List of currently active DoAfters on the player.
/// </summary>
protected IEnumerable<Shared.DoAfter.DoAfter> ActiveDoAfters
=> DoAfters.DoAfters.Values.Where(x => !x.Cancelled && !x.Completed);
/// <summary>
/// Convenience method to get components on the target. Returns SERVER-SIDE components.
/// </summary>
protected T Comp<T>() => SEntMan.GetComponent<T>(Target!.Value);
/// <summary>
/// Set the tile at the target position to some prototype.
/// </summary>
protected async Task SetTile(string? proto, EntityCoordinates? coords = null, MapGridComponent? grid = null)
{
var tile = proto == null
? Tile.Empty
: new Tile(TileMan[proto].TileId);
var pos = (coords ?? TargetCoords).ToMap(SEntMan, Transform);
await Server.WaitPost(() =>
{
if (grid != null || MapMan.TryFindGridAt(pos, out grid))
{
grid.SetTile(coords ?? TargetCoords, tile);
return;
}
if (proto == null)
return;
grid = MapMan.CreateGrid(MapData.MapId);
var gridXform = SEntMan.GetComponent<TransformComponent>(grid.Owner);
Transform.SetWorldPosition(gridXform, pos.Position);
grid.SetTile(coords ?? TargetCoords, tile);
if (!MapMan.TryFindGridAt(pos, out grid))
Assert.Fail("Failed to create grid?");
});
await AssertTile(proto, coords);
}
protected async Task Delete(EntityUid uid)
{
await Server.WaitPost(() => SEntMan.DeleteEntity(uid));
await RunTicks(5);
}
protected async Task RunTicks(int ticks)
{
await PoolManager.RunTicksSync(PairTracker.Pair, ticks);
}
protected async Task RunSeconds(float seconds)
=> await RunTicks((int) Math.Ceiling(seconds / TickPeriod));
}

View File

@@ -0,0 +1,198 @@
#nullable enable
using System.Linq;
using System.Threading.Tasks;
using Content.Client.Construction;
using Content.Client.Examine;
using Content.Server.Body.Systems;
using Content.Server.Mind.Components;
using Content.Server.Stack;
using Content.Server.Tools;
using Content.Shared.Body.Part;
using Content.Shared.DoAfter;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.UnitTesting;
namespace Content.IntegrationTests.Tests.Interaction;
/// <summary>
/// This is a base class designed to make it easier to test various interactions like construction & DoAfters.
///
/// For construction tests, the interactions are intentionally hard-coded and not pulled automatically from the
/// construction graph, even though this may be a pain to maintain. This is because otherwise these tests could not
/// detect errors in the graph pathfinding (e.g., infinite loops, missing steps, etc).
/// </summary>
[TestFixture]
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public abstract partial class InteractionTest
{
protected PairTracker PairTracker = default!;
protected TestMapData MapData = default!;
protected RobustIntegrationTest.ServerIntegrationInstance Server => PairTracker.Pair.Server;
protected RobustIntegrationTest.ClientIntegrationInstance Client => PairTracker.Pair.Client;
protected MapId MapId => MapData.MapId;
/// <summary>
/// Target coordinates. Note that this does not necessarily correspond to the position of the <see cref="Target"/>
/// entity.
/// </summary>
protected EntityCoordinates TargetCoords;
/// <summary>
/// Initial player coordinates. Note that this does not necessarily correspond to the position of the
/// <see cref="Player"/> entity.
/// </summary>
protected EntityCoordinates PlayerCoords;
/// <summary>
/// The player entity that performs all these interactions. Defaults to an admin-observer with 1 hand.
/// </summary>
protected EntityUid Player;
/// <summary>
/// The current target entity. This is the default entity for various helper functions.
/// </summary>
/// <remarks>
/// Note that this target may be automatically modified by various interactions, in particular construction
/// interactions often swap out entities, and there are helper methods that attempt to automatically upddate
/// the target entity. See <see cref="CheckTargetChange"/>
/// </remarks>
protected EntityUid? Target;
/// <summary>
/// When attempting to start construction, this is the client-side ID of the construction ghost.
/// </summary>
protected int ConstructionGhostId;
// SERVER dependencies
protected IEntityManager SEntMan = default!;
protected ITileDefinitionManager TileMan = default!;
protected IMapManager MapMan = default!;
protected IPrototypeManager ProtoMan = default!;
protected IGameTiming Timing = default!;
protected IComponentFactory Factory = default!;
protected SharedHandsSystem HandSys = default!;
protected StackSystem Stack = default!;
protected SharedInteractionSystem InteractSys = default!;
protected Content.Server.Construction.ConstructionSystem SConstruction = default!;
protected SharedDoAfterSystem DoAfterSys = default!;
protected ToolSystem ToolSys = default!;
protected InteractionTestSystem STestSystem = default!;
protected SharedTransformSystem Transform = default!;
// CLIENT dependencies
protected IEntityManager CEntMan = default!;
protected ConstructionSystem CConSys = default!;
protected ExamineSystem ExamineSys = default!;
protected InteractionTestSystem CTestSystem = default!;
// player components
protected HandsComponent Hands = default!;
protected DoAfterComponent DoAfters = default!;
public float TickPeriod => (float)Timing.TickPeriod.TotalSeconds;
[SetUp]
public async Task Setup()
{
PairTracker = await PoolManager.GetServerClient(new PoolSettings());
// server dependencies
SEntMan = Server.ResolveDependency<IEntityManager>();
TileMan = Server.ResolveDependency<ITileDefinitionManager>();
MapMan = Server.ResolveDependency<IMapManager>();
ProtoMan = Server.ResolveDependency<IPrototypeManager>();
Factory = Server.ResolveDependency<IComponentFactory>();
Timing = Server.ResolveDependency<IGameTiming>();
HandSys = SEntMan.System<SharedHandsSystem>();
InteractSys = SEntMan.System<SharedInteractionSystem>();
ToolSys = SEntMan.System<ToolSystem>();
DoAfterSys = SEntMan.System<SharedDoAfterSystem>();
Transform = SEntMan.System<SharedTransformSystem>();
SConstruction = SEntMan.System<Content.Server.Construction.ConstructionSystem>();
STestSystem = SEntMan.System<InteractionTestSystem>();
Stack = SEntMan.System<StackSystem>();
// client dependencies
CEntMan = Client.ResolveDependency<IEntityManager>();
CTestSystem = CEntMan.System<InteractionTestSystem>();
CConSys = CEntMan.System<ConstructionSystem>();
ExamineSys = CEntMan.System<ExamineSystem>();
// Setup map.
MapData = await PoolManager.CreateTestMap(PairTracker);
PlayerCoords = MapData.GridCoords.Offset((0.5f, 0.5f)).WithEntityId(MapData.MapUid, Transform, SEntMan);
TargetCoords = MapData.GridCoords.Offset((1.5f, 0.5f)).WithEntityId(MapData.MapUid, Transform, SEntMan);
await SetTile(Plating, grid: MapData.MapGrid);
// Get player data
var sPlayerMan = Server.ResolveDependency<Robust.Server.Player.IPlayerManager>();
var cPlayerMan = Client.ResolveDependency<Robust.Client.Player.IPlayerManager>();
if (cPlayerMan.LocalPlayer?.Session == null)
Assert.Fail("No player");
var cSession = cPlayerMan.LocalPlayer!.Session!;
var sSession = sPlayerMan.GetSessionByUserId(cSession.UserId);
// Spawn player entity & attach
EntityUid? old = default;
await Server.WaitPost(() =>
{
old = cPlayerMan.LocalPlayer.ControlledEntity;
Player = SEntMan.SpawnEntity(PlayerEntity, PlayerCoords);
sSession.AttachToEntity(Player);
Hands = SEntMan.GetComponent<HandsComponent>(Player);
DoAfters = SEntMan.GetComponent<DoAfterComponent>(Player);
});
// Check player got attached.
await RunTicks(5);
Assert.That(cPlayerMan.LocalPlayer.ControlledEntity, Is.EqualTo(Player));
// Delete old player entity.
await Server.WaitPost(() =>
{
if (old == null)
return;
// Fuck you mind system I want an hour of my life back
if (SEntMan.TryGetComponent(old, out MindComponent? mind))
mind.GhostOnShutdown = false;
SEntMan.DeleteEntity(old.Value);
});
// Ensure that the player only has one hand, so that they do not accidentally pick up deconstruction protucts
await Server.WaitPost(() =>
{
var bodySystem = SEntMan.System<BodySystem>();
var hands = bodySystem.GetBodyChildrenOfType(Player, BodyPartType.Hand).ToArray();
for (var i = 1; i < hands.Length; i++)
{
bodySystem.DropPart(hands[i].Id);
SEntMan.DeleteEntity(hands[i].Id);
}
});
// Final player asserts/checks.
await PoolManager.ReallyBeIdle(PairTracker.Pair, 5);
Assert.That(cPlayerMan.LocalPlayer.ControlledEntity, Is.EqualTo(Player));
Assert.That(sPlayerMan.GetSessionByUserId(cSession.UserId).AttachedEntity, Is.EqualTo(Player));
}
[TearDown]
public async Task Cleanup()
{
await Server.WaitPost(() => MapMan.DeleteMap(MapId));
await PairTracker.CleanReturnAsync();
}
}

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Content.Server.Construction;
using Content.Shared.Construction;
using Robust.Shared.GameObjects;
namespace Content.IntegrationTests.Tests.Interaction;
/// <summary>
/// System for listening to events that get raised when construction entities change.
/// In particular, when construction ghosts become real entities, and when existing entities get replaced with
/// new ones.
/// </summary>
public sealed class InteractionTestSystem : EntitySystem
{
public Dictionary<int, EntityUid> Ghosts = new();
public Dictionary<EntityUid, EntityUid> EntChanges = new();
public override void Initialize()
{
SubscribeNetworkEvent<AckStructureConstructionMessage>(OnAck);
SubscribeLocalEvent<ConstructionChangeEntityEvent>(OnEntChange);
}
private void OnEntChange(ConstructionChangeEntityEvent ev)
{
EntChanges[ev.Old] = ev.New;
}
private void OnAck(AckStructureConstructionMessage ev)
{
if (ev.Uid != null)
Ghosts[ev.GhostId] = ev.Uid.Value;
}
}