1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6 "bytes"
7 "strings"
8 "testing"
9
10 "bnema.dev/ermite/internal/domain"
11
12 "github.com/a-h/templ"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15)
16
17func renderToString(t *testing.T, c templ.Component) string {
18 t.Helper()
19 var b bytes.Buffer
20 require.NoError(t, c.Render(t.Context(), &b))
21 return b.String()
22}
23
24// A live banner is the content of a status region that stays in the page: the
25// empty slot of the page and the fragment of the stream are the same element
26// (same id, role and marker), so that ermiteLive can replace the children and
27// leave the region where a screen reader watches it.
28func TestLiveSlotsAreStableStatusRegions(t *testing.T) {
29 t.Parallel()
30 for _, tt := range []struct {
31 name string
32 c templ.Component
33 id string
34 text string
35 }{
36 {"empty commits slot", LiveSlot("commits-head"), "commits-head", ""},
37 {"empty mr slot", LiveSlot("mr-notice"), "mr-notice", ""},
38 {"mergeability slot, as sent by the stream", MRMergeabilityFragment(MergeabilityView{State: "up_to_date", Label: "Ready to merge", FFOk: true, MergeOk: true}), "mr-mergeability", "Ready to merge"},
39 {"commits banner", CommitsBanner("2 new commits — reload", "/x/commits/main"), "commits-head", "2 new commits — reload"},
40 {"mr notice", MRNotice("a comment was added — reload", "/x/merge-requests/1"), "mr-notice", "a comment was added — reload"},
41 } {
42 t.Run(tt.name, func(t *testing.T) {
43 t.Parallel()
44 got := renderToString(t, tt.c)
45 assert.True(t, strings.HasPrefix(got, `<div id="`+tt.id+`" class="live-slot" role="status" data-slot>`), got)
46 if tt.text == "" {
47 assert.True(t, strings.HasSuffix(got, "data-slot></div>"), "an empty slot has no child: %s", got)
48 } else {
49 assert.Contains(t, got, tt.text)
50 }
51 assert.True(t, strings.HasSuffix(got, "</div>"), got)
52 })
53 }
54}
55
56// The header's refs button is a link to the refs page, which the script turns
57// into the dialog; the filters and the buttons that need scripts are marked.
58func TestShellDegradesWithoutScripts(t *testing.T) {
59 t.Parallel()
60 l := Layout{Repo: "demo", Ref: "main", Short: "abc1234", Assets: Assets{}}
61 got := renderToString(t, Header(l))
62 assert.Contains(t, got, `<a class="btn ref" href="/demo/refs" data-fire="refs"`)
63 assert.NotContains(t, got, "hx-on:click", "no handler is inlined in the header")
64 assert.Contains(t, got, `class="btn search" data-js`)
65 assert.NotContains(t, got, `data-fire="help"`, "the help button is in the footer only")
66 assert.Contains(t, renderToString(t, Footer(l)), `class="foot-keys" data-js data-fire="help"`)
67}
68
69// Data reaches the scripts through data-* attributes, never spliced into an
70// attribute that is evaluated.
71func TestDiffControlsPassDataThroughAttributes(t *testing.T) {
72 t.Parallel()
73 files := renderToString(t, FileList([]DiffFile{{ID: "f1'x", Path: "a.go", Status: "M"}}))
74 assert.NotContains(t, files, ":aria-current", "the file in view is marked by ermite.js")
75 assert.NotContains(t, files, "f1'x'", "no id is spliced into a script")
76
77 link := renderToString(t, viewLink(DiffView{Both: true, View: "split"}, "split", "Split", "d", "?view=split"))
78 assert.Contains(t, link, `data-fire="split"`)
79 assert.Contains(t, link, `this.dataset.view`)
80 assert.NotContains(t, link, "=== 'split'")
81}
82
83// The push signal is intentionally constant-sized: diff work belongs to the
84// subsequent windowed page request, not to each subscriber's event handler.
85func TestMRPushedHeadSignal(t *testing.T) {
86 t.Parallel()
87 l := liveStream{s: &Server{}, repo: "demo", view: "mr", n: "1", ref: "feature", target: "main"}
88 for _, tt := range []struct {
89 ref string
90 want int
91 }{{"refs/heads/feature", 1}, {"refs/heads/main", 0}, {"refs/heads/other", 0}} {
92 fs, err := l.mrPushed(t.Context(), domain.PushEvent{Updates: []domain.RefUpdate{{Ref: tt.ref}}})
93 require.NoError(t, err)
94 require.Len(t, fs, tt.want)
95 if tt.want > 0 {
96 assert.Equal(t, "mr-head", fs[0].event)
97 assert.Equal(t, `<div id="mr-head"></div>`, fs[0].html)
98 assert.NotContains(t, fs[0].html, "the branch changed")
99 }
100 }
101}