| 1 | package render_test |
| 2 | |
| 3 | // A scene block on every surface that draws HTML (scenes and clips spec |
| 4 | // §3, §8.2, §8.3; plan call R4): the stills of the states it passes |
| 5 | // through, each the state's picture with its description as the text |
| 6 | // alternative, or the panel Slides draws in its place, and the states |
| 7 | // after `from` arriving on presses as an `each` list's items do. |
| 8 | |
| 9 | import ( |
| 10 | "regexp" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/oficina/slides/internal/slides/deck" |
| 16 | "amadan.net/oficina/slides/internal/slides/render" |
| 17 | ) |
| 18 | |
| 19 | const worldID = "worldaaaaaaa" |
| 20 | |
| 21 | // sceneDeck is a deck with one scene of three states: field and town |
| 22 | // have stills registered in the deck, city has none. Slide 0 is a canvas |
| 23 | // with the scene from field to city; slide 1 a flow slide with it from |
| 24 | // town to town; slide 2 names a scene the deck no longer has; slide 3 |
| 25 | // names states the scene no longer has. |
| 26 | func sceneDeck() *deck.Deck { |
| 27 | block := func(id, scene, from, to string) deck.Block { |
| 28 | return deck.Block{ID: id, Type: "scene", Props: map[string]any{"scene": scene, "from": from, "to": to}} |
| 29 | } |
| 30 | canvas := block("scenecanvas1", worldID, "field", "city") |
| 31 | canvas.Box = box(0.1, 0.1, 0.5, 0.5) |
| 32 | flow := block("sceneflow001", worldID, "town", "town") |
| 33 | flow.Region = "body" |
| 34 | return &deck.Deck{ |
| 35 | Meta: deck.Meta{Title: "Scenes", Theme: "paper", Aspect: "16:9"}, |
| 36 | Assets: map[string]deck.Asset{ |
| 37 | "stillfield01": {Ref: "5f1e1d01", Name: "field.png", Width: 1920, Height: 1080, Alt: "a picture"}, |
| 38 | "stilltown001": {Ref: "5f1e1d02", Name: "town.png", Width: 1920, Height: 1080}, |
| 39 | }, |
| 40 | Scenes: map[string]deck.Scene{worldID: { |
| 41 | Name: "The world", Code: "c0de", |
| 42 | States: []deck.SceneState{ |
| 43 | {ID: "field", Name: "Field", Description: "An empty field"}, |
| 44 | {ID: "town", Name: "Town", Description: "A town by a river"}, |
| 45 | {ID: "city", Name: "City", Description: "A city at night"}, |
| 46 | }, |
| 47 | Stills: map[string]deck.Still{ |
| 48 | "field": {Ref: "5f1e1d01", Made: "v1"}, |
| 49 | "town": {Ref: "5f1e1d02", Made: "v1"}, |
| 50 | // Never registered in the deck: drawn as the panel. |
| 51 | "city": {Ref: "5f1e1d09", Made: "v1"}, |
| 52 | }, |
| 53 | }}, |
| 54 | Slides: []deck.Slide{ |
| 55 | {ID: "slidecanvas1", Layout: "canvas", Blocks: []deck.Block{canvas}}, |
| 56 | {ID: "slideflow001", Layout: "flow", Template: "title-body", Blocks: []deck.Block{flow}}, |
| 57 | {ID: "slidegone001", Layout: "flow", Template: "blank", Blocks: []deck.Block{block("scenegone001", "goneaaaaaaaa", "field", "city")}}, |
| 58 | {ID: "slidestate01", Layout: "flow", Template: "blank", Blocks: []deck.Block{block("scenestate01", worldID, "dawn", "dusk")}}, |
| 59 | }, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // sceneStates is the scene block's children in order: for each, its |
| 64 | // state id, whether it is a picture (img) or the panel (div), and its |
| 65 | // attributes. |
| 66 | type sceneChild struct { |
| 67 | state, tag, attrs, body string |
| 68 | } |
| 69 | |
| 70 | var sceneChildPattern = regexp.MustCompile(`(?s)<img ([^>]*data-scene-state="([^"]*)"[^>]*)>|<div ([^>]*data-scene-state="([^"]*)"[^>]*)>(.*?)</div>`) |
| 71 | |
| 72 | func sceneChildren(t *testing.T, html, block string) []sceneChild { |
| 73 | t.Helper() |
| 74 | start := strings.Index(html, `data-id="`+block+`"`) |
| 75 | if start < 0 { |
| 76 | t.Fatalf("no block %s in:\n%s", block, html) |
| 77 | } |
| 78 | open := strings.LastIndex(html[:start], "<div") |
| 79 | // Each slide here has the one block, so it runs to the section's end. |
| 80 | end := strings.Index(html[open:], "</section>") |
| 81 | if end < 0 { |
| 82 | t.Fatalf("block %s is not closed:\n%s", block, html) |
| 83 | } |
| 84 | var out []sceneChild |
| 85 | for _, m := range sceneChildPattern.FindAllStringSubmatch(html[open:open+end], -1) { |
| 86 | if m[1] != "" { |
| 87 | out = append(out, sceneChild{state: m[2], tag: "img", attrs: m[1]}) |
| 88 | } else { |
| 89 | out = append(out, sceneChild{state: m[4], tag: "div", attrs: m[3], body: m[5]}) |
| 90 | } |
| 91 | } |
| 92 | return out |
| 93 | } |
| 94 | |
| 95 | func TestASceneBlockDrawsItsStills(t *testing.T) { |
| 96 | d := sceneDeck() |
| 97 | html, err := render.SlideWith(d, 0, render.Desktop, render.Options{AssetBase: "/d/x/assets"}) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | out := string(html) |
| 102 | for _, want := range []string{`data-block="scene"`, `data-scene="` + worldID + `"`, `left:10%;top:10%;width:50%;height:50%`} { |
| 103 | if !strings.Contains(out, want) { |
| 104 | t.Errorf("the scene block lacks %s:\n%s", want, out) |
| 105 | } |
| 106 | } |
| 107 | kids := sceneChildren(t, out, "scenecanvas1") |
| 108 | var states []string |
| 109 | for _, k := range kids { |
| 110 | states = append(states, k.state) |
| 111 | } |
| 112 | if strings.Join(states, " ") != "field town city" { |
| 113 | t.Fatalf("the block draws states %v, want field town city", states) |
| 114 | } |
| 115 | // A still is the picture under this surface's base, its alt the |
| 116 | // state's description (not the asset's own alt). |
| 117 | if k := kids[0]; k.tag != "img" || !strings.Contains(k.attrs, `src="/d/x/assets/5f1e1d01"`) || |
| 118 | !strings.Contains(k.attrs, `alt="An empty field"`) || !strings.Contains(k.attrs, `width="1920" height="1080"`) { |
| 119 | t.Errorf("the field state is not its still: %+v", k) |
| 120 | } |
| 121 | if k := kids[1]; k.tag != "img" || !strings.Contains(k.attrs, `src="/d/x/assets/5f1e1d02"`) || !strings.Contains(k.attrs, `alt="A town by a river"`) { |
| 122 | t.Errorf("the town state is not its still: %+v", k) |
| 123 | } |
| 124 | // A still the deck never registered is no picture: the panel, with |
| 125 | // the scene's name and the state's description as its words. |
| 126 | if k := kids[2]; k.tag != "div" || !strings.Contains(k.attrs, "data-scene-placeholder") || |
| 127 | !strings.Contains(k.body, "The world") || !strings.Contains(k.body, "A city at night") || strings.Contains(out, "5f1e1d09") { |
| 128 | t.Errorf("the city state is not the panel: %+v", k) |
| 129 | } |
| 130 | // The finished slide: nothing waits. |
| 131 | if strings.Contains(out, "data-press") || strings.Contains(out, "data-waiting") { |
| 132 | t.Errorf("the finished slide marks presses:\n%s", out) |
| 133 | } |
| 134 | |
| 135 | // On a flow slide, in its region, the one state it shows. |
| 136 | flow, _ := render.SlideWith(d, 1, render.Desktop, render.Options{AssetBase: "/d/x/assets"}) |
| 137 | if kids := sceneChildren(t, string(flow), "sceneflow001"); len(kids) != 1 || kids[0].state != "town" || kids[0].tag != "img" { |
| 138 | t.Errorf("from town to town draws %+v, want the town still alone", kids) |
| 139 | } |
| 140 | if !strings.Contains(string(flow), `data-region="body"`) { |
| 141 | t.Errorf("the flow scene block is in no region:\n%s", flow) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestASceneBlockArrivesPressByPress(t *testing.T) { |
| 146 | d := sceneDeck() |
| 147 | for at, want := range map[int]string{ |
| 148 | 0: `town:press=1,waiting city:press=2,waiting`, |
| 149 | 1: `town:press=1 city:press=2,waiting`, |
| 150 | 2: `town:press=1 city:press=2`, |
| 151 | } { |
| 152 | html, err := render.SlideWith(d, 0, render.Desktop, render.Options{AssetBase: "/d/x/assets", Reveal: &at}) |
| 153 | if err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | var got []string |
| 157 | for _, k := range sceneChildren(t, string(html), "scenecanvas1") { |
| 158 | var marks []string |
| 159 | if m := regexp.MustCompile(`data-press="(\d+)"`).FindStringSubmatch(k.attrs); m != nil { |
| 160 | marks = append(marks, "press="+m[1]) |
| 161 | } |
| 162 | if strings.Contains(k.attrs, "data-waiting") { |
| 163 | marks = append(marks, "waiting") |
| 164 | } |
| 165 | if len(marks) > 0 { |
| 166 | got = append(got, k.state+":"+strings.Join(marks, ",")) |
| 167 | } |
| 168 | } |
| 169 | if strings.Join(got, " ") != want { |
| 170 | t.Errorf("at press %d: %v, want %s", at, got, want) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // TestARemovedSceneLeavesAPlaceholder (Review Focus 3): a block naming |
| 176 | // a scene the deck no longer has draws an empty panel and makes no |
| 177 | // press; one naming states the scene no longer has draws the panel |
| 178 | // with the scene's name. |
| 179 | func TestARemovedSceneLeavesAPlaceholder(t *testing.T) { |
| 180 | d := sceneDeck() |
| 181 | for _, c := range []struct { |
| 182 | slide int |
| 183 | block string |
| 184 | want []string |
| 185 | not []string |
| 186 | }{ |
| 187 | {2, "scenegone001", nil, []string{"The world", "<img"}}, |
| 188 | {3, "scenestate01", []string{"The world"}, []string{"<img", "An empty field"}}, |
| 189 | } { |
| 190 | at := 0 |
| 191 | html, err := render.SlideWith(d, c.slide, render.Desktop, render.Options{AssetBase: "/d/x/assets", Reveal: &at}) |
| 192 | if err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | out := string(html) |
| 196 | kids := sceneChildren(t, out, c.block) |
| 197 | if len(kids) != 1 || kids[0].tag != "div" || !strings.Contains(kids[0].attrs, "data-scene-placeholder") { |
| 198 | t.Errorf("%s draws %+v, want one panel", c.block, kids) |
| 199 | } |
| 200 | for _, w := range c.want { |
| 201 | if !strings.Contains(out, w) { |
| 202 | t.Errorf("%s lacks %q:\n%s", c.block, w, out) |
| 203 | } |
| 204 | } |
| 205 | for _, n := range c.not { |
| 206 | if strings.Contains(out, n) { |
| 207 | t.Errorf("%s has %q:\n%s", c.block, n, out) |
| 208 | } |
| 209 | } |
| 210 | if strings.Contains(out, "data-press") { |
| 211 | t.Errorf("%s makes a press:\n%s", c.block, out) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // TestTheStillsArePictures: a window holding the talk fetches every |
| 217 | // still a slide may show, as it fetches its pictures; a still nobody |
| 218 | // registered is no address at all. |
| 219 | func TestTheStillsArePictures(t *testing.T) { |
| 220 | d := sceneDeck() |
| 221 | got := render.AssetPaths(d, 0, "/p/s/assets") |
| 222 | want := []string{"/p/s/assets/5f1e1d01", "/p/s/assets/5f1e1d02"} |
| 223 | if !slices.Equal(got, want) { |
| 224 | t.Errorf("AssetPaths = %v, want %v", got, want) |
| 225 | } |
| 226 | if got := render.AssetPaths(d, 2, "/p/s/assets"); len(got) != 0 { |
| 227 | t.Errorf("a removed scene's slide fetches %v", got) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // TestExistingDecksRenderUnchanged: a deck that shows no scene renders |
| 232 | // byte for byte as it did before scenes, whatever its scenes map holds: |
| 233 | // every golden slide, both viewports, at rest and at a press, with the |
| 234 | // fixture deck given a scene no slide shows. |
| 235 | func TestExistingDecksRenderUnchanged(t *testing.T) { |
| 236 | plain := fixture() |
| 237 | with := fixture() |
| 238 | with.Scenes = sceneDeck().Scenes |
| 239 | for i := range plain.Slides { |
| 240 | for _, v := range viewports { |
| 241 | for _, at := range []*int{nil, new(int)} { |
| 242 | a, err := render.SlideWith(plain, i, v.vp, render.Options{AssetBase: "/d/x/assets", Reveal: at}) |
| 243 | if err != nil { |
| 244 | t.Fatal(err) |
| 245 | } |
| 246 | b, err := render.SlideWith(with, i, v.vp, render.Options{AssetBase: "/d/x/assets", Reveal: at}) |
| 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | if a != b { |
| 251 | t.Errorf("slide %d (%s) changed with a scene in the deck", i, v.name) |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | if a, b := render.AssetPaths(plain, i, "/x"), render.AssetPaths(with, i, "/x"); !slices.Equal(a, b) { |
| 256 | t.Errorf("slide %d's pictures changed: %v, then %v", i, a, b) |
| 257 | } |
| 258 | } |
| 259 | // And the goldens hold (TestSlideGoldens is the byte-for-byte half). |
| 260 | for _, s := range plain.Slides { |
| 261 | for _, b := range s.Blocks { |
| 262 | if b.Type == "scene" { |
| 263 | t.Fatalf("the golden fixture shows a scene; this test is about decks that do not") |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |