~bigbes/sr-ht-ecore

ref: 0b1bba82f63780a9455f7ed08457e071dd7f59f0 sr-ht-ecore/chrome/chrome_test.go -rw-r--r-- 12.9 KiB
0b1bba82 — Eugene Blikh bd: turn on dolt auto-push 3 days ago
                                                                                
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package chrome

import (
	"html/template"
	"net/http/httptest"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vaughan0/go-ini"
)

// testConf synthesizes the shared instance config the way the services read
// it: one section per service plus the [sr.ht] block.
func testConf() ini.File {
	return ini.File{
		"sr.ht": {
			"site-name":   "srht.example",
			"environment": "production",
		},
		"meta.sr.ht":   {"origin": "https://meta.example"},
		"git.sr.ht":    {"origin": "https://git.example"},
		"todo.sr.ht":   {"origin": "https://todo.example"},
		"builds.sr.ht": {"origin": "https://builds.example"},
		"hub.sr.ht":    {"origin": "https://hub.example"},
		"paste.sr.ht":  {"origin": "https://paste.example"},
		"pages.sr.ht":  {"origin": "https://pages.example"},
		"diff.sr.ht":   {"origin": "https://diff.example"},
		"dolt.sr.ht":   {"origin": "https://dolt.example"},
		"ghost.sr.ht":  {}, // no origin -> must not appear
		"webhooks":     {"private-key": "x"},
	}
}

func TestBuildNavOrderExclusionsActive(t *testing.T) {
	nav := BuildNav(testConf(), "diff.sr.ht")

	var names []string
	for _, it := range nav {
		names = append(names, it.Name)
	}
	// Canonical services first in canonical order, customs alphabetical after;
	// hub/paste/pages and the origin-less section excluded.
	assert.Equal(t, []string{"git", "todo", "builds", "meta", "diff", "dolt"}, names)

	for _, it := range nav {
		assert.Equal(t, it.Name == "diff", it.Active, "active flag for %s", it.Name)
	}
}

func TestPageURLsAndIdentity(t *testing.T) {
	svc := NewService(testConf(), "diff.sr.ht")
	r := httptest.NewRequest("GET", "/~alice/demo?a=1", nil)

	p := svc.Page(r, "t", "alice")
	assert.Equal(t, "srht.example", p.SiteName)
	assert.Equal(t, "diff", p.SiteLabel)
	assert.Equal(t, "https://meta.example/login?return_to="+
		"https%3A%2F%2Fdiff.example%2F~alice%2Fdemo%3Fa%3D1", p.LoginURL)
	assert.Equal(t, "https://meta.example/logout?return_to="+
		"https%3A%2F%2Fdiff.example", p.LogoutURL)
	// Hub is configured, so the profile link prefers hub's ~username page.
	assert.Equal(t, "https://hub.example/~alice", p.ProfileURL)
	assert.Equal(t, "container", p.ContainerClass)
	assert.False(t, p.ShowBanner, "production must not show the env banner")
}

func TestPageProfileFallsBackToMeta(t *testing.T) {
	conf := testConf()
	delete(conf, "hub.sr.ht")
	svc := NewService(conf, "diff.sr.ht")
	r := httptest.NewRequest("GET", "/", nil)

	assert.Equal(t, "https://meta.example/profile", svc.Page(r, "t", "alice").ProfileURL)
	// Anonymous viewers get the meta profile link regardless of hub.
	assert.Equal(t, "https://meta.example/profile",
		NewService(testConf(), "diff.sr.ht").Page(r, "t", "").ProfileURL)
}

func TestPageEnvBanner(t *testing.T) {
	conf := testConf()
	conf["sr.ht"]["environment"] = "staging"
	svc := NewService(conf, "diff.sr.ht")
	p := svc.Page(httptest.NewRequest("GET", "/", nil), "t", "")

	assert.True(t, p.ShowBanner)
	assert.Equal(t, "STAGING", p.Environment)
}

