main summary_render_test.go
Eric Bower  ·  2026-02-25
 1package patchbin
 2
 3import (
 4	"bytes"
 5	"testing"
 6)
 7
 8func TestSemanticSummaryTemplateRenders(t *testing.T) {
 9	files, _, err := ParsePatch(sampleDiff)
10	if err != nil {
11		t.Fatalf("ParsePatch: %v", err)
12	}
13
14	semanticChanges := AnalyzeSemanticChanges(files[0])
15	for i := range semanticChanges {
16		semanticChanges[i].HunkAnchor = hunkAnchor(1, "foo.go", semanticChanges[i].HunkIndex)
17	}
18
19	summary := SummarizeSemanticChanges(SemanticSummary{}, "foo.go", true, semanticChanges)
20	summary = SummarizeSemanticChanges(summary, "go.sum", false, nil)
21
22	pf := &PatchFile{
23		File:            files[0],
24		DisplayName:     "foo.go",
25		FileAnchor:      "patch-1-foo.go",
26		Adds:            5,
27		Dels:            2,
28		SemanticChanges: semanticChanges,
29	}
30
31	pd := &PatchData{
32		PatchFiles:      []*PatchFile{pf},
33		SemanticSummary: summary,
34	}
35
36	tmpl := getTemplate("pr.html")
37	summaryTmpl := tmpl.Lookup("semantic-summary")
38	if summaryTmpl == nil {
39		t.Fatalf("semantic-summary template not found")
40	}
41
42	var buf bytes.Buffer
43	if err := summaryTmpl.Execute(&buf, pd); err != nil {
44		t.Fatalf("execute: %v", err)
45	}
46
47	out := buf.String()
48	t.Logf("rendered output:\n%s", out)
49
50	for _, want := range []string{
51		"Semantic diff summary",
52		"foo.go",
53		"1 added",
54		"1 signature changed",
55		"1 analyzed file",
56		"1 file skipped",
57	} {
58		if !bytes.Contains(buf.Bytes(), []byte(want)) {
59			t.Errorf("expected output to contain %q", want)
60		}
61	}
62}