-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_file_test.go
More file actions
304 lines (286 loc) · 10 KB
/
Copy pathconfig_file_test.go
File metadata and controls
304 lines (286 loc) · 10 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package syralit
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestFileConfigPrecedence(t *testing.T) {
dir := t.TempDir()
toml := `title = "From File"
host = "0.0.0.0"
port = 9000
[theme]
mode = "dark"
accent = "#FF0000"
radius = "8px"
`
if err := os.WriteFile(filepath.Join(dir, ConfigFileName), []byte(toml), 0o644); err != nil {
t.Fatal(err)
}
fc := loadFileConfig(dir)
if fc == nil {
t.Fatal("expected config, got nil")
}
// Unset fields are filled from the file.
cfg := Config{}
fc.applyToConfig(&cfg)
if cfg.Title != "From File" || cfg.Port != 9000 || cfg.Host != "0.0.0.0" {
t.Fatalf("file values not applied: %+v", cfg)
}
if cfg.Theme.Mode != "dark" || cfg.Theme.Accent != "#FF0000" || cfg.Theme.Radius != "8px" {
t.Fatalf("theme not applied: %+v", cfg.Theme)
}
// Explicit code values win over the file.
cfg2 := Config{Title: "Explicit", Port: 1234}
fc.applyToConfig(&cfg2)
if cfg2.Title != "Explicit" || cfg2.Port != 1234 {
t.Fatalf("explicit values overridden by file: %+v", cfg2)
}
if cfg2.Host != "0.0.0.0" { // unset field still filled
t.Fatalf("expected host from file, got %q", cfg2.Host)
}
}
func TestLoadFileConfigMissing(t *testing.T) {
if fc := loadFileConfig(t.TempDir()); fc != nil {
t.Fatalf("expected nil for missing config, got %+v", fc)
}
}
func TestRenderIndexTheme(t *testing.T) {
html := renderIndex("My App", Theme{Mode: "dark", ThemeStyle: ThemeStyle{Accent: "#7C3AED", Radius: "12px"}})
for _, want := range []string{
"<title>My App</title>",
`data-theme="dark"`,
"--sy-accent:#7C3AED",
"--sy-radius:12px",
} {
if !strings.Contains(html, want) {
t.Fatalf("rendered index missing %q:\n%s", want, html)
}
}
}
func TestRenderIndexFontTheme(t *testing.T) {
html := renderIndex("Fonts", Theme{
ThemeStyle: ThemeStyle{
Font: "serif",
HeadingFont: `"My Brand", sans-serif`,
CodeFont: "monospace",
BaseFontSize: 18,
BaseFontWeight: 350,
HeadingFontSizes: []string{"2.2rem", "1.6rem"},
HeadingFontWeights: []int{800},
CodeFontSize: "0.875rem",
CodeFontWeight: 500,
},
FontFaces: []FontFace{{
Family: "My Brand", URL: "https://example.com/brand.woff2",
Weight: "200 900", Style: "italic", UnicodeRange: "U+0-10FFFF",
}},
Sidebar: ThemeStyle{Font: "sans-serif"},
})
for _, want := range []string{
`--sy-font:"Source Serif 4"`,
`--sy-font-heading:"My Brand", sans-serif`,
`--sy-font-code:"Source Code Pro"`,
"html{font-size:18px}",
"body{font-weight:350}",
".sy-title, .sy-markdown h1{font-size:2.2rem;font-weight:800}",
".sy-header, .sy-markdown h2{font-size:1.6rem}",
"font-size:0.875rem;font-weight:500}",
`@font-face{font-family:"My Brand";src:url("https://example.com/brand.woff2");font-weight:200 900;font-style:italic;unicode-range:U+0-10FFFF;font-display:swap}`,
`#syralit-sidebar{--sy-font:"Source Sans 3"`,
} {
if !strings.Contains(html, want) {
t.Fatalf("rendered index missing %q:\n%s", want, html)
}
}
}
func TestRenderIndexRejectsUnsafeFontTheme(t *testing.T) {
html := renderIndex("X", Theme{
ThemeStyle: ThemeStyle{Font: "sans}</style><script>alert(1)</script>"},
FontFaces: []FontFace{{
Family: "Evil",
URL: `x") format("woff2"); } </style><script>alert(2)</script>`,
}},
})
if strings.Contains(html, "<script>") {
t.Fatalf("unsafe font value leaked into output:\n%s", html)
}
if strings.Contains(html, "--sy-font:") || strings.Contains(html, "@font-face") {
t.Fatalf("unsafe font theme should have been rejected:\n%s", html)
}
}
func boolPtr(b bool) *bool { return &b }
func TestRenderIndexFullTheme(t *testing.T) {
html := renderIndex("Full", Theme{
ThemeStyle: ThemeStyle{
Accent: "#0F766E",
Radius: "8px",
ButtonRadius: "999px",
BackgroundColor: "#101418",
SecondaryBackgroundColor: "#1a1f26",
TextColor: "#e6e8eb",
LinkColor: "#38bdf8",
LinkUnderline: boolPtr(true),
CodeTextColor: "#a5f3fc",
CodeBackgroundColor: "#0b1020",
BorderColor: "#334155",
DataframeBorderColor: "#475569",
DataframeHeaderBackgroundColor: "#1e293b",
ShowWidgetBorder: boolPtr(false),
RedColor: "#f87171",
BlueBackgroundColor: "#0c4a6e",
GreenTextColor: "#bbf7d0",
},
ShowSidebarBorder: boolPtr(false),
ChartCategoricalColors: []string{"#111111", "#222222"},
ChartSequentialColors: []string{"#000000", "#ffffff"},
Sidebar: ThemeStyle{
Accent: "#f59e0b",
BackgroundColor: "#0a0a0a",
RedColor: "#ff0000",
BaseFontSize: 15,
},
})
for _, want := range []string{
"--sy-accent:#0F766E", "--sy-radius:8px", "--sy-btn-radius:999px",
"--sy-bg:#101418", "--sy-fg:#e6e8eb", "--sy-sidebar-bg:#1a1f26",
"--sy-link:#38bdf8", "--sy-border:#334155",
"--sy-df-border:#475569", "--sy-df-header-bg:#1e293b",
"--sy-color-red:#f87171", "--sy-color-blue-bg:#0c4a6e", "--sy-color-green-text:#bbf7d0",
".sy-link, .sy-markdown a{text-decoration:underline}",
".sy-input, .sy-select, .sy-textarea{border-color:transparent}",
"color:#a5f3fc;background:#0b1020}",
"#syralit-root.has-sidebar #syralit-sidebar{border-right:none}",
// Sidebar scope re-derives aliases so scoped overrides cascade.
"--sy-primary:var(--sy-accent)",
"--sy-color-red-bg:color-mix(in srgb, var(--sy-color-red) 10%, var(--sy-bg))",
"--sy-error:var(--sy-color-red)",
"background:#0a0a0a",
"#syralit-sidebar{font-size:15px}",
`window.__SY_THEME={"chart_categorical_colors":["#111111","#222222"],"chart_sequential_colors":["#000000","#ffffff"]}`,
} {
if !strings.Contains(html, want) {
t.Fatalf("rendered index missing %q:\n%s", want, html)
}
}
}
func TestFileConfigFullTheme(t *testing.T) {
dir := t.TempDir()
toml := `[theme]
background_color = "#101418"
secondary_background_color = "#1a1f26"
text_color = "#e6e8eb"
link_color = "#38bdf8"
link_underline = true
code_text_color = "#a5f3fc"
code_background_color = "#0b1020"
border_color = "#334155"
dataframe_border_color = "#475569"
dataframe_header_background_color = "#1e293b"
show_widget_border = false
show_sidebar_border = false
button_radius = "999px"
red_color = "#f87171"
blue_background_color = "#0c4a6e"
green_text_color = "#bbf7d0"
chart_categorical_colors = ["#111111", "#222222"]
chart_sequential_colors = ["#000000", "#ffffff"]
chart_diverging_colors = ["#ff0000", "#0000ff"]
[theme.sidebar]
accent = "#f59e0b"
background_color = "#0a0a0a"
red_color = "#ff0000"
`
if err := os.WriteFile(filepath.Join(dir, ConfigFileName), []byte(toml), 0o644); err != nil {
t.Fatal(err)
}
fc := loadFileConfig(dir)
if fc == nil {
t.Fatal("expected config, got nil")
}
cfg := Config{}
fc.applyToConfig(&cfg)
th := cfg.Theme
if th.BackgroundColor != "#101418" || th.SecondaryBackgroundColor != "#1a1f26" ||
th.TextColor != "#e6e8eb" || th.LinkColor != "#38bdf8" {
t.Fatalf("base colors not applied: %+v", th.ThemeStyle)
}
if th.LinkUnderline == nil || !*th.LinkUnderline ||
th.ShowWidgetBorder == nil || *th.ShowWidgetBorder ||
th.ShowSidebarBorder == nil || *th.ShowSidebarBorder {
t.Fatalf("bool toggles not applied: %+v", th)
}
if th.CodeTextColor != "#a5f3fc" || th.CodeBackgroundColor != "#0b1020" ||
th.BorderColor != "#334155" || th.DataframeBorderColor != "#475569" ||
th.DataframeHeaderBackgroundColor != "#1e293b" || th.ButtonRadius != "999px" {
t.Fatalf("borders/code/radius not applied: %+v", th.ThemeStyle)
}
if th.RedColor != "#f87171" || th.BlueBackgroundColor != "#0c4a6e" || th.GreenTextColor != "#bbf7d0" {
t.Fatalf("palette not applied: %+v", th.ThemeStyle)
}
if len(th.ChartCategoricalColors) != 2 || len(th.ChartSequentialColors) != 2 || len(th.ChartDivergingColors) != 2 {
t.Fatalf("chart colors not applied: %+v", th)
}
if th.Sidebar.Accent != "#f59e0b" || th.Sidebar.BackgroundColor != "#0a0a0a" || th.Sidebar.RedColor != "#ff0000" {
t.Fatalf("sidebar not applied: %+v", th.Sidebar)
}
}
func TestFileConfigFontTheme(t *testing.T) {
dir := t.TempDir()
toml := `[theme]
font = "serif"
heading_font = "Inter, sans-serif"
code_font = "monospace"
base_font_size = 18
base_font_weight = 350
heading_font_sizes = ["2rem", "1.5rem"]
heading_font_weights = [700, 600]
code_font_size = "0.875rem"
code_font_weight = 500
[[theme.font_faces]]
family = "Inter"
url = "/fonts/inter.woff2"
weight = "100 900"
style = "normal"
unicode_range = "U+0-10FFFF"
[theme.sidebar]
font = "sans-serif"
`
if err := os.WriteFile(filepath.Join(dir, ConfigFileName), []byte(toml), 0o644); err != nil {
t.Fatal(err)
}
fc := loadFileConfig(dir)
if fc == nil {
t.Fatal("expected config, got nil")
}
cfg := Config{}
fc.applyToConfig(&cfg)
th := cfg.Theme
if th.Font != "serif" || th.HeadingFont != "Inter, sans-serif" || th.CodeFont != "monospace" {
t.Fatalf("fonts not applied: %+v", th)
}
if th.BaseFontSize != 18 || th.BaseFontWeight != 350 || th.CodeFontSize != "0.875rem" || th.CodeFontWeight != 500 {
t.Fatalf("sizes/weights not applied: %+v", th)
}
if len(th.HeadingFontSizes) != 2 || th.HeadingFontSizes[0] != "2rem" || len(th.HeadingFontWeights) != 2 {
t.Fatalf("heading sizes/weights not applied: %+v", th)
}
if len(th.FontFaces) != 1 || th.FontFaces[0].Family != "Inter" || th.FontFaces[0].URL != "/fonts/inter.woff2" ||
th.FontFaces[0].Weight != "100 900" || th.FontFaces[0].UnicodeRange != "U+0-10FFFF" {
t.Fatalf("font_faces not applied: %+v", th.FontFaces)
}
if th.Sidebar.Font != "sans-serif" {
t.Fatalf("sidebar font not applied: %+v", th.Sidebar)
}
}
func TestRenderIndexRejectsUnsafeTheme(t *testing.T) {
// A value trying to break out of the CSS context must be dropped.
html := renderIndex("X", Theme{ThemeStyle: ThemeStyle{Accent: "#fff}</style><script>alert(1)</script>"}})
if strings.Contains(html, "<script>") {
t.Fatalf("unsafe theme value leaked into output:\n%s", html)
}
if strings.Contains(html, "--sy-accent") {
t.Fatalf("unsafe accent should have been rejected:\n%s", html)
}
}