// inspector-steps.test.mjs — how a box appears (spec §4, In the editor): // the three choices as ops, After… as a midpoint that renumbers nothing, // and the press numbers drawn beside the boxes. import test from 'node:test' import assert from 'node:assert/strict' import { appearsOf, appearsOps, afterChoices, afterOps } from './inspector-steps.mjs' import { appearsShown } from './box-menu-appears.mjs' import { marksFor } from './step-marks.mjs' import { presses } from './presses.mjs' const para = t => [{ t }] const box = (id, extra = {}) => ({ id, type: 'text', props: {}, text: [para(id)], ...extra }) const list = (id, n, extra = {}) => ({ id, type: 'list', props: {}, text: Array.from({ length: n }, (_, i) => para(`${id} ${i}`)), ...extra }) const slideOf = (...blocks) => ({ id: 's1', layout: 'flow', blocks }) const set = (id, fields) => ({ op: 'set_block', slide: 's1', id, ...fields }) test('appearsOf: with the slide, on a press, item by item', () => { assert.equal(appearsOf(box('a')), 'slide') assert.equal(appearsOf(box('a', { step: 2 })), 'press') assert.equal(appearsOf(list('l', 3, { step: 1, items: 'each' })), 'each') // A list that is each but has no step arrives with the slide. assert.equal(appearsOf(list('l', 3, { items: 'each' })), 'slide') }) test('appearsOps: On a press takes the next step after the largest', () => { const a = box('a') const s = slideOf(box('x', { step: 1 }), box('y', { step: 2.5 }), a) assert.deepEqual(appearsOps(s, a, 'press'), [set('a', { step: 3.5 })]) // The first press on a slide is 1. const lone = box('lone') assert.deepEqual(appearsOps(slideOf(lone), lone, 'press'), [set('lone', { step: 1 })]) // A box already on a press keeps its step: nothing to write. const on = box('on', { step: 2 }) assert.deepEqual(appearsOps(slideOf(on), on, 'press'), []) // From item by item, the list keeps its press and arrives whole. const l = list('l', 3, { step: 2, items: 'each' }) assert.deepEqual(appearsOps(slideOf(l), l, 'press'), [set('l', { items: null })]) // A list with items "each" left over but no step arrives with the // slide; On a press gives it a step and takes the items away, so it // is on a press, not item by item. const latent = list('latent', 3, { items: 'each' }) assert.equal(appearsOf(latent), 'slide') const ops = appearsOps(slideOf(box('x', { step: 2 }), latent), latent, 'press') assert.deepEqual(ops, [set('latent', { step: 3, items: null })]) const { items, ...after } = latent assert.equal(appearsOf({ ...after, step: ops[0].step }), 'press') }) test('appearsOps: With the slide clears the step and the items', () => { const a = box('a', { step: 4 }) assert.deepEqual(appearsOps(slideOf(a), a, 'slide'), [set('a', { step: null, items: null })]) const l = list('l', 2, { step: 1, items: 'each' }) assert.deepEqual(appearsOps(slideOf(l), l, 'slide'), [set('l', { step: null, items: null })]) // Already with the slide: nothing. const b = box('b') assert.deepEqual(appearsOps(slideOf(b), b, 'slide'), []) }) test('appearsOps: Item by item is a list\'s, and gives it a press when it has none', () => { const l = list('l', 3) const s = slideOf(box('x', { step: 2 }), l) assert.deepEqual(appearsOps(s, l, 'each'), [set('l', { step: 3, items: 'each' })]) const on = list('on', 3, { step: 1 }) assert.deepEqual(appearsOps(slideOf(on), on, 'each'), [set('on', { items: 'each' })]) const done = list('d', 3, { step: 1, items: 'each' }) assert.deepEqual(appearsOps(slideOf(done), done, 'each'), []) // Not a list: nothing. const t = box('t') assert.deepEqual(appearsOps(slideOf(t), t, 'each'), []) assert.deepEqual(appearsOps(slideOf(t), t, 'nonsense'), []) }) test('afterOps: the midpoint between the press and the next one, nothing renumbered', () => { const x = box('x', { step: 1 }) const y = box('y', { step: 2 }) const a = box('a') const s = slideOf(x, y, a) const ops = afterOps(s, a, 1) assert.deepEqual(ops, [set('a', { step: 1.5 })]) // Only the box moved: every other step is as it was. assert.ok(ops.every(op => op.id === 'a')) // After the last press: one more than it. assert.deepEqual(afterOps(s, a, 2), [set('a', { step: 3 })]) // Between two halves, a quarter. const z = box('z', { step: 1.5 }) assert.deepEqual(afterOps(slideOf(x, z, y, a), a, 1), [set('a', { step: 1.25 })]) // The box's own step is not a press to fit between: already there, // nothing to write. const self = box('self', { step: 2 }) assert.deepEqual(afterOps(slideOf(x, self, box('w', { step: 3 })), self, 1), []) assert.deepEqual(afterOps(slideOf(x, self, box('w', { step: 3 })), self, 3), [set('self', { step: 4 })]) }) test('afterOps: the box ends up in the press after the one chosen', () => { const s = slideOf(box('x', { step: 1 }), box('y', { step: 2 }), box('a', { step: 5 })) const [op] = afterOps(s, s.blocks[2], 1) const moved = slideOf(s.blocks[0], s.blocks[1], { ...s.blocks[2], step: op.step }) assert.deepEqual(presses(moved).map(p => p.map(m => m.block)), [['x'], ['a'], ['y']]) }) test('afterChoices: each press of the other boxes, by number; an item-by-item list counts its items', () => { const l = list('l', 3, { step: 1, items: 'each' }) const y = box('y', { step: 2 }) const a = box('a', { step: 7 }) const s = slideOf(l, y, a) // Presses: l0, l1, l2 (value 1), y (value 2), a (value 7). After a // value means after its last press. assert.deepEqual(afterChoices(s, a), [{ n: 3, value: 1 }, { n: 4, value: 2 }]) // Nothing on a slide without steps, nor when the box is the only press. const b = box('b') assert.deepEqual(afterChoices(slideOf(b, box('c')), b), []) const only = box('only', { step: 1 }) assert.deepEqual(afterChoices(slideOf(only, box('c')), only), []) }) test('appearsShown: Item by item is offered on a list alone', () => { assert.deepEqual(appearsShown(list('l', 2)), ['slide', 'press', 'each']) assert.deepEqual(appearsShown(box('t')), ['slide', 'press']) }) test('marksFor: press numbers in press order, not step values; a list item by item numbers each item', () => { const l = list('l', 2, { step: 10, items: 'each' }) const s = slideOf(box('a', { step: 0.5 }), l, box('b'), box('c', { step: 0.5 }), box('d', { step: 40 })) assert.deepEqual(marksFor(s), [ { block: 'a', item: -1, n: 1 }, { block: 'c', item: -1, n: 1 }, { block: 'l', item: 0, n: 2 }, { block: 'l', item: 1, n: 3 }, { block: 'd', item: -1, n: 4 } ]) assert.deepEqual(marksFor(slideOf(box('a'), box('b'))), []) }) test('a scene block counts in the editor\'s press numbering, and After… never offers press value 0', () => { const scenes = { w: { states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }] } } const sc = { id: 'sc', type: 'scene', props: { scene: 'w', from: 'a', to: 'c' } } const t1 = box('t1', { step: 1 }) const t2 = box('t2') const s = slideOf(sc, t1, t2) // The scene's two states after from are presses 1 and 2, with no step // of its own (value 0); the text on step 1 is press 3. One mark for // the scene, its first and last press. assert.deepEqual(marksFor(s, scenes), [{ block: 'sc', item: -1, n: 1, last: 2 }, { block: 't1', item: -1, n: 3 }]) // After… lists the text's press by its number, never the scene's // value 0, which is not a step a box can be put after. assert.deepEqual(afterChoices(s, t2, scenes), [{ n: 3, value: 1 }]) assert.deepEqual(afterOps(s, t2, 1, scenes), [set('t2', { step: 2 })]) assert.deepEqual(afterChoices(slideOf(sc, t2), t2, scenes), []) // A scene of one press is one number. const one = { id: 'one', type: 'scene', props: { scene: 'w', from: 'b', to: 'c' } } assert.deepEqual(marksFor(slideOf(one), scenes), [{ block: 'one', item: -1, n: 1 }]) })