|
| 1 | +import type { NodeOptions } from '../src/NodeOptions'; |
| 2 | +import validateOptions, { filterToBaseOptions, ValidatedOptions } from '../src/options'; |
| 3 | +import { createMockLogger } from './testHelpers'; |
| 4 | + |
| 5 | +// A value no option validator should accept regardless of the field's expected type |
| 6 | +const BOGUS_VALUE = Symbol('invalid-option-value'); |
| 7 | + |
| 8 | +// Exhaustive over keyof ValidatedOptions: adding a new node-specific option fails to compile |
| 9 | +// here until a bogus case is added, which forces the wrong-type-warning test below to cover |
| 10 | +// it. |
| 11 | +const wrongTypedOptions: Record<keyof ValidatedOptions, unknown> = { |
| 12 | + tlsParams: BOGUS_VALUE, |
| 13 | + enableEventCompression: BOGUS_VALUE, |
| 14 | + initialConnectionMode: BOGUS_VALUE, |
| 15 | + plugins: BOGUS_VALUE, |
| 16 | + localStoragePath: BOGUS_VALUE, |
| 17 | + hash: BOGUS_VALUE, |
| 18 | +}; |
| 19 | + |
| 20 | +const nodeOptionKeys = Object.keys(wrongTypedOptions) as (keyof ValidatedOptions)[]; |
| 21 | + |
| 22 | +let logger: ReturnType<typeof createMockLogger>; |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + logger = createMockLogger(); |
| 26 | +}); |
| 27 | + |
| 28 | +it('applies defaults when no node-specific options are provided', () => { |
| 29 | + const out = validateOptions({}, logger); |
| 30 | + |
| 31 | + expect(out.initialConnectionMode).toBe('streaming'); |
| 32 | + expect(out.plugins).toEqual([]); |
| 33 | + expect(out.tlsParams).toBeUndefined(); |
| 34 | + expect(out.enableEventCompression).toBeUndefined(); |
| 35 | + expect(out.localStoragePath).toBeUndefined(); |
| 36 | + expect(out.hash).toBeUndefined(); |
| 37 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 38 | +}); |
| 39 | + |
| 40 | +it('passes through valid node-specific options', () => { |
| 41 | + const out = validateOptions( |
| 42 | + { |
| 43 | + initialConnectionMode: 'polling', |
| 44 | + enableEventCompression: true, |
| 45 | + localStoragePath: '/tmp/ld-cache', |
| 46 | + hash: 'abc123', |
| 47 | + }, |
| 48 | + logger, |
| 49 | + ); |
| 50 | + |
| 51 | + expect(out.initialConnectionMode).toBe('polling'); |
| 52 | + expect(out.enableEventCompression).toBe(true); |
| 53 | + expect(out.localStoragePath).toBe('/tmp/ld-cache'); |
| 54 | + expect(out.hash).toBe('abc123'); |
| 55 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 56 | +}); |
| 57 | + |
| 58 | +it('warns and falls back to the default for an invalid initialConnectionMode', () => { |
| 59 | + const out = validateOptions({ initialConnectionMode: 'STREAMING' as any }, logger); |
| 60 | + |
| 61 | + expect(out.initialConnectionMode).toBe('streaming'); |
| 62 | + expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('initialConnectionMode')); |
| 63 | +}); |
| 64 | + |
| 65 | +it('warns when TLS certificate verification is disabled', () => { |
| 66 | + validateOptions({ tlsParams: { rejectUnauthorized: false } }, logger); |
| 67 | + |
| 68 | + expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('rejectUnauthorized')); |
| 69 | +}); |
| 70 | + |
| 71 | +it('strips every node-specific option from the base options but keeps base options', () => { |
| 72 | + const opts: NodeOptions = { |
| 73 | + initialConnectionMode: 'polling', |
| 74 | + plugins: [], |
| 75 | + tlsParams: {}, |
| 76 | + enableEventCompression: true, |
| 77 | + localStoragePath: '/tmp/ld-cache', |
| 78 | + hash: 'abc123', |
| 79 | + sendEvents: false, |
| 80 | + }; |
| 81 | + |
| 82 | + const base = filterToBaseOptions(opts) as Record<string, unknown>; |
| 83 | + |
| 84 | + nodeOptionKeys.forEach((key) => { |
| 85 | + expect(base).not.toHaveProperty(key); |
| 86 | + }); |
| 87 | + expect(base).toHaveProperty('sendEvents', false); |
| 88 | +}); |
| 89 | + |
| 90 | +it('warns for every validated option when given a value of the wrong type', () => { |
| 91 | + nodeOptionKeys.forEach((key) => { |
| 92 | + const fieldLogger = createMockLogger(); |
| 93 | + validateOptions({ [key]: wrongTypedOptions[key] } as unknown as NodeOptions, fieldLogger); |
| 94 | + |
| 95 | + expect(fieldLogger.warn).toHaveBeenCalledWith(expect.stringContaining(key)); |
| 96 | + }); |
| 97 | +}); |
0 commit comments