oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1package side
2
3import (
4 "bytes"
5 "html/template"
6 "net/http"
7 "net/http/httptest"
8 "os/exec"
9 "strings"
10 "testing"
11
12 "amadan.net/oficina/estilo.git/internal/tokencheck"
13)
14
15const glyph = template.HTML(`<svg aria-hidden="true"></svg>`)
16
17func render(t *testing.T, s Side) string {
18 t.Helper()
19 tmpl, err := template.ParseFS(Templates(), "templates/*.html")
20 if err != nil {
21 t.Fatal(err)
22 }
23 var b bytes.Buffer
24 if err := tmpl.ExecuteTemplate(&b, "estilo-side", s); err != nil {
25 t.Fatal(err)
26 }
27 return b.String()
28}
29
30func sample() Side {
31 return Side{
32 Label: "Mapa",
33 Places: []Item{
34 {Href: "/", Label: "Home", Icon: glyph, Current: true},
35 {Href: "/search", Label: "Search", Icon: glyph, Key: "⌘K"},
36 },
37 Sections: []Section{
38 {Heading: "Pinned", Items: []Item{
39 {Href: "/p/a", Label: "Alpha", Icon: glyph, Current: true, Locked: ", only you see this"},
40 }},
41 {Heading: "Recently edited", Items: []Item{
42 {Href: "/p/a", Label: "Alpha", Icon: glyph, Current: true},
43 {Href: "/p/b", Label: "Beta", Icon: glyph},
44 }},
45 {Heading: "Empty"},
46 },
47 Foot: template.HTML(`<a estilo-side-item href="/new">` + string(glyph) + `<span estilo-side-label>New</span></a>`),
48 Fold: &Fold{Action: "/view/sidebar", Return: "/p/a", Hide: "Hide sidebar",
49 Show: "Show sidebar", Key: `⌘\`, Cookie: "mapa-sidebar"},
50 }
51}
52
53// Exactly one full highlight: the first page row for the page, and its
54// place drawn twice (unlit open, lit and hidden for the rail).
55func TestLightLeavesOneHighlight(t *testing.T) {
56 s := sample()
57 s.Light()
58 if s.Places[0].Current || !s.Places[0].Holds {
59 t.Errorf("place holding the page: Current %v Holds %v, want false true", s.Places[0].Current, s.Places[0].Holds)
60 }
61 if s.Places[1].Holds {
62 t.Error("a place not holding the page became Holds")
63 }
64 if !s.Sections[0].Items[0].Current || s.Sections[1].Items[0].Current {
65 t.Error("the first page row must keep the highlight and the second lose it")
66 }
67 html := render(t, s)
68 if n := strings.Count(html, `aria-current="page"`); n != 2 {
69 t.Errorf("%d rows carry aria-current, want 2 (the page row and the rail's hidden place)", n)
70 }
71 for _, want := range []string{
72 `<a estilo-side-item estilo-side-holds="open" href="/">`,
73 `<a estilo-side-item estilo-side-holds="rail" href="/" aria-current="page" hidden>`,
74 `<a estilo-side-item estilo-side-page href="/p/a" aria-current="page">`,
75 } {
76 if !strings.Contains(html, want) {
77 t.Errorf("missing %s", want)
78 }
79 }
80}
81
82// With no page row lit, the place keeps the highlight and is drawn once.
83func TestLightKeepsThePlaceWithoutAPageRow(t *testing.T) {
84 s := sample()
85 s.Sections = nil
86 s.Light()
87 if !s.Places[0].Current || s.Places[0].Holds {
88 t.Error("the place must keep the highlight when no page row has it")
89 }
90 if strings.Contains(render(t, s), "estilo-side-holds") {
91 t.Error("a place is drawn twice with no page row lit")
92 }
93}
94
95func TestRendersRowsHeadsFootAndFold(t *testing.T) {
96 s := sample()
97 s.Light()
98 html := render(t, s)
99 for _, want := range []string{
100 `<nav estilo-side aria-label="Mapa">`,
101 `<span estilo-side-label>Search</span><kbd estilo-side-key aria-hidden="true">⌘K</kbd>`,
102 `<h2 estilo-side-head>Pinned</h2>`,
103 `<h2 estilo-side-head>Recently edited</h2>`,
104 `<span estilo-side-label>Alpha<span estilo-visually-hidden>, only you see this</span></span><span estilo-side-lock aria-hidden="true">`,
105 `<div estilo-side-foot><a estilo-side-item href="/new">`,
106 `<form method="post" action="/view/sidebar" estilo-side-fold data-estilo-side-cookie="mapa-sidebar">`,
107 `<input type="hidden" name="return" value="/p/a">`,
108 `name="state" value="rail" estilo-side-fold-to="rail" data-estilo-side-fold>`,
109 `<span estilo-side-label>Hide sidebar</span><kbd estilo-side-key aria-hidden="true">⌘\</kbd>`,
110 `name="state" value="open" estilo-side-fold-to="open" data-estilo-side-fold>`,
111 `<span estilo-side-label>Show sidebar</span></button>`,
112 } {
113 if !strings.Contains(html, want) {
114 t.Errorf("missing %s", want)
115 }
116 }
117 if strings.Contains(html, "Empty") {
118 t.Error("a section with no items drew its heading")
119 }
120 s.Fold = nil
121 if strings.Contains(render(t, s), "estilo-side-fold") {
122 t.Error("a nil Fold still drew the fold")
123 }
124}
125
126func TestStateAndSetState(t *testing.T) {
127 r := httptest.NewRequest(http.MethodGet, "/", nil)
128 if got := State(r, "app-side"); got != "" {
129 t.Errorf("no cookie: %q, want empty", got)
130 }
131 for value, want := range map[string]string{"open": "open", "rail": "rail", "wide": ""} {
132 r := httptest.NewRequest(http.MethodGet, "/", nil)
133 r.AddCookie(&http.Cookie{Name: "app-side", Value: value})
134 if got := State(r, "app-side"); got != want {
135 t.Errorf("cookie %q: %q, want %q", value, got, want)
136 }
137 }
138 for _, c := range []struct{ form, want string }{
139 {"state=rail", "rail"},
140 {"state=open", "open"},
141 {"shown=rail", "open"},
142 {"shown=open", "rail"},
143 {"state=wide", "rail"},
144 } {
145 r := httptest.NewRequest(http.MethodPost, "/view/side", strings.NewReader(c.form))
146 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
147 w := httptest.NewRecorder()
148 if got := SetState(w, r, "app-side"); got != c.want {
149 t.Errorf("%s: %q, want %q", c.form, got, c.want)
150 }
151 set := w.Result().Cookies()
152 if len(set) != 1 || set[0].Name != "app-side" || set[0].Value != c.want ||
153 set[0].Path != "/" || !set[0].Secure || set[0].SameSite != http.SameSiteLaxMode || set[0].MaxAge != cookieYear {
154 t.Errorf("%s: cookie %+v", c.form, set)
155 }
156 }
157}
158
159func TestSideTokensAreDefined(t *testing.T) {
160 missing, n := tokencheck.Undefined(CSS())
161 if n == 0 {
162 t.Fatal("side.css reads no rastrillo token; the check would pass vacuously")
163 }
164 if len(missing) > 0 {
165 t.Fatal("side.css reads undefined tokens:", missing)
166 }
167}
168
169// The values are Mapa's own, copied; a change here is a design decision.
170func TestValuesAreMapas(t *testing.T) {
171 src := string(CSS())
172 for _, want := range []string{
173 "--estilo-side-inline-size: 224px",
174 "--estilo-side-rail-inline-size: 56px",
175 "background: light-dark(#f8f9fb, #191b21)",
176 "grid-template-columns: 16px minmax(0, 1fr) auto",
177 "min-block-size: 30px",
178 "[estilo-side-item][aria-current] { background: var(--rst-accent-soft)",
179 "inline-size: 40px",
180 "min-block-size: 38px",
181 "@media (max-width: 79.99rem)",
182 "anchor-scope: --estilo-side-item",
183 } {
184 if !strings.Contains(src, want) {
185 t.Errorf("side.css lost %q", want)
186 }
187 }
188}
189
190// Below 800px there is no side; the rule is last so it wins over the
191// side's own display at any specificity it shares.
192func TestPhoneHidesTheSideLast(t *testing.T) {
193 src := strings.TrimSpace(string(CSS()))
194 i := strings.LastIndex(src, "@media (max-width: 799.98px)")
195 if i < 0 {
196 t.Fatal("side.css has no phone rule")
197 }
198 tail := src[i:]
199 if !strings.Contains(tail, "[estilo-side] { display: none; }") ||
200 !strings.Contains(tail, "[estilo-side-frame] { grid-template-columns: minmax(0, 1fr); }") ||
201 strings.Count(tail, "}") != 3 {
202 t.Errorf("the phone rule must be side.css's last block:\n%s", tail)
203 }
204}
205
206// side.js's behaviour runs under Node against a minimal DOM
207// (side.test.mjs), the way chrome's does.
208func TestScriptBehaviour(t *testing.T) {
209 out, err := exec.Command("node", "--test", "side.test.mjs").CombinedOutput()
210 if err != nil {
211 t.Fatalf("node --test side.test.mjs: %v\n%s", err, out)
212 }
213}
214
215// Both rails (the folded one and the width's) let the pointer onto the
216// tooltip across the gap from its row, and let Escape dismiss it
217// (Astra, contra-joins-ring). The browser test proves the behaviour.
218func TestRailTooltipsCanBeHoveredAndDismissed(t *testing.T) {
219 src := string(CSS())
220 for _, rail := range []string{
221 `[estilo-side-frame~="rail"]`,
222 `[estilo-side-frame]:not([estilo-side-frame~="open"], [estilo-side-frame~="rail"])`,
223 } {
224 for _, want := range []string{
225 rail + ` [estilo-side-label]::before {`,
226 rail + ` [estilo-side-item]:not([data-estilo-side-tip="off"]):hover [estilo-side-label],`,
227 rail + ` [estilo-side-item]:not([data-estilo-side-tip="off"]):focus-visible [estilo-side-label] {
228 opacity: 1;
229 clip-path: none;
230 pointer-events: auto;`,
231 } {
232 if !strings.Contains(src, want) {
233 t.Errorf("side.css lost %q", want)
234 }
235 }
236 }
237 if strings.Count(src, "overflow: visible;") != 2 {
238 t.Error("each rail's tooltip must let its bridge out (overflow: visible)")
239 }
240}
241