// Server rediscovery scan (src/native/rediscovery.js, plan step 5) — pure logic, fake network. // Run: npm test import assert from 'node:assert/strict' import { test } from 'node:test' import { scanForVenue, subnetCandidates } from '../src/native/rediscovery.js' test('same-/24 candidates, nearest first, old host excluded', () => { const c = subnetCandidates('http://192.168.1.50') assert.equal(c.length, 253) assert.deepEqual(c.slice(0, 4), ['http://192.168.1.49', 'http://192.168.1.51', 'http://192.168.1.48', 'http://192.168.1.52']) assert.ok(!c.includes('http://192.168.1.50')) }) test('scheme and port are kept (plain HTTP and the TLS entry)', () => { assert.equal(subnetCandidates('http://10.0.0.7:8080')[0], 'http://10.0.0.6:8080') assert.equal(subnetCandidates('https://10.0.0.7:8443')[0], 'https://10.0.0.6:8443') }) test('hostnames and garbage are never scanned', () => { assert.deepEqual(subnetCandidates('http://waiter.example.gr'), []) assert.deepEqual(subnetCandidates('not a url'), []) }) const network = { 'http://192.168.1.12': { app: 'xenia-pos', site_id: 'OTHER-venue', api_version: 1 }, 'http://192.168.1.77': { app: 'xenia-pos', site_id: 'MY-venue', api_version: 1 }, } function fakeProbe() { const calls = { n: 0 } const probe = async origin => { calls.n++; await new Promise(r => setTimeout(r, 1)); return network[origin] ?? null } return { probe, calls } } test('finds this venue at its new address and stops early', async () => { const { probe, calls } = fakeProbe() assert.equal(await scanForVenue('http://192.168.1.50', 'MY-venue', { probe, concurrency: 8 }), 'http://192.168.1.77') assert.ok(calls.n < 253) }) test('another venue\'s server is never chosen', async () => { const { probe } = fakeProbe() assert.equal(await scanForVenue('http://192.168.1.50', 'NOBODY', { probe, concurrency: 8 }), null) }) test('no site id → no scan', async () => { const { probe, calls } = fakeProbe() assert.equal(await scanForVenue('http://192.168.1.50', null, { probe }), null) assert.equal(calls.n, 0) }) test('progress is reported for every probe', async () => { const { probe } = fakeProbe() let progress = 0 await scanForVenue('http://192.168.1.50', 'NOBODY', { probe, concurrency: 16, onProgress: () => progress++ }) assert.equal(progress, 253) })