OverviewFilesCommitsIssuesDocsReleasesPeers

favela-surface / test / router-import.test.js

35 lines · javascript ref:
// ⛔ Importing the ROUTER must not start a SERVER.
//
// This is a regression test for a real failure: `lib/node.js` in favela-surface-android imported
// `favela-surface/index.js` to reach `route`, which bound port 4042 at module load — and on a
// machine already running favela-surface it threw EADDRINUSE before a single test ran.
//
// An entrypoint boots. A library exports. The worklet wants the routes and supplies its own
// listener, and nothing about that should require a port to be free.

import test from 'brittle'
import fs from '#node:fs'
import { join } from '#node:path'

const ROOT = new URL('..', import.meta.url).pathname

test('★ lib/router.js binds nothing and reads no config', async (t) => {
  const src = fs.readFileSync(join(ROOT, 'lib/router.js'), 'utf8')
  t.absent(/\.listen\(/.test(src), 'no listener')
  t.absent(/createServer/.test(src), 'no server')
  // ⛔ Nor does it decide where the daemon is. `ctx.client.call(cmd, args)` is the whole contract,
  // satisfied by a socket client on a desktop and a direct dispatch binding in a worklet.
  t.absent(/new Client\(/.test(src), 'and it constructs no client')
  t.absent(/process\.env/.test(src), 'no environment — an embedder has no env to read')

  // The proof rather than the proxy: importing it must simply work, with no port free.
  const mod = await import('../lib/router.js')
  t.is(typeof mod.route, 'function', 'and importing it just gives you the routes')
})

test('the entrypoint is the only thing that listens', (t) => {
  const src = fs.readFileSync(join(ROOT, 'index.js'), 'utf8')
  t.ok(/server\.listen\(/.test(src), 'index.js binds the port')
  t.ok(/new Client\(/.test(src), 'and index.js is where the socket client is built')
})