| 1 | package slidestest |
| 2 | |
| 3 | // Editing a scene by hand, the server's half (scenes and clips spec §5; |
| 4 | // templates/editor.html's scene group, editor.go): the words are Paul's |
| 5 | // approved ones, the Insert menu's Scene row waits for the deck to have |
| 6 | // a scene, and the out-of-date note's Ask an agent goes to the |
| 7 | // assistant where there is one and to the Agents page where there is |
| 8 | // not. The browser's half is scenes_editor_browser_test.go. |
| 9 | |
| 10 | import ( |
| 11 | "encoding/json" |
| 12 | "net/http" |
| 13 | "os" |
| 14 | "path/filepath" |
| 15 | "regexp" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | ) |
| 19 | |
| 20 | // editorScene is the scene the editor's tests edit. It answers ready to |
| 21 | // every init (a preview, or a frame made afresh, is a document of its |
| 22 | // own) and tells the page, under a name of its own, what each message |
| 23 | // said: the state, its words' title, its data as JSON and its MARK, the |
| 24 | // line the code panel's tests change. |
| 25 | const editorScene = `<!doctype html> |
| 26 | <html><head><meta charset="utf-8"><style>html,body{margin:0;height:100%;background:#2471a3}</style></head> |
| 27 | <body><script> |
| 28 | const MARK = 'saved'; |
| 29 | const doc = Math.random().toString(36).slice(2); |
| 30 | const log = []; |
| 31 | const tell = () => parent.postMessage({type: 'seen', doc, log}, '*'); |
| 32 | addEventListener('message', (e) => { |
| 33 | if (e.source !== parent || !e.data) return; |
| 34 | const m = e.data; |
| 35 | log.push({type: m.type, state: m.state, mark: MARK, title: m.words ? m.words.title : '', |
| 36 | data: m.data === undefined ? '' : JSON.stringify(m.data)}); |
| 37 | if (m.type === 'init') parent.postMessage({type: 'ready'}, '*'); |
| 38 | tell(); |
| 39 | }); |
| 40 | setInterval(tell, 150); |
| 41 | </script></body></html> |
| 42 | ` |
| 43 | |
| 44 | // sceneEditorWords are the scene card's words as Paul approved them |
| 45 | // (copy review, 2026-11-24), by catalog key. |
| 46 | var sceneEditorWords = map[string]string{ |
| 47 | "slides.editor.block.scene": "Scene", |
| 48 | "slides.editor.scene.from": "From", |
| 49 | "slides.editor.scene.to": "To", |
| 50 | "slides.editor.scene.words": "Words in this scene", |
| 51 | "slides.editor.scene.stale": "These pictures are older than the scene.", |
| 52 | "slides.editor.scene.stale.ask": "Ask an agent to redraw them", |
| 53 | "slides.editor.scene.missing": "{n} states have no picture yet.", |
| 54 | "slides.editor.scene.missing.one": "1 state has no picture yet.", |
| 55 | "slides.editor.scene.data": "Data", |
| 56 | "slides.editor.scene.data.table": "Table", |
| 57 | "slides.editor.scene.data.json": "JSON", |
| 58 | "slides.editor.scene.data.add": "Add a row", |
| 59 | "slides.editor.scene.data.remove": "Remove row", |
| 60 | "slides.editor.scene.data.invalid": "That isn't valid JSON.", |
| 61 | "slides.editor.scene.data.big": "This data is over 960 KB, so it wasn't saved.", |
| 62 | "slides.editor.scene.missing.list": "Scenes still missing pictures", |
| 63 | "slides.editor.scene.code": "Code", |
| 64 | "slides.editor.scene.code.preview": "Preview", |
| 65 | "slides.editor.scene.code.save": "Save code", |
| 66 | "slides.editor.scene.code.author": "Last changed by {name}", |
| 67 | "slides.refused.scene": "{name} isn't a single HTML page.", |
| 68 | "slides.refused.scene.big": "{name} is over 2 MB.", |
| 69 | } |
| 70 | |
| 71 | // TestTheSceneCardSaysPaulsWords: every key is in en.toml with the |
| 72 | // approved English, and every editor key is listed in editorStrings. |
| 73 | func TestTheSceneCardSaysPaulsWords(t *testing.T) { |
| 74 | toml := readSource(t, "locales/en.toml") |
| 75 | listed := map[string]bool{} |
| 76 | for _, k := range goStrings(t, readSource(t, "editor.go"), "editorStrings") { |
| 77 | listed[k] = true |
| 78 | } |
| 79 | for key, english := range sceneEditorWords { |
| 80 | m := regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(key) + ` = "(.*)"$`).FindStringSubmatch(toml) |
| 81 | if m == nil { |
| 82 | t.Errorf("en.toml has no %s", key) |
| 83 | } else if got := strings.ReplaceAll(m[1], `\"`, `"`); got != english { |
| 84 | t.Errorf("%s is %q, want Paul's %q", key, got, english) |
| 85 | } |
| 86 | if short, ok := strings.CutPrefix(key, "slides.editor."); ok && !listed[short] { |
| 87 | t.Errorf("editorStrings does not list %s", short) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // sceneRowRE is the Insert menu's Scene row. |
| 93 | var sceneRowRE = regexp.MustCompile(`<button estilo-menu-item type="button" data-insert-scene[^>]*>`) |
| 94 | |
| 95 | // TestTheSceneRowWaitsForAScene: Insert → Scene is in every editor's |
| 96 | // Insert menu, named Scene, and disabled until the deck has a scene to |
| 97 | // show (agents make scenes; the row only places one). |
| 98 | func TestTheSceneRowWaitsForAScene(t *testing.T) { |
| 99 | a := newAppWithStore(t, dirStore(t)) |
| 100 | cookies := signedIn(t, a, allowedAddress) |
| 101 | page := mustGet(t, a, deckWithSlides(t, a, cookies, 2), http.StatusOK, cookies).Body.String() |
| 102 | row := sceneRowRE.FindString(page) |
| 103 | if row == "" { |
| 104 | t.Fatal("the Insert menu has no Scene row") |
| 105 | } |
| 106 | if !strings.Contains(row, " disabled") { |
| 107 | t.Errorf("a deck with no scene offers Insert → Scene: %s", row) |
| 108 | } |
| 109 | if !strings.Contains(page, row+`<span estilo-menu-label>Scene</span>`) { |
| 110 | t.Errorf("the Scene row is not named Scene") |
| 111 | } |
| 112 | |
| 113 | token := mintAgent(t, a, cookies, "scene-row") |
| 114 | sqid := apiDeck(t, a, token, "With a scene") |
| 115 | addScene(t, a, token, sqid, uploadScene(t, a, "/d/"+sqid, cookies, []byte(editorScene))) |
| 116 | page = mustGet(t, a, "/d/"+sqid+"/edit", http.StatusOK, cookies).Body.String() |
| 117 | if row := sceneRowRE.FindString(page); row == "" || strings.Contains(row, " disabled") { |
| 118 | t.Errorf("a deck with a scene does not offer Insert → Scene: %q", row) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // TestAskingForNewPicturesGoesToAnAgent: Ask an agent to redraw them |
| 123 | // is a link to the Agents page, in a tab of its own, with the assistant |
| 124 | // on or off: no agent inside the editor can draw a still. |
| 125 | func TestAskingForNewPicturesGoesToAnAgent(t *testing.T) { |
| 126 | ask := regexp.MustCompile(`<(a|button) [^>]*slides-scene-ask[^>]*>`) |
| 127 | link := `<a slides-card-row slides-scene-ask href="/agents" target="_blank" rel="noopener">` |
| 128 | |
| 129 | a := newApp(t) |
| 130 | cookies := signedIn(t, a, allowedAddress) |
| 131 | page := mustGet(t, a, deckWithSlides(t, a, cookies, 1), http.StatusOK, cookies).Body.String() |
| 132 | if got := ask.FindAllString(page, -1); len(got) != 1 || got[0] != link { |
| 133 | t.Errorf("with no assistant, Ask an agent is %q, want %s", got, link) |
| 134 | } |
| 135 | |
| 136 | b, _ := newAssistApp(t) |
| 137 | cookies = signedIn(t, b, allowedAddress) |
| 138 | page = mustGet(t, b, createDeck(t, b, cookies, "Asked")+"/edit", http.StatusOK, cookies).Body.String() |
| 139 | if got := ask.FindAllString(page, -1); len(got) != 1 || got[0] != link { |
| 140 | t.Errorf("with the assistant, Ask an agent is %q, want %s", got, link) |
| 141 | } |
| 142 | if !strings.Contains(page, "Ask an agent to redraw them") { |
| 143 | t.Error("the editor never says Ask an agent to redraw them") |
| 144 | } |
| 145 | if src := readFixture(t, "scene-card.mjs"); strings.Contains(src, "slides-assist") { |
| 146 | t.Error("scene-card.mjs still opens the assistant, which cannot draw a still") |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // TestEveryInsertControlIsCounted: the Insert menu's controls are its |
| 151 | // layouts, the twelve kinds of box, Scene and Clip, each one of them, |
| 152 | // and nothing else. |
| 153 | func TestEveryInsertControlIsCounted(t *testing.T) { |
| 154 | a := newApp(t) |
| 155 | cookies := signedIn(t, a, allowedAddress) |
| 156 | page := mustGet(t, a, deckWithSlides(t, a, cookies, 1), http.StatusOK, cookies).Body.String() |
| 157 | menu := regexp.MustCompile(`(?s)<details[^>]*slides-menu="insert".*?</details>`).FindString(page) |
| 158 | var layouts, kinds, scenes, clips, other []string |
| 159 | for _, b := range regexp.MustCompile(`<button [^>]*>`).FindAllString(menu, -1) { |
| 160 | switch { |
| 161 | case strings.Contains(b, "data-layout-choice="): |
| 162 | layouts = append(layouts, b) |
| 163 | case strings.Contains(b, "data-insert-block="): |
| 164 | kinds = append(kinds, regexp.MustCompile(`data-insert-block="([^"]+)"`).FindStringSubmatch(b)[1]) |
| 165 | case strings.Contains(b, "data-insert-scene"): |
| 166 | scenes = append(scenes, b) |
| 167 | case strings.Contains(b, "data-insert-clip"): |
| 168 | clips = append(clips, b) |
| 169 | default: |
| 170 | other = append(other, b) |
| 171 | } |
| 172 | } |
| 173 | if len(layouts) != 8 || strings.Join(kinds, " ") != strings.Join(everyKind, " ") || len(scenes) != 1 || len(clips) != 1 || len(other) != 0 { |
| 174 | t.Errorf("Insert holds %d layouts, kinds %v, %d Scene rows, %d Clip rows and %d others %q; want 8, the twelve, one, one and none", |
| 175 | len(layouts), kinds, len(scenes), len(clips), len(other), other) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // TestSceneAuthorsAreNamedOnAsking: GET /d/{sqid}/scene-authors names |
| 180 | // every scene author of the deck as the deck list does, and the reader; |
| 181 | // it is the deck's own, a stranger's 404. |
| 182 | func TestSceneAuthorsAreNamedOnAsking(t *testing.T) { |
| 183 | cfg, mailer := newConfig(t) |
| 184 | cfg.AllowedAddresses = []string{allowedAddress, otherAddress} |
| 185 | cfg.Blobs = dirStore(t) |
| 186 | a := newAppWith(t, cfg, mailer) |
| 187 | cookies := signedIn(t, a, allowedAddress) |
| 188 | token := mintAgent(t, a, cookies, "named-agent") |
| 189 | sqid := apiDeck(t, a, token, "Authors") |
| 190 | addScene(t, a, token, sqid, uploadScene(t, a, "/d/"+sqid, cookies, []byte(editorScene))) |
| 191 | rec := mustGet(t, a, "/d/"+sqid+"/scene-authors", http.StatusOK, cookies) |
| 192 | var got map[string]string |
| 193 | if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil { |
| 194 | t.Fatalf("not a JSON map: %v\n%s", err, rec.Body) |
| 195 | } |
| 196 | var agent, person bool |
| 197 | for actor, label := range got { |
| 198 | agent = agent || (strings.HasPrefix(actor, "agent:") && strings.Contains(label, "named-agent")) |
| 199 | person = person || (strings.HasPrefix(actor, "person:") && label == allowedAddress) |
| 200 | } |
| 201 | if !agent || !person { |
| 202 | t.Errorf("scene-authors answered %v, want the agent named-agent and the reader", got) |
| 203 | } |
| 204 | stranger := signedIn(t, a, otherAddress) |
| 205 | mustGet(t, a, "/d/"+sqid+"/scene-authors", http.StatusNotFound, stranger) |
| 206 | } |
| 207 | |
| 208 | // TestDeckSettingsHasTheScenesMissingPictures: the deck dialog holds the |
| 209 | // list of scenes whose pictures are missing or older than them, hidden |
| 210 | // until the bundle fills it, and no button of its own. |
| 211 | func TestDeckSettingsHasTheScenesMissingPictures(t *testing.T) { |
| 212 | a := newApp(t) |
| 213 | cookies := signedIn(t, a, allowedAddress) |
| 214 | page := mustGet(t, a, deckWithSlides(t, a, cookies, 1), http.StatusOK, cookies).Body.String() |
| 215 | dialog := extent(page, strings.Index(page, "<dialog slides-deck-dialog")) |
| 216 | at := strings.Index(dialog, "<fieldset slides-scene-stills hidden>") |
| 217 | if at < 0 { |
| 218 | t.Fatalf("the deck dialog has no <fieldset slides-scene-stills hidden>:\n%s", dialog) |
| 219 | } |
| 220 | if list := extent(dialog, at); !strings.Contains(list, "<legend>") || !strings.Contains(list, "<ul slides-scene-stills-list></ul>") || strings.Contains(list, "<button") { |
| 221 | t.Errorf("the list is %s", list) |
| 222 | } |
| 223 | if !strings.Contains(readSource(t, "templates/editor.html"), `<legend>{{.L "scene.missing.list"}}</legend>`) { |
| 224 | t.Error("the list is not headed by slides.editor.scene.missing.list") |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // readFixture is fixtures/<name>, the bundle's source. |
| 229 | func readFixture(t *testing.T, name string) string { |
| 230 | t.Helper() |
| 231 | b, err := os.ReadFile(filepath.Join("..", "..", "fixtures", name)) |
| 232 | if err != nil { |
| 233 | t.Fatal(err) |
| 234 | } |
| 235 | return string(b) |
| 236 | } |
| 237 | |
| 238 | // TestEveryNewBlockTypeIsNamed: the outline and stage focus name a box |
| 239 | // by its type's label, read from the editor's data-type-*: a scene, a |
| 240 | // clip and a follow-along code each have theirs, never the raw type. |
| 241 | func TestEveryNewBlockTypeIsNamed(t *testing.T) { |
| 242 | a := newApp(t) |
| 243 | cookies := signedIn(t, a, allowedAddress) |
| 244 | page := mustGet(t, a, deckWithSlides(t, a, cookies, 1), http.StatusOK, cookies).Body.String() |
| 245 | for _, want := range []string{`data-type-scene="Scene"`, `data-type-clip="Clip"`, `data-type-qr="Follow-along code"`} { |
| 246 | if !strings.Contains(page, want) { |
| 247 | t.Errorf("the editor has no %s", want) |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |