//go:build browser package slidestest // Editing a scene by hand, the review's round (scenes and clips spec // §5, §8 call 3): Deck settings lists the scenes whose pictures are // missing or out of date, whatever is selected; data over the limit is // kept in its field and refused in words; code that did not load says // so and can be asked for again, and cannot be saved meanwhile; and an // author who arrives while the editor is open is named. import ( "net/http" "reflect" "strings" "testing" "github.com/chromedp/chromedp" "amadan.net/oficina/slides/internal/slides/deck" ) // stillsListJS: Deck settings' list of scenes missing pictures, each // item's words, and whether it is drawn. const stillsListJS = `(() => { const f = document.querySelector('[slides-scene-stills]') return { Shown: !!f && f.checkVisibility(), Legend: f ? f.querySelector('legend').textContent.trim() : '', Items: f ? [...f.querySelectorAll('[slides-scene-stills-list] li')].map(li => li.textContent.replace(/\s+/g, ' ').trim()) : [] } })()` type stillsListState struct { Shown bool Legend string Items []string } // TestDeckSettingsListsScenesMissingPictures: with a text selected (no // scene in sight), Deck settings lists each scene whose pictures are // missing or older than it, by name, with the card's own lines: World // has none, Other's is out of date. A deck whose pictures are all // current shows no list. func TestDeckSettingsListsScenesMissingPictures(t *testing.T) { se := openSceneEditor(t, sceneEditorOpts{two: true, staleOther: true}) se.selectBoxByID(t, "scenetext002") se.rig.Run(openDeckSettings()) var got stillsListState se.rig.Run(evalInto(&got, stillsListJS)) want := []string{"Other These pictures are older than the scene.", "World 3 states have no picture yet."} if !got.Shown || got.Legend != "Scenes still missing pictures" || !reflect.DeepEqual(got.Items, want) { t.Errorf("Deck settings' list is %+v, want it shown, headed, listing %q", got, want) } se.rig.Screen(`[slides-deck-dialog]`, "Deck settings, scenes missing pictures") current := openSceneEditor(t, sceneEditorOpts{stills: true}) current.selectBoxByID(t, "scenetext002") current.rig.Run(openDeckSettings()) got = stillsListState{} current.rig.Run(evalInto(&got, stillsListJS)) if got.Shown || len(got.Items) != 0 { t.Errorf("every picture current: the list is %+v, want none drawn", got) } } // bigData is data whose canonical JSON, {"s":"x…"}, is n bytes. func bigData(n int) string { return `{"s":"` + strings.Repeat("x", n-8) + `"}` } // maxSceneData is 960 KiB, the most a scene's data may be. const maxSceneData = 960 << 10 // TestSceneDataOverTheLimitIsRefused: data one byte over 960 KiB of // canonical JSON stays in its field, marked invalid, with the words // for it, and nothing is written; exactly 960 KiB is written. func TestSceneDataOverTheLimitIsRefused(t *testing.T) { se := openSceneEditor(t, sceneEditorOpts{}) se.selectScene(t) json := `textarea[name="slides-scene-json"]` se.rig.Run(clickLabelOf(`input[name="slides-scene-data-view"][value="json"]`), waitFor(`document.querySelector('`+json+`')?.checkVisibility()`)) put := func(text string) { 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 + `'))`)) } put(bigData(maxSceneData + 1)) se.rig.Run(waitFor(`document.querySelector('[slides-scene-data-big]')?.checkVisibility()`)) var state struct { Length int Invalid string Words string } se.rig.Run(evalInto(&state, `({ Length: document.querySelector('`+json+`').value.length, Invalid: document.querySelector('`+json+`').getAttribute('aria-invalid') || '', Words: document.querySelector('[slides-scene-data-big]').textContent.trim() })`)) if state.Length != maxSceneData+1 || state.Invalid != "true" || state.Words != "This data is over 960 KB, so it wasn't saved." { 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) } if p := pieces(se.scene(t, se.world).Data); len(p) != 2 { t.Errorf("data over the limit was written: %v", se.scene(t, se.world).Data) } se.rig.Screen(`[slides-scene-data]`, "data over the limit") put(bigData(maxSceneData)) se.untilScene(t, se.world, "960 KiB of data", func(s deck.Scene) bool { m, _ := s.Data.(map[string]any) v, _ := m["s"].(string) return len(v) == maxSceneData-8 }) se.rig.Run(waitFor(`!document.querySelector('[slides-scene-data-big]').checkVisibility() && document.querySelector('` + json + `').getAttribute('aria-invalid') !== 'true'`)) } // codePanelJS: the code panel's text, its line, its Try again and Save. const codePanelJS = `(() => { const p = document.querySelector('[slides-scene-code]'), vis = e => !!e && e.checkVisibility() return { Text: p.querySelector('textarea').value, Status: vis(p.querySelector('[slides-scene-status]')) ? p.querySelector('[slides-scene-status]').textContent.trim() : '', Retry: vis(p.querySelector('[slides-scene-retry]')), SaveOff: p.querySelector('[slides-scene-save]').disabled } })()` type codePanel struct { Text, Status string Retry, SaveOff bool } // TestSceneCodeThatDidNotLoadCanBeRetried: when the bundle's text does // not come, the panel says so in the app's words with Try again, and // Save code is off, so an empty field can never be saved as the code; // Try again reads it, and Save is back. func TestSceneCodeThatDidNotLoadCanBeRetried(t *testing.T) { se := openSceneEditor(t, sceneEditorOpts{}) se.selectScene(t) 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)`)) se.rig.Run(clickLabelOf(`input[name="slides-scene-panel"][value="code"]`), waitFor(`document.querySelector('[slides-scene-retry]')?.checkVisibility()`)) var p codePanel se.rig.Run(evalInto(&p, codePanelJS)) if p.Text != "" || p.Status != "That didn’t work" || !p.Retry || !p.SaveOff { t.Errorf("code that did not load: %+v, want no text, That didn’t work, Try again, and Save off", p) } se.rig.Run(chromedp.Evaluate[chromedp.Void](`window.fetch = window.__fetch`), chromedp.Click(chromedp.CSS(`[slides-scene-retry]`)), waitFor(`document.querySelector('[slides-scene-code] textarea').value === `+jsString(editorScene))) p = codePanel{} se.rig.Run(evalInto(&p, codePanelJS)) if p.Status != "" || p.Retry || p.SaveOff { t.Errorf("after Try again: %+v, want the code, no line, and Save on", p) } } // TestASceneAuthorArrivingLiveIsNamed: an agent the page did not know // when it opened changes the code; the author line names it. func TestASceneAuthorArrivingLiveIsNamed(t *testing.T) { se := openSceneEditor(t, sceneEditorOpts{}) se.selectScene(t) se.rig.Run(clickLabelOf(`input[name="slides-scene-panel"][value="code"]`), waitFor(`document.querySelector('[slides-scene-author]').textContent.includes('scene-editor')`)) late := mintAgent(t, se.app, se.cookies, "late-agent") draft := strings.Replace(editorScene, "const MARK = 'saved';", "const MARK = 'late';", 1) rec := agentCall(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/scene-bundles", late, []byte(draft)) if rec.Code != http.StatusOK { t.Fatalf("POST scene-bundles: %d\n%s", rec.Code, rec.Body) } apiJSON(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/ops", late, `{"ops":[{"op":"set_scene","id":"`+se.world+`","code":"`+hashOf([]byte(draft))+`"}]}`, http.StatusOK) se.rig.Run(waitFor(`document.querySelector('[slides-scene-author]').textContent.trim().startsWith('Last changed by') && document.querySelector('[slides-scene-author]').textContent.includes('late-agent')`)) } // TestNewCodeThatDidNotLoadCannotBeSavedOver: the code loads and Save is // on; an agent changes the code while the panel is open, and the new // text does not come: the panel says so, and Save is off, so the old // text cannot be saved over the newer code. func TestNewCodeThatDidNotLoadCannotBeSavedOver(t *testing.T) { se := openSceneEditor(t, sceneEditorOpts{}) se.selectScene(t) se.rig.Run(clickLabelOf(`input[name="slides-scene-panel"][value="code"]`), waitFor(`document.querySelector('[slides-scene-code] textarea').value === `+jsString(editorScene)+` && !document.querySelector('[slides-scene-save]').disabled`)) 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)`)) newer := strings.Replace(editorScene, "const MARK = 'saved';", "const MARK = 'newer';", 1) if rec := agentCall(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/scene-bundles", se.token, []byte(newer)); rec.Code != http.StatusOK { t.Fatalf("POST scene-bundles: %d\n%s", rec.Code, rec.Body) } apiJSON(t, se.app, http.MethodPost, "/api/v1/decks/"+se.sqid()+"/ops", se.token, `{"ops":[{"op":"set_scene","id":"`+se.world+`","code":"`+hashOf([]byte(newer))+`"}]}`, http.StatusOK) se.rig.Run(waitFor(`document.querySelector('[slides-scene-retry]')?.checkVisibility()`)) var p codePanel se.rig.Run(evalInto(&p, codePanelJS)) if p.Status != "That didn’t work" || !p.Retry || !p.SaveOff { t.Errorf("newer code that did not load: %+v, want That didn’t work, Try again, and Save off", p) } se.rig.Run(chromedp.Evaluate[chromedp.Void](`window.fetch = window.__fetch`), chromedp.Click(chromedp.CSS(`[slides-scene-retry]`)), waitFor(`document.querySelector('[slides-scene-code] textarea').value === `+jsString(newer)+` && !document.querySelector('[slides-scene-save]').disabled`)) }