fix(cli): sample LAN addresses once — trust and the printed LAN URL share the snapshot

web.ts re-sampled interfaces after boot, so an address change during
entry.run() could advertise a LAN URL absent from the trustedHosts snapshot
composePatches captured, answering 403 on arrival. resolveLanTrust now
returns the single sample and AppCLIEntry exposes it for display.
This commit is contained in:
creatixchu
2026-07-28 17:47:56 +08:00
parent 772653464d
commit f43cfb2406
3 changed files with 43 additions and 36 deletions

View File

@@ -1,7 +1,7 @@
/** LAN-authority derivation for the /api browser-trust fence (`resolveTrustedHosts`). */
/** Single-sample LAN-trust resolution for the /api browser-trust fence (`resolveLanTrust`). */
import { afterEach, describe, expect, it, vi } from 'vitest'
import { lanIPv4Addresses, resolveTrustedHosts } from '../src/app-cli-entry.ts'
import { describe, expect, it, vi } from 'vitest'
import { resolveLanTrust } from '../src/app-cli-entry.ts'
vi.mock('node:os', () => ({
networkInterfaces: () => ({
@@ -19,22 +19,15 @@ vi.mock('node:os', () => ({
}),
}))
afterEach(() => { vi.restoreAllMocks() })
describe('resolveLanTrust', () => {
it('samples non-internal IPv4 addresses once for an all-interfaces bind: trust and display share them', () => {
const { lanAddresses, trustedHosts } = resolveLanTrust('0.0.0.0', ['harness.internal:3080'])
expect(lanAddresses).toEqual(['192.168.1.5', '10.0.0.7'])
expect(trustedHosts).toEqual(['192.168.1.5', '10.0.0.7', 'harness.internal:3080'])
})
describe('lanIPv4Addresses', () => {
it('returns only non-internal IPv4 addresses, in interface order', () => {
expect(lanIPv4Addresses()).toEqual(['192.168.1.5', '10.0.0.7'])
})
})
describe('resolveTrustedHosts', () => {
it('derives port-less LAN IP literals for an all-interfaces bind, ahead of the extras', () => {
expect(resolveTrustedHosts('0.0.0.0', ['harness.internal:3080']))
.toEqual(['192.168.1.5', '10.0.0.7', 'harness.internal:3080'])
})
it('derives nothing for a loopback or unresolved bind — extras alone stand', () => {
expect(resolveTrustedHosts('127.0.0.1', [])).toEqual([])
expect(resolveTrustedHosts(undefined, ['lab.internal'])).toEqual(['lab.internal'])
it('derives nothing for a loopback or unresolved bind — extras alone stand, no LAN URL to print', () => {
expect(resolveLanTrust('127.0.0.1', [])).toEqual({ lanAddresses: [], trustedHosts: [] })
expect(resolveLanTrust(undefined, ['lab.internal'])).toEqual({ lanAddresses: [], trustedHosts: ['lab.internal'] })
})
})