diff --git a/website/static/website/js/video-age.js b/website/static/website/js/video-age.js index d16f16e7..75a3557a 100644 --- a/website/static/website/js/video-age.js +++ b/website/static/website/js/video-age.js @@ -54,6 +54,14 @@ const VideoAge = (function() { round: true // Round to nearest unit }; + /** + * Milliseconds in a day. Video dates have day granularity (server computes + * age from a DateField), so any age below this means "published today". + * Without this guard, a same-day video humanizes to "0 seconds ago" — the + * symptom reported in issue #1091. + */ + const ONE_DAY_MS = 24 * 60 * 60 * 1000; + // ========================================================================== // INITIALIZATION @@ -134,9 +142,13 @@ const VideoAge = (function() { * * @private * @param {number} ageInMs - Age in milliseconds - * @returns {string} Formatted string like "2 years ago" + * @returns {string} Formatted string like "2 years ago", or "Today" for a + * video published today (avoids "0 seconds ago" — issue #1091) */ function formatPastAge(ageInMs) { + if (ageInMs < ONE_DAY_MS) { + return 'Today'; + } try { const humanized = humanizeDuration(ageInMs, HUMANIZE_OPTIONS); return humanized + ' ago'; diff --git a/website/tests/test_video.py b/website/tests/test_video.py index 32b07b9c..d79b5d80 100644 --- a/website/tests/test_video.py +++ b/website/tests/test_video.py @@ -26,6 +26,18 @@ def test_past_date_returns_positive_int(self): # 7 days in ms, allowing slack for the now() call crossing midnight. self.assertGreaterEqual(age, 6 * 24 * 60 * 60 * 1000) + def test_today_is_below_one_day(self): + # A video published today must yield an age below one day in ms. This + # is the contract video-age.js relies on to render "Today" instead of + # "0 seconds ago" (issue #1091); pin it server-side since the JS guard + # itself has no test runner in this repo. + from website.models import Video + one_day_ms = 24 * 60 * 60 * 1000 + age = Video(date=date.today()).get_age_in_ms() + self.assertIsInstance(age, int) + self.assertGreaterEqual(age, 0) + self.assertLess(age, one_day_ms) + def test_future_date_returns_negative_int(self): from website.models import Video age = Video(date=date.today() + timedelta(days=7)).get_age_in_ms()