// presenter-notes.mjs: which part is bright, how fit steps down, the // size steps, and the cookie the server reads. import test from 'node:test' import assert from 'node:assert/strict' import { brightPart, stepSize, fitSize, parsePrefs, formatPrefs, readPrefs, writePrefs, fadeEdges, scrollFor, DEFAULT_PANELS } from './presenter-notes.mjs' test('brightPart is the step, or the last part when there are fewer parts than presses', () => { assert.equal(brightPart(['a', 'b', 'c'], 0), 0) assert.equal(brightPart(['a', 'b', 'c'], 2), 2) assert.equal(brightPart(['a', 'b', 'c'], 4), 2) assert.equal(brightPart(['only'], 3), 0) assert.equal(brightPart(3, 1), 1) assert.equal(brightPart(['a', 'b'], -1), 0) }) test('fitSize steps down a pixel to the floor and stops there', () => { assert.equal(fitSize(24, () => false), 24) assert.equal(fitSize(24, px => px > 19), 19) assert.equal(fitSize(24, () => true), 16) assert.equal(fitSize(48, px => px > 30), 30) }) test('smaller and larger step 2px between 16 and 48', () => { assert.equal(stepSize(24, 1), 26) assert.equal(stepSize(24, -1), 22) assert.equal(stepSize(16, -1), 16) assert.equal(stepSize(48, 1), 48) }) test('the cookie is percent-encoded and keeps the panels', () => { const v = formatPrefs({ notes: 'scroll', size: 30, panels: DEFAULT_PANELS }) assert.ok(!v.includes(';')) assert.deepEqual(parsePrefs(decodeURIComponent(v)), { notes: 'scroll', size: 30, panels: DEFAULT_PANELS }) assert.equal(parsePrefs('fit.15.x'), null) assert.equal(parsePrefs('wide.24.x'), null) assert.equal(formatPrefs({ notes: 'fit', size: 24 }), encodeURIComponent('fit.24.' + DEFAULT_PANELS)) }) test('writePrefs sets path /p, a year, SameSite=Lax, and readPrefs reads it back', () => { const doc = { cookie: '' } const panels = '1,1,2,2;3,3,2,2;5,5,2,2' writePrefs({ notes: 'fit', size: 26, panels }, doc, { protocol: 'http:' }) assert.match(doc.cookie, /^slides-presenter=[^;]+; Path=\/p; Max-Age=31536000; SameSite=Lax$/) const jar = { cookie: 'a=b; ' + doc.cookie.split(';')[0] } assert.deepEqual(readPrefs(jar), { notes: 'fit', size: 26, panels }) assert.equal(readPrefs({ cookie: 'a=b' }), null) }) test('the fade is on the sides with more', () => { assert.equal(fadeEdges(0, 100, 100), '') assert.equal(fadeEdges(0, 100, 300), 'bottom') assert.equal(fadeEdges(100, 100, 300), 'both') assert.equal(fadeEdges(200, 100, 300), 'top') }) test('a part already in view is left; otherwise its top is brought up', () => { assert.equal(scrollFor(10, 50, 0, 100), null) assert.equal(scrollFor(150, 220, 0, 100), 150) assert.equal(scrollFor(-5, 40, 0, 100), 0) })