// commands.mjs: the deck's own commands — the bar's title and Deck // settings' accent reset — are one set_meta each, through commit. import test from 'node:test' import assert from 'node:assert/strict' import { createCommands } from './commands.mjs' function commands () { const batches = [] const c = createCommands({ commit: ops => { batches.push(ops); return Promise.resolve(true) } }) return { c, batches } } test('setTitle commits the deck\'s title, one set_meta', () => { const { c, batches } = commands() c.setTitle('A new talk') assert.deepEqual(batches, [[{ op: 'set_meta', title: 'A new talk' }]]) }) test('accent-reset commits the theme\'s accent back, once', () => { const { c, batches } = commands() c.run('accent-reset') assert.deepEqual(batches, [[{ op: 'set_meta', accent: null }]]) }) test('addBlock commits the kind after the selected box, then selects it; refused, selects nothing', async () => { const slide = { id: 'slideaaaaaaa', layout: 'flow', template: 'title-body', blocks: [{ id: 'blockheading', type: 'heading' }] } const batches = [] const selected = [] let answer = true const c = createCommands({ commit: ops => { batches.push(ops); return Promise.resolve(answer) }, slideId: () => 'slideaaaaaaa', slideJSON: id => id === 'slideaaaaaaa' ? slide : null, selected: () => 'blockheading', select: id => selected.push(id) }) await c.addBlock('shape') assert.equal(batches.length, 1) const [op] = batches[0] assert.equal(op.op, 'add_block') assert.equal(op.slide, 'slideaaaaaaa') assert.equal(op.type, 'shape') assert.equal(op.after, 'blockheading') assert.deepEqual(selected, [op.id]) answer = false await c.addBlock('text') assert.equal(batches.length, 2) assert.deepEqual(selected, [op.id], 'a refused batch selects nothing') }) test('addBlock with no slide on the stage commits nothing', async () => { const batches = [] const c = createCommands({ commit: ops => { batches.push(ops); return Promise.resolve(true) }, slideId: () => '', slideJSON: () => null }) await c.addBlock('shape') assert.deepEqual(batches, []) }) test('the box menu\'s rows into the card: Format and Position and size, for a selected box only', () => { const calls = [] const slide = { id: 'slideaaaaaaa', layout: 'canvas', blocks: [{ id: 'blockshape01', type: 'shape' }] } let selected = 'blockshape01' const c = createCommands({ commit: () => Promise.resolve(true), slideId: () => slide.id, selected: () => selected, slideJSON: () => slide, blockJSON: (s, id) => slide.blocks.find(b => b.id === id) || null, showFormat: () => calls.push('format'), openPlace: () => calls.push('place') }) c.run('block-format') c.run('block-place') assert.deepEqual(calls, ['format', 'place']) selected = '' c.run('block-format') c.run('block-place') assert.deepEqual(calls, ['format', 'place'], 'nothing selected: nothing to show') }) // A follow-along code (the qr block) is added with its link already in // it: the audience link is asked for first (POST /d/{sqid}/qr-link, // editor-entry.js's ctx.qrLink), then the box is one add_block carrying // the follow address, so one undo takes back the box and its link. // "Make an audience link" in its card points the selected code at a // new link, one set_block. With no link to be had (the request refused, // or nobody here may share the deck) the box is still added, empty. test('addBlock("qr") asks for an audience link and adds the box with it; the card\'s button replaces it', async () => { const url = 'https://slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde?follow=1' const slide = { id: 'slideaaaaaaa', layout: 'canvas', template: 'blank', blocks: [] } const batches = [] const selected = [] let link = url let asked = 0 const c = createCommands({ commit: ops => { batches.push(ops); return Promise.resolve(true) }, slideId: () => 'slideaaaaaaa', slideJSON: id => id === 'slideaaaaaaa' ? slide : null, selected: () => selected[selected.length - 1] || '', select: id => selected.push(id), blockJSON: (s, id) => slide.blocks.find(b => b.id === id) || null, qrLink: () => { asked++; return Promise.resolve(link) } }) await c.addBlock('qr') assert.equal(asked, 1) assert.equal(batches.length, 1) const [op] = batches[0] assert.equal(op.op, 'add_block') assert.equal(op.type, 'qr') assert.deepEqual(op.props, { url }) assert.deepEqual(selected, [op.id]) slide.blocks.push({ id: op.id, type: 'qr', props: { url } }) link = url.replace('AbCd', 'WxYz') await c.run('qr-link') assert.equal(asked, 2) assert.deepEqual(batches[1], [{ op: 'set_block', slide: 'slideaaaaaaa', id: op.id, props: { url: link } }]) link = null await c.run('qr-link') assert.equal(batches.length, 2, 'no link, no change') await c.addBlock('qr') assert.equal(batches.length, 3) assert.deepEqual(batches[2][0].props, {}, 'no link: the box is added empty') await c.addBlock('text') assert.equal(asked, 4, 'only a follow-along code asks for a link') })