| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // The quieter editor's inventory (docs/superpowers/specs/2026-11-17-quieter-editor-design.md): |
| 11 | // one table naming where every field and command of the editor lives. |
| 12 | // A task that moves a control changes that control's row first; the test |
| 13 | // then says whether the markup agrees. A needle must be found in its |
| 14 | // home and nowhere else in the editor page outside <template> elements. |
| 15 | |
| 16 | // The homes. A block field's home is homeCardBlock+":"+group, the group |
| 17 | // being the data-group of the card's block form ("" is the form's tail |
| 18 | // after its last group, which holds nothing since Delete block moved |
| 19 | // to the box's menu). |
| 20 | const ( |
| 21 | homeCardSlide = "card-slide" // the card's slide form, every group of it |
| 22 | homeCardBlock = "card-block" // + ":" + group, the card's block form |
| 23 | homePopBackground = "pop-background" // #slides-pop-background[popover] |
| 24 | homePopTransition = "pop-transition" // #slides-pop-transition[popover] |
| 25 | homeDeckDialog = "deck-dialog" // <dialog slides-deck-dialog> |
| 26 | homeBoxMenu = "box-menu" // [slides-box-menu] |
| 27 | homeInsertMenu = "insert-menu" // the menubar's Insert menu |
| 28 | homeOutline = "outline" // #slides-outline[popover] |
| 29 | homePhoneOnly = "phone-only" // what only the phone view shows |
| 30 | homeStrip = "strip" // the strip's own rows |
| 31 | homeSlideMenu = "slide-menu" // the menubar's Slide menu |
| 32 | homeStagePane = "stage-pane" // the stage pane, outside the slide |
| 33 | ) |
| 34 | |
| 35 | // needle is one control, found by a string specific enough to mean only |
| 36 | // it, and the home it has today. |
| 37 | type needle struct{ needle, home string } |
| 38 | |
| 39 | // homes is the inventory, from the editor on main: every field, command |
| 40 | // and list the old inspector held, plus the stage bar's five marks. |
| 41 | var homes = buildHomes() |
| 42 | |
| 43 | func buildHomes() []needle { |
| 44 | var t []needle |
| 45 | add := func(home string, needles ...string) { |
| 46 | for _, n := range needles { |
| 47 | t = append(t, needle{n, home}) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // The deck's settings are a dialog under File (Q2, spec §8); its |
| 52 | // title is the bar's alone (retired, below). |
| 53 | for _, n := range []string{"theme", "accent", "font-heading", "font-body"} { |
| 54 | add(homeDeckDialog, `name="rst-deck-`+n+`"`) |
| 55 | } |
| 56 | add(homeDeckDialog, `data-command="accent-reset"`, `<fieldset slides-unused-assets`) |
| 57 | // How what waits for a press arrives (steps and presenter spec §4). |
| 58 | add(homeDeckDialog, `name="rst-deck-arrive"`) |
| 59 | |
| 60 | // The slide's layout and template are one row of pictures (Q3, |
| 61 | // spec §3.1), and leaving Free placement asks in the card; |
| 62 | // background and transition are each a quiet row's popover. |
| 63 | for _, v := range []string{"title", "title-body", "two-column", "section", "image-full", "quote", "blank", "free"} { |
| 64 | add(homeCardSlide, `name="slides-layout-pick" value="`+v+`"`) |
| 65 | } |
| 66 | add(homeCardSlide, `<fieldset slides-layout-pick`, `slides-confirm-layout`, |
| 67 | `data-command="layout-leave-free"`, `data-command="layout-leave-free-cancel"`) |
| 68 | add(homePopBackground, `name="rst-slide-background"`, `name="rst-slide-background-color"`) |
| 69 | add(homePopTransition, `name="rst-slide-transition"`) |
| 70 | add(homeCardSlide, `popovertarget="slides-pop-background"`, `popovertarget="slides-pop-transition"`) |
| 71 | // The slide's phone field is in the card, but only the phone views |
| 72 | // draw it (Q6, spec §3.4). |
| 73 | add(homePhoneOnly, `name="rst-slide-phone"`) |
| 74 | add(homeCardSlide, |
| 75 | `slides-confirm `, `data-command="slide-delete"`, `data-command="slide-delete-cancel"`) |
| 76 | |
| 77 | // Adding a box is the Insert menu's alone (Q5, spec §5), and the |
| 78 | // slide's boxes are listed in View → Outline (spec §4.4). |
| 79 | for _, kind := range everyKind { |
| 80 | add(homeInsertMenu, `data-insert-block="`+kind+`"`) |
| 81 | } |
| 82 | add(homeOutline, `slides-outline-list`, `slides-outline-none`) |
| 83 | |
| 84 | for _, f := range []struct{ name, group string }{ |
| 85 | {"level", "style"}, {"size", "style"}, {"style", "style"}, {"cite", "style"}, {"count", "style"}, |
| 86 | {"language", "style"}, {"kind", "style"}, {"fill", "style"}, {"stroke", "style"}, {"strokeWidth", "style"}, |
| 87 | {"label", "style"}, {"rows", "style"}, {"cols", "style"}, {"header", "style"}, {"spacer-size", "style"}, |
| 88 | {"align", "alignment"}, |
| 89 | {"radius", "style"}, // R16: Style for images and shapes alike, not the spec table's Image |
| 90 | {"image", "image"}, {"fit", "image"}, |
| 91 | {"qr-url", "qr"}, |
| 92 | {"alt", "description"}, {"decorative", "description"}, |
| 93 | {"x", "place"}, {"y", "place"}, {"w", "place"}, {"h", "place"}, |
| 94 | } { |
| 95 | add(homeCardBlock+":"+f.group, `name="rst-block-`+f.name+`"`) |
| 96 | } |
| 97 | // The box's On a phone group is in the card, but only the phone |
| 98 | // views draw it (Q6, spec §3.4). |
| 99 | for _, n := range []string{"phone-hidden", "phone-order", "phone-size"} { |
| 100 | add(homePhoneOnly, `name="rst-block-`+n+`"`) |
| 101 | } |
| 102 | for _, mark := range []string{"b", "i", "c", "h", "href"} { |
| 103 | add(homeCardBlock+":text", `data-mark="`+mark+`"`) |
| 104 | } |
| 105 | add(homePhoneOnly, `slides-phone-badge`, `data-command="phone-reset"`) |
| 106 | // A follow-along code's new audience link is its own group's. |
| 107 | add(homeCardBlock+":qr", `data-command="qr-link"`) |
| 108 | // How the box arrives is the head of the card (steps and presenter |
| 109 | // spec §4): the Appears segments, and After… with its popover. |
| 110 | add(homeCardBlock+":appears", `name="rst-block-appears"`, `popovertarget="slides-pop-after"`, `id="slides-pop-after"`) |
| 111 | // Position and size is a closed group in the card (spec §4.3), |
| 112 | // opened by its own row or by the box's menu. |
| 113 | add(homeCardBlock+":place", `<details slides-place`) |
| 114 | // A scene block's options are a group of their own (scenes and |
| 115 | // clips spec §5): its scene, From and To, its words, its data and |
| 116 | // its code; Insert places one. |
| 117 | add(homeCardBlock+":scene", `name="rst-block-scene"`, `name="rst-block-scene-from"`, `name="rst-block-scene-to"`, |
| 118 | `<fieldset slides-scene-words`, `name="slides-scene-panel"`, `name="slides-scene-data-view"`, |
| 119 | `name="slides-scene-json"`, `slides-scene-add`, `name="slides-scene-code"`, `slides-scene-preview`, `slides-scene-save`) |
| 120 | add(homeInsertMenu, `data-insert-scene`) |
| 121 | |
| 122 | // Arranging a box is the menu on the box (Q3, spec §4.2): its |
| 123 | // order, its layer, its region (a row per region, the Region |
| 124 | // field's four values), Delete, and the two rows into the card: |
| 125 | // Format (R12: the keyboard's way into the box's options) and |
| 126 | // Position and size. |
| 127 | add(homeBoxMenu, |
| 128 | `data-command="block-format"`, |
| 129 | `data-command="block-forward"`, `data-command="block-back"`, |
| 130 | `data-command="block-move-up"`, `data-command="block-move-down"`, |
| 131 | `data-command="block-place"`, `data-command="block-delete"`) |
| 132 | for _, r := range boxRegions { |
| 133 | add(homeBoxMenu, `role="menuitemradio" data-region="`+r+`"`) |
| 134 | } |
| 135 | // The same choice as the card's Appears row (steps and presenter §4). |
| 136 | for _, a := range []string{"slide", "press", "each"} { |
| 137 | add(homeBoxMenu, `role="menuitemradio" data-appears="`+a+`"`) |
| 138 | } |
| 139 | |
| 140 | // New slide's layouts, in the strip's last row (Q7, spec §6.1). The |
| 141 | // New slide button's own needle is in the Slide menu as well. |
| 142 | add(homeStrip, `slides-layouts-toggle`, `id="slides-layouts"`) |
| 143 | |
| 144 | // A slide's place in the talk (steps and presenter spec §4): two |
| 145 | // toggle rows in the Slide menu, and the same two in each strip |
| 146 | // item's menu (one pair per slide, every one in the strip). |
| 147 | for _, c := range []string{"slide-talk-full", "slide-talk-parked"} { |
| 148 | add(homeSlideMenu, `aria-pressed="false" data-command="`+c+`"`) |
| 149 | add(homeStrip, `data-slide-command="`+c+`"`) |
| 150 | } |
| 151 | // Seeing a slide's steps: the stage's presses control, outside the |
| 152 | // slide, and each strip item's count. |
| 153 | add(homeStagePane, `<button type="button" slides-presses`) |
| 154 | add(homeStrip, `<span slides-strip-presses>`, `<span slides-strip-talk>`) |
| 155 | return t |
| 156 | } |
| 157 | |
| 158 | // boxRegions are a flow slide's regions, the Region field's values in |
| 159 | // its order: one row each in the box's menu. |
| 160 | var boxRegions = []string{"heading", "body", "aside", "media"} |
| 161 | |
| 162 | // homeOf is the slice of page that home denotes, or "" when the page |
| 163 | // has no such markup (yet). To give a new home a case, add one line to |
| 164 | // the switch: find its opening tag, return extentAt of it. |
| 165 | func homeOf(page, home string) string { |
| 166 | // from returns the element whose opening tag matches re. |
| 167 | from := func(re string) string { |
| 168 | loc := regexp.MustCompile(re).FindStringIndex(page) |
| 169 | if loc == nil { |
| 170 | return "" |
| 171 | } |
| 172 | return extent(page, loc[0]) |
| 173 | } |
| 174 | form := func(section string) string { |
| 175 | card := from(`<aside slides-inspector`) |
| 176 | for _, loc := range regexp.MustCompile(`<form [^>]*slides-inspector-form[^>]*>`).FindAllStringIndex(card, -1) { |
| 177 | if strings.Contains(card[loc[0]:loc[1]], `data-section="`+section+`"`) { |
| 178 | return extent(card, loc[0]) |
| 179 | } |
| 180 | } |
| 181 | return "" |
| 182 | } |
| 183 | |
| 184 | switch { |
| 185 | case home == homeCardSlide: |
| 186 | return form("slide") |
| 187 | case strings.HasPrefix(home, homeCardBlock+":"): |
| 188 | block, group := form("block"), strings.TrimPrefix(home, homeCardBlock+":") |
| 189 | gs := tidyGroups(block) |
| 190 | if group == "" { |
| 191 | if len(gs) == 0 { |
| 192 | return "" |
| 193 | } |
| 194 | return block[gs[len(gs)-1].end:] |
| 195 | } |
| 196 | for _, g := range gs { |
| 197 | if g.name == group { |
| 198 | return block[g.start:g.end] |
| 199 | } |
| 200 | } |
| 201 | return "" |
| 202 | case home == homePopBackground: |
| 203 | return from(`<[a-z]+ [^>]*id="slides-pop-background"`) |
| 204 | case home == homePopTransition: |
| 205 | return from(`<[a-z]+ [^>]*id="slides-pop-transition"`) |
| 206 | case home == homeDeckDialog: |
| 207 | return from(`<dialog [^>]*slides-deck-dialog`) |
| 208 | case home == homeBoxMenu: |
| 209 | return from(`<div [^>]*slides-box-menu`) |
| 210 | case home == homeInsertMenu: |
| 211 | return from(`<details [^>]*slides-menu="insert"`) |
| 212 | case home == homeOutline: |
| 213 | return from(`<[a-z]+ [^>]*id="slides-outline"`) |
| 214 | case home == homeStrip: |
| 215 | return from(`<ol [^>]*slides-strip[\s>]`) |
| 216 | case home == homeSlideMenu: |
| 217 | return from(`<details [^>]*slides-menu="slide"`) |
| 218 | case home == homeStagePane: |
| 219 | return from(`<section [^>]*slides-stage-pane`) |
| 220 | case home == homePhoneOnly: |
| 221 | // The two parts the phone views alone draw: the slide's phone |
| 222 | // field, and the block form's phone group, each taken whole by |
| 223 | // the marker the CSS rule hides it by. |
| 224 | var parts []string |
| 225 | slide, block := form("slide"), form("block") |
| 226 | for _, loc := range regexp.MustCompile(`<div [^>]*\bdata-phone-only\b[^>]*>`).FindAllStringIndex(slide, -1) { |
| 227 | parts = append(parts, extent(slide, loc[0])) |
| 228 | } |
| 229 | for _, loc := range regexp.MustCompile(`<div [^>]*\bdata-group="phone"[^>]*>`).FindAllStringIndex(block, -1) { |
| 230 | parts = append(parts, extent(block, loc[0])) |
| 231 | } |
| 232 | return strings.Join(parts, "\n") |
| 233 | } |
| 234 | return "" |
| 235 | } |
| 236 | |
| 237 | // retired are needles that left the editor on purpose, each with the |
| 238 | // one control that now does its work: found exactly once in the page. |
| 239 | var retired = func() []struct{ gone, instead string } { |
| 240 | r := []struct{ gone, instead string }{ |
| 241 | {`name="rst-deck-title"`, `name="slides-head-title"`}, // Q2: the deck's title is the bar's alone |
| 242 | {`slides-blocks-buttons`, `slides-outline-list`}, // Q5: the card's Boxes list is View → Outline |
| 243 | {`slides-add-block`, `slides-menu="insert"`}, // Q5: the card's Add block is the Insert menu |
| 244 | {`name="rst-slide-layout"`, `<fieldset slides-layout-pick`}, // Q3: Flow and Canvas are the pictures |
| 245 | {`name="rst-slide-template"`, `<fieldset slides-layout-pick`}, // Q3: the Template field is the pictures |
| 246 | {`name="rst-block-region"`, `<div role="group" slides-box-region`}, // Q4: the Region field is the box menu's rows |
| 247 | {`slides-block-delete`, `<div slides-box-menu`}, // Q4: Delete block is the box menu's |
| 248 | } |
| 249 | for _, kind := range everyKind { // Q5: Add block's rows are Insert's |
| 250 | r = append(r, struct{ gone, instead string }{`data-add-block="` + kind + `"`, `data-insert-block="` + kind + `"`}) |
| 251 | } |
| 252 | return r |
| 253 | }() |
| 254 | |
| 255 | // TestQuieterNothingIsLost: every needle in homes is in its home, and |
| 256 | // nowhere else in the editor page outside <template> elements; every |
| 257 | // retired needle is gone, and what replaces it is there once. |
| 258 | func TestQuieterNothingIsLost(t *testing.T) { |
| 259 | a := newApp(t) |
| 260 | cookies := signedIn(t, a, allowedAddress) |
| 261 | page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String() |
| 262 | live := regexp.MustCompile(`(?s)<template.*?</template>`).ReplaceAllString(page, "") |
| 263 | |
| 264 | for _, n := range homes { |
| 265 | where := homeOf(live, n.home) |
| 266 | if where == "" { |
| 267 | t.Errorf("%s: its home %q has no markup in the editor page", n.needle, n.home) |
| 268 | continue |
| 269 | } |
| 270 | in, all := strings.Count(where, n.needle), strings.Count(live, n.needle) |
| 271 | if in == all && in > 0 { |
| 272 | continue |
| 273 | } |
| 274 | t.Errorf("%s: %d of its %d occurrences are in its home %q; found in: %s", |
| 275 | n.needle, in, all, n.home, foundIn(live, n.needle)) |
| 276 | } |
| 277 | for _, r := range retired { |
| 278 | if n := strings.Count(page, r.gone); n != 0 { |
| 279 | t.Errorf("%s is retired, and the editor page still has it %d times", r.gone, n) |
| 280 | } |
| 281 | if n := strings.Count(page, r.instead); n != 1 { |
| 282 | t.Errorf("%s stands in for %s and is in the editor page %d times, want once", r.instead, r.gone, n) |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // foundIn names the homes (the ones the table uses, with the block |
| 288 | // form's groups) whose markup holds needle, for a failure message. |
| 289 | func foundIn(live, needle string) string { |
| 290 | var got []string |
| 291 | for _, h := range []string{ |
| 292 | homeCardSlide, homePopBackground, homePopTransition, homeDeckDialog, |
| 293 | homeBoxMenu, homeInsertMenu, homeOutline, homeStrip, homeSlideMenu, homeStagePane, homeCardBlock + ":", |
| 294 | homeCardBlock + ":appears", homeCardBlock + ":scene", homeCardBlock + ":style", homeCardBlock + ":text", homeCardBlock + ":alignment", |
| 295 | homeCardBlock + ":image", homeCardBlock + ":clip", homeCardBlock + ":qr", homeCardBlock + ":description", homeCardBlock + ":place", |
| 296 | homeCardBlock + ":phone", homePhoneOnly, |
| 297 | } { |
| 298 | if strings.Contains(homeOf(live, h), needle) { |
| 299 | got = append(got, h) |
| 300 | } |
| 301 | } |
| 302 | if len(got) == 0 { |
| 303 | return "no home in the table" |
| 304 | } |
| 305 | return strings.Join(got, ", ") |
| 306 | } |
| 307 | |