import test from 'node:test'; import assert from 'node:assert/strict'; import { Info, Store, internalSecretMatches } from '../src/coreconfig.js'; test('store seeds from the fallback Info', () => { const store = new Store({ authGuardUrl: 'http://auth.example.com', carrierNodeUrl: 'http://carrier.example.com' }); assert.equal(store.authGuardUrl, 'http://auth.example.com'); assert.equal(store.carrierNodeUrl, 'http://carrier.example.com'); assert.deepEqual(store.carrierNodeUrls, ['http://carrier.example.com']); }); test('store defaults to empty with no fallback', () => { const store = new Store(); assert.equal(store.authGuardUrl, ''); assert.deepEqual(store.carrierNodeUrls, []); }); test('set ignores a blank authGuardUrl rather than clobbering the existing one', () => { const store = new Store({ authGuardUrl: 'http://auth.example.com' }); store.set({ authGuardUrl: '', carrierNodeUrl: 'http://carrier.example.com' }); assert.equal(store.authGuardUrl, 'http://auth.example.com'); assert.equal(store.carrierNodeUrl, 'http://carrier.example.com'); }); test('set only replaces carrierNodeUrls when the push actually carries one', () => { const store = new Store({ carrierNodeUrl: 'http://carrier.example.com' }); store.set({ authGuardUrl: 'http://auth.example.com' }); assert.equal(store.carrierNodeUrl, 'http://carrier.example.com'); assert.equal(store.authGuardUrl, 'http://auth.example.com'); }); test('onUpdate is called with the resulting Info after set', () => { const store = new Store(); const seen = []; store.onUpdate((info) => seen.push(info)); store.set({ authGuardUrl: 'http://auth.example.com' }); assert.equal(seen.length, 1); assert.equal(seen[0].authGuardUrl, 'http://auth.example.com'); }); test('Info.fromJSON/toJSON round-trip the wire shape', () => { const payload = { authGuardUrl: 'http://auth.example.com', carrierNodeUrl: 'http://carrier.example.com', carrierNodeUrls: ['http://carrier.example.com', 'http://carrier2.example.com'], }; const info = Info.fromJSON(payload); assert.deepEqual(info.carrierNodeUrls, ['http://carrier.example.com', 'http://carrier2.example.com']); assert.equal(info.toJSON().carrierNodeUrl, 'http://carrier.example.com'); }); test('internal secret check fails closed when the secret is unset or empty', () => { assert.equal(internalSecretMatches(undefined, undefined), false); assert.equal(internalSecretMatches(undefined, ''), false); assert.equal(internalSecretMatches('', ''), false); assert.equal(internalSecretMatches(undefined, 'shared-secret'), false); assert.equal(internalSecretMatches('shared-secreT', 'shared-secret'), false); assert.equal(internalSecretMatches('shared-secret', 'shared-secret'), true); }); test('set rejects a URL that is not an http or https URL and changes nothing', () => { const store = new Store({ authGuardUrl: 'http://auth.example.com' }); assert.throws(() => store.set({ authGuardUrl: 'javascript:alert(1)' }), TypeError); assert.throws(() => store.set({ authGuardUrl: 'auth.example.com' }), TypeError); assert.throws(() => store.set({ carrierNodeUrls: ['ftp://carrier.example.com'] }), TypeError); assert.equal(store.authGuardUrl, 'http://auth.example.com'); });