// render executes a minimal layout that invokes both shared partials against v.
func render(t *testing.T, v any) string {
	t.Helper()
	tpl := template.New("layout")
	tpl = MustAttach(tpl)
	tpl, err := tpl.Parse(`{{template "srht-env-banner" .}}<nav>{{template "srht-nav" .}}</nav>`)
	require.NoError(t, err)
	var b strings.Builder
	require.NoError(t, tpl.Execute(&b, v))
	return b.String()
}

func TestNavTemplateLoggedIn(t *testing.T) {
	svc := NewService(testConf(), "diff.sr.ht")
	svc.ExtraNav = []NavItem{{Name: "tokens", Origin: "/tokens"}}
	p := svc.Page(httptest.NewRequest("GET", "/", nil), "t", "alice")

	out := render(t, p)
	assert.Contains(t, out, "icon icon-circle")
	// The brand is two links: the site name to hub, the red label to us.
	assert.Contains(t, out, `<a href="https://hub.example">srht.example</a>`)
	assert.Contains(t, out, `<a href="/"><span class="text-danger">diff</span></a>`)
	assert.Contains(t, out, `href="https://git.example"`)
	assert.Contains(t, out, `href="/tokens"`, "extra nav entries must render")
	assert.Contains(t, out, "Logged in as")
	assert.NotContains(t, out, "ENVIRONMENT")
}

func TestNavTemplateAnonymous(t *testing.T) {
	svc := NewService(testConf(), "diff.sr.ht")
	p := svc.Page(httptest.NewRequest("GET", "/", nil), "t", "")

	out := render(t, p)
	// The switcher renders only for authenticated viewers.
	assert.NotContains(t, out, `href="https://git.example"`)
	assert.Contains(t, out, "Log in")
	assert.Contains(t, out, "Register")
}

// TestNavBrandWithoutHub covers the instance that runs no hub: the site name
// has nowhere else to point, so it falls back to the service root and the
// brand becomes two links to the same place rather than a dead one.
func TestNavBrandWithoutHub(t *testing.T) {
	conf := testConf()
	delete(conf, "hub.sr.ht")
	svc := NewService(conf, "diff.sr.ht")

	out := render(t, svc.Page(httptest.NewRequest("GET", "/", nil), "t", "alice"))
	assert.Contains(t, out, `<a href="/">srht.example</a>`)
	assert.Contains(t, out, `<a href="/"><span class="text-danger">diff</span></a>`)
}

// TestNavTemplateEmbeddedPage guards the documented consumption pattern: a
// service view struct embedding Page resolves the promoted fields.
func TestNavTemplateEmbeddedPage(t *testing.T) {
	type viewData struct {
		Page
		Data any
	}
	svc := NewService(testConf(), "dolt.sr.ht")
	v := viewData{Page: svc.Page(httptest.NewRequest("GET", "/", nil), "t", "alice")}

	out := render(t, v)
	assert.Contains(t, out, `<span class="text-danger">dolt</span>`)
	assert.Contains(t, out, "Logged in as")
}

// renderList executes the srht-repo-list partial alone against v.
func renderList(t *testing.T, v RepoList) string {
	t.Helper()
	tpl := MustAttach(template.New("t"))
	tpl, err := tpl.Parse(`{{template "srht-repo-list" .}}`)
	require.NoError(t, err)
	var b strings.Builder
	require.NoError(t, tpl.Execute(&b, v))
	return b.String()
}

func TestRepoListCards(t *testing.T) {
	out := renderList(t, RepoList{Items: []ListItem{
		{Href: "/~a/pub", Title: "~a/pub", Visibility: "PUBLIC", Description: "described"},
		{Href: "/~a/unl", Title: "~a/unl", Visibility: "UNLISTED"},
		{Href: "/~a/prv", Title: "~a/prv", Visibility: "PRIVATE"},
		{Href: "/~a/none", Title: "~a/none"}, // service without visibility (spec spaces)
	}})

	assert.Equal(t, 4, strings.Count(out, `class="event"`))
	assert.Contains(t, out, "<p>described</p>")
	// PUBLIC and empty visibility render no label; the others render lowercase.
	assert.Equal(t, 1, strings.Count(out, ">unlisted</small>"))
	assert.Equal(t, 1, strings.Count(out, ">private</small>"))
	assert.Equal(t, 2, strings.Count(out, "pull-right"))
}

