oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slidestest
2
3import (
4 "net/http"
5 "os"
6 "path/filepath"
7 "regexp"
8 "strings"
9 "testing"
10)
11
12// The editor, tidied (docs/superpowers/specs/2026-11-16-editor-tidy-design.md).
13
14// deskLink is estilo's desk part as the layout links it: fingerprinted,
15// under static/estilo/.
16var deskLink = regexp.MustCompile(`/static/estilo/desk\.[0-9a-f]{16}\.css`)
17
18// TestEditorTidyServesEstiloDesk: estilo's desk part is served beside
19// its chrome, and the editor and the deck list both link it, so the
20// grey under a deck is estilo's token and not a copy of it.
21func TestEditorTidyServesEstiloDesk(t *testing.T) {
22 a := newApp(t)
23
24 css := mustGet(t, a, "/static/estilo/desk.css", http.StatusOK, nil).Body.String()
25 if !strings.Contains(css, "--estilo-desk:") {
26 t.Errorf("/static/estilo/desk.css defines no --estilo-desk:\n%s", css)
27 }
28
29 cookies := signedIn(t, a, allowedAddress)
30 path := sampleDeck(t, a, cookies)
31 for _, page := range []string{"/", path + "/edit"} {
32 body := mustGet(t, a, page, http.StatusOK, cookies).Body.String()
33 link := deskLink.FindString(body)
34 if link == "" {
35 t.Errorf("GET %s links no estilo desk.css:\n%s", page, body)
36 continue
37 }
38 if got := mustGet(t, a, link, http.StatusOK, nil).Body.String(); got != css {
39 t.Errorf("GET %s: %s is not the desk.css served unfingerprinted", page, link)
40 }
41 chrome := regexp.MustCompile(`/static/estilo/chrome\.[0-9a-f]{16}\.css`).FindStringIndex(body)
42 if chrome == nil || strings.Index(body, link) < chrome[1] {
43 t.Errorf("GET %s: desk.css is not linked after chrome.css", page)
44 }
45 }
46}
47
48// TestEditorTidyDeskIsEstilos: Slides' desk is estilo's grey, Docs'
49// own, not Slides' warm grey; and the deck list sits on that same desk,
50// with no wash of colour over it.
51func TestEditorTidyDeskIsEstilos(t *testing.T) {
52 desk := readStatic(t, "desk.css")
53 if !strings.Contains(desk, "var(--estilo-desk)") {
54 t.Error("static/desk.css does not read var(--estilo-desk)")
55 }
56 if strings.Contains(desk, "#ebe7e2") {
57 t.Error("static/desk.css still carries Slides' warm grey #ebe7e2")
58 }
59
60 decks := readStatic(t, "decks.css")
61 rule := regexp.MustCompile(`body:has\(\[slides-decks\]\)\s*\{[^}]*\}`).FindString(decks)
62 if rule == "" {
63 t.Fatal("static/decks.css has no body:has([slides-decks]) rule")
64 }
65 if strings.Contains(rule, "gradient") {
66 t.Errorf("the deck list's ground still has a gradient:\n%s", rule)
67 }
68 if !strings.Contains(rule, "var(--slides-desk)") {
69 t.Errorf("the deck list's ground does not read var(--slides-desk):\n%s", rule)
70 }
71}
72
73func readStatic(t *testing.T, name string) string {
74 t.Helper()
75 b, err := os.ReadFile(filepath.Join("..", "slides", "static", name))
76 if err != nil {
77 t.Fatal(err)
78 }
79 return string(b)
80}
81
82// TestEditorTidyViewSwitchIsThreeIconRadios (spec §3): the view switch
83// is still three radios named slides-view, each inside an estilo-seg
84// item label with an accessible name, a tooltip and an icon.
85func TestEditorTidyViewSwitchIsThreeIconRadios(t *testing.T) {
86 a := newApp(t)
87 cookies := signedIn(t, a, allowedAddress)
88 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
89
90 if n := strings.Count(page, `name="slides-view"`); n != 3 {
91 t.Fatalf("the editor has %d name=\"slides-view\" radios, want 3", n)
92 }
93 set := regexp.MustCompile(`(?s)<fieldset slides-switcher estilo-seg>.*?</fieldset>`).FindString(page)
94 if set == "" {
95 t.Fatal("no <fieldset slides-switcher estilo-seg> in the editor")
96 }
97 for _, c := range []struct{ value, name, title string }{
98 {"desktop", "Desktop", "Desktop"},
99 {"phone", "Phone", "Phone"},
100 {"both", "Both, side by side", "Both, side by side"},
101 } {
102 var label string
103 for _, l := range regexp.MustCompile(`(?s)<label estilo-seg-item.*?</label>`).FindAllString(set, -1) {
104 if strings.Contains(l, `<input type="radio" name="slides-view" value="`+c.value+`"`) {
105 label = l
106 }
107 }
108 if label == "" {
109 t.Errorf("no estilo-seg-item label wrapping the %q radio in:\n%s", c.value, set)
110 continue
111 }
112 open := label[:strings.Index(label, ">")+1]
113 if !strings.Contains(open, `aria-label="`+c.name+`"`) {
114 t.Errorf("%s: label has no aria-label=%q: %s", c.value, c.name, open)
115 }
116 if !strings.Contains(open, `title="`+c.title+`"`) {
117 t.Errorf("%s: label has no title=%q: %s", c.value, c.title, open)
118 }
119 if !strings.Contains(label, `<svg class="icon"`) {
120 t.Errorf("%s: label has no icon: %s", c.value, label)
121 }
122 }
123}
124
125// TestEditorTidyNotesMarkup (spec §5): the notes are a card, the stage
126// pane's own and its last child, with an × with a name, a top edge that
127// is a focusable horizontal separator with a name, and the notes field
128// itself; the tab that opens them is there too, and the switch above
129// the stage is gone. The quieter editor (§6.2) took the card's heading
130// and its estilo-card (TestQuieterNotesMarkup holds what replaced
131// them).
132func TestEditorTidyNotesMarkup(t *testing.T) {
133 a := newApp(t)
134 cookies := signedIn(t, a, allowedAddress)
135 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
136
137 // The pane runs from its opening tag to the </section> that closes
138 // it; the slide on the stage is a <section> too, so the card is
139 // found first and the pane is what ends right after it.
140 at := strings.Index(page, "<section slides-stage-pane")
141 if at < 0 {
142 t.Fatal("no <section slides-stage-pane> in the editor")
143 }
144 card := regexp.MustCompile(`(?s)<section slides-notes-pane[^>]*>.*?</section>`).FindString(page[at:])
145 if card == "" {
146 t.Fatalf("no <section slides-notes-pane> inside the stage pane:\n%s", page[at:])
147 }
148 cardEnd := at + strings.Index(page[at:], card) + len(card)
149 pane := page[at:cardEnd]
150 closeBtn := regexp.MustCompile(`<button[^>]*\sslides-notes-close[^>]*>`).FindString(card)
151 if closeBtn == "" || !strings.Contains(closeBtn, `aria-label="Close notes"`) {
152 t.Errorf("no button[slides-notes-close] with aria-label=\"Close notes\" in the card: %q", closeBtn)
153 }
154 edge := regexp.MustCompile(`<[a-z]+[^>]*\sslides-notes-resize[^>]*>`).FindString(card)
155 for _, want := range []string{`role="separator"`, `aria-orientation="horizontal"`, `tabindex="0"`, `aria-label="Resize notes"`} {
156 if !strings.Contains(edge, want) {
157 t.Errorf("[slides-notes-resize] has no %s: %q", want, edge)
158 }
159 }
160 if !strings.Contains(card, "<div slides-notes ") {
161 t.Errorf("the card holds no <div slides-notes …>:\n%s", card)
162 }
163 if rest := strings.TrimSpace(page[cardEnd:]); !strings.HasPrefix(rest, "</section>") {
164 t.Errorf("the notes card is not the stage pane's last child; after it comes:\n%.200s", rest)
165 }
166
167 openBtn := regexp.MustCompile(`<button[^>]*\sslides-notes-open[^>]*>`).FindString(pane)
168 if openBtn == "" {
169 t.Fatal("no button[slides-notes-open] in the stage pane")
170 }
171 // deckWithSlides' first slide has notes: the tab says so in words.
172 if _, text := notesTab(t, pane); text != "Notes · 400 words" {
173 t.Errorf("the tab does not say the first slide has 400 words of notes: %s%s", openBtn, text)
174 }
175 for _, gone := range []string{`name="slides-notes"`, "slides-notes-toggle"} {
176 if strings.Contains(page, gone) {
177 t.Errorf("the editor still has %s", gone)
178 }
179 }
180}
181
182// TestEditorTidyPhonePlacementIsShared (spec §6.1): the phone frame
183// places the slide by the viewer's own lengths, not copies of them.
184// desk.css, which the viewer and the editor both link, defines the bar
185// and the pills once; the viewer's rules read the pill height from it,
186// and the editor's --slides-phone-page-top reads both and holds no
187// number of its own for them.
188func TestEditorTidyPhonePlacementIsShared(t *testing.T) {
189 desk := readStatic(t, "desk.css")
190 for _, def := range []string{
191 "--slides-page-bar-block-size: 3.25rem;",
192 "--slides-pill-block-size: 36px;",
193 } {
194 if !strings.Contains(desk, def) {
195 t.Errorf("static/desk.css does not define %q", def)
196 }
197 }
198
199 editor := readStatic(t, "editor.css")
200 rule := regexp.MustCompile(`--slides-phone-page-top:[^;]*;`).FindString(editor)
201 if rule == "" {
202 t.Fatal("static/editor.css defines no --slides-phone-page-top")
203 }
204 for _, literal := range []string{"3.25rem", "36px", "52px"} {
205 if strings.Contains(rule, literal) {
206 t.Errorf("--slides-phone-page-top copies the viewer's %s: %s", literal, rule)
207 }
208 }
209 for _, ref := range []string{"var(--slides-page-bar-block-size)", "var(--slides-pill-block-size)"} {
210 if !strings.Contains(rule, ref) {
211 t.Errorf("--slides-phone-page-top does not read %s: %s", ref, rule)
212 }
213 }
214
215 viewer, err := os.ReadFile(filepath.Join("..", "slides", "templates", "viewer.html"))
216 if err != nil {
217 t.Fatal(err)
218 }
219 if !strings.Contains(string(viewer), "var(--slides-pill-block-size)") {
220 t.Error("templates/viewer.html does not read var(--slides-pill-block-size)")
221 }
222 if strings.Contains(string(viewer), "36px") {
223 t.Error("templates/viewer.html still writes 36px where it could read --slides-pill-block-size")
224 }
225
226 // Both pages link the desk.css that defines them.
227 a := newApp(t)
228 cookies := signedIn(t, a, allowedAddress)
229 path := sampleDeck(t, a, cookies)
230 link := regexp.MustCompile(`href="/static/desk\.[0-9a-f]{16}\.css"`)
231 for _, page := range []string{path + "?slide=1", path + "/edit"} {
232 if body := mustGet(t, a, page, http.StatusOK, cookies).Body.String(); !link.MatchString(body) {
233 t.Errorf("GET %s does not link static/desk.css", page)
234 }
235 }
236}
237
238// TestEditorTidyInspectorState (spec §2, §4): the inspector is a card,
239// an <aside> with no title, and the bar's Format button shows and hides
240// it. Whether it shows is the slides-inspector cookie, read by the
241// server, so the first paint is final: no cookie, or any value but
242// closed, is open.
243func TestEditorTidyInspectorState(t *testing.T) {
244 a := newApp(t)
245 cookies := signedIn(t, a, allowedAddress)
246 path := deckWithSlides(t, a, cookies, 2)
247
248 aside := regexp.MustCompile(`<aside slides-inspector[^>]*>`)
249 format := regexp.MustCompile(`<button[^>]*\sdata-view-toggle="inspector"[^>]*\saria-controls="slides-inspector"[^>]*>`)
250 row := regexp.MustCompile(`<button estilo-menu-item[^>]*\sdata-view-toggle="inspector"[^>]*>`)
251 for _, c := range []struct{ name, value, state, expanded string }{
252 {"no cookie", "", "open", "true"},
253 {"closed", "closed", "closed", "false"},
254 {"open", "open", "open", "true"},
255 {"anything else", "shut", "open", "true"},
256 } {
257 jar := cookies
258 if c.value != "" {
259 jar = append(append([]*http.Cookie{}, cookies...), &http.Cookie{Name: "slides-inspector", Value: c.value})
260 }
261 page := mustGet(t, a, path, http.StatusOK, jar).Body.String()
262 open := aside.FindString(page)
263 if open == "" {
264 t.Fatalf("%s: no <aside slides-inspector> in the editor", c.name)
265 }
266 if !strings.Contains(open, `data-state="`+c.state+`"`) {
267 t.Errorf("%s: the card is %s; want data-state=%q", c.name, open, c.state)
268 }
269 btn := format.FindString(page)
270 if btn == "" {
271 t.Fatalf("%s: no Format button (data-view-toggle=\"inspector\", aria-controls=\"slides-inspector\") in the bar", c.name)
272 }
273 if !strings.Contains(btn, `aria-expanded="`+c.expanded+`"`) {
274 t.Errorf("%s: the Format button is %s; want aria-expanded=%q", c.name, btn, c.expanded)
275 }
276 if r := row.FindString(page); !strings.Contains(r, `aria-pressed="`+c.expanded+`"`) {
277 t.Errorf("%s: the View menu's Inspector row is %s; want aria-pressed=%q", c.name, r, c.expanded)
278 }
279 }
280
281 page := mustGet(t, a, path, http.StatusOK, cookies).Body.String()
282 open := aside.FindString(page)
283 for _, want := range []string{`id="slides-inspector"`, " estilo-card", " estilo-inspector", `aria-label="Inspector"`} {
284 if !strings.Contains(open, want) {
285 t.Errorf("the card has no %s: %s", strings.TrimSpace(want), open)
286 }
287 }
288 start := strings.Index(page, open)
289 end := strings.Index(page[start:], "</aside>")
290 if end < 0 {
291 t.Fatal("the <aside slides-inspector> is not closed")
292 }
293 card := page[start : start+end]
294 // The card's own children: a <summary> anywhere but inside a
295 // dropdown (Add block's) would be the old title.
296 if regexp.MustCompile(`<summary>`).MatchString(card) {
297 t.Error("the card still has a bare <summary> (the old title)")
298 }
299 if regexp.MustCompile(`>\s*Inspector\s*<`).MatchString(card) {
300 t.Error("the card still shows the old Inspector title")
301 }
302 if strings.Contains(page, "<details slides-inspector") {
303 t.Error("the editor still has <details slides-inspector>")
304 }
305
306 btn := format.FindString(page)
307 if !strings.Contains(btn, "slides-press") {
308 t.Errorf("the Format button has no slides-press: %s", btn)
309 }
310 if !strings.Contains(btn, `aria-label="Format"`) {
311 t.Errorf("the Format button has no aria-label=\"Format\" (it is its icon alone in a narrow window): %s", btn)
312 }
313 if strings.Contains(btn, "aria-pressed") {
314 t.Errorf("the Format button still carries aria-pressed (aria-expanded is its state): %s", btn)
315 }
316 at := strings.Index(page, btn)
317 whole := page[at : at+strings.Index(page[at:], "</button>")]
318 if m := regexp.MustCompile(`<span>([^<]*)</span>`).FindStringSubmatch(whole); m == nil || m[1] != "Format" {
319 t.Errorf("the Format button's text is %q, want Format", m)
320 }
321 if !strings.Contains(whole, `<path d="m15 5 4 4"/>`) {
322 t.Errorf("the Format button has no pencil icon: %s", whole)
323 }
324 if wrap := page[strings.LastIndex(page[:at], "<"):at]; !strings.Contains(wrap, "estilo-bar-control") {
325 t.Errorf("the Format button is not inside an estilo-bar-control: %q", wrap)
326 }
327 if share := strings.Index(page, "slides-head-share"); share < at {
328 t.Errorf("the Format button (at %d) does not precede Share (at %d)", at, share)
329 }
330 if strings.Contains(page, "data-inspector-off") {
331 t.Error("the editor still mentions data-inspector-off")
332 }
333}
334
335// TestEditorTidyNotesState (final review, finding 6; ruling R21): the
336// notes card's open or closed state and its height are cookies the
337// server reads, as the inspector's state is, so the first paint is the
338// final one. slides-notes: only exactly "closed" is closed. Its height,
339// slides-notes-height, is a whole number of px clamped to sane bounds
340// and written as the card's --slides-notes-block-size; anything that is
341// not a whole number is no height, and the stylesheet's default holds.
342func TestEditorTidyNotesState(t *testing.T) {
343 a := newApp(t)
344 cookies := signedIn(t, a, allowedAddress)
345 path := deckWithSlides(t, a, cookies, 2)
346
347 pane := regexp.MustCompile(`<section slides-notes-pane[^>]*>`)
348 row := regexp.MustCompile(`<button estilo-menu-item[^>]*\sdata-view-toggle="notes"[^>]*>`)
349 height := regexp.MustCompile(`style="--slides-notes-block-size: ?(\d+)px;?"`)
350 with := func(kv ...string) []*http.Cookie {
351 jar := append([]*http.Cookie{}, cookies...)
352 for i := 0; i < len(kv); i += 2 {
353 jar = append(jar, &http.Cookie{Name: kv[i], Value: kv[i+1]})
354 }
355 return jar
356 }
357 for _, c := range []struct {
358 name string
359 jar []*http.Cookie
360 state string
361 pressed string
362 height string // "" is no style: the stylesheet's default
363 }{
364 {"no cookie", cookies, "open", "true", ""},
365 {"closed", with("slides-notes", "closed"), "closed", "false", ""},
366 {"open", with("slides-notes", "open"), "open", "true", ""},
367 {"anything else", with("slides-notes", "off"), "open", "true", ""},
368 {"a height", with("slides-notes-height", "220"), "open", "true", "220"},
369 {"closed, with a height", with("slides-notes", "closed", "slides-notes-height", "300"), "closed", "false", "300"},
370 {"a height too small", with("slides-notes-height", "10"), "open", "true", "73"},
371 {"a height too large", with("slides-notes-height", "90000"), "open", "true", "2000"},
372 {"a negative height", with("slides-notes-height", "-50"), "open", "true", "73"},
373 {"a fraction", with("slides-notes-height", "220.5"), "open", "true", ""},
374 {"words", with("slides-notes-height", "tall"), "open", "true", ""},
375 {"a unit", with("slides-notes-height", "220px"), "open", "true", ""},
376 {"an exponent", with("slides-notes-height", "2e2"), "open", "true", ""},
377 {"empty", with("slides-notes-height", ""), "open", "true", ""},
378 {"too many digits", with("slides-notes-height", "99999999999999999999999"), "open", "true", ""},
379 } {
380 page := mustGet(t, a, path, http.StatusOK, c.jar).Body.String()
381 open := pane.FindString(page)
382 if open == "" {
383 t.Fatalf("%s: no <section slides-notes-pane> in the editor", c.name)
384 }
385 if !strings.Contains(open, `data-state="`+c.state+`"`) {
386 t.Errorf("%s: the notes card is %s; want data-state=%q", c.name, open, c.state)
387 }
388 if r := row.FindString(page); !strings.Contains(r, `aria-pressed="`+c.pressed+`"`) {
389 t.Errorf("%s: the View menu's Speaker notes row is %s; want aria-pressed=%q", c.name, r, c.pressed)
390 }
391 got := ""
392 if m := height.FindStringSubmatch(open); m != nil {
393 got = m[1]
394 } else if strings.Contains(open, "style=") {
395 t.Errorf("%s: the notes card carries a style that is not its height: %s", c.name, open)
396 }
397 if got != c.height {
398 t.Errorf("%s: the notes card's height is %q; want %q (%s)", c.name, got, c.height, open)
399 }
400 }
401}
402
403// TestEditorTidyCardShowsOneThing (spec §4.1): the server paints the
404// card on the slide's options (data-inspect="slide" on the root), and
405// which section shows is that attribute and the stylesheet's, never a
406// hidden attribute the bundle has to take off: each section's form
407// carries data-section and no hidden (the card holds the Slide and
408// Block sections; the Deck's is the Deck settings dialog's since Q2,
409// TestQuieterDeckSettingsMarkup), and no section has a heading of its own (the card has no title; its groups
410// are labelled, spec §4). With no slide in the deck the notes icon is
411// disabled (Review Focus 4).
412func TestEditorTidyCardShowsOneThing(t *testing.T) {
413 a := newApp(t)
414 cookies := signedIn(t, a, allowedAddress)
415 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
416
417 root := regexp.MustCompile(`(?s)<div slides-editor[^>]*>`).FindString(page)
418 if !strings.Contains(root, `data-inspect="slide"`) {
419 t.Errorf("the editor's root does not carry data-inspect=\"slide\": %s", root)
420 }
421 start := strings.Index(page, "<aside slides-inspector")
422 if start < 0 {
423 t.Fatal("no <aside slides-inspector> in the editor")
424 }
425 end := strings.Index(page[start:], "</aside>")
426 if end < 0 {
427 t.Fatal("the <aside slides-inspector> is not closed")
428 }
429 card := page[start : start+end]
430 at := map[string]int{}
431 for _, section := range []string{"slide", "block"} {
432 for _, tag := range []string{`slides-inspector-form data-section="` + section + `"`} {
433 el := regexp.MustCompile(regexp.QuoteMeta(tag) + `[^>]*>`).FindString(card)
434 if el == "" {
435 t.Errorf("the card has no %s…>", tag)
436 continue
437 }
438 if regexp.MustCompile(`\shidden[\s>]`).MatchString(el) {
439 t.Errorf("the %s section is hidden by attribute (data-inspect says what shows): %s", section, el)
440 }
441 if strings.Contains(el, "slides-when-") {
442 t.Errorf("the %s section still carries a slides-when- hook: %s", section, el)
443 }
444 if _, ok := at[section]; !ok {
445 at[section] = strings.Index(card, el)
446 }
447 }
448 }
449 if strings.Contains(card, "rst-box-head") || regexp.MustCompile(`<h[1-6][\s>]`).MatchString(card) {
450 t.Error("the card still has a section heading")
451 }
452
453 icon := regexp.MustCompile(`<button[^>]*\sslides-notes-open[^>]*>`)
454 if b := icon.FindString(page); !strings.Contains(b, "data-needs-slide") || regexp.MustCompile(`\sdisabled[\s>]`).MatchString(b) {
455 t.Errorf("with slides, the notes icon is %s; want data-needs-slide and not disabled", b)
456 }
457 empty := mustGet(t, a, emptyDeck(t, a, cookies), http.StatusOK, cookies).Body.String()
458 if b := icon.FindString(empty); !regexp.MustCompile(`\sdisabled[\s>]`).MatchString(b) {
459 t.Errorf("with no slides, the notes icon is %s; want it disabled", b)
460 }
461}
462
463// extent is the element whose opening tag starts at at: from there to
464// the end of its matching close tag, nested elements of the same name
465// counted. The template is well formed, so counting is enough.
466func extent(page string, at int) string {
467 m := regexp.MustCompile(`^<([a-z]+)`).FindStringSubmatch(page[at:])
468 if m == nil {
469 return ""
470 }
471 open := regexp.MustCompile(`<` + m[1] + `[\s>]|</` + m[1] + `>`)
472 depth := 0
473 for _, loc := range open.FindAllStringIndex(page[at:], -1) {
474 if page[at+loc[0]+1] == '/' {
475 depth--
476 if depth == 0 {
477 return page[at : at+loc[1]]
478 }
479 } else {
480 depth++
481 }
482 }
483 return ""
484}
485
486// tidyGroup is one labelled group in the card: its name and where it
487// starts and ends inside the form that holds it.
488type tidyGroup struct {
489 name string
490 start, end int
491}
492
493// tidyGroups lists the estilo-inspector-group elements in form, in
494// order, each named by its data-group.
495func tidyGroups(form string) []tidyGroup {
496 var out []tidyGroup
497 name := regexp.MustCompile(`\sdata-group="([^"]+)"`)
498 for _, loc := range regexp.MustCompile(`<div estilo-inspector-group[^>]*>`).FindAllStringIndex(form, -1) {
499 g := tidyGroup{start: loc[0], end: loc[0] + len(extent(form, loc[0]))}
500 if n := name.FindStringSubmatch(form[loc[0]:loc[1]]); n != nil {
501 g.name = n[1]
502 }
503 out = append(out, g)
504 }
505 return out
506}
507
508// TestEditorTidyCardShape (spec E8, E10, §4.2, §4.3): the card's forms
509// hold their groups in the spec's order, each group opening in estilo's
510// shape and beginning with its label (the place group's is its closed
511// disclosure's summary, quieter editor §4.3); nothing follows the last
512// group, Delete block being the box menu's (quieter editor §4.2,
513// TestQuieterBoxMenuMarkup); the delete confirmation
514// precedes the slide's groups; the five slide commands that left the
515// card are still in the Slide menu and in each slide's own menu in the
516// strip; and the stage bar keeps the view switch and no marks. Which
517// field lives in which form and group is TestQuieterNothingIsLost's
518// table (quieter_inventory_test.go).
519func TestEditorTidyCardShape(t *testing.T) {
520 a := newApp(t)
521 cookies := signedIn(t, a, allowedAddress)
522 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
523
524 at := strings.Index(page, "<aside slides-inspector")
525 if at < 0 {
526 t.Fatal("no <aside slides-inspector> in the editor")
527 }
528 card := extent(page, at)
529 forms := map[string]string{}
530 section := regexp.MustCompile(`\sdata-section="([^"]+)"`)
531 for _, loc := range regexp.MustCompile(`<form [^>]*slides-inspector-form[^>]*>`).FindAllStringIndex(card, -1) {
532 if s := section.FindStringSubmatch(card[loc[0]:loc[1]]); s != nil {
533 forms[s[1]] = extent(card, loc[0])
534 }
535 }
536
537 // The groups themselves: estilo's labelled group, its label's id the
538 // group's name (the slide's names its pictures instead), in the
539 // spec's order.
540 for _, w := range []struct {
541 section string
542 names []string
543 }{
544 {"block", []string{"appears", "scene", "style", "text", "alignment", "image", "clip", "qr", "description", "place", "phone"}},
545 {"slide", []string{"slide"}},
546 } {
547 var got []string
548 form := forms[w.section]
549 for _, g := range tidyGroups(form) {
550 got = append(got, g.name)
551 open := form[g.start : g.start+strings.Index(form[g.start:], ">")+1]
552 want := `<div estilo-inspector-group role="group" aria-labelledby="slides-g-` + g.name + `" data-group="` + g.name + `">`
553 if g.name == "slide" {
554 // Its label names the pictures' radio group instead (Q10):
555 // a group around them named the same said it twice.
556 want = `<div estilo-inspector-group data-group="slide">`
557 }
558 if open != want {
559 t.Errorf("the %s group opens %s, want %s", g.name, open, want)
560 }
561 label := `^<div[^>]*>\s*<span estilo-inspector-label id="slides-g-` + regexp.QuoteMeta(g.name) + `">[^<]+</span>`
562 if g.name == "place" {
563 label = `^<div[^>]*>\s*<details slides-place[^>]*>\s*<summary [^>]*id="slides-g-place"[^>]*>`
564 }
565 if !regexp.MustCompile(label).MatchString(form[g.start:g.end]) {
566 t.Errorf("the %s group does not begin with its label, id=\"slides-g-%s\"", g.name, g.name)
567 }
568 }
569 if strings.Join(got, " ") != strings.Join(w.names, " ") {
570 t.Errorf("the %s form's groups are %v, want %v", w.section, got, w.names)
571 }
572 }
573 // The deck's settings left the card for a dialog under File (Q2,
574 // spec §8; TestQuieterDeckSettingsMarkup).
575 if _, ok := forms["deck"]; ok {
576 t.Error("the card still holds the deck's form")
577 }
578 if block := forms["block"]; block != "" {
579 if gs := tidyGroups(block); len(gs) > 0 {
580 if tail := strings.TrimSpace(strings.TrimSuffix(block[gs[len(gs)-1].end:], "</form>")); tail != "" {
581 t.Errorf("the box's options hold something after their last group: %s", tail)
582 }
583 }
584 if strings.Contains(block, `data-command="block-delete"`) {
585 t.Error("the box's options still hold Delete block")
586 }
587 }
588 if slide := forms["slide"]; slide != "" {
589 confirm := strings.Index(slide, "slides-confirm")
590 if gs := tidyGroups(slide); confirm < 0 || (len(gs) > 0 && confirm > gs[0].start) {
591 t.Error("the delete confirmation is not first in the slide's options")
592 }
593 }
594
595 // The slide's own commands left the card, and are where they were.
596 for _, c := range []string{"slide-move-up", "slide-move-down", "slide-add-after", "duplicate-slide", "slide-delete-ask"} {
597 if strings.Contains(card, `data-command="`+c+`"`) {
598 t.Errorf("the card still holds data-command=%q", c)
599 }
600 }
601 menubar := regexp.MustCompile(`(?s)<nav estilo-menubar slides-menubar.*?</nav>`).FindString(page)
602 menu := regexp.MustCompile(`(?s)<details[^>]*slides-menu="slide".*?</details>`).FindString(menubar)
603 if menu == "" {
604 t.Fatal("no Slide menu in [slides-menubar]")
605 }
606 for _, c := range []string{"new-slide", "duplicate-slide", "slide-move-up", "slide-move-down", "slide-delete-ask"} {
607 if !strings.Contains(menu, `data-command="`+c+`"`) {
608 t.Errorf("the Slide menu has no data-command=%q", c)
609 }
610 }
611 tmpl := regexp.MustCompile(`(?s)<template slides-strip-item-template>.*?</template>`).FindString(page)
612 for _, c := range []string{"add-after", "duplicate", "move-up", "move-down", "delete"} {
613 if !strings.Contains(tmpl, `data-slide-command="`+c+`"`) {
614 t.Errorf("the strip item's menu has no data-slide-command=%q", c)
615 }
616 }
617
618 bar := strings.Index(page, "<div slides-stage-bar")
619 if bar < 0 {
620 t.Fatal("no [slides-stage-bar] in the editor")
621 }
622 stageBar := extent(page, bar)
623 if !strings.Contains(stageBar, "slides-switcher") {
624 t.Error("the stage bar has no view switch")
625 }
626 if strings.Contains(stageBar, "data-mark") {
627 t.Error("the stage bar still holds marks")
628 }
629}
630
631// TestEditorTidyBarControls (spec §2): the bar's controls, in order —
632// the save line, undo and redo in one pill, Format, Share…, Present,
633// the switcher, the account — and its buttons estilo's pills: each
634// sits in an estilo-bar-control wrapper (estilo draws the wrapper's
635// child), Present's the "primary" one. Undo and redo stay two buttons,
636// each with its own name, inside the one [slides-history] group.
637func TestEditorTidyBarControls(t *testing.T) {
638 a := newApp(t)
639 cookies := signedIn(t, a, allowedAddress)
640 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
641
642 start := strings.Index(page, "slides-head-end>")
643 if start < 0 {
644 t.Fatal("the editor has no [slides-head-end]")
645 }
646 end := start + strings.Index(page[start:], "</header>")
647 bar := page[start:end]
648
649 at := func(name, re string) int {
650 m := regexp.MustCompile(re).FindStringIndex(bar)
651 if m == nil {
652 t.Fatalf("the bar has no %s (%s)", name, re)
653 }
654 return m[0]
655 }
656 order := []struct{ name, re string }{
657 {"save line", `<span estilo-save-status`},
658 {"history", `<div slides-history`},
659 {"Format", `<button[^>]*data-view-toggle="inspector"`},
660 {"Share", `<a [^>]*slides-head-share`},
661 {"Present", `<form [^>]*slides-present-form`},
662 {"account", `<details estilo-account`},
663 }
664 if strings.Contains(bar, "estilo-switcher") {
665 order = append(order[:5], append([]struct{ name, re string }{{"switcher", `<details estilo-switcher`}}, order[5:]...)...)
666 }
667 last, lastName := -1, ""
668 for _, o := range order {
669 i := at(o.name, o.re)
670 if i < last {
671 t.Errorf("the bar has %s before %s; want them in the order of spec §2", lastName, o.name)
672 }
673 last, lastName = i, o.name
674 }
675
676 for _, c := range []struct{ name, re, want string }{
677 {"Format", `<button[^>]*data-view-toggle="inspector"`, "<span estilo-bar-control>"},
678 {"Share", `<a [^>]*slides-head-share`, "<span estilo-bar-control>"},
679 {"Present", `<form [^>]*slides-present-form`, `<div estilo-bar-control="primary">`},
680 } {
681 i := at(c.name, c.re)
682 // the open tag immediately before the control
683 j := strings.LastIndex(bar[:i], "<")
684 if got := strings.TrimSpace(bar[j:i]); got != c.want {
685 t.Errorf("%s sits in %q; want %s", c.name, got, c.want)
686 }
687 }
688
689 h := at("history", `<div slides-history`)
690 group := bar[h : h+strings.Index(bar[h:], "</div>")]
691 if !strings.Contains(group, `role="group"`) {
692 t.Errorf("the history pill is not a group: %s", group)
693 }
694 buttons := regexp.MustCompile(`<span estilo-bar-control><button [^>]*data-command="(undo|redo)"[^>]*aria-label="([^"]+)"[^>]*>`).FindAllStringSubmatch(group, -1)
695 if len(buttons) != 2 || buttons[0][1] != "undo" || buttons[1][1] != "redo" {
696 t.Fatalf("the history pill holds %d wrapped, named buttons; want undo then redo, each in an estilo-bar-control:\n%s", len(buttons), group)
697 }
698 if buttons[0][2] == buttons[1][2] {
699 t.Errorf("undo and redo share the name %q", buttons[0][2])
700 }
701}
702
703// readSource is a file of the slides package, by its path under
704// internal/slides.
705func readSource(t *testing.T, name string) string {
706 t.Helper()
707 b, err := os.ReadFile(filepath.Join("..", "slides", filepath.FromSlash(name)))
708 if err != nil {
709 t.Fatal(err)
710 }
711 return string(b)
712}
713
714// goStrings is the quoted strings of the Go var declared as
715// `var name = []string{…}` in src.
716func goStrings(t *testing.T, src, name string) []string {
717 t.Helper()
718 block := regexp.MustCompile(`(?s)var ` + name + ` = \[\]string\{(.*?)\}`).FindStringSubmatch(src)
719 if block == nil {
720 t.Fatalf("editor.go declares no var %s = []string{…}", name)
721 }
722 var out []string
723 for _, m := range regexp.MustCompile(`"([^"]*)"`).FindAllStringSubmatch(block[1], -1) {
724 out = append(out, m[1])
725 }
726 return out
727}
728
729// editorKeysRead is every slides.editor key the editor reads, without
730// its prefix: by editor.html through L (the strings a module needs
731// included: the template renders them into the data-* attributes it
732// reads), by its add-block menus as type.<block type>, or by editor.go
733// itself through page.S.
734func editorKeysRead(t *testing.T, tmpl, src string) map[string]bool {
735 t.Helper()
736 read := map[string]bool{}
737 for _, m := range regexp.MustCompile(`\.L "([^"]+)"`).FindAllStringSubmatch(tmpl, -1) {
738 read[m[1]] = true
739 }
740 for _, m := range regexp.MustCompile(`\.L \(printf "([^"%]*)%s" \.\)`).FindAllStringSubmatch(tmpl, -1) {
741 for _, b := range goStrings(t, src, "blockTypes") {
742 read[m[1]+b] = true
743 }
744 }
745 for _, m := range regexp.MustCompile(`page\.S\["([^"]+)"\]`).FindAllStringSubmatch(src, -1) {
746 read[m[1]] = true
747 }
748 return read
749}
750
751// editorCatalog is en.toml's slides.editor strings, by key without the
752// prefix.
753func editorCatalog(t *testing.T) map[string]string {
754 t.Helper()
755 catalog := map[string]string{}
756 for _, m := range regexp.MustCompile(`(?m)^slides\.editor\.(\S+) = "(.*)"$`).FindAllStringSubmatch(readSource(t, "locales/en.toml"), -1) {
757 catalog[m[1]] = m[2]
758 }
759 return catalog
760}
761
762// TestEditorTidyStrings: the strings of the tidied editor are catalog
763// keys, each in en.toml with the strings table's English and in
764// editorStrings; every key in editorStrings is read (by editor.html
765// through L, by its add-block menus as type.<block type>, or by
766// editor.go itself through page.S); every key editor.html reads is in
767// editorStrings; and the keys the old inspector and notes switch read
768// are gone from both. The table's group labels that already had a key
769// with the same English reuse it: Text is format.label, Alignment
770// block.align and On a phone block.phone. The card's Deck group and its
771// Title field went with Q2 (the Deck settings dialog, file.deck and
772// deck.settings), and their keys with them.
773func TestEditorTidyStrings(t *testing.T) {
774 toml := readSource(t, "locales/en.toml")
775 catalog := map[string]string{}
776 for _, m := range regexp.MustCompile(`(?m)^slides\.editor\.(\S+) = "(.*)"$`).FindAllStringSubmatch(toml, -1) {
777 catalog[m[1]] = m[2]
778 }
779 src := readSource(t, "editor.go")
780 listed := map[string]bool{}
781 keys := goStrings(t, src, "editorStrings")
782 for _, k := range keys {
783 listed[k] = true
784 }
785
786 for _, c := range []struct{ key, english string }{
787 {"head.format", "Format"},
788 {"notes.close", "Close notes"},
789 {"notes.resize", "Resize notes"},
790 {"view.both.title", "Both, side by side"},
791 {"group.style", "Style"},
792 {"group.image", "Image"},
793 {"group.description", "Description"},
794 {"group.slide", "This slide"},
795 // reused, not new
796 {"format.label", "Text"},
797 {"block.align", "Alignment"},
798 {"block.phone", "On a phone"},
799 // the quieter editor (Q2)
800 {"file.deck", "Deck settings…"},
801 {"deck.settings", "Deck settings"},
802 // the quieter editor (Q5)
803 {"view.outline", "Outline"},
804 {"outline.none", "This slide has no boxes yet."},
805 // the quieter editor (Q3)
806 {"layout.free", "Free placement"},
807 {"layout.leave.confirm", "Put the boxes back into this layout's regions? Their positions are dropped. Undo brings them back."},
808 {"layout.leave", "Use the layout"},
809 {"layout.leave.cancel", "Keep free placement"},
810 {"slide.background", "Background"},
811 {"slide.transition", "Transition"},
812 // the quieter editor (Q4)
813 {"level.1", "Title"}, {"level.2", "Heading"}, {"level.3", "Subheading"},
814 {"box.more", "More for this box"},
815 {"box.place", "Position and size…"},
816 {"place.title", "Position and size"},
817 {"box.region", "Move to region"},
818 {"box.delete", "Delete"},
819 {"keys.box.forward", "Bring the box forward"},
820 {"keys.box.back", "Send the box back"},
821 {"keys.box.up", "Move the box up"},
822 {"keys.box.down", "Move the box down"},
823 {"keys.box.delete", "Delete the selected box"},
824 {"keys.box.menu", "Open the box's menu"},
825 // the quieter editor (Q8): the closed notes' tab
826 {"notes.tab", "Notes"},
827 {"notes.tab.words", "Notes · {n} words"},
828 } {
829 if got, ok := catalog[c.key]; !ok {
830 t.Errorf("en.toml has no slides.editor.%s", c.key)
831 } else if got != c.english {
832 t.Errorf("slides.editor.%s is %q, want %q", c.key, got, c.english)
833 }
834 if !listed[c.key] {
835 t.Errorf("editorStrings does not list %s", c.key)
836 }
837 }
838
839 // Each group in the card is labelled by the key the table gives it.
840 tmpl := readSource(t, "templates/editor.html")
841 for group, key := range map[string]string{
842 "slide": "group.slide",
843 "appears": "appears", // how the box arrives (steps and presenter §4)
844 "style": "group.style", "text": "format.label", "alignment": "block.align",
845 "image": "group.image", "description": "group.description", "place": "place.title",
846 "phone": "block.phone",
847 } {
848 label := `<span estilo-inspector-label id="slides-g-` + group + `">{{.L "` + key + `"}}</span>`
849 if group == "place" {
850 // A closed disclosure, named by its summary (quieter §4.3).
851 label = `id="slides-g-place"><span>{{.L "` + key + `"}}</span>`
852 }
853 if !strings.Contains(tmpl, label) {
854 t.Errorf("editor.html has no %s", label)
855 }
856 }
857
858 // Every listed key is read, and every key read is listed.
859 read := editorKeysRead(t, tmpl, src)
860 for _, k := range keys {
861 if !read[k] {
862 t.Errorf("editorStrings lists %s, which nothing reads", k)
863 }
864 if _, ok := catalog[k]; !ok {
865 t.Errorf("editorStrings lists %s, which en.toml does not define", k)
866 }
867 }
868 for _, m := range regexp.MustCompile(`\.L "([^"]+)"`).FindAllStringSubmatch(tmpl, -1) {
869 if !listed[m[1]] {
870 t.Errorf("editor.html reads %s, which editorStrings does not list", m[1])
871 }
872 }
873
874 // The old inspector's, the notes switch's and the notes icon's keys
875 // are gone.
876 for _, k := range []string{"head.inspector", "inspector", "inspector.empty", "inspector.slide", "inspector.block", "inspector.blocks", "slide.notes.show", "inspector.deck", "deck.title",
877 "group.boxes", "inspector.blocks.none", "block.add",
878 "slide.layout", "layout.flow", "layout.canvas", "layout.flow.desc", "layout.canvas.desc", "slide.template",
879 "block.delete", "group.place",
880 // the closed notes were an icon until Q8; the card had a heading
881 "notes.title", "notes.open", "notes.open.has"} {
882 if _, ok := catalog[k]; ok {
883 t.Errorf("en.toml still defines slides.editor.%s", k)
884 }
885 if listed[k] {
886 t.Errorf("editorStrings still lists %s", k)
887 }
888 }
889
890 // The view switch is icons now; the catalog's note on it says so.
891 if strings.Contains(toml, "never icons alone") {
892 t.Error("en.toml still says the view switch is words, never icons alone")
893 }
894}
895
896// TestEditorTidyNoDeadSelectors: what the inspector's <details>, its
897// off switch and the notes switch were drawn and wired by is gone from
898// the stylesheet and the bundle's sources; and the view switch's first
899// corner is estilo's, which rounds the first segment after a <legend>.
900func TestEditorTidyNoDeadSelectors(t *testing.T) {
901 files := []string{filepath.Join("..", "slides", "static", "editor.css"), filepath.Join("..", "..", "fixtures", "editor-entry.js")}
902 mjs, err := filepath.Glob(filepath.Join("..", "..", "fixtures", "*.mjs"))
903 if err != nil || len(mjs) == 0 {
904 t.Fatalf("no fixtures/*.mjs (%v)", err)
905 }
906 files = append(files, mjs...)
907 for _, f := range files {
908 b, err := os.ReadFile(f)
909 if err != nil {
910 t.Fatal(err)
911 }
912 for _, dead := range []string{"data-inspector-off", "slides-notes-toggle", "details[slides-inspector]"} {
913 if strings.Contains(string(b), dead) {
914 t.Errorf("%s still mentions %s", filepath.Base(f), dead)
915 }
916 }
917 }
918 if regexp.MustCompile(`\[slides-switcher\][^{]*nth-child`).MatchString(readStatic(t, "editor.css")) {
919 t.Error("static/editor.css still rounds the view switch's first segment itself (estilo does)")
920 }
921}
922