import { describe, expect, test } from 'vitest' import { initialConfig, LATEST_WEBCHAT_CONFIG_VERSION, LatestVersionedWebchatConfig } from '../schemas' import { migrateConfig } from './config' describe('given v0 config', () => { const configV0 = { composerPlaceholder: 'Type here', botName: 'Beep Boop', botDescription: 'Slick bot', color: '#3B82F6', variant: 'soft', themeMode: 'light', fontFamily: 'inter', radius: 4, showPoweredBy: true, allowFileUpload: true, } test('then return be the latest version', () => { const migratedConfig = migrateConfig(configV0 as any) expect(migratedConfig.version).toBe(LATEST_WEBCHAT_CONFIG_VERSION) }) test('then set feedbackEnabled to false', () => { const migratedConfig = migrateConfig(configV0 as any) expect(migratedConfig.feedbackEnabled).toBe(false) }) test('then set soundEnabled to false', () => { const migratedConfig = migrateConfig(configV0 as any) expect(migratedConfig.soundEnabled).toBe(false) }) test('then default rateLimit enabled to false when not in config', () => { const migratedConfig = migrateConfig(configV0 as any) expect(migratedConfig.rateLimit?.enabled).toBe(false) }) test('when showPoweredBy is false then set footer to empty string', () => { const showPoweredByFalseConfig = { ...configV0, showPoweredBy: false } const migratedConfig = migrateConfig(showPoweredByFalseConfig as any) expect(migratedConfig.footer).toBe('') }) test('when showPoweredBy is true then set footer to initial config footer', () => { const showPoweredByTrueConfig = { ...configV0, showPoweredBy: true } const migratedConfig = migrateConfig(showPoweredByTrueConfig as any) expect(migratedConfig.footer).toBe(initialConfig.footer) }) test('when soft variant then return glass header variant', () => { const softVariantConfig = { ...configV0, variant: 'soft' } const migratedConfig = migrateConfig(softVariantConfig as any) expect(migratedConfig.headerVariant).toBe('glass') }) test('when solid variant then return solid header variant', () => { const solidVariantConfig = { ...configV0, variant: 'solid' } const migratedConfig = migrateConfig(solidVariantConfig as any) expect(migratedConfig.headerVariant).toBe('solid') }) }) describe('given latest config', () => { const latestConfig: LatestVersionedWebchatConfig = { botName: 'Beep Boop', botDescription: 'Slick bot', composerPlaceholder: 'Type here', color: '#3B82F6', radius: 4, themeMode: 'light', variant: 'soft', fontFamily: 'inter', footer: '⚡By Botpress', allowFileUpload: true, headerVariant: 'glass', feedbackEnabled: false, soundEnabled: true, conversationHistory: false, version: LATEST_WEBCHAT_CONFIG_VERSION, proactiveMessageEnabled: true, proactiveBubbleMessage: 'Hello!', proactiveBubbleTriggerType: 'afterDelay', proactiveBubbleDelayTime: 5, rateLimit: { enabled: true, requests: 25, windowMinutes: 1, }, ipBlocklist: [], homePageEnabled: false, mainCardEnabled: false, conversationStartersEnabled: false, conversationStarters: [], conversationStartersDisplayStyle: 'cards', } test('latest config should return itself', () => { const migratedConfig = migrateConfig(latestConfig) expect(migratedConfig).toEqual(latestConfig) }) }) describe('given unknown config', () => { test('then return fallback config', () => { const unknownConfig = { version: '69', shaq: 'Unknown Bot' } const migratedConfig = migrateConfig(unknownConfig as any) const fallbackConfig = { ...initialConfig, headerVariant: 'solid', version: LATEST_WEBCHAT_CONFIG_VERSION, } expect(migratedConfig).toStrictEqual(fallbackConfig) }) })