// TestOptionalColumnsRenderOnlyWhenCarried is the rule that keeps all four
// listing services consumers: dolt has no timestamp in its schema, bench and
// spec do, and neither may force a column of blanks on the other.
func TestOptionalColumnsRenderOnlyWhenCarried(t *testing.T) {
	at := time.Now().Add(-3 * time.Hour)

	bare := renderList(t, RepoList{Items: []ListItem{{Href: "/a", Title: "~a/db"}}})
	assert.NotContains(t, bare, "text-muted\">\n", "no empty metadata line")
	assert.NotContains(t, bare, "ago")

	rich := renderList(t, RepoList{Items: []ListItem{
		{Href: "/a", Title: "~a/repo", Updated: at, Meta: []string{"12 runs"}},
	}})
	assert.Contains(t, rich, "3 hours ago")
	assert.Contains(t, rich, AbsTime(at), "the exact stamp rides in the title")
	assert.Contains(t, rich, "12 runs")
}

// renderTable executes the srht-repo-table partial alone against v.
func renderTable(t *testing.T, v RepoList) string {
	t.Helper()
	tpl := MustAttach(template.New("t"))
	tpl, err := tpl.Parse(`{{template "srht-repo-table" .}}`)
	require.NoError(t, err)
	var b strings.Builder
	require.NoError(t, tpl.Execute(&b, v))
	return b.String()
}

func TestRepoTableIsTheSameDotAsTheCards(t *testing.T) {
	at := time.Now().Add(-2 * 24 * time.Hour)
	list := RepoList{
		Items: []ListItem{
			{Href: "/a", Title: "~a/one", Visibility: "PRIVATE", Updated: at},
			{Href: "/b", Title: "~a/two", Description: "described"},
		},
		Empty: "No repositories yet.",
	}

	out := renderTable(t, list)
	assert.Equal(t, 2, strings.Count(out, "<tr>"))
	assert.Contains(t, out, `<a href="/a">~a/one</a>`)
	assert.Contains(t, out, ">private</small>")
	assert.Contains(t, out, "2 days ago")
	assert.Contains(t, out, "described")
	// The second row carries no time, so it grows no cell for one.
	assert.Equal(t, 1, strings.Count(out, "text-right"))

	assert.Contains(t, renderTable(t, RepoList{Empty: "No repositories yet."}),
		"No repositories yet.")
}

func TestRepoListEmptyState(t *testing.T) {
	out := renderList(t, RepoList{Empty: "No databases yet."})
	assert.NotContains(t, out, "event-list")
	assert.Contains(t, out, "No databases yet.")
}

// TestLoginURLForMatchesTheNav guards the whole reason the accessor exists: a
// handler redirecting to login must land the viewer exactly where the nav's
// "Log in" would have.
func TestLoginURLForMatchesTheNav(t *testing.T) {
	svc := NewService(testConf(), "diff.sr.ht")
	r := httptest.NewRequest("GET", "/~alice/demo?a=1", nil)

	assert.Equal(t, svc.Page(r, "t", "").LoginURL, svc.LoginURLFor(r))
	assert.Equal(t, "https://meta.example/login?return_to="+
		"https%3A%2F%2Fdiff.example%2F~alice%2Fdemo%3Fa%3D1", svc.LoginURLFor(r))
}

