// fixtures/engines/share-pop.mjs — the editor's Share popover // (brand/share-pop.js), in Chromium and WebKit, through the real server. // // Run by internal/docs/share_pop_browser_test.go with the same // DOCS_ENGINES_CONFIG as first-reveal.mjs: "simple" documents owned by // the signed-in person, two per engine (desk and phone). // // Desk (1280px): Share opens a panel under the button without leaving // the page, focus in the email field; Share again changes nothing (no // flash: the same panel element stays); an invite lands and the person // is listed; the team switch and the link switch each move their rung, // the link's panel carrying the one-time URL; removing the person drops // them; a pointerdown outside closes it; Esc closes it and gives focus // back to Share. Phone (390px): Share is in estilo's Menu sheet, opens a // modal sheet, and Esc gives focus to the Menu trigger. const { chromium, webkit } = await import('playwright') const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG) const pause = (ms) => new Promise((r) => setTimeout(r, ms)) const failures = [] for (const engine of [chromium, webkit]) { const name = engine.name() const fail = (msg) => failures.push(`${name}: ${msg}`) const browser = await engine.launch({ headless: true }) const until = async (fn, ms = 4000) => { const end = Date.now() + ms while (Date.now() < end) { if (await fn()) return true; await pause(50) } return fn() } // Desk. { const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) const page = await context.newPage() const doc = config.docs.simple.shift() const url = config.origin + '/d/' + doc await page.goto(url) await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) const share = page.locator('[estilo-bar-actions] a[href$="/share"]') const panel = () => page.locator('[docs-share-pop] [docs-share-panel]') const open = () => page.evaluate(() => document.querySelector('[docs-share-pop]')?.matches(':popover-open') ?? false) const people = () => page.locator('[docs-share-people] li').allTextContents() await share.click() if (!(await until(async () => (await panel().count()) === 1))) fail('Share did not open the panel') if (page.url() !== url) fail(`Share navigated to ${page.url()}`) if (!(await until(() => page.evaluate(() => document.activeElement?.matches('[docs-share-invite] input[type=email]'))))) fail('focus is not in the email field') const box = await page.locator('[docs-share-pop]').boundingBox() const sbox = await share.boundingBox() if (!box || box.y < sbox.y + sbox.height || Math.abs(box.x + box.width - (sbox.x + sbox.width)) > 2) fail(`the panel is not under Share, end-aligned: panel ${JSON.stringify(box)}, Share ${JSON.stringify(sbox)}`) // Share while open: nothing (the same panel element stays). await page.evaluate(() => { window.__panel = document.querySelector('[docs-share-panel]') }) await share.click() await pause(300) if (!(await open())) fail('Share while open closed the panel') if (!(await page.evaluate(() => document.querySelector('[docs-share-panel]') === window.__panel))) fail('Share while open redrew the panel (a flash)') // Invite. await page.fill('[docs-share-invite] input[type=email]', 'pat@example.com') await page.selectOption('[docs-share-invite] select', 'writer') await page.click('[docs-share-submit]') if (!(await until(async () => (await people()).some((t) => t.includes('pat@example.com'))))) fail('the invited person is not listed') if (page.url() !== url) fail('the invite navigated') // The two switches. for (const [i, what] of [[0, 'team'], [1, 'link']]) { const sw = page.locator('[docs-share-rung]').nth(i).locator('input[type=checkbox]') const before = await sw.isChecked() await page.locator('[docs-share-rung]').nth(i).locator('[rst-switch]').click() const moved = await until(async () => (await page.locator('[docs-share-rung]').nth(i).locator('input[type=checkbox]').isChecked()) !== before && !(await page.locator('[docs-share-pop] [aria-busy="true"]').count())) if (!moved) fail(`the ${what} switch did not stay moved`) } // The link was just turned on (new documents are unpublished): the // panel carries the URL once. const link = await page.locator('[docs-share-link] input').inputValue().catch(() => '') if (!/\/p\/[A-Za-z0-9_-]{16,}/.test(link)) fail(`publishing from the panel did not show the link: ${JSON.stringify(link)}`) // Remove. await page.locator('[docs-share-people] li', { hasText: 'pat@example.com' }).locator('[docs-share-remove]').click() if (!(await until(async () => !(await people()).some((t) => t.includes('pat@example.com'))))) fail('removing the person did not drop them') // Outside closes; Esc closes and returns focus to Share. await page.mouse.click(640, 700) if (!(await until(async () => !(await open())))) fail('a click outside did not close the panel') await share.click() await until(async () => (await panel().count()) === 1) await page.keyboard.press('Escape') if (!(await until(async () => !(await open())))) fail('Esc did not close the panel') if (!(await page.evaluate(() => document.activeElement?.matches('[estilo-bar-actions] a[href$="/share"]')))) fail('Esc did not return focus to Share') await context.close() } // One request at a time (review on #106): while a switch's POST is held // back, flipping the other switch puts it straight back and sends // nothing; past 500ms the waiting switch says aria-busy; the answer // redraws the panel. Then a POST that fails: the switch goes back and // the panel says so. { const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) const page = await context.newPage() await page.goto(config.origin + '/d/' + config.docs.simple.shift()) await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) let mode = 'slow' let posts = 0 let finished = 0 let postAt = 0 page.on('requestfinished', (r) => { if (r.method() === 'POST') finished++ }) page.on('requestfailed', (r) => { if (r.method() === 'POST') finished++ }) await page.route((u) => u.pathname.includes('/share'), async (route) => { if (route.request().method() !== 'POST') return route.continue() posts++ if (!postAt) postAt = Date.now() if (mode === 'abort') return route.abort() await pause(1500) return route.continue() }) await page.click('[estilo-bar-actions] a[href$="/share"]') await until(async () => (await page.locator('[docs-share-pop] [docs-share-panel]').count()) === 1) const rung = (i) => page.locator('[docs-share-rung]').nth(i) const checked = (i) => rung(i).locator('input[type=checkbox]').isChecked() const team = await checked(0) const link = await checked(1) // Timed from the request, not the click, which can take a while to // land: no aria-busy at 250ms, and aria-busy by 650ms (the rule is // 500). const busyAt = async (ms) => { await until(async () => postAt > 0, 2000) await pause(Math.max(0, postAt + ms - Date.now())) return page.locator('[docs-share-rung] input[aria-busy="true"]').count() } await rung(0).locator('[rst-switch]').click() if ((await busyAt(250)) !== 0) fail('the waiting switch said aria-busy before 500ms') await rung(1).locator('[rst-switch]').click() await pause(100) if ((await checked(1)) !== link) fail('a second switch moved while the first one\'s request was in flight') if ((await busyAt(650)) !== 1) fail('the waiting switch did not say aria-busy after 500ms') const settled = await until(async () => finished === 1 && (await page.locator('[docs-share-pop] [aria-busy="true"]').count()) === 0 && (await checked(0)) !== team, 5000) if (!settled) fail('the held request did not land and redraw the panel') if (posts !== 1) fail(`${posts} requests were sent for one switch and one refused flip, want 1`) if ((await checked(1)) !== link) fail('the refused flip took effect after all') // Invite twice while the first is held: one request. await page.fill('[docs-share-invite] input[type=email]', 'dee@example.com') const sent = posts await page.click('[docs-share-submit]') await page.click('[docs-share-submit]') await until(async () => finished === sent + 1, 5000) await pause(300) if (posts !== sent + 1) fail(`two presses of Invite while the first was in flight sent ${posts - sent} requests, want 1`) mode = 'abort' const before = await checked(0) await rung(0).locator('[rst-switch]').click() const reverted = await until(async () => (await page.locator('[docs-share-error]').count()) === 1 && (await checked(0)) === before) if (!reverted) fail(`a failed request: error shown ${await page.locator('[docs-share-error]').count()}, switch ${await checked(0)} (want back at ${before})`) await context.close() } // The first load failing (review on #106). An answer that is not the // panel (a 404, the sign-in page) goes to the share page, which handles // both itself. A request that never got an answer (offline) stays on // the document: leaving would put the browser's error page where the // editor was, and anything typed offline with it. And a popover the // person has already closed stays closed, whatever comes back. { const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) const page = await context.newPage() const doc = config.docs.simple.shift() const docURL = config.origin + '/d/' + doc const share = () => page.click('[estilo-bar-actions] a[href$="/share"]') const isPanel = (u) => u.searchParams.get('panel') === '1' const fresh = async () => { if (page.url() !== docURL) await page.goto(docURL) await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) } const popOpen = () => page.evaluate(() => { const d = document.querySelector('[docs-share-pop]'); return !!d && (d.open || d.matches(':popover-open')) }) const stays = async (what) => { await pause(1500) if (page.url() !== docURL) fail(`${what}: Share left the document for ${page.url()}`) else if (!(await page.locator('[docs-text][contenteditable="true"]').count().catch(() => 0))) fail(`${what}: the editor is gone`) else if (await popOpen()) fail(`${what}: the empty popover is still open`) } // A 404 answer: the share page. await fresh() await page.route(isPanel, (route) => route.fulfill({ status: 404, contentType: 'text/html', body: '
Not found
' })) await Promise.all([page.waitForURL(docURL + '/share', { timeout: 8000 }).catch(() => {}), share()]) if (page.url() !== docURL + '/share') fail(`a 404 for the panel left the page at ${page.url()}, want the share page`) await page.unroute(isPanel) // Offline: stays, with the words typed offline. await fresh() await page.click('[docs-text] p') await page.keyboard.press('End') await context.setOffline(true) await page.keyboard.type(' offline words') await share() await stays('offline') if (!(await page.locator('[docs-text]').innerText({ timeout: 2000 }).catch(() => '')).includes('offline words')) fail('offline: the words typed offline are gone') await context.setOffline(false) // A request that fails without an answer: stays. await fresh() await page.route(isPanel, (route) => route.abort()) await share() await stays('an aborted panel request') await page.unroute(isPanel) // Escape while it loads, then a 404: stays, closed. await fresh() await page.route(isPanel, async (route) => { await pause(800) await route.fulfill({ status: 404, contentType: 'text/html', body: 'Not found
' }) }) await share() await pause(100) await page.keyboard.press('Escape') await stays('Escape, then a 404') await page.unroute(isPanel) // The session gone: the sign-in page came back, so sign-in. await fresh() await context.clearCookies() await Promise.all([page.waitForURL((u) => u.pathname.startsWith('/signin'), { timeout: 8000 }).catch(() => {}), share()]) if (!new URL(page.url()).pathname.startsWith('/signin')) fail(`with the session gone, Share left the page at ${page.url()}, want sign-in`) await context.close() } // Phone: Share is a row in estilo's Menu sheet; a modal sheet. { const context = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 1, hasTouch: true }) await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) const page = await context.newPage() await page.goto(config.origin + '/d/' + config.docs.simple.shift()) await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) await page.click('[estilo-bar-more] > summary') await page.click('[estilo-bar-more] a[href$="/share"]') const modal = await until(() => page.evaluate(() => { const d = document.querySelector('[docs-share-pop]'); return !!d && d.open && !d.hasAttribute('popover') && !!d.querySelector('[docs-share-panel]') })) if (!modal) fail('on a phone, Share in the Menu sheet did not open the modal sheet') if (await page.evaluate(() => document.querySelector('[estilo-bar-more]').open)) fail('the Menu sheet stayed open under the share sheet') const [w, vw] = await page.evaluate(() => [document.querySelector('[docs-share-pop]').getBoundingClientRect().width, innerWidth]) if (Math.abs(w - vw) > 1) fail(`the phone sheet is ${w}px wide, want the full ${vw}`) await page.keyboard.press('Escape') if (!(await until(() => page.evaluate(() => !document.querySelector('[docs-share-pop]').open)))) fail('Esc did not close the phone sheet') if (!(await page.evaluate(() => document.activeElement?.matches('[estilo-bar-more] > summary')))) fail('Esc did not return focus to the Menu trigger') await context.close() } await browser.close() } if (failures.length) { for (const f of failures) console.log('FAIL ' + f) process.exit(1) } console.log('ok: the Share popover opens, invites, switches, removes and closes, desk and phone, in both engines')