Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions apps/web/__tests__/unit/activity-time-ago.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { formatTimeAgo } from "@/app/s/[videoId]/_components/tabs/Activity/utils";

const NOW = new Date("2026-10-30T12:00:00Z");
const daysAgo = (days: number) =>
new Date(NOW.getTime() - days * 24 * 60 * 60 * 1000);

describe("formatTimeAgo", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(NOW);
});

afterEach(() => {
vi.useRealTimers();
});

it("shows months until twelve thirty-day months have passed", () => {
expect(formatTimeAgo(daysAgo(30))).toBe("1mo ago");
expect(formatTimeAgo(daysAgo(359))).toBe("11mo ago");
});

it("never shows zero years between 360 and 364 days", () => {
for (const days of [360, 362, 364]) {
expect(formatTimeAgo(daysAgo(days))).toBe("1y ago");
}
});

it("counts whole years after that", () => {
expect(formatTimeAgo(daysAgo(365))).toBe("1y ago");
expect(formatTimeAgo(daysAgo(729))).toBe("1y ago");
expect(formatTimeAgo(daysAgo(730))).toBe("2y ago");
});
});
3 changes: 2 additions & 1 deletion apps/web/app/s/[videoId]/_components/tabs/Activity/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const formatTimeAgo = (date: Date) => {
const diffInMonths = Math.floor(diffInDays / 30);
if (diffInMonths < 12) return `${diffInMonths}mo ago`;

const diffInYears = Math.floor(diffInDays / 365);
// 12 thirty-day months is 360 days, five days short of a 365-day year.
const diffInYears = Math.max(1, Math.floor(diffInDays / 365));
return `${diffInYears}y ago`;
};