//go:build browser package slidestest import ( "net/http" "regexp" "strings" "testing" "github.com/chromedp/chromedp" ) // TestAddingAFollowAlongCodeMakesALink: Insert → Follow-along code puts // a code on the slide that already holds a new audience link — the // share page lists it, named Audience; the box draws the code and the // short address; its card shows the follow address and offers a new // one — and the address, opened in a second browser with no session // while the deck is presented, is the follow page, the code on it. func TestAddingAFollowAlongCodeMakesALink(t *testing.T) { rig, app := newRigApp(t, nil) browserSignIn(t, rig, app.mailer) var deck string rig.Run(evalInto(&deck, `(document.querySelector('a[href^="/d/"]')?.getAttribute('href') ?? '').replace(/\/edit$/, '')`)) rig.Run( chromedp.Navigate(rig.Origin+deck+"/edit"), chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), waitFor(`document.querySelector('[slides-stage] [data-block="heading"]')?.getAttribute('contenteditable') === 'true'`), chromedp.Click(chromedp.CSS(`[slides-menu="insert"] > summary`)), waitFor(`document.querySelector('[slides-menu="insert"]')?.open === true`), ) var label string rig.Run(evalInto(&label, `document.querySelector('[data-insert-block="qr"]')?.textContent.trim() ?? ''`)) if label != "Follow-along code" { t.Errorf("the Insert menu's row says %q, want Follow-along code", label) } rig.Run( chromedp.Click(chromedp.CSS(`[data-insert-block="qr"]`)), waitFor(`document.querySelector('[slides-stage] [data-block="qr"] svg path') !== null`), waitFor(`document.querySelector('[estilo-save-status]').dataset.state === 'saved'`), ) rig.Screen("[slides-editor]", "a follow-along code added") var follow, caption, alt string rig.Run( evalInto(&follow, `document.querySelector('#rst-block-qr-url')?.value ?? ''`), evalInto(&caption, `document.querySelector('[slides-stage] [data-block="qr"] figcaption')?.textContent ?? ''`), evalInto(&alt, `document.querySelector('[slides-stage] [data-block="qr"] svg')?.getAttribute('aria-label') ?? ''`), ) m := regexp.MustCompile(`^` + regexp.QuoteMeta(rig.Origin) + `/s/([A-Za-z0-9_-]{43})\?follow=1$`).FindStringSubmatch(follow) if m == nil { t.Fatalf("the card's share link is %q, want %s/s/?follow=1", follow, rig.Origin) } token := m[1] address := strings.TrimPrefix(strings.TrimPrefix(rig.Origin, "http://"), "https://") + "/s/" + token if caption != address { t.Errorf("the code prints %q, want %q", caption, address) } if alt != "QR code to follow this talk at "+address { t.Errorf("the code's alt text is %q", alt) } var visible bool rig.Run(evalInto(&visible, `(() => { const b = document.querySelector('[data-command="qr-link"]'); const f = document.querySelector('#rst-block-qr-url'); return !!b && !!f && b.offsetParent !== null && f.offsetParent !== null && f.readOnly && b.textContent.trim() === 'Make an audience link'; })()`)) if !visible { t.Error("the card does not show the read-only share link and Make an audience link") } // The share page lists the link, by name. rig.Run( chromedp.Navigate(rig.Origin+deck+"/share"), chromedp.WaitVisible(chromedp.CSS(`[slides-share-links]`)), ) var rows string rig.Run(evalInto(&rows, `document.querySelector('[slides-share-links] [rst-list]')?.textContent ?? ''`)) if !strings.Contains(rows, "Audience") { t.Errorf("the share page does not list the audience link:\n%s", rows) } // Present the deck, then open the address in a second browser that // has never signed in: the follow page, with the code on its slide. cookies := signedIn(t, app, allowedAddress) if rec := post(t, app, deck+"/present", nil, cookies...); rec.Code != http.StatusSeeOther { t.Fatalf("POST %s/present: status %d", deck, rec.Code) } stranger, cancel := chromedp.NewContext(rig.Context(), chromedp.WithNewBrowserContext()) defer cancel() var surface string runOn(t, stranger, chromedp.Navigate(follow), chromedp.WaitVisible(chromedp.CSS(`[slides-surface]`)), waitFor(`document.querySelector('[slides-stage] [data-block="qr"] figcaption')?.textContent === `+jsString(address)), evalInto(&surface, `location.pathname + location.search`), ) if surface != "/s/"+token+"?follow=1" { t.Errorf("the second browser is at %q, want the follow page", surface) } }