2021-02-28 17:16:41 +01:00
using System.Text ;
using System.Threading.Tasks ;
using Content.Server.Construction.Completions ;
using Content.Shared.Construction ;
2021-06-09 22:19:39 +02:00
using Content.Shared.Construction.Prototypes ;
2021-02-28 17:16:41 +01:00
using NUnit.Framework ;
using Robust.Shared.Prototypes ;
namespace Content.IntegrationTests.Tests.Construction
{
[TestFixture]
2022-06-19 20:22:28 -07:00
public sealed class ConstructionActionValid
2021-02-28 17:16:41 +01:00
{
2021-03-08 05:09:17 +01:00
private bool IsValid ( IGraphAction action , IPrototypeManager protoMan , out string prototype )
{
switch ( action )
{
case SpawnPrototype spawn :
prototype = spawn . Prototype ;
return protoMan . TryIndex < EntityPrototype > ( spawn . Prototype , out _ ) ;
case SpawnPrototypeAtContainer spawn :
prototype = spawn . Prototype ;
return protoMan . TryIndex < EntityPrototype > ( spawn . Prototype , out _ ) ;
case ConditionalAction conditional :
var valid = IsValid ( conditional . Action , protoMan , out var protoA ) & IsValid ( conditional . Else , protoMan , out var protoB ) ;
if ( ! string . IsNullOrEmpty ( protoA ) & & string . IsNullOrEmpty ( protoB ) )
{
prototype = protoA ;
}
else if ( string . IsNullOrEmpty ( protoA ) & & ! string . IsNullOrEmpty ( protoB ) )
{
prototype = protoB ;
}
else
{
prototype = $"{protoA}, {protoB}" ;
}
return valid ;
default :
prototype = string . Empty ;
return true ;
}
}
2021-02-28 17:16:41 +01:00
[Test]
public async Task ConstructionGraphSpawnPrototypeValid ( )
{
2022-06-19 20:22:28 -07:00
await using var pairTracker = await PoolManager . GetServerClient ( new PoolSettings { NoClient = true } ) ;
var server = pairTracker . Pair . Server ;
2021-02-28 17:16:41 +01:00
var protoMan = server . ResolveDependency < IPrototypeManager > ( ) ;
var valid = true ;
var message = new StringBuilder ( ) ;
foreach ( var graph in protoMan . EnumeratePrototypes < ConstructionGraphPrototype > ( ) )
{
2021-04-09 20:47:31 +02:00
foreach ( var node in graph . Nodes . Values )
2021-02-28 17:16:41 +01:00
{
foreach ( var action in node . Actions )
{
2021-03-08 05:09:17 +01:00
if ( IsValid ( action , protoMan , out var prototype ) ) continue ;
2021-02-28 17:16:41 +01:00
valid = false ;
2021-03-08 05:09:17 +01:00
message . Append ( $"Invalid entity prototype \" { prototype } \ " on graph action in node \"{node.Name}\" of graph \"{graph.ID}\"\n" ) ;
2021-02-28 17:16:41 +01:00
}
foreach ( var edge in node . Edges )
{
foreach ( var action in edge . Completed )
{
2021-03-08 05:09:17 +01:00
if ( IsValid ( action , protoMan , out var prototype ) ) continue ;
2021-02-28 17:16:41 +01:00
valid = false ;
2021-03-08 05:09:17 +01:00
message . Append ( $"Invalid entity prototype \" { prototype } \ " on graph action in edge \"{edge.Target}\" of node \"{node.Name}\" of graph \"{graph.ID}\"\n" ) ;
2021-02-28 17:16:41 +01:00
}
}
}
}
2022-06-19 20:22:28 -07:00
await pairTracker . CleanReturnAsync ( ) ;
2021-02-28 17:16:41 +01:00
Assert . That ( valid , Is . True , $"One or more SpawnPrototype actions specified invalid entity prototypes!\n{message}" ) ;
}
[Test]
public async Task ConstructionGraphNodeEntityPrototypeValid ( )
{
2022-06-19 20:22:28 -07:00
await using var pairTracker = await PoolManager . GetServerClient ( new PoolSettings { NoClient = true } ) ;
var server = pairTracker . Pair . Server ;
2021-02-28 17:16:41 +01:00
var protoMan = server . ResolveDependency < IPrototypeManager > ( ) ;
var valid = true ;
var message = new StringBuilder ( ) ;
foreach ( var graph in protoMan . EnumeratePrototypes < ConstructionGraphPrototype > ( ) )
{
2021-04-09 20:47:31 +02:00
foreach ( var node in graph . Nodes . Values )
2021-02-28 17:16:41 +01:00
{
if ( string . IsNullOrEmpty ( node . Entity ) | | protoMan . TryIndex ( node . Entity , out EntityPrototype _ ) ) continue ;
valid = false ;
message . Append ( $"Invalid entity prototype \" { node . Entity } \ " on node \"{node.Name}\" of graph \"{graph.ID}\"\n" ) ;
}
}
2022-06-19 20:22:28 -07:00
await pairTracker . CleanReturnAsync ( ) ;
2021-02-28 17:16:41 +01:00
Assert . That ( valid , Is . True , $"One or more nodes specified invalid entity prototypes!\n{message}" ) ;
}
[Test]
public async Task ConstructionGraphEdgeValid ( )
{
2022-06-19 20:22:28 -07:00
await using var pairTracker = await PoolManager . GetServerClient ( new PoolSettings { NoClient = true } ) ;
var server = pairTracker . Pair . Server ;
2021-02-28 17:16:41 +01:00
var protoMan = server . ResolveDependency < IPrototypeManager > ( ) ;
var valid = true ;
var message = new StringBuilder ( ) ;
foreach ( var graph in protoMan . EnumeratePrototypes < ConstructionGraphPrototype > ( ) )
{
2021-04-09 20:47:31 +02:00
foreach ( var node in graph . Nodes . Values )
2021-02-28 17:16:41 +01:00
{
foreach ( var edge in node . Edges )
{
if ( graph . Nodes . ContainsKey ( edge . Target ) ) continue ;
valid = false ;
message . Append ( $"Invalid target \" { edge . Target } \ " in edge on node \"{node.Name}\" of graph \"{graph.ID}\"\n" ) ;
}
}
}
Assert . That ( valid , Is . True , $"One or more edges specified invalid node targets!\n{message}" ) ;
2022-06-19 20:22:28 -07:00
await pairTracker . CleanReturnAsync ( ) ;
2021-02-28 17:16:41 +01:00
}
}
}