package server import ( "bytes" "html/template" "io/fs" "strings" "testing" "git.coopcloud.tech/wiki-cafe/member-console/internal/embeds" "git.coopcloud.tech/wiki-cafe/member-console/internal/web" ) // renderGrantsIndex executes the real operator_grants.html template with the // given data, mirroring renderOrgEnrollment (operator_enrollment_render_test.go) // so the grants index and the org-detail composite are both exercised // against their real templates rather than a stub. func renderGrantsIndex(t *testing.T, data GrantsData) string { t.Helper() partialsSub, err := fs.Sub(embeds.Templates, "templates/partials") if err != nil { t.Fatalf("fs.Sub partials: %v", err) } tmpl := template.New("operator").Funcs(template.FuncMap{ "renderBody": func(string, any) (template.HTML, error) { return "", nil }, "routeURL": web.RouteURL, "stripeEntityURL": func(string, string) string { return "" }, "fieldErr": func(activeForm, expectedForm, activeScope, expectedScope string, errs web.FieldErrors, field string) string { return "" }, }) tmpl, err = tmpl.ParseFS(partialsSub, "operator_*.html") if err != nil { t.Fatalf("ParseFS: %v", err) } var buf bytes.Buffer if err := tmpl.ExecuteTemplate(&buf, "operator_grants.html", data); err != nil { t.Fatalf("ExecuteTemplate: %v", err) } return buf.String() } // TestGrantsIndex_RendersDeliveryStateNotLedgerBadge covers // ux-honest-surfaces UX-3: /operator/grants must render the derived // delivery state (live/superseded/inactive), not the raw grants.status // ledger as a green "active" badge under a bare "Status" header, and must // carry the ledger-vs-delivery explanation and lineage. func TestGrantsIndex_RendersDeliveryStateNotLedgerBadge(t *testing.T) { data := GrantsData{ Grants: []GrantViewModel{ {GrantID: "g-old", OrgName: "Acme Co", ProductName: "Legacy Plan", GrantReason: "manual", Status: "active", CreatedAt: "Jan 1, 2026", DeliveryState: "superseded", ReplacedByGrantID: "g-new", ReplacedByLabel: "Standard Plan"}, {GrantID: "g-new", OrgName: "Acme Co", ProductName: "Standard Plan", GrantReason: "manual", Status: "active", CreatedAt: "Jan 2, 2026", DeliveryState: "live", ExtendsGrantID: "g-old", ExtendsLabel: "Legacy Plan"}, {GrantID: "g-gone", OrgName: "Beta Org", ProductName: "Trial", GrantReason: "evaluation", Status: "revoked", CreatedAt: "Jan 3, 2026", DeliveryState: "inactive"}, }, } out := renderGrantsIndex(t, data) if !strings.Contains(out, "Delivery") { t.Errorf("expected a Delivery column header, got:\n%s", out) } if strings.Contains(out, "Status") { t.Errorf("a bare Status header must not render (UX-3): \n%s", out) } if !strings.Contains(out, ">Live<") || !strings.Contains(out, ">Superseded<") || !strings.Contains(out, ">Inactive<") { t.Errorf("expected all three delivery-state badges to render, got:\n%s", out) } if strings.Contains(out, `badge bg-success">active<`) || strings.Contains(out, `badge bg-danger">revoked<`) { t.Errorf("grants.status must not render as a colored state badge:\n%s", out) } if !strings.Contains(out, "replaced by Standard Plan") { t.Errorf("expected the superseded row's lineage (replaced by), got:\n%s", out) } if !strings.Contains(out, "extends Legacy Plan") { t.Errorf("expected the live row's lineage (extends), got:\n%s", out) } if !strings.Contains(out, "issuance ledger") { t.Errorf("expected the in-place ledger-vs-delivery explanation, got:\n%s", out) } } // TestGrantsIndex_EmptyState covers the genuinely-empty case still renders // its own message rather than an empty table. func TestGrantsIndex_EmptyState(t *testing.T) { out := renderGrantsIndex(t, GrantsData{}) if !strings.Contains(out, "No grants found.") { t.Errorf("expected the empty-state message, got:\n%s", out) } }