// The surface port must survive a restart.
//
// ⛔ THE BUG THIS FIXES, SEEN ON THE HANDSET: "restart to apply" appeared to do nothing and the update
// banner looked frozen at the top of the page. The node HAD restarted and WAS serving the new bundle —
// logcat showed it booting the staged tree and reaching ready — but every boot bound a fresh ephemeral
// port, so the WebView was still pointing at the previous one. A dead URL renders the page it last
// loaded, which is indistinguishable from nothing having happened.
import test from 'brittle'
import http from 'bare-http1'
import { listenPreferring } from '../lib/node.js'
const close = (s) => new Promise((r) => s.close(r))
test('a remembered port is bound again', async (t) => {
const a = http.createServer(() => {})
await listenPreferring(a, 0)
const port = a.address().port
await close(a)
const b = http.createServer(() => {})
await listenPreferring(b, port)
t.is(b.address().port, port, 'the same URL keeps working across a restart')
await close(b)
})
test('a port someone else holds falls back instead of failing to boot', async (t) => {
// Refusing to start over a port number would be a far worse failure than a changed URL.
const squatter = http.createServer(() => {})
await listenPreferring(squatter, 0)
const taken = squatter.address().port
const mine = http.createServer(() => {})
await listenPreferring(mine, taken)
t.ok(mine.address().port > 0, 'still bound')
t.not(mine.address().port, taken, 'on a different port')
await close(mine); await close(squatter)
})
test('port 0 still means any free port', async (t) => {
const s = http.createServer(() => {})
await listenPreferring(s, 0)
t.ok(s.address().port > 0)
await close(s)
})