Files
projax/web/dashboard_view_test.go
mAi 87132ee166 feat(dashboard): scope chip + Quiet (N) ▾ fold + Stale folded into Tiles
Phase 5h slice 3 — splits the Tiles rollup into ProjectsCurrent (primary
grid) and ProjectsQuiet (collapsible fold) per m's §7 'pinned ∪
recently-active ∪ open-work' rule.

URL contract extended:
  /dashboard                      — Tiles, scope=current (defaults elided)
  /dashboard?scope=all            — every active project in the grid
  /dashboard?scope=current        — same as default (chip allows explicit)

Scope chip lives next to the tab strip on Tiles only; Tasks + Events
tabs hide it (no scope concept there). Default chip label: '◇ current',
flips to '○ all' when scope=all. Chip href toggles to the alternate
state preserving filter + view.

Quiet fold:
- <details> element opened on click — projects with IsCurrent=false land
  here, including all stale candidates.
- Fold summary: 'Quiet (N) — older than 14d · M stale' (M omitted when 0).
- Quiet tiles render with the same shape as primary tiles, slightly
  faded; stale tiles also carry a 'tile-stale' class (dashed border) and
  a 'stale' flag in the header.

Stale card on the Tasks tab retires entirely — m's pick. The
LastActivity stamp on each tile carries the staleness signal; the
'consider archiving?' nudge migrates to the Quiet fold framing. Stale
data still computes (collectStale runs in buildDashboard) because the
rollup needs the per-item stale flag and the repo-activity map for
LastActivity.

Cache key extends: (filter | view=X | scope=Y) so toggling scope from
the chip lands in a separate cache slot (no stale render).

Tests:
- TestDashboardStaleCardSurfacesDormantMaiProject retargeted at the new
  Quiet fold + tile-stale class on Tiles.
- TestDashboardStaleCardSkipsRecentRepo asserts the inverse via class
  inspection on the tile <article>.
- 4 new tests cover the scope chip: renders on Tiles only, label flips
  on scope=all, scope=all hides the Quiet fold, chip URL flips correctly.

Empty state: scope=current with no current projects shows a
'Nothing current. Pin a project, or show all active.' note with a
direct link to scope=all.
2026-05-26 12:27:13 +02:00

253 lines
8.9 KiB
Go

