main
template_render_test.go
Eric Bower
·
2026-02-25
1package patchbin
2
3import (
4 "bytes"
5 "html/template"
6 "testing"
7)
8
9func TestPatchFileTemplateRendersLineDiffOnly(t *testing.T) {
10 files, _, err := ParsePatch(sampleDiff)
11 if err != nil {
12 t.Fatalf("ParsePatch: %v", err)
13 }
14
15 semanticChanges := AnalyzeSemanticChanges(files[0])
16 for i := range semanticChanges {
17 semanticChanges[i].HunkAnchor = hunkAnchor(1, "foo.go", semanticChanges[i].HunkIndex)
18 }
19
20 hunks := make([]PatchHunk, 0, len(files[0].TextFragments))
21 for i, frag := range files[0].TextFragments {
22 hunks = append(hunks, PatchHunk{
23 Anchor: hunkAnchor(1, "foo.go", i),
24 DiffText: template.HTML(frag.String()),
25 })
26 }
27
28 pf := &PatchFile{
29 File: files[0],
30 DisplayName: "foo.go",
31 FileAnchor: "patch-1-foo.go",
32 Adds: 5,
33 Dels: 2,
34 Hunks: hunks,
35 SemanticChanges: semanticChanges,
36 }
37
38 tmpl := getTemplate("pr.html")
39 if tmpl == nil {
40 t.Fatalf("getTemplate returned nil")
41 }
42
43 patchFileTmpl := tmpl.Lookup("patch-file")
44 if patchFileTmpl == nil {
45 t.Fatalf("patch-file template not found")
46 }
47
48 var buf bytes.Buffer
49 if err := patchFileTmpl.Execute(&buf, pf); err != nil {
50 t.Fatalf("execute: %v", err)
51 }
52
53 out := buf.String()
54 t.Logf("rendered output:\n%s", out)
55
56 // Per-file details should be pure line-diff now -- the semantic
57 // breakdown lives only in the patch-level summary, so repeating it
58 // here would be redundant.
59 if bytes.Contains(buf.Bytes(), []byte("semantic-diff")) {
60 t.Errorf("expected no semantic-diff content inside patch-file, semantic breakdown belongs in the summary only")
61 }
62 if bytes.Contains(buf.Bytes(), []byte("Show line diff")) {
63 t.Errorf("expected no nested line-diff details, file details should be a flat line-diff")
64 }
65 if !bytes.Contains(buf.Bytes(), []byte("patch-1-foo.go-hunk-0")) {
66 t.Errorf("expected hunk anchor in rendered line diff")
67 }
68}