| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | // Editing a scene by hand in the editor (scenes and clips spec §5; |
| 6 | // fixtures/scene-card.mjs, scene-data.mjs, scene-code.mjs): the scene |
| 7 | // block's card (its scene, From and To, its words, the out-of-date note |
| 8 | // and the missing pictures), the data as a table or as JSON, the code |
| 9 | // with a live preview and Save, and a scene block's states counted in |
| 10 | // the editor's press numbers. Every write is read back over the API. |
| 11 | |
| 12 | import ( |
| 13 | "encoding/json" |
| 14 | "net/http" |
| 15 | "reflect" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | "time" |
| 19 | |
| 20 | "github.com/chromedp/chromedp" |
| 21 | "github.com/chromedp/chromedp/kb" |
| 22 | |
| 23 | "amadan.net/oficina/slides/internal/slides/deck" |
| 24 | ) |
| 25 | |
| 26 | // worldData is the World scene's data: a list of objects under a key, |
| 27 | // which the data panel shows as a table. |
| 28 | const worldData = `{"pieces":[{"name":"Tower","floors":3,"lit":true},{"name":"Garden","floors":0,"lit":false}]}` |
| 29 | |
| 30 | // sceneEditor is an editor (with an object store) on a deck holding the |
| 31 | // scene World (editorScene; states a, b, c named A, B, C; words title |
| 32 | // and sign; worldData) and, with two, the scene Other (one state, x), |
| 33 | // and one canvas slide, scenecanvas1: a heading, World's block |
| 34 | // sceneblock01 from a to c (presses 1 and 2), a text on step 1 |
| 35 | // (scenetext001, press 3) and a text with the slide (scenetext002). |
| 36 | type sceneEditor struct { |
| 37 | *tidyEditor |
| 38 | token, world, other string |
| 39 | } |
| 40 | |
| 41 | type sceneEditorOpts struct { |
| 42 | two bool // the scene Other too |
| 43 | stills bool // a still for each of World's states, made from its version now |
| 44 | // staleOther: Other's one still, made from its version before its |
| 45 | // words changed (with two). |
| 46 | staleOther bool |
| 47 | } |
| 48 | |
| 49 | func openSceneEditor(t *testing.T, o sceneEditorOpts) *sceneEditor { |
| 50 | t.Helper() |
| 51 | se := &sceneEditor{} |
| 52 | te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { |
| 53 | se.token = mintAgent(t, a, cookies, "scene-editor") |
| 54 | sqid := apiDeck(t, a, se.token, "Scenes by hand") |
| 55 | code := uploadScene(t, a, "/d/"+sqid, cookies, []byte(editorScene)) |
| 56 | ops := []string{`{"op":"add_scene","name":"World","code":"` + code + `","states":[` + |
| 57 | `{"id":"a","name":"A","description":"The field"},{"id":"b","name":"B","description":"The town"},` + |
| 58 | `{"id":"c","name":"C","description":"The city"}],"words":{"title":"Vienna","sign":"Open"},"data":` + worldData + `}`} |
| 59 | if o.two { |
| 60 | ops = append(ops, `{"op":"add_scene","name":"Other","code":"`+code+`","states":[{"id":"x","name":"X","description":"Only"}]}`) |
| 61 | } |
| 62 | out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", se.token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 63 | ids, _ := out["ids"].(map[string]any) |
| 64 | se.world, _ = ids["0"].(string) |
| 65 | if o.two { |
| 66 | se.other, _ = ids["1"].(string) |
| 67 | } |
| 68 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, se.token, "", http.StatusOK) |
| 69 | ops = nil |
| 70 | for _, s := range view["slides"].([]any) { |
| 71 | ops = append(ops, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`) |
| 72 | } |
| 73 | ops = append(ops, `{"op":"add_slide","id":"scenecanvas1","layout":"canvas"}`, |
| 74 | `{"op":"add_block","slide":"scenecanvas1","id":"sceneshead01","type":"heading","box":{"x":0.05,"y":0.03,"w":0.9,"h":0.15},"props":{"level":1},"markdown":"By hand"}`, |
| 75 | `{"op":"add_block","slide":"scenecanvas1","id":"sceneblock01","type":"scene","box":{"x":0.05,"y":0.25,"w":0.5,"h":0.6},"props":{"scene":"`+se.world+`","from":"a","to":"c"}}`, |
| 76 | `{"op":"add_block","slide":"scenecanvas1","id":"scenetext001","type":"text","box":{"x":0.6,"y":0.25,"w":0.35,"h":0.2},"markdown":"Later"}`, |
| 77 | `{"op":"set_block","slide":"scenecanvas1","id":"scenetext001","step":1}`, |
| 78 | `{"op":"add_block","slide":"scenecanvas1","id":"scenetext002","type":"text","box":{"x":0.6,"y":0.6,"w":0.35,"h":0.2},"markdown":"Last"}`) |
| 79 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", se.token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 80 | if o.stills { |
| 81 | made := deck.SceneVersion(sceneOf(t, a, se.token, sqid, se.world)) |
| 82 | for _, state := range []string{"a", "b", "c"} { |
| 83 | if rec := agentCall(t, a, http.MethodPut, stillURL(sqid, se.world, state, made), se.token, onePixelPNG); rec.Code != http.StatusOK { |
| 84 | t.Fatalf("PUT still %s: status %d\n%s", state, rec.Code, rec.Body) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | if o.two && o.staleOther { |
| 89 | made := deck.SceneVersion(sceneOf(t, a, se.token, sqid, se.other)) |
| 90 | if rec := agentCall(t, a, http.MethodPut, stillURL(sqid, se.other, "x", made), se.token, onePixelPNG); rec.Code != http.StatusOK { |
| 91 | t.Fatalf("PUT Other's still: status %d\n%s", rec.Code, rec.Body) |
| 92 | } |
| 93 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", se.token, |
| 94 | `{"ops":[{"op":"set_scene","id":"`+se.other+`","words":{"note":"Changed"}}]}`, http.StatusOK) |
| 95 | } |
| 96 | return "/d/" + sqid + "/edit" |
| 97 | }) |
| 98 | te.rig.Run( |
| 99 | chromedp.Navigate(te.rig.Origin+te.path), |
| 100 | chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), |
| 101 | waitFor(editorBound), |
| 102 | evalInto(new(bool), sceneWatch), |
| 103 | ) |
| 104 | se.tidyEditor = te |
| 105 | return se |
| 106 | } |
| 107 | |
| 108 | // scene is World (or another scene) as the API reads it. |
| 109 | func (se *sceneEditor) scene(t *testing.T, id string) deck.Scene { |
| 110 | t.Helper() |
| 111 | return sceneOf(t, se.app, se.token, se.sqid(), id) |
| 112 | } |
| 113 | |
| 114 | // untilScene polls the API until scene id satisfies ok, for up to 20 s. |
| 115 | func (se *sceneEditor) untilScene(t *testing.T, id, what string, ok func(deck.Scene) bool) deck.Scene { |
| 116 | t.Helper() |
| 117 | deadline := time.Now().Add(20 * time.Second) |
| 118 | for { |
| 119 | s := se.scene(t, id) |
| 120 | if ok(s) { |
| 121 | return s |
| 122 | } |
| 123 | if time.Now().After(deadline) { |
| 124 | raw, _ := json.Marshal(s) |
| 125 | t.Fatalf("the scene never became %s: %s", what, raw) |
| 126 | } |
| 127 | time.Sleep(100 * time.Millisecond) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // selectScene selects sceneblock01 and waits for its card. |
| 132 | func (se *sceneEditor) selectScene(t *testing.T) { |
| 133 | t.Helper() |
| 134 | se.selectBoxByID(t, "sceneblock01") |
| 135 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-body]')?.checkVisibility()`)) |
| 136 | } |
| 137 | |
| 138 | // sceneCardJS reads the scene block's card as a person sees it. |
| 139 | const sceneCardJS = `(() => { |
| 140 | const body = document.querySelector('[slides-scene-body]') |
| 141 | const vis = el => !!el && el.checkVisibility() |
| 142 | const text = el => vis(el) ? el.textContent.replace(/\s+/g, ' ').trim() : '' |
| 143 | const q = s => body ? body.querySelector(s) : null |
| 144 | const pick = q('select[name="rst-block-scene"]'), from = q('select[name="rst-block-scene-from"]'), to = q('select[name="rst-block-scene-to"]') |
| 145 | return { |
| 146 | Shown: vis(body), |
| 147 | Picks: vis(pick) ? [...pick.options].map(o => o.textContent) : [], |
| 148 | Pick: vis(pick) ? pick.value : '', |
| 149 | Name: text(q('[slides-scene-name]')), |
| 150 | States: vis(from) ? [...from.options].map(o => o.textContent) : [], |
| 151 | From: vis(from) ? from.value : '', To: vis(to) ? to.value : '', |
| 152 | Words: body ? [...body.querySelectorAll('[slides-scene-words] input')].filter(vis).map(i => i.labels[0].textContent + '=' + i.value) : [], |
| 153 | Stale: text(q('[slides-scene-stale]')), |
| 154 | Ask: vis(q('[slides-scene-ask]')) ? (q('[slides-scene-ask]').getAttribute('href') + ' ' + q('[slides-scene-ask]').getAttribute('target')) : '', |
| 155 | Missing: text(q('[slides-scene-missing]')) |
| 156 | } |
| 157 | })()` |
| 158 | |
| 159 | type sceneCard struct { |
| 160 | Shown bool |
| 161 | Picks, States, Words []string |
| 162 | Pick, Name, From, To string |
| 163 | Stale, Ask, Missing string |
| 164 | } |
| 165 | |
| 166 | func (se *sceneEditor) card(t *testing.T) sceneCard { |
| 167 | t.Helper() |
| 168 | var c sceneCard |
| 169 | se.rig.Run(evalInto(&c, sceneCardJS)) |
| 170 | return c |
| 171 | } |
| 172 | |
| 173 | // choose picks value in the card's select named name, as a person does. |
| 174 | func (se *sceneEditor) choose(name, value string) { |
| 175 | se.rig.Run(chromedp.Evaluate[chromedp.Void](`(s => { s.focus(); s.value = ` + jsString(value) + `; s.dispatchEvent(new Event('change', { bubbles: true })) })(document.querySelector('[slides-scene-body] select[name="` + name + `"]'))`)) |
| 176 | } |
| 177 | |
| 178 | // typeInto replaces the text of the field sel with text, typed, and |
| 179 | // leaves it with Tab: one change. |
| 180 | func (se *sceneEditor) typeInto(sel, text string) { |
| 181 | se.rig.Run( |
| 182 | chromedp.Focus(chromedp.CSS(sel)), |
| 183 | chromedp.Evaluate[chromedp.Void](`document.activeElement.select()`), |
| 184 | chromedp.KeyEvent(text), |
| 185 | ) |
| 186 | } |
| 187 | |
| 188 | func (se *sceneEditor) leave() { se.rig.Run(chromedp.KeyEvent(kb.Tab)) } |
| 189 | |
| 190 | // propsOf is block id's props on slide s as the API reads them. |
| 191 | func propsOf(s map[string]any, id string) map[string]any { |
| 192 | b, _ := blockOf(s, id) |
| 193 | p, _ := b["props"].(map[string]any) |
| 194 | return p |
| 195 | } |
| 196 | |
| 197 | // TestTheSceneCardEditsWordsAndStates: a scene block's card names its |
| 198 | // scene (a choice when the deck has several), its From and To by state |
| 199 | // name, its words as fields, and the states with no picture. From and |
| 200 | // To are each one set_block, kept in order; a word is written when its |
| 201 | // field is left, not as it is typed; another scene starts at its first |
| 202 | // state; and Insert → Scene places a block of one. |
| 203 | func TestTheSceneCardEditsWordsAndStates(t *testing.T) { |
| 204 | se := openSceneEditor(t, sceneEditorOpts{two: true}) |
| 205 | se.selectScene(t) |
| 206 | c := se.card(t) |
| 207 | if !reflect.DeepEqual(c.Picks, []string{"Other", "World"}) || c.Pick != se.world || c.Name != "" { |
| 208 | t.Errorf("two scenes: the card picks %v (%q) named %q, want Other and World with World chosen", c.Picks, c.Pick, c.Name) |
| 209 | } |
| 210 | if !reflect.DeepEqual(c.States, []string{"A", "B", "C"}) || c.From != "a" || c.To != "c" { |
| 211 | t.Errorf("From and To: %v from %q to %q, want A B C from a to c", c.States, c.From, c.To) |
| 212 | } |
| 213 | if !reflect.DeepEqual(c.Words, []string{"sign=Open", "title=Vienna"}) { |
| 214 | t.Errorf("the words are %v, want sign=Open title=Vienna", c.Words) |
| 215 | } |
| 216 | if c.Missing != "3 states have no picture yet." || c.Stale != "" { |
| 217 | t.Errorf("no stills: missing %q and stale %q, want 3 states have no picture yet. and no note", c.Missing, c.Stale) |
| 218 | } |
| 219 | se.rig.Screen(`[slides-inspector]`, "a scene block's card") |
| 220 | |
| 221 | se.choose("rst-block-scene-to", "b") |
| 222 | se.untilSlide(t, se.token, "scenecanvas1", "to b", func(s map[string]any) bool { |
| 223 | p := propsOf(s, "sceneblock01") |
| 224 | return p["from"] == "a" && p["to"] == "b" |
| 225 | }) |
| 226 | // From past To takes To with it: the two stay in order. |
| 227 | se.choose("rst-block-scene-from", "c") |
| 228 | se.untilSlide(t, se.token, "scenecanvas1", "from c to c", func(s map[string]any) bool { |
| 229 | p := propsOf(s, "sceneblock01") |
| 230 | return p["from"] == "c" && p["to"] == "c" |
| 231 | }) |
| 232 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-body] select[name="rst-block-scene-to"]').value === 'c'`)) |
| 233 | |
| 234 | // A word: nothing while it is typed, written once it is left. |
| 235 | title := `[slides-scene-words] input[data-word="title"]` |
| 236 | se.typeInto(title, "Wien") |
| 237 | time.Sleep(800 * time.Millisecond) |
| 238 | if w := se.scene(t, se.world).Words; w["title"] != "Vienna" { |
| 239 | t.Errorf("a word was written while it was typed: %v", w) |
| 240 | } |
| 241 | se.leave() |
| 242 | se.untilScene(t, se.world, "titled Wien", func(s deck.Scene) bool { return s.Words["title"] == "Wien" && s.Words["sign"] == "Open" }) |
| 243 | |
| 244 | // Another scene: its first state to its last. |
| 245 | se.choose("rst-block-scene", se.other) |
| 246 | se.untilSlide(t, se.token, "scenecanvas1", "showing Other", func(s map[string]any) bool { |
| 247 | p := propsOf(s, "sceneblock01") |
| 248 | return p["scene"] == se.other && p["from"] == "x" && p["to"] == "x" |
| 249 | }) |
| 250 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-body] select[name="rst-block-scene-from"]').options.length === 1`)) |
| 251 | |
| 252 | // Insert → Scene: a block of the first scene by name, first state to last. |
| 253 | sel := `[slides-stage] [slides-block][data-block="scene"]` |
| 254 | se.rig.Run( |
| 255 | chromedp.Click(chromedp.CSS(`[slides-menu="insert"] > summary`)), |
| 256 | chromedp.Click(chromedp.CSS(`[slides-menu="insert"] [data-insert-scene]`)), |
| 257 | waitFor(`document.querySelectorAll('`+sel+`').length === 2`), |
| 258 | ) |
| 259 | s := se.untilSlide(t, se.token, "scenecanvas1", "with a second scene block", func(s map[string]any) bool { |
| 260 | n := 0 |
| 261 | for _, b := range s["blocks"].([]any) { |
| 262 | if b.(map[string]any)["type"] == "scene" { |
| 263 | n++ |
| 264 | } |
| 265 | } |
| 266 | return n == 2 |
| 267 | }) |
| 268 | for _, b := range s["blocks"].([]any) { |
| 269 | m := b.(map[string]any) |
| 270 | if m["type"] != "scene" || m["id"] == "sceneblock01" { |
| 271 | continue |
| 272 | } |
| 273 | if p, _ := m["props"].(map[string]any); p["scene"] != se.other || p["from"] != "x" || p["to"] != "x" { |
| 274 | t.Errorf("Insert → Scene made props %v, want Other from x to x", p) |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // sceneFramesJS: every scene frame on the stage, with what its document |
| 280 | // was told at its last init. |
| 281 | const sceneFramesJS = `[...document.querySelectorAll('[slides-scene-layer] iframe, [slides-scene-frame]')].map(f => { |
| 282 | const log = (window.__scenes || {})[f.__doc] || [] |
| 283 | const init = log.filter(m => m.type === 'init').pop() |
| 284 | return { Doc: f.__doc || '', Live: getComputedStyle(f).opacity === '1', Mark: init ? init.mark : '', Title: init ? init.title : '', Data: init ? init.data : '' } |
| 285 | })` |
| 286 | |
| 287 | type editorFrame struct { |
| 288 | Doc, Mark, Title, Data string |
| 289 | Live bool |
| 290 | } |
| 291 | |
| 292 | // untilFrame waits for a live frame on the stage that ok accepts. |
| 293 | func (se *sceneEditor) untilFrame(t *testing.T, what string, ok func(editorFrame) bool) editorFrame { |
| 294 | t.Helper() |
| 295 | deadline := time.Now().Add(15 * time.Second) |
| 296 | for { |
| 297 | var fs []editorFrame |
| 298 | se.rig.Run(evalInto(&fs, sceneFramesJS)) |
| 299 | for _, f := range fs { |
| 300 | if f.Live && f.Doc != "" && ok(f) { |
| 301 | return f |
| 302 | } |
| 303 | } |
| 304 | if time.Now().After(deadline) { |
| 305 | t.Fatalf("no live frame %s; the frames: %+v", what, fs) |
| 306 | } |
| 307 | time.Sleep(80 * time.Millisecond) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // dataOf is the scene's data decoded from JSON f told it. |
| 312 | func dataOf(t *testing.T, raw string) map[string]any { |
| 313 | t.Helper() |
| 314 | var v map[string]any |
| 315 | if err := json.Unmarshal([]byte(raw), &v); err != nil { |
| 316 | t.Fatalf("data %q: %v", raw, err) |
| 317 | } |
| 318 | return v |
| 319 | } |
| 320 | |
| 321 | func pieces(v any) []any { |
| 322 | m, _ := v.(map[string]any) |
| 323 | p, _ := m["pieces"].([]any) |
| 324 | return p |
| 325 | } |
| 326 | |
| 327 | // tableJS: the data table's columns and rows as shown. |
| 328 | const tableJS = `(() => { |
| 329 | const t = document.querySelector('[slides-scene-table] table') |
| 330 | if (!t || !t.checkVisibility()) return { Cols: [], Rows: [] } |
| 331 | return { Cols: [...t.querySelectorAll('thead th')].map(th => th.textContent.trim()).filter(Boolean), |
| 332 | Rows: [...t.querySelectorAll('tbody tr')].map(tr => [...tr.querySelectorAll('input')].map(i => i.value)) } |
| 333 | })()` |
| 334 | |
| 335 | type dataTable struct { |
| 336 | Cols []string |
| 337 | Rows [][]string |
| 338 | } |
| 339 | |
| 340 | // TestTheDataTableRedrawsTheScene: a scene whose data holds a list of |
| 341 | // objects shows it as a table, a column per key (in code point order: |
| 342 | // stored keys keep no other); a cell, Add a row and |
| 343 | // Remove row each write the whole data once, keeping a number a number, |
| 344 | // and the scene running on the stage is told the new data. The JSON |
| 345 | // view takes anything, and says so when what it is given is not JSON. |
| 346 | func TestTheDataTableRedrawsTheScene(t *testing.T) { |
| 347 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 348 | se.selectScene(t) |
| 349 | if c := se.card(t); c.Name != "World" || len(c.Picks) != 0 { |
| 350 | t.Errorf("one scene: the card names %q and picks %v, want World and no choice", c.Name, c.Picks) |
| 351 | } |
| 352 | var tbl dataTable |
| 353 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-table] table')?.checkVisibility()`), evalInto(&tbl, tableJS)) |
| 354 | if !reflect.DeepEqual(tbl.Cols, []string{"floors", "lit", "name"}) || |
| 355 | !reflect.DeepEqual(tbl.Rows, [][]string{{"3", "true", "Tower"}, {"0", "false", "Garden"}}) { |
| 356 | t.Errorf("the table is %+v, want floors lit name over 3 true Tower and 0 false Garden", tbl) |
| 357 | } |
| 358 | se.rig.Screen(`[slides-inspector]`, "a scene's data as a table") |
| 359 | first := se.untilFrame(t, "told World's data", func(f editorFrame) bool { return len(pieces(dataOf(t, f.Data))) == 2 }) |
| 360 | |
| 361 | se.typeInto(`[slides-scene-table] input[data-row="0"][data-col="floors"]`, "5") |
| 362 | se.leave() |
| 363 | got := se.untilScene(t, se.world, "with five floors", func(s deck.Scene) bool { |
| 364 | p := pieces(s.Data) |
| 365 | return len(p) == 2 && p[0].(map[string]any)["floors"] == float64(5) |
| 366 | }) |
| 367 | if p := pieces(got.Data)[0].(map[string]any); p["name"] != "Tower" || p["lit"] != true { |
| 368 | t.Errorf("a cell's write changed the rest of its row: %v", p) |
| 369 | } |
| 370 | se.untilFrame(t, "told five floors", func(f editorFrame) bool { |
| 371 | p := pieces(dataOf(t, f.Data)) |
| 372 | return f.Doc != first.Doc && len(p) == 2 && p[0].(map[string]any)["floors"] == float64(5) |
| 373 | }) |
| 374 | |
| 375 | se.rig.Run(chromedp.Click(chromedp.CSS(`[slides-scene-add]`))) |
| 376 | se.untilScene(t, se.world, "with a third row", func(s deck.Scene) bool { |
| 377 | p := pieces(s.Data) |
| 378 | return len(p) == 3 && reflect.DeepEqual(p[2], map[string]any{"name": "", "floors": float64(0), "lit": false}) |
| 379 | }) |
| 380 | se.rig.Run(chromedp.Click(chromedp.CSS(`[slides-scene-remove][data-row="2"]`))) |
| 381 | se.untilScene(t, se.world, "two rows again", func(s deck.Scene) bool { return len(pieces(s.Data)) == 2 }) |
| 382 | |
| 383 | // The JSON view. |
| 384 | se.rig.Run(clickLabelOf(`input[name="slides-scene-data-view"][value="json"]`), |
| 385 | waitFor(`document.querySelector('textarea[name="slides-scene-json"]')?.checkVisibility()`)) |
| 386 | var shown string |
| 387 | se.rig.Run(evalInto(&shown, `document.querySelector('textarea[name="slides-scene-json"]').value`)) |
| 388 | if v := dataOf(t, shown); len(pieces(v)) != 2 { |
| 389 | t.Errorf("the JSON view shows %q", shown) |
| 390 | } |
| 391 | se.typeInto(`textarea[name="slides-scene-json"]`, `{"pieces": [`) |
| 392 | se.leave() |
| 393 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-invalid]')?.checkVisibility()`)) |
| 394 | var invalid string |
| 395 | se.rig.Run(evalInto(&invalid, `document.querySelector('[slides-scene-invalid]').textContent.trim()`)) |
| 396 | if invalid != "That isn't valid JSON." { |
| 397 | t.Errorf("not JSON: the panel says %q", invalid) |
| 398 | } |
| 399 | time.Sleep(500 * time.Millisecond) |
| 400 | if len(pieces(se.scene(t, se.world).Data)) != 2 { |
| 401 | t.Error("text that is not JSON was written") |
| 402 | } |
| 403 | se.typeInto(`textarea[name="slides-scene-json"]`, `{"pieces":[{"name":"Tower","floors":7,"lit":true}]}`) |
| 404 | se.leave() |
| 405 | se.untilScene(t, se.world, "one row of seven floors", func(s deck.Scene) bool { |
| 406 | p := pieces(s.Data) |
| 407 | return len(p) == 1 && p[0].(map[string]any)["floors"] == float64(7) |
| 408 | }) |
| 409 | se.rig.Run(waitFor(`!document.querySelector('[slides-scene-invalid]').checkVisibility()`)) |
| 410 | } |
| 411 | |
| 412 | // TestTheCodePanelPreviewsAndSaves: the code panel holds the bundle's |
| 413 | // text and who last changed it; Preview runs the unsaved text in the |
| 414 | // block's frame through the sealed preview route and writes nothing; |
| 415 | // Save code stores a new bundle and names it as the scene's code, Paul |
| 416 | // its author; a text that is not one HTML page is refused in words. |
| 417 | func TestTheCodePanelPreviewsAndSaves(t *testing.T) { |
| 418 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 419 | se.selectScene(t) |
| 420 | code := `textarea[name="slides-scene-code"]` |
| 421 | se.rig.Run(clickLabelOf(`input[name="slides-scene-panel"][value="code"]`), |
| 422 | waitFor(`document.querySelector('`+code+`')?.checkVisibility() && document.querySelector('`+code+`').value === `+jsString(editorScene))) |
| 423 | var author string |
| 424 | se.rig.Run(evalInto(&author, `document.querySelector('[slides-scene-author]').textContent.trim()`)) |
| 425 | if !strings.HasPrefix(author, "Last changed by ") || !strings.Contains(author, "scene-editor") { |
| 426 | t.Errorf("the author line says %q, want Last changed by the agent scene-editor", author) |
| 427 | } |
| 428 | before := se.scene(t, se.world).Code |
| 429 | saved := se.untilFrame(t, "running the saved code", func(f editorFrame) bool { return f.Mark == "saved" }) |
| 430 | |
| 431 | draft := strings.Replace(editorScene, "const MARK = 'saved';", "const MARK = 'draft';", 1) |
| 432 | set := func(text string) { |
| 433 | se.rig.Run(chromedp.Evaluate[chromedp.Void](`(a => { a.value = ` + jsString(text) + `; a.dispatchEvent(new Event('input', { bubbles: true })) })(document.querySelector('` + code + `'))`)) |
| 434 | } |
| 435 | set(draft) |
| 436 | se.rig.Run(chromedp.Click(chromedp.CSS(`[slides-scene-preview]`))) |
| 437 | se.untilFrame(t, "running the draft", func(f editorFrame) bool { return f.Mark == "draft" && f.Doc != saved.Doc }) |
| 438 | if got := se.scene(t, se.world).Code; got != before { |
| 439 | t.Errorf("Preview changed the scene's code to %s", got) |
| 440 | } |
| 441 | se.rig.Screen(`[slides-stage-pane]`, "the draft previewing on the stage") |
| 442 | |
| 443 | se.rig.Run(chromedp.Click(chromedp.CSS(`[slides-scene-save]`))) |
| 444 | s := se.untilScene(t, se.world, "with the draft as its code", func(s deck.Scene) bool { return s.Code == hashOf([]byte(draft)) }) |
| 445 | if !strings.HasPrefix(s.Author, "person:") { |
| 446 | t.Errorf("a hand edit's author is %q, want the person", s.Author) |
| 447 | } |
| 448 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-author]').textContent.trim() === 'Last changed by ` + allowedAddress + `'`)) |
| 449 | se.untilFrame(t, "running the saved draft", func(f editorFrame) bool { return f.Mark == "draft" }) |
| 450 | |
| 451 | set("not a page") |
| 452 | se.rig.Run(chromedp.Click(chromedp.CSS(`[slides-scene-save]`)), |
| 453 | waitFor(`document.querySelector('[slides-scene-status]').textContent.trim() === "World isn't a single HTML page."`)) |
| 454 | if got := se.scene(t, se.world).Code; got != hashOf([]byte(draft)) { |
| 455 | t.Errorf("a refused save changed the code to %s", got) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // TestAHandEditMarksStillsOutOfDate: with a still for every state made |
| 460 | // from the scene as it is, the card says nothing about pictures; a word |
| 461 | // changed by hand leaves them older than the scene, which the card says, |
| 462 | // with the way to an agent who can redraw them: the Agents page, in a |
| 463 | // tab of its own. |
| 464 | func TestAHandEditMarksStillsOutOfDate(t *testing.T) { |
| 465 | se := openSceneEditor(t, sceneEditorOpts{stills: true}) |
| 466 | se.selectScene(t) |
| 467 | if c := se.card(t); c.Stale != "" || c.Missing != "" { |
| 468 | t.Errorf("current stills: the card says %q / %q, want nothing", c.Stale, c.Missing) |
| 469 | } |
| 470 | se.typeInto(`[slides-scene-words] input[data-word="sign"]`, "Closed") |
| 471 | se.leave() |
| 472 | se.untilScene(t, se.world, "signed Closed", func(s deck.Scene) bool { return s.Words["sign"] == "Closed" }) |
| 473 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-stale]')?.checkVisibility()`)) |
| 474 | c := se.card(t) |
| 475 | if c.Stale != "These pictures are older than the scene. Ask an agent to redraw them" || c.Ask != "/agents _blank" { |
| 476 | t.Errorf("after a hand edit the note is %q asking at %q, want the out-of-date note and the Agents page", c.Stale, c.Ask) |
| 477 | } |
| 478 | if stillsCurrent(se.scene(t, se.world)) { |
| 479 | t.Error("the API still reads the stills as current") |
| 480 | } |
| 481 | se.rig.Screen(`[slides-inspector]`, "stills older than the scene") |
| 482 | } |
| 483 | |
| 484 | // TestAfterNeverOffersPressZero: a scene block's states are the slide's |
| 485 | // first presses in the editor too; After… on another box lists the |
| 486 | // presses after them by their numbers and never the scene's value 0, |
| 487 | // and the press numbers mark the scene's range. |
| 488 | func TestAfterNeverOffersPressZero(t *testing.T) { |
| 489 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 490 | se.selectBoxByID(t, "scenetext002") |
| 491 | if m := se.stepMarks(); !reflect.DeepEqual(m.Numbers, []string{"1–2", "3"}) { |
| 492 | t.Errorf("the press numbers are %v, want 1–2 on the scene and 3 on the later text", m.Numbers) |
| 493 | } |
| 494 | after := `[slides-inspector-form][data-section="block"] [popovertarget="slides-pop-after"]` |
| 495 | se.rig.Run( |
| 496 | waitFor(`document.querySelector('`+after+`')?.checkVisibility()`), |
| 497 | chromedp.Click(chromedp.CSS(after)), |
| 498 | waitFor(`document.querySelector('#slides-pop-after').matches(':popover-open')`), |
| 499 | ) |
| 500 | var rows []string |
| 501 | se.rig.Run(evalInto(&rows, `[...document.querySelectorAll('#slides-pop-after [data-after]')].map(r => r.textContent.trim() + ' @' + r.dataset.after)`)) |
| 502 | if !reflect.DeepEqual(rows, []string{"After press 3 @1"}) { |
| 503 | t.Errorf("After… lists %v, want After press 3 alone (no value 0)", rows) |
| 504 | } |
| 505 | se.rig.Run(chromedp.Click(chromedp.CSS(`#slides-pop-after [data-after]`))) |
| 506 | se.untilSlide(t, se.token, "scenecanvas1", "the last text after press 3", stepsAre(map[string]float64{"scenetext002": 2, "scenetext001": 1})) |
| 507 | } |
| 508 | |