oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// The asset half of the inspector (spec §2.7): one finished upload as
2// the two ops it is, the Unused images list derived from the document,
3// and the words a refusal is said in.
4//
5// A file of its own rather than more of inspector.test.mjs: that one is
6// about controls and their ops, and this is about a thing that arrives
7// from outside the document and then joins it.
8import test from 'node:test'
9import assert from 'node:assert/strict'
10import * as Y from 'yjs'
11import { applyOps, view } from './ops.mjs'
12import { registerAssetOps, registerClipOps, removeAssetOp, unusedAssets } from './inspector-ops.mjs'
13import { fillUnused, uploadMessage } from './inspector-image.mjs'
14import { el as stub, withDocument } from './dom-stub.mjs'
15
16function deck () {
17 const doc = new Y.Doc()
18 applyOps(doc, [
19 { op: 'set_meta', title: 'T', theme: 'paper' },
20 { op: 'add_slide', id: 'slideaaaaaaa', layout: 'flow', template: 'title-body' },
21 { op: 'add_slide', id: 'slidebbbbbbb', layout: 'canvas' },
22 { op: 'add_block', slide: 'slidebbbbbbb', id: 'canvasimage1', type: 'image', box: { x: 0.2, y: 0.6, w: 0.3, h: 0.2 }, z: 0 },
23 { op: 'add_slide', id: 'slideccccccc', layout: 'flow', template: 'blank' }
24 ], 'test')
25 return doc
26}
27const accepts = (doc, ops) => assert.doesNotThrow(() => applyOps(doc, Array.isArray(ops) ? ops : [ops], 'test'))
28
29test('one finished upload is register_asset plus the block that uses it, in one batch', () => {
30 const doc = deck()
31 const record = { id: 'assetaaaaaaa', ref: 'aa01bb02', name: 'chart.png', width: 1600, height: 900, alt: 'A bar chart' }
32 const ops = registerAssetOps(record, 'slidebbbbbbb', 'canvasimage1')
33 assert.deepEqual(ops, [
34 { op: 'register_asset', id: 'assetaaaaaaa', ref: 'aa01bb02', name: 'chart.png', width: 1600, height: 900, alt: 'A bar chart' },
35 { op: 'set_block', slide: 'slidebbbbbbb', id: 'canvasimage1', props: { asset: 'assetaaaaaaa' } }
36 ])
37 accepts(doc, ops)
38 const v = view(doc)
39 assert.equal(v.assets.assetaaaaaaa.ref, 'aa01bb02')
40 assert.equal(v.slides[1].blocks[0].props.asset, 'assetaaaaaaa')
41
42 // With no block selected it is the registration alone — still legal,
43 // and it lands in the Unused images list where somebody can drop it.
44 assert.equal(registerAssetOps({ ...record, id: 'assetbbbbbbb' }, '', '').length, 1)
45})
46
47test('a record missing its numbers still makes an op the core accepts', () => {
48 const doc = deck()
49 accepts(doc, registerAssetOps({ id: 'assetcccccccc', ref: 'r9' }, '', ''))
50 const a = view(doc).assets.assetcccccccc
51 assert.equal(a.width, 0)
52 assert.equal(a.height, 0)
53 assert.equal(a.alt, '')
54})
55
56test('unusedAssets is what no slide refers to, and remove_asset drops one', () => {
57 const doc = deck()
58 applyOps(doc, [
59 ...registerAssetOps({ id: 'usedbyblock1', ref: 'r1', name: 'used.png', width: 1, height: 1, alt: 'a' }, 'slidebbbbbbb', 'canvasimage1'),
60 ...registerAssetOps({ id: 'usedasbackgr', ref: 'r2', name: 'bg.png', width: 1, height: 1, alt: 'b' }, '', ''),
61 ...registerAssetOps({ id: 'usedbynobody', ref: 'r3', name: 'orphan.png', width: 1, height: 1, alt: 'c' }, '', ''),
62 { op: 'set_slide', id: 'slideccccccc', background: { kind: 'image', value: 'usedasbackgr' } }
63 ], 'test')
64
65 const v = view(doc)
66 assert.deepEqual(unusedAssets(v).map(a => a.id), ['usedbynobody'])
67 // The entry carries the asset's own fields, so the list can name it.
68 assert.equal(unusedAssets(v)[0].name, 'orphan.png')
69
70 accepts(doc, removeAssetOp('usedbynobody'))
71 assert.deepEqual(unusedAssets(view(doc)), [])
72
73 // Deleting the block that used one leaves the registration behind
74 // (§2.7) — which is the whole reason this list exists.
75 accepts(doc, { op: 'remove_block', slide: 'slidebbbbbbb', id: 'canvasimage1' })
76 assert.deepEqual(unusedAssets(view(doc)).map(a => a.id), ['usedbyblock1'])
77})
78
79test('a finished clip is its poster, its video and the block naming it, in one batch; the poster is in use', () => {
80 const doc = deck()
81 accepts(doc, { op: 'add_block', slide: 'slidebbbbbbb', id: 'clipblock001', type: 'clip', box: { x: 0.1, y: 0.1, w: 0.4, h: 0.3 }, props: { play: 'press' } })
82 const poster = { id: 'posteraaaaaa', ref: 'pp01', name: 'demo.webp', width: 640, height: 360, alt: '' }
83 const video = { id: 'videoaaaaaaa', ref: 'vv01', name: 'demo.webm', width: 640, height: 360, alt: '', poster: 'pp01', duration: 2500 }
84 const ops = registerClipOps(video, poster, 'slidebbbbbbb', 'clipblock001')
85 assert.deepEqual(ops, [
86 { op: 'register_asset', id: 'posteraaaaaa', ref: 'pp01', name: 'demo.webp', width: 640, height: 360, alt: '' },
87 { op: 'register_asset', id: 'videoaaaaaaa', ref: 'vv01', name: 'demo.webm', width: 640, height: 360, alt: '', poster: 'pp01', duration: 2500 },
88 { op: 'set_block', slide: 'slidebbbbbbb', id: 'clipblock001', props: { asset: 'videoaaaaaaa' } }
89 ])
90 accepts(doc, ops)
91 assert.equal(view(doc).assets.videoaaaaaaa.poster, 'pp01')
92 // The poster is used by the video the block shows; once no block shows
93 // the video, both are unused.
94 assert.deepEqual(unusedAssets(view(doc)), [])
95 accepts(doc, { op: 'remove_block', slide: 'slidebbbbbbb', id: 'clipblock001' })
96 assert.deepEqual(unusedAssets(view(doc)).map(a => a.id).sort(), ['posteraaaaaa', 'videoaaaaaaa'])
97})
98
99test('a scene still is in use: its asset is never offered as unused', () => {
100 const doc = deck()
101 const still = 'e'.repeat(64)
102 accepts(doc, [
103 ...registerAssetOps({ id: 'stillasset01', ref: still, name: 'a.png', width: 1, height: 1, alt: '' }, '', ''),
104 { op: 'add_scene', id: 'worldsceneaa', name: 'World', code: 'a'.repeat(64), states: [{ id: 'a' }] },
105 { op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: still, made: 'b'.repeat(64) } } }
106 ])
107 assert.deepEqual(unusedAssets(view(doc)), [])
108 // Once no scene's still names it, it is unused like any other.
109 accepts(doc, { op: 'set_scene', id: 'worldsceneaa', stills: { a: null } })
110 assert.deepEqual(unusedAssets(view(doc)).map(a => a.id), ['stillasset01'])
111})
112
113test('the Unused images list reconciles by id and names each Remove button', () => {
114 const ctx = withDocument()
115 try {
116 const list = stub('ul', { 'slides-unused-list': '' })
117 const none = stub('p', { 'slides-unused-none': '' })
118 const assets = [{ id: 'a1', name: 'chart.png' }, { id: 'a2', name: '', ref: 'ff00aa11bb22' }]
119
120 fillUnused({ list, none, assets, removeLabel: 'Remove' })
121 assert.equal(none.hidden, true)
122 assert.deepEqual(list.children.map(li => li.dataset.asset), ['a1', 'a2'])
123 const [first, second] = list.children
124 assert.equal(first.querySelector('[slides-asset-name]').textContent, 'chart.png')
125 // No filename: the first eight characters of the address, which is
126 // the only other thing there is to call it.
127 assert.equal(second.querySelector('[slides-asset-name]').textContent, 'ff00aa11')
128 assert.equal(first.querySelector('[data-remove-asset]').getAttribute('aria-label'), 'Remove: chart.png')
129
130 fillUnused({ list, none, assets, removeLabel: 'Remove' })
131 assert.equal(list.children[0], first, 'the same element, not a replacement')
132 assert.equal(list.children[1], second)
133
134 fillUnused({ list, none, assets: [], removeLabel: 'Remove' })
135 assert.deepEqual(list.children, [])
136 assert.equal(none.hidden, false)
137 } finally {
138 ctx.restore()
139 }
140})
141
142test('an upload refusal is said in words the person can act on', () => {
143 const labels = { type: 'Use a PNG…', toolarge: 'Bigger than 20 MB.', altfirst: 'Alt text first.', failed: 'Try again.' }
144 const say = (code, message) => uploadMessage({ code, serverMessage: message || '' }, labels)
145 assert.equal(say('type'), labels.type)
146 assert.equal(say('toolarge'), labels.toolarge)
147 assert.equal(say('too_large'), labels.toolarge)
148 assert.equal(say('altfirst'), labels.altfirst)
149 assert.equal(say('nohash'), labels.failed)
150 // The server knows better than the catalog does why IT said no.
151 assert.equal(say('unsafe_svg', 'That SVG carries a script.'), 'That SVG carries a script.')
152 assert.equal(uploadMessage(null, labels), '')
153})
154