| 1 | package slidestest |
| 2 | |
| 3 | // Stills where code doesn't run (scenes and clips spec §3, §8.2, §8.7; |
| 4 | // plan call R5): the scriptless viewer and its phone view draw a scene |
| 5 | // block as its stills, a thumbnail and the PDF as its `to` state, and |
| 6 | // the pages that run scenes carry what the frame layer needs (the |
| 7 | // bundle's address, the words, the data) and fetch the stills as |
| 8 | // pictures. |
| 9 | |
| 10 | import ( |
| 11 | "encoding/json" |
| 12 | "fmt" |
| 13 | "net/http" |
| 14 | "net/url" |
| 15 | "os" |
| 16 | "regexp" |
| 17 | "strings" |
| 18 | "testing" |
| 19 | |
| 20 | "amadan.net/oficina/slides/internal/slides/deck" |
| 21 | "amadan.net/oficina/slides/internal/slides/layout" |
| 22 | ) |
| 23 | |
| 24 | // sceneData is docs/scenes/example.html's data for the three states of |
| 25 | // sceneDeck's scene. |
| 26 | const sceneData = `{"states":[{"id":"one","colour":"#c0392b"},{"id":"two","colour":"#2471a3"},{"id":"three","colour":"#1e8449"}]}` |
| 27 | |
| 28 | // sceneDeck is a deck for scene tests, built over the API: |
| 29 | // |
| 30 | // - the scene Boxes (code bundle; states one, two, three; a still for |
| 31 | // one and two, none for three) and the scene Silent (code silent, |
| 32 | // one state with a still), when silent is not nil; |
| 33 | // - slide carry1: Boxes in box A from one to two (one press); |
| 34 | // - slide carry2: Boxes in box A from two to three (one press); |
| 35 | // - slide fresh3: Boxes in box B at one; |
| 36 | // - slide silent4: Silent in box A (only when silent is not nil); |
| 37 | // - slide plain5: a heading; |
| 38 | // - slide covered6: Boxes in box A at one, a caption above it (z 1) |
| 39 | // over its middle; |
| 40 | // - slide delayed7: Boxes at one in a columns box that arrives on |
| 41 | // press 1; |
| 42 | // - slide short8: Boxes at one, alone on a blank flow slide, whose |
| 43 | // region is shorter than a 16:9 box at its width; |
| 44 | // - slide full9: image-full, its heading over Boxes at one in the |
| 45 | // media region (a scene run inside its block), a line of text under |
| 46 | // it taking some of the region's height. |
| 47 | type sceneFixture struct { |
| 48 | sqid, boxes, silent, still string |
| 49 | code, silentCode string |
| 50 | } |
| 51 | |
| 52 | var boxA = `{"x":0.1,"y":0.1,"w":0.6,"h":0.6}` |
| 53 | |
| 54 | func sceneDeckOver(t *testing.T, a *testApp, cookies []*http.Cookie, token string, bundle, silent []byte) sceneFixture { |
| 55 | t.Helper() |
| 56 | f := sceneFixture{sqid: apiDeck(t, a, token, "Scenes")} |
| 57 | deckPath := "/d/" + f.sqid |
| 58 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+f.sqid, token, "", http.StatusOK) |
| 59 | var ops []string |
| 60 | for _, s := range view["slides"].([]any) { |
| 61 | ops = append(ops, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`) |
| 62 | } |
| 63 | record := upload(t, a, deckPath, cookies, "still.png", "image/png", onePixelPNG, "A still") |
| 64 | registerInDeck(t, a, token, f.sqid, record) |
| 65 | f.still = record["ref"].(string) |
| 66 | f.code = uploadScene(t, a, deckPath, cookies, bundle) |
| 67 | ops = append(ops, `{"op":"add_scene","name":"Boxes","code":"`+f.code+`","states":[`+ |
| 68 | `{"id":"one","name":"One","description":"One box"},{"id":"two","name":"Two","description":"Two boxes"},`+ |
| 69 | `{"id":"three","name":"Three","description":"Three boxes"}],`+ |
| 70 | `"words":{"one":"First","two":"Second","three":"Third"},"data":`+sceneData+`}`) |
| 71 | if silent != nil { |
| 72 | f.silentCode = uploadScene(t, a, deckPath, cookies, silent) |
| 73 | ops = append(ops, `{"op":"add_scene","name":"Silent","code":"`+f.silentCode+`","states":[`+ |
| 74 | `{"id":"one","name":"One","description":"A quiet box"}]}`) |
| 75 | } |
| 76 | out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+f.sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 77 | ids, _ := out["ids"].(map[string]any) |
| 78 | n := len(ops) - 1 |
| 79 | if silent != nil { |
| 80 | n-- |
| 81 | } |
| 82 | f.boxes, _ = ids[fmt.Sprint(n)].(string) |
| 83 | if silent != nil { |
| 84 | f.silent, _ = ids[fmt.Sprint(n+1)].(string) |
| 85 | } |
| 86 | if f.boxes == "" || (silent != nil && f.silent == "") { |
| 87 | t.Fatalf("add_scene minted no ids: %v", out) |
| 88 | } |
| 89 | made := strings.Repeat("0", 64) |
| 90 | stills := `{"one":{"ref":"` + f.still + `","made":"` + made + `"},"two":{"ref":"` + f.still + `","made":"` + made + `"}}` |
| 91 | ops = []string{`{"op":"set_scene","id":"` + f.boxes + `","stills":` + stills + `}`} |
| 92 | if silent != nil { |
| 93 | ops = append(ops, `{"op":"set_scene","id":"`+f.silent+`","stills":{"one":{"ref":"`+f.still+`","made":"`+made+`"}}}`) |
| 94 | } |
| 95 | scene := func(slide, block, scene, box, from, to string) []string { |
| 96 | return []string{ |
| 97 | `{"op":"add_slide","id":"` + slide + `","layout":"canvas"}`, |
| 98 | `{"op":"add_block","slide":"` + slide + `","id":"` + block + `","type":"scene","box":` + box + |
| 99 | `,"props":{"scene":"` + scene + `","from":"` + from + `","to":"` + to + `"}}`, |
| 100 | } |
| 101 | } |
| 102 | ops = append(ops, scene("carry1", "scenea", f.boxes, boxA, "one", "two")...) |
| 103 | ops = append(ops, scene("carry2", "sceneb", f.boxes, boxA, "two", "three")...) |
| 104 | ops = append(ops, scene("fresh3", "scenec", f.boxes, `{"x":0.3,"y":0.2,"w":0.5,"h":0.5}`, "one", "one")...) |
| 105 | if silent != nil { |
| 106 | ops = append(ops, scene("silent4", "scened", f.silent, boxA, "one", "one")...) |
| 107 | } |
| 108 | ops = append(ops, `{"op":"add_slide","id":"plain5","layout":"flow","template":"title"}`, |
| 109 | `{"op":"add_block","slide":"plain5","id":"plainhead","type":"heading","region":"heading","props":{"level":1},"markdown":"Plain"}`) |
| 110 | ops = append(ops, scene("covered6", "scenee", f.boxes, boxA, "one", "one")...) |
| 111 | ops = append(ops, |
| 112 | `{"op":"add_block","slide":"covered6","id":"caption","type":"shape","box":{"x":0.25,"y":0.3,"w":0.3,"h":0.15},"props":{"kind":"rect","fill":"#ffffff"},"markdown":"A caption"}`, |
| 113 | `{"op":"set_block","slide":"covered6","id":"caption","z":1}`, |
| 114 | `{"op":"add_slide","id":"delayed7","layout":"flow","template":"blank"}`, |
| 115 | `{"op":"add_block","slide":"delayed7","id":"scenef","type":"scene","region":"body","props":{"scene":"`+f.boxes+`","from":"one","to":"one"}}`, |
| 116 | `{"op":"add_block","slide":"delayed7","id":"later","type":"columns","region":"body","props":{"count":2,"children":["scenef"]}}`, |
| 117 | `{"op":"set_block","slide":"delayed7","id":"later","step":1}`, |
| 118 | `{"op":"add_slide","id":"short8","layout":"flow","template":"blank"}`, |
| 119 | `{"op":"add_block","slide":"short8","id":"sceneg","type":"scene","region":"body","props":{"scene":"`+f.boxes+`","from":"one","to":"one"}}`, |
| 120 | `{"op":"add_slide","id":"full9","layout":"flow","template":"image-full"}`, |
| 121 | `{"op":"add_block","slide":"full9","id":"fullhead","type":"heading","region":"heading","props":{"level":1},"markdown":"Over the scene"}`, |
| 122 | `{"op":"add_block","slide":"full9","id":"sceneh","type":"scene","region":"media","props":{"scene":"`+f.boxes+`","from":"one","to":"one"}}`, |
| 123 | `{"op":"add_block","slide":"full9","id":"fullnote","type":"text","region":"media","markdown":"Under the scene"}`) |
| 124 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+f.sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 125 | return f |
| 126 | } |
| 127 | |
| 128 | // TestAShortFlowRegionFitsTheScene: in a flow region too short for a |
| 129 | // 16:9 box at its width, the scene is the largest 16:9 box that fits, |
| 130 | // centred, in a picture as on the page (the page's half is |
| 131 | // TestASceneFitsAShortRegionOnThePage). |
| 132 | func TestAShortFlowRegionFitsTheScene(t *testing.T) { |
| 133 | a := newAppWithStore(t, dirStore(t)) |
| 134 | cookies := signedIn(t, a, allowedAddress) |
| 135 | token := mintAgent(t, a, cookies, "scenes") |
| 136 | f := sceneDeckOver(t, a, cookies, token, exampleScene(t), nil) |
| 137 | // short8's scene runs over the slide; full9's, under image-full's |
| 138 | // heading, inside its block: both are pictured the same way, the |
| 139 | // largest 16:9 box in what the region leaves the block, centred. |
| 140 | // short8's region is the frame inside its 120px margin, 1680 by 840, |
| 141 | // so its box is 840 tall; full9's media is the whole frame, less the |
| 142 | // line under the scene. |
| 143 | for _, c := range []struct { |
| 144 | slide string |
| 145 | col, minH float64 |
| 146 | maxH float64 |
| 147 | }{{"short8", 1680, 839, 841}, {"full9", 1920, 600, 1079}} { |
| 148 | slide := c.slide |
| 149 | thumb := mustGet(t, a, "/d/"+f.sqid+"/slides/"+slide+".svg", http.StatusOK, cookies).Body.String() |
| 150 | m := regexp.MustCompile(`<image[^>]*? x="([\d.]+)" y="([\d.]+)" width="([\d.]+)" height="([\d.]+)"`).FindStringSubmatch(thumb) |
| 151 | if m == nil { |
| 152 | t.Fatalf("%s's thumbnail draws no still:\n%s", slide, thumb) |
| 153 | } |
| 154 | var x, y, w, h float64 |
| 155 | fmt.Sscan(m[1]+" "+m[2]+" "+m[3]+" "+m[4], &x, &y, &w, &h) |
| 156 | if r := h / w; r < 0.5624 || r > 0.5626 { |
| 157 | t.Errorf("%s: the still is %vx%v, want 16:9", slide, w, h) |
| 158 | } |
| 159 | if w >= c.col || h < c.minH || h > c.maxH || x+w/2 < 959 || x+w/2 > 961 { |
| 160 | t.Errorf("%s: the still is at %v,%v %vx%v, want the largest 16:9 box in the region, centred", slide, x, y, w, h) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func exampleScene(t *testing.T) []byte { |
| 166 | t.Helper() |
| 167 | b, err := os.ReadFile("../../docs/scenes/example.html") |
| 168 | if err != nil { |
| 169 | t.Fatalf("read the example scene: %v", err) |
| 170 | } |
| 171 | return b |
| 172 | } |
| 173 | |
| 174 | // pageScenes is what a page's <script slides-scenes> says: scene id to |
| 175 | // {src, name, words, data}. |
| 176 | func pageScenes(t *testing.T, page string) map[string]map[string]any { |
| 177 | t.Helper() |
| 178 | m := regexp.MustCompile(`(?s)<script type="application/json" slides-scenes>(.*?)</script>`).FindStringSubmatch(page) |
| 179 | if m == nil { |
| 180 | return nil |
| 181 | } |
| 182 | var out map[string]map[string]any |
| 183 | if err := json.Unmarshal([]byte(m[1]), &out); err != nil { |
| 184 | t.Fatalf("the page's scenes are not JSON: %v\n%s", err, m[1]) |
| 185 | } |
| 186 | return out |
| 187 | } |
| 188 | |
| 189 | func TestStillsWhereCodeDoesntRun(t *testing.T) { |
| 190 | a := newAppWithStore(t, dirStore(t)) |
| 191 | cookies := signedIn(t, a, allowedAddress) |
| 192 | token := mintAgent(t, a, cookies, "scenes") |
| 193 | f := sceneDeckOver(t, a, cookies, token, exampleScene(t), nil) |
| 194 | deckPath := "/d/" + f.sqid |
| 195 | stillAt := func(base string) string { return base + "/assets/" + f.still } |
| 196 | |
| 197 | // Scripts off, desktop and phone: the stills, each with its |
| 198 | // description, the latest that has arrived the one drawn (all of |
| 199 | // them, on a finished slide); a state with no still is the panel. |
| 200 | for _, vp := range []string{"", "&vp=phone"} { |
| 201 | page := mustGet(t, a, deckPath+"?slide=1"+vp, http.StatusOK, cookies).Body.String() |
| 202 | section := slideSection(t, page) |
| 203 | for _, want := range []string{ |
| 204 | `<img data-scene-state="one" src="` + stillAt(deckPath) + `" alt="One box"`, |
| 205 | `<img data-scene-state="two" src="` + stillAt(deckPath) + `" alt="Two boxes"`, |
| 206 | } { |
| 207 | if !strings.Contains(section, want) { |
| 208 | t.Errorf("viewer%s: the slide lacks %s:\n%s", vp, want, section) |
| 209 | } |
| 210 | } |
| 211 | page2 := mustGet(t, a, deckPath+"?slide=2"+vp, http.StatusOK, cookies).Body.String() |
| 212 | if s := slideSection(t, page2); !strings.Contains(s, `data-scene-state="three" data-scene-placeholder`) || !strings.Contains(s, "Three boxes") || !strings.Contains(s, "Boxes") { |
| 213 | t.Errorf("viewer%s: the state with no still is not the panel:\n%s", vp, s) |
| 214 | } |
| 215 | // What the frame layer runs: the bundle under this surface, the |
| 216 | // words and the data, for the scenes this slide shows. |
| 217 | sc := pageScenes(t, page) |
| 218 | if sc == nil || sc[f.boxes] == nil { |
| 219 | t.Fatalf("viewer%s: the page carries no scenes", vp) |
| 220 | } |
| 221 | if got, want := sc[f.boxes]["src"], deckPath+"/scenes/"+f.boxes+"/"+f.code; got != want { |
| 222 | t.Errorf("viewer%s: the bundle is at %v, want %s", vp, got, want) |
| 223 | } |
| 224 | if w, _ := sc[f.boxes]["words"].(map[string]any); w["two"] != "Second" { |
| 225 | t.Errorf("viewer%s: the scene's words are %v", vp, sc[f.boxes]["words"]) |
| 226 | } |
| 227 | if d, _ := sc[f.boxes]["data"].(map[string]any); d["states"] == nil { |
| 228 | t.Errorf("viewer%s: the scene's data is %v", vp, sc[f.boxes]["data"]) |
| 229 | } |
| 230 | } |
| 231 | // The last slide shows no scene, and its page carries none. |
| 232 | if sc := pageScenes(t, mustGet(t, a, deckPath+"?slide=4", http.StatusOK, cookies).Body.String()); sc != nil { |
| 233 | t.Errorf("a slide with no scene carries scenes: %v", sc) |
| 234 | } |
| 235 | |
| 236 | // A thumbnail is the slide fully built: the still of `to`, inlined. |
| 237 | thumb := mustGet(t, a, deckPath+"/slides/carry1.svg", http.StatusOK, cookies).Body.String() |
| 238 | if !strings.Contains(thumb, "<image") || !strings.Contains(thumb, "data:image/png;base64,") { |
| 239 | t.Errorf("the thumbnail draws no still:\n%s", thumb) |
| 240 | } |
| 241 | thumb2 := mustGet(t, a, deckPath+"/slides/carry2.svg", http.StatusOK, cookies).Body.String() |
| 242 | if strings.Contains(thumb2, "<image") || !strings.Contains(thumb2, "Three boxes") { |
| 243 | t.Errorf("a thumbnail at a state with no still is not the panel:\n%s", thumb2) |
| 244 | } |
| 245 | |
| 246 | // The PDF is drawn from the same paint list: the `to` state. |
| 247 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+f.sqid, token, "", http.StatusOK) |
| 248 | raw, _ := json.Marshal(view) |
| 249 | var d deck.Deck |
| 250 | if err := json.Unmarshal(raw, &d); err != nil { |
| 251 | t.Fatal(err) |
| 252 | } |
| 253 | p, _, err := layout.Layout(&d, 0) |
| 254 | if err != nil { |
| 255 | t.Fatal(err) |
| 256 | } |
| 257 | var pictured []string |
| 258 | for _, op := range p.Ops { |
| 259 | if im, ok := op.(layout.Image); ok { |
| 260 | pictured = append(pictured, im.Alt) |
| 261 | } |
| 262 | } |
| 263 | if strings.Join(pictured, "|") != "Two boxes" { |
| 264 | t.Errorf("the PDF's carry1 pictures %v, want the to state's still alone", pictured) |
| 265 | } |
| 266 | |
| 267 | // The presenting windows hold the talk: each slide's stills among its |
| 268 | // pictures, and the scenes it shows with their bundle's address. |
| 269 | session, remote := startPresenting(t, a, deckPath, cookies) |
| 270 | talk := mustGet(t, a, "/p/"+session+"/talk?t="+url.QueryEscape(remote), http.StatusOK, nil).Body.Bytes() |
| 271 | var payload struct { |
| 272 | Slides []struct { |
| 273 | ID string `json:"id"` |
| 274 | Assets []string `json:"assets"` |
| 275 | } `json:"slides"` |
| 276 | Scenes map[string]map[string]any `json:"scenes"` |
| 277 | } |
| 278 | if err := json.Unmarshal(talk, &payload); err != nil { |
| 279 | t.Fatal(err) |
| 280 | } |
| 281 | if len(payload.Slides) == 0 || strings.Join(payload.Slides[0].Assets, " ") != stillAt("/p/"+session) { |
| 282 | t.Errorf("the talk's first slide fetches %v, want its still", payload.Slides) |
| 283 | } |
| 284 | if got, want := payload.Scenes[f.boxes]["src"], "/p/"+session+"/scenes/"+f.boxes+"/"+f.code; got != want { |
| 285 | t.Errorf("the talk's scene is at %v, want %s", got, want) |
| 286 | } |
| 287 | |
| 288 | // The follow link carries the scenes too, under the share link. |
| 289 | share := "/s/" + mintShareLink(t, a, deckPath, cookies) |
| 290 | follow := mustGet(t, a, share+"?follow=1", http.StatusOK, nil).Body.String() |
| 291 | if sc := pageScenes(t, follow); sc == nil || sc[f.boxes]["src"] != share+"/scenes/"+f.boxes+"/"+f.code { |
| 292 | t.Errorf("the follow view's scenes are %v", sc) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // TestADeckWithNoSceneCarriesNone: the pages and the talk of a deck that |
| 297 | // shows no scene are what they were before scenes. |
| 298 | func TestADeckWithNoSceneCarriesNone(t *testing.T) { |
| 299 | a := newApp(t) |
| 300 | cookies := signedIn(t, a, allowedAddress) |
| 301 | deckPath := sampleDeck(t, a, cookies) |
| 302 | page := mustGet(t, a, deckPath+"?slide=1", http.StatusOK, cookies).Body.String() |
| 303 | if strings.Contains(page, "slides-scenes") { |
| 304 | t.Errorf("the viewer of a deck with no scene carries scenes") |
| 305 | } |
| 306 | session, remote := startPresenting(t, a, deckPath, cookies) |
| 307 | talk := mustGet(t, a, "/p/"+session+"/talk?t="+url.QueryEscape(remote), http.StatusOK, nil).Body.String() |
| 308 | if strings.Contains(talk, `"scenes"`) { |
| 309 | t.Errorf("the talk of a deck with no scene carries scenes") |
| 310 | } |
| 311 | } |
| 312 | |