oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package slidestest
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "regexp"
8 "strings"
9 "testing"
10)
11
12// bundleLink finds the editor bundle's script tag on a page: the one
13// committed file under internal/slides/static, served through the
14// fingerprinted asset handler.
15var bundleLink = regexp.MustCompile(`/static/editor\.[0-9a-f]{16}(\.[0-9a-f]{16})?\.js`)
16
17// TestEditorPageLinksTheCommittedBundle: GET /d/{sqid}/edit is the one
18// screen that needs the bundle (spec §8), and the page carries
19// everything the bundle reads back — the deck, the seq, the current
20// slide, the person — as data rather than as script. The strip and the
21// stage are server-rendered too, so the first paint is right before a
22// byte of JS has run.
23func TestEditorPageLinksTheCommittedBundle(t *testing.T) {
24 a := newApp(t)
25 cookies := signedIn(t, a, allowedAddress)
26 path := sampleDeck(t, a, cookies)
27 sqid := strings.TrimPrefix(path, "/d/")
28
29 page := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
30
31 src := bundleLink.FindString(page)
32 if src == "" {
33 t.Fatalf("the editor page links no committed bundle:\n%s", page)
34 }
35 if !strings.Contains(page, `type="module"`) {
36 t.Errorf("the bundle is an ES module and must be loaded as one:\n%s", page)
37 }
38 js := mustGet(t, a, src, http.StatusOK, nil)
39 if ct := js.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/javascript") {
40 t.Errorf("GET %s: Content-Type = %q, want text/javascript", src, ct)
41 }
42 if !strings.Contains(js.Header().Get("Cache-Control"), "immutable") {
43 t.Errorf("GET %s: Cache-Control = %q, want immutable", src, js.Header().Get("Cache-Control"))
44 }
45 for _, css := range []string{"editor", "slides"} {
46 if !regexp.MustCompile(`/static/` + css + `\.[0-9a-f]{16}\.css`).MatchString(page) {
47 t.Errorf("the editor page links no fingerprinted %s.css:\n%s", css, page)
48 }
49 }
50
51 for _, attr := range []string{
52 `data-deck="` + sqid + `"`,
53 `data-seq="`,
54 `data-slide="`,
55 `data-client-name="`,
56 `data-client-colour="`,
57 } {
58 if !strings.Contains(page, attr) {
59 t.Errorf("the editor page carries no %s:\n%s", attr, page)
60 }
61 }
62 if strings.Contains(page, `data-slide=""`) {
63 t.Errorf("the sample deck has slides, so data-slide must name the first one:\n%s", page)
64 }
65
66 // The strip: one <li> per slide, each an <img> of the server's SVG
67 // of it (thumbs.go), the current one saying so in words.
68 // The <template> the bundle clones for a new slide is an item too,
69 // with no slide id; the eight with one are the deck's.
70 if n := len(regexp.MustCompile(`<li slides-strip-item[^>]*\sdata-slide-id="`).FindAllString(page, -1)); n != 8 {
71 t.Errorf("the strip lists %d slides, want the sample deck's 8:\n%s", n, page)
72 }
73 // One section on the page, and it is the stage. The strip used to
74 // carry eight more — a whole live copy of every slide, scaled down
75 // by CSS — and each is now one picture.
76 if n := strings.Count(page, "<section slides-slide"); n != 1 {
77 t.Errorf("the page renders %d sections, want only the stage:\n%s", n, page)
78 }
79 if n := strings.Count(page, `<img slides-thumb-img src="`); n != 8 {
80 t.Errorf("the strip has %d thumbnails with a picture, want the sample deck's 8:\n%s", n, page)
81 }
82 if !strings.Contains(page, `.svg?seq=`) {
83 t.Errorf("a strip thumbnail does not point at the SVG route:\n%s", page)
84 }
85 if !strings.Contains(page, `aria-current="true"`) {
86 t.Errorf("no strip item is marked current:\n%s", page)
87 }
88 if !strings.Contains(page, "Current slide") {
89 t.Errorf("the current slide is not said in words:\n%s", page)
90 }
91 if !strings.Contains(page, sampleSlide2Heading) {
92 t.Errorf("the strip does not label slide 2 by its heading:\n%s", page)
93 }
94
95 // The status line, with every state's text server-rendered from the
96 // catalog so the bundle never carries English.
97 for _, want := range []string{`data-text-saved="Saved"`, `data-text-saving="Saving…"`, `data-text-retrying="Offline, retrying"`, `data-text-failed="Some changes could not be saved. Reload to continue."`, "<noscript>"} {
98 if !strings.Contains(page, want) {
99 t.Errorf("the editor page lacks %s:\n%s", want, page)
100 }
101 }
102 // The viewer by its first slide: the bare address opens this page.
103 if !strings.Contains(page, `href="`+path+`?slide=1"`) {
104 t.Errorf("the noscript fallback does not link the viewer:\n%s", page)
105 }
106 // Every shortcut has a visible menu equivalent.
107 for _, want := range []string{"New slide", "Duplicate slide", `action="` + path + `/present"`} {
108 if !strings.Contains(page, want) {
109 t.Errorf("the editor's menu lacks %s:\n%s", want, page)
110 }
111 }
112}
113
114// TestEditorRequiresMembership: the editor is deckFor's audience — the
115// owner and, on a team deck, every member — and 404 to anybody else, so
116// the sqid of a private deck stays unconfirmed. The viewer offers the
117// Edit button to exactly the people the editor lets in.
118func TestEditorRequiresMembership(t *testing.T) {
119 a := newSharedApp(t)
120 mine := signedIn(t, a, allowedAddress)
121 theirs := signedIn(t, a, otherAddress)
122
123 path := createDeck(t, a, mine, "Board update")
124 mustGet(t, a, path+"/edit", http.StatusNotFound, theirs)
125 mustGet(t, a, path+"/edit", http.StatusOK, mine)
126
127 owner := mustGet(t, a, path+"?slide=1", http.StatusOK, mine).Body.String()
128 if !strings.Contains(owner, `href="`+path+`/edit"`) {
129 t.Errorf("the owner's viewer offers no Edit:\n%s", owner)
130 }
131
132 if rec := post(t, a, path+"/settings", url.Values{
133 "title": {"Board update"}, "theme": {"ink"}, "rung": {"team"},
134 }, mine...); rec.Code != http.StatusSeeOther {
135 t.Fatalf("raising the rung: status %d\n%s", rec.Code, rec.Body)
136 }
137 mustGet(t, a, path+"/edit", http.StatusOK, theirs)
138 member := mustGet(t, a, path+"?slide=1", http.StatusOK, theirs).Body.String()
139 if !strings.Contains(member, `href="`+path+`/edit"`) {
140 t.Errorf("a member's viewer offers no Edit:\n%s", member)
141 }
142
143 // Signed out, the editor is behind the gate like every other
144 // signed-in screen: a redirect to sign in, never the page.
145 if rec := get(t, a, path+"/edit"); rec.Code != http.StatusSeeOther && rec.Code != http.StatusFound {
146 t.Errorf("GET %s/edit signed out: status %d, want a redirect to /signin", path, rec.Code)
147 }
148}
149
150// TestEditorOfAnEmptyDeck: a deck with no slides is a real state (the
151// viewer's own rule), so the editor opens on it with no current slide
152// and a way to make the first one, rather than a 404 or a 500.
153func TestEditorOfAnEmptyDeck(t *testing.T) {
154 a := newApp(t)
155 cookies := signedIn(t, a, allowedAddress)
156 path := createDeck(t, a, cookies, "Blank")
157 sqid := strings.TrimPrefix(path, "/d/")
158
159 // A new deck opens with one slide; an agent takes it away, which
160 // is the only way a deck ends up with none.
161 token := mintAgent(t, a, cookies, "claude")
162 before := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
163 m := regexp.MustCompile(`data-slide="([a-z2-7]{12})"`).FindStringSubmatch(before)
164 if m == nil {
165 t.Fatalf("a new deck's editor names no current slide:\n%s", before)
166 }
167 apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token,
168 `{"ops":[{"op":"remove_slide","id":"`+m[1]+`"}]}`, http.StatusOK)
169
170 page := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
171 if !strings.Contains(page, `data-slide=""`) {
172 t.Errorf("an empty deck's editor names a current slide:\n%s", page)
173 }
174 if !strings.Contains(page, "No slides yet") {
175 t.Errorf("an empty deck's editor does not say so:\n%s", page)
176 }
177 if !strings.Contains(page, "New slide") {
178 t.Errorf("an empty deck's editor offers no way to add a slide:\n%s", page)
179 }
180}
181
182// TestEditorSlideHTMLServesTheThumbnailViewport: ?vp=thumb is the strip's
183// rendering and is the desktop one — a thumbnail is a desktop slide
184// scaled by CSS, never a third rendering.
185func TestEditorSlideHTMLServesTheThumbnailViewport(t *testing.T) {
186 a := newApp(t)
187 cookies := signedIn(t, a, allowedAddress)
188 path := sampleDeck(t, a, cookies)
189
190 page := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
191 m := regexp.MustCompile(`data-slide="([a-z2-7]{12})"`).FindStringSubmatch(page)
192 if m == nil {
193 t.Fatalf("no current slide id on the editor page:\n%s", page)
194 }
195 thumb := mustGet(t, a, path+"/slides/"+m[1]+".html?vp=thumb", http.StatusOK, cookies).Body.String()
196 if !strings.HasPrefix(thumb, "<section slides-slide") {
197 t.Errorf("the thumbnail is not a bare section:\n%s", thumb)
198 }
199 if strings.Contains(thumb, `data-vp="phone"`) {
200 t.Errorf("the thumbnail rendered as a phone:\n%s", thumb)
201 }
202}
203
204// TestEditorInspectorMarkupIsFormsWithLabels: every control in the
205// inspector is a real form control with a name — a <label for> that
206// names its id, or a wrapping <label> (the switch's shape) — whether or
207// not the bundle has it on screen. The scan is a walk of the tags in
208// [slides-inspector] and the Deck settings dialog: a control inside a
209// <label> passes, a control with an id that some <label for> names
210// passes, anything else fails by name. It also checks the three sections are forms and the twelve
211// block types are in the Insert menu (and not the card), the way a keyboard reaches them.
212func TestEditorInspectorMarkupIsFormsWithLabels(t *testing.T) {
213 a := newApp(t)
214 cookies := signedIn(t, a, allowedAddress)
215 path := sampleDeck(t, a, cookies)
216 page := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
217
218 start := strings.Index(page, "<aside slides-inspector")
219 // The inspector is the last pane in <main>; the dialogs after it
220 // (the shortcuts, the link) are forms of their own.
221 end := strings.Index(page, "</main>")
222 if start < 0 || end < start {
223 t.Fatalf("no inspector on the page:\n%s", page)
224 }
225 // The deck's form is the Deck settings dialog's now (spec §8),
226 // after </main>: its controls are held to the same rule.
227 at := strings.Index(page, "<dialog slides-deck-dialog")
228 if at < end {
229 t.Fatalf("no deck settings dialog after </main>:\n%s", page)
230 }
231 card, deck := page[start:end], extent(page, at)
232 inspector := card + deck
233
234 labelFor := map[string]bool{}
235 for _, m := range regexp.MustCompile(`<label[^>]*\sfor="([^"]+)"`).FindAllStringSubmatch(inspector, -1) {
236 labelFor[m[1]] = true
237 }
238 tag := regexp.MustCompile(`<(/?)(label|input|select|textarea)\b([^>]*)>`)
239 idAttr := regexp.MustCompile(`\sid="([^"]+)"`)
240 nameAttr := regexp.MustCompile(`\sname="([^"]+)"`)
241 depth, controls := 0, 0
242 for _, m := range tag.FindAllStringSubmatch(inspector, -1) {
243 closing, name, attrs := m[1] == "/", m[2], m[3]
244 if name == "label" {
245 if closing {
246 depth--
247 } else {
248 depth++
249 }
250 continue
251 }
252 if closing {
253 continue
254 }
255 controls++
256 if depth > 0 {
257 continue
258 }
259 id := idAttr.FindStringSubmatch(attrs)
260 if id == nil {
261 t.Errorf("an inspector <%s> has no label and no id: <%s%s>", name, name, attrs)
262 continue
263 }
264 if !labelFor[id[1]] {
265 t.Errorf("no <label for=%q> names the inspector's <%s%s>", id[1], name, attrs)
266 }
267 if n := nameAttr.FindStringSubmatch(attrs); n == nil {
268 t.Errorf("the inspector's %s has no name", id[1])
269 }
270 }
271 if controls < 30 {
272 t.Errorf("the inspector holds %d controls, want the deck, slide and block sections' worth", controls)
273 }
274 for _, section := range []string{"deck", "slide", "block"} {
275 if !strings.Contains(inspector, `data-section="`+section+`"`) {
276 t.Errorf("the inspector has no form for the %s section", section)
277 }
278 }
279 if n := strings.Count(card, "<form "); n != 2 {
280 t.Errorf("the card holds %d forms, want 2 (the slide's, the box's)", n)
281 }
282 if n := strings.Count(deck, "<form "); n != 1 || !strings.Contains(deck, `data-section="deck"`) {
283 t.Errorf("the deck dialog holds %d forms, want the deck's alone", n)
284 }
285 if n := strings.Count(page, "slides-inspector-form"); n != 3 {
286 t.Errorf("the page holds %d slides-inspector-form forms, want 3", n)
287 }
288 // A box is added from the Insert menu, the one place (Q5, spec §5):
289 // its twelve rows, and none in the card.
290 insert := regexp.MustCompile(`(?s)<details[^>]*slides-menu="insert".*?</details>`).FindString(page)
291 for _, kind := range everyKind {
292 row := `data-insert-block="` + kind + `"`
293 if !strings.Contains(insert, row) {
294 t.Errorf("the Insert menu lists no %s", kind)
295 }
296 if strings.Contains(inspector, row) || strings.Contains(inspector, `data-add-block="`+kind+`"`) {
297 t.Errorf("the card still adds a %s", kind)
298 }
299 }
300 // The strip's menu, the switcher and the canvas overlay carry their
301 // words from the catalog too.
302 for _, want := range []string{
303 `data-slide-command="move-up"`, `name="slides-view" value="phone"`, `data-text-phone-override="Phone override"`,
304 `data-type-heading="Heading"`, `<template slides-strip-item-template>`,
305 } {
306 if !strings.Contains(page, want) {
307 t.Errorf("the editor page lacks %s:\n%s", want, page)
308 }
309 }
310}
311
312// TestEditorSlideHTMLAnswers304ToItsOwnETag: the stage sends the tag
313// back on its next fetch, and an unchanged slide costs the server no
314// document open. A different seq is a different tag.
315func TestEditorSlideHTMLAnswers304ToItsOwnETag(t *testing.T) {
316 a := newApp(t)
317 cookies := signedIn(t, a, allowedAddress)
318 path := sampleDeck(t, a, cookies)
319 page := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
320 m := regexp.MustCompile(`data-slide="([a-z2-7]{12})"`).FindStringSubmatch(page)
321 if m == nil {
322 t.Fatalf("no current slide id on the editor page:\n%s", page)
323 }
324 target := path + "/slides/" + m[1] + ".html?vp=desktop"
325 first := mustGet(t, a, target, http.StatusOK, cookies)
326 etag := first.Header().Get("ETag")
327 if etag == "" {
328 t.Fatalf("the slide HTML carries no ETag")
329 }
330
331 req := httptest.NewRequest(http.MethodGet, target, nil)
332 for _, c := range cookies {
333 req.AddCookie(c)
334 }
335 req.Header.Set("If-None-Match", etag)
336 rec := httptest.NewRecorder()
337 a.ServeHTTP(rec, req)
338 if rec.Code != http.StatusNotModified {
339 t.Errorf("GET with the slide's own ETag: status %d, want 304", rec.Code)
340 }
341 if rec.Body.Len() != 0 {
342 t.Errorf("a 304 carried a body:\n%s", rec.Body)
343 }
344 // The phone rendering of the same slide at the same seq is other
345 // bytes, and says so.
346 phone := mustGet(t, a, path+"/slides/"+m[1]+".html?vp=phone", http.StatusOK, cookies)
347 if phone.Header().Get("ETag") == etag {
348 t.Errorf("the phone rendering shares the desktop's ETag %s", etag)
349 }
350}
351
352// TestEditorOfAnEmptyDeckHasAStage: the stage and the notes pane are on
353// the page whether or not there is a slide, because New slide loads
354// the first slide INTO the stage; the empty state sits inside it until
355// then (task 3 review round 1, finding 1).
356func TestEditorOfAnEmptyDeckHasAStage(t *testing.T) {
357 a := newApp(t)
358 cookies := signedIn(t, a, allowedAddress)
359 path := createDeck(t, a, cookies, "Blank")
360 sqid := strings.TrimPrefix(path, "/d/")
361 token := mintAgent(t, a, cookies, "claude")
362 before := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
363 m := regexp.MustCompile(`data-slide="([a-z2-7]{12})"`).FindStringSubmatch(before)
364 if m == nil {
365 t.Fatalf("a new deck's editor names no current slide:\n%s", before)
366 }
367 apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token,
368 `{"ops":[{"op":"remove_slide","id":"`+m[1]+`"}]}`, http.StatusOK)
369
370 page := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String()
371 stage := regexp.MustCompile(`(?s)<div slides-stage data-vp="desktop">(.*?)</div>\s*<div slides-stage-phone`).FindStringSubmatch(page)
372 if stage == nil {
373 t.Fatalf("an empty deck's editor has no [slides-stage]:\n%s", page)
374 }
375 if !strings.Contains(stage[1], "No slides yet") {
376 t.Errorf("the empty state is not inside the stage:\n%s", stage[1])
377 }
378 if !strings.Contains(page, "<div slides-notes ") {
379 t.Errorf("an empty deck's editor has no notes element:\n%s", page)
380 }
381}
382