using System.Runtime.InteropServices;
using System.Text.Json;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
static uint GalaxyFilterFactsQueueSelfTest()
{
static JsonElement Request(string text){using var document=JsonDocument.Parse(text);return document.RootElement.Clone();}
lock(galaxyFactsMutex)
{
var savedWork=galaxyFactsWork;var savedGeneration=galaxyFactsGeneration;
try
{
uint passed=0;galaxyFactsWork=new();galaxyFactsGeneration=0;
JsonElement Wire(string op,string request)=>Request(GalaxyFilterFactsRequest(op,Request(request)).ToJsonString());
var initial=Wire("galaxy.facts","{\"systemAddresses\":[\"9007199254740995\",\"9007199254740995\",\"300\"]}");
if(initial.GetProperty("generation").GetUInt64()==1&&!initial.GetProperty("done").GetBoolean()&&initial.GetProperty("items").GetArrayLength()==0&&
initial.GetProperty("requested").GetInt32()==2&&galaxyFactsWork.addresses[0]==9007199254740995UL&&!galaxyFactsWork.includeColonisation)passed|=1;
var rejected=0;
foreach(var bad in new[]{"{\"systemAddresses\":[300]}","{\"systemAddresses\":[\"0\"]}","{\"systemAddresses\":[\"36028797018963968\"]}","{\"systemAddresses\":[]}"})
try{Wire("galaxy.facts",bad);}catch(InvalidDataException){rejected++;}
try{Wire("galaxy.facts","{\"systemAddresses\":["+string.Join(",",Enumerable.Repeat("\"300\"",4097))+"]}");}catch(InvalidDataException){rejected++;}
if(rejected==5)passed|=2;
var unknown=new GalaxyFilterFacts();galaxyFactsWork.items.Add((9007199254740995UL,new(false,"dataavailable","havedata",true,3,0,5)));galaxyFactsWork.items.Add((300,unknown));
var page=Wire("galaxy.facts.result","{\"generation\":1,\"limit\":1}");var first=page.GetProperty("items")[0];
var second=Wire("galaxy.facts.result","{\"generation\":1,\"offset\":1}").GetProperty("items")[0];
if(first.GetProperty("systemAddress").GetString()=="9007199254740995"&&!first.GetProperty("visited").GetBoolean()&&
first.GetProperty("metadataStatus").GetInt32()==3&&first.GetProperty("colonisationStatus").GetInt32()==0&&first.GetProperty("colonisationNormalStatus").GetInt32()==5&&
second.GetProperty("colonisationNormalStatus").ValueKind==JsonValueKind.Null&&second.GetProperty("colonisationStatus").ValueKind==JsonValueKind.Null&&second.GetProperty("visited").ValueKind==JsonValueKind.Null&&second.GetProperty("tradeData").ValueKind==JsonValueKind.Null)passed|=4;
galaxyFactsWork.items=Enumerable.Range(0,200).Select(i=>((ulong)i+1,unknown)).ToList();
if(Wire("galaxy.facts.result","{\"generation\":1,\"limit\":9999}").GetProperty("items").GetArrayLength()==96)passed|=8;
var cancelled=Wire("galaxy.facts.cancel","{\"generation\":1}");var old=galaxyFactsWork;
Wire("galaxy.facts","{\"systemAddresses\":[\"300\"],\"includeColonisation\":true}");var mismatch=false;
try{Wire("galaxy.facts.result","{\"generation\":1}");}catch(InvalidOperationException){mismatch=true;}
if(cancelled.GetProperty("done").GetBoolean()&&cancelled.GetProperty("cancelled").GetBoolean()&&old.done&&mismatch&&galaxyFactsWork.generation==2&&galaxyFactsWork.includeColonisation)passed|=16;
var frame=new Snapshot{valid=true,entity=42,referenceWorld=100};var work=new GalaxyFilterFactsWork{expires=100};
var bound=BindGalaxyFilterFactsWork(work,frame,1);frame.referenceWorld=101;
if(bound&&!BindGalaxyFilterFactsWork(work,frame,2)&&work.done&&work.error.Contains("changed")&&work.contextSystem==100)passed|=32;
work=new(){expires=100};if(!BindGalaxyFilterFactsWork(work,frame,100)&&work.done&&work.error.Contains("deadline"))passed|=64;
var positions=new Dictionary<ulong,double[]>();var order=new Queue<ulong>();
var position=new[]{1d,2d,3d};CopyNativeGalaxyFactPosition(new(){id=300,position=position},positions,order);position[0]=999;
CopyNativeGalaxyFactPosition(new(){id=0,position=[1,2,3]},positions,order);CopyNativeGalaxyFactPosition(new(){id=301,position=[double.NaN,2,3]},positions,order);
if(positions.Count==1&&positions[300][0]==1&&!positions.ContainsKey(301))passed|=128;
return passed;
}
finally{galaxyFactsWork=savedWork;galaxyFactsGeneration=savedGeneration;}
}
}
#if PLUGIN_NAVIGATION
[UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyFilterFactsQueueSelfTest")]
internal static uint ExportGalaxyFilterFactsQueueSelfTest()=>RunTest(GalaxyFilterFactsQueueSelfTest);
#endif
}
using System.Runtime.InteropServices;
using System.Text.Json;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
static uint GalaxyFilterFactsQueueSelfTest()
{
static JsonElement Request(string text){using var document=JsonDocument.Parse(text);return document.RootElement.Clone();}
lock(galaxyFactsMutex)
{
var savedWork=galaxyFactsWork;var savedGeneration=galaxyFactsGeneration;
try
{
uint passed=0;galaxyFactsWork=new();galaxyFactsGeneration=0;
JsonElement Wire(string op,string request)=>Request(GalaxyFilterFactsRequest(op,Request(request)).ToJsonString());
var initial=Wire("galaxy.facts","{\"systemAddresses\":[\"9007199254740995\",\"9007199254740995\",\"300\"]}");
if(initial.GetProperty("generation").GetUInt64()==1&&!initial.GetProperty("done").GetBoolean()&&initial.GetProperty("items").GetArrayLength()==0&&
initial.GetProperty("requested").GetInt32()==2&&galaxyFactsWork.addresses[0]==9007199254740995UL&&!galaxyFactsWork.includeColonisation)passed|=1;
var rejected=0;
foreach(var bad in new[]{"{\"systemAddresses\":[300]}","{\"systemAddresses\":[\"0\"]}","{\"systemAddresses\":[\"36028797018963968\"]}","{\"systemAddresses\":[]}"})
try{Wire("galaxy.facts",bad);}catch(InvalidDataException){rejected++;}
try{Wire("galaxy.facts","{\"systemAddresses\":["+string.Join(",",Enumerable.Repeat("\"300\"",4097))+"]}");}catch(InvalidDataException){rejected++;}
if(rejected==5)passed|=2;
var unknown=new GalaxyFilterFacts();galaxyFactsWork.items.Add((9007199254740995UL,new(false,"dataavailable","havedata",true,3,0,5)));galaxyFactsWork.items.Add((300,unknown));
var page=Wire("galaxy.facts.result","{\"generation\":1,\"limit\":1}");var first=page.GetProperty("items")[0];
var second=Wire("galaxy.facts.result","{\"generation\":1,\"offset\":1}").GetProperty("items")[0];
if(first.GetProperty("systemAddress").GetString()=="9007199254740995"&&!first.GetProperty("visited").GetBoolean()&&
first.GetProperty("metadataStatus").GetInt32()==3&&first.GetProperty("colonisationStatus").GetInt32()==0&&first.GetProperty("colonisationNormalStatus").GetInt32()==5&&
second.GetProperty("colonisationNormalStatus").ValueKind==JsonValueKind.Null&&second.GetProperty("colonisationStatus").ValueKind==JsonValueKind.Null&&second.GetProperty("visited").ValueKind==JsonValueKind.Null&&second.GetProperty("tradeData").ValueKind==JsonValueKind.Null)passed|=4;
galaxyFactsWork.items=Enumerable.Range(0,200).Select(i=>((ulong)i+1,unknown)).ToList();
if(Wire("galaxy.facts.result","{\"generation\":1,\"limit\":9999}").GetProperty("items").GetArrayLength()==96)passed|=8;
var cancelled=Wire("galaxy.facts.cancel","{\"generation\":1}");var old=galaxyFactsWork;
Wire("galaxy.facts","{\"systemAddresses\":[\"300\"],\"includeColonisation\":true}");var mismatch=false;
try{Wire("galaxy.facts.result","{\"generation\":1}");}catch(InvalidOperationException){mismatch=true;}
if(cancelled.GetProperty("done").GetBoolean()&&cancelled.GetProperty("cancelled").GetBoolean()&&old.done&&mismatch&&galaxyFactsWork.generation==2&&galaxyFactsWork.includeColonisation)passed|=16;
var frame=new Snapshot{valid=true,entity=42,referenceWorld=100};var work=new GalaxyFilterFactsWork{expires=100};
var bound=BindGalaxyFilterFactsWork(work,frame,1);frame.referenceWorld=101;
if(bound&&!BindGalaxyFilterFactsWork(work,frame,2)&&work.done&&work.error.Contains("changed")&&work.contextSystem==100)passed|=32;
work=new(){expires=100};if(!BindGalaxyFilterFactsWork(work,frame,100)&&work.done&&work.error.Contains("deadline"))passed|=64;
var positions=new Dictionary<ulong,double[]>();var order=new Queue<ulong>();
var position=new[]{1d,2d,3d};CopyNativeGalaxyFactPosition(new(){id=300,position=position},positions,order);position[0]=999;
CopyNativeGalaxyFactPosition(new(){id=0,position=[1,2,3]},positions,order);CopyNativeGalaxyFactPosition(new(){id=301,position=[double.NaN,2,3]},positions,order);
if(positions.Count==1&&positions[300][0]==1&&!positions.ContainsKey(301))passed|=128;
return passed;
}
finally{galaxyFactsWork=savedWork;galaxyFactsGeneration=savedGeneration;}
}
}
#if PLUGIN_NAVIGATION
[UnmanagedCallersOnly(EntryPoint="XFENavigationGalaxyFilterFactsQueueSelfTest")]
internal static uint ExportGalaxyFilterFactsQueueSelfTest()=>RunTest(GalaxyFilterFactsQueueSelfTest);
#endif
}