package web_test
import (
"context"
"strings"
"testing"
"time"
)
// TestDashboardDefaultViewIsTiles asserts the default landing surface on
// /dashboard (no ?view= param) is the Tiles tab — m's Phase 5h pick.
func TestDashboardDefaultViewIsTiles(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
code, body := get(t, h, "/dashboard")
if code != 200 {
t.Fatalf("GET /dashboard → %d", code)
}
if !strings.Contains(body, `class="dash-tiles"`) {
t.Errorf("default view should be Tiles — body lacks 'class=\"dash-tiles\"'")
}
if strings.Contains(body, `class="card card-tasks"`) {
t.Errorf("default view should NOT render the Tasks 5-card layout")
}
}
// TestDashboardTabsRenderAllThree confirms the tab strip shows the three
// expected entries (Tiles / Tasks / Events) and marks the active one.
func TestDashboardTabsRenderAllThree(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
cases := []struct {
url string
activeTab string
activeLabel string
}{
{"/dashboard", "tiles", "Tiles"},
{"/dashboard?view=tasks", "tasks", "Tasks"},
{"/dashboard?view=events", "events", "Events"},
}
for _, c := range cases {
t.Run(c.activeTab, func(t *testing.T) {
code, body := get(t, h, c.url)
if code != 200 {
t.Fatalf("GET %s → %d", c.url, code)
}
if !strings.Contains(body, `class="dash-tabs"`) {
t.Errorf("expected dash-tabs nav element")
}
for _, label := range []string{"Tiles", "Tasks", "Events"} {
if !strings.Contains(body, label+"</a>") {
t.Errorf("tab strip missing label %q", label)
}
}
// Each <a class="dash-tab ..."> carries many HTMX attrs between
// the class and the label; look for the active class + the
// label somewhere later in the body. Approximate but stable.
activeIdx := strings.Index(body, `class="dash-tab active"`)
if activeIdx < 0 {
t.Fatalf("no active tab marker in body")
}
// Active label must appear after the active class marker AND
// within a reasonable window (one tab worth of HTML, ~300 chars).
window := body[activeIdx:]
if cut := strings.Index(window, `class="dash-tab"`); cut > 0 {
window = window[:cut]
}
if !strings.Contains(window, c.activeLabel) {
t.Errorf("active tab should be %q — active-class window does not contain it", c.activeLabel)
}
})
}
}
// TestDashboardTasksViewFallback confirms that ?view=tasks renders the
// today's 5-card layout (cards), not the tile grid.
func TestDashboardTasksViewFallback(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, body := get(t, h, "/dashboard?view=tasks")
if strings.Contains(body, `class="dash-tiles"`) {
t.Errorf("view=tasks should NOT render the Tiles grid")
}
// Cards either render with chrome or collapse to muted notes; either
// shape proves the cards partial dispatched, not Tiles.
if !strings.Contains(body, "No open tasks") {
t.Errorf("view=tasks with no deps should show collapsed 'No open tasks' note")
}
}
// TestDashboardEventsViewRenders confirms that ?view=events renders the
// promoted Events surface (dash-events-view) and not the cards or tiles.
func TestDashboardEventsViewRenders(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, body := get(t, h, "/dashboard?view=events")
if !strings.Contains(body, `class="dash-events-view"`) {
t.Errorf("view=events should render the promoted Events surface")
}
if strings.Contains(body, `class="dash-tiles"`) {
t.Errorf("view=events should NOT render the Tiles grid")
}
if strings.Contains(body, `class="card card-tasks"`) {
t.Errorf("view=events should NOT render the Tasks 5-card layout")
}
}
// TestDashboardUnknownViewFallsBackToTiles confirms graceful default
// behaviour: an unknown ?view= value renders Tiles, not a 404 or empty.
func TestDashboardUnknownViewFallsBackToTiles(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
code, body := get(t, h, "/dashboard?view=gibberish")
if code != 200 {
t.Fatalf("GET /dashboard?view=gibberish → %d", code)
}
if !strings.Contains(body, `class="dash-tiles"`) {
t.Errorf("unknown view should fall back to Tiles")
}
}
// TestDashboardTilesViewShowsRollupForSeededItem seeds an item, asserts
// the Tiles view renders a tile for it (the rollup runs across every
// active item, regardless of links).
func TestDashboardTilesViewShowsRollupForSeededItem(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stamp := strings.ReplaceAll(time.Now().UTC().Format("150405.000000"), ".", "")
slug := "tile-target-" + stamp
var dev, id string
if err := pool.QueryRow(ctx, `select id from projax.items where slug='dev' and cardinality(parent_ids)=0`).Scan(&dev); err != nil {
t.Fatalf("dev: %v", err)
}
if err := pool.QueryRow(ctx,
`insert into projax.items (kind, title, slug, parent_ids)
values (array['project']::text[], 'tile target', $1, ARRAY[$2]::uuid[])
returning id`,
slug, dev,
).Scan(&id); err != nil {
t.Fatalf("seed item: %v", err)
}
defer pool.Exec(context.Background(), `delete from projax.items where id=$1`, id)
code, body := get(t, h, "/dashboard")
if code != 200 {
t.Fatalf("GET /dashboard → %d", code)
}
if !strings.Contains(body, `data-item-path="dev.`+slug+`"`) {
t.Errorf("expected tile for dev.%s on default Tiles view", slug)
}
// Title is rendered as text inside <a class="tile-title">…</a> with
// surrounding whitespace; a substring check is enough.
if !strings.Contains(body, "tile target") {
t.Errorf("expected tile title 'tile target' to appear in body")
}
}
// TestDashboardCacheKeySeparatesViews ensures the cache layer keys by
// (filter, view): the same filter under different views must hit
// independent cache entries. We prove this by priming /dashboard, then
// /dashboard?view=tasks, and asserting both report "fresh" on their
// first call (i.e. they don't share a cache slot).
func TestDashboardCacheKeySeparatesViews(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, body1 := get(t, h, "/dashboard")
if !strings.Contains(body1, "fresh") {
t.Fatalf("first /dashboard load should be fresh")
}
_, body2 := get(t, h, "/dashboard?view=tasks")
if !strings.Contains(body2, "fresh") {
t.Errorf("first /dashboard?view=tasks load should be fresh — sharing a cache slot with Tiles would mark it cached")
}
}
// TestDashboardScopeChipRendersOnTilesOnly asserts the scope chip
// (◇ current / ○ all) renders next to the tab strip on Tiles view
// only — Tasks and Events tabs don't have a scope concept.
func TestDashboardScopeChipRendersOnTilesOnly(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, tiles := get(t, h, "/dashboard")
if !strings.Contains(tiles, `class="dash-scope-chip"`) {
t.Errorf("Tiles view should render the scope chip")
}
if !strings.Contains(tiles, "◇ current") {
t.Errorf("default scope chip should show '◇ current'")
}
_, tasks := get(t, h, "/dashboard?view=tasks")
if strings.Contains(tasks, `class="dash-scope-chip"`) {
t.Errorf("Tasks view should NOT render the scope chip")
}
_, events := get(t, h, "/dashboard?view=events")
if strings.Contains(events, `class="dash-scope-chip"`) {
t.Errorf("Events view should NOT render the scope chip")
}
}
// TestDashboardScopeAllChipFlipsLabel asserts that scope=all renders
// the chip with the alternate label so m can flip back.
func TestDashboardScopeAllChipFlipsLabel(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, body := get(t, h, "/dashboard?scope=all")
if !strings.Contains(body, "○ all") {
t.Errorf("scope=all should render '○ all' chip label")
}
if strings.Contains(body, "◇ current") {
t.Errorf("scope=all should NOT render the '◇ current' label")
}
}
// TestDashboardScopeAllHidesQuietFold asserts that scope=all puts
// everything in the primary grid; no Quiet fold should render because
// nothing is "quiet" under that scope.
func TestDashboardScopeAllHidesQuietFold(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, body := get(t, h, "/dashboard?scope=all")
if strings.Contains(body, `class="dash-quiet"`) {
t.Errorf("scope=all should NOT render the Quiet fold — everything is in the primary grid")
}
}
// TestDashboardScopeChipURLFlips asserts the chip's href flips between
// ?scope=all and the default /dashboard each toggle.
func TestDashboardScopeChipURLFlips(t *testing.T) {
srv, pool := mustServer(t)
defer pool.Close()
h := srv.Routes()
_, defaultBody := get(t, h, "/dashboard")
if !strings.Contains(defaultBody, `href="/dashboard?scope=all"`) {
t.Errorf("default scope chip should link to ?scope=all")
}
_, allBody := get(t, h, "/dashboard?scope=all")
if !strings.Contains(allBody, `href="/dashboard"`) {
t.Errorf("scope=all chip should link back to /dashboard (scope=current is default+elided)")
}
}