oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/engines/richer.mjs — three behaviours from the richer-UI pass,
2// in Chromium and WebKit (Fable's branch review on #105).
3//
4// Run by internal/docs/richer_engines_browser_test.go with the same
5// DOCS_ENGINES_CONFIG as first-reveal.mjs: one "simple" and one "empty"
6// document per engine.
7//
8// 1. Undo and Redo (the bar's lead pair, and the strip's with it) follow
9// keyboard undo and redo. Those never touch the buttons, which used
10// to keep whatever the last button-driven change had left.
11// 2. An empty document opens with the caret in it; a document with text
12// does not take focus.
13// 3. On the list, a search emptied with Backspace stays on its results;
14// Escape and the field's own × go back to the whole list.
15
16const { chromium, webkit } = await import('playwright')
17
18const config = JSON.parse(process.env.DOCS_ENGINES_CONFIG)
19const pause = (ms) => new Promise((r) => setTimeout(r, ms))
20
21const failures = []
22for (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 context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 })
27 await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })))
28 const page = await context.newPage()
29 const focusInText = () => page.evaluate(() => !!document.activeElement && !!document.activeElement.closest('[docs-text]'))
30 // The pair a person sees at this width is the bar's lead slot's (spec
31 // I2); the strip's, drawn below 800px, must agree with it.
32 const disabled = (cmd) => page.evaluate((c) => {
33 const lead = document.querySelector(`[estilo-bar-lead] [data-docs-command="${c}"]`).disabled
34 const strip = document.querySelector(`[docs-bar] > [docs-history] [data-docs-command="${c}"]`).disabled
35 return lead === strip ? lead : 'the two pairs disagree'
36 }, cmd)
37 const until = async (fn, ms = 3000) => {
38 const end = Date.now() + ms
39 while (Date.now() < end) {
40 if (await fn()) return true
41 await pause(50)
42 }
43 return fn()
44 }
45
46 // 2. Focus: the document with text first, then the empty one.
47 await page.goto(config.origin + '/d/' + config.docs.simple.shift())
48 await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 })
49 await pause(1500)
50 if (await focusInText()) fail('a document with text took focus on opening')
51
52 // 1. Undo and redo, from the keyboard, on that same document.
53 await page.click('[docs-text] p')
54 await page.keyboard.press('End')
55 await page.keyboard.type('xyz')
56 if (!(await until(async () => !(await disabled('undo'))))) fail('Undo is still disabled after typing')
57 await pause(700)
58 await page.keyboard.press('ControlOrMeta+z')
59 if (!(await until(async () => (await disabled('undo')) && !(await disabled('redo'))))) {
60 fail(`after a keyboard undo: undo disabled=${await disabled('undo')}, redo disabled=${await disabled('redo')}; want true, false`)
61 }
62 await page.keyboard.press('ControlOrMeta+Shift+z')
63 if (!(await until(async () => !(await disabled('undo')) && (await disabled('redo'))))) {
64 fail(`after a keyboard redo: undo disabled=${await disabled('undo')}, redo disabled=${await disabled('redo')}; want false, true`)
65 }
66
67 await page.goto(config.origin + '/d/' + config.docs.empty.shift())
68 await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 })
69 if (!(await until(focusInText, 5000))) fail('an empty document did not open with the caret in it')
70
71 // 3. Search: Backspace to empty stays; Escape clears and leaves.
72 await page.goto(config.origin + '/?q=simple')
73 const input = page.locator('[docs-search-input]')
74 if ((await input.inputValue()) !== 'simple') fail(`the search field holds ${JSON.stringify(await input.inputValue())}, want "simple"`)
75 await input.click()
76 await page.keyboard.press('End')
77 for (let i = 0; i < 6; i++) await page.keyboard.press('Backspace')
78 await pause(800)
79 if (!page.url().includes('q=simple')) fail(`emptying the search with Backspace navigated to ${page.url()}`)
80 await page.keyboard.type('simple')
81 await Promise.all([
82 page.waitForURL(config.origin + '/', { timeout: 5000 }).catch(() => {}),
83 page.keyboard.press('Escape'),
84 ])
85 if (page.url() !== config.origin + '/') fail(`clearing the search with Escape left the page at ${page.url()}, field ${JSON.stringify(await input.inputValue())}`)
86
87 // The field's own ×, clicked where it is drawn: the input's right
88 // end, inside its padding. Chromium fires an input event with no
89 // inputType and then "search"; WebKit fires "search".
90 await page.goto(config.origin + '/?q=simple')
91 await input.hover()
92 const box = await input.boundingBox()
93 const pad = await input.evaluate((el) => parseFloat(getComputedStyle(el).paddingRight) || 0)
94 await Promise.all([
95 page.waitForURL(config.origin + '/', { timeout: 5000 }).catch(() => {}),
96 page.mouse.click(box.x + box.width - pad - 8, box.y + box.height / 2),
97 ])
98 if (page.url() !== config.origin + '/') fail(`clicking the field's × left the page at ${page.url()}, field ${JSON.stringify(await input.inputValue().catch(() => '?'))}`)
99
100 await browser.close()
101}
102
103if (failures.length) {
104 for (const f of failures) console.log('FAIL ' + f)
105 process.exit(1)
106}
107console.log('ok: undo/redo stay current, empty documents take the caret, search clear vs backspace, in both engines')
108