import test from 'node:test'; import assert from 'node:assert/strict'; import { EXAMPLE_INTERNAL_SECRETS, Info, MIN_INTERNAL_SECRET_LENGTH, Store, internalSecretMatches, internalSecretUsable, } 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'); }); const LONG_SECRET = 'shared-secret-value'; 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, LONG_SECRET), false); assert.equal(internalSecretMatches('shared-secret-valuE', LONG_SECRET), false); assert.equal(internalSecretMatches(LONG_SECRET, LONG_SECRET), true); }); test('internal secret check refuses a short or placeholder secret', () => { const short = 'x'.repeat(MIN_INTERNAL_SECRET_LENGTH - 1); assert.equal(internalSecretMatches(short, short), false); for (const placeholder of EXAMPLE_INTERNAL_SECRETS) { assert.equal(internalSecretMatches(placeholder, placeholder), false, placeholder); } }); test('internal secret usability needs the minimum length and no placeholder', () => { assert.equal(internalSecretUsable('x'.repeat(MIN_INTERNAL_SECRET_LENGTH - 1)), false); assert.equal(internalSecretUsable('x'.repeat(MIN_INTERNAL_SECRET_LENGTH)), true); assert.equal(internalSecretUsable('change-me-to-a-long-random-value'), false); assert.equal(internalSecretUsable(undefined), false); }); 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'); });