oficina / docs Public

test: the Format sheet's steps, refocus, chips, a remote edit, bold at a caret

Review 10 on #181: tests for what P2 added and had none for.

- toolbar.test.mjs: the size steps walk SIZES one size at a time and
  stop at its ends, and a dry run answers without acting; a control
  hands the caret back only where refocus() says so (this one fails if
  the click always refocuses); a style chip is pressed for the style
  the selection is in, and applies it. The stand-in DOM now keeps its
  listeners (emit plays one) and can draw the chips.
- TestARemoteEditWhileTheSheetIsOpen: a collaborator's paragraph lands
  above the selection with the sheet open; the kept selection moves
  with its text and Heading 2 then makes that text the heading. A new
  vector, remote-before.bin, from generate-fragment.mjs (the others
  regenerate byte for byte).
- TestBoldAtACaretFromTheSheet: B in the sheet at a caret, ×, Z: the Z
  is bold.

All pass on the branch as it stands: they hold behaviour that was
right, rather than reproduce a fault.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 5615bcb4237b183125a6c9e06f71260931e842d0 parent 72fa1ea
4 files changed, +132 −6
  • fixtures/generate-fragment.mjs +6 −0
  • fixtures/toolbar.test.mjs +81 −6
  • fixtures/vectors/fragment/remote-before.bin binary
  • internal/docs/phone_sheet_browser_test.go +45 −0
