Files
Docwell/Server/internal/directory/analytics_test.go
T
tajniak81andClaude Opus 5.5 9d674e9699
CI / go (Media) (push) Successful in 14m33s
CI / go (Server) (push) Failing after 23m30s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 9m30s
CI / desktop (ubuntu-latest) (push) Successful in 1h21m24s
CI / mobile (push) Failing after 4m16s
CI / docker (push) Successful in 1h1m25s
Docs / build (push) Successful in 14m59s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Add invite links with approval, sharing switches, page views and unused-file cleanup
Phase G of docs/AFFINE.md (ADR-17):

- Invite links: one per workspace (member or guest, optional end date,
  optional admin approval) on a new /join page. Requests are approved
  (optionally as another role) or declined from workspace settings;
  join_request, join_approved and join_declined notifications reach the
  inbox, phones and the email digest. Workspace admins decide, else the
  organization's admins, else superadmins. Signup honours ?redirect=.
- Workspace switches: allowSharing (off makes every share link answer 404
  until it is back on) and urlPreview (chat apps may show private pages'
  titles). docwell-web writes title and Open Graph tags for known
  link-preview bots, asking docwell-server via DOCWELL_PREVIEW_API_URL;
  search engines are left out.
- Page analytics: page_views counts opens per page, person and day from
  the existing visit call and share-link reads; editors see totals, a
  daily chart and recent viewers in the Info panel. analytics.prune
  drops rows after 400 days.
- Unused files: attachments whose id appears in no page's Yjs state,
  page icon or comment (older than a day) are listed and deleted from
  workspace settings.
- Migration, schema snapshot, OpenAPI, generated clients, translations
  in five languages, phone inbox, compose files and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:59:55 +01:00

81 lines
2.6 KiB
Go

// 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)
}
}