2026-11-28 18:33:08 +00:00
using System.Linq ;
using BlaxSlaves ;
using HarmonyLib ;
using RimWorld ;
using Verse ;
namespace BlaxSlavesTests
{
public static class SmokeTests
{
[BlaxTest("startup: patches applied and logged")]
public static void PatchesApplied ( )
{
var ours = Harmony . GetAllPatchedMethods ( )
. Select ( Harmony . GetPatchInfo )
. Where ( info = > info ! = null )
. SelectMany ( info = > info . Prefixes . Concat ( info . Postfixes ) . Concat ( info . Finalizers ) . Concat ( info . Transpilers ) )
. Count ( p = > p . owner = = "blax.slaves" ) ;
2026-11-29 16:07:37 +00:00
T . Info ( "Harmony patches owned by blax.slaves: " + ours + ", DLCs: " + Fixtures . ActiveDlcs ( ) ) ;
2026-11-28 18:33:08 +00:00
T . Check ( ours > = 17 , "expected at least 17 patches from blax.slaves, found " + ours ) ;
bool logged = Log . Messages . Any ( m = > m . text ! = null & & m . text . Contains ( "[Blax Slaves]" ) & & m . text . Contains ( "patches applied" ) ) ;
T . Check ( logged , "startup line '[Blax Slaves] N patches applied' not in the log" ) ;
var errors = Log . Messages . Where ( m = > m . type = = LogMessageType . Error & & m . text ! = null & & m . text . Contains ( "Blax" ) ) . ToList ( ) ;
T . Check ( errors . Count = = 0 , "errors mentioning Blax at startup: " + string . Join ( " | " , errors . Take ( 3 ) . Select ( e = > e . text ) ) ) ;
2026-11-29 16:07:37 +00:00
// Any DLC combination loads quietly: no warning from the mod or about its defs either.
var warnings = Log . Messages . Where ( m = > m . type = = LogMessageType . Warning & & m . text ! = null
& & ( m . text . Contains ( "Blax Slaves" ) | | m . text . Contains ( "BlaxSlaves" ) ) ) . ToList ( ) ;
T . Check ( warnings . Count = = 0 , "warnings mentioning Blax Slaves at startup: " + string . Join ( " | " , warnings . Take ( 3 ) . Select ( e = > e . text ) ) ) ;
2026-11-28 18:33:08 +00:00
}
[BlaxTest("startup: settings and defs loaded")]
public static void DefsLoaded ( )
{
T . NotNull ( BlaxSlavesMod . Settings , "BlaxSlavesMod.Settings" ) ;
T . NotNull ( BlaxDefOf . BlaxSlaves_SlaveBrand , "hediff BlaxSlaves_SlaveBrand" ) ;
T . NotNull ( DefDatabase < RecipeDef > . GetNamedSilentFail ( "BlaxSlaves_BrandSlave" ) , "recipe BlaxSlaves_BrandSlave" ) ;
T . NotNull ( DefDatabase < RecipeDef > . GetNamedSilentFail ( "BlaxSlaves_RebrandSlave" ) , "recipe BlaxSlaves_RebrandSlave" ) ;
T . NotNull ( DefDatabase < ThoughtDef > . GetNamedSilentFail ( "BlaxSlaves_Branded" ) , "thought BlaxSlaves_Branded" ) ;
T . Check ( ThingDefOf . Human . AllRecipes . Any ( r = > r . defName = = "BlaxSlaves_BrandSlave" ) , "humans don't list the branding surgery" ) ;
}
[BlaxTest("harness: colony slave fixture")]
public static void SlaveFixture ( )
{
Pawn slave = Fixtures . ColonySlave ( ) ;
T . Check ( slave . Spawned , "slave not spawned" ) ;
T . Check ( slave . IsSlaveOfColony , "fixture pawn is not a slave of the colony" ) ;
}
2026-11-29 16:07:37 +00:00
/// <summary>
/// A pawn left inside rock has no district, and vanilla AI throws when it ticks (a prisoner's
/// escape check). Random quick-test maps sometimes have rock at the center, so a wall stands in for it.
/// </summary>
[BlaxTest("harness: test pawns spawn on an open cell, also when the map center is blocked")]
public static void SpawnOnOpenCell ( )
{
Map map = Fixtures . Map ;
Pawn first = Fixtures . Colonist ( ) ;
T . Check ( Fixtures . IsOpen ( first . Position , map ) , "test pawn " + first . LabelShort + " spawned on " + first . Position + ", which is not open (rock, fog or no district)" ) ;
IntVec3 center = map . Center ;
if ( ! Fixtures . IsOpen ( center , map ) )
{
T . Info ( "map center " + center + " is blocked on this map, so the spawn above already took the fallback" ) ;
return ;
}
if ( center . GetFirstPawn ( map ) ! = null | | center . GetFirstItem ( map ) ! = null | | center . GetEdifice ( map ) ! = null | | center . Roofed ( map ) )
T . Skip ( "setup: something stands on the map center " + center + " or it is roofed, so no wall can block it" ) ;
ThingDef wallDef = ThingDefOf . Wall ;
Thing wall = GenSpawn . Spawn ( ThingMaker . MakeThing ( wallDef , GenStuff . DefaultStuffFor ( wallDef ) ) , center , map ) ;
try
{
T . Check ( ! Fixtures . IsOpen ( center , map ) , "setup: the map center " + center + " is still open with a wall on it" ) ;
Pawn p = Fixtures . Colonist ( ) ;
T . Check ( p . Position ! = center & & Fixtures . IsOpen ( p . Position , map ) ,
"with the map center walled off, test pawn " + p . LabelShort + " spawned on " + p . Position + ", which is not open" ) ;
}
finally
{
if ( ! wall . Destroyed )
wall . Destroy ( DestroyMode . Vanish ) ;
}
}
2026-11-28 18:33:08 +00:00
[BlaxTest("harness: save and reload kept a marker pawn", phase: 2)]
public static void ReloadWorked ( )
{
T . Check ( T . Carry . TryGetValue ( "marker" , out string id ) , "phase 1 marker missing" ) ;
2026-11-29 16:07:37 +00:00
T . Check ( T . Carry . TryGetValue ( "marker.kind" , out string kind ) , "phase 1 marker kind missing" ) ;
2026-11-28 18:33:08 +00:00
Pawn p = Fixtures . FindPawnById ( id ) ;
T . NotNull ( p , "marker pawn " + id + " after reload" ) ;
2026-11-29 16:07:37 +00:00
if ( kind = = "slave" )
T . Check ( p . IsSlaveOfColony , "marker pawn lost slave status over save and load" ) ;
else
T . Check ( p . IsPrisonerOfColony , "marker pawn lost prisoner status over save and load" ) ;
2026-11-28 18:33:08 +00:00
}
2026-11-29 16:07:37 +00:00
/// <summary>With Ideology a colony slave, without it a prisoner, so the save and reload check runs in every DLC set.</summary>
2026-11-28 18:33:08 +00:00
[BlaxTest("harness: set up marker pawn for reload")]
public static void MarkerSetup ( )
{
2026-11-29 16:07:37 +00:00
Pawn marker = Fixtures . Keep ( ModsConfig . IdeologyActive ? Fixtures . ColonySlave ( ) : Fixtures . ColonyPrisoner ( ) ) ;
T . Carry [ "marker" ] = marker . ThingID ;
T . Carry [ "marker.kind" ] = ModsConfig . IdeologyActive ? "slave" : "prisoner" ;
2026-11-28 18:33:08 +00:00
}
}
}