import { test } from "node:test"; import assert from "node:assert/strict"; import { byUploadDateDesc, byCreatedAtThenId, rankByUploadDateDesc, rankThread, } from "./rank"; test("rankByUploadDateDesc is newest-first and stable within a date", () => { const rows = [ { uploadDate: "20240101", id: "a" }, { uploadDate: "20250601", id: "b" }, { uploadDate: "20240101", id: "c" }, { uploadDate: "20251231", id: "d" }, ]; assert.deepEqual( rankByUploadDateDesc(rows).map((r) => r.id), ["d", "b", "a", "c"], ); assert.equal(byUploadDateDesc({ uploadDate: "x" }, { uploadDate: "x" }), 0); }); test("rankThread is oldest-first, tie-broken by id so the order is total", () => { const posts = [ { createdAt: "2025-01-02T00:00:00Z", id: "b" }, { createdAt: "2025-01-01T00:00:00Z", id: "z" }, { createdAt: "2025-01-01T00:00:00Z", id: "a" }, ]; assert.deepEqual( rankThread(posts).map((p) => p.id), ["a", "z", "b"], ); assert.ok( byCreatedAtThenId({ createdAt: "t", id: "a" }, { createdAt: "t", id: "b" }) < 0, ); });