| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | // The final review's round on scenes (scenes and clips spec §5, §8): |
| 6 | // a scene edit is one undo step, data that would encode past what one |
| 7 | // live update may carry is refused before it is written, and two |
| 8 | // blocks of one scene on a slide each run. |
| 9 | |
| 10 | import ( |
| 11 | "context" |
| 12 | "encoding/json" |
| 13 | "net/http" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "amadan.net/rastrillo/rastrillo/blobs" |
| 19 | "github.com/chromedp/chromedp" |
| 20 | "github.com/chromedp/chromedp/kb" |
| 21 | |
| 22 | "amadan.net/oficina/slides/internal/slides/deck" |
| 23 | ) |
| 24 | |
| 25 | // TestASceneWordEditIsUndone: a word written from the scene card is |
| 26 | // taken back by one click on Undo, in the deck and in its field. |
| 27 | func TestASceneWordEditIsUndone(t *testing.T) { |
| 28 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 29 | se.selectScene(t) |
| 30 | title := `[slides-scene-words] input[data-word="title"]` |
| 31 | se.typeInto(title, "Wien") |
| 32 | se.leave() |
| 33 | se.untilScene(t, se.world, "titled Wien", func(s deck.Scene) bool { return s.Words["title"] == "Wien" }) |
| 34 | se.rig.Run(chromedp.Click(chromedp.CSS(`[slides-press][data-command="undo"]`))) |
| 35 | se.untilScene(t, se.world, "titled Vienna again", func(s deck.Scene) bool { return s.Words["title"] == "Vienna" && s.Words["sign"] == "Open" }) |
| 36 | se.rig.Run(waitFor(`document.querySelector('` + title + `').value === 'Vienna'`)) |
| 37 | } |
| 38 | |
| 39 | // tenths is a JSON list of n copies of 0.1: four bytes each in JSON, |
| 40 | // nine in a Yjs update. |
| 41 | func tenths(n int) string { return "[" + strings.TrimSuffix(strings.Repeat("0.1,", n), ",") + "]" } |
| 42 | |
| 43 | // TestSceneDataPastOneUpdateIsRefused (final review call 3): 150,000 |
| 44 | // tenths are 600,001 bytes of JSON, within the data limit, but their |
| 45 | // set_scene would be a 1.35 MB update, past the 1 MiB the server takes. |
| 46 | // The editor refuses it before writing anything, in the data limit's |
| 47 | // words, and keeps it in its field; 116,000 of them, just under one |
| 48 | // update, are written. |
| 49 | func TestSceneDataPastOneUpdateIsRefused(t *testing.T) { |
| 50 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 51 | se.selectScene(t) |
| 52 | json := `textarea[name="slides-scene-json"]` |
| 53 | se.rig.Run(clickLabelOf(`input[name="slides-scene-data-view"][value="json"]`), waitFor(`document.querySelector('`+json+`')?.checkVisibility()`)) |
| 54 | put := func(text string) { |
| 55 | se.rig.Run(chromedp.Evaluate[chromedp.Void](`(a => { a.focus(); a.value = ` + jsString(text) + `; a.dispatchEvent(new Event('input', { bubbles: true })); a.dispatchEvent(new Event('change', { bubbles: true })) })(document.querySelector('` + json + `'))`)) |
| 56 | } |
| 57 | over := tenths(150000) |
| 58 | put(over) |
| 59 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-data-big]')?.checkVisibility()`)) |
| 60 | var state struct { |
| 61 | Length int |
| 62 | Invalid string |
| 63 | Words string |
| 64 | } |
| 65 | se.rig.Run(evalInto(&state, `({ Length: document.querySelector('`+json+`').value.length, Invalid: document.querySelector('`+json+`').getAttribute('aria-invalid') || '', |
| 66 | Words: document.querySelector('[slides-scene-data-big]').textContent.trim() })`)) |
| 67 | if state.Length != len(over) || state.Invalid != "true" || state.Words != "This data is over 960 KB, so it wasn't saved." { |
| 68 | t.Errorf("past one update: the field holds %d characters, aria-invalid %q, saying %q; want the edit kept, invalid, and words", state.Length, state.Invalid, state.Words) |
| 69 | } |
| 70 | if p := pieces(se.scene(t, se.world).Data); len(p) != 2 { |
| 71 | t.Errorf("data past one update was written: %v", se.scene(t, se.world).Data) |
| 72 | } |
| 73 | |
| 74 | put(tenths(116000)) |
| 75 | se.untilScene(t, se.world, "116,000 tenths", func(s deck.Scene) bool { |
| 76 | l, _ := s.Data.([]any) |
| 77 | return len(l) == 116000 |
| 78 | }) |
| 79 | se.rig.Run(waitFor(`!document.querySelector('[slides-scene-data-big]').checkVisibility() && document.querySelector('` + json + `').getAttribute('aria-invalid') !== 'true'`)) |
| 80 | } |
| 81 | |
| 82 | // placementsJS: every frame in the scene layer, by the block it is over, |
| 83 | // with what its document was last told. |
| 84 | const placementsJS = `[...document.querySelectorAll('[slides-scene-layer] iframe')].map((f) => { |
| 85 | const log = (window.__scenes || {})[f.__doc] || []; |
| 86 | const last = log[log.length - 1] || {}; |
| 87 | return {For: f.getAttribute('data-for') || '', Doc: f.__doc || '', Mark: !!f.__mark, |
| 88 | Visible: getComputedStyle(f).opacity === '1', Inits: log.filter((m) => m.type === 'init').length, |
| 89 | Type: last.type || '', State: last.state || ''}; |
| 90 | })` |
| 91 | |
| 92 | type placement struct { |
| 93 | For, Doc, Type, State string |
| 94 | Mark, Visible bool |
| 95 | Inits int |
| 96 | } |
| 97 | |
| 98 | // TestTwoBlocksOfOneSceneEachRun (final review call 5): a slide with two |
| 99 | // blocks of one scene runs a frame over each, live. On the next slide |
| 100 | // the first block of that scene carries on from the first one (the same |
| 101 | // box: the same frame, told its new state), and the second, in another |
| 102 | // box, starts fresh. |
| 103 | func TestTwoBlocksOfOneSceneEachRun(t *testing.T) { |
| 104 | const scene = "twinsceneaaa" |
| 105 | block := func(slide, id, box, state string) string { |
| 106 | return `{"op":"add_block","slide":"` + slide + `","id":"` + id + `","type":"scene","box":` + box + |
| 107 | `,"props":{"scene":"` + scene + `","from":"` + state + `","to":"` + state + `"}}` |
| 108 | } |
| 109 | fr, sqid := twinDeck(t, scene, |
| 110 | `{"op":"add_slide","id":"twinslide01","layout":"canvas"}`, |
| 111 | block("twinslide01", "twinleft0001", boxL, "one"), block("twinslide01", "twinright001", boxR, "one"), |
| 112 | `{"op":"add_slide","id":"twinslide02","layout":"canvas"}`, |
| 113 | block("twinslide02", "twinleft0002", boxL, "two"), block("twinslide02", "twinlow00002", boxLow, "two")) |
| 114 | deckPath := "/d/" + sqid |
| 115 | |
| 116 | ctx := fr.rig.Context() |
| 117 | run(t, ctx, chromedp.Navigate(fr.rig.Origin+deckPath+"?slide=1"), |
| 118 | waitFor(`document.documentElement.getAttribute('data-slides-fonts') !== 'pending' && !!document.querySelector('[slides-slide]')`)) |
| 119 | run(t, ctx, evalValue(nil, sceneWatch)) |
| 120 | until := func(what string, ok func(map[string]placement) bool) map[string]placement { |
| 121 | t.Helper() |
| 122 | return untilPlacements(t, ctx, what, ok) |
| 123 | } |
| 124 | first := until("a live frame over each block", func(m map[string]placement) bool { |
| 125 | return len(m) == 2 && live(m["twinleft0001"], "one") && live(m["twinright001"], "one") |
| 126 | }) |
| 127 | if first["twinleft0001"].Doc == first["twinright001"].Doc { |
| 128 | t.Errorf("the two blocks share one frame's document: %+v", first) |
| 129 | } |
| 130 | run(t, ctx, evalValue(nil, `document.querySelectorAll('[slides-scene-layer] iframe').forEach((f) => { f.__mark = true; })`)) |
| 131 | |
| 132 | run(t, ctx, chromedp.Click(chromedp.CSS(`[slides-pager] a[rel="next"]`)), waitFor(`location.search.includes('slide=2')`)) |
| 133 | next := until("the first carried on and the second made afresh", func(m map[string]placement) bool { |
| 134 | return len(m) == 2 && live(m["twinleft0002"], "two") && live(m["twinlow00002"], "two") |
| 135 | }) |
| 136 | if l := next["twinleft0002"]; !l.Mark || l.Doc != first["twinleft0001"].Doc || l.Type != "state" || l.Inits != 1 { |
| 137 | t.Errorf("the first block of the scene did not carry on from the first: %+v (was %+v)", l, first["twinleft0001"]) |
| 138 | } |
| 139 | if r := next["twinlow00002"]; r.Mark || r.Doc == first["twinright001"].Doc || r.Inits != 1 || r.Type != "init" { |
| 140 | t.Errorf("the second block, in another box, should start fresh: %+v", r) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | var ( |
| 145 | boxL = `{"x":0.05,"y":0.1,"w":0.4,"h":0.4}` |
| 146 | boxR = `{"x":0.55,"y":0.1,"w":0.4,"h":0.4}` |
| 147 | boxLow = `{"x":0.55,"y":0.55,"w":0.4,"h":0.4}` |
| 148 | ) |
| 149 | |
| 150 | // twinDeck is a deck holding only the scene id, recorderScene with |
| 151 | // states one and two, and the slides ops make. |
| 152 | func twinDeck(t *testing.T, scene string, ops ...string) (*fontRig, string) { |
| 153 | t.Helper() |
| 154 | store, err := blobs.Dir(t.TempDir()) |
| 155 | if err != nil { |
| 156 | t.Fatalf("blobs.Dir: %v", err) |
| 157 | } |
| 158 | fr := newFontRigWith(t, store) |
| 159 | sqid := apiDeck(t, fr.app, fr.token, "Twins") |
| 160 | code := uploadScene(t, fr.app, "/d/"+sqid, fr.cookies, []byte(recorderScene)) |
| 161 | view := apiJSON(t, fr.app, http.MethodGet, "/api/v1/decks/"+sqid, fr.token, "", http.StatusOK) |
| 162 | var batch []string |
| 163 | for _, s := range view["slides"].([]any) { |
| 164 | batch = append(batch, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`) |
| 165 | } |
| 166 | batch = append(batch, `{"op":"add_scene","id":"`+scene+`","name":"Twins","code":"`+code+`","states":[{"id":"one"},{"id":"two"}],"words":{"two":"Second"}}`) |
| 167 | batch = append(batch, ops...) |
| 168 | apiJSON(t, fr.app, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", fr.token, `{"ops":[`+strings.Join(batch, ",")+`]}`, http.StatusOK) |
| 169 | return fr, sqid |
| 170 | } |
| 171 | |
| 172 | // untilPlacements waits for the frames, by the block each is over, to |
| 173 | // satisfy ok. |
| 174 | func untilPlacements(t *testing.T, ctx context.Context, what string, ok func(map[string]placement) bool) map[string]placement { |
| 175 | t.Helper() |
| 176 | deadline := time.Now().Add(10 * time.Second) |
| 177 | for { |
| 178 | var list []placement |
| 179 | run(t, ctx, evalInto(&list, placementsJS)) |
| 180 | got := map[string]placement{} |
| 181 | for _, p := range list { |
| 182 | got[p.For] = p |
| 183 | } |
| 184 | if len(got) == len(list) && ok(got) { |
| 185 | return got |
| 186 | } |
| 187 | if time.Now().After(deadline) { |
| 188 | raw, _ := json.MarshalIndent(list, "", " ") |
| 189 | t.Fatalf("waiting for %s; the frames are:\n%s", what, raw) |
| 190 | } |
| 191 | time.Sleep(60 * time.Millisecond) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func live(p placement, state string) bool { return p.Visible && p.Doc != "" && p.State == state } |
| 196 | |
| 197 | // TestAnEarlierBlockArrivingLeavesALaterOneRunning (re-review): of two |
| 198 | // blocks of one scene, the first is in a box that arrives on press 1 |
| 199 | // (a scene block itself is there when its slide arrives, so the box |
| 200 | // holding it is what waits) and the second is there from the start. |
| 201 | // The press brings the first in with a frame of its own and leaves the |
| 202 | // second's frame as it was: the same element, the same document, never |
| 203 | // told init again. |
| 204 | func TestAnEarlierBlockArrivingLeavesALaterOneRunning(t *testing.T) { |
| 205 | const scene = "twinsceneaaa" |
| 206 | block := func(id, region string) string { |
| 207 | return `{"op":"add_block","slide":"waitslide001","id":"` + id + `","type":"scene","region":"` + region + |
| 208 | `","props":{"scene":"` + scene + `","from":"one","to":"one"}}` |
| 209 | } |
| 210 | fr, sqid := twinDeck(t, scene, `{"op":"add_slide","id":"waitslide001","layout":"flow","template":"two-column"}`, |
| 211 | block("waitfirst001", "body"), |
| 212 | `{"op":"add_block","slide":"waitslide001","id":"waitlater001","type":"columns","region":"body","props":{"count":2,"children":["waitfirst001"]}}`, |
| 213 | `{"op":"set_block","slide":"waitslide001","id":"waitlater001","step":1}`, |
| 214 | block("waitsecond01", "aside")) |
| 215 | session, _ := startPresenting(t, fr.app, "/d/"+sqid, fr.cookies) |
| 216 | ctx := fr.rig.Context() |
| 217 | open(t, ctx, fr.rig.Origin, "/p/"+session) |
| 218 | run(t, ctx, evalValue(nil, sceneWatch)) |
| 219 | before := untilPlacements(t, ctx, "the second block running alone", func(m map[string]placement) bool { |
| 220 | return len(m) == 1 && live(m["waitsecond01"], "one") |
| 221 | }) |
| 222 | run(t, ctx, evalValue(nil, `document.querySelectorAll('[slides-scene-layer] iframe').forEach((f) => { f.__mark = true; })`)) |
| 223 | run(t, ctx, key(kb.ArrowRight), waitFor(rootJS+`.dataset.step === '1'`)) |
| 224 | after := untilPlacements(t, ctx, "both blocks running", func(m map[string]placement) bool { |
| 225 | return len(m) == 2 && live(m["waitfirst001"], "one") && live(m["waitsecond01"], "one") |
| 226 | }) |
| 227 | if s := after["waitsecond01"]; !s.Mark || s.Doc != before["waitsecond01"].Doc || s.Inits != 1 { |
| 228 | t.Errorf("the first block's arrival restarted the second's frame: %+v (was %+v)", s, before["waitsecond01"]) |
| 229 | } |
| 230 | if f := after["waitfirst001"]; f.Mark || f.Inits != 1 { |
| 231 | t.Errorf("the first block, arriving, should have a frame of its own: %+v", f) |
| 232 | } |
| 233 | } |
| 234 | |