Files
projax/web/dashboard_view_test.go
mAi fee3251946 feat(dashboard): polish Events tab — summary header, fuller day labels
Phase 5h slice 5 — the Events tab's routing landed in slice 2; this
slice adds the dedicated-surface polish that distinguishes it from
the Events card on the Tasks tab.

Changes:
- Top header summary: 'N events · next 7 days' so m sees the window
  shape at a glance without scanning rows.
- Day headings now carry three columns: the relative label
  ('Today' / 'Tomorrow' / weekday), the ISO date (mono font), and a
  right-aligned event count. Bigger visual hierarchy than the cards-tab
  flavour to justify the dedicated tab's existence.
- Empty-state copy invites linking a CalDAV calendar from a project's
  detail page so a never-seen-events tab doesn't feel broken.
- StartLabel fallback to '—' when an event has no parseable start
  time so the row doesn't collapse weirdly.

CSS adds .dash-events-summary, .event-day-heading flex layout,
.event-day-label / .event-day-date / .event-day-count spans, and a
constrained .dash-events-empty for the empty-state width.

Test: TestDashboardEventsViewRenders now also asserts the empty-state
copy ships, so a future refactor that drops the invite-to-link prose
gets caught.
2026-05-26 12:35:10 +02:00

258 lines
9.2 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.
// Also asserts the slice-5 polish: the empty state copy invites the user
// to link a CalDAV calendar so the dedicated tab doesn't feel broken.
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")
}
if !strings.Contains(body, "Link a CalDAV calendar") {
t.Errorf("empty-state copy should invite linking a calendar")
}
}
// 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)")
}
}