2026-07-18 18:38:07 +01:00
using System ;
2026-07-13 23:44:55 +01:00
using System.Reflection ;
2026-07-15 19:59:37 +01:00
using System.Collections.Generic ;
2026-07-18 18:38:07 +01:00
using System.Diagnostics ;
2026-08-19 04:03:00 +02:00
using System.Linq ;
2026-07-19 19:35:49 +01:00
using System.Net ;
using System.Net.Sockets ;
2026-07-19 21:21:52 +01:00
using System.IO ;
2026-08-18 10:08:03 +02:00
using System.Threading ;
using System.Threading.Tasks ;
2026-07-19 21:21:52 +01:00
using GameCult.Eve.Surface ;
2026-07-13 23:44:55 +01:00
using GameCult.Eve.UnityScene ;
2026-08-18 10:08:03 +02:00
using GameCult.Mesh ;
2026-07-13 23:44:55 +01:00
using NUnit.Framework ;
2026-07-18 18:38:07 +01:00
using UnityEngine ;
2026-07-13 23:44:55 +01:00
namespace GameCult.Eve.UnityScene.Tests
{
public sealed class EveUnityCultMeshAssetLookupTests
{
2026-08-18 10:08:03 +02:00
[Test]
public void ConnectCannotPerformRemoteWorkBeforeAsynchronousPreparation ( )
{
using var transport = new EveUnityCultMeshLiveProviderTransport (
"test-cache" ,
"cultnet+tcp://127.0.0.1:1" ,
"test.verse" ,
"test.runtime" ,
"test.provider" ,
"test.surface" ) ;
var elapsed = Stopwatch . StartNew ( ) ;
var failure = Assert . Throws < InvalidOperationException > ( ( ) = > transport . Connect ( ) ) ;
StringAssert . Contains ( "Await PrepareAsync" , failure ! . Message ) ;
Assert . That ( elapsed . Elapsed , Is . LessThan ( TimeSpan . FromMilliseconds ( 100 ) ) ) ;
}
[Test]
2026-08-18 17:47:34 +02:00
public async Task CommandOutboxCompletesOnlyAfterCanonicalReceipt ( )
2026-08-18 10:08:03 +02:00
{
var entered = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
var release = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
var sends = 0 ;
using var outbox = new EveUnityCultMeshCommandOutbox (
async ( _ , cancellationToken ) = >
{
Interlocked . Increment ( ref sends ) ;
entered . TrySetResult ( true ) ;
await AwaitOrCancel ( release . Task , cancellationToken ) ;
} ,
2026-08-18 17:47:34 +02:00
_ = > null ,
retryDelay : TimeSpan . FromMilliseconds ( 250 ) ) ;
2026-08-18 10:08:03 +02:00
var elapsed = Stopwatch . StartNew ( ) ;
outbox . Enqueue ( Request ( "one-shot" , "dock" ) ) ;
Assert . That ( elapsed . Elapsed , Is . LessThan ( TimeSpan . FromMilliseconds ( 100 ) ) ) ;
await RequireCompletion ( entered . Task ) ;
Assert . That ( outbox . PendingCount , Is . EqualTo ( 1 ) ) ;
release . TrySetResult ( true ) ;
2026-08-18 17:47:34 +02:00
await WaitUntilAsync ( ( ) = > outbox . StateOf ( "one-shot" ) = =
EveUnityCultMeshCommandOutbox . DeliveryState . AwaitingCanonicalReceipt ) ;
Assert . That ( outbox . PendingCount , Is . EqualTo ( 1 ) ) ;
2026-08-18 10:08:03 +02:00
Assert . That ( sends , Is . EqualTo ( 1 ) ) ;
2026-08-18 17:47:34 +02:00
Assert . That ( outbox . Acknowledge ( "one-shot" ) , Is . True ) ;
Assert . That ( outbox . Acknowledge ( "one-shot" ) , Is . False ) ;
await WaitUntilAsync ( ( ) = > outbox . PendingCount = = 0 ) ;
2026-08-18 10:08:03 +02:00
}
[Test]
public async Task CommandOutboxCoalescesBlockedContinuousInputToTheLatestValue ( )
{
var entered = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
var release = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
var sent = new List < string > ( ) ;
using var outbox = new EveUnityCultMeshCommandOutbox (
async ( request , cancellationToken ) = >
{
lock ( sent ) sent . Add ( request . CommandId ) ;
entered . TrySetResult ( true ) ;
await AwaitOrCancel ( release . Task , cancellationToken ) ;
} ,
2026-08-18 17:47:34 +02:00
request = > request . PayloadFields [ "commandId" ] + "\u001f" + request . PayloadFields [ "entityId" ] ,
retryDelay : TimeSpan . FromMilliseconds ( 250 ) ) ;
2026-08-18 10:08:03 +02:00
outbox . Enqueue ( Request ( "move-0" , "move" ) ) ;
await RequireCompletion ( entered . Task ) ;
for ( var index = 1 ; index < = 1000 ; index + + )
outbox . Enqueue ( Request ( "move-" + index , "move" ) ) ;
Assert . That ( outbox . PendingCount , Is . LessThanOrEqualTo ( 2 ) ) ;
release . TrySetResult ( true ) ;
2026-08-18 17:47:34 +02:00
await WaitUntilAsync ( ( ) = > outbox . StateOf ( "move-1000" ) = =
EveUnityCultMeshCommandOutbox . DeliveryState . AwaitingCanonicalReceipt ) ;
Assert . That ( outbox . Acknowledge ( "move-1000" ) , Is . True ) ;
2026-08-18 10:08:03 +02:00
await WaitUntilAsync ( ( ) = > outbox . PendingCount = = 0 ) ;
lock ( sent )
{
CollectionAssert . AreEqual ( new [ ] { "move-0" , "move-1000" } , sent ) ;
}
}
2026-08-18 17:47:34 +02:00
[Test]
public async Task CommandOutboxRetriesSameIdWithoutLettingPoisonCommandStarveLaterWork ( )
{
var attempts = new List < string > ( ) ;
using var outbox = new EveUnityCultMeshCommandOutbox (
( request , _ ) = >
{
lock ( attempts ) attempts . Add ( request . CommandId ) ;
if ( request . CommandId = = "poison" ) throw new IOException ( "route unavailable" ) ;
return Task . CompletedTask ;
} ,
_ = > null ,
retryDelay : TimeSpan . FromMilliseconds ( 20 ) ,
maximumRetryDelay : TimeSpan . FromMilliseconds ( 40 ) ) ;
outbox . Enqueue ( Request ( "poison" , "dock" ) ) ;
outbox . Enqueue ( Request ( "healthy" , "undock" ) ) ;
await WaitUntilAsync ( ( ) = > outbox . StateOf ( "healthy" ) = =
EveUnityCultMeshCommandOutbox . DeliveryState . AwaitingCanonicalReceipt ) ;
Assert . That ( outbox . Acknowledge ( "healthy" ) , Is . True ) ;
lock ( attempts )
{
Assert . That ( attempts . IndexOf ( "healthy" ) , Is . GreaterThanOrEqualTo ( 0 ) ) ;
Assert . That ( attempts . FindAll ( value = > value = = "poison" ) , Is . Not . Empty ) ;
}
Assert . That ( outbox . PendingCount , Is . EqualTo ( 1 ) ) ;
}
[Test]
public async Task CommandOutboxRetransmitsUnacknowledgedCommandWithOriginalId ( )
{
var attempts = 0 ;
using var outbox = new EveUnityCultMeshCommandOutbox (
( request , _ ) = >
{
Assert . That ( request . CommandId , Is . EqualTo ( "retry-me" ) ) ;
Interlocked . Increment ( ref attempts ) ;
return Task . CompletedTask ;
} ,
_ = > null ,
retryDelay : TimeSpan . FromMilliseconds ( 20 ) ,
maximumRetryDelay : TimeSpan . FromMilliseconds ( 20 ) ) ;
outbox . Enqueue ( Request ( "retry-me" , "dock" ) ) ;
await WaitUntilAsync ( ( ) = > Volatile . Read ( ref attempts ) > = 2 ) ;
Assert . That ( outbox . PendingCount , Is . EqualTo ( 1 ) ) ;
Assert . That ( outbox . Acknowledge ( "retry-me" ) , Is . True ) ;
await WaitUntilAsync ( ( ) = > outbox . PendingCount = = 0 ) ;
}
2026-08-19 04:03:00 +02:00
[Test]
public void PendingReceiptIsObservedWithoutRetiringTheCommand ( )
{
using var transport = new EveUnityCultMeshLiveProviderTransport (
"test-cache" ,
"cultnet+tcp://127.0.0.1:1" ,
"test.verse" ,
"test.runtime" ,
"test.provider" ,
"test.surface" ) ;
var request = Request ( "launch-1" , "launch" ) ;
var pendingField = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetField (
"_pendingCommands" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ;
var publish = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"PublishReceipt" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ;
Assert . That ( pendingField , Is . Not . Null ) ;
Assert . That ( publish , Is . Not . Null ) ;
var pending = ( IDictionary < string , EveSurfaceCommandRequest > ) pendingField ! . GetValue ( transport ) ! ;
pending . Add ( request . CommandId , request ) ;
var observed = new List < EveUnitySceneCommandReceipt > ( ) ;
transport . CommandReceiptAvailable + = observed . Add ;
publish ! . Invoke ( transport , new object [ ] { Receipt ( request , "pending" , "receipt-pending" ) } ) ;
Assert . That ( pending . ContainsKey ( request . CommandId ) , Is . True ) ;
Assert . That ( observed . Select ( value = > value . State ) , Is . EqualTo ( new [ ] { "pending" } ) ) ;
2026-08-19 09:36:55 +02:00
publish . Invoke ( transport , new object [ ]
{
Receipt ( request , "accepted" , "receipt-accepted" , sourceVersion : 1000 )
} ) ;
2026-08-19 04:03:00 +02:00
Assert . That ( pending . ContainsKey ( request . CommandId ) , Is . False ) ;
Assert . That ( observed . Select ( value = > value . State ) , Is . EqualTo ( new [ ] { "pending" , "accepted" } ) ) ;
}
2026-08-19 09:10:37 +02:00
[Test]
public void TerminalReceiptWaitsForItsPresentationGenerationAndFreezesTheOldSurface ( )
{
using var transport = new EveUnityCultMeshLiveProviderTransport (
"test-cache" ,
"cultnet+tcp://127.0.0.1:1" ,
"test.verse" ,
"test.runtime" ,
"test.provider" ,
"test.surface" ) ;
var request = Request ( "select-verse-1" , "selectVerse" ) ;
var type = typeof ( EveUnityCultMeshLiveProviderTransport ) ;
var pending = ( IDictionary < string , EveSurfaceCommandRequest > ) type . GetField (
"_pendingCommands" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ! . GetValue ( transport ) ! ;
var publish = type . GetMethod ( "PublishReceipt" , BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
var complete = type . GetMethod (
"CompletePresentationTransition" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
type . GetField ( "_bootstrapped" , BindingFlags . NonPublic | BindingFlags . Instance ) !
. SetValue ( transport , true ) ;
2026-08-19 09:19:04 +02:00
complete . Invoke ( transport , new object [ ] { 1L } ) ;
2026-08-19 09:10:37 +02:00
pending . Add ( request . CommandId , request ) ;
var observed = new List < EveUnitySceneCommandReceipt > ( ) ;
transport . CommandReceiptAvailable + = observed . Add ;
2026-08-19 09:36:55 +02:00
using var outbox = new EveUnityCultMeshCommandOutbox (
( _ , _ ) = > Task . CompletedTask ,
_ = > null ,
retryDelay : TimeSpan . FromSeconds ( 30 ) ,
maximumRetryDelay : TimeSpan . FromSeconds ( 30 ) ) ;
type . GetField ( "_commandOutbox" , BindingFlags . NonPublic | BindingFlags . Instance ) !
. SetValue ( transport , outbox ) ;
outbox . Enqueue ( request ) ;
2026-08-19 09:10:37 +02:00
publish . Invoke ( transport , new object [ ]
{
2026-08-19 09:36:55 +02:00
Receipt (
request ,
"accepted" ,
"receipt-accepted" ,
sourceVersion : 1000 ,
presentationSurfaceVersion : 7 )
2026-08-19 09:10:37 +02:00
} ) ;
Assert . That ( pending . ContainsKey ( request . CommandId ) , Is . True ) ;
2026-08-19 09:36:55 +02:00
Assert . That ( outbox . PendingCount , Is . Zero ) ;
2026-08-19 09:10:37 +02:00
Assert . That ( observed , Is . Empty ) ;
2026-08-19 09:19:04 +02:00
type . GetProperty ( "CurrentSurfaceDocument" ) ! . SetValue ( transport , SceneSurface ( 100 ) ) ;
2026-08-19 09:10:37 +02:00
var blocked = Assert . Throws < InvalidOperationException > ( ( ) = >
transport . SubmitCommand ( Request ( "stale-launch" , "launch" ) ) ) ;
StringAssert . Contains ( "read-only" , blocked ! . Message ) ;
complete . Invoke ( transport , new object [ ] { 7L } ) ;
Assert . That ( pending . ContainsKey ( request . CommandId ) , Is . False ) ;
Assert . That ( observed . Select ( value = > value . State ) , Is . EqualTo ( new [ ] { "accepted" } ) ) ;
}
2026-08-19 04:25:32 +02:00
[Test]
public void ReceiptForAnotherInvocationCannotRetireTheCommand ( )
{
using var transport = new EveUnityCultMeshLiveProviderTransport (
"test-cache" ,
"cultnet+tcp://127.0.0.1:1" ,
"test.verse" ,
"test.runtime" ,
"test.provider" ,
"test.surface" ) ;
var request = Request ( "launch-1" , "launch" ) ;
var pendingField = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetField (
"_pendingCommands" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ;
var publish = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"PublishReceipt" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ;
var pending = ( IDictionary < string , EveSurfaceCommandRequest > ) pendingField ! . GetValue ( transport ) ! ;
pending . Add ( request . CommandId , request ) ;
var observed = new List < EveUnitySceneCommandReceipt > ( ) ;
transport . CommandReceiptAvailable + = observed . Add ;
var collision = new EveCommandReceiptDocument (
"receipt-collision" ,
request . CommandId ,
request . Command ,
"accepted" ,
"AetheriaEve" ,
"test.runtime" ,
request . ProviderId ,
request . SurfaceId ,
"" ,
DateTimeOffset . UtcNow . ToString ( "O" ) ,
1 ,
invocationHash : EveCommandInvocationHash . Compute ( Request ( request . CommandId , "another-payload" ) ) ) ;
publish ! . Invoke ( transport , new object [ ] { collision } ) ;
Assert . That ( pending . ContainsKey ( request . CommandId ) , Is . True ) ;
Assert . That ( observed , Is . Empty ) ;
}
2026-08-26 21:03:27 +02:00
[TestCase("provider", "other.provider")]
[TestCase("surface", "other.surface")]
[TestCase("authority", "other.runtime")]
[TestCase("state", "mysterious")]
public void UntrustedReceiptCannotRetireOrPublishTheCommand ( string field , string value )
{
using var transport = new EveUnityCultMeshLiveProviderTransport (
"test-cache" ,
"cultnet+tcp://127.0.0.1:1" ,
"test.verse" ,
"test.runtime" ,
"test.provider" ,
"test.surface" ) ;
var request = Request ( "launch-untrusted" , "launch" ) ;
var type = typeof ( EveUnityCultMeshLiveProviderTransport ) ;
var pending = ( IDictionary < string , EveSurfaceCommandRequest > ) type . GetField (
"_pendingCommands" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ! . GetValue ( transport ) ! ;
var publish = type . GetMethod ( "PublishReceipt" , BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
pending . Add ( request . CommandId , request ) ;
var observed = new List < EveUnitySceneCommandReceipt > ( ) ;
transport . CommandReceiptAvailable + = observed . Add ;
var providerId = field = = "provider" ? value : request . ProviderId ;
var surfaceId = field = = "surface" ? value : request . SurfaceId ;
var authority = field = = "authority" ? value : "test.runtime" ;
var state = field = = "state" ? value : "accepted" ;
var receipt = new EveCommandReceiptDocument (
"receipt-untrusted-" + field ,
request . CommandId ,
request . Command ,
state ,
"AetheriaEve" ,
authority ,
providerId ,
surfaceId ,
"" ,
DateTimeOffset . UtcNow . ToString ( "O" ) ,
1 ,
invocationHash : EveCommandInvocationHash . Compute ( request ) ) ;
publish . Invoke ( transport , new object [ ] { receipt } ) ;
Assert . That ( pending . ContainsKey ( request . CommandId ) , Is . True ) ;
Assert . That ( observed , Is . Empty ) ;
}
2026-08-17 21:02:01 +02:00
[Test]
public void ProviderSelectionCarriesStableIdentityWithoutExposingAPhysicalRoute ( )
{
var selection = new EveUnityCultMeshProviderSelection (
"cultnet+tcp://odin.example:3075" ,
2026-08-17 22:43:10 +02:00
"aetheria.public" ,
2026-08-17 21:02:01 +02:00
"aetheria.public" ,
"aetheria.daemon" ,
"aetheria.pilot" ,
"interactive-world" ) ;
Assert . That ( selection . RendezvousEndpoint , Is . EqualTo ( "cultnet+tcp://odin.example:3075" ) ) ;
Assert . That ( selection . VerseId , Is . EqualTo ( "aetheria.public" ) ) ;
2026-08-18 07:27:13 +02:00
Assert . That ( selection . AuthorityRuntimeId , Is . EqualTo ( "aetheria.public" ) ) ;
2026-08-17 21:02:01 +02:00
Assert . That ( typeof ( EveUnityCultMeshProviderSelection ) . GetProperty ( "Endpoint" ) , Is . Null ) ;
}
2026-08-19 03:31:16 +02:00
[Test]
public void NavigationAuthorityCannotBeReplacedByAlphabeticallyEarlierPeer ( )
{
var target = new EveUnitySceneNavigationTarget (
"gamecult.aetheria" ,
"aetheria.daemon" ,
"aetheria.pilot" ,
"interactive-world" ,
new [ ] { "cultnet+tcp://odin.example:3075" } ,
"z-owner" ) ;
var eligible = EveUnityCultMeshProviderDiscovery . EligibleAuthorityRuntimeIds (
new [ ] { "a-decoy" , "z-owner" } ,
target . AuthorityRuntimeId ) ;
Assert . That ( target . AuthorityRuntimeId , Is . EqualTo ( "z-owner" ) ) ;
CollectionAssert . AreEqual ( new [ ] { "z-owner" } , eligible ) ;
}
2026-08-18 10:08:03 +02:00
private static EveSurfaceCommandRequest Request ( string id , string command ) = > new EveSurfaceCommandRequest (
"test.provider" ,
"test.surface" ,
CultMesh . OperationInvocation ( "test.command" , idempotencyKey : id ) ,
CultMesh . OperationPayload ( new Dictionary < string , string >
{
["commandId"] = command ,
["entityId"] = "ship"
} ) ,
DateTimeOffset . UtcNow ,
"test-client" ) ;
2026-08-19 04:03:00 +02:00
private static EveCommandReceiptDocument Receipt (
EveSurfaceCommandRequest request ,
string state ,
2026-08-19 09:10:37 +02:00
string receiptId ,
2026-08-19 09:36:55 +02:00
long sourceVersion = 0 ,
long presentationSurfaceVersion = 0 ) = > new EveCommandReceiptDocument (
2026-08-19 04:03:00 +02:00
receiptId ,
request . CommandId ,
request . Command ,
state ,
"AetheriaEve" ,
"test.runtime" ,
request . ProviderId ,
request . SurfaceId ,
"" ,
DateTimeOffset . UtcNow . ToString ( "O" ) ,
2026-08-19 09:10:37 +02:00
sourceVersion ,
2026-08-19 09:36:55 +02:00
invocationHash : EveCommandInvocationHash . Compute ( request ) ,
presentationSurfaceVersion : presentationSurfaceVersion ) ;
[Test]
2026-08-19 09:56:36 +02:00
public void FailedNewestCandidateCanRetryWithoutAllowingAnOlderSurface ( )
2026-08-19 09:36:55 +02:00
{
using var transport = new EveUnityCultMeshLiveProviderTransport (
"test-cache" ,
"cultnet+tcp://127.0.0.1:1" ,
"test.verse" ,
"test.runtime" ,
"test.provider" ,
"test.surface" ) ;
2026-08-19 09:56:36 +02:00
var begin = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"TryBeginBaseSurfaceCandidate" ,
2026-08-19 09:36:55 +02:00
BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
2026-08-19 09:56:36 +02:00
var retire = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"RetireFailedBaseSurfaceCandidate" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
var first = new object [ ] { 10L , - 1L } ;
var older = new object [ ] { 7L , - 1L } ;
var duplicate = new object [ ] { 10L , - 1L } ;
Assert . That ( begin . Invoke ( transport , first ) , Is . True ) ;
Assert . That ( begin . Invoke ( transport , older ) , Is . False ) ;
Assert . That ( begin . Invoke ( transport , duplicate ) , Is . False ) ;
retire . Invoke ( transport , new [ ] { first [ 0 ] , first [ 1 ] } ) ;
var retry = new object [ ] { 10L , - 1L } ;
Assert . That ( begin . Invoke ( transport , retry ) , Is . True ) ;
Assert . That ( ( long ) retry [ 1 ] , Is . GreaterThan ( ( long ) first [ 1 ] ) ) ;
Assert . That ( begin . Invoke ( transport , new object [ ] { 9L , - 1L } ) , Is . False ) ;
2026-08-19 09:36:55 +02:00
}
2026-08-19 04:03:00 +02:00
2026-08-19 10:04:30 +02:00
[Test]
public async Task FailedAssetPreparationRunsBoundedRetryPolicyAndRetiresCandidate ( )
{
using var transport = new EveUnityCultMeshLiveProviderTransport (
"test-cache" ,
"cultnet+tcp://127.0.0.1:1" ,
"test.verse" ,
"test.runtime" ,
"test.provider" ,
"test.surface" ) ;
var type = typeof ( EveUnityCultMeshLiveProviderTransport ) ;
var begin = type . GetMethod (
"TryBeginBaseSurfaceCandidate" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
var refresh = type . GetMethod (
"RefreshAssetsForSurfaceAsync" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
var attempts = type . GetField (
"_lastAssetPresentationAttemptCount" ,
BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
var first = new object [ ] { 10L , - 1L } ;
Assert . That ( begin . Invoke ( transport , first ) , Is . True ) ;
var task = ( Task ) refresh . Invoke ( transport , new object [ ]
{
SceneSurface ( 10 ) . SurfaceDocument ,
CreateAssetSource ( "test.verse" , "test.runtime" , "test.provider" , "catalog" , 41 ) ,
( long ) first [ 1 ] ,
CancellationToken . None
} ) ! ;
await task ;
Assert . That ( attempts . GetValue ( transport ) , Is . EqualTo ( 3 ) ) ;
Assert . That ( begin . Invoke ( transport , new object [ ] { 10L , - 1L } ) , Is . True ) ;
}
2026-08-19 09:19:04 +02:00
private static EveUnitySceneProviderSurfaceDocument SceneSurface ( long version )
{
var surface = new EveSurfaceDocument (
"test.provider" ,
"" ,
"" ,
version ,
"" ,
new EveSurfaceTree (
"test.surface" ,
new EveSurfaceComponent (
"root" ,
"surface" ,
new Dictionary < string , string > ( ) ,
Array . Empty < EveSurfaceComponent > ( ) ) ,
Array . Empty < EveStyleToken > ( ) ) ,
Array . Empty < EveCommandTemplate > ( ) ) ;
return new EveUnitySceneProviderSurfaceDocument (
surface ,
new EveUnitySceneProviderSurfaceAdvertisement (
"test.surface" ,
"interactive-world" ,
new EveUnitySceneWorldInteraction ( "" , "" , EveCommandReceiptDocument . SchemaId , "" ) ) ,
"test:surface" ,
version ) ;
}
2026-08-18 10:08:03 +02:00
private static async Task AwaitOrCancel ( Task task , CancellationToken cancellationToken )
{
var cancelled = Task . Delay ( Timeout . Infinite , cancellationToken ) ;
if ( await Task . WhenAny ( task , cancelled ) ! = task )
cancellationToken . ThrowIfCancellationRequested ( ) ;
await task ;
}
private static async Task RequireCompletion ( Task task )
{
if ( await Task . WhenAny ( task , Task . Delay ( TimeSpan . FromSeconds ( 2 ) ) ) ! = task )
Assert . Fail ( "Timed out waiting for asynchronous command delivery." ) ;
await task ;
}
private static async Task WaitUntilAsync ( Func < bool > predicate )
{
var deadline = DateTimeOffset . UtcNow + TimeSpan . FromSeconds ( 2 ) ;
while ( ! predicate ( ) & & DateTimeOffset . UtcNow < deadline )
await Task . Delay ( 10 ) ;
Assert . That ( predicate ( ) , Is . True , "Timed out waiting for the command outbox to drain." ) ;
}
2026-08-17 21:02:01 +02:00
[Test]
public void LiveTransportOwnsOneCultMeshClientAndNoDirectSnapshotReplica ( )
{
var fields = typeof ( EveUnityCultMeshLiveProviderTransport )
. GetFields ( BindingFlags . Instance | BindingFlags . NonPublic ) ;
var names = Array . ConvertAll ( fields , field = > field . Name ) ;
CollectionAssert . Contains ( names , "_meshClient" ) ;
CollectionAssert . DoesNotContain ( names , "_node" ) ;
CollectionAssert . DoesNotContain ( names , "_snapshot" ) ;
CollectionAssert . DoesNotContain ( names , "_networkRegistry" ) ;
CollectionAssert . DoesNotContain ( names , "_replicaShard" ) ;
CollectionAssert . DoesNotContain ( names , "_subscriptions" ) ;
CollectionAssert . DoesNotContain ( names , "_entitySubscriptions" ) ;
}
2026-07-19 19:35:49 +01:00
[Test]
public void UnavailableTcpRendezvousIsReportedAsRetryablePreparationFailure ( )
{
var listener = new TcpListener ( IPAddress . Loopback , 0 ) ;
listener . Start ( ) ;
var port = ( ( IPEndPoint ) listener . LocalEndpoint ) . Port ;
listener . Stop ( ) ;
var error = Assert . ThrowsAsync < InvalidOperationException > ( ( ) = >
new EveUnityCultMeshProviderDiscovery ( ) . DiscoverAsync ( $"cultnet+tcp://127.0.0.1:{port}" ) ) ;
StringAssert . Contains ( "Could not query CultMesh rendezvous endpoint" , error ! . Message ) ;
Assert . That ( error . InnerException , Is . TypeOf < SocketException > ( ) ) ;
}
2026-07-18 18:38:07 +01:00
[Test]
public void UnpreparedProviderGetterFailsFastWithoutStartingNetworkDiscovery ( )
{
var gameObject = new GameObject ( "unprepared-eve-provider" ) ;
try
{
var provider = gameObject . AddComponent < EveUnityCultMeshPlayableWorldProvider > ( ) ;
provider . Configure ( "rudp://127.0.0.1:1" ) ;
var stopwatch = Stopwatch . StartNew ( ) ;
var error = Assert . Throws < InvalidOperationException > ( ( ) = > _ = provider . CurrentInputCapability ) ;
stopwatch . Stop ( ) ;
StringAssert . Contains ( "Await PrepareAsync" , error ! . Message ) ;
Assert . That ( stopwatch . ElapsedMilliseconds , Is . LessThan ( 100 ) ) ;
Assert . That ( provider . Selection , Is . Null ) ;
}
finally
{
UnityEngine . Object . DestroyImmediate ( gameObject ) ;
}
}
2026-07-20 04:58:49 +01:00
[Test]
public void ProviderSubscriptionsDoNotDependOnPreparedTransportLifetime ( )
{
var gameObject = new GameObject ( "unprepared-eve-provider-subscriptions" ) ;
var provider = gameObject . AddComponent < EveUnityCultMeshPlayableWorldProvider > ( ) ;
Action < EveUnitySceneProviderSurfaceDocument > documentHandler = _ = > { } ;
Action < EveUnitySceneCommandReceipt > receiptHandler = _ = > { } ;
Assert . DoesNotThrow ( ( ) = > provider . DocumentAvailable + = documentHandler ) ;
Assert . DoesNotThrow ( ( ) = > provider . ReceiptAvailable + = receiptHandler ) ;
Assert . DoesNotThrow ( ( ) = > provider . DocumentAvailable - = documentHandler ) ;
Assert . DoesNotThrow ( ( ) = > provider . ReceiptAvailable - = receiptHandler ) ;
Assert . DoesNotThrow ( ( ) = > UnityEngine . Object . DestroyImmediate ( gameObject ) ) ;
}
2026-07-13 23:44:55 +01:00
[Test]
public void ResolvesUnityNormalizedBundleNamesWithoutChangingLogicalAssetIdentity ( )
{
var method = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"ResolveBundleAssetName" ,
BindingFlags . Static | BindingFlags . NonPublic ) ;
Assert . That ( method , Is . Not . Null ) ;
var result = method ! . Invoke ( null , new object [ ]
{
new [ ] { "assets/generated/eve/thermal/death.asset" } ,
"Assets/Generated/Eve/Thermal/Death.asset"
} ) ;
Assert . That ( result , Is . EqualTo ( "assets/generated/eve/thermal/death.asset" ) ) ;
}
[Test]
public void RejectsBundleNamesThatDoNotMatchTheAdvertisedAsset ( )
{
var method = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"ResolveBundleAssetName" ,
BindingFlags . Static | BindingFlags . NonPublic ) ;
var result = method ! . Invoke ( null , new object [ ]
{
new [ ] { "assets/generated/eve/thermal/heatstroke.asset" } ,
"Assets/Generated/Eve/Thermal/Death.asset"
} ) ;
Assert . That ( result , Is . Null ) ;
}
2026-07-15 19:59:37 +01:00
[Test]
public void SelectedRuntimeVariantOwnsConcreteNativeProgramBindings ( )
{
var method = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"MergeAssetMetadata" ,
BindingFlags . Static | BindingFlags . NonPublic ) ;
Assert . That ( method , Is . Not . Null ) ;
var result = method ! . Invoke ( null , new object [ ]
{
new Dictionary < string , string > { [ "presentationRole" ] = "environment.volume" } ,
new Dictionary < string , string >
{
["unity.volume.texturePort.surfaceHeight"] = "_NebulaSurfaceHeight" ,
["unity.volume.pass.raymarch"] = "0"
}
} ) as IReadOnlyDictionary < string , string > ;
Assert . That ( result , Is . Not . Null ) ;
Assert . That ( result ! [ "presentationRole" ] , Is . EqualTo ( "environment.volume" ) ) ;
Assert . That ( result [ "unity.volume.texturePort.surfaceHeight" ] , Is . EqualTo ( "_NebulaSurfaceHeight" ) ) ;
Assert . That ( result [ "unity.volume.pass.raymarch" ] , Is . EqualTo ( "0" ) ) ;
}
2026-07-19 21:21:52 +01:00
[Test]
2026-08-19 07:57:33 +02:00
public void ConfiguringCandidateCatalogRegistersBundlesWithoutMaterializingThem ( )
2026-07-19 21:21:52 +01:00
{
var platformMethod = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"CurrentBundlePlatform" ,
BindingFlags . Static | BindingFlags . NonPublic ) ;
2026-08-19 07:57:33 +02:00
var configureMethod = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"ConfigureAssetCatalog" ,
BindingFlags . Static | BindingFlags . NonPublic ) ;
2026-07-19 21:21:52 +01:00
Assert . That ( platformMethod , Is . Not . Null ) ;
2026-08-19 07:57:33 +02:00
Assert . That ( configureMethod , Is . Not . Null ) ;
2026-07-19 21:21:52 +01:00
var variant = new EveAssetVariant (
"unity-scene" ,
( string ) platformMethod ! . Invoke ( null , Array . Empty < object > ( ) ) ! ,
"unity-assetbundle" ,
"cdn:manifest:lazy" ,
"sha256:00" ,
1 ,
"assets/prefab.prefab" ) ;
var catalog = new EveAssetCatalogDocument (
"provider" ,
"catalog" ,
1 ,
DateTimeOffset . UtcNow . ToString ( "O" ) ,
new [ ] { new EveAssetCatalogEntry ( "prefab.entity" , "prefab" , new [ ] { variant } ) } ) ;
2026-08-19 07:57:33 +02:00
var candidate = CreateAssetGeneration ( "verse" , "authority" , "provider" , "catalog" , "candidate" , 1 ) ;
2026-07-19 21:21:52 +01:00
2026-08-19 07:57:33 +02:00
configureMethod ! . Invoke ( null , new [ ] { candidate , catalog } ) ;
2026-07-19 21:21:52 +01:00
2026-08-19 07:57:33 +02:00
Assert . That ( GenerationCollection ( candidate , "AssetBundles" ) . Count , Is . Zero ) ;
Assert . That ( GenerationDictionary ( candidate , "AssetSelections" ) . Count , Is . EqualTo ( 1 ) ) ;
( ( IDisposable ) candidate ) . Dispose ( ) ;
2026-07-19 21:21:52 +01:00
}
2026-08-19 07:57:33 +02:00
[Test]
public void FailedAssetCandidatePreservesCommittedGeneration ( )
{
var transportType = typeof ( EveUnityCultMeshLiveProviderTransport ) ;
var generationField = transportType . GetField (
"_assetGeneration" ,
BindingFlags . Instance | BindingFlags . NonPublic ) ! ;
2026-08-19 08:18:57 +02:00
var configureMethod = transportType . GetMethod (
"ConfigureAssetCatalog" ,
BindingFlags . Static | BindingFlags . NonPublic ) ! ;
2026-08-19 07:57:33 +02:00
using var transport = new EveUnityCultMeshLiveProviderTransport (
Path . Combine ( Path . GetTempPath ( ) , $"eve-asset-generation-{Guid.NewGuid():N}.cc" ) ,
"cultnet+tcp://127.0.0.1:1" ,
"verse-a" ,
"authority-a" ,
"provider-a" ,
"surface-a" ) ;
var initial = ( IDisposable ) generationField . GetValue ( transport ) ! ;
var committed = CreateAssetGeneration (
"verse-a" , "authority-a" , "provider-a" , "catalog-a" , "source-a" , 1 ) ;
var prefab = new GameObject ( "committed-a" ) ;
GenerationDictionary ( committed , "Prefabs" ) [ "prefab.a" ] = prefab ;
initial . Dispose ( ) ;
generationField . SetValue ( transport , committed ) ;
var invalidVariant = new EveAssetVariant (
"browser" ,
"web" ,
"url" ,
"https://example.invalid/a" ,
"sha256:00" ,
1 ,
"prefab.a" ) ;
var invalidCatalog = new EveAssetCatalogDocument (
"provider-b" ,
"catalog-b" ,
2 ,
DateTimeOffset . UtcNow . ToString ( "O" ) ,
new [ ] { new EveAssetCatalogEntry ( "prefab.b" , "prefab" , new [ ] { invalidVariant } ) } ) ;
2026-08-19 08:18:57 +02:00
var rejected = CreateAssetGeneration (
"verse-b" , "authority-b" , "provider-b" , "catalog-b" , "source-b" , - 1 ) ;
var error = Assert . Throws < TargetInvocationException > ( ( ) = >
configureMethod . Invoke ( null , new [ ] { rejected , invalidCatalog } ) ) ;
Assert . That ( error ! . InnerException , Is . TypeOf < InvalidOperationException > ( ) ) ;
( ( IDisposable ) rejected ) . Dispose ( ) ;
2026-08-19 07:57:33 +02:00
Assert . That ( generationField . GetValue ( transport ) , Is . SameAs ( committed ) ) ;
Assert . That ( GenerationProperty < string > ( committed , "SourceIdentity" ) , Is . EqualTo ( "source-a" ) ) ;
Assert . That ( GenerationDictionary ( committed , "Prefabs" ) [ "prefab.a" ] , Is . SameAs ( prefab ) ) ;
UnityEngine . Object . DestroyImmediate ( prefab ) ;
}
2026-08-19 08:18:57 +02:00
[Test]
public void CandidateCatalogObservationSurvivesPrecommitWindow ( )
{
var candidate = CreateAssetGeneration (
"verse-b" , "authority-b" , "provider-b" , "catalog-b" , "source-b" , 1 ) ;
var candidateType = candidate . GetType ( ) ;
var observe = candidateType . GetMethod ( "ObserveCatalog" ) ! ;
var activate = candidateType . GetMethod ( "ActivateCatalogObservation" ) ! ;
var variant = new EveAssetVariant (
"unity-scene" , "StandaloneWindows64" , "url" , "https://example.invalid/b" , "sha256:01" , 1 , "prefab.b" ) ;
var catalogB = new EveAssetCatalogDocument (
"provider-b" , "catalog-b" , 2 , DateTimeOffset . UtcNow . ToString ( "O" ) ,
new [ ] { new EveAssetCatalogEntry ( "prefab.b" , "prefab" , new [ ] { variant } ) } ) ;
Assert . That ( observe . Invoke ( candidate , new object [ ] { catalogB } ) , Is . Null ) ;
Assert . That ( activate . Invoke ( candidate , Array . Empty < object > ( ) ) , Is . SameAs ( catalogB ) ) ;
( ( IDisposable ) candidate ) . Dispose ( ) ;
}
2026-08-19 09:50:50 +02:00
[Test]
public void PinnedSurfaceCatalogDoesNotAutoAdvanceWithoutANewerBaseSurface ( )
{
var transportType = typeof ( EveUnityCultMeshLiveProviderTransport ) ;
var generationField = transportType . GetField (
"_assetGeneration" ,
BindingFlags . Instance | BindingFlags . NonPublic ) ! ;
var pendingField = transportType . GetField (
"_pendingAssetCatalog" ,
BindingFlags . Instance | BindingFlags . NonPublic ) ! ;
var queue = transportType . GetMethod (
"QueueAssetCatalogUpdate" ,
BindingFlags . Instance | BindingFlags . NonPublic ) ! ;
using var transport = new EveUnityCultMeshLiveProviderTransport (
Path . Combine ( Path . GetTempPath ( ) , $"eve-pinned-catalog-{Guid.NewGuid():N}.cc" ) ,
"cultnet+tcp://127.0.0.1:1" ,
"verse-a" ,
"authority-a" ,
"provider-a" ,
"surface-a" ) ;
var initial = ( IDisposable ) generationField . GetValue ( transport ) ! ;
var committed = CreateAssetGeneration (
"verse-a" , "authority-a" , "provider-a" , "catalog-a" , "source-a" , 41 , 41 ) ;
initial . Dispose ( ) ;
generationField . SetValue ( transport , committed ) ;
var catalog42 = new EveAssetCatalogDocument (
"provider-a" ,
"catalog-a" ,
42 ,
DateTimeOffset . UtcNow . ToString ( "O" ) ,
Array . Empty < EveAssetCatalogEntry > ( ) ) ;
queue . Invoke ( transport , new object [ ] { "source-a" , catalog42 } ) ;
Assert . That ( GenerationProperty < long > ( generationField . GetValue ( transport ) ! , "CatalogVersion" ) , Is . EqualTo ( 41 ) ) ;
var committedSource = GenerationProperty < object > ( generationField . GetValue ( transport ) ! , "Source" ) ;
Assert . That ( GenerationProperty < long > ( committedSource , "RequiredCatalogVersion" ) , Is . EqualTo ( 41 ) ) ;
Assert . That ( pendingField . GetValue ( transport ) , Is . Null ) ;
}
2026-08-19 10:14:37 +02:00
[Test]
public void PinnedSurfaceRejectsWrongImmutableCatalogRecordImmediately ( )
{
var require = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"RequireCatalogVersion" ,
BindingFlags . Static | BindingFlags . NonPublic ) ! ;
var catalog42 = new EveAssetCatalogDocument (
"provider-a" ,
"eve:assets:aetheria.daemon:version:42" ,
42 ,
DateTimeOffset . UtcNow . ToString ( "O" ) ,
Array . Empty < EveAssetCatalogEntry > ( ) ) ;
var error = Assert . Throws < TargetInvocationException > ( ( ) = > require . Invoke (
null ,
new object [ ] { catalog42 , 41L , "eve:assets:aetheria.daemon:version:41" } ) ) ;
Assert . That ( error ! . InnerException , Is . TypeOf < InvalidDataException > ( ) ) ;
Assert . That ( error . InnerException ! . Message , Does . Contain ( "requires immutable version 41" ) ) ;
}
2026-08-19 08:09:59 +02:00
[Test]
public void CrossTargetAssetsUseConfiguredRemoteTrust ( )
{
var localTrust = new CultMeshAuthorityTrustPolicy ( CultMeshAuthorityTrustMode . LocalDevelopment ) ;
var remoteTrust = new CultMeshAuthorityTrustPolicy ( CultMeshAuthorityTrustMode . AuthenticatedRemote ) ;
using var transport = new EveUnityCultMeshLiveProviderTransport (
Path . Combine ( Path . GetTempPath ( ) , $"eve-asset-trust-{Guid.NewGuid():N}.cc" ) ,
"cultnet+tcp://127.0.0.1:1" ,
"verse-a" ,
"authority-a" ,
"provider-a" ,
"surface-a" ,
authorityTrust : localTrust ,
crossTargetAuthorityTrust : remoteTrust ) ;
var trustMethod = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetMethod (
"AssetAuthorityTrust" ,
BindingFlags . Instance | BindingFlags . NonPublic ) ! ;
var localSource = CreateAssetSource ( "verse-a" , "authority-a" , "provider-a" , "catalog-a" ) ;
var remoteSource = CreateAssetSource ( "verse-b" , "authority-b" , "provider-b" , "catalog-b" ) ;
Assert . That ( trustMethod . Invoke ( transport , new [ ] { localSource } ) , Is . SameAs ( localTrust ) ) ;
Assert . That ( trustMethod . Invoke ( transport , new [ ] { remoteSource } ) , Is . SameAs ( remoteTrust ) ) ;
}
2026-08-19 07:57:33 +02:00
private static object CreateAssetSource (
string verseId ,
string authorityRuntimeId ,
string providerId ,
2026-08-19 09:50:50 +02:00
string manifestRecordRef ,
long requiredCatalogVersion = 0 )
2026-08-19 07:57:33 +02:00
{
var type = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetNestedType (
"AssetSource" ,
BindingFlags . NonPublic ) ! ;
return Activator . CreateInstance (
type ,
BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ,
null ,
new object [ ]
{
new CultMeshSessionTarget ( verseId , authorityRuntimeId ) ,
providerId ,
manifestRecordRef ,
2026-08-19 09:50:50 +02:00
Array . Empty < string > ( ) ,
requiredCatalogVersion
2026-08-19 07:57:33 +02:00
} ,
null ) ! ;
}
private static object CreateAssetGeneration (
string verseId ,
string authorityRuntimeId ,
string providerId ,
string manifestRecordRef ,
string sourceIdentity ,
2026-08-19 09:50:50 +02:00
long catalogVersion ,
long requiredCatalogVersion = 0 )
2026-08-19 07:57:33 +02:00
{
var type = typeof ( EveUnityCultMeshLiveProviderTransport ) . GetNestedType (
"AssetGeneration" ,
BindingFlags . NonPublic ) ! ;
return Activator . CreateInstance (
type ,
BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ,
null ,
new [ ]
{
2026-08-19 09:50:50 +02:00
CreateAssetSource ( verseId , authorityRuntimeId , providerId , manifestRecordRef , requiredCatalogVersion ) ,
2026-08-19 07:57:33 +02:00
sourceIdentity ,
catalogVersion ,
null
} ,
null ) ! ;
}
private static System . Collections . IList GenerationCollection ( object generation , string property ) = >
( System . Collections . IList ) generation . GetType ( ) . GetProperty ( property ) ! . GetValue ( generation ) ! ;
private static System . Collections . IDictionary GenerationDictionary ( object generation , string property ) = >
( System . Collections . IDictionary ) generation . GetType ( ) . GetProperty ( property ) ! . GetValue ( generation ) ! ;
private static T GenerationProperty < T > ( object generation , string property ) = >
( T ) generation . GetType ( ) . GetProperty ( property ) ! . GetValue ( generation ) ! ;
2026-07-13 23:44:55 +01:00
}
}