// upload.test.mjs — the upload queue (images spec §4.2), without a // browser: a fake document for the box, a fake XMLHttpRequest the test // answers, and a view that is an EditorState. Fable's round on 49d8df3: // M3 (text on the clipboard wins), M4 (Replace… keeps the old image // until the new one is in, and its alt), M5 (a box survives edits next // to it), M6 (several files in order), L12 (an undone failure does not // stall the queue). import { test } from 'node:test' import assert from 'node:assert/strict' import { EditorState } from 'prosemirror-state' import { history, undo } from 'prosemirror-history' import { schema } from './testkit.mjs' import { fromMarkdown } from './fromMarkdown.mjs' import { serialise } from './serialize.mjs' import { uploadsPlugin, uploadFiles, droppingPlugin, replaceImage, inFlight, refusal } from './upload.mjs' class Element { constructor (tag) { this.tagName = tag this.attributes = {} this.listeners = {} this.children = [] this.hidden = false this.textContent = '' this.value = 0 } setAttribute (name, value) { this.attributes[name] = String(value) } getAttribute (name) { return this.attributes[name] ?? null } removeAttribute (name) { delete this.attributes[name] } append (...children) { this.children.push(...children) } remove () {} addEventListener (type, fn) { (this.listeners[type] ??= []).push(fn) } querySelector (tag) { return this.children.find(child => child.tagName === tag) ?? null } click () { for (const fn of this.listeners.click ?? []) fn({ preventDefault () {} }) } } globalThis.document = { createElement: tag => new Element(tag) } // replies are the uploads waiting for an answer, in order. const replies = [] globalThis.XMLHttpRequest = class { constructor () { this.listeners = {} this.upload = { addEventListener () {} } } open () {} setRequestHeader () {} addEventListener (type, fn) { this.listeners[type] = fn } send () { replies.push((status, json) => { this.status = status this.response = json this.listeners.load?.() }) } } // answer replies to the oldest upload waiting, once one is. async function answer (status, json) { for (let tries = 0; replies.length === 0 && tries < 200; tries++) { await new Promise(resolve => setTimeout(resolve, 1)) } assert.ok(replies.length, 'no upload was sent') replies.shift()(status, json) await new Promise(resolve => setTimeout(resolve, 5)) } const image = id => ({ id, src: `/i/${id}`, width: 10, height: 10 }) const png = name => new File([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], name, { type: 'image/png' }) function view (body) { const doc = fromMarkdown(schema, body) const holder = { state: EditorState.create({ doc, plugins: [uploadsPlugin(), history()] }), dispatch (tr) { holder.state = holder.state.apply(tr) }, focus () {} } return holder } const hooks = () => ({ answers: { set () {} }, say () {}, notice () {}, alt () {} }) const text = v => serialise(v.state.doc, null) // end is the position at the end of the first paragraph's text. const end = v => v.state.doc.firstChild.nodeSize - 1 test('M6: several files land in the order they were chosen', async () => { const v = view('Pictures:') const done = uploadFiles(v, [png('one.png'), png('two.png')], end(v), hooks()) await answer(200, image('aa1')) await answer(200, image('bb2')) await done assert.equal(text(v), 'Pictures:') }) test('M5: one Backspace before the box keeps the image where the box is', async () => { const v = view('Pictures: ') const done = uploadFiles(v, [png('one.png')], end(v), hooks()) await new Promise(resolve => setTimeout(resolve, 5)) v.dispatch(v.state.tr.delete(end(v) - 1, end(v))) await answer(200, image('aa1')) await done assert.equal(text(v), 'Pictures:') }) test('M5: undoing text typed just before the box keeps the upload', async () => { const v = view('Pictures') v.dispatch(v.state.tr.insertText(' here', end(v))) const done = uploadFiles(v, [png('one.png')], end(v), hooks()) await new Promise(resolve => setTimeout(resolve, 5)) assert.ok(undo(v.state, tr => v.dispatch(tr)), 'nothing to undo') await answer(200, image('aa1')) await done assert.equal(text(v), 'Pictures') }) test('L12: a failed upload an undo takes away does not stall the next', async () => { const v = view('One') v.dispatch(v.state.tr.insertText(' Hello world', end(v))) const middle = end(v) - ' world'.length const done = uploadFiles(v, [png('one.png'), png('two.png')], middle, hooks()) await answer(500, { refused: 'no' }) // The typing the box sits in is undone, box and all. assert.ok(undo(v.state, tr => v.dispatch(tr)), 'nothing to undo') await answer(200, image('bb2')) await Promise.race([done, new Promise((resolve, reject) => setTimeout(() => reject(new Error('the queue stalled')), 500))]) assert.match(text(v), /bb2/u) assert.equal(inFlight(v.state), 0) }) test('M4: Replace… keeps the old image until the new one is in, and its alt', async () => { const v = view('A  here') const at = 3 assert.equal(v.state.doc.nodeAt(at)?.attrs.id, 'old1') const failing = replaceImage(v, at, png('new.png'), hooks()) await answer(500, { refused: 'no' }) assert.equal(text(v), 'A  here', 'a failed replace leaves the old image') assert.equal(inFlight(v.state), 1) void failing const v2 = view('A  here') const working = replaceImage(v2, at, png('new.png'), hooks()) await answer(200, image('new2')) await working assert.equal(text(v2), 'A  here') }) test('M3 and N1: text wins, except over a picture copied from a page', () => { const v = view('Cells:') const plugin = droppingPlugin(hooks()) const event = (types) => ({ preventDefault () {}, clipboardData: { files: [png('cells.png')], getData: type => types[type] ?? '' } }) const pastes = [ // Spreadsheet cells: text, HTML and a picture of them. The cells win. [{ 'text/plain': 'a\tb', 'text/html': '
| a | b |
' }, true],
// Firefox's: the same, with the address as text.
[{ 'text/html': '
', 'text/plain': 'https://example.com/a.png' }, true],
// An See
' }, true],
// A bare picture: a screenshot.
[{}, true]
]
for (const [types, uploads] of pastes) {
assert.equal(plugin.props.handlePaste(v, event(types)), uploads, JSON.stringify(types))
}
})
test('N2: a JPEG over 10 MB is scaled before its size is checked', () => {
const big = size => new File([new Uint8Array(size)], 'photo.jpg', { type: 'image/jpeg' })
assert.equal(refusal(big(12 * 1024 * 1024)), null)
const png = new File([new Uint8Array(12 * 1024 * 1024)], 'shot.png', { type: 'image/png' })
assert.match(refusal(png), /10 MB/u)
})