1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
package main
import (
"strings"
"testing"
)
// testAssetURL is a stand-in for the preview's URL builder, chosen to look
// nothing like it so a hardcoded route in the renderer would show up here.
func testAssetURL(a *Asset, variant string) string {
return "ASSET:" + variant
}
func TestRenderMarkdown(t *testing.T) {
html, err := renderMarkdown("# Title\n\nSome *emphasis* and a [link](https://example.com).\n")
if err != nil {
t.Fatalf("renderMarkdown: %v", err)
}
for _, want := range []string{"<h1", "Title", "<em>emphasis</em>", `href="https://example.com"`} {
if !strings.Contains(html, want) {
t.Errorf("missing %q in:\n%s", want, html)
}
}
}
// GFM extensions are enabled deliberately; tables in particular are the reason.
func TestRenderMarkdownGFMTable(t *testing.T) {
html, err := renderMarkdown("| a | b |\n|---|---|\n| 1 | 2 |\n")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, "<table>") {
t.Errorf("GFM table not rendered:\n%s", html)
}
}
// Headings get ids so post sections are linkable.
func TestRenderMarkdownHeadingIDs(t *testing.T) {
html, err := renderMarkdown("## Why Go?\n")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, `id="why-go"`) {
t.Errorf("no heading id:\n%s", html)
}
}
func TestRenderCode(t *testing.T) {
html, err := renderCode("func main() {}\n", "go")
if err != nil {
t.Fatalf("renderCode: %v", err)
}
if !strings.Contains(html, "<pre") {
t.Errorf("no <pre> in:\n%s", html)
}
// Classes rather than inline styles, which is what lets one stylesheet
// carry both the light and dark palettes.
if strings.Contains(html, "style=\"color:") {
t.Errorf("inline styles emitted; expected CSS classes:\n%s", html)
}
}
// An unknown language must still render, falling back rather than erroring:
// rejecting a code block over a language tag would be a poor trade.
func TestRenderCodeUnknownLanguage(t *testing.T) {
for _, lang := range []string{"", "not-a-real-language", "brainfuck"} {
html, err := renderCode("some text\n", lang)
if err != nil {
t.Errorf("language %q: %v", lang, err)
}
if !strings.Contains(html, "some text") {
t.Errorf("language %q: content missing", lang)
}
}
}
func TestRenderImage(t *testing.T) {
b := &Block{
Kind: KindImage,
Meta: BlockMeta{Alt: "a wide shot", Caption: "The thing"},
Asset: &Asset{ID: 7, Kind: AssetImage, Width: 1600, Height: 900},
}
html, err := renderImage(b, testAssetURL)
if err != nil {
t.Fatalf("renderImage: %v", err)
}
for _, want := range []string{
`alt="a wide shot"`,
`<figcaption>The thing</figcaption>`,
`width="1600"`, `height="900"`, // reserve layout space, avoid shift
`ASSET:webp-800 800w`, `ASSET:webp-1600 1600w`,
`loading="lazy"`,
} {
if !strings.Contains(html, want) {
t.Errorf("missing %q in:\n%s", want, html)
}
}
}
// Alt text and captions come from the author and must never become markup.
func TestRenderImageEscapesText(t *testing.T) {
b := &Block{
Kind: KindImage,
Meta: BlockMeta{Alt: `"><script>alert(1)</script>`, Caption: "<b>bold</b>"},
Asset: &Asset{ID: 1, Kind: AssetImage, Width: 10, Height: 10},
}
html, err := renderImage(b, testAssetURL)
if err != nil {
t.Fatal(err)
}
if strings.Contains(html, "<script>") || strings.Contains(html, "<b>bold</b>") {
t.Errorf("author text was not escaped:\n%s", html)
}
}
func TestRenderSTL(t *testing.T) {
b := &Block{
Kind: KindSTL,
Meta: BlockMeta{Caption: "A bracket"},
Asset: &Asset{ID: 3, Kind: AssetSTL, Filename: "bracket.stl"},
}
html, err := renderSTL(b, testAssetURL)
if err != nil {
t.Fatalf("renderSTL: %v", err)
}
for _, want := range []string{
`class="stl-viewer"`,
`data-mesh="ASSET:mesh"`,
`stl-noscript`, // says what is needed when no script runs
`ASSET:original`, // the download link
"bracket.stl",
} {
if !strings.Contains(html, want) {
t.Errorf("missing %q in:\n%s", want, html)
}
}
}
// The download link must sit outside the viewer, so the original file stays
// reachable whether or not the viewer starts — and equally when it works fine
// and the reader simply wants the STL.
func TestRenderSTLDownloadIsOutsideViewer(t *testing.T) {
b := &Block{
Kind: KindSTL,
Asset: &Asset{ID: 3, Kind: AssetSTL, Filename: "bracket.stl"},
}
html, err := renderSTL(b, testAssetURL)
if err != nil {
t.Fatal(err)
}
viewerEnd := strings.Index(html, "</div>")
download := strings.Index(html, "stl-download")
if viewerEnd < 0 || download < 0 {
t.Fatalf("expected a viewer div and a download link, got:\n%s", html)
}
if download < viewerEnd {
t.Errorf("download link is inside the viewer:\n%s", html)
}
if !strings.Contains(html, `download>`) {
t.Errorf("download link lacks the download attribute:\n%s", html)
}
}
// A block whose asset is missing is a data inconsistency; it must be reported,
// not rendered as a broken image.
func TestRenderAssetBlockWithoutAsset(t *testing.T) {
for _, kind := range []string{KindImage, KindSTL} {
if _, err := renderBlock(&Block{Kind: kind}, testAssetURL); err == nil {
t.Errorf("%s block without an asset rendered without error", kind)
}
}
}
// One broken block must not take the whole post down with it.
func TestRenderPostIsolatesBlockFailures(t *testing.T) {
blocks := []Block{
{Kind: KindMarkdown, Content: "before"},
{Kind: KindImage}, // no asset — will fail
{Kind: KindMarkdown, Content: "after"},
}
html, _, err := renderPost(blocks, testAssetURL)
if err != nil {
t.Fatalf("renderPost: %v", err)
}
for _, want := range []string{"before", "after", "block-error"} {
if !strings.Contains(html, want) {
t.Errorf("missing %q in:\n%s", want, html)
}
}
}
// The viewer script is only loaded on pages that contain a model.
func TestRenderPostReportsSTLUsage(t *testing.T) {
noSTL := []Block{{Kind: KindMarkdown, Content: "text"}}
if _, uses, _ := renderPost(noSTL, testAssetURL); uses {
t.Error("reported STL usage for a post without a model")
}
withSTL := []Block{{Kind: KindSTL, Asset: &Asset{ID: 1, Filename: "x.stl"}}}
if _, uses, _ := renderPost(withSTL, testAssetURL); !uses {
t.Error("did not report STL usage for a post with a model")
}
}
// The renderer must not know where assets live — that seam is what lets a
// static export reuse it unchanged.
func TestRendererUsesSuppliedAssetURLs(t *testing.T) {
blocks := []Block{
{Kind: KindImage, Asset: &Asset{ID: 42, Kind: AssetImage, Width: 100, Height: 100}},
{Kind: KindSTL, Asset: &Asset{ID: 43, Kind: AssetSTL, Filename: "m.stl"}},
}
html, _, err := renderPost(blocks, func(a *Asset, variant string) string {
return "https://cdn.example.com/" + variant + ".bin"
})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, "https://cdn.example.com/") {
t.Errorf("supplied asset URLs not used:\n%s", html)
}
if strings.Contains(html, "/asset/") {
t.Errorf("renderer emitted a hardcoded server route:\n%s", html)
}
}
func TestChromaCSSHasBothPalettes(t *testing.T) {
css := chromaCSS()
if !strings.Contains(css, "prefers-color-scheme: dark") {
t.Error("no dark palette in the generated stylesheet")
}
if len(css) < 500 {
t.Errorf("stylesheet suspiciously short (%d bytes)", len(css))
}
}
|