// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial // SPDX-FileCopyrightText: 2025-2026 Christian Galo 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 "" }, "helpIcon": helpIcon, }) tmpl, err = web.ParseUIPartials(template.Must(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"}, {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("the live row must not carry an extends line, which only repeats its product (maintainer, 2026-09-05), got:\n%s", out) } if strings.Contains(out, "Issuance is what the record says happened to the grant.") { t.Errorf("the standing Issuance-vs-Delivery paragraph must be replaced by header help icons (design D23):\n%s", out) } if !strings.Contains(out, `data-bs-content="What the record says happened to the grant: active, revoked, or expired."`) { t.Errorf("expected the Issuance column header's help icon, got:\n%s", out) } if !strings.Contains(out, `data-bs-content="Whether the grant is delivering into the pool now."`) { t.Errorf("expected the Delivery column header's help icon, got:\n%s", out) } if strings.Contains(out, "Ledger") { t.Errorf("the 'Ledger' header must be renamed 'Issuance' (design D5):\n%s", out) } if !strings.Contains(out, "Issuance ") || !strings.Contains(out, "Granted by") { t.Errorf("expected Issuance and Granted by column headers, got:\n%s", out) } } // TestGrantsIndex_OrganizationCellLinksToComposite covers design D5 / // chrome-conventions row navigation: the grant's home is a section of the // organization composite, not a page of its own, so the primary cell links // to the organization's plan-grants-panel anchor. func TestGrantsIndex_OrganizationCellLinksToComposite(t *testing.T) { out := renderGrantsIndex(t, GrantsData{ Grants: []GrantViewModel{ {GrantID: "g1", OrgID: "org-123", OrgName: "Acme Co", ProductName: "Pro", GrantReason: "manual", Status: "active", DeliveryState: "live", CreatedAt: "Jan 1, 2026"}, }, }) if !strings.Contains(out, `Acme Co`) { t.Errorf("expected the organization cell to link to the composite's grants section, got:\n%s", out) } } // TestGrantsIndex_GrantedByColumn covers ACC-28 / plan-enrollment-administration's // "A row names its grantor and reaches its home": an operator-issued grant // names and links to the granting person, while a system-issued grant (no // granting person recorded) renders the system marker instead. func TestGrantsIndex_GrantedByColumn(t *testing.T) { out := renderGrantsIndex(t, GrantsData{ Grants: []GrantViewModel{ {GrantID: "g1", OrgID: "org-1", OrgName: "Acme Co", ProductName: "Pro", GrantReason: "manual", Status: "active", DeliveryState: "live", CreatedAt: "Jan 1, 2026", GrantedByPersonID: "p-1", GrantedByName: "Jamie Rivera"}, {GrantID: "g2", OrgID: "org-1", OrgName: "Acme Co", ProductName: "Starter", GrantReason: "default", Status: "active", DeliveryState: "live", CreatedAt: "Jan 2, 2026"}, }, }) if !strings.Contains(out, `Jamie Rivera`) { t.Errorf("expected the operator-issued grant to name and link its grantor, got:\n%s", out) } if !strings.Contains(out, `System`) { t.Errorf("expected the system-issued grant to render the system marker, got:\n%s", out) } } // TestGrantsIndex_NoteLine covers design D7: a grant's audit note renders as // a muted line under its reason, and a grant with no note carries no such // line. func TestGrantsIndex_NoteLine(t *testing.T) { out := renderGrantsIndex(t, GrantsData{ Grants: []GrantViewModel{ {GrantID: "g-note", OrgName: "Acme Co", ProductName: "Pro", GrantReason: "manual", Note: "Comped for the pilot cohort.", Status: "active", DeliveryState: "live", CreatedAt: "Jan 1, 2026"}, {GrantID: "g-bare", OrgName: "Acme Co", ProductName: "Starter", GrantReason: "default", Status: "active", DeliveryState: "live", CreatedAt: "Jan 2, 2026"}, }, }) if !strings.Contains(out, `manual
Comped for the pilot cohort.
`) { t.Errorf("expected the noted grant's reason cell to carry the muted note line, got:\n%s", out) } if !strings.Contains(out, "default") { t.Errorf("expected the note-less grant's reason cell to carry no note line, got:\n%s", out) } } // TestGrantsIndex_EmptyState covers the genuinely-empty case still renders // its own message rather than an empty table: blocked (warning alert) while // no product is published, the plain empty state once one is. func TestGrantsIndex_EmptyState(t *testing.T) { out := renderGrantsIndex(t, GrantsData{}) if !strings.Contains(out, "no product has been published yet") { t.Errorf("expected the blocked empty state with zero products, got:\n%s", out) } out = renderGrantsIndex(t, GrantsData{Products: []ProductViewModel{{ProductID: "p1", Name: "Pro"}}}) if !strings.Contains(out, "No grants issued yet.") { t.Errorf("expected the empty-state message, got:\n%s", out) } }