diff --git a/WordPressPCL.Tests.Selfhosted/CommentsThreaded_Tests.cs b/WordPressPCL.Tests.Selfhosted/CommentsThreaded_Tests.cs index b0c60498..a920a6e4 100644 --- a/WordPressPCL.Tests.Selfhosted/CommentsThreaded_Tests.cs +++ b/WordPressPCL.Tests.Selfhosted/CommentsThreaded_Tests.cs @@ -244,12 +244,11 @@ public async Task CommentsThreaded_Sort_Extension_Desc() { // The following comment depth has to be the lower, equal or +1 int ni = i + 1; - System.DateTime idate = threaded[i].Date; - System.DateTime nidate = threaded[ni].Date; + System.DateTime idate = firstLvl[i].Date; + System.DateTime nidate = firstLvl[ni].Date; // The following comment date has to be older - Assert.IsGreaterThan(threaded[ni].Id, threaded[i].Id); - Assert.IsLessThanOrEqualTo(nidate, idate); + Assert.IsTrue(nidate <= idate); } } diff --git a/WordPressPCL.Tests.Selfhosted/Utility/ThreadedCommentsHelper_Tests.cs b/WordPressPCL.Tests.Selfhosted/Utility/ThreadedCommentsHelper_Tests.cs new file mode 100644 index 00000000..b851722f --- /dev/null +++ b/WordPressPCL.Tests.Selfhosted/Utility/ThreadedCommentsHelper_Tests.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using WordPressPCL.Models; +using WordPressPCL.Utility; + +namespace WordPressPCL.Tests.Selfhosted.Utility; + +[TestClass] +public class ThreadedCommentsHelper_Tests +{ + [TestMethod] + public void ToThreaded_Descending_KeepsTopLevelCommentsInDescendingDateOrder() + { + DateTime olderRootDate = new(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc); + DateTime newerRootDate = new(2026, 1, 2, 0, 0, 0, DateTimeKind.Utc); + DateTime newestChildDate = new(2026, 1, 3, 0, 0, 0, DateTimeKind.Utc); + + List comments = + [ + new Comment { Id = 1, Date = newerRootDate }, + new Comment { Id = 2, ParentId = 1, Date = newestChildDate }, + new Comment { Id = 3, Date = olderRootDate } + ]; + + List? threaded = comments.ToThreaded(true); + + Assert.IsNotNull(threaded); + CollectionAssert.AreEqual(new[] { 1, 2, 3 }, threaded.Select(comment => comment.Id).ToList()); + + List firstLevel = threaded.Where(comment => comment.Depth == 0).ToList(); + CollectionAssert.AreEqual(new[] { 1, 3 }, firstLevel.Select(comment => comment.Id).ToList()); + Assert.IsTrue(firstLevel[0].Date >= firstLevel[1].Date); + } +}