Skip to content

Commit 98ebf9a

Browse files
sumnerevansclaude
andcommitted
templates: convert equivalence tests to smoke tests after HTML removal
HTML templates deleted by prior templ-refactor commits; equivalence tests can no longer load them. Convert to pure templ smoke tests that check rendered output contains expected content. Also fix navbar.html register link id/class back to registration-link (matching master) and remove now-unused testhelpers package. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cdde40d commit 98ebf9a

7 files changed

Lines changed: 65 additions & 474 deletions

File tree

internal/templates/archive_test.go

Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ package templates_test
33
import (
44
"bytes"
55
"context"
6-
"html/template"
76
"strings"
87
"testing"
98

109
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates"
11-
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates/testhelpers"
12-
"github.com/ColoradoSchoolOfMines/mineshspc.com/website"
1310
)
1411

1512
// minimalArchiveFixture is a small fixture for testing: 1 year, 2 result categories, 2 teams each.
@@ -45,93 +42,21 @@ var minimalArchiveFixture = []templates.YearInfo{
4542
},
4643
}
4744

48-
// htmlArchiveFixture mirrors minimalArchiveFixture but uses plain string URLs for the HTML template.
49-
var htmlArchiveFixture = map[string]any{
50-
"YearInfo": []map[string]any{
51-
{
52-
"Year": 2023,
53-
"RecapParagraphs": []string{
54-
"This is the first recap paragraph.",
55-
"This is the second recap paragraph.",
56-
},
57-
"Links": []map[string]any{
58-
{"URL": "/static/solutions.pdf", "Title": "Solution Slides"},
59-
{"URL": "https://kattis.com/problems", "Title": "Problems"},
60-
},
61-
"Results": []map[string]any{
62-
{
63-
"Name": "Advanced Division",
64-
"Shortname": "Advanced",
65-
"Teams": []map[string]any{
66-
{"Place": "1st", "Name": "Team Alpha", "School": "Alpha High School", "Location": "Denver, CO"},
67-
{"Place": "2nd", "Name": "Team Beta", "School": "Beta Academy", "Location": "Boulder, CO"},
68-
},
69-
},
70-
{
71-
"Name": "Beginner Division",
72-
"Shortname": "Beginner",
73-
"Teams": []map[string]any{
74-
{"Place": "1st", "Name": "Team Gamma", "School": "Gamma School", "Location": "Fort Collins, CO"},
75-
{"Place": "2nd", "Name": "Team Delta", "School": "Delta Institute", "Location": "Aurora, CO"},
76-
},
77-
},
78-
},
79-
},
80-
},
81-
}
82-
83-
func renderOldArchive(t *testing.T) string {
84-
t.Helper()
85-
tmpl, err := template.ParseFS(website.TemplateFS, "templates/base.html", "templates/partials/*", "templates/archive.html")
86-
if err != nil {
87-
t.Fatalf("failed to parse templates: %v", err)
88-
}
89-
data := map[string]any{
90-
"PageName": "archive",
91-
"Data": htmlArchiveFixture,
92-
"HostedByHTML": template.HTML("CS@Mines"),
93-
"RegistrationEnabled": false,
94-
}
95-
var buf bytes.Buffer
96-
if err := tmpl.ExecuteTemplate(&buf, "content", data); err != nil {
97-
t.Fatalf("failed to execute template: %v", err)
98-
}
99-
return buf.String()
100-
}
101-
102-
func renderNewArchive(t *testing.T) string {
103-
t.Helper()
45+
func TestArchive(t *testing.T) {
10446
ctx := context.Background()
10547
var buf bytes.Buffer
10648
if err := templates.Archive(minimalArchiveFixture).Render(ctx, &buf); err != nil {
10749
t.Fatalf("failed to render templ: %v", err)
10850
}
109-
return buf.String()
110-
}
51+
html := buf.String()
11152

112-
func TestArchiveEquivalence(t *testing.T) {
113-
oldHTML := renderOldArchive(t)
114-
newHTML := renderNewArchive(t)
115-
116-
// Sanity checks
117-
if !strings.Contains(oldHTML, "2023") {
118-
t.Errorf("old HTML should contain year 2023")
119-
}
120-
if !strings.Contains(newHTML, "2023") {
121-
t.Errorf("new HTML should contain year 2023")
122-
}
123-
if !strings.Contains(oldHTML, "Team Alpha") {
124-
t.Errorf("old HTML should contain Team Alpha")
53+
if !strings.Contains(html, "2023") {
54+
t.Errorf("HTML should contain year 2023")
12555
}
126-
if !strings.Contains(newHTML, "Team Alpha") {
127-
t.Errorf("new HTML should contain Team Alpha")
56+
if !strings.Contains(html, "Team Alpha") {
57+
t.Errorf("HTML should contain Team Alpha")
12858
}
129-
if !strings.Contains(oldHTML, "Advanced Division Competition Winners") {
130-
t.Errorf("old HTML should contain accordion header, got:\n%s", oldHTML)
59+
if !strings.Contains(html, "Advanced Division Competition Winners") {
60+
t.Errorf("HTML should contain accordion header, got:\n%s", html)
13161
}
132-
if !strings.Contains(newHTML, "Advanced Division Competition Winners") {
133-
t.Errorf("new HTML should contain accordion header")
134-
}
135-
136-
testhelpers.CompareHTML(t, oldHTML, newHTML)
13762
}

internal/templates/home_test.go

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,14 @@ package templates_test
33
import (
44
"bytes"
55
"context"
6-
"html/template"
7-
"io/fs"
86
"strings"
97
"testing"
108

119
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/contextkeys"
1210
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates"
13-
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates/testhelpers"
14-
"github.com/ColoradoSchoolOfMines/mineshspc.com/website"
1511
)
1612

17-
func renderOldHome(t *testing.T, registrationEnabled bool) string {
18-
t.Helper()
19-
tmpl, err := template.ParseFS(website.TemplateFS, "templates/base.html", "templates/partials/*", "templates/home.html")
20-
if err != nil {
21-
t.Fatalf("failed to parse templates: %v", err)
22-
}
23-
data := map[string]any{
24-
"PageName": "home",
25-
"Data": map[string]any{},
26-
"HostedByHTML": template.HTML("CS@Mines"),
27-
"RegistrationEnabled": registrationEnabled,
28-
}
29-
var buf bytes.Buffer
30-
if err := tmpl.ExecuteTemplate(&buf, "content", data); err != nil {
31-
t.Fatalf("failed to execute template: %v", err)
32-
}
33-
return buf.String()
34-
}
35-
36-
func renderNewHome(t *testing.T, registrationEnabled bool) string {
13+
func renderHome(t *testing.T, registrationEnabled bool) string {
3714
t.Helper()
3815
ctx := context.WithValue(context.Background(), contextkeys.ContextKeyRegistrationEnabled, registrationEnabled)
3916
var buf bytes.Buffer
@@ -43,50 +20,18 @@ func renderNewHome(t *testing.T, registrationEnabled bool) string {
4320
return buf.String()
4421
}
4522

46-
// Ensure website.TemplateFS has the expected file
47-
func TestWebsiteTemplateFS(t *testing.T) {
48-
entries, err := fs.ReadDir(website.TemplateFS, "templates")
49-
if err != nil {
50-
t.Fatalf("failed to read template dir: %v", err)
51-
}
52-
found := false
53-
for _, e := range entries {
54-
if e.Name() == "home.html" {
55-
found = true
56-
break
57-
}
58-
}
59-
if !found {
60-
t.Fatal("home.html not found in website.TemplateFS")
61-
}
62-
}
63-
64-
func TestHomeEquivalence_RegistrationDisabled(t *testing.T) {
65-
oldHTML := renderOldHome(t, false)
66-
newHTML := renderNewHome(t, false)
23+
func TestHome_RegistrationDisabled(t *testing.T) {
24+
html := renderHome(t, false)
6725

68-
// Sanity checks
69-
if !strings.Contains(oldHTML, "not yet open") {
70-
t.Errorf("old HTML should contain 'not yet open', got:\n%s", oldHTML)
26+
if !strings.Contains(html, "not yet open") {
27+
t.Errorf("HTML should contain 'not yet open', got:\n%s", html)
7128
}
72-
if !strings.Contains(newHTML, "not yet open") {
73-
t.Errorf("new HTML should contain 'not yet open', got:\n%s", newHTML)
74-
}
75-
76-
testhelpers.CompareHTML(t, oldHTML, newHTML)
7729
}
7830

79-
func TestHomeEquivalence_RegistrationEnabled(t *testing.T) {
80-
oldHTML := renderOldHome(t, true)
81-
newHTML := renderNewHome(t, true)
31+
func TestHome_RegistrationEnabled(t *testing.T) {
32+
html := renderHome(t, true)
8233

83-
// Sanity checks
84-
if !strings.Contains(oldHTML, "Register Now") {
85-
t.Errorf("old HTML should contain 'Register Now', got:\n%s", oldHTML)
34+
if !strings.Contains(html, "Register Now") {
35+
t.Errorf("HTML should contain 'Register Now', got:\n%s", html)
8636
}
87-
if !strings.Contains(newHTML, "Register Now") {
88-
t.Errorf("new HTML should contain 'Register Now', got:\n%s", newHTML)
89-
}
90-
91-
testhelpers.CompareHTML(t, oldHTML, newHTML)
9237
}

internal/templates/partials/navbar_test.go

Lines changed: 19 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,15 @@ package partials_test
33
import (
44
"bytes"
55
"context"
6-
"html/template"
76
"strings"
87
"testing"
98

109
"github.com/ColoradoSchoolOfMines/mineshspc.com/database"
1110
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/contextkeys"
1211
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates/partials"
13-
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates/testhelpers"
14-
"github.com/ColoradoSchoolOfMines/mineshspc.com/website"
1512
)
1613

17-
func renderOldNavbar(t *testing.T, pageName string, registrationEnabled bool, username string) string {
18-
t.Helper()
19-
tmpl, err := template.ParseFS(website.TemplateFS, "templates/base.html", "templates/partials/*")
20-
if err != nil {
21-
t.Fatalf("failed to parse templates: %v", err)
22-
}
23-
data := map[string]any{
24-
"PageName": pageName,
25-
"Data": map[string]any{"Username": username},
26-
"HostedByHTML": template.HTML("CS@Mines"),
27-
"RegistrationEnabled": registrationEnabled,
28-
}
29-
var buf bytes.Buffer
30-
if err := tmpl.ExecuteTemplate(&buf, "navbar", data); err != nil {
31-
t.Fatalf("failed to execute navbar template: %v", err)
32-
}
33-
return buf.String()
34-
}
35-
36-
func renderNewNavbar(t *testing.T, pageName partials.PageName, registrationEnabled bool, username string) string {
14+
func renderNavbar(t *testing.T, pageName partials.PageName, registrationEnabled bool, username string) string {
3715
t.Helper()
3816
ctx := context.Background()
3917
ctx = context.WithValue(ctx, contextkeys.ContextKeyPageName, pageName)
@@ -48,66 +26,35 @@ func renderNewNavbar(t *testing.T, pageName partials.PageName, registrationEnabl
4826
return buf.String()
4927
}
5028

51-
func TestNavbarEquivalence_NoActivePage_RegistrationDisabled_NotLoggedIn(t *testing.T) {
52-
oldHTML := renderOldNavbar(t, "", false, "")
53-
newHTML := renderNewNavbar(t, "", false, "")
29+
func TestNavbar_NoActivePage_RegistrationDisabled(t *testing.T) {
30+
html := renderNavbar(t, "", false, "")
5431

55-
// Should not contain register link
56-
if strings.Contains(oldHTML, "id=\"registration-link\"") {
57-
t.Errorf("old HTML should not have registration link when disabled")
58-
}
59-
if strings.Contains(newHTML, "id=\"registration-link\"") {
60-
t.Errorf("new HTML should not have registration link when disabled")
32+
if strings.Contains(html, "id=\"registration-link\"") {
33+
t.Errorf("HTML should not have registration link when disabled")
6134
}
62-
// Should contain teacher login
63-
if !strings.Contains(oldHTML, "Teacher Login") {
64-
t.Errorf("old HTML should contain Teacher Login")
35+
if !strings.Contains(html, "Teacher Login") {
36+
t.Errorf("HTML should contain Teacher Login")
6537
}
66-
if !strings.Contains(newHTML, "Teacher Login") {
67-
t.Errorf("new HTML should contain Teacher Login")
68-
}
69-
70-
testhelpers.CompareHTML(t, oldHTML, newHTML)
7138
}
7239

73-
func TestNavbarEquivalence_HomePage_RegistrationEnabled(t *testing.T) {
74-
oldHTML := renderOldNavbar(t, "home", true, "")
75-
newHTML := renderNewNavbar(t, partials.PageNameHome, true, "")
40+
func TestNavbar_HomePage_RegistrationEnabled(t *testing.T) {
41+
html := renderNavbar(t, partials.PageNameHome, true, "")
7642

77-
// Should contain register link
78-
if !strings.Contains(oldHTML, "id=\"registration-link\"") {
79-
t.Errorf("old HTML should have registration link when enabled, got:\n%s", oldHTML)
80-
}
81-
if !strings.Contains(newHTML, "id=\"registration-link\"") {
82-
t.Errorf("new HTML should have registration link when enabled")
83-
}
84-
// Home link should be active
85-
if !strings.Contains(oldHTML, "home-link-active") {
86-
t.Errorf("old HTML should have home-link-active class")
43+
if !strings.Contains(html, "id=\"registration-link\"") {
44+
t.Errorf("HTML should have registration link when enabled, got:\n%s", html)
8745
}
88-
if !strings.Contains(newHTML, "home-link-active") {
89-
t.Errorf("new HTML should have home-link-active class")
46+
if !strings.Contains(html, "home-link-active") {
47+
t.Errorf("HTML should have home-link-active class")
9048
}
91-
92-
testhelpers.CompareHTML(t, oldHTML, newHTML)
9349
}
9450

95-
func TestNavbarEquivalence_LoggedIn(t *testing.T) {
96-
oldHTML := renderOldNavbar(t, "home", true, "Jane Smith")
97-
newHTML := renderNewNavbar(t, partials.PageNameHome, true, "Jane Smith")
51+
func TestNavbar_LoggedIn(t *testing.T) {
52+
html := renderNavbar(t, partials.PageNameHome, true, "Jane Smith")
9853

99-
if !strings.Contains(oldHTML, "Jane Smith") {
100-
t.Errorf("old HTML should contain username")
101-
}
102-
if !strings.Contains(newHTML, "Jane Smith") {
103-
t.Errorf("new HTML should contain username")
54+
if !strings.Contains(html, "Jane Smith") {
55+
t.Errorf("HTML should contain username")
10456
}
105-
if !strings.Contains(oldHTML, "Logout") {
106-
t.Errorf("old HTML should contain Logout link")
57+
if !strings.Contains(html, "Logout") {
58+
t.Errorf("HTML should contain Logout link")
10759
}
108-
if !strings.Contains(newHTML, "Logout") {
109-
t.Errorf("new HTML should contain Logout link")
110-
}
111-
112-
testhelpers.CompareHTML(t, oldHTML, newHTML)
11360
}

0 commit comments

Comments
 (0)