Files
member-console/internal/server/forms_doc_test.go
T
cgalo5758 88db730fcc Add dual licensing and SPDX headers
Introduce a commercial license option alongside AGPL-3.0-only, require a
CLA for contributors, and document the terms in COMMERCIAL.md and
NOTICE. Add a script to stamp SPDX headers on Go files and apply it
across the tree.
2026-09-06 02:29:42 -05:00

124 lines
4.0 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
package server
import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"git.coopcloud.tech/wiki-cafe/member-console/internal/forms"
)
// The design system's registered-forms table (design D17; docs
// "docs/design-system.md" §8 "Registered forms") is checked against the
// registry the way internal/config/config_reference_test.go checks
// docs/environment-reference.md against every Viper key: a completeness
// check, not a correctness check. It does not verify a form's fields, its
// copy or its route, only that the form's name, kind and family are not
// silently undocumented, and that a documented form still exists.
// formsDocRowRE matches one table row under "### Registered forms":
// "| `name` | Kind[^footnote] | Family | ... |". The kind cell may carry
// a trailing footnote marker ("Create[^edit]"), stripped before compare.
var formsDocRowRE = regexp.MustCompile("^\\|\\s*`([a-zA-Z0-9_.-]+)`\\s*\\|\\s*([A-Za-z-]+)(?:\\[\\^[a-z]+\\])?\\s*\\|\\s*([A-Za-z]+)\\s*\\|")
// docFormsRepoRoot locates the repository root from this test file's own
// path, so the test works regardless of the working directory it runs
// under, mirroring config_reference_test.go's repoRootFromCaller.
func docFormsRepoRoot(t *testing.T) string {
t.Helper()
_, thisFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller(0) failed; cannot locate repo root")
}
// This file lives at <repoRoot>/internal/server/forms_doc_test.go.
return filepath.Clean(filepath.Join(filepath.Dir(thisFile), "..", ".."))
}
// docFormRow is one parsed table row.
type docFormRow struct {
Name, Kind, Family string
}
// parseFormsDocTable reads docs/design-system.md and returns every row of
// the "Registered forms" table, keyed by form name.
func parseFormsDocTable(t *testing.T, docPath string) map[string]docFormRow {
t.Helper()
body, err := os.ReadFile(docPath)
if err != nil {
t.Fatalf("reading %s: %v", docPath, err)
}
lines := strings.Split(string(body), "\n")
start := -1
for i, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "### Registered forms") {
start = i
break
}
}
if start == -1 {
t.Fatalf("%s has no \"### Registered forms\" section", docPath)
}
rows := map[string]docFormRow{}
for _, line := range lines[start:] {
m := formsDocRowRE.FindStringSubmatch(line)
if m == nil {
continue
}
rows[m[1]] = docFormRow{
Name: m[1],
Kind: strings.ToLower(m[2]),
Family: strings.ToLower(m[3]),
}
}
if len(rows) == 0 {
t.Fatalf("%s's Registered forms table has no parsed rows; the table markup may have drifted from formsDocRowRE", docPath)
}
return rows
}
// TestDesignSystemDocumentsEveryRegisteredForm asserts every registered
// form appears in the table with its kind and family, and that every
// row in the table still names a registered form (design D17).
func TestDesignSystemDocumentsEveryRegisteredForm(t *testing.T) {
root := docFormsRepoRoot(t)
docPath := filepath.Join(root, "docs", "design-system.md")
rows := parseFormsDocTable(t, docPath)
all := forms.All()
if len(all) == 0 {
t.Fatal("no forms are registered; the declarations register as their package initialises")
}
for _, spec := range all {
row, ok := rows[spec.Name]
if !ok {
t.Errorf("%s is registered but not listed in docs/design-system.md's Registered forms table; add a row naming its kind and family", spec.Name)
continue
}
if row.Kind != string(spec.Kind) {
t.Errorf("%s: table names kind %q, registry says %q", spec.Name, row.Kind, spec.Kind)
}
if row.Family != string(spec.Family) {
t.Errorf("%s: table names family %q, registry says %q", spec.Name, row.Family, spec.Family)
}
}
registered := map[string]bool{}
for _, spec := range all {
registered[spec.Name] = true
}
for name := range rows {
if !registered[name] {
t.Errorf("docs/design-system.md's Registered forms table lists %q, which no package registers; remove the row", name)
}
}
}