// SPDX-License-Identifier: AGPL-3.0-or-later package directory import ( "testing" "time" ) func TestAggregateViews(t *testing.T) { now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) rows := []pageViewRecord{ {User: "ann", Day: "2026-11-16", Views: 3, Last: "2026-11-16 11:00:00.000Z"}, {User: "bob", Day: "2026-11-16", Views: 1, Last: "2026-11-16 09:00:00.000Z"}, {User: "", Day: "2026-11-16", Views: 5, Last: "2026-11-16 10:00:00.000Z"}, {User: "bob", Day: "2026-11-15", Views: 2, Last: "2026-11-15 20:00:00.000Z"}, // Outside the 7 days: ignored. {User: "cat", Day: "2026-11-01", Views: 9, Last: "2026-11-01 08:00:00.000Z"}, } out, viewers := aggregateViews(rows, now, 7) if out.Views != 11 || out.LinkViews != 5 || out.Viewers != 2 { t.Fatalf("totals = %d views, %d link views, %d viewers; want 11, 5, 2", out.Views, out.LinkViews, out.Viewers) } if len(out.Daily) != 7 || out.Daily[0].Date != "2026-11-10" || out.Daily[6].Date != "2026-11-16" { t.Fatalf("daily = %+v, want 7 days ending today", out.Daily) } if d := out.Daily[6]; d.Views != 9 || d.Viewers != 2 { t.Errorf("today = %+v, want 9 views by 2 people", d) } if d := out.Daily[5]; d.Views != 2 || d.Viewers != 1 { t.Errorf("yesterday = %+v, want 2 views by 1 person", d) } if len(viewers) != 2 || viewers[0].User.ID != "ann" || viewers[1].User.ID != "bob" || viewers[1].Views != 3 { t.Errorf("viewers = %+v, want ann then bob (3 views)", viewers) } } func TestViewLogCountsRepeatOpensOnce(t *testing.T) { l := &viewLog{seen: map[string]time.Time{}} t0 := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) if !l.fresh("p|u", t0) { t.Fatal("first open should count") } if l.fresh("p|u", t0.Add(10*time.Minute)) { t.Error("an open 10 minutes later should not count") } if !l.fresh("p|other", t0.Add(10*time.Minute)) { t.Error("another person's open should count") } if !l.fresh("p|u", t0.Add(41*time.Minute)) { t.Error("an open after the gap should count") } } func TestJoinLinkExpiry(t *testing.T) { now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) cases := []struct { expires string want bool }{ {"", false}, {"2026-11-17 12:00:00.000Z", false}, {"2026-11-16 11:59:59.000Z", true}, } for _, c := range cases { if got := (joinLinkRecord{Expires: c.expires}).expired(now); got != c.want { t.Errorf("expired(%q) = %v, want %v", c.expires, got, c.want) } } } func TestPreviewTitle(t *testing.T) { if got := previewTitle(pageRecord{Title: " ", Icon: "📄"}); got != "📄 Untitled" { t.Errorf("got %q", got) } if got := previewTitle(pageRecord{Title: "Plan"}); got != "Plan" { t.Errorf("got %q", got) } }