/** * Copyright 2026 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import * as assert from 'assert'; import { describe, it } from 'node:test'; import { advertisedReflectionHost, isLoopbackHost, resolveReflectionConfig, resolveReflectionPort, secretsEqual, } from '../src/reflection-config.js'; describe('resolveReflectionConfig', () => { it('is off when nothing is set', () => { assert.deepStrictEqual(resolveReflectionConfig({}), { kind: 'off' }); }); it('is disabled by GENKIT_REFLECTION_ENABLED=false, even in dev', () => { assert.deepStrictEqual( resolveReflectionConfig({ GENKIT_REFLECTION_ENABLED: 'false', GENKIT_ENV: 'dev', GENKIT_REFLECTION_PORT: '3100', GENKIT_REFLECTION_V2_SERVER: 'ws://127.0.0.1:3200', }), { kind: 'disabled' } ); }); it('is on outside dev with GENKIT_REFLECTION_ENABLED=true', () => { assert.deepStrictEqual( resolveReflectionConfig({ GENKIT_REFLECTION_ENABLED: 'true' }), { kind: 'v1', host: '127.0.0.1', port: { kind: 'probe' }, secret: undefined, } ); }); it('treats an empty GENKIT_REFLECTION_ENABLED as unset', () => { assert.strictEqual( resolveReflectionConfig({ GENKIT_REFLECTION_ENABLED: '' }).kind, 'off' ); assert.strictEqual( resolveReflectionConfig({ GENKIT_REFLECTION_ENABLED: '', GENKIT_ENV: 'dev', }).kind, 'v1' ); }); it('rejects other spellings of GENKIT_REFLECTION_ENABLED', () => { for (const value of ['1', '0', 'yes', 'on', 'TRUE', 'False']) { assert.throws( () => resolveReflectionConfig({ GENKIT_REFLECTION_ENABLED: value }), /GENKIT_REFLECTION_ENABLED/, `for ${value}` ); } }); it('does not turn on from host, port or v2 settings alone', () => { for (const env of [ { GENKIT_REFLECTION_HOST: '0.0.0.0' }, { GENKIT_REFLECTION_PORT: '4200' }, { GENKIT_REFLECTION_V2_SERVER: 'ws://127.0.0.1:3200' }, ]) { assert.deepStrictEqual( resolveReflectionConfig(env), { kind: 'off' }, JSON.stringify(env) ); } }); it('does not parse the port while reflection is off', () => { assert.deepStrictEqual( resolveReflectionConfig({ GENKIT_REFLECTION_PORT: 'abc' }), { kind: 'off' } ); }); it('dials out when a v2 server is set', () => { assert.deepStrictEqual( resolveReflectionConfig({ GENKIT_ENV: 'dev', GENKIT_REFLECTION_V2_SERVER: 'ws://127.0.0.1:3200', GENKIT_REFLECTION_SECRET_TOKEN: 's3cret', }), { kind: 'v2', url: 'ws://127.0.0.1:3200', secret: 's3cret' } ); }); it('prefers v2 over a configured v1 port', () => { const config = resolveReflectionConfig({ GENKIT_REFLECTION_ENABLED: 'true', GENKIT_REFLECTION_V2_SERVER: 'ws://127.0.0.1:3200', GENKIT_REFLECTION_PORT: '3100', }); assert.strictEqual(config.kind, 'v2'); }); it('uses the configured host', () => { assert.deepStrictEqual( resolveReflectionConfig({ GENKIT_REFLECTION_ENABLED: 'true', GENKIT_REFLECTION_HOST: '0.0.0.0', }), { kind: 'v1', host: '0.0.0.0', port: { kind: 'probe' }, secret: undefined, } ); }); it('pins the configured port', () => { assert.deepStrictEqual( resolveReflectionConfig({ GENKIT_ENV: 'dev', GENKIT_REFLECTION_PORT: '4200', }), { kind: 'v1', host: '127.0.0.1', port: { kind: 'pinned', port: 4200 }, secret: undefined, } ); }); it('probes from 3100 in dev', () => { assert.deepStrictEqual(resolveReflectionConfig({ GENKIT_ENV: 'dev' }), { kind: 'v1', host: '127.0.0.1', port: { kind: 'probe' }, secret: undefined, }); }); it('lets the environment beat the programmatic port', () => { const config = resolveReflectionConfig( { GENKIT_ENV: 'dev', GENKIT_REFLECTION_PORT: '4200' }, { port: 9999 } ); assert.deepStrictEqual(config, { kind: 'v1', host: '127.0.0.1', port: { kind: 'pinned', port: 4200 }, secret: undefined, }); }); it('pins the programmatic port when the env has none', () => { const config = resolveReflectionConfig( { GENKIT_ENV: 'dev' }, { port: 9999 } ); assert.deepStrictEqual(config, { kind: 'v1', host: '127.0.0.1', port: { kind: 'pinned', port: 9999 }, secret: undefined, }); }); it('does not turn the server on from a programmatic port alone', () => { assert.deepStrictEqual(resolveReflectionConfig({}, { port: 9999 }), { kind: 'off', }); }); it('rejects an invalid programmatic port even when the server is off', () => { for (const port of [-1, 1.5, 70000, NaN]) { assert.throws( () => resolveReflectionConfig({}, { port }), /reflectionPort/, `for ${port}` ); } }); it('rejects an invalid port rather than falling back', () => { for (const port of [ 'abc', '-1', '70000', '3100.5', '+7', '0x10', '1e3', ' 7', '1_000', ]) { assert.throws( () => resolveReflectionConfig({ GENKIT_ENV: 'dev', GENKIT_REFLECTION_PORT: port, }), /GENKIT_REFLECTION_PORT/, `for ${port}` ); } }); it('accepts port 0', () => { const config = resolveReflectionConfig({ GENKIT_ENV: 'dev', GENKIT_REFLECTION_PORT: '0', }); assert.deepStrictEqual(config, { kind: 'v1', host: '127.0.0.1', port: { kind: 'pinned', port: 0 }, secret: undefined, }); }); }); describe('resolveReflectionPort', () => { it('probes when nobody chose a port', () => { assert.deepStrictEqual(resolveReflectionPort(undefined, undefined), { kind: 'probe', }); }); it('pins a programmatic port exactly', () => { assert.deepStrictEqual(resolveReflectionPort(undefined, 4300), { kind: 'pinned', port: 4300, }); }); it('maps 0 to an OS-assigned port, same as GENKIT_REFLECTION_PORT=0', () => { assert.deepStrictEqual( resolveReflectionPort(undefined, 0), resolveReflectionPort(0, undefined) ); assert.deepStrictEqual(resolveReflectionPort(undefined, 0), { kind: 'pinned', port: 0, }); }); it('lets the environment port win', () => { assert.deepStrictEqual(resolveReflectionPort(4200, 4300), { kind: 'pinned', port: 4200, }); }); it('rejects an invalid programmatic port even when the env port wins', () => { assert.throws(() => resolveReflectionPort(4200, 70000), /reflectionPort/); }); }); describe('advertisedReflectionHost', () => { it('advertises loopback for wildcard binds', () => { for (const host of ['0.0.0.0', '::', '[::]']) { assert.strictEqual(advertisedReflectionHost(host), '127.0.0.1', host); } }); it('keeps a specific host, bracketing IPv6', () => { assert.strictEqual(advertisedReflectionHost('127.0.0.1'), '127.0.0.1'); assert.strictEqual(advertisedReflectionHost('192.168.1.5'), '192.168.1.5'); assert.strictEqual(advertisedReflectionHost('::1'), '[::1]'); assert.strictEqual(advertisedReflectionHost('[::1]'), '[::1]'); }); }); describe('isLoopbackHost', () => { it('recognizes loopback addresses', () => { for (const host of [ '127.0.0.1', '127.1.2.3', 'localhost', '::1', '[::1]', '0:0:0:0:0:0:0:1', // Abbreviated and hex forms that listen() resolves to 127.0.0.1. '127.1', '0x7f.1', ]) { assert.strictEqual(isLoopbackHost(host), true, host); } }); it('rejects routable addresses', () => { for (const host of [ '0.0.0.0', '::', '192.168.1.5', '10.0.0.1', 'example.com', // Hostnames that merely start with "127." may resolve anywhere. '127.internal.example', '127.0.0.1.nip.io', '127.a.b.c', // Not a valid IP literal; listen() cannot bind it either. '127.300.400.500', ]) { assert.strictEqual(isLoopbackHost(host), false, host); } }); }); describe('secretsEqual', () => { it('compares by value, including different lengths', () => { assert.strictEqual(secretsEqual('abc', 'abc'), true); assert.strictEqual(secretsEqual('abc', 'abd'), false); assert.strictEqual(secretsEqual('abc', 'much-longer-secret'), false); assert.strictEqual(secretsEqual('', ''), true); }); });