| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/chromedp/chromedp" |
| 12 | ) |
| 13 | |
| 14 | // TestAddingAFollowAlongCodeMakesALink: Insert → Follow-along code puts |
| 15 | // a code on the slide that already holds a new audience link — the |
| 16 | // share page lists it, named Audience; the box draws the code and the |
| 17 | // short address; its card shows the follow address and offers a new |
| 18 | // one — and the address, opened in a second browser with no session |
| 19 | // while the deck is presented, is the follow page, the code on it. |
| 20 | func TestAddingAFollowAlongCodeMakesALink(t *testing.T) { |
| 21 | rig, app := newRigApp(t, nil) |
| 22 | browserSignIn(t, rig, app.mailer) |
| 23 | var deck string |
| 24 | rig.Run(evalInto(&deck, `(document.querySelector('a[href^="/d/"]')?.getAttribute('href') ?? '').replace(/\/edit$/, '')`)) |
| 25 | |
| 26 | rig.Run( |
| 27 | chromedp.Navigate(rig.Origin+deck+"/edit"), |
| 28 | chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), |
| 29 | waitFor(`document.querySelector('[slides-stage] [data-block="heading"]')?.getAttribute('contenteditable') === 'true'`), |
| 30 | chromedp.Click(chromedp.CSS(`[slides-menu="insert"] > summary`)), |
| 31 | waitFor(`document.querySelector('[slides-menu="insert"]')?.open === true`), |
| 32 | ) |
| 33 | var label string |
| 34 | rig.Run(evalInto(&label, `document.querySelector('[data-insert-block="qr"]')?.textContent.trim() ?? ''`)) |
| 35 | if label != "Follow-along code" { |
| 36 | t.Errorf("the Insert menu's row says %q, want Follow-along code", label) |
| 37 | } |
| 38 | rig.Run( |
| 39 | chromedp.Click(chromedp.CSS(`[data-insert-block="qr"]`)), |
| 40 | waitFor(`document.querySelector('[slides-stage] [data-block="qr"] svg path') !== null`), |
| 41 | waitFor(`document.querySelector('[estilo-save-status]').dataset.state === 'saved'`), |
| 42 | ) |
| 43 | rig.Screen("[slides-editor]", "a follow-along code added") |
| 44 | |
| 45 | var follow, caption, alt string |
| 46 | rig.Run( |
| 47 | evalInto(&follow, `document.querySelector('#rst-block-qr-url')?.value ?? ''`), |
| 48 | evalInto(&caption, `document.querySelector('[slides-stage] [data-block="qr"] figcaption')?.textContent ?? ''`), |
| 49 | evalInto(&alt, `document.querySelector('[slides-stage] [data-block="qr"] svg')?.getAttribute('aria-label') ?? ''`), |
| 50 | ) |
| 51 | m := regexp.MustCompile(`^` + regexp.QuoteMeta(rig.Origin) + `/s/([A-Za-z0-9_-]{43})\?follow=1$`).FindStringSubmatch(follow) |
| 52 | if m == nil { |
| 53 | t.Fatalf("the card's share link is %q, want %s/s/<token>?follow=1", follow, rig.Origin) |
| 54 | } |
| 55 | token := m[1] |
| 56 | address := strings.TrimPrefix(strings.TrimPrefix(rig.Origin, "http://"), "https://") + "/s/" + token |
| 57 | if caption != address { |
| 58 | t.Errorf("the code prints %q, want %q", caption, address) |
| 59 | } |
| 60 | if alt != "QR code to follow this talk at "+address { |
| 61 | t.Errorf("the code's alt text is %q", alt) |
| 62 | } |
| 63 | var visible bool |
| 64 | rig.Run(evalInto(&visible, `(() => { |
| 65 | const b = document.querySelector('[data-command="qr-link"]'); |
| 66 | const f = document.querySelector('#rst-block-qr-url'); |
| 67 | return !!b && !!f && b.offsetParent !== null && f.offsetParent !== null && f.readOnly && b.textContent.trim() === 'Make an audience link'; |
| 68 | })()`)) |
| 69 | if !visible { |
| 70 | t.Error("the card does not show the read-only share link and Make an audience link") |
| 71 | } |
| 72 | |
| 73 | // The share page lists the link, by name. |
| 74 | rig.Run( |
| 75 | chromedp.Navigate(rig.Origin+deck+"/share"), |
| 76 | chromedp.WaitVisible(chromedp.CSS(`[slides-share-links]`)), |
| 77 | ) |
| 78 | var rows string |
| 79 | rig.Run(evalInto(&rows, `document.querySelector('[slides-share-links] [rst-list]')?.textContent ?? ''`)) |
| 80 | if !strings.Contains(rows, "Audience") { |
| 81 | t.Errorf("the share page does not list the audience link:\n%s", rows) |
| 82 | } |
| 83 | |
| 84 | // Present the deck, then open the address in a second browser that |
| 85 | // has never signed in: the follow page, with the code on its slide. |
| 86 | cookies := signedIn(t, app, allowedAddress) |
| 87 | if rec := post(t, app, deck+"/present", nil, cookies...); rec.Code != http.StatusSeeOther { |
| 88 | t.Fatalf("POST %s/present: status %d", deck, rec.Code) |
| 89 | } |
| 90 | stranger, cancel := chromedp.NewContext(rig.Context(), chromedp.WithNewBrowserContext()) |
| 91 | defer cancel() |
| 92 | var surface string |
| 93 | runOn(t, stranger, |
| 94 | chromedp.Navigate(follow), |
| 95 | chromedp.WaitVisible(chromedp.CSS(`[slides-surface]`)), |
| 96 | waitFor(`document.querySelector('[slides-stage] [data-block="qr"] figcaption')?.textContent === `+jsString(address)), |
| 97 | evalInto(&surface, `location.pathname + location.search`), |
| 98 | ) |
| 99 | if surface != "/s/"+token+"?follow=1" { |
| 100 | t.Errorf("the second browser is at %q, want the follow page", surface) |
| 101 | } |
| 102 | } |
| 103 | |