import test from "node:test"; import assert from "node:assert/strict"; import { postComparator, isPostPinned, getPostTimestamp } from "../src/_utils/sort-posts.js"; test("Post Sorting: isPostPinned detection", () => { assert.equal(isPostPinned({ data: { pinned: true } }), true); assert.equal(isPostPinned({ data: { featured: true } }), true); assert.equal(isPostPinned({ data: { pinned: false } }), false); assert.equal(isPostPinned({ data: { featured: false } }), false); assert.equal(isPostPinned({ data: { pinned: null } }), false); assert.equal(isPostPinned({ data: {} }), false); assert.equal(isPostPinned({}), false); assert.equal(isPostPinned(null), false); assert.equal(isPostPinned(undefined), false); assert.equal(isPostPinned("invalid"), false); }); test("Post Sorting: getPostTimestamp extraction and fallback", () => { const d = new Date("2026-09-01T00:00:00Z"); assert.equal(getPostTimestamp({ date: d }), d.getTime()); assert.equal(getPostTimestamp({ data: { date: "2026-09-01" } }), new Date("2026-09-01").getTime()); assert.equal(getPostTimestamp({ data: { created: "2026-09-01" } }), new Date("2026-09-01").getTime()); assert.equal(getPostTimestamp({ date: "not-a-date" }), 0); assert.equal(getPostTimestamp({}), 0); assert.equal(getPostTimestamp(null), 0); }); test("Post Sorting: standard chronological sort (unpinned posts)", () => { const postNew = { data: { title: "Newer Post" }, date: new Date("2026-09-10") }; const postOld = { data: { title: "Older Post" }, date: new Date("2026-08-01") }; const sorted = [postOld, postNew].sort(postComparator); assert.deepEqual(sorted.map((p) => p.data.title), ["Newer Post", "Older Post"]); }); test("Post Sorting: pinned post forces top position over newer unpinned post", () => { const newerUnpinned = { data: { title: "Newer Unpinned", pinned: false }, date: new Date("2026-09-20") }; const olderPinned = { data: { title: "Older Pinned", pinned: true }, date: new Date("2026-07-01") }; const sorted = [newerUnpinned, olderPinned].sort(postComparator); assert.equal(sorted[0].data.title, "Older Pinned"); assert.equal(sorted[1].data.title, "Newer Unpinned"); }); test("Post Sorting: featured post forces top position over newer unpinned post", () => { const newerRegular = { data: { title: "Fresh Article" }, date: new Date("2026-09-22") }; const olderFeatured = { data: { title: "Featured Pillar", featured: true }, date: new Date("2026-06-15") }; const sorted = [newerRegular, olderFeatured].sort(postComparator); assert.equal(sorted[0].data.title, "Featured Pillar"); assert.equal(sorted[1].data.title, "Fresh Article"); }); test("Post Sorting: multiple pinned posts sort among themselves chronologically", () => { const olderPinned = { data: { title: "Older Pinned", pinned: true }, date: new Date("2026-05-01") }; const newerPinned = { data: { title: "Newer Pinned", featured: true }, date: new Date("2026-08-15") }; const freshUnpinned = { data: { title: "Fresh Regular" }, date: new Date("2026-09-01") }; const list = [olderPinned, freshUnpinned, newerPinned]; const sorted = list.sort(postComparator); assert.deepEqual( sorted.map((p) => p.data.title), ["Newer Pinned", "Older Pinned", "Fresh Regular"] ); }); test("Post Sorting: adversarial robustness against missing data or equal dates", () => { const postNoData = { date: new Date("2026-09-01") }; const postMalformedDate = { data: { title: "Malformed", date: "invalid-date" } }; const postIdenticalDate1 = { data: { title: "Post A" }, date: new Date("2026-09-01") }; const postIdenticalDate2 = { data: { title: "Post B" }, date: new Date("2026-09-01") }; assert.doesNotThrow(() => { [postNoData, postMalformedDate, postIdenticalDate1, postIdenticalDate2].sort(postComparator); }); });