using AyCode.Core.Extensions;
using AyCode.Core.Serializers.Binaries;
using AyCode.Core.Tests.TestModels;
namespace AyCode.Core.Tests.Serialization;
///
/// Tests for IId-based reference handling in Binary serializer.
/// Two scenarios:
/// 1. Same instance referenced multiple times (object identity)
/// 2. Different instances with same IId.Id (IId-based deduplication)
///
/// Tests verify BOTH:
/// - Serialized output uses ObjectRef (not redundant full objects)
/// - Deserialized result maintains reference identity
///
[TestClass]
public class AcBinarySerializerIIdReferenceTests
{
// BinaryTypeCode.ObjectRef = 27
private const byte ObjectRefTypeCode = 27;
#region Helper Methods
///
/// Counts occurrences of ObjectRef (0x1B = 27) in binary data.
///
private static int CountObjectRefs(byte[] binary)
{
var count = 0;
for (var i = 0; i < binary.Length; i++)
{
if (binary[i] == ObjectRefTypeCode)
count++;
}
return count;
}
///
/// Counts occurrences of a string in binary data (UTF8).
///
private static int CountStringOccurrences(byte[] binary, string searchString)
{
var searchBytes = System.Text.Encoding.UTF8.GetBytes(searchString);
var count = 0;
for (var i = 0; i <= binary.Length - searchBytes.Length; i++)
{
var match = true;
for (var j = 0; j < searchBytes.Length; j++)
{
if (binary[i + j] != searchBytes[j])
{
match = false;
break;
}
}
if (match) count++;
}
return count;
}
#endregion
#region Scenario 1: Same Instance (Object Identity)
///
/// SCENARIO 1: Same instance referenced multiple times.
/// Validates: ObjectRef present + data integrity after deserialize + reference identity.
///
[TestMethod]
public void SameInstance_SerializeAndDeserialize()
{
// Arrange: SAME instance used 4 times
var sharedTag = new SharedTag { Id = 1, Name = "ImportantTag", Color = "#FF0000" };
var order = new TestOrder
{
Id = 1,
OrderNumber = "ORD-001",
PrimaryTag = sharedTag,
Items =
[
new TestOrderItem { Id = 1, ProductName = "Product-A", Tag = sharedTag },
new TestOrderItem { Id = 2, ProductName = "Product-B", Tag = sharedTag },
new TestOrderItem { Id = 3, ProductName = "Product-C", Tag = sharedTag }
]
};
// Act
var binary = order.ToBinary();
var result = binary.BinaryTo();
// Assert 1: Binary should have ObjectRef entries (reference handling is active)
var objectRefCount = CountObjectRefs(binary);
Console.WriteLine($"Binary size: {binary.Length} bytes");
Console.WriteLine($"ObjectRef count: {objectRefCount}");
Assert.IsTrue(objectRefCount >= 3,
$"Expected at least 3 ObjectRef entries for shared tag, found {objectRefCount}. " +
"Reference handling may not be active!");
// Assert 2: Data integrity - ALL tags are present and have correct data
Assert.IsNotNull(result, "Deserialized result is null");
Assert.IsNotNull(result.PrimaryTag, "PrimaryTag is null - data lost!");
Assert.AreEqual(1, result.PrimaryTag.Id, "PrimaryTag.Id incorrect");
Assert.AreEqual("ImportantTag", result.PrimaryTag.Name, "PrimaryTag.Name incorrect");
Assert.AreEqual("#FF0000", result.PrimaryTag.Color, "PrimaryTag.Color incorrect");
Assert.IsNotNull(result.Items, "Items is null");
Assert.AreEqual(3, result.Items.Count, "Items count incorrect");
for (var i = 0; i < 3; i++)
{
Assert.IsNotNull(result.Items[i].Tag, $"Items[{i}].Tag is null - data lost!");
Assert.AreEqual(1, result.Items[i].Tag!.Id, $"Items[{i}].Tag.Id incorrect");
Assert.AreEqual("ImportantTag", result.Items[i].Tag.Name, $"Items[{i}].Tag.Name incorrect");
}
// Assert 3: Reference identity - all should be same object reference
Assert.AreSame(result.PrimaryTag, result.Items[0].Tag,
"Item[0].Tag should be same reference as PrimaryTag");
Assert.AreSame(result.PrimaryTag, result.Items[1].Tag,
"Item[1].Tag should be same reference as PrimaryTag");
Assert.AreSame(result.PrimaryTag, result.Items[2].Tag,
"Item[2].Tag should be same reference as PrimaryTag");
}
#endregion
#region Scenario 2: Different Instances with Same IId (IId-Based Deduplication)
///
/// SCENARIO 2: DIFFERENT instances with SAME IId.Id value.
/// CRITICAL test - if IId-based deduplication works:
/// - ObjectRef should be used in binary
/// - Data should be complete after deserialize
/// - References should be identical (AreSame)
/// - Different TYPES with same int Id should NOT be confused!
///
[TestMethod]
public void DifferentInstances_SameIId_SerializeAndDeserialize()
{
// Arrange: DIFFERENT instances but SAME IId.Id
// CRITICAL: Multiple DIFFERENT TYPES all have Id=1 - must not be confused!
var order = new TestOrder
{
Id = 1,
OrderNumber = "ORD-001",
// All three types have Id=1 - tests (Type, Id) keying, not just Id
PrimaryTag = new SharedTag { Id = 1, Name = "Tag_Id1", Color = "#FF0000" },
Owner = new SharedUser { Id = 1, Username = "User_Id1", Email = "user1@test.com" },
Category = new SharedCategory { Id = 1, Name = "Category_Id1", SortOrder = 10 },
Items =
[
new TestOrderItem
{
Id = 1,
ProductName = "Product-A",
Tag = new SharedTag { Id = 1, Name = "Tag_Id1", Color = "#FF0000" },
Assignee = new SharedUser { Id = 1, Username = "User_Id1", Email = "user1@test.com" }
},
new TestOrderItem
{
Id = 2,
ProductName = "Product-B",
Tag = new SharedTag { Id = 1, Name = "Tag_Id1", Color = "#FF0000" },
Assignee = new SharedUser { Id = 1, Username = "User_Id1", Email = "user1@test.com" }
},
new TestOrderItem
{
Id = 3,
ProductName = "Product-C",
Tag = new SharedTag { Id = 1, Name = "Tag_Id1", Color = "#FF0000" },
Assignee = new SharedUser { Id = 1, Username = "User_Id1", Email = "user1@test.com" }
}
]
};
// Act
var binary = order.ToBinary();
var result = binary.BinaryTo();
// Assert 1: Check if ObjectRef is used (IId-based deduplication active)
var objectRefCount = CountObjectRefs(binary);
Console.WriteLine($"Binary size: {binary.Length} bytes");
Console.WriteLine($"ObjectRef count: {objectRefCount}");
// Assert 3: Reference identity - same TYPE with same Id should be same reference
// Tags with Id=1 should all be same reference
Assert.AreSame(result.PrimaryTag, result.Items[0].Tag,
"CRITICAL: Item[0].Tag should be same reference as PrimaryTag (same SharedTag.Id=1)");
Assert.AreSame(result.PrimaryTag, result.Items[1].Tag,
"CRITICAL: Item[1].Tag should be same reference as PrimaryTag (same SharedTag.Id=1)");
Assert.AreSame(result.PrimaryTag, result.Items[2].Tag,
"CRITICAL: Item[2].Tag should be same reference as PrimaryTag (same SharedTag.Id=1)");
// Users with Id=1 should all be same reference
Assert.AreSame(result.Owner, result.Items[0].Assignee,
"CRITICAL: Item[0].Assignee should be same reference as Owner (same SharedUser.Id=1)");
Assert.AreSame(result.Owner, result.Items[1].Assignee,
"CRITICAL: Item[1].Assignee should be same reference as Owner (same SharedUser.Id=1)");
Assert.AreSame(result.Owner, result.Items[2].Assignee,
"CRITICAL: Item[2].Assignee should be same reference as Owner (same SharedUser.Id=1)");
// Assert 4: Different TYPES with same Id should NOT be same reference!
Assert.AreNotSame