oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// presenter-notes.mjs: which part is bright, how fit steps down, the
2// size steps, and the cookie the server reads.
3import test from 'node:test'
4import assert from 'node:assert/strict'
5import { brightPart, stepSize, fitSize, parsePrefs, formatPrefs, readPrefs, writePrefs, fadeEdges, scrollFor, DEFAULT_PANELS } from './presenter-notes.mjs'
6
7test('brightPart is the step, or the last part when there are fewer parts than presses', () => {
8 assert.equal(brightPart(['a', 'b', 'c'], 0), 0)
9 assert.equal(brightPart(['a', 'b', 'c'], 2), 2)
10 assert.equal(brightPart(['a', 'b', 'c'], 4), 2)
11 assert.equal(brightPart(['only'], 3), 0)
12 assert.equal(brightPart(3, 1), 1)
13 assert.equal(brightPart(['a', 'b'], -1), 0)
14})
15
16test('fitSize steps down a pixel to the floor and stops there', () => {
17 assert.equal(fitSize(24, () => false), 24)
18 assert.equal(fitSize(24, px => px > 19), 19)
19 assert.equal(fitSize(24, () => true), 16)
20 assert.equal(fitSize(48, px => px > 30), 30)
21})
22
23test('smaller and larger step 2px between 16 and 48', () => {
24 assert.equal(stepSize(24, 1), 26)
25 assert.equal(stepSize(24, -1), 22)
26 assert.equal(stepSize(16, -1), 16)
27 assert.equal(stepSize(48, 1), 48)
28})
29
30test('the cookie is percent-encoded and keeps the panels', () => {
31 const v = formatPrefs({ notes: 'scroll', size: 30, panels: DEFAULT_PANELS })
32 assert.ok(!v.includes(';'))
33 assert.deepEqual(parsePrefs(decodeURIComponent(v)), { notes: 'scroll', size: 30, panels: DEFAULT_PANELS })
34 assert.equal(parsePrefs('fit.15.x'), null)
35 assert.equal(parsePrefs('wide.24.x'), null)
36 assert.equal(formatPrefs({ notes: 'fit', size: 24 }), encodeURIComponent('fit.24.' + DEFAULT_PANELS))
37})
38
39test('writePrefs sets path /p, a year, SameSite=Lax, and readPrefs reads it back', () => {
40 const doc = { cookie: '' }
41 const panels = '1,1,2,2;3,3,2,2;5,5,2,2'
42 writePrefs({ notes: 'fit', size: 26, panels }, doc, { protocol: 'http:' })
43 assert.match(doc.cookie, /^slides-presenter=[^;]+; Path=\/p; Max-Age=31536000; SameSite=Lax$/)
44 const jar = { cookie: 'a=b; ' + doc.cookie.split(';')[0] }
45 assert.deepEqual(readPrefs(jar), { notes: 'fit', size: 26, panels })
46 assert.equal(readPrefs({ cookie: 'a=b' }), null)
47})
48
49test('the fade is on the sides with more', () => {
50 assert.equal(fadeEdges(0, 100, 100), '')
51 assert.equal(fadeEdges(0, 100, 300), 'bottom')
52 assert.equal(fadeEdges(100, 100, 300), 'both')
53 assert.equal(fadeEdges(200, 100, 300), 'top')
54})
55
56test('a part already in view is left; otherwise its top is brought up', () => {
57 assert.equal(scrollFor(10, 50, 0, 100), null)
58 assert.equal(scrollFor(150, 220, 0, 100), 150)
59 assert.equal(scrollFor(-5, 40, 0, 100), 0)
60})
61