2026-11-30 21:33:40 +00:00
using System.Collections.Generic ;
2026-11-29 18:27:08 +00:00
using System.Linq ;
using System.Reflection ;
using BlaxZones ;
using HarmonyLib ;
using RimWorld ;
using Verse ;
namespace BlaxZonesTests
{
/// <summary>The skeleton and the harness work: patches, settings, the component, the API shape, map fixtures, all three phases.</summary>
public static class SmokeTests
{
private static readonly string [ ] SharedKeys =
{
"BlaxZones_DeliveryZone" , "BlaxZones_NeedsDlc" ,
2026-11-30 21:33:40 +00:00
"BlaxZones_SettingDeliveryZones" , "BlaxZones_SettingRouteOrbital" , "BlaxZones_SettingRouteRewards" , "BlaxZones_SettingOverflowNearZone" ,
"BlaxZones_SettingOverflowRadius" , "BlaxZones_SettingRouteCaravan" , "BlaxZones_SettingCaravanMessage" ,
"BlaxZones_SettingCampAtZones" , "BlaxZones_SettingsReset" ,
2026-11-29 18:27:08 +00:00
} ;
[BlaxTest("startup: every patch class applied and logged, no errors mentioning Blax Zones")]
public static void StartupLogged ( )
{
int 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 = = BlaxZonesStartup . HarmonyId ) ;
T . Info ( "Harmony patches owned by " + BlaxZonesStartup . HarmonyId + ": " + ours ) ;
T . Check ( BlaxZonesStartup . FailedPatches . Count = = 0 , "patch classes failed: " + string . Join ( ", " , BlaxZonesStartup . FailedPatches ) ) ;
// Only what was logged before the first test: later tests log expected errors on purpose.
var startup = TestRunner . StartupLog ;
bool logged = startup . Any ( m = > m . text ! = null & & m . text . Contains ( "[Blax Zones]" ) & & m . text . Contains ( "patches applied" ) ) ;
T . Check ( logged , "startup line '[Blax Zones] N patches applied' not in the log" ) ;
var errors = startup . Where ( m = > m . type = = LogMessageType . Error & & m . text ! = null & & ( m . text . Contains ( "Blax Zones" ) | | m . text . Contains ( "BlaxZones" ) ) ) . ToList ( ) ;
T . Check ( errors . Count = = 0 , "errors mentioning Blax Zones at startup: " + string . Join ( " | " , errors . Take ( 3 ) . Select ( e = > e . text ) ) ) ;
}
2026-11-30 21:33:40 +00:00
[BlaxTest("startup: every Blax Zones finalizer returns void, so Harmony rethrows exceptions with their stack trace")]
public static void FinalizersReturnVoid ( )
{
// One finalizer returning Exception on a method makes Harmony "throw ex" instead of "rethrow"
// (0Harmony EmitFinalizers), which cuts the inner frames off every exception passing through it.
var finalizers = Harmony . GetAllPatchedMethods ( )
. Select ( Harmony . GetPatchInfo )
. Where ( info = > info ! = null )
. SelectMany ( info = > info . Finalizers )
. Where ( p = > p . owner = = BlaxZonesStartup . HarmonyId )
. ToList ( ) ;
T . Check ( finalizers . Count > = 6 , "Blax Zones finalizers found: " + finalizers . Count + ", expected at least 6" ) ;
var bad = finalizers . Where ( p = > p . PatchMethod . ReturnType ! = typeof ( void ) ) . Select ( p = > p . PatchMethod . DeclaringType ? . Name ) . ToList ( ) ;
T . Check ( bad . Count = = 0 , "finalizers that return a value: " + string . Join ( ", " , bad ) ) ;
}
2026-11-29 18:27:08 +00:00
[BlaxTest("startup: settings, shared strings and one component per map")]
public static void Loaded ( )
{
T . NotNull ( BlaxZonesMod . Settings , "BlaxZonesMod.Settings" ) ;
var missing = SharedKeys . Where ( k = > ! k . CanTranslate ( ) ) . ToList ( ) ;
T . Check ( missing . Count = = 0 , "strings missing from Keyed: " + string . Join ( ", " , missing ) ) ;
foreach ( Map m in Find . Maps )
T . Equal ( 1 , m . components . Count ( c = > c is MapComponent_BlaxZones ) , "MapComponent_BlaxZones on map " + m . Index ) ;
T . Check ( Fixtures . Comp ! = null , "MapComponent_BlaxZones.Of(current map) is null" ) ;
}
[BlaxTest("settings: the overflow radius clamps to 5..30")]
public static void OverflowRadiusClamp ( )
{
BlaxZonesSettings s = Fixtures . Settings ;
s . overflowRadius = 2 ;
T . Equal ( BlaxZonesSettings . MinOverflowRadius , s . OverflowRadiusClamped , "radius 2 clamped" ) ;
s . overflowRadius = 99 ;
T . Equal ( BlaxZonesSettings . MaxOverflowRadius , s . OverflowRadiusClamped , "radius 99 clamped" ) ;
T . Equal ( BlaxZonesSettings . DefaultOverflowRadius , new BlaxZonesSettings ( ) . overflowRadius , "default radius" ) ;
}
[BlaxTest("api: ZonesApi version 1 binds the way Blax Slaves binds it and refuses a pawn")]
public static void ApiShape ( )
{
System . Type api = AccessTools . TypeByName ( "BlaxZones.ZonesApi" ) ;
T . NotNull ( api , "BlaxZones.ZonesApi by name" ) ;
PropertyInfo version = AccessTools . Property ( api , "Version" ) ;
T . NotNull ( version , "ZonesApi.Version property" ) ;
T . Equal ( 1 , ( int ) version . GetValue ( null ) , "ZonesApi.Version" ) ;
MethodInfo m = AccessTools . Method ( api , "TryFindDropSpot" , new [ ] { typeof ( Map ) , typeof ( Thing ) , typeof ( IntVec3 ) . MakeByRefType ( ) } ) ;
T . Check ( m ! = null & & m . IsStatic & & m . ReturnType = = typeof ( bool ) , "ZonesApi.TryFindDropSpot(Map, Thing, out IntVec3) static bool" ) ;
Pawn p = Fixtures . Colonist ( ) ;
T . Check ( ! ZonesApi . TryFindDropSpot ( Fixtures . Map , p , out IntVec3 cell ) , "TryFindDropSpot accepted a pawn" ) ;
T . Check ( ! cell . IsValid , "TryFindDropSpot set a cell for a pawn" ) ;
}
[BlaxTest("harness: TestArea, Roof, Wall, Water and DeliveryZone fixtures")]
public static void MapFixtures ( )
{
Map map = Fixtures . Map ;
CellRect a = Fixtures . TestArea ( 4 , 3 ) ;
T . Equal ( 12 , a . Area , "area size" ) ;
foreach ( IntVec3 c in a . ExpandedBy ( Fixtures . AreaMargin ) )
{
T . Check ( c . GetRoof ( map ) = = null , "roof left at " + c ) ;
T . Check ( ! c . Fogged ( map ) , "fog left at " + c ) ;
T . Check ( c . Standable ( map ) , "cell not standable at " + c ) ;
}
CellRect b = Fixtures . TestArea ( 4 , 3 ) ;
T . Check ( ! a . ExpandedBy ( Fixtures . AreaMargin ) . Overlaps ( b . ExpandedBy ( Fixtures . AreaMargin ) ) , "two test areas overlap" ) ;
Zone_Delivery zone = Fixtures . DeliveryZone ( a , priority : StoragePriority . Important ) ;
T . Equal ( 12 , zone . CellCount , "zone cells" ) ;
T . Check ( map . zoneManager . ZoneAt ( a . CenterCell ) = = zone , "ZoneAt(center) is not the delivery zone" ) ;
T . Check ( map . zoneManager . AllZones . Contains ( zone ) , "zone not registered" ) ;
T . Check ( zone . Accepts ( Fixtures . Stack ( ThingDefOf . Steel , 10 ) ) , "default filter refuses steel" ) ;
Fixtures . Roof ( new CellRect ( a . minX , a . minZ , 1 , 1 ) ) ;
T . Check ( new IntVec3 ( a . minX , 0 , a . minZ ) . Roofed ( map ) , "Roof fixture left the cell unroofed" ) ;
IntVec3 wallCell = new IntVec3 ( a . maxX , 0 , a . maxZ ) ;
Fixtures . Wall ( wallCell ) ;
T . Equal ( 11 , zone . CellCount , "zone cells after a wall on one" ) ;
Fixtures . Water ( new IntVec3 ( a . minX + 1 , 0 , a . minZ ) ) ;
T . Check ( new IntVec3 ( a . minX + 1 , 0 , a . minZ ) . GetTerrain ( map ) . IsWater , "Water fixture is not water" ) ;
}
[BlaxTest("harness: colonist, prisoner and colony slave fixtures")]
public static void PawnFixtures ( )
{
Pawn c = Fixtures . Colonist ( ) ;
T . Check ( c . Spawned & & c . IsFreeColonist , "colonist fixture is not a spawned free colonist" ) ;
Pawn pr = Fixtures . Prisoner ( ) ;
T . Check ( pr . IsPrisonerOfColony , "prisoner fixture is not a prisoner of the colony" ) ;
Pawn s = Fixtures . ColonySlave ( ) ;
T . Check ( s . IsSlaveOfColony , "slave fixture is not a slave of the colony" ) ;
}
[BlaxTest("harness: the component is on every map after the reload", phase: 2)]
public static void ComponentAfterReload ( )
{
foreach ( Map m in Find . Maps )
T . Equal ( 1 , m . components . Count ( c = > c is MapComponent_BlaxZones ) , "MapComponent_BlaxZones on map " + m . Index ) ;
}
2026-11-30 21:33:40 +00:00
[BlaxTest("harness: the reload logs no 'Could not resolve reference' warning except for vanilla's unsaved tale pawns and Hospitality's stale lords", phase: 2)]
public static void ReloadResolvesEveryReference ( )
{
// A reference Blax Zones saves but can't load would show here. Phase-1 tests that leave a pawn
// with a relation to a pawn nobody saves show as "dangling relation" INFO lines under that test.
// Two leaks of other code are excused by name:
// - vanilla: a pawn TaleData_Pawn.GenerateRandom made for a book in a trader caravan's stock,
// which every forced caravan can bring;
// - Hospitality (-Compat): CompGuest.PostSpawnSetup stores Pawn.GetLord() on every humanlike pawn
// and saves it with Scribe_References, and nothing clears it when the pawn leaves the map
// (Hospitality Source/CompGuest.cs, lord; PostDeSpawn only drops PresentGuests). Every forced
// caravan's pawns keep their ended lord. Blax Zones saves no Lord reference.
List < string > all = T . Phase2ResolveWarnings ;
List < string > vanilla = all . Where ( T . NamesVanillaTalePawn ) . ToList ( ) ;
bool hospitality = Compat . IsActive ( "Orion.Hospitality" ) ;
List < string > staleLords = hospitality ? all . Where ( w = > w . Contains ( "of type Verse.AI.Group.Lord." ) ) . ToList ( ) : new List < string > ( ) ;
List < string > rest = all . Where ( w = > ! vanilla . Contains ( w ) & & ! staleLords . Contains ( w ) ) . ToList ( ) ;
T . Info ( all . Count + " warning(s) on the reload: " + vanilla . Count + " for pawns vanilla made for books or tales and never saved, " +
staleLords . Count + " for lords Hospitality's CompGuest still names" ) ;
T . Equal ( 0 , rest . Count , "other 'Could not resolve reference' warnings while the phase-1 save loaded (first: " +
string . Join ( " | " , rest . Take ( 3 ) ) + ")" ) ;
}
2026-11-29 18:27:08 +00:00
[BlaxTest("harness: phase 3 loads with exactly the two removal errors per map and a fresh component", phase: 3)]
public static void Phase3Removal ( )
{
int maps = TestRunner . Phase3ReplacedCount ;
T . Check ( maps > = 1 , "phase 3 replaced no component node" ) ;
T . Equal ( Find . Maps . Count , maps , "replaced component nodes vs maps" ) ;
T . ExpectErrors ( "Could not find class " + TestRunner . RemovedComponentClass , maps ) ;
T . ExpectErrors ( "Can't load abstract class Verse.MapComponent" , maps ) ;
T . Info ( "load errors: " + T . Phase3LoadErrors . Count + ", resolve warnings: " + T . Phase3ResolveWarnings ) ;
T . Equal ( 2 * maps , T . Phase3LoadErrors . Count , "errors while loading the phase-3 save" ) ;
foreach ( Map m in Find . Maps )
T . Equal ( 1 , m . components . Count ( c = > c is MapComponent_BlaxZones ) , "fresh MapComponent_BlaxZones on map " + m . Index ) ;
}
}
}