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 "fmt"
5 "html"
6 "net/http"
7 "os"
8 "path/filepath"
9 "regexp"
10 "strings"
11 "sync/atomic"
12 "testing"
13)
14
15// The quieter editor (docs/superpowers/specs/2026-11-17-quieter-editor-design.md),
16// as the server renders it.
17
18// TestQuieterStripMarkup (spec §6.1): the strip's foot is gone; New
19// slide and its layouts chevron are li[slides-strip-new], the <ol>'s
20// last child, after every slide; and each slide's link carries its
21// label as its title, the tooltip that stands in for the words no
22// longer drawn under the picture; and each link starts with the slide's
23// number as text, drawn (aria-hidden) and said (visually hidden).
24func TestQuieterStripMarkup(t *testing.T) {
25 a := newApp(t)
26 cookies := signedIn(t, a, allowedAddress)
27 page := mustGet(t, a, deckWithSlides(t, a, cookies, 40), http.StatusOK, cookies).Body.String()
28 live := regexp.MustCompile(`(?s)<template.*?</template>`).ReplaceAllString(page, "")
29
30 if strings.Contains(page, "slides-strip-foot") {
31 t.Error("the editor still has a slides-strip-foot")
32 }
33 if css := readStatic(t, "editor.css"); strings.Contains(css, "slides-strip-foot") {
34 t.Error("static/editor.css still styles slides-strip-foot")
35 }
36
37 ol := homeOf(live, homeStrip)
38 if ol == "" {
39 t.Fatal("no <ol slides-strip> in the editor")
40 }
41 row := regexp.MustCompile(`<li slides-strip-new[\s>]`).FindStringIndex(ol)
42 if row == nil {
43 t.Fatalf("the strip has no <li slides-strip-new>:\n%s", ol)
44 }
45 tail := ol[row[0]:]
46 if li := extent(tail, 0); li == "" || !regexp.MustCompile(`^\s*</ol>$`).MatchString(tail[len(li):]) {
47 t.Errorf("<li slides-strip-new> is not the strip's last child:\n%s", tail)
48 } else {
49 for _, want := range []string{`data-command="new-slide"`, `slides-layouts-toggle`, `popovertarget="slides-layouts"`, `id="slides-layouts"`} {
50 if !strings.Contains(li, want) {
51 t.Errorf("the New slide row has no %s:\n%s", want, li)
52 }
53 }
54 }
55 if n := strings.Count(ol[:row[0]], "<li slides-strip-item"); n != 40 {
56 t.Errorf("%d slides come before the New slide row, want all 40", n)
57 }
58
59 items := regexp.MustCompile(`(?s)<li slides-strip-item.*?</li>`).FindAllString(ol, -1)
60 link := regexp.MustCompile(`<a slides-strip-link [^>]*\btitle="([^"]*)"`)
61 label := regexp.MustCompile(`<span slides-strip-label>([^<]*)</span>`)
62 for i, li := range items {
63 l, w := link.FindStringSubmatch(li), label.FindStringSubmatch(li)
64 switch {
65 case l == nil:
66 t.Errorf("slide %d's link has no title:\n%s", i+1, li)
67 case w == nil:
68 t.Errorf("slide %d has no [slides-strip-label]:\n%s", i+1, li)
69 case l[1] != w[1] || l[1] == "":
70 t.Errorf("slide %d's link is titled %q, its label is %q", i+1, l[1], w[1])
71 }
72 // The number is text: drawn and hidden from a screen reader, and
73 // its twin said, both before the label inside the link.
74 n := fmt.Sprint(i + 1)
75 want := `<span slides-strip-number aria-hidden="true">` + n + `</span>
76 <span slides-strip-number-said>` + n + `</span>
77 <span slides-strip-label>`
78 if !strings.Contains(li, want) {
79 t.Errorf("slide %d's link does not start with its number, drawn and said, before the label:\n%s", i+1, li)
80 }
81 }
82 if regexp.MustCompile(`counter\(|counter-(reset|increment)`).MatchString(readStatic(t, "editor.css")) {
83 t.Error("static/editor.css still draws a CSS counter: the number is text now, and a counter could be said twice")
84 }
85
86 // The item the bundle clones has the title too, for relabel() to fill.
87 tmpl := regexp.MustCompile(`(?s)<template slides-strip-item-template>.*?</template>`).FindString(page)
88 if !regexp.MustCompile(`<a slides-strip-link [^>]*\btitle=`).MatchString(tmpl) {
89 t.Errorf("the strip item template's link has no title:\n%s", tmpl)
90 }
91}
92
93// The block in static/editor.css that holds every value the editor
94// needs and the published set lacks (spec §7), and the published sizes
95// and radii a declaration outside it uses instead.
96const (
97 namedValuesStart = "/* slides: named values */"
98 namedValuesEnd = "/* end named values */"
99 publishedSizes = "--rst-fs-xs|sm|base|lg, --rst-radius, --rst-radius-sm, --rst-radius-pill, --estilo-float-radius"
100)
101
102// TestQuieterDeckSettingsMarkup (spec §8): the deck's settings are a
103// <dialog slides-deck-dialog> after </main>, inside [slides-editor] so
104// its events reach the bundle, named by its own title; it holds the
105// deck form, method="dialog", with every deck control but the title
106// and one submit button, Close. Nothing in it carries the card's old
107// Deck group label. File has the row that opens it; the card holds no
108// deck form.
109func TestQuieterDeckSettingsMarkup(t *testing.T) {
110 a := newApp(t)
111 cookies := signedIn(t, a, allowedAddress)
112 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
113 live := regexp.MustCompile(`(?s)<template.*?</template>`).ReplaceAllString(page, "")
114
115 open := regexp.MustCompile(`<dialog slides-deck-dialog id="slides-deck-dialog" aria-labelledby="([^"]+)"[^>]*>`).FindStringSubmatchIndex(live)
116 if open == nil {
117 t.Fatalf("no <dialog slides-deck-dialog id=\"slides-deck-dialog\" aria-labelledby=…> in the editor")
118 }
119 dialog := extent(live, open[0])
120 if main := strings.Index(live, "</main>"); main < 0 || open[0] < main {
121 t.Error("the deck dialog is not after </main>")
122 }
123 if root := strings.Index(live, "<div slides-editor"); root < 0 || !strings.Contains(extent(live, root), `<dialog slides-deck-dialog`) {
124 t.Error("the deck dialog is not inside [slides-editor], where the bundle listens")
125 }
126 title := regexp.MustCompile(`<h2 id="` + regexp.QuoteMeta(live[open[2]:open[3]]) + `">([^<]*)</h2>`).FindStringSubmatch(dialog)
127 if title == nil || title[1] != "Deck settings" {
128 t.Errorf("the dialog's aria-labelledby names %v, want an <h2> saying Deck settings", title)
129 }
130
131 forms := regexp.MustCompile(`<form [^>]*>`).FindAllString(dialog, -1)
132 if len(forms) != 1 || forms[0] != `<form rst-form slides-inspector-form data-section="deck" method="dialog">` {
133 t.Errorf("the dialog's forms are %q, want one <form rst-form slides-inspector-form data-section=\"deck\" method=\"dialog\">", forms)
134 }
135 for _, want := range []string{
136 `name="rst-deck-theme"`, `name="rst-deck-accent"`, `data-command="accent-reset"`,
137 `name="rst-deck-font-heading"`, `name="rst-deck-font-body"`, `<fieldset slides-unused-assets`,
138 } {
139 if !strings.Contains(dialog, want) {
140 t.Errorf("the deck dialog has no %s", want)
141 }
142 }
143 if strings.Contains(dialog, "rst-deck-title") {
144 t.Error("the deck dialog still has a title field: the bar's is the one place")
145 }
146 submits := regexp.MustCompile(`<button[^>]*\stype="submit"[^>]*>([^<]*)</button>`).FindAllStringSubmatch(dialog, -1)
147 if len(submits) != 1 || submits[0][1] != "Close" {
148 t.Errorf("the dialog's submit buttons are %q, want one, Close", submits)
149 }
150 if n := strings.Count(dialog, "<button"); n != 2 {
151 t.Errorf("the dialog has %d buttons, want Close and the accent's reset", n)
152 }
153 for _, old := range []string{`slides-g-deck`, `estilo-inspector-label`, `>Deck<`} {
154 if strings.Contains(dialog, old) {
155 t.Errorf("the deck dialog still has the card's Deck group label (%s)", old)
156 }
157 }
158
159 file := regexp.MustCompile(`(?s)<details[^>]*slides-menu="file".*?</details>`).FindString(live)
160 row := `<button estilo-menu-item type="button" data-open-dialog="slides-deck-dialog"><span estilo-menu-label>Deck settings…</span></button>`
161 if !strings.Contains(file, row) {
162 t.Errorf("the File menu has no %s:\n%s", row, file)
163 }
164 if n := strings.Count(live, `data-open-dialog="slides-deck-dialog"`); n != 1 {
165 t.Errorf("%d rows open the deck dialog, want File's alone", n)
166 }
167 card := extent(live, strings.Index(live, "<aside slides-inspector"))
168 if strings.Contains(card, `data-section="deck"`) {
169 t.Error("the card still holds a deck form")
170 }
171}
172
173// TestQuieterCSSUsesTokens (spec §7): outside the one named-values
174// block, no font-size, font shorthand or border radius (border-radius
175// and every longhand, physical or logical: border-start-start-radius,
176// border-top-left-radius) in editor.css states a number with a unit;
177// it uses a published token or a name from the block. A percentage
178// counts as a unit (a circle's 50% is a named value); a bare 0, a
179// unitless line height (the 1 in "600 14px/1") and keywords (inherit)
180// are not sizes. Comments are not read. Inside the block every
181// declaration is a custom property, declared once, and each is used
182// somewhere: in editor.css or a template.
183func TestQuieterCSSUsesTokens(t *testing.T) {
184 raw := readStatic(t, "editor.css")
185 // With no block (or a broken one) the whole file is outside it, so
186 // every offending line is still listed.
187 start, end := 0, 0
188 if n, m := strings.Count(raw, namedValuesStart), strings.Count(raw, namedValuesEnd); n != 1 || m != 1 {
189 t.Errorf("static/editor.css has %d %q and %d %q, want one block, opened and closed once, at the top of the file", n, namedValuesStart, m, namedValuesEnd)
190 } else if start, end = strings.Index(raw, namedValuesStart), strings.Index(raw, namedValuesEnd)+len(namedValuesEnd); end < start {
191 t.Errorf("static/editor.css closes its named values before it opens them")
192 start, end = 0, 0
193 }
194 // Comments blanked to spaces, newlines kept, so an offset is still
195 // the line it was on.
196 css := regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllStringFunc(raw, blankKeepingLines)
197 block := css[start:end]
198 outside := css[:start] + blankKeepingLines(block) + css[end:]
199 lineAt := func(off int) int { return strings.Count(raw[:off], "\n") + 1 }
200
201 decl := regexp.MustCompile(`(?m)(?:^|[{;])\s*(--[-\w]+|[a-zA-Z][-a-zA-Z]*)\s*:\s*([^;{}]*)`)
202 sized := regexp.MustCompile(`^(font|font-size|border(-[a-z]+)*-radius)$`)
203 // A custom property that holds a size or a radius by its name, so
204 // the rule cannot be stepped round by naming a number outside the
205 // block.
206 sizedName := regexp.MustCompile(`^--.*(radius|-fs-|font-size)`)
207 withUnit := regexp.MustCompile(`(?:^|[^-\w.#])[+-]?(?:\d+\.?\d*|\.\d+)(?:[a-zA-Z]+|%)`)
208 for _, m := range decl.FindAllStringSubmatchIndex(outside, -1) {
209 prop, value := outside[m[2]:m[3]], outside[m[4]:m[5]]
210 if !sized.MatchString(prop) && !sizedName.MatchString(prop) {
211 continue
212 }
213 var nums []string
214 for _, u := range withUnit.FindAllString(value, -1) {
215 nums = append(nums, strings.TrimLeft(u, " \t\n:(,/"))
216 }
217 if len(nums) > 0 {
218 n := lineAt(m[2])
219 t.Errorf("static/editor.css:%d: \"%s: %s\" states %s: use a published token (%s) or add a named value to the block between %s and %s, named for what it is for",
220 n, prop, strings.TrimSpace(value), strings.Join(nums, ", "), publishedSizes, namedValuesStart, namedValuesEnd)
221 }
222 }
223
224 // Inside the block: custom properties only, each declared once and
225 // used somewhere.
226 if end == 0 {
227 return
228 }
229 if strings.Contains(css[:start], "{") {
230 t.Error("the named values block is not at the top of static/editor.css: a rule comes before it")
231 }
232 if strings.Count(block, "{") != 1 || strings.Count(block, "}") != 1 {
233 t.Fatalf("the named values block is not one rule: it has %d { and %d }", strings.Count(block, "{"), strings.Count(block, "}"))
234 }
235 body := block[strings.Index(block, "{")+1 : strings.Index(block, "}")]
236 used := outside
237 for _, name := range []string{"editor.html", "layout.html"} {
238 b, err := os.ReadFile(filepath.Join("..", "slides", "templates", name))
239 if err != nil {
240 t.Fatal(err)
241 }
242 used += string(b)
243 }
244 seen := map[string]bool{}
245 for _, d := range strings.Split(body, ";") {
246 d = strings.TrimSpace(d)
247 if d == "" {
248 continue
249 }
250 name, _, _ := strings.Cut(d, ":")
251 name = strings.TrimSpace(name)
252 switch {
253 case !strings.HasPrefix(name, "--"):
254 t.Errorf("the named values block declares %q, which is not a custom property: only names are declared there", d)
255 case seen[name]:
256 t.Errorf("the named values block declares %s twice", name)
257 case !strings.Contains(used, "var("+name+")") && !strings.Contains(used, "var("+name+",") && !strings.Contains(used, "var("+name+" "):
258 t.Errorf("the named values block declares %s and nothing uses it: drop it, or use it", name)
259 }
260 seen[name] = true
261 if strings.HasPrefix(name, "--") && regexp.MustCompile(`(?m)(?:^|[{;])\s*`+regexp.QuoteMeta(name)+`\s*:`).MatchString(outside) {
262 t.Errorf("%s is declared in the named values block and again outside it: a named value is declared once", name)
263 }
264 }
265 if len(seen) == 0 {
266 t.Error("the named values block declares nothing")
267 }
268}
269
270// blankKeepingLines is s with every byte but a newline made a space: the
271// same length, so an offset into it is an offset into s.
272func blankKeepingLines(s string) string {
273 b := []byte(s)
274 for i, c := range b {
275 if c != '\n' {
276 b[i] = ' '
277 }
278 }
279 return string(b)
280}
281
282// layoutPictures is the card's row of layout pictures (spec §3.1), in
283// order, each with the name it is said and shown by: the seven
284// templates, then Free placement.
285var layoutPictures = []struct{ value, name string }{
286 {"title", "Title"}, {"title-body", "Title and body"}, {"two-column", "Two columns"},
287 {"section", "Section"}, {"image-full", "Full image"}, {"quote", "Quote"}, {"blank", "Blank"},
288 {"free", "Free placement"},
289}
290
291// childrenOf is the elements directly inside el (an element's whole
292// markup, from its opening tag), each its whole markup. Text and
293// template comments between them are skipped; none is a void element.
294func childrenOf(el string) []string {
295 var out []string
296 body := el[strings.Index(el, ">")+1:]
297 for {
298 at := strings.Index(body, "<")
299 if at < 0 || strings.HasPrefix(body[at:], "</") {
300 return out
301 }
302 c := extent(body, at)
303 if c == "" {
304 return out
305 }
306 out = append(out, c)
307 body = body[at+len(c):]
308 }
309}
310
311// openTag is el's opening tag.
312func openTag(el string) string { return el[:strings.Index(el, ">")+1] }
313
314// TestQuieterPhoneOnlyMarkup (spec §3.4, Q6, ruling R18): the slide's
315// phone field, the box's On a phone group and the stage's one line are
316// in the page for every view, and the stylesheet draws the field and
317// the group in the phone views alone and the line in Phone view alone
318// (in Both, placing a box edits the desktop layout): the server paints
319// Desktop (the view is applied after mount), so at the first paint none
320// of the three is drawn and nothing moves when the bundle mounts.
321func TestQuieterPhoneOnlyMarkup(t *testing.T) {
322 a := newApp(t)
323 cookies := signedIn(t, a, allowedAddress)
324 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
325 live := regexp.MustCompile(`(?s)<template.*?</template>`).ReplaceAllString(page, "")
326
327 if !regexp.MustCompile(`<div slides-editor[^>]*\sdata-view="desktop"`).MatchString(live) {
328 t.Error("the editor's root does not carry data-view=\"desktop\" at the first paint")
329 }
330 parts := homeOf(live, homePhoneOnly)
331 if n := strings.Count(parts, "data-phone-only") + strings.Count(parts, `data-group="phone"`); n != 2 {
332 t.Errorf("the phone-only parts are marked %d times, want the slide's field and the box's group:\n%s", n, parts)
333 }
334 css := readStatic(t, "editor.css")
335 rule := regexp.MustCompile(`(?s)\[slides-editor\]:not\(:is\(\[data-view="phone"\], \[data-view="both"\]\)\)\s+:is\(\[data-group="phone"\], \[data-phone-only\]\)\s*\{[^}]*display:\s*none`)
336 if !rule.MatchString(css) {
337 t.Error("editor.css has no rule hiding [data-group=\"phone\"] and [data-phone-only] outside the phone and both views")
338 }
339 only := regexp.MustCompile(`(?s)\[slides-editor\]:not\(\[data-view="phone"\]\)\s+\[slides-phone-editing\]\s*\{[^}]*display:\s*none`)
340 if !only.MatchString(css) {
341 t.Error("editor.css has no rule hiding [slides-phone-editing] outside Phone view (ruling R18: not in Both, where placing a box edits the desktop)")
342 }
343 bar := extent(live, strings.Index(live, "<div slides-stage-bar>"))
344 line := regexp.MustCompile(`<[a-z]+ [^>]*slides-phone-editing[^>]*>([^<]*)<`).FindStringSubmatch(live)
345 if line == nil || line[1] != "Editing the phone layout" || !strings.Contains(bar, "slides-phone-editing") {
346 t.Errorf("the stage bar has no line saying Editing the phone layout: %v", line)
347 }
348 if !regexp.MustCompile(`<[a-z]+ [^>]*(role="status"|aria-live="polite")[^>]*>\s*<span slides-phone-editing`).MatchString(live) {
349 t.Error("the phone line is not in a live region that exists in every view")
350 }
351}
352
353// TestQuieterCardAtRest (spec §3.1, §0.3): with nothing selected the
354// card is the slide's layout as eight pictures, two quiet rows that each
355// open a popover holding today's field, the two questions (hidden until
356// asked). The slide's phone field is in the group, drawn by the phone
357// views alone (TestQuieterPhoneOnlyMarkup). No standing help. The server lights the painted slide's picture, so the first
358// paint is the final one.
359func TestQuieterCardAtRest(t *testing.T) {
360 a := newApp(t)
361 cookies := signedIn(t, a, allowedAddress)
362 token := mintAgent(t, a, cookies, "card-at-rest")
363
364 for _, pic := range layoutPictures {
365 sqid := apiDeck(t, a, token, "At rest "+pic.value)
366 view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK)
367 first, _ := view["slides"].([]any)[0].(map[string]any)["id"].(string)
368 set := `"template":"` + pic.value + `"`
369 if pic.value == "free" {
370 set = `"layout":"canvas"`
371 }
372 apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"set_slide","id":`+quote(first)+`,`+set+`}]}`, http.StatusOK)
373 page := mustGet(t, a, "/d/"+sqid+"/edit", http.StatusOK, cookies).Body.String()
374 live := regexp.MustCompile(`(?s)<template.*?</template>`).ReplaceAllString(page, "")
375 form := homeOf(live, homeCardSlide)
376 if form == "" {
377 t.Fatal("no slide form in the card")
378 }
379 canvas := pic.value == "free"
380 where := "a slide of " + pic.value
381
382 // The form: the two questions, hidden, then the one group.
383 kids := childrenOf(form)
384 if len(kids) != 3 {
385 t.Fatalf("%s: the slide form has %d children, want the two questions and the group:\n%s", where, len(kids), form)
386 }
387 for i, want := range []string{`<div rst-callout rst-tone="warning" slides-confirm hidden>`, `<div rst-callout rst-tone="warning" slides-confirm-layout hidden>`} {
388 if openTag(kids[i]) != want {
389 t.Errorf("%s: the form's child %d opens %s, want %s", where, i+1, openTag(kids[i]), want)
390 }
391 }
392 q := kids[1]
393 for _, want := range []string{
394 "Put the boxes back into this layout&#39;s regions? Their positions are dropped. Undo brings them back.",
395 `data-command="layout-leave-free">Use the layout</button>`,
396 `data-command="layout-leave-free-cancel">Keep free placement</button>`,
397 } {
398 if !strings.Contains(q, want) {
399 t.Errorf("%s: the leave-free question has no %s:\n%s", where, want, q)
400 }
401 }
402
403 // The group: its label, the pictures, the rows, and the phone
404 // field (shown on a canvas slide alone).
405 group := kids[2]
406 parts := childrenOf(group)
407 var tags []string
408 for _, p := range parts {
409 tags = append(tags, openTag(p))
410 }
411 want := []string{
412 `<span estilo-inspector-label id="slides-g-slide">`,
413 `<fieldset slides-layout-pick aria-labelledby="slides-g-slide">`,
414 `<div slides-slide-rows>`,
415 }
416 // The phone field is in the markup on every slide, marked as the
417 // phone views' alone, and hidden unless the slide is canvas.
418 phone := `<div rst-field data-layout="canvas" data-phone-only hidden>`
419 if canvas {
420 phone = `<div rst-field data-layout="canvas" data-phone-only>`
421 }
422 want = append(want, phone)
423 if strings.Join(tags, "\n") != strings.Join(want, "\n") {
424 t.Errorf("%s: the slide group holds\n%s\nwant\n%s", where, strings.Join(tags, "\n"), strings.Join(want, "\n"))
425 continue
426 }
427 if strings.Contains(form, "rst-field-hint") || strings.Contains(form, "slides-seg-desc") {
428 t.Errorf("%s: the slide form still has a standing hint", where)
429 }
430
431 // The pictures: named This slide by the group's label alone (no
432 // legend saying it again), then eight labels, each its name as
433 // tooltip and accessible name, wrapping its radio and its
434 // drawing; the painted slide's lit.
435 pick := parts[1]
436 if strings.Contains(pick, "<legend") {
437 t.Errorf("%s: the pictures still have a legend; the group's label names them:\n%s", where, pick)
438 }
439 labels := regexp.MustCompile(`(?s)<label .*?</label>`).FindAllString(pick, -1)
440 if len(labels) != len(layoutPictures) {
441 t.Fatalf("%s: %d pictures, want %d", where, len(labels), len(layoutPictures))
442 }
443 var lit []string
444 for i, p := range layoutPictures {
445 l := labels[i]
446 open := `<label slides-layout-pick-item title="` + p.name + `" aria-label="` + p.name + `">`
447 if !strings.HasPrefix(l, open) {
448 t.Errorf("%s: picture %d opens %s, want %s", where, i+1, openTag(l), open)
449 }
450 if !regexp.MustCompile(`<input type="radio" name="slides-layout-pick" value="` + p.value + `"( checked)?>`).MatchString(l) {
451 t.Errorf("%s: picture %d has no radio for %s:\n%s", where, i+1, p.value, l)
452 }
453 if !strings.Contains(l, `aria-hidden="true"><svg viewBox="0 0 48 27"`) {
454 t.Errorf("%s: picture %d (%s) has no drawing hidden from a screen reader:\n%s", where, i+1, p.value, l)
455 }
456 if strings.Contains(l, " checked>") {
457 lit = append(lit, p.value)
458 }
459 }
460 if len(lit) != 1 || lit[0] != pic.value {
461 t.Errorf("%s: the server lit %v, want [%s]", where, lit, pic.value)
462 }
463
464 // The rows: a quiet button for each, and its popover after it,
465 // holding today's field.
466 rows := childrenOf(parts[2])
467 var rowTags []string
468 for _, r := range rows {
469 rowTags = append(rowTags, openTag(r))
470 }
471 wantRows := []string{
472 `<button type="button" slides-slide-row popovertarget="slides-pop-background">`,
473 `<div popover id="slides-pop-background" slides-slide-pop>`,
474 `<button type="button" slides-slide-row popovertarget="slides-pop-transition">`,
475 `<div popover id="slides-pop-transition" slides-slide-pop>`,
476 }
477 if strings.Join(rowTags, "\n") != strings.Join(wantRows, "\n") {
478 t.Errorf("%s: the rows are\n%s\nwant\n%s", where, strings.Join(rowTags, "\n"), strings.Join(wantRows, "\n"))
479 continue
480 }
481 for i, name := range []string{"Background", "Transition"} {
482 if !strings.Contains(rows[2*i], "<span>"+name+"</span>") {
483 t.Errorf("%s: the %s row does not say %s:\n%s", where, name, name, rows[2*i])
484 }
485 }
486 if !strings.Contains(rows[1], `name="rst-slide-background"`) || !strings.Contains(rows[1], `name="rst-slide-background-color"`) {
487 t.Errorf("%s: the background popover does not hold the background field and its colour", where)
488 }
489 if !strings.Contains(rows[3], `name="rst-slide-transition"`) {
490 t.Errorf("%s: the transition popover does not hold the transition field", where)
491 }
492 }
493}
494
495// TestQuieterBlockCardIsStyle (spec §3.2, §4.3): with a box selected the
496// card is that box's style. Its form holds no outlined pill (rst-btn),
497// no Delete block and no standing hint; heading level is three radio
498// segments named in words, size three named by the sizes; and the
499// position fields are a closed <details slides-place> group, shown on a
500// canvas slide alone, its summary the group's name.
501func TestQuieterBlockCardIsStyle(t *testing.T) {
502 a := newApp(t)
503 cookies := signedIn(t, a, allowedAddress)
504 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
505 live := regexp.MustCompile(`(?s)<template.*?</template>`).ReplaceAllString(page, "")
506 card := extent(live, strings.Index(live, "<aside slides-inspector"))
507 var form string
508 if loc := regexp.MustCompile(`<form [^>]*data-section="block"[^>]*>`).FindStringIndex(card); loc != nil {
509 form = extent(card, loc[0])
510 }
511 if form == "" {
512 t.Fatal("no block form in the card")
513 }
514 for _, gone := range []string{"rst-btn", "slides-block-delete", "rst-field-hint"} {
515 if strings.Contains(form, gone) {
516 t.Errorf("the block form still holds %s", gone)
517 }
518 }
519 // R17: one quiet line under the image's file field, describing it:
520 // its limits (with an object store; this app has none, so the page
521 // says why the field is disabled, in the same line).
522 line := `<p rst-text-muted slides-asset-hint id="slides-asset-hint">`
523 if image := regexp.MustCompile(`(?s)<div rst-field data-types="image">\s*<label rst-field-label for="rst-block-image">.*?</p>`).FindString(form); !strings.Contains(image, line) || !strings.Contains(image, `aria-describedby="slides-asset-hint"`) {
524 t.Errorf("the image file field has no %s describing it:\n%s", line, image)
525 }
526 if tmpl := readSource(t, "templates/editor.html"); !strings.Contains(tmpl, line+`{{if .HasStore}}{{.L "block.image.hint"}}{{else}}`) {
527 t.Errorf("editor.html does not draw the image's limits (block.image.hint) in %s", line)
528 }
529
530 for _, seg := range []struct {
531 name string
532 values []string
533 words []string
534 }{
535 {"rst-block-level", []string{"1", "2", "3"}, []string{"Title", "Heading", "Subheading"}},
536 {"rst-block-size", []string{"s", "m", "l"}, []string{"Small", "Medium", "Large"}},
537 } {
538 if strings.Contains(form, `<select rst-input="short" id="`+seg.name+`"`) {
539 t.Errorf("%s is still a <select>", seg.name)
540 }
541 got := regexp.MustCompile(`<label estilo-seg-item><input type="radio" name="`+seg.name+`" value="([^"]+)"[^>]*>([^<]+)</label>`).FindAllStringSubmatch(form, -1)
542 var values, words []string
543 for _, m := range got {
544 values, words = append(values, m[1]), append(words, m[2])
545 }
546 if strings.Join(values, " ") != strings.Join(seg.values, " ") || strings.Join(words, "|") != strings.Join(seg.words, "|") {
547 t.Errorf("%s's segments are %v named %q, want %v named %q", seg.name, values, words, seg.values, seg.words)
548 }
549 }
550
551 place := ""
552 for _, g := range tidyGroups(form) {
553 if g.name == "place" {
554 place = form[g.start:g.end]
555 }
556 }
557 at := strings.Index(place, "<details slides-place")
558 if at < 0 {
559 t.Fatalf("the place group holds no <details slides-place>:\n%s", place)
560 }
561 details := extent(place, at)
562 if open := openTag(details); open != `<details slides-place data-layout="canvas">` {
563 t.Errorf("the place group opens %s, want it closed and for a canvas slide: <details slides-place data-layout=\"canvas\">", open)
564 }
565 if !regexp.MustCompile(`<summary [^>]*id="slides-g-place"[^>]*><span>Position and size</span>`).MatchString(details) {
566 t.Errorf("the place group's summary does not name it Position and size, as its group's label:\n%s", details)
567 }
568 for _, f := range []string{"x", "y", "w", "h"} {
569 if !strings.Contains(details, `name="rst-block-`+f+`"`) {
570 t.Errorf("rst-block-%s is not inside <details slides-place>", f)
571 }
572 }
573 if strings.Contains(place, "data-command") {
574 t.Error("the place group still holds a command button")
575 }
576}
577
578// boxMenuRows is the menu on a box, in its order (spec §4.2, R12): each
579// row's data-row and its words.
580var boxMenuRows = []struct{ row, words string }{
581 {"block-format", "Format"},
582 {"block-forward", "Bring forward"}, {"block-back", "Send back"},
583 {"block-move-up", "Move up"}, {"block-move-down", "Move down"},
584 {"block-place", "Position and size…"},
585 {"region", "Move to region"},
586 {"block-delete", "Delete"},
587}
588
589// TestQuieterBoxMenuMarkup (spec §4.1, §4.2): the round button and the
590// menu are the server's, after the stages and inside the stage pane;
591// the button is a named button that opens the menu, hidden until a box
592// is selected; the menu is estilo's, a manual popover with every row
593// drawn (the bundle hides those that do not apply), each row a button
594// with its words and, where it has one, its key; the region rows are a
595// group named Move to region, one radio row per region. The keyboard
596// shortcuts dialog lists the box's keys.
597func TestQuieterBoxMenuMarkup(t *testing.T) {
598 a := newApp(t)
599 cookies := signedIn(t, a, allowedAddress)
600 page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String()
601 live := regexp.MustCompile(`(?s)<template.*?</template>`).ReplaceAllString(page, "")
602
603 pane := extent(live, strings.Index(live, "<section slides-stage-pane"))
604 stages := strings.Index(pane, "<div slides-stages")
605 button := regexp.MustCompile(`<button [^>]*slides-box-more[^>]*>`).FindStringIndex(pane)
606 menuAt := regexp.MustCompile(`<div slides-box-menu [^>]*>`).FindStringIndex(pane)
607 if button == nil || menuAt == nil || stages < 0 {
608 t.Fatalf("the stage pane has no button[slides-box-more] or no div[slides-box-menu] (or no stages)")
609 }
610 if after := stages + len(extent(pane, stages)); button[0] < after || menuAt[0] < after {
611 t.Error("the round button and the menu are not after the stages")
612 }
613 bt := pane[button[0]:button[1]]
614 for _, want := range []string{`type="button"`, `aria-label="More for this box"`, `title="More for this box"`, `aria-haspopup="menu"`, `aria-expanded="false"`, `aria-controls="slides-box-menu"`, ` hidden`} {
615 if !strings.Contains(bt, want) {
616 t.Errorf("the round button %s has no %s", bt, want)
617 }
618 }
619 menu := extent(pane, menuAt[0])
620 if open := openTag(menu); open != `<div slides-box-menu estilo-menu popover="manual" role="menu" id="slides-box-menu" aria-label="More for this box">` {
621 t.Errorf("the menu opens %s", open)
622 }
623 var got []string
624 for _, m := range regexp.MustCompile(`<(?:button|div) [^>]*data-row="([^"]+)"`).FindAllStringSubmatch(menu, -1) {
625 got = append(got, m[1])
626 }
627 var want []string
628 for _, r := range boxMenuRows {
629 want = append(want, r.row)
630 re := `data-row="` + regexp.QuoteMeta(r.row) + `"[^>]*>(?:<span estilo-menu-check[^>]*>.*?</span>)?<span estilo-menu-label>` + regexp.QuoteMeta(r.words) + `</span>`
631 if r.row == "region" {
632 re = `<div role="group" slides-box-region data-row="region" aria-labelledby="slides-box-region-label"><p estilo-menu-heading id="slides-box-region-label">` + regexp.QuoteMeta(r.words) + `</p>`
633 }
634 if !regexp.MustCompile(re).MatchString(menu) {
635 t.Errorf("the menu's %s row does not say %q:\n%s", r.row, r.words, menu)
636 }
637 }
638 if strings.Join(got, " ") != strings.Join(want, " ") {
639 t.Errorf("the menu's rows are %v, want %v", got, want)
640 }
641 items := regexp.MustCompile(`<button estilo-menu-item(?:="danger")? type="button" role="(menuitem|menuitemradio)"[^>]*>`).FindAllStringSubmatch(menu, -1)
642 // How the box arrives (steps and presenter §4): three radio rows of
643 // their own, which box-menu-appears.mjs draws and runs.
644 appears := []string{"slide", "press", "each"}
645 if n := strings.Count(menu, "<button"); n != len(items) || n != len(boxMenuRows)-1+len(boxRegions)+len(appears) {
646 t.Errorf("the menu holds %d buttons, %d of them estilo's menu rows with a menu role; want %d", n, len(items), len(boxMenuRows)-1+len(boxRegions)+len(appears))
647 }
648 for _, a := range appears {
649 if !regexp.MustCompile(`role="menuitemradio" data-appears="` + a + `" aria-checked="false"`).MatchString(menu) {
650 t.Errorf("the menu has no radio row for appearing %s", a)
651 }
652 }
653 for _, r := range boxRegions {
654 if !regexp.MustCompile(`role="menuitemradio" data-region="` + r + `" aria-checked="false"`).MatchString(menu) {
655 t.Errorf("the menu has no radio row for the %s region", r)
656 }
657 }
658 if !strings.Contains(menu, `<button estilo-menu-item="danger" type="button" role="menuitem" data-row="block-delete"`) {
659 t.Error("Delete is not the menu's danger row")
660 }
661 // Each row with a key shows it, as the bar's menus do (read as
662 // text: the template writes a + as &#43;).
663 menu = html.UnescapeString(menu)
664 for row, key := range map[string]string{"block-forward": "Ctrl+]", "block-back": "Ctrl+[", "block-move-up": "Ctrl+Shift+↑", "block-move-down": "Ctrl+Shift+↓", "block-delete": "Delete"} {
665 if !regexp.MustCompile(`data-row="` + row + `"[^>]*>.*?<kbd estilo-menu-key aria-hidden="true">` + regexp.QuoteMeta(key) + `</kbd></button>`).MatchString(menu) {
666 t.Errorf("the %s row does not show its key %s", row, key)
667 }
668 }
669
670 keys := html.UnescapeString(extent(live, strings.Index(live, "<dialog slides-keys-dialog")))
671 for _, k := range []struct{ words, key string }{
672 {"Bring the box forward", "Ctrl+]"}, {"Send the box back", "Ctrl+["},
673 {"Move the box up", "Ctrl+Shift+↑"}, {"Move the box down", "Ctrl+Shift+↓"},
674 {"Open the box's menu", "Shift+F10"}, {"Delete the selected box", "Delete"},
675 } {
676 if row := `<div><dt>` + k.words + `</dt><dd><kbd>` + k.key + `</kbd></dd></div>`; !strings.Contains(keys, row) {
677 t.Errorf("the keyboard shortcuts dialog has no %s", row)
678 }
679 }
680}
681
682// notesMarkdown is notes that test the word count where the server's
683// and the browser's ways of reading them could part: marks inside a
684// word and across words, a link, paragraphs, and spaces doubled and
685// trailing. Seven words: "Bold words and a link" and "Second
686// paragraph."
687const notesMarkdown = "**Bold** words and [a link](https://example.com) \n\nSecond *para*graph. "
688
689// deckWithNotes is a deck of two slides whose first slide's notes are
690// markdown; it returns the editor's path.
691func deckWithNotes(t *testing.T, a *testApp, cookies []*http.Cookie, markdown string) string {
692 t.Helper()
693 // An agent's name is the person's own, once: each deck mints its own.
694 token := mintAgent(t, a, cookies, fmt.Sprintf("deck-with-notes-%d", decksWithNotes.Add(1)))
695 sqid := apiDeck(t, a, token, "Notes")
696 view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK)
697 slides, _ := view["slides"].([]any)
698 if len(slides) == 0 {
699 t.Fatal("a new deck has no slide")
700 }
701 first, _ := slides[0].(map[string]any)["id"].(string)
702 ops := []string{`{"op":"add_slide","layout":"flow","template":"title-body"}`}
703 if markdown != "" {
704 ops = append(ops, `{"op":"set_notes","id":`+quote(first)+`,"markdown":`+quote(markdown)+`}`)
705 }
706 apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK)
707 return "/d/" + sqid + "/edit"
708}
709
710// decksWithNotes numbers deckWithNotes' agents.
711var decksWithNotes atomic.Int64
712
713// notesTab is the closed notes' tab in an editor page: its opening tag
714// and its text.
715func notesTab(t *testing.T, page string) (open, text string) {
716 t.Helper()
717 m := regexp.MustCompile(`(?s)(<button[^>]*\sslides-notes-open[^>]*>)(.*?)</button>`).FindStringSubmatch(page)
718 if m == nil {
719 t.Fatal("no button[slides-notes-open] in the editor")
720 }
721 return m[1], m[2]
722}
723
724// TestQuieterNotesMarkup (spec §6.2, Q8): the notes card has no heading
725// and is not an estilo-card (no shadow; a hairline of its own), and is
726// named by the View menu's words for it; closed, it is a text tab that
727// says Notes, or Notes and the count of the first slide's words, from
728// the server, with the two strings the bundle rewrites it from. No
729// icon, no dot.
730func TestQuieterNotesMarkup(t *testing.T) {
731 a := newApp(t)
732 cookies := signedIn(t, a, allowedAddress)
733 page := mustGet(t, a, deckWithNotes(t, a, cookies, notesMarkdown), http.StatusOK, cookies).Body.String()
734
735 card := regexp.MustCompile(`(?s)<section slides-notes-pane[^>]*>.*?</section>`).FindString(page)
736 if card == "" {
737 t.Fatal("no <section slides-notes-pane> in the editor")
738 }
739 open := openTag(card)
740 if strings.Contains(open, "estilo-card") {
741 t.Errorf("the notes card is still an estilo-card: %s", open)
742 }
743 if !strings.Contains(open, `aria-label="Speaker notes"`) || strings.Contains(open, "aria-labelledby") {
744 t.Errorf("the notes card is not named by aria-label=\"Speaker notes\": %s", open)
745 }
746 if strings.Contains(card, "slides-notes-heading") || regexp.MustCompile(`<h[1-6][\s>]`).MatchString(card) {
747 t.Errorf("the notes card still has a heading:\n%s", card)
748 }
749 if !strings.Contains(card, `<div slides-notes data-placeholder="Add speaker notes">`) {
750 t.Errorf("the notes field is not named by its placeholder:\n%s", card)
751 }
752
753 tab, text := notesTab(t, page)
754 if text != "Notes · 7 words" {
755 t.Errorf("the tab says %q on a slide whose notes have seven words; want %q", text, "Notes · 7 words")
756 }
757 for _, want := range []string{`type="button"`, `data-label="Notes"`, `data-label-words="Notes · {n} words"`} {
758 if !strings.Contains(tab, want) {
759 t.Errorf("the tab %s has no %s", tab, want)
760 }
761 }
762 for _, gone := range []string{"data-has-notes", "aria-label", "title=", "data-label-has"} {
763 if strings.Contains(tab, gone) {
764 t.Errorf("the tab still carries %s: %s", gone, tab)
765 }
766 }
767 if strings.Contains(text, "<") {
768 t.Errorf("the tab holds markup (an icon?), not text: %q", text)
769 }
770 if regexp.MustCompile(`\sdisabled[\s>]`).MatchString(tab) {
771 t.Errorf("with slides the tab is disabled: %s", tab)
772 }
773
774 // A first slide without notes: the word alone.
775 if _, text := notesTab(t, mustGet(t, a, deckWithNotes(t, a, cookies, ""), http.StatusOK, cookies).Body.String()); text != "Notes" {
776 t.Errorf("on a slide without notes the tab says %q, want Notes", text)
777 }
778 // No slide at all: the word alone, and the tab disabled.
779 tab, text = notesTab(t, mustGet(t, a, emptyDeck(t, a, cookies), http.StatusOK, cookies).Body.String())
780 if text != "Notes" || !regexp.MustCompile(`\sdisabled[\s>]`).MatchString(tab) {
781 t.Errorf("with no slides the tab is %s%s; want it disabled, saying Notes", tab, text)
782 }
783}
784
785// quieterStrings is the quieter editor's strings table as Paul approved
786// it (rules.md, the copy review before Q2): every key under
787// slides.editor., with its English.
788var quieterStrings = []struct{ key, english string }{
789 {"file.deck", "Deck settings…"},
790 {"deck.settings", "Deck settings"},
791 {"layout.free", "Free placement"},
792 {"layout.leave.confirm", "Put the boxes back into this layout's regions? Their positions are dropped. Undo brings them back."},
793 {"layout.leave", "Use the layout"},
794 {"layout.leave.cancel", "Keep free placement"},
795 {"level.1", "Title"}, {"level.2", "Heading"}, {"level.3", "Subheading"},
796 {"box.more", "More for this box"},
797 {"box.place", "Position and size…"},
798 {"place.title", "Position and size"},
799 {"box.region", "Move to region"},
800 {"box.delete", "Delete"},
801 {"view.outline", "Outline"},
802 {"outline.none", "This slide has no boxes yet."},
803 {"phone.editing", "Editing the phone layout"},
804 {"notes.tab", "Notes"},
805 {"notes.tab.words", "Notes · {n} words"},
806 {"keys.box.forward", "Bring the box forward"},
807 {"keys.box.back", "Send the box back"},
808 {"keys.box.up", "Move the box up"},
809 {"keys.box.down", "Move the box down"},
810 {"keys.box.delete", "Delete the selected box"},
811 {"keys.box.menu", "Open the box's menu"},
812}
813
814// TestQuieterStrings (Q10): every key of the approved strings table is
815// read by the editor (none went unused), is in en.toml with exactly the
816// table's English, and is listed in editorStrings; the keys the table
817// reuses are still there; every key in editorStrings is read (as
818// TestEditorTidyStrings counts reading) and defined; and the keys the
819// quieter editor retired are gone from both, except block.level (the
820// heading level's legend) and block.box (the position fields' legend,
821// a quiet line), which the finished markup still reads.
822func TestQuieterStrings(t *testing.T) {
823 catalog := editorCatalog(t)
824 src := readSource(t, "editor.go")
825 tmpl := readSource(t, "templates/editor.html")
826 read := editorKeysRead(t, tmpl, src)
827 listed := map[string]bool{}
828 keys := goStrings(t, src, "editorStrings")
829 for _, k := range keys {
830 listed[k] = true
831 }
832
833 if len(quieterStrings) != 25 {
834 t.Fatalf("the strings table has %d keys, want the 25 Paul approved", len(quieterStrings))
835 }
836 for _, c := range quieterStrings {
837 if !read[c.key] {
838 t.Errorf("nothing in the editor reads %s", c.key)
839 }
840 if got, ok := catalog[c.key]; !ok {
841 t.Errorf("en.toml has no slides.editor.%s", c.key)
842 } else if got != c.english {
843 t.Errorf("slides.editor.%s is %q, want Paul's %q", c.key, got, c.english)
844 }
845 if !listed[c.key] {
846 t.Errorf("editorStrings does not list %s", c.key)
847 }
848 }
849
850 reused := []string{"slide.background", "slide.transition", "block.forward", "block.back", "block.move.up", "block.move.down", "keys.close"}
851 for _, tpl := range []string{"title", "title-body", "two-column", "section", "image-full", "quote", "blank"} {
852 reused = append(reused, "template."+tpl)
853 }
854 for _, r := range []string{"heading", "body", "aside", "media"} {
855 reused = append(reused, "region."+r)
856 }
857 for _, k := range reused {
858 if _, ok := catalog[k]; !ok || !listed[k] || !read[k] {
859 t.Errorf("the reused key %s is not defined, listed and read (%v, %v, %v)", k, ok, listed[k], read[k])
860 }
861 }
862
863 for _, k := range keys {
864 if !read[k] {
865 t.Errorf("editorStrings lists %s, which nothing reads", k)
866 }
867 if _, ok := catalog[k]; !ok {
868 t.Errorf("editorStrings lists %s, which en.toml does not define", k)
869 }
870 }
871
872 for _, k := range []string{"slide.layout", "layout.flow", "layout.canvas", "layout.flow.desc", "layout.canvas.desc",
873 "slide.template", "group.boxes", "inspector.blocks.none", "block.add", "block.delete",
874 "notes.title", "notes.open", "notes.open.has", "inspector.deck", "deck.title",
875 "block.region", "group.place"} {
876 if _, ok := catalog[k]; ok {
877 t.Errorf("en.toml still defines slides.editor.%s", k)
878 }
879 if listed[k] {
880 t.Errorf("editorStrings still lists %s", k)
881 }
882 if read[k] {
883 t.Errorf("the editor still reads %s", k)
884 }
885 }
886 // Kept: the plan listed them as removed, but the finished markup
887 // reads them.
888 for _, k := range []string{"block.level", "block.box"} {
889 if _, ok := catalog[k]; !ok || !listed[k] || !read[k] {
890 t.Errorf("%s is still read by the markup, so it stays defined and listed (%v, %v, %v)", k, ok, listed[k], read[k])
891 }
892 }
893}
894
895// TestQuieterNoDeadSelectors (Q10): what the strip's foot, the card's
896// Boxes list and its buttons, Add block, the notes' heading, the card's
897// Delete block, the layout's standing descriptions and the notes icon's
898// dot were drawn and wired by is gone from the stylesheet and the
899// bundle's sources (their tests included).
900func TestQuieterNoDeadSelectors(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{"slides-strip-foot", "slides-blocks-list", "slides-blocks-buttons", "slides-add-block",
913 "slides-notes-heading", "slides-block-delete", "slides-seg-desc", "data-has-notes"} {
914 if strings.Contains(string(b), dead) {
915 t.Errorf("%s still mentions %s", filepath.Base(f), dead)
916 }
917 }
918 }
919}
920