| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | // Editing a scene by hand, the review's round (scenes and clips spec |
| 6 | // §5, §8 call 3): Deck settings lists the scenes whose pictures are |
| 7 | // missing or out of date, whatever is selected; data over the limit is |
| 8 | // kept in its field and refused in words; code that did not load says |
| 9 | // so and can be asked for again, and cannot be saved meanwhile; and an |
| 10 | // author who arrives while the editor is open is named. |
| 11 | |
| 12 | import ( |
| 13 | "net/http" |
| 14 | "reflect" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | |
| 18 | "github.com/chromedp/chromedp" |
| 19 | |
| 20 | "amadan.net/oficina/slides/internal/slides/deck" |
| 21 | ) |
| 22 | |
| 23 | // stillsListJS: Deck settings' list of scenes missing pictures, each |
| 24 | // item's words, and whether it is drawn. |
| 25 | const stillsListJS = `(() => { |
| 26 | const f = document.querySelector('[slides-scene-stills]') |
| 27 | return { Shown: !!f && f.checkVisibility(), Legend: f ? f.querySelector('legend').textContent.trim() : '', |
| 28 | Items: f ? [...f.querySelectorAll('[slides-scene-stills-list] li')].map(li => li.textContent.replace(/\s+/g, ' ').trim()) : [] } |
| 29 | })()` |
| 30 | |
| 31 | type stillsListState struct { |
| 32 | Shown bool |
| 33 | Legend string |
| 34 | Items []string |
| 35 | } |
| 36 | |
| 37 | // TestDeckSettingsListsScenesMissingPictures: with a text selected (no |
| 38 | // scene in sight), Deck settings lists each scene whose pictures are |
| 39 | // missing or older than it, by name, with the card's own lines: World |
| 40 | // has none, Other's is out of date. A deck whose pictures are all |
| 41 | // current shows no list. |
| 42 | func TestDeckSettingsListsScenesMissingPictures(t *testing.T) { |
| 43 | se := openSceneEditor(t, sceneEditorOpts{two: true, staleOther: true}) |
| 44 | se.selectBoxByID(t, "scenetext002") |
| 45 | se.rig.Run(openDeckSettings()) |
| 46 | var got stillsListState |
| 47 | se.rig.Run(evalInto(&got, stillsListJS)) |
| 48 | want := []string{"Other These pictures are older than the scene.", "World 3 states have no picture yet."} |
| 49 | if !got.Shown || got.Legend != "Scenes still missing pictures" || !reflect.DeepEqual(got.Items, want) { |
| 50 | t.Errorf("Deck settings' list is %+v, want it shown, headed, listing %q", got, want) |
| 51 | } |
| 52 | se.rig.Screen(`[slides-deck-dialog]`, "Deck settings, scenes missing pictures") |
| 53 | |
| 54 | current := openSceneEditor(t, sceneEditorOpts{stills: true}) |
| 55 | current.selectBoxByID(t, "scenetext002") |
| 56 | current.rig.Run(openDeckSettings()) |
| 57 | got = stillsListState{} |
| 58 | current.rig.Run(evalInto(&got, stillsListJS)) |
| 59 | if got.Shown || len(got.Items) != 0 { |
| 60 | t.Errorf("every picture current: the list is %+v, want none drawn", got) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // bigData is data whose canonical JSON, {"s":"x…"}, is n bytes. |
| 65 | func bigData(n int) string { return `{"s":"` + strings.Repeat("x", n-8) + `"}` } |
| 66 | |
| 67 | // maxSceneData is 960 KiB, the most a scene's data may be. |
| 68 | const maxSceneData = 960 << 10 |
| 69 | |
| 70 | // TestSceneDataOverTheLimitIsRefused: data one byte over 960 KiB of |
| 71 | // canonical JSON stays in its field, marked invalid, with the words |
| 72 | // for it, and nothing is written; exactly 960 KiB is written. |
| 73 | func TestSceneDataOverTheLimitIsRefused(t *testing.T) { |
| 74 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 75 | se.selectScene(t) |
| 76 | json := `textarea[name="slides-scene-json"]` |
| 77 | se.rig.Run(clickLabelOf(`input[name="slides-scene-data-view"][value="json"]`), waitFor(`document.querySelector('`+json+`')?.checkVisibility()`)) |
| 78 | put := func(text string) { |
| 79 | 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 + `'))`)) |
| 80 | } |
| 81 | put(bigData(maxSceneData + 1)) |
| 82 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-data-big]')?.checkVisibility()`)) |
| 83 | var state struct { |
| 84 | Length int |
| 85 | Invalid string |
| 86 | Words string |
| 87 | } |
| 88 | se.rig.Run(evalInto(&state, `({ Length: document.querySelector('`+json+`').value.length, Invalid: document.querySelector('`+json+`').getAttribute('aria-invalid') || '', |
| 89 | Words: document.querySelector('[slides-scene-data-big]').textContent.trim() })`)) |
| 90 | if state.Length != maxSceneData+1 || state.Invalid != "true" || state.Words != "This data is over 960 KB, so it wasn't saved." { |
| 91 | t.Errorf("one byte over: the field holds %d characters, aria-invalid %q, saying %q; want the edit kept, invalid, and words", state.Length, state.Invalid, state.Words) |
| 92 | } |
| 93 | if p := pieces(se.scene(t, se.world).Data); len(p) != 2 { |
| 94 | t.Errorf("data over the limit was written: %v", se.scene(t, se.world).Data) |
| 95 | } |
| 96 | se.rig.Screen(`[slides-scene-data]`, "data over the limit") |
| 97 | |
| 98 | put(bigData(maxSceneData)) |
| 99 | se.untilScene(t, se.world, "960 KiB of data", func(s deck.Scene) bool { |
| 100 | m, _ := s.Data.(map[string]any) |
| 101 | v, _ := m["s"].(string) |
| 102 | return len(v) == maxSceneData-8 |
| 103 | }) |
| 104 | se.rig.Run(waitFor(`!document.querySelector('[slides-scene-data-big]').checkVisibility() && document.querySelector('` + json + `').getAttribute('aria-invalid') !== 'true'`)) |
| 105 | } |
| 106 | |
| 107 | // codePanelJS: the code panel's text, its line, its Try again and Save. |
| 108 | const codePanelJS = `(() => { |
| 109 | const p = document.querySelector('[slides-scene-code]'), vis = e => !!e && e.checkVisibility() |
| 110 | return { Text: p.querySelector('textarea').value, Status: vis(p.querySelector('[slides-scene-status]')) ? p.querySelector('[slides-scene-status]').textContent.trim() : '', |
| 111 | Retry: vis(p.querySelector('[slides-scene-retry]')), SaveOff: p.querySelector('[slides-scene-save]').disabled } |
| 112 | })()` |
| 113 | |
| 114 | type codePanel struct { |
| 115 | Text, Status string |
| 116 | Retry, SaveOff bool |
| 117 | } |
| 118 | |
| 119 | // TestSceneCodeThatDidNotLoadCanBeRetried: when the bundle's text does |
| 120 | // not come, the panel says so in the app's words with Try again, and |
| 121 | // Save code is off, so an empty field can never be saved as the code; |
| 122 | // Try again reads it, and Save is back. |
| 123 | func TestSceneCodeThatDidNotLoadCanBeRetried(t *testing.T) { |
| 124 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 125 | se.selectScene(t) |
| 126 | se.rig.Run(chromedp.Evaluate[chromedp.Void](`window.__fetch = window.fetch; window.fetch = (u, o) => String(u).includes('/scenes/') ? Promise.resolve(new Response('gone', { status: 500 })) : window.__fetch(u, o)`)) |
| 127 | se.rig.Run(clickLabelOf(`input[name="slides-scene-panel"][value="code"]`), waitFor(`document.querySelector('[slides-scene-retry]')?.checkVisibility()`)) |
| 128 | var p codePanel |
| 129 | se.rig.Run(evalInto(&p, codePanelJS)) |
| 130 | if p.Text != "" || p.Status != "That didn’t work" || !p.Retry || !p.SaveOff { |
| 131 | t.Errorf("code that did not load: %+v, want no text, That didn’t work, Try again, and Save off", p) |
| 132 | } |
| 133 | se.rig.Run(chromedp.Evaluate[chromedp.Void](`window.fetch = window.__fetch`), |
| 134 | chromedp.Click(chromedp.CSS(`[slides-scene-retry]`)), |
| 135 | waitFor(`document.querySelector('[slides-scene-code] textarea').value === `+jsString(editorScene))) |
| 136 | p = codePanel{} |
| 137 | se.rig.Run(evalInto(&p, codePanelJS)) |
| 138 | if p.Status != "" || p.Retry || p.SaveOff { |
| 139 | t.Errorf("after Try again: %+v, want the code, no line, and Save on", p) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // TestASceneAuthorArrivingLiveIsNamed: an agent the page did not know |
| 144 | // when it opened changes the code; the author line names it. |
| 145 | func TestASceneAuthorArrivingLiveIsNamed(t *testing.T) { |
| 146 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 147 | se.selectScene(t) |
| 148 | se.rig.Run(clickLabelOf(`input[name="slides-scene-panel"][value="code"]`), |
| 149 | waitFor(`document.querySelector('[slides-scene-author]').textContent.includes('scene-editor')`)) |
| 150 | late := mintAgent(t, se.app, se.cookies, "late-agent") |
| 151 | draft := strings.Replace(editorScene, "const MARK = 'saved';", "const MARK = 'late';", 1) |
| 152 | rec := agentCall(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/scene-bundles", late, []byte(draft)) |
| 153 | if rec.Code != http.StatusOK { |
| 154 | t.Fatalf("POST scene-bundles: %d\n%s", rec.Code, rec.Body) |
| 155 | } |
| 156 | apiJSON(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/ops", late, |
| 157 | `{"ops":[{"op":"set_scene","id":"`+se.world+`","code":"`+hashOf([]byte(draft))+`"}]}`, http.StatusOK) |
| 158 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-author]').textContent.trim().startsWith('Last changed by') && document.querySelector('[slides-scene-author]').textContent.includes('late-agent')`)) |
| 159 | } |
| 160 | |
| 161 | // TestNewCodeThatDidNotLoadCannotBeSavedOver: the code loads and Save is |
| 162 | // on; an agent changes the code while the panel is open, and the new |
| 163 | // text does not come: the panel says so, and Save is off, so the old |
| 164 | // text cannot be saved over the newer code. |
| 165 | func TestNewCodeThatDidNotLoadCannotBeSavedOver(t *testing.T) { |
| 166 | se := openSceneEditor(t, sceneEditorOpts{}) |
| 167 | se.selectScene(t) |
| 168 | se.rig.Run(clickLabelOf(`input[name="slides-scene-panel"][value="code"]`), |
| 169 | waitFor(`document.querySelector('[slides-scene-code] textarea').value === `+jsString(editorScene)+` && !document.querySelector('[slides-scene-save]').disabled`)) |
| 170 | se.rig.Run(chromedp.Evaluate[chromedp.Void](`window.__fetch = window.fetch; window.fetch = (u, o) => String(u).includes('/scenes/') && !String(u).includes('/scenes/preview') ? Promise.resolve(new Response('gone', { status: 500 })) : window.__fetch(u, o)`)) |
| 171 | newer := strings.Replace(editorScene, "const MARK = 'saved';", "const MARK = 'newer';", 1) |
| 172 | if rec := agentCall(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/scene-bundles", se.token, []byte(newer)); rec.Code != http.StatusOK { |
| 173 | t.Fatalf("POST scene-bundles: %d\n%s", rec.Code, rec.Body) |
| 174 | } |
| 175 | apiJSON(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/ops", se.token, |
| 176 | `{"ops":[{"op":"set_scene","id":"`+se.world+`","code":"`+hashOf([]byte(newer))+`"}]}`, http.StatusOK) |
| 177 | se.rig.Run(waitFor(`document.querySelector('[slides-scene-retry]')?.checkVisibility()`)) |
| 178 | var p codePanel |
| 179 | se.rig.Run(evalInto(&p, codePanelJS)) |
| 180 | if p.Status != "That didn’t work" || !p.Retry || !p.SaveOff { |
| 181 | t.Errorf("newer code that did not load: %+v, want That didn’t work, Try again, and Save off", p) |
| 182 | } |
| 183 | se.rig.Run(chromedp.Evaluate[chromedp.Void](`window.fetch = window.__fetch`), |
| 184 | chromedp.Click(chromedp.CSS(`[slides-scene-retry]`)), |
| 185 | waitFor(`document.querySelector('[slides-scene-code] textarea').value === `+jsString(newer)+` && !document.querySelector('[slides-scene-save]').disabled`)) |
| 186 | } |
| 187 | |