import { describe, it, type TestContext } from 'node:test'; import { type Config, type Session, validateConfig, resolveConfig } from './config.ts'; import { AssertionError } from './util/asserts.ts'; describe('config', () => { describe('validateConfig()', () => { it('should validate that object must be used', t => { const err = new AssertionError('API config must be an "object"'); t.assert.throws(() => validateConfig(0), err); t.assert.throws(() => validateConfig(15), err); t.assert.throws(() => validateConfig(true), err); t.assert.throws(() => validateConfig(false), err); t.assert.throws(() => validateConfig(null), err); t.assert.throws(() => validateConfig(undefined), err); }); it('should validate that clusterId is a non-empty string', t => { const err = new AssertionError('Property "clusterId" must be a non-empty string'); t.assert.throws(() => validateConfig({}), err); t.assert.throws(() => validateConfig({ clusterId: undefined }), err); t.assert.throws(() => validateConfig({ clusterId: [1, 2, 3] }), err); t.assert.throws(() => validateConfig({ clusterId: '' }), err); }); it('should validate that market is a non-empty string', t => { const err = new AssertionError('Property "market" must be a non-empty string'); t.assert.throws(() => validateConfig({ clusterId: 'w000000' }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: 0 }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: 1337 }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: true }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: false }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: null }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: undefined }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: '' }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: {} }), err); t.assert.throws(() => validateConfig({ clusterId: 'w000000', market: ['a', 'b'] }), err); }); it('should validate that session is an object or function', t => { const err = new AssertionError('Property "session" must be a string, object, promise, or method.'); const base = { clusterId: 'w000000', market: 'a' }; t.assert.throws(() => validateConfig({ ...base }), err); t.assert.throws(() => validateConfig({ ...base, session: 0 }), err); t.assert.throws(() => validateConfig({ ...base, session: true }), err); t.assert.throws(() => validateConfig({ ...base, session: null }), err); t.assert.throws(() => validateConfig({ ...base, session: undefined }), err); }); it('should validate that locale is a non-empty string', t => { const err = new AssertionError('Property "locale" must be a string and valid locale, e.g. "en-US".'); const base = { clusterId: 'w000000', market: 'a', session: () => {} }; t.assert.throws(() => validateConfig({ ...base }), err); t.assert.throws(() => validateConfig({ ...base, locale: 0 }), err); t.assert.throws(() => validateConfig({ ...base, locale: 4242 }), err); t.assert.throws(() => validateConfig({ ...base, locale: true }), err); t.assert.throws(() => validateConfig({ ...base, locale: false }), err); t.assert.throws(() => validateConfig({ ...base, locale: null }), err); t.assert.throws(() => validateConfig({ ...base, locale: undefined }), err); t.assert.throws(() => validateConfig({ ...base, locale: '' }), err); t.assert.throws(() => validateConfig({ ...base, locale: {} }), err); t.assert.throws(() => validateConfig({ ...base, locale: ['jane', 'john'] }), err); }); it('should validate that touchpoint is a valid enum value', t => { const err = new AssertionError('Property "touchpoint" must be one of: desktop, mobile'); const base = { clusterId: 'w000000', market: 'a', locale: 'a', session: () => ({ customerKey: '', sessionKey: '' }) }; t.assert.throws(() => validateConfig({ ...base }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: 0 }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: 9001 }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: true }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: false }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: null }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: undefined }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: '' }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: ['a', 'b'] }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: 'foo' }), err); t.assert.throws(() => validateConfig({ ...base, touchpoint: 'desktopp' }), err); }); it('should not throw errors on valid shape `session` properties', t => { const base = { clusterId: 'w000000', market: 'a', locale: 'b', touchpoint: 'desktop' }; t.assert.doesNotThrow(() => validateConfig({ ...base, session: [] })); // False positive, but satisfies "object" shape t.assert.doesNotThrow(() => validateConfig({ ...base, session: {} })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: 'foo' })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: 'ANONYMOUS' })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: Promise.resolve('bar') })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: () => 'SYNTHETIC' })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: () => Promise.resolve('baz') })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: { customerKey: 'a', sessionKey: 'a' } satisfies Session })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: Promise.resolve({ customerKey: 'b', sessionKey: 'b' }) satisfies Session })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: () => ({ customerKey: 'c', sessionKey: 'c' }) satisfies Session })); t.assert.doesNotThrow(() => validateConfig({ ...base, session: async () => await ({ customerKey: 'd', sessionKey: 'd' }) satisfies Session })); }); it('should not throw errors on valid configurations', () => { const session = () => ({ customerKey: '', sessionKey: '' }); validateConfig({ clusterId: 'wabcdef0', market: 'se', locale: 'sv-SE', touchpoint: 'mobile', session }); validateConfig({ clusterId: 'wABCDEF012', market: 'gb', locale: 'en-GB', touchpoint: 'desktop', session }); validateConfig({ clusterId: 'https://example.com/', market: 'uk', locale: 'en-GB', touchpoint: 'desktop', session }); validateConfig({ clusterId: 'http://localhost:12345', market: 'uk', locale: 'en-GB', touchpoint: 'desktop', session }); }); it('should have correct properties for the `Config` interface', () => { ({ clusterId: 'w123abc', market: 'se', locale: 'en-US', touchpoint: 'desktop', session: () => ({ customerKey: '', sessionKey: '' }) } satisfies Config); }); }); describe('resolveConfig()', () => { it('should return a correct resolved config', (t: TestContext) => { const constantSession = () => ({ customerKey: '', sessionKey: '' }); const config = resolveConfig({ clusterId: 'w123ABC', market: 'se', locale: 'sv-SE', touchpoint: 'desktop', session: constantSession }); const { market, locale, touchpoint, session, clusterUrl, ...rest } = config; t.assert.strictEqual(market, 'se'); t.assert.strictEqual(locale, 'sv-SE'); t.assert.strictEqual(touchpoint, 'desktop'); t.assert.strictEqual(session, constantSession); t.assert.strictEqual(clusterUrl, 'https://w123abc.api.esales.apptus.cloud/'); t.assert.deepStrictEqual(rest, {}); }); it('should allow case insensitive `clusterId`', (t: TestContext) => { const constantSession = () => ({ customerKey: '', sessionKey: '' }); const config = resolveConfig({ clusterId: 'wABcdEF12', market: 'se', locale: 'sv-SE', touchpoint: 'desktop', session: constantSession }); const { market, locale, touchpoint, session, clusterUrl, ...rest } = config; t.assert.strictEqual(market, 'se'); t.assert.strictEqual(locale, 'sv-SE'); t.assert.strictEqual(touchpoint, 'desktop'); t.assert.strictEqual(session, constantSession); t.assert.strictEqual(clusterUrl, 'https://wabcdef12.api.esales.apptus.cloud/'); t.assert.deepStrictEqual(rest, {}); }); it('should return a correct processed config when using URL for `clusterId`', (t: TestContext) => { const constantSession = () => ({ customerKey: '', sessionKey: '' }); const config = resolveConfig({ clusterId: 'https://localhost:3000/', market: 'de', locale: 'no-NO', touchpoint: 'mobile', session: constantSession }); const { market, locale, touchpoint, session, clusterUrl, ...rest } = config; t.assert.strictEqual(market, 'de'); t.assert.strictEqual(locale, 'no-NO'); t.assert.strictEqual(touchpoint, 'mobile'); t.assert.strictEqual(session, constantSession); t.assert.strictEqual(clusterUrl, 'https://localhost:3000/'); t.assert.deepStrictEqual(rest, {}); }); it('should automatically normalize trailing slash for `clusterUrl`', (t: TestContext) => { const constantSession = () => ({ customerKey: '', sessionKey: '' }); const config = resolveConfig({ clusterId: 'https://localhost:12345', market: 'nada', locale: 'zip', touchpoint: 'mobile', session: constantSession }); t.assert.strictEqual(config.clusterUrl, 'https://localhost:12345/'); }); }); });