From d235839e2267a37b6cbbd072bc0677226b2eaf60 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Mon, 27 Oct 2025 09:57:12 -0400 Subject: [PATCH] Fix video deduper timestamp comparison --- js/utils/videoDeduper.js | 10 +++++++++- tests/utils/video-deduper.test.mjs | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/utils/video-deduper.test.mjs diff --git a/js/utils/videoDeduper.js b/js/utils/videoDeduper.js index 76575c42..bfbe8c05 100644 --- a/js/utils/videoDeduper.js +++ b/js/utils/videoDeduper.js @@ -8,6 +8,11 @@ export function dedupeToNewestByRoot(videos) { const map = new Map(); + const normalizeTimestamp = (value) => { + const numeric = Number(value); + return Number.isFinite(numeric) ? numeric : -Infinity; + }; + for (const vid of videos || []) { if (!vid) { continue; @@ -18,7 +23,10 @@ export function dedupeToNewestByRoot(videos) { } const existing = map.get(rootId); - if (!existing || vid.created_at > existing.created_at) { + if ( + !existing || + normalizeTimestamp(vid.created_at) > normalizeTimestamp(existing.created_at) + ) { map.set(rootId, vid); } } diff --git a/tests/utils/video-deduper.test.mjs b/tests/utils/video-deduper.test.mjs new file mode 100644 index 00000000..5c149584 --- /dev/null +++ b/tests/utils/video-deduper.test.mjs @@ -0,0 +1,23 @@ +import test from "node:test"; +import assert from "node:assert/strict"; + +import { dedupeToNewestByRoot } from "../../js/utils/videoDeduper.js"; + +test("dedupeToNewestByRoot replaces entries with missing timestamps", () => { + const original = { id: "abc", created_at: undefined }; + const newer = { id: "abc", created_at: 123 }; + + const [result] = dedupeToNewestByRoot([original, newer]); + + assert.equal(result, newer); +}); + +test("dedupeToNewestByRoot replaces entries with non-numeric timestamps", () => { + const rootId = "root-1"; + const invalid = { id: "old", videoRootId: rootId, created_at: "not-a-number" }; + const newer = { id: "new", videoRootId: rootId, created_at: "456" }; + + const [result] = dedupeToNewestByRoot([invalid, newer]); + + assert.equal(result, newer); +});