// TestHeadLinksAreGuardedAndTheFaviconSurvivesEscaping covers the two ways the
// head links go wrong: an empty href that re-requests the page, and a data:
// URI that html/template rewrites to #ZgotmplZ unless it is a template.URL.
func TestHeadLinksAreGuardedAndTheFaviconSurvivesEscaping(t *testing.T) {
	renderHead := func(t *testing.T, p Page) string {
		t.Helper()
		tpl := MustAttach(template.New("h"))
		tpl, err := tpl.Parse(`{{template "srht-head-links" .}}`)
		require.NoError(t, err)
		var b strings.Builder
		require.NoError(t, tpl.Execute(&b, p))
		return b.String()
	}

	svc := NewService(testConf(), "diff.sr.ht")
	svc.StyleHref = "/static/main.min.0badc0de.css"
	out := renderHead(t, svc.Page(httptest.NewRequest("GET", "/", nil), "t", ""))

	assert.Contains(t, out, `<link rel="stylesheet" href="/static/main.min.0badc0de.css">`)
	// The "+" of image/svg+xml renders as the HTML entity &#43;, which the
	// browser decodes back before the href is ever parsed as a URL.
	assert.Contains(t, out, `<link rel="icon" href="data:image/svg&#43;xml,`,
		"the default icon must survive the URL filter")
	assert.Contains(t, out, "%3Csvg", "and carry its markup")
	assert.NotContains(t, out, "ZgotmplZ", "a filtered href would render as this")

	// A binary built without a stylesheet, and a service that switched the
	// icon off, render neither link rather than an empty one.
	bare := NewService(testConf(), "diff.sr.ht")
	bare.FaviconHref = ""
	out = renderHead(t, bare.Page(httptest.NewRequest("GET", "/", nil), "t", ""))
	assert.NotContains(t, out, "<link")
	assert.NotContains(t, out, `href=""`)
}

// TestPageCarriesTheServiceAssets pins the slot three services grew their own
// copy of: the layout reads an extra hashed artefact off the chrome, not off
// the page's payload.
func TestPageCarriesTheServiceAssets(t *testing.T) {
	svc := NewService(testConf(), "bench.sr.ht")
	svc.Assets = map[string]string{"uplot.js": "/static/uplot.0badc0de.js"}
	p := svc.Page(httptest.NewRequest("GET", "/", nil), "t", "alice")

	assert.Equal(t, "/static/uplot.0badc0de.js", p.Assets["uplot.js"])
	// A name the service never registered reads as empty, which is what the
	// layout's {{if}} guard is written against.
	assert.Empty(t, p.Assets["missing.js"])
	assert.Empty(t, NewService(testConf(), "bench.sr.ht").
		Page(httptest.NewRequest("GET", "/", nil), "t", "alice").Assets)
}

func TestServiceAccessors(t *testing.T) {
	svc := NewService(testConf(), "diff.sr.ht")
	assert.Equal(t, "https://diff.example", svc.SelfOrigin())
	assert.Equal(t, "https://meta.example", svc.MetaOrigin())
	assert.Equal(t, "https://hub.example", svc.HubOrigin())
	assert.Equal(t, "srht.example", svc.SiteName())
	assert.Equal(t, "production", svc.Environment())

	conf := testConf()
	delete(conf, "hub.sr.ht")
	assert.Empty(t, NewService(conf, "diff.sr.ht").HubOrigin())
}

func TestRelTimeFacesBothDirections(t *testing.T) {
	now := time.Now()
	assert.Equal(t, "just now", RelTime(now))
	assert.Equal(t, "just now", RelTime(now.Add(30*time.Second)))
	assert.Equal(t, "3 hours ago", RelTime(now.Add(-3*time.Hour)))
	assert.Equal(t, "in 3 hours", RelTime(now.Add(3*time.Hour+time.Minute)))
	assert.Equal(t, "1 minute ago", RelTime(now.Add(-time.Minute-time.Second)))
	assert.Equal(t, "2 years ago", RelTime(now.Add(-2*365*24*time.Hour)))

	assert.Equal(t, "2026-08-08 12:34:56 UTC",
		AbsTime(time.Date(2026, 8, 8, 12, 34, 56, 0, time.UTC)))
}

func TestFuncs(t *testing.T) {
	assert.Equal(t, "12345678", ShortSHA("1234567890abcdef"))
	assert.Equal(t, "abc", ShortSHA("abc"))

	for _, name := range []string{"dict", "shortsha", "reltime", "abstime"} {
		assert.Contains(t, Funcs(), name)
	}

	m, err := Dict("a", 1, "b", "x")
	require.NoError(t, err)
	assert.Equal(t, map[string]any{"a": 1, "b": "x"}, m)

	_, err = Dict("a")
	require.Error(t, err)
	_, err = Dict(1, "v")
	require.Error(t, err)
}