// The asset half of the inspector (spec §2.7): one finished upload as // the two ops it is, the Unused images list derived from the document, // and the words a refusal is said in. // // A file of its own rather than more of inspector.test.mjs: that one is // about controls and their ops, and this is about a thing that arrives // from outside the document and then joins it. import test from 'node:test' import assert from 'node:assert/strict' import * as Y from 'yjs' import { applyOps, view } from './ops.mjs' import { registerAssetOps, removeAssetOp, unusedAssets } from './inspector-ops.mjs' import { fillUnused, uploadMessage } from './inspector-image.mjs' import { el as stub, withDocument } from './dom-stub.mjs' function deck () { const doc = new Y.Doc() applyOps(doc, [ { op: 'set_meta', title: 'T', theme: 'paper' }, { op: 'add_slide', id: 'slideaaaaaaa', layout: 'flow', template: 'title-body' }, { op: 'add_slide', id: 'slidebbbbbbb', layout: 'canvas' }, { op: 'add_block', slide: 'slidebbbbbbb', id: 'canvasimage1', type: 'image', box: { x: 0.2, y: 0.6, w: 0.3, h: 0.2 }, z: 0 }, { op: 'add_slide', id: 'slideccccccc', layout: 'flow', template: 'blank' } ], 'test') return doc } const accepts = (doc, ops) => assert.doesNotThrow(() => applyOps(doc, Array.isArray(ops) ? ops : [ops], 'test')) test('one finished upload is register_asset plus the block that uses it, in one batch', () => { const doc = deck() const record = { id: 'assetaaaaaaa', ref: 'aa01bb02', name: 'chart.png', width: 1600, height: 900, alt: 'A bar chart' } const ops = registerAssetOps(record, 'slidebbbbbbb', 'canvasimage1') assert.deepEqual(ops, [ { op: 'register_asset', id: 'assetaaaaaaa', ref: 'aa01bb02', name: 'chart.png', width: 1600, height: 900, alt: 'A bar chart' }, { op: 'set_block', slide: 'slidebbbbbbb', id: 'canvasimage1', props: { asset: 'assetaaaaaaa' } } ]) accepts(doc, ops) const v = view(doc) assert.equal(v.assets.assetaaaaaaa.ref, 'aa01bb02') assert.equal(v.slides[1].blocks[0].props.asset, 'assetaaaaaaa') // With no block selected it is the registration alone — still legal, // and it lands in the Unused images list where somebody can drop it. assert.equal(registerAssetOps({ ...record, id: 'assetbbbbbbb' }, '', '').length, 1) }) test('a record missing its numbers still makes an op the core accepts', () => { const doc = deck() accepts(doc, registerAssetOps({ id: 'assetcccccccc', ref: 'r9' }, '', '')) const a = view(doc).assets.assetcccccccc assert.equal(a.width, 0) assert.equal(a.height, 0) assert.equal(a.alt, '') }) test('unusedAssets is what no slide refers to, and remove_asset drops one', () => { const doc = deck() applyOps(doc, [ ...registerAssetOps({ id: 'usedbyblock1', ref: 'r1', name: 'used.png', width: 1, height: 1, alt: 'a' }, 'slidebbbbbbb', 'canvasimage1'), ...registerAssetOps({ id: 'usedasbackgr', ref: 'r2', name: 'bg.png', width: 1, height: 1, alt: 'b' }, '', ''), ...registerAssetOps({ id: 'usedbynobody', ref: 'r3', name: 'orphan.png', width: 1, height: 1, alt: 'c' }, '', ''), { op: 'set_slide', id: 'slideccccccc', background: { kind: 'image', value: 'usedasbackgr' } } ], 'test') const v = view(doc) assert.deepEqual(unusedAssets(v).map(a => a.id), ['usedbynobody']) // The entry carries the asset's own fields, so the list can name it. assert.equal(unusedAssets(v)[0].name, 'orphan.png') accepts(doc, removeAssetOp('usedbynobody')) assert.deepEqual(unusedAssets(view(doc)), []) // Deleting the block that used one leaves the registration behind // (§2.7) — which is the whole reason this list exists. accepts(doc, { op: 'remove_block', slide: 'slidebbbbbbb', id: 'canvasimage1' }) assert.deepEqual(unusedAssets(view(doc)).map(a => a.id), ['usedbyblock1']) }) test('the Unused images list reconciles by id and names each Remove button', () => { const ctx = withDocument() try { const list = stub('ul', { 'slides-unused-list': '' }) const none = stub('p', { 'slides-unused-none': '' }) const assets = [{ id: 'a1', name: 'chart.png' }, { id: 'a2', name: '', ref: 'ff00aa11bb22' }] fillUnused({ list, none, assets, removeLabel: 'Remove' }) assert.equal(none.hidden, true) assert.deepEqual(list.children.map(li => li.dataset.asset), ['a1', 'a2']) const [first, second] = list.children assert.equal(first.querySelector('[slides-asset-name]').textContent, 'chart.png') // No filename: the first eight characters of the address, which is // the only other thing there is to call it. assert.equal(second.querySelector('[slides-asset-name]').textContent, 'ff00aa11') assert.equal(first.querySelector('[data-remove-asset]').getAttribute('aria-label'), 'Remove: chart.png') fillUnused({ list, none, assets, removeLabel: 'Remove' }) assert.equal(list.children[0], first, 'the same element, not a replacement') assert.equal(list.children[1], second) fillUnused({ list, none, assets: [], removeLabel: 'Remove' }) assert.deepEqual(list.children, []) assert.equal(none.hidden, false) } finally { ctx.restore() } }) test('an upload refusal is said in words the person can act on', () => { const labels = { type: 'Use a PNG…', toolarge: 'Bigger than 20 MB.', altfirst: 'Alt text first.', failed: 'Try again.' } const say = (code, message) => uploadMessage({ code, serverMessage: message || '' }, labels) assert.equal(say('type'), labels.type) assert.equal(say('toolarge'), labels.toolarge) assert.equal(say('too_large'), labels.toolarge) assert.equal(say('altfirst'), labels.altfirst) assert.equal(say('nohash'), labels.failed) // The server knows better than the catalog does why IT said no. assert.equal(say('unsafe_svg', 'That SVG carries a script.'), 'That SVG carries a script.') assert.equal(uploadMessage(null, labels), '') })