diff --git a/fixtures/generate-fragment.mjs b/fixtures/generate-fragment.mjs
index 13f6881..8e4740b 100644
--- a/fixtures/generate-fragment.mjs
+++ b/fixtures/generate-fragment.mjs
@@ -183,6 +183,12 @@ incrementOnSimple('remote-codeblock.bin', f => {
incrementOnSimple('remote-bold.bin', f => {
f.insert(f.length, [el('paragraph', null, text('A collaborator\'s bold', { strong: {} }))])
})
+// remote-before.bin puts a paragraph before both of simple.bin's, so a
+// selection in them moves (the phone's Format sheet maps its kept
+// selection through it).
+incrementOnSimple('remote-before.bin', f => {
+ f.insert(0, [el('paragraph', null, text('A collaborator\'s first line.'))])
+})
// long.bin runs to several pages, for the check that a paged document is
// first shown already paginated.
diff --git a/fixtures/toolbar.test.mjs b/fixtures/toolbar.test.mjs
index 908dd75..daf315f 100644
--- a/fixtures/toolbar.test.mjs
+++ b/fixtures/toolbar.test.mjs
@@ -48,7 +48,7 @@ import { schema as basic } from 'prosemirror-schema-basic'
import { addListNodes } from 'prosemirror-schema-list'
import { alignedParagraph } from './align.mjs'
import { withIndent } from './indent.mjs'
-import { SIZE_MARKS, SIZES } from './sizes.mjs'
+import { SIZE_MARKS, SIZES, sizeMark, sizeForMarks } from './sizes.mjs'
import { FONTS } from './fonts.mjs'
import { createToolbar } from './toolbar.mjs'
import { mountMenu } from './menu.mjs'
@@ -132,7 +132,7 @@ const schema = new Schema({ nodes, marks })
// and both hold elements carrying data-docs-command. A single root
// would hand the toolbar the menu's items and test something the
// browser never does.
-function makeDOM() {
+function makeDOM({ chips = false } = {}) {
const log = []
const bar = []
const menu = []
@@ -149,6 +149,7 @@ function makeDOM() {
let disabled = 'disabled' in own
let tabIndex = 'tabindex' in own ? Number(own.tabindex) : -1
let value = ''
+ const listeners = {}
const node = {
tag,
id,
@@ -176,7 +177,14 @@ function makeDOM() {
querySelector: (sel) => node.children.find((c) => c.matches(sel)) || null,
querySelectorAll: (sel) => node.children.filter((c) => c.matches(sel)),
closest: () => null,
- addEventListener() {},
+ // Listeners are kept, so a test can play a click (emit); none of
+ // the write-counting tests emits anything.
+ addEventListener(type, fn) {
+ ;(listeners[type] = listeners[type] || []).push(fn)
+ },
+ emit(type, event = {}) {
+ for (const fn of listeners[type] || []) fn({ preventDefault() {}, stopPropagation() {}, ...event })
+ },
// Focus is recorded as the page's active element, so the roving
// tab stop's keyboard handler can be driven here.
focus() {
@@ -312,6 +320,9 @@ function makeDOM() {
SIZES.map((pt) => [pt, String(pt)]),
)
for (const name of ['strong', 'em', 'code']) button(name)
+ // The phone's Format sheet's style chips (views/show.html): buttons
+ // named by the block_ commands, with a pressed state.
+ if (chips) for (const name of ['block_paragraph', 'block_heading-2']) button(name)
for (const name of ['align_left', 'align_center', 'align_right']) button(name)
for (const name of ['bullet_list', 'ordered_list', 'blockquote']) button(name)
@@ -364,14 +375,23 @@ function makeDOM() {
// to commands called with a null dispatch, which is a contract every
// command in the table honours (buildCommands' own comment has the
// evening that cost).
-function mount(state) {
- const dom = makeDOM()
- const view = { state, focus() {}, dispatch() {} }
+function mount(state, { chips = false, refocus } = {}) {
+ const dom = makeDOM({ chips })
+ // dispatch and focus as ProseMirror's are called: passed on bare,
+ // so they close over the view rather than trust `this`.
+ const view = { state, focused: 0 }
+ view.focus = () => {
+ view.focused++
+ }
+ view.dispatch = (tr) => {
+ view.state = view.state.apply(tr)
+ }
let menu = null
const toolbar = createToolbar({
root: dom.toolbarRoot,
history: dom.historyRoots,
schema,
+ ...(refocus ? { refocus } : {}),
getView: () => view,
onSync: (v) => {
if (menu) menu.sync(v)
@@ -386,6 +406,8 @@ function mount(state) {
const pluginView = toolbar.plugin.spec.view(view)
return {
dom,
+ view,
+ commands: toolbar.commands,
to(next) {
view.state = next
pluginView.update(view)
@@ -593,3 +615,56 @@ test('the arrow keys walk the bar in reading order: the pickers, then the button
assert.deepEqual(walked, want)
assert.equal(globalThis.document.activeElement.id, want[0], 'ArrowRight from the last control did not wrap to the first')
})
+
+// The phone's Format sheet (design/phone-web, P2; Fable's review 10 on
+// #181). The size steps walk SIZES in order and stop at its ends; called
+// with no dispatch they only say whether they would act.
+function sizedState(pt) {
+ const marks = pt === 12 ? [] : [schema.marks[sizeMark(pt)].create()]
+ const doc = schema.node('doc', null, [schema.node('paragraph', null, [schema.text('Some words', marks)])])
+ return EditorState.create({ doc, selection: TextSelection.create(doc, 1, 11) })
+}
+const sizeAt = (state) => sizeForMarks(state.doc.firstChild.firstChild.marks.map((m) => m.type.name))
+
+test('the size steps walk the ladder one size at a time and stop at its ends', () => {
+ const { commands } = mount(EditorState.create({ schema }))
+ const ladder = [...SIZES].sort((a, b) => a - b)
+ for (const [from, cmd, to] of [[12, 'size_up', 14], [12, 'size_down', 11], [8, 'size_up', 9], [72, 'size_down', 48]]) {
+ let next = null
+ assert.equal(commands[cmd](sizedState(from), (tr) => { next = sizedState(from).apply(tr) }), true, `${cmd} from ${from}`)
+ assert.equal(sizeAt(next), to, `${cmd} from ${from}`)
+ }
+ for (const [at, cmd] of [[ladder[0], 'size_down'], [ladder[ladder.length - 1], 'size_up']]) {
+ let dispatched = false
+ assert.equal(commands[cmd](sizedState(at), () => { dispatched = true }), false, `${cmd} at ${at}, the ladder's end`)
+ assert.equal(dispatched, false)
+ }
+ // A dry run: the answer, and nothing done.
+ const state = sizedState(12)
+ assert.equal(commands.size_up(state), true)
+ assert.equal(commands.size_up(state, null), true)
+ assert.equal(sizeAt(state), 12)
+ assert.equal(commands.size_up(sizedState(72)), false)
+})
+
+test('a control hands the caret back to the document only where refocus says so', () => {
+ for (const [refocus, want] of [[() => true, 1], [() => false, 0]]) {
+ const bar = mount(sizedState(12), { refocus })
+ bar.dom.byId('strong').emit('click')
+ assert.equal(bar.view.focused, want, `refocus() ${refocus()}`)
+ assert.ok(bar.view.state.doc.firstChild.firstChild.marks.some((m) => m.type.name === 'strong'), 'B did not act')
+ }
+})
+
+test('a style chip is pressed for the style the selection is in', () => {
+ const heading = schema.node('doc', null, [schema.node('heading', { level: 2 }, [schema.text('A heading')])])
+ const bar = mount(sizedState(12), { chips: true })
+ assert.equal(bar.dom.byId('block_paragraph').getAttribute('aria-pressed'), 'true')
+ assert.equal(bar.dom.byId('block_heading-2').getAttribute('aria-pressed'), 'false')
+ bar.to(EditorState.create({ doc: heading, selection: TextSelection.create(heading, 1, 3) }))
+ assert.equal(bar.dom.byId('block_paragraph').getAttribute('aria-pressed'), 'false')
+ assert.equal(bar.dom.byId('block_heading-2').getAttribute('aria-pressed'), 'true')
+ // And a chip applies its style.
+ bar.dom.byId('block_paragraph').emit('click')
+ assert.equal(bar.view.state.doc.firstChild.type.name, 'paragraph')
+})
diff --git a/fixtures/vectors/fragment/remote-before.bin b/fixtures/vectors/fragment/remote-before.bin
new file mode 100644
index 0000000..82cc54d
Binary files /dev/null and b/fixtures/vectors/fragment/remote-before.bin differ
diff --git a/internal/docs/phone_sheet_browser_test.go b/internal/docs/phone_sheet_browser_test.go
index a7f400f..6cd281f 100644
--- a/internal/docs/phone_sheet_browser_test.go
+++ b/internal/docs/phone_sheet_browser_test.go
@@ -17,6 +17,7 @@ import (
"net/http"
"net/http/httptest"
"os"
+ "strings"
"testing"
"time"
@@ -366,3 +367,47 @@ func resyncWithTheSheetOpen(t *testing.T, control string) {
t.Errorf("after the resync: sheet open %v, kept drawn over %q; want open, over \"Second paragraph.\"", got.Open, got.Kept)
}
}
+
+// Review 10: a collaborator's paragraph arrives above the selection
+// while the sheet is open; the kept selection moves with its text, and
+// a chip then applies to that text, not to what is now at its old
+// place.
+func TestARemoteEditWhileTheSheetIsOpen(t *testing.T) {
+ s := newSheetRig(t, "simple.bin", "")
+ s.run("select, Aa", chromedp.Click(chromedp.CSS(`[docs-text] p:last-child`)), settle,
+ do(`getSelection().selectAllChildren(document.querySelector('[docs-text] p:last-child'))`), settle,
+ chromedp.Click(chromedp.CSS(`[docs-aa]`)), settle)
+ s.post("remote-before.bin")
+ s.run("the collaborator's line arrives", chromedp.WaitVisible(chromedp.CSS(`[docs-text] > :nth-child(3)`)), settle)
+ var kept string
+ s.eval("kept", `(document.querySelector('[data-docs-kept]') || {}).textContent || ''`, &kept)
+ if kept != "Second paragraph." {
+ t.Errorf("after the collaborator's line: the kept selection is drawn over %q, want \"Second paragraph.\"", kept)
+ }
+ s.run("Heading 2", chromedp.Click(chromedp.CSS(`[docs-chip="heading-2"]`)), settle)
+ var blocks []string
+ s.eval("the blocks", `[...document.querySelectorAll('[docs-text] > *')].map((b) => b.tagName + ' ' + b.textContent)`, &blocks)
+ want := []string{"P A collaborator's first line.", "P First paragraph.", "H2 Second paragraph."}
+ if strings.Join(blocks, "|") != strings.Join(want, "|") {
+ t.Errorf("after Heading 2 from the sheet: %q, want %q", blocks, want)
+ }
+}
+
+// Review 10: B in the sheet at a caret, × (the keyboard comes back),
+// then typing: the typed letter is bold.
+func TestBoldAtACaretFromTheSheet(t *testing.T) {
+ s := newSheetRig(t, "simple.bin", "")
+ s.run("the caret at the end, a keyboard", chromedp.Click(chromedp.CSS(`[docs-text] p:last-child`)), settle,
+ do(`const p = document.querySelector('[docs-text] p:last-child'); const r = document.createRange(); r.selectNodeContents(p); r.collapse(false); getSelection().removeAllRanges(); getSelection().addRange(r)`), settle,
+ do(`__keyboard(336, 0)`), settle,
+ chromedp.Click(chromedp.CSS(`[docs-quickbar-aa]`)), settle,
+ chromedp.Click(chromedp.CSS(`[docs-inspector] [docs-toolbar-group="text"] [data-docs-command="strong"]`)), settle,
+ chromedp.Click(chromedp.CSS(`[docs-sheet-close]`)), settle,
+ chromedp.KeyEvent("Z"), settle)
+ var got struct{ Last, Bold string }
+ s.eval("typed", `(() => { const p = document.querySelector('[docs-text] p:last-child'), b = p.querySelector('strong');
+ return { Last: p.textContent, Bold: b ? b.textContent : '' } })()`, &got)
+ if got.Last != "Second paragraph.Z" || got.Bold != "Z" {
+ t.Errorf("B, ×, then Z: the paragraph reads %q with %q bold, want \"Second paragraph.Z\" with \"Z\" bold", got.Last, got.Bold)
+ }
+}