| 1 | // fixtures/engines/share-pop.mjs — the editor's Share popover |
| 2 | // (brand/share-pop.js), in Chromium and WebKit, through the real server. |
| 3 | // |
| 4 | // Run by internal/docs/share_pop_browser_test.go with the same |
| 5 | // DOCS_ENGINES_CONFIG as first-reveal.mjs: "simple" documents owned by |
| 6 | // the signed-in person, two per engine (desk and phone). |
| 7 | // |
| 8 | // Desk (1280px): Share opens a panel under the button without leaving |
| 9 | // the page, focus in the email field; Share again changes nothing (no |
| 10 | // flash: the same panel element stays); an invite lands and the person |
| 11 | // is listed; the team switch and the link switch each move their rung, |
| 12 | // the link's panel carrying the one-time URL; removing the person drops |
| 13 | // them; a pointerdown outside closes it; Esc closes it and gives focus |
| 14 | // back to Share. Phone (390px): Share is in estilo's Menu sheet, opens a |
| 15 | // modal sheet, and Esc gives focus to the Menu trigger. |
| 16 | |
| 17 | const { chromium, webkit } = await import('playwright') |
| 18 | const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG) |
| 19 | const pause = (ms) => new Promise((r) => setTimeout(r, ms)) |
| 20 | |
| 21 | const failures = [] |
| 22 | for (const engine of [chromium, webkit]) { |
| 23 | const name = engine.name() |
| 24 | const fail = (msg) => failures.push(`${name}: ${msg}`) |
| 25 | const browser = await engine.launch({ headless: true }) |
| 26 | const until = async (fn, ms = 4000) => { |
| 27 | const end = Date.now() + ms |
| 28 | while (Date.now() < end) { if (await fn()) return true; await pause(50) } |
| 29 | return fn() |
| 30 | } |
| 31 | |
| 32 | // Desk. |
| 33 | { |
| 34 | const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) |
| 35 | await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) |
| 36 | const page = await context.newPage() |
| 37 | const doc = config.docs.simple.shift() |
| 38 | const url = config.origin + '/d/' + doc |
| 39 | await page.goto(url) |
| 40 | await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) |
| 41 | const share = page.locator('[estilo-bar-actions] a[href$="/share"]') |
| 42 | const panel = () => page.locator('[docs-share-pop] [docs-share-panel]') |
| 43 | const open = () => page.evaluate(() => document.querySelector('[docs-share-pop]')?.matches(':popover-open') ?? false) |
| 44 | const people = () => page.locator('[docs-share-people] li').allTextContents() |
| 45 | |
| 46 | await share.click() |
| 47 | if (!(await until(async () => (await panel().count()) === 1))) fail('Share did not open the panel') |
| 48 | if (page.url() !== url) fail(`Share navigated to ${page.url()}`) |
| 49 | if (!(await until(() => page.evaluate(() => document.activeElement?.matches('[docs-share-invite] input[type=email]'))))) fail('focus is not in the email field') |
| 50 | const box = await page.locator('[docs-share-pop]').boundingBox() |
| 51 | const sbox = await share.boundingBox() |
| 52 | 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)}`) |
| 53 | |
| 54 | // Share while open: nothing (the same panel element stays). |
| 55 | await page.evaluate(() => { window.__panel = document.querySelector('[docs-share-panel]') }) |
| 56 | await share.click() |
| 57 | await pause(300) |
| 58 | if (!(await open())) fail('Share while open closed the panel') |
| 59 | if (!(await page.evaluate(() => document.querySelector('[docs-share-panel]') === window.__panel))) fail('Share while open redrew the panel (a flash)') |
| 60 | |
| 61 | // Invite. |
| 62 | await page.fill('[docs-share-invite] input[type=email]', 'pat@example.com') |
| 63 | await page.selectOption('[docs-share-invite] select', 'writer') |
| 64 | await page.click('[docs-share-submit]') |
| 65 | if (!(await until(async () => (await people()).some((t) => t.includes('pat@example.com'))))) fail('the invited person is not listed') |
| 66 | if (page.url() !== url) fail('the invite navigated') |
| 67 | |
| 68 | // The two switches. |
| 69 | for (const [i, what] of [[0, 'team'], [1, 'link']]) { |
| 70 | const sw = page.locator('[docs-share-rung]').nth(i).locator('input[type=checkbox]') |
| 71 | const before = await sw.isChecked() |
| 72 | await page.locator('[docs-share-rung]').nth(i).locator('[rst-switch]').click() |
| 73 | const moved = await until(async () => (await page.locator('[docs-share-rung]').nth(i).locator('input[type=checkbox]').isChecked()) !== before |
| 74 | && !(await page.locator('[docs-share-pop] [aria-busy="true"]').count())) |
| 75 | if (!moved) fail(`the ${what} switch did not stay moved`) |
| 76 | } |
| 77 | // The link was just turned on (new documents are unpublished): the |
| 78 | // panel carries the URL once. |
| 79 | const link = await page.locator('[docs-share-link] input').inputValue().catch(() => '') |
| 80 | if (!/\/p\/[A-Za-z0-9_-]{16,}/.test(link)) fail(`publishing from the panel did not show the link: ${JSON.stringify(link)}`) |
| 81 | |
| 82 | // Remove. |
| 83 | await page.locator('[docs-share-people] li', { hasText: 'pat@example.com' }).locator('[docs-share-remove]').click() |
| 84 | if (!(await until(async () => !(await people()).some((t) => t.includes('pat@example.com'))))) fail('removing the person did not drop them') |
| 85 | |
| 86 | // Outside closes; Esc closes and returns focus to Share. |
| 87 | await page.mouse.click(640, 700) |
| 88 | if (!(await until(async () => !(await open())))) fail('a click outside did not close the panel') |
| 89 | await share.click() |
| 90 | await until(async () => (await panel().count()) === 1) |
| 91 | await page.keyboard.press('Escape') |
| 92 | if (!(await until(async () => !(await open())))) fail('Esc did not close the panel') |
| 93 | if (!(await page.evaluate(() => document.activeElement?.matches('[estilo-bar-actions] a[href$="/share"]')))) fail('Esc did not return focus to Share') |
| 94 | await context.close() |
| 95 | } |
| 96 | |
| 97 | // One request at a time (review on #106): while a switch's POST is held |
| 98 | // back, flipping the other switch puts it straight back and sends |
| 99 | // nothing; past 500ms the waiting switch says aria-busy; the answer |
| 100 | // redraws the panel. Then a POST that fails: the switch goes back and |
| 101 | // the panel says so. |
| 102 | { |
| 103 | const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) |
| 104 | await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) |
| 105 | const page = await context.newPage() |
| 106 | await page.goto(config.origin + '/d/' + config.docs.simple.shift()) |
| 107 | await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) |
| 108 | let mode = 'slow' |
| 109 | let posts = 0 |
| 110 | let finished = 0 |
| 111 | let postAt = 0 |
| 112 | page.on('requestfinished', (r) => { if (r.method() === 'POST') finished++ }) |
| 113 | page.on('requestfailed', (r) => { if (r.method() === 'POST') finished++ }) |
| 114 | await page.route((u) => u.pathname.includes('/share'), async (route) => { |
| 115 | if (route.request().method() !== 'POST') return route.continue() |
| 116 | posts++ |
| 117 | if (!postAt) postAt = Date.now() |
| 118 | if (mode === 'abort') return route.abort() |
| 119 | await pause(1500) |
| 120 | return route.continue() |
| 121 | }) |
| 122 | await page.click('[estilo-bar-actions] a[href$="/share"]') |
| 123 | await until(async () => (await page.locator('[docs-share-pop] [docs-share-panel]').count()) === 1) |
| 124 | const rung = (i) => page.locator('[docs-share-rung]').nth(i) |
| 125 | const checked = (i) => rung(i).locator('input[type=checkbox]').isChecked() |
| 126 | const team = await checked(0) |
| 127 | const link = await checked(1) |
| 128 | // Timed from the request, not the click, which can take a while to |
| 129 | // land: no aria-busy at 250ms, and aria-busy by 650ms (the rule is |
| 130 | // 500). |
| 131 | const busyAt = async (ms) => { |
| 132 | await until(async () => postAt > 0, 2000) |
| 133 | await pause(Math.max(0, postAt + ms - Date.now())) |
| 134 | return page.locator('[docs-share-rung] input[aria-busy="true"]').count() |
| 135 | } |
| 136 | await rung(0).locator('[rst-switch]').click() |
| 137 | if ((await busyAt(250)) !== 0) fail('the waiting switch said aria-busy before 500ms') |
| 138 | await rung(1).locator('[rst-switch]').click() |
| 139 | await pause(100) |
| 140 | if ((await checked(1)) !== link) fail('a second switch moved while the first one\'s request was in flight') |
| 141 | if ((await busyAt(650)) !== 1) fail('the waiting switch did not say aria-busy after 500ms') |
| 142 | const settled = await until(async () => finished === 1 && (await page.locator('[docs-share-pop] [aria-busy="true"]').count()) === 0 && (await checked(0)) !== team, 5000) |
| 143 | if (!settled) fail('the held request did not land and redraw the panel') |
| 144 | if (posts !== 1) fail(`${posts} requests were sent for one switch and one refused flip, want 1`) |
| 145 | if ((await checked(1)) !== link) fail('the refused flip took effect after all') |
| 146 | |
| 147 | // Invite twice while the first is held: one request. |
| 148 | await page.fill('[docs-share-invite] input[type=email]', 'dee@example.com') |
| 149 | const sent = posts |
| 150 | await page.click('[docs-share-submit]') |
| 151 | await page.click('[docs-share-submit]') |
| 152 | await until(async () => finished === sent + 1, 5000) |
| 153 | await pause(300) |
| 154 | if (posts !== sent + 1) fail(`two presses of Invite while the first was in flight sent ${posts - sent} requests, want 1`) |
| 155 | |
| 156 | mode = 'abort' |
| 157 | const before = await checked(0) |
| 158 | await rung(0).locator('[rst-switch]').click() |
| 159 | const reverted = await until(async () => (await page.locator('[docs-share-error]').count()) === 1 && (await checked(0)) === before) |
| 160 | if (!reverted) fail(`a failed request: error shown ${await page.locator('[docs-share-error]').count()}, switch ${await checked(0)} (want back at ${before})`) |
| 161 | await context.close() |
| 162 | } |
| 163 | |
| 164 | // The first load failing (review on #106). An answer that is not the |
| 165 | // panel (a 404, the sign-in page) goes to the share page, which handles |
| 166 | // both itself. A request that never got an answer (offline) stays on |
| 167 | // the document: leaving would put the browser's error page where the |
| 168 | // editor was, and anything typed offline with it. And a popover the |
| 169 | // person has already closed stays closed, whatever comes back. |
| 170 | { |
| 171 | const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) |
| 172 | await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) |
| 173 | const page = await context.newPage() |
| 174 | const doc = config.docs.simple.shift() |
| 175 | const docURL = config.origin + '/d/' + doc |
| 176 | const share = () => page.click('[estilo-bar-actions] a[href$="/share"]') |
| 177 | const isPanel = (u) => u.searchParams.get('panel') === '1' |
| 178 | const fresh = async () => { |
| 179 | if (page.url() !== docURL) await page.goto(docURL) |
| 180 | await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) |
| 181 | } |
| 182 | const popOpen = () => page.evaluate(() => { const d = document.querySelector('[docs-share-pop]'); return !!d && (d.open || d.matches(':popover-open')) }) |
| 183 | const stays = async (what) => { |
| 184 | await pause(1500) |
| 185 | if (page.url() !== docURL) fail(`${what}: Share left the document for ${page.url()}`) |
| 186 | else if (!(await page.locator('[docs-text][contenteditable="true"]').count().catch(() => 0))) fail(`${what}: the editor is gone`) |
| 187 | else if (await popOpen()) fail(`${what}: the empty popover is still open`) |
| 188 | } |
| 189 | |
| 190 | // A 404 answer: the share page. |
| 191 | await fresh() |
| 192 | await page.route(isPanel, (route) => route.fulfill({ status: 404, contentType: 'text/html', body: '<!doctype html><p>Not found</p>' })) |
| 193 | await Promise.all([page.waitForURL(docURL + '/share', { timeout: 8000 }).catch(() => {}), share()]) |
| 194 | if (page.url() !== docURL + '/share') fail(`a 404 for the panel left the page at ${page.url()}, want the share page`) |
| 195 | await page.unroute(isPanel) |
| 196 | |
| 197 | // Offline: stays, with the words typed offline. |
| 198 | await fresh() |
| 199 | await page.click('[docs-text] p') |
| 200 | await page.keyboard.press('End') |
| 201 | await context.setOffline(true) |
| 202 | await page.keyboard.type(' offline words') |
| 203 | await share() |
| 204 | await stays('offline') |
| 205 | if (!(await page.locator('[docs-text]').innerText({ timeout: 2000 }).catch(() => '')).includes('offline words')) fail('offline: the words typed offline are gone') |
| 206 | await context.setOffline(false) |
| 207 | |
| 208 | // A request that fails without an answer: stays. |
| 209 | await fresh() |
| 210 | await page.route(isPanel, (route) => route.abort()) |
| 211 | await share() |
| 212 | await stays('an aborted panel request') |
| 213 | await page.unroute(isPanel) |
| 214 | |
| 215 | // Escape while it loads, then a 404: stays, closed. |
| 216 | await fresh() |
| 217 | await page.route(isPanel, async (route) => { |
| 218 | await pause(800) |
| 219 | await route.fulfill({ status: 404, contentType: 'text/html', body: '<!doctype html><p>Not found</p>' }) |
| 220 | }) |
| 221 | await share() |
| 222 | await pause(100) |
| 223 | await page.keyboard.press('Escape') |
| 224 | await stays('Escape, then a 404') |
| 225 | await page.unroute(isPanel) |
| 226 | |
| 227 | // The session gone: the sign-in page came back, so sign-in. |
| 228 | await fresh() |
| 229 | await context.clearCookies() |
| 230 | await Promise.all([page.waitForURL((u) => u.pathname.startsWith('/signin'), { timeout: 8000 }).catch(() => {}), share()]) |
| 231 | if (!new URL(page.url()).pathname.startsWith('/signin')) fail(`with the session gone, Share left the page at ${page.url()}, want sign-in`) |
| 232 | await context.close() |
| 233 | } |
| 234 | |
| 235 | // Phone: Share is a row in estilo's Menu sheet; a modal sheet. |
| 236 | { |
| 237 | const context = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 1, hasTouch: true }) |
| 238 | await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) |
| 239 | const page = await context.newPage() |
| 240 | await page.goto(config.origin + '/d/' + config.docs.simple.shift()) |
| 241 | await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) |
| 242 | await page.click('[estilo-bar-more] > summary') |
| 243 | await page.click('[estilo-bar-more] a[href$="/share"]') |
| 244 | 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]') })) |
| 245 | if (!modal) fail('on a phone, Share in the Menu sheet did not open the modal sheet') |
| 246 | if (await page.evaluate(() => document.querySelector('[estilo-bar-more]').open)) fail('the Menu sheet stayed open under the share sheet') |
| 247 | const [w, vw] = await page.evaluate(() => [document.querySelector('[docs-share-pop]').getBoundingClientRect().width, innerWidth]) |
| 248 | if (Math.abs(w - vw) > 1) fail(`the phone sheet is ${w}px wide, want the full ${vw}`) |
| 249 | await page.keyboard.press('Escape') |
| 250 | if (!(await until(() => page.evaluate(() => !document.querySelector('[docs-share-pop]').open)))) fail('Esc did not close the phone sheet') |
| 251 | if (!(await page.evaluate(() => document.activeElement?.matches('[estilo-bar-more] > summary')))) fail('Esc did not return focus to the Menu trigger') |
| 252 | await context.close() |
| 253 | } |
| 254 | await browser.close() |
| 255 | } |
| 256 | |
| 257 | if (failures.length) { |
| 258 | for (const f of failures) console.log('FAIL ' + f) |
| 259 | process.exit(1) |
| 260 | } |
| 261 | console.log('ok: the Share popover opens, invites, switches, removes and closes, desk and phone, in both engines') |
| 262 | |