char/sorcery

static-files based git repo viewer

git clone https://git.t4t.associates/char/sorcery

Charlotte Somfeat: give commits absolute <time> timestamps, relativized while recentc166af0

main
1.2 KiB26 linesraw
1import { assertEquals } from "jsr:@std/assert@1";
2import { commitDate, timeAgo } from "./util.ts";
3
4const now = new Date("2026-10-05T12:00:00Z");
5const before = (seconds: number) => new Date(now.getTime() - seconds * 1000);
6
7Deno.test("timeAgo picks the largest whole unit", () => {
8  assertEquals(timeAgo(before(47 * 60 + 59), now), "47 minutes ago");
9  assertEquals(timeAgo(before(3 * 86400 + 3600), now), "3 days ago");
10  assertEquals(timeAgo(before(86400), now), "yesterday");
11  assertEquals(timeAgo(before(13 * 86400), now), "last week");
12  assertEquals(timeAgo(before(400 * 86400), now), "last year");
13});
14
15Deno.test("timeAgo reads the future as now", () => {
16  assertEquals(timeAgo(before(0), now), "now");
17  assertEquals(timeAgo(before(-90), now), "now");
18});
19
20Deno.test("commitDate is relative only while recent", () => {
21  const id = (date: Date) => ({ time: date.getTime() / 1000, tz: "+0000" });
22  assertEquals(commitDate(id(before(3 * 86400)), now), "3 days ago");
23  assertEquals(commitDate(id(before(29 * 86400)), now), "4 weeks ago");
24  assertEquals(commitDate(id(before(30 * 86400)), now), "2026-09-05");
25  assertEquals(commitDate(id(before(400 * 86400)), now), "2025-08-31");
26});