using System.Text.Json;
using XFEEDToolBox.Core.Galaxy;
using XFEExtension.NetCore.XUnit.Attributes;
namespace XFEEDToolBox.Test;
[TestFixture]
public sealed class GalaxyOriginalBasicFilteringTests
{
private static GalaxyNativeBasicSystemFacts Basic=>new(50,4,4,6,17,9007199254740995,79);
private static GalacticSystem Sample(GalaxyNativeBasicSystemFacts? basic,bool requested=true)=>new("Actual",new(0,0,0),300)
{Data=new(){Government="Anarchy",Economy="Tourism",SecondaryEconomy="Agriculture",State="War",Population=1000000000,
NativeFilterFacts=new("F1",42,100){BasicSystem=basic,BasicSystemRequested=requested}}};
private static GalaxyMapFilter Filter(GalaxyFilterCategory category,string value)=>new(){Mode=GalaxyMapMode.Map,Category=category,Values=new HashSet<string>{value},RequireOriginalBasicSystemFacts=true};
[Test] public void ReadyClassifierMissingRowMatchesOriginalNoneAndARealSnapshotReplacesItsDefaults()
{
var fallback=new GalaxyNativeBasicSystemFacts(0,1,1,1,0,ulong.MaxValue,0){Defaulted=true,AllegianceKey="None"};
var original=Sample(fallback);var none=Filter(GalaxyFilterCategory.Government,"none");
Check.That(none.Matches(original)&&!none.IsUnknown(original));Check.Equal(none.ColourForValue("none"),none.GetColour(original));
var rgba=none.GetOriginalRgba(original)!.Value;Check.That(GalaxyOriginalAppearance.TryColour(GalaxyFilterCategory.Government,"none",out var expected));
Check.Equal(expected.R,rgba.X);Check.Equal(expected.G,rgba.Y);Check.Equal(expected.B,rgba.Z);
Check.That(Filter(GalaxyFilterCategory.Economy,"none").Matches(original));
Check.That(Filter(GalaxyFilterCategory.Allegiance,"none").Matches(original));
Check.That(Filter(GalaxyFilterCategory.Security,"anarchy").Matches(original));
var pending=Sample(null);Check.That(!none.Matches(pending)&&none.IsUnknown(pending));
var updated=original with{Data=original.Data.Merge(new(){NativeFilterFacts=new("F1",42,100){BasicSystemRequested=true,BasicSystem=Basic}})};
Check.That(updated.Data.NativeFilterFacts?.BasicSystem is{Defaulted:false});
Check.That(!none.Matches(updated)&&Filter(GalaxyFilterCategory.Government,"democracy").Matches(updated));
Check.Equal(50L,none.PopulationValue(updated));
}
[Test] public void RequiredOriginalUnknownCannotUseOldPublicFieldsOrServerProof()
{
var filter=Filter(GalaxyFilterCategory.Government,"anarchy");var system=Sample(null,false);
system=system with{Data=system.Data with{ConfirmedQueries=new HashSet<string>{filter.QueryKey}}};
Check.That(!filter.Matches(system)&&filter.IsUnknown(system)&&!filter.Evaluate(system).Matches);
Check.That(filter.GetOriginalRgba(system) is null);Check.That((filter with{IncludeUnknown=true}).Matches(system));
system=Sample(null);Check.That(!filter.Matches(system));
Check.That(!JsonSerializer.Serialize(filter).Contains("RequireOriginalBasicSystemFacts"));
}
[Test] public void GovernmentAndBaseStateUseActualEnumsBeforeContradictoryPublicValues()
{
var system=Sample(Basic);var government=Filter(GalaxyFilterCategory.Government,"democracy");
Check.That(government.Matches(system)&&!government.IsUnknown(system));Check.That(!(government with{Values=new HashSet<string>{"anarchy"}}).Matches(system));
Check.Equal("democracy",GalaxyMapFilter.Keys(system,GalaxyFilterCategory.Government)![0]);
var state=Filter(GalaxyFilterCategory.State,"civilliberty");Check.That(state.Matches(system));Check.That(!(state with{Values=new HashSet<string>{"war"}}).Matches(system));
var future=Sample(Basic with{GovernmentId=uint.MaxValue,FactionStateId=40});
future=future with{Data=future.Data with{ConfirmedQueries=new HashSet<string>{government.QueryKey,state.QueryKey}}};
Check.That(!government.Matches(future)&&government.IsUnknown(future)&&!state.Matches(future));
}
[Test] public void EitherEconomyMatchesAndSelectedSecondaryChoosesItsOwnOriginalColour()
{
var filter=Filter(GalaxyFilterCategory.Economy,"hightech");var system=Sample(Basic);
Check.That(filter.Matches(system)&&!filter.IsUnknown(system));Check.Equal(filter.ColourForValue("hightech"),filter.GetColour(system));
var both=filter with{Values=new HashSet<string>{"hightech","industrial"}};
Check.Equal(both.ColourForValue("industrial"),both.GetColour(system));
Check.That(!(filter with{Values=new HashSet<string>{"tourism"}}).Matches(system));
var partial=Sample(Basic with{PrimaryEconomyId=29});Check.That(filter.Matches(partial));
var unmatched=filter with{Values=new HashSet<string>{"industrial"}};
partial=partial with{Data=partial.Data with{ConfirmedQueries=new HashSet<string>{unmatched.QueryKey}}};
Check.That(!unmatched.Matches(partial)&&unmatched.IsUnknown(partial)&&!unmatched.Evaluate(partial).Matches);
Check.That((unmatched with{IncludeUnknown=true}).Matches(partial));
}
[Test] public void OneOriginalPopulationControlsThresholdSizeOpacityAndFloatColour()
{
var filter=Filter(GalaxyFilterCategory.Population,"uninhabited") with{SizeCategory=GalaxyFilterCategory.Population,PopulationMinimum=1,PopulationMaximum=100};
var zero=Sample(Basic with{Population=0});
Check.Equal(0L,filter.PopulationValue(zero));Check.That(!filter.Matches(zero)&&!filter.IsUnknown(zero));
Check.Equal(0f,filter.RelativeSize(zero));Check.Equal(0f,filter.GetOpacity(zero));Check.Equal(0f,filter.GetOriginalRgba(zero)!.Value.W);
Check.That((filter with{PopulationMinimum=0}).Matches(zero));
var middle=Sample(Basic);var range=filter with{Values=null,PopulationMinimum=0};
Check.Equal(range.PopulationColour(50),range.GetColour(middle));Check.Equal(1f,range.GetOpacity(middle));
Check.That(Math.Abs(range.RelativeSize(middle)-1.15f)<.00001f);
var raw=range.GetOriginalRgba(middle)!.Value;var expected=GalaxyOriginalAppearance.PopulationLow.Interpolate(GalaxyOriginalAppearance.PopulationHigh,.5f);
Check.Equal(expected.R,raw.X);Check.Equal(expected.G,raw.Y);Check.Equal(expected.B,raw.Z);
var overflow=Sample(Basic with{Population=ulong.MaxValue});Check.That(range.PopulationValue(overflow) is null&&!range.Matches(overflow)&&range.IsUnknown(overflow));
Check.That(range.GetOriginalRgba(overflow) is null);Check.Equal(GalaxyOriginalAppearance.DefaultSize,range.RelativeSize(overflow));
}
[Test] public void UnrelatedQueriesPreserveBasicButExplicitUnknownAndContextLossClearRuntimeEvidence()
{
var filter=Filter(GalaxyFilterCategory.Government,"democracy");var old=Sample(Basic);
var unrelated=old with{Data=old.Data.Merge(new(){NativeFilterFacts=new("F1",42,100,Visited:true)})};
Check.That(unrelated.Data.NativeFilterFacts is{BasicSystemRequested:true,BasicSystem:not null}&&filter.Matches(unrelated));
var unknown=old with{Data=old.Data.Merge(new(){NativeFilterFacts=new("F1",42,100){BasicSystemRequested=true,BasicSystem=null}})};
Check.That(!filter.Matches(unknown)&&filter.IsUnknown(unknown));
var disconnected=old with{Data=old.Data.ForNativeContext("F1",43,100)};
Check.That(disconnected.Data.NativeFilterFacts is null&&!filter.Matches(disconnected));
var publicFilter=filter with{RequireOriginalBasicSystemFacts=false,Values=new HashSet<string>{"anarchy"}};
Check.That(publicFilter.Matches(disconnected));Check.Equal(1000000000L,publicFilter.PopulationValue(disconnected));
Check.That(!JsonSerializer.Serialize(old.Data).Contains("BasicSystem"));
Check.That(publicFilter.RequestsOriginalBasicSystemFacts&&!(publicFilter with{Category=GalaxyFilterCategory.Services}).RequestsOriginalBasicSystemFacts);
}
[Test] public void OriginalAnarchyOverridesOldPublicHighSecurityAndOriginalKeysUseTheirRealAliases()
{
var system=Sample(Basic with{GovernmentId=2,SecurityValue=255});system=system with{Data=system.Data with{Security="High"}};
var filter=Filter(GalaxyFilterCategory.Security,"anarchy");Check.That(filter.RequestsOriginalBasicSystemFacts&&filter.Matches(system)&&!filter.IsUnknown(system));
Check.Equal("anarchy",GalaxyMapFilter.Keys(system,GalaxyFilterCategory.Security)![0]);
Check.That(!(filter with{Values=new HashSet<string>{"high"}}).Matches(system));
Check.Equal("low",GalaxyMapFilter.Normalize("LowSecurity"));Check.Equal("medium",GalaxyMapFilter.Normalize("MediumSecurity"));Check.Equal("high",GalaxyMapFilter.Normalize("HighSecurity"));
var high=Sample(Basic with{GovernmentId=4,SecurityValue=80});Check.That((filter with{Values=new HashSet<string>{"high"}}).Matches(high));
Check.Equal(filter.ColourForValue("high"),(filter with{Values=new HashSet<string>{"high"}}).GetColour(high));
}
[Test] public void UnknownOriginalSecurityCannotBeRestoredByPublicHighOrMatchingPublicProof()
{
var filter=Filter(GalaxyFilterCategory.Security,"high");
foreach(var system in new[]{Sample(null),Sample(Basic with{GovernmentId=uint.MaxValue}),Sample(null,false)})
{
var old=system with{Data=system.Data with{Security="High",ConfirmedQueries=new HashSet<string>{filter.QueryKey}}};
Check.That(!filter.Matches(old)&&filter.IsUnknown(old)&&!filter.Evaluate(old).Matches);
Check.That(filter.GetOriginalRgba(old) is null);Check.That((filter with{IncludeUnknown=true}).Matches(old));
}
}
[Test] public void ActualAllegianceDefinitionKeyOverridesPublicAllegianceWithoutUsingNumericOrder()
{
var filter=Filter(GalaxyFilterCategory.Allegiance,"empire");
var actual=Sample(Basic with{AllegianceId=9007199254740995,AllegianceKey="Empire"});actual=actual with{Data=actual.Data with{Allegiance="Federation"}};
Check.That(filter.RequestsOriginalBasicSystemFacts&&filter.Matches(actual)&&!filter.IsUnknown(actual));
Check.That(!(filter with{Values=new HashSet<string>{"federation"}}).Matches(actual));
Check.Equal("empire",GalaxyMapFilter.Keys(actual,GalaxyFilterCategory.Allegiance)![0]);
var missing=Sample(Basic with{AllegianceId=9007199254740995,AllegianceKey=null});
missing=missing with{Data=missing.Data with{Allegiance="Empire",ConfirmedQueries=new HashSet<string>{filter.QueryKey}}};
Check.That(!filter.Matches(missing)&&filter.IsUnknown(missing)&&!filter.Evaluate(missing).Matches);
Check.That(filter.GetOriginalRgba(missing) is null);
}
}
using System.Text.Json;
using XFEEDToolBox.Core.Galaxy;
using XFEExtension.NetCore.XUnit.Attributes;
namespace XFEEDToolBox.Test;
[TestFixture]
public sealed class GalaxyOriginalBasicFilteringTests
{
private static GalaxyNativeBasicSystemFacts Basic=>new(50,4,4,6,17,9007199254740995,79);
private static GalacticSystem Sample(GalaxyNativeBasicSystemFacts? basic,bool requested=true)=>new("Actual",new(0,0,0),300)
{Data=new(){Government="Anarchy",Economy="Tourism",SecondaryEconomy="Agriculture",State="War",Population=1000000000,
NativeFilterFacts=new("F1",42,100){BasicSystem=basic,BasicSystemRequested=requested}}};
private static GalaxyMapFilter Filter(GalaxyFilterCategory category,string value)=>new(){Mode=GalaxyMapMode.Map,Category=category,Values=new HashSet<string>{value},RequireOriginalBasicSystemFacts=true};
[Test] public void ReadyClassifierMissingRowMatchesOriginalNoneAndARealSnapshotReplacesItsDefaults()
{
var fallback=new GalaxyNativeBasicSystemFacts(0,1,1,1,0,ulong.MaxValue,0){Defaulted=true,AllegianceKey="None"};
var original=Sample(fallback);var none=Filter(GalaxyFilterCategory.Government,"none");
Check.That(none.Matches(original)&&!none.IsUnknown(original));Check.Equal(none.ColourForValue("none"),none.GetColour(original));
var rgba=none.GetOriginalRgba(original)!.Value;Check.That(GalaxyOriginalAppearance.TryColour(GalaxyFilterCategory.Government,"none",out var expected));
Check.Equal(expected.R,rgba.X);Check.Equal(expected.G,rgba.Y);Check.Equal(expected.B,rgba.Z);
Check.That(Filter(GalaxyFilterCategory.Economy,"none").Matches(original));
Check.That(Filter(GalaxyFilterCategory.Allegiance,"none").Matches(original));
Check.That(Filter(GalaxyFilterCategory.Security,"anarchy").Matches(original));
var pending=Sample(null);Check.That(!none.Matches(pending)&&none.IsUnknown(pending));
var updated=original with{Data=original.Data.Merge(new(){NativeFilterFacts=new("F1",42,100){BasicSystemRequested=true,BasicSystem=Basic}})};
Check.That(updated.Data.NativeFilterFacts?.BasicSystem is{Defaulted:false});
Check.That(!none.Matches(updated)&&Filter(GalaxyFilterCategory.Government,"democracy").Matches(updated));
Check.Equal(50L,none.PopulationValue(updated));
}
[Test] public void RequiredOriginalUnknownCannotUseOldPublicFieldsOrServerProof()
{
var filter=Filter(GalaxyFilterCategory.Government,"anarchy");var system=Sample(null,false);
system=system with{Data=system.Data with{ConfirmedQueries=new HashSet<string>{filter.QueryKey}}};
Check.That(!filter.Matches(system)&&filter.IsUnknown(system)&&!filter.Evaluate(system).Matches);
Check.That(filter.GetOriginalRgba(system) is null);Check.That((filter with{IncludeUnknown=true}).Matches(system));
system=Sample(null);Check.That(!filter.Matches(system));
Check.That(!JsonSerializer.Serialize(filter).Contains("RequireOriginalBasicSystemFacts"));
}
[Test] public void GovernmentAndBaseStateUseActualEnumsBeforeContradictoryPublicValues()
{
var system=Sample(Basic);var government=Filter(GalaxyFilterCategory.Government,"democracy");
Check.That(government.Matches(system)&&!government.IsUnknown(system));Check.That(!(government with{Values=new HashSet<string>{"anarchy"}}).Matches(system));
Check.Equal("democracy",GalaxyMapFilter.Keys(system,GalaxyFilterCategory.Government)![0]);
var state=Filter(GalaxyFilterCategory.State,"civilliberty");Check.That(state.Matches(system));Check.That(!(state with{Values=new HashSet<string>{"war"}}).Matches(system));
var future=Sample(Basic with{GovernmentId=uint.MaxValue,FactionStateId=40});
future=future with{Data=future.Data with{ConfirmedQueries=new HashSet<string>{government.QueryKey,state.QueryKey}}};
Check.That(!government.Matches(future)&&government.IsUnknown(future)&&!state.Matches(future));
}
[Test] public void EitherEconomyMatchesAndSelectedSecondaryChoosesItsOwnOriginalColour()
{
var filter=Filter(GalaxyFilterCategory.Economy,"hightech");var system=Sample(Basic);
Check.That(filter.Matches(system)&&!filter.IsUnknown(system));Check.Equal(filter.ColourForValue("hightech"),filter.GetColour(system));
var both=filter with{Values=new HashSet<string>{"hightech","industrial"}};
Check.Equal(both.ColourForValue("industrial"),both.GetColour(system));
Check.That(!(filter with{Values=new HashSet<string>{"tourism"}}).Matches(system));
var partial=Sample(Basic with{PrimaryEconomyId=29});Check.That(filter.Matches(partial));
var unmatched=filter with{Values=new HashSet<string>{"industrial"}};
partial=partial with{Data=partial.Data with{ConfirmedQueries=new HashSet<string>{unmatched.QueryKey}}};
Check.That(!unmatched.Matches(partial)&&unmatched.IsUnknown(partial)&&!unmatched.Evaluate(partial).Matches);
Check.That((unmatched with{IncludeUnknown=true}).Matches(partial));
}
[Test] public void OneOriginalPopulationControlsThresholdSizeOpacityAndFloatColour()
{
var filter=Filter(GalaxyFilterCategory.Population,"uninhabited") with{SizeCategory=GalaxyFilterCategory.Population,PopulationMinimum=1,PopulationMaximum=100};
var zero=Sample(Basic with{Population=0});
Check.Equal(0L,filter.PopulationValue(zero));Check.That(!filter.Matches(zero)&&!filter.IsUnknown(zero));
Check.Equal(0f,filter.RelativeSize(zero));Check.Equal(0f,filter.GetOpacity(zero));Check.Equal(0f,filter.GetOriginalRgba(zero)!.Value.W);
Check.That((filter with{PopulationMinimum=0}).Matches(zero));
var middle=Sample(Basic);var range=filter with{Values=null,PopulationMinimum=0};
Check.Equal(range.PopulationColour(50),range.GetColour(middle));Check.Equal(1f,range.GetOpacity(middle));
Check.That(Math.Abs(range.RelativeSize(middle)-1.15f)<.00001f);
var raw=range.GetOriginalRgba(middle)!.Value;var expected=GalaxyOriginalAppearance.PopulationLow.Interpolate(GalaxyOriginalAppearance.PopulationHigh,.5f);
Check.Equal(expected.R,raw.X);Check.Equal(expected.G,raw.Y);Check.Equal(expected.B,raw.Z);
var overflow=Sample(Basic with{Population=ulong.MaxValue});Check.That(range.PopulationValue(overflow) is null&&!range.Matches(overflow)&&range.IsUnknown(overflow));
Check.That(range.GetOriginalRgba(overflow) is null);Check.Equal(GalaxyOriginalAppearance.DefaultSize,range.RelativeSize(overflow));
}
[Test] public void UnrelatedQueriesPreserveBasicButExplicitUnknownAndContextLossClearRuntimeEvidence()
{
var filter=Filter(GalaxyFilterCategory.Government,"democracy");var old=Sample(Basic);
var unrelated=old with{Data=old.Data.Merge(new(){NativeFilterFacts=new("F1",42,100,Visited:true)})};
Check.That(unrelated.Data.NativeFilterFacts is{BasicSystemRequested:true,BasicSystem:not null}&&filter.Matches(unrelated));
var unknown=old with{Data=old.Data.Merge(new(){NativeFilterFacts=new("F1",42,100){BasicSystemRequested=true,BasicSystem=null}})};
Check.That(!filter.Matches(unknown)&&filter.IsUnknown(unknown));
var disconnected=old with{Data=old.Data.ForNativeContext("F1",43,100)};
Check.That(disconnected.Data.NativeFilterFacts is null&&!filter.Matches(disconnected));
var publicFilter=filter with{RequireOriginalBasicSystemFacts=false,Values=new HashSet<string>{"anarchy"}};
Check.That(publicFilter.Matches(disconnected));Check.Equal(1000000000L,publicFilter.PopulationValue(disconnected));
Check.That(!JsonSerializer.Serialize(old.Data).Contains("BasicSystem"));
Check.That(publicFilter.RequestsOriginalBasicSystemFacts&&!(publicFilter with{Category=GalaxyFilterCategory.Services}).RequestsOriginalBasicSystemFacts);
}
[Test] public void OriginalAnarchyOverridesOldPublicHighSecurityAndOriginalKeysUseTheirRealAliases()
{
var system=Sample(Basic with{GovernmentId=2,SecurityValue=255});system=system with{Data=system.Data with{Security="High"}};
var filter=Filter(GalaxyFilterCategory.Security,"anarchy");Check.That(filter.RequestsOriginalBasicSystemFacts&&filter.Matches(system)&&!filter.IsUnknown(system));
Check.Equal("anarchy",GalaxyMapFilter.Keys(system,GalaxyFilterCategory.Security)![0]);
Check.That(!(filter with{Values=new HashSet<string>{"high"}}).Matches(system));
Check.Equal("low",GalaxyMapFilter.Normalize("LowSecurity"));Check.Equal("medium",GalaxyMapFilter.Normalize("MediumSecurity"));Check.Equal("high",GalaxyMapFilter.Normalize("HighSecurity"));
var high=Sample(Basic with{GovernmentId=4,SecurityValue=80});Check.That((filter with{Values=new HashSet<string>{"high"}}).Matches(high));
Check.Equal(filter.ColourForValue("high"),(filter with{Values=new HashSet<string>{"high"}}).GetColour(high));
}
[Test] public void UnknownOriginalSecurityCannotBeRestoredByPublicHighOrMatchingPublicProof()
{
var filter=Filter(GalaxyFilterCategory.Security,"high");
foreach(var system in new[]{Sample(null),Sample(Basic with{GovernmentId=uint.MaxValue}),Sample(null,false)})
{
var old=system with{Data=system.Data with{Security="High",ConfirmedQueries=new HashSet<string>{filter.QueryKey}}};
Check.That(!filter.Matches(old)&&filter.IsUnknown(old)&&!filter.Evaluate(old).Matches);
Check.That(filter.GetOriginalRgba(old) is null);Check.That((filter with{IncludeUnknown=true}).Matches(old));
}
}
[Test] public void ActualAllegianceDefinitionKeyOverridesPublicAllegianceWithoutUsingNumericOrder()
{
var filter=Filter(GalaxyFilterCategory.Allegiance,"empire");
var actual=Sample(Basic with{AllegianceId=9007199254740995,AllegianceKey="Empire"});actual=actual with{Data=actual.Data with{Allegiance="Federation"}};
Check.That(filter.RequestsOriginalBasicSystemFacts&&filter.Matches(actual)&&!filter.IsUnknown(actual));
Check.That(!(filter with{Values=new HashSet<string>{"federation"}}).Matches(actual));
Check.Equal("empire",GalaxyMapFilter.Keys(actual,GalaxyFilterCategory.Allegiance)![0]);
var missing=Sample(Basic with{AllegianceId=9007199254740995,AllegianceKey=null});
missing=missing with{Data=missing.Data with{Allegiance="Empire",ConfirmedQueries=new HashSet<string>{filter.QueryKey}}};
Check.That(!filter.Matches(missing)&&filter.IsUnknown(missing)&&!filter.Evaluate(missing).Matches);
Check.That(filter.GetOriginalRgba(missing) is null);
}
}