2026-10-01 11:25:48 +02:00
|
|
|
using MongoDB.Bson;
|
|
|
|
|
|
|
|
|
|
using PrivaPub.Infrastructure.Ids;
|
|
|
|
|
using PrivaPub.Models.User;
|
|
|
|
|
|
|
|
|
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
|
|
|
|
|
|
|
|
|
namespace PrivaPub.Tests.Infrastructure
|
|
|
|
|
{
|
|
|
|
|
public class PrivacyIdsTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Persona_ids_carry_only_the_day_and_share_nothing_else()
|
|
|
|
|
{
|
2026-10-01 17:39:58 +02:00
|
|
|
var avatar = new Avatar();
|
|
|
|
|
var first = (string)avatar.GenerateNewID();
|
|
|
|
|
var second = (string)new Avatar { PublishedOn = avatar.PublishedOn }.GenerateNewID();
|
|
|
|
|
var group = new GroupEntity();
|
2026-10-01 11:25:48 +02:00
|
|
|
|
|
|
|
|
Assert.True(ObjectId.TryParse(first, out var a));
|
|
|
|
|
Assert.True(ObjectId.TryParse(second, out var b));
|
2026-10-01 17:39:58 +02:00
|
|
|
Assert.Equal(avatar.PublishedOn, a.CreationTime);
|
|
|
|
|
Assert.InRange(a.CreationTime, DateTime.UtcNow.Date.AddDays(-13), DateTime.UtcNow.Date);
|
2026-10-01 11:25:48 +02:00
|
|
|
Assert.Equal(a.CreationTime, b.CreationTime);
|
|
|
|
|
Assert.NotEqual(first[8..14], second[8..14]);
|
2026-10-01 17:39:58 +02:00
|
|
|
Assert.Equal(group.PublishedOn, ObjectId.Parse((string)group.GenerateNewID()).CreationTime);
|
2026-10-01 11:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void A_remote_post_id_sorts_by_its_published_time()
|
|
|
|
|
{
|
|
|
|
|
var older = PrivacyIds.At(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc));
|
|
|
|
|
var newer = PrivacyIds.At(new DateTime(2025, 1, 2, 0, 0, 0, DateTimeKind.Utc));
|
|
|
|
|
|
|
|
|
|
Assert.True(string.CompareOrdinal(older, newer) < 0);
|
|
|
|
|
Assert.Equal(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), ObjectId.Parse(older).CreationTime);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 13:19:16 +02:00
|
|
|
[Fact]
|
|
|
|
|
public void A_post_arriving_live_sorts_after_everything_already_stored()
|
|
|
|
|
{
|
|
|
|
|
var local = ObjectId.GenerateNewId().ToString();
|
|
|
|
|
var reply = PrivacyIds.Arrived(DateTime.UtcNow.AddSeconds(-30));
|
|
|
|
|
var skewed = PrivacyIds.Arrived(DateTime.UtcNow.AddYears(50));
|
|
|
|
|
|
|
|
|
|
Assert.True(string.CompareOrdinal(local, reply) < 0);
|
|
|
|
|
Assert.True(string.CompareOrdinal(reply, skewed) < 0);
|
|
|
|
|
Assert.True(ObjectId.Parse(skewed).CreationTime < DateTime.UtcNow.AddMinutes(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void A_backfilled_post_keeps_its_published_time() =>
|
|
|
|
|
Assert.Equal(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), ObjectId.Parse(PrivacyIds.Arrived(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc))).CreationTime);
|
|
|
|
|
|
2026-10-01 17:39:58 +02:00
|
|
|
[Fact]
|
|
|
|
|
public void A_persona_publishes_a_day_within_two_weeks_before_its_creation()
|
|
|
|
|
{
|
|
|
|
|
var created = new DateTime(2026, 10, 1, 15, 30, 0, DateTimeKind.Utc);
|
|
|
|
|
var days = Enumerable.Range(0, 200).Select(_ => PrivacyIds.PublishedDay(created)).ToList();
|
|
|
|
|
|
|
|
|
|
Assert.All(days, d => Assert.Equal(TimeSpan.Zero, d.TimeOfDay));
|
|
|
|
|
Assert.All(days, d => Assert.InRange(d, created.Date.AddDays(-13), created.Date));
|
|
|
|
|
Assert.True(days.Distinct().Count() > 1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:25:48 +02:00
|
|
|
[Fact]
|
|
|
|
|
public void A_published_time_in_the_future_is_clamped() =>
|
|
|
|
|
Assert.True(ObjectId.Parse(PrivacyIds.At(DateTime.UtcNow.AddYears(50))).CreationTime < DateTime.UtcNow.AddDays(2));
|
|
|
|
|
}
|
|
|
|
|
}
|