// fixtures/talk-link.test.mjs — the link between a window and the // server (talk-link.mjs), on a fake clock and a fake network: what is // sent, in what order, and which of the server's positions the window // is asked to take. import { test } from 'node:test' import assert from 'node:assert/strict' import { createLink, pressID } from './talk-link.mjs' // fakeNet is the network: every post waits until the test answers or // fails it, and the stream's handlers are the test's to call. function fakeNet () { const posts = [] const net = { posts, handlers: null, post (fields) { return new Promise((resolve, reject) => posts.push({ fields, resolve, reject })) }, stream (handlers) { net.handlers = handlers return () => { net.handlers = null } } } return net } // fakeClock is time that moves only when the test says so. function fakeClock () { let t = 0 let timers = [] return { now: () => t, after (ms, fn) { const timer = { at: t + ms, fn } timers.push(timer) return () => { timers = timers.filter(x => x !== timer) } }, advance (ms) { t += ms const due = timers.filter(x => x.at <= t) timers = timers.filter(x => x.at > t) for (const x of due) x.fn() } } } const settle = () => new Promise(resolve => setImmediate(resolve)) const at = (slide, step, seq) => ({ slide, step, mode: 'core', black: false, seq, deckSeq: 1 }) function rig (start = at('a', 0, 1)) { const net = fakeNet() const clock = fakeClock() const moved = [] const statuses = [] const timers = [] const link = createLink({ post: net.post, stream: net.stream, now: clock.now, after: clock.after, onPosition: pos => moved.push(pos), onStatus: s => statuses.push(s), onTimer: ms => timers.push(ms) }) link.resume(start) net.handlers.open() net.handlers.position(start) return { net, clock, link, moved, statuses, timers } } test('a press is sent once and in order', async () => { const { net, link } = rig() link.press({ op: 'next' }, at('a', 1)) link.press({ op: 'next' }, at('a', 2)) link.press({ op: 'back' }, at('a', 1)) link.press({ op: 'goto', slide: 'c' }, at('c', 0)) await settle() assert.equal(net.posts.length, 1, 'one press on the wire at a time') const ids = new Set() const ops = [] for (let i = 0; i < 4; i++) { const p = net.posts[i] assert.ok(p, `press ${i + 1} was sent`) ops.push(p.fields.op + (p.fields.slide ? ':' + p.fields.slide : '')) assert.match(p.fields.press, /^[A-Za-z0-9_-]{22}$/) ids.add(p.fields.press) p.resolve(at('x', 0, 2 + i)) await settle() } assert.deepEqual(ops, ['next', 'next', 'prev', 'goto:c']) assert.equal(ids.size, 4, 'each press has its own id') assert.equal(net.posts.length, 4, 'nothing sent twice') }) test('a late answer does not move a window with presses waiting', async () => { const { net, link, moved } = rig() link.press({ op: 'next' }, at('a', 1)) link.press({ op: 'next' }, at('a', 2)) await settle() // The phone pressed meanwhile: heard, not taken. net.handlers.position(at('b', 0, 2)) net.posts[0].resolve(at('b', 0, 3)) await settle() assert.deepEqual(moved, [], 'nothing applied while a press waits') net.posts[1].resolve(at('b', 1, 4)) await settle() assert.deepEqual(moved.map(p => [p.slide, p.step]), [['b', 1]], 'the newest, once the queue is empty') // An older event arriving late changes nothing. net.handlers.position(at('a', 2, 3)) await settle() assert.equal(moved.length, 1) // Its own answer, where the window already stands, moves nothing. link.press({ op: 'next' }, at('b', 2)) await settle() net.posts[2].resolve(at('b', 2, 5)) await settle() assert.equal(moved.length, 1) net.handlers.position(at('c', 0, 6)) await settle() assert.deepEqual(moved.map(p => p.slide), ['b', 'c']) }) test('offline presses end in one resume', async () => { const { net, clock, link, statuses } = rig() link.press({ op: 'next' }, at('a', 1)) link.press({ op: 'next' }, at('a', 2)) await settle() // No answer in three seconds: offline, and the waiting press dropped. clock.advance(3001) await settle() assert.equal(link.status(), 'offline') assert.equal(statuses.at(-1), 'offline') link.press({ op: 'next' }, at('b', 0)) link.press({ op: 'next' }, at('c', 0)) await settle() assert.equal(net.posts.length, 1, 'nothing sent while offline') // The late answer to the dropped press changes nothing. net.posts[0].resolve(at('a', 1, 2)) net.handlers.error() await settle() net.handlers.open() await settle() assert.equal(link.status(), 'online') assert.equal(net.posts.length, 2, 'one resume') assert.deepEqual( { op: net.posts[1].fields.op, slide: net.posts[1].fields.at_slide, step: net.posts[1].fields.at_step, mode: net.posts[1].fields.at_mode, black: net.posts[1].fields.at_black }, { op: 'resume', slide: 'c', step: '0', mode: 'core', black: 'false' }) net.handlers.position(at('z', 0, 9)) // the phone's press, made while cut net.posts[1].resolve(at('c', 0, 10)) await settle() net.handlers.open() await settle() assert.equal(net.posts.length, 2, 'still one resume') }) test('a failed send is offline at once', async () => { const { net, link } = rig() link.press({ op: 'next' }, at('a', 1)) await settle() net.posts[0].reject(new Error('offline')) await settle() assert.equal(link.status(), 'offline') }) test('a stream that comes back by itself is not a split', async () => { const { net, clock, link, statuses } = rig() net.handlers.error() clock.advance(3000) net.handlers.open() clock.advance(5000) await settle() assert.equal(link.status(), 'online') assert.deepEqual(statuses, []) }) test('a window that pressed nothing takes the server\'s position', async () => { const { net, clock, link, moved, statuses } = rig() net.handlers.error() clock.advance(5001) await settle() assert.equal(link.status(), 'offline') net.handlers.open() net.handlers.position(at('d', 1, 7)) await settle() assert.equal(link.status(), 'online') assert.deepEqual(statuses, ['offline', 'online']) assert.equal(net.posts.length, 0, 'no resume') assert.deepEqual(moved.map(p => [p.slide, p.step]), [['d', 1]]) }) test('a press id is 22 characters of the allowed alphabet', () => { const seen = new Set() for (let i = 0; i < 200; i++) { const id = pressID() assert.match(id, /^[A-Za-z0-9_-]{22}$/) seen.add(id) } assert.equal(seen.size, 200) }) test('a press given up on is numbered below the resume that replaces it', async () => { const { net, clock, link } = rig() link.press({ op: 'next' }, at('a', 1)) await settle() link.press({ op: 'next' }, at('a', 2)) clock.advance(3001) await settle() link.press({ op: 'next' }, at('b', 0)) net.handlers.open() net.handlers.position(at('a', 0, 2)) await settle() const [abandoned, resume] = net.posts assert.equal(resume.fields.op, 'resume') assert.match(abandoned.fields.link, /^[A-Za-z0-9_-]{22}$/) assert.equal(resume.fields.link, abandoned.fields.link, 'one link for the window') // The server refuses a command of this link numbered at or below one // it has applied: the resume outnumbers every press sent before it. assert.ok(Number(resume.fields.n) > Number(abandoned.fields.n), `resume n ${resume.fields.n} above the abandoned ${abandoned.fields.n}`) }) test('every command carries the link and a rising number', async () => { const { net, link } = rig() for (let i = 0; i < 3; i++) link.press({ op: 'next' }, at('a', i + 1)) for (let i = 0; i < 3; i++) { await settle() net.posts[i].resolve(at('a', i + 1, 2 + i)) } await settle() const ns = net.posts.map(p => Number(p.fields.n)) assert.deepEqual(ns, [1, 2, 3]) assert.equal(new Set(net.posts.map(p => p.fields.link)).size, 1) }) test('the black screen pressed offline goes with the resume, either way', async () => { for (const black of [true, false]) { const { net, clock, link } = rig({ ...at('a', 0, 1), black: !black }) link.press({ op: 'black' }, { ...at('a', 0), black }) await settle() net.posts[0].reject(new Error('offline')) await settle() clock.advance(2001) await settle() assert.equal(net.posts[1].fields.op, 'resume') assert.equal(net.posts[1].fields.at_black, String(black)) } }) test('the timer is taken from the server even where the position is the same', async () => { const { net, moved, timers } = rig() assert.deepEqual(timers, [], 'a position that says nothing of a timer reports none') net.handlers.position({ ...at('a', 0, 2), timerAt: 5000 }) assert.deepEqual(timers, [5000], 'another window pressed: the timer started') assert.equal(moved.length, 0, 'and the window did not move') net.handlers.position({ ...at('a', 0, 3), timerAt: null }) assert.deepEqual(timers, [5000, null], 'a reset elsewhere is heard as null') net.handlers.position({ ...at('a', 0, 2), timerAt: 7000 }) assert.deepEqual(timers, [5000, null], 'an older event says nothing') }) test('a stale timer never undoes a press still waiting for its answer', async () => { const { net, link, timers } = rig() link.press({ op: 'next' }, at('a', 1)) await settle() net.handlers.position({ ...at('a', 0, 2), timerAt: null }) assert.deepEqual(timers, [], 'nothing is taken while a press waits') net.posts[0].resolve({ ...at('a', 1, 3), timerAt: 9000 }) await settle() assert.deepEqual(timers, [9000], 'the answer brings the timer the press started') })