OverviewFilesCommitsIssuesDocsReleasesPeers

cthuloid / android / test / ipc-discovery.test.js

38 lines · javascript ref:
// The shell's NSD messages become the rig's discovery events.
import test from 'brittle'
import { IpcDiscovery } from '../lib/ipc-discovery.js'

test('unit-up / unit-down become up / down', (t) => {
  const d = new IpcDiscovery()
  const ev = []
  d.on('up', (u) => ev.push(['up', u]))
  d.on('down', (u) => ev.push(['down', u]))
  t.ok(d.handle({ t: 'unit-up', name: 'studiolight', host: 'studiolight.local', addr: '192.168.50.89', port: 1337, proto: '3' }))
  t.ok(d.handle({ t: 'unit-down', name: 'studiolight' }))
  t.alike(ev, [
    ['up', { name: 'studiolight', host: 'studiolight.local', addr: '192.168.50.89', port: 1337, proto: 3 }],
    ['down', { name: 'studiolight' }]
  ])
})

test('incomplete unit-up is ignored, unrelated messages are not ours', (t) => {
  const d = new IpcDiscovery()
  let n = 0
  d.on('up', () => n++)
  t.ok(d.handle({ t: 'unit-up', name: 'x', port: 1337 }), 'consumed')
  t.is(n, 0, 'but no event without an address')
  t.absent(d.handle({ t: 'ping' }))
})

test('availability changes are reported once', (t) => {
  const d = new IpcDiscovery()
  const ev = []
  d.on('unavailable', (r) => ev.push('unavailable:' + r))
  d.on('available', () => ev.push('available'))
  d.handle({ t: 'discovery', available: false, reason: 'wifi off' })
  d.handle({ t: 'discovery', available: false, reason: 'wifi off' })
  d.handle({ t: 'discovery', available: true })
  t.alike(ev, ['unavailable:wifi off', 'available'])
  t.is(d.reason, '')
})