import { detectNavTarget, getNavigationResponse, type NavTarget } from '../shared/utils/keyword-navigation'; // Mock the urlForNavTarget function to avoid window.location dependency jest.mock('../shared/utils/keyword-navigation', () => { const original = jest.requireActual('../shared/utils/keyword-navigation'); return { ...original, urlForNavTarget: jest.fn((target: string) => { // Mock URL mapping for testing const mockUrls: Record = { 'locking-washers': 'https://www.accu.co.uk/shop/precision-components/washers-spacers/locking-washers/external-fine-tooth-locking-washers?page=1', 'rivets': 'https://www.accu.co.uk/shop/precision-components/rivets/plastic-rivets', 'home': 'http://localhost:8083/chatbot/dev/index.html#', 'size': 'http://localhost:8083/chatbot/dev/index.html#', 'theme': 'http://localhost:8083/chatbot/dev/index.html#', 'tracking': 'https://www.sendcloud.com/parcel-tracking-solution/', 'returns': 'https://www.sendcloud.com/returns-management-software/', 'success': 'https://www.sendcloud.com/customer-success/', }; return mockUrls[target] || 'https://example.com'; }) }; }); // Import the mocked function import { urlForNavTarget } from '../shared/utils/keyword-navigation'; describe('Chatbot Navigation System', () => { describe('detectNavTarget', () => { it('should detect existing navigation targets', () => { expect(detectNavTarget('I need lock washers')).toBe('locking-washers'); expect(detectNavTarget('Show me rivets')).toBe('rivets'); expect(detectNavTarget('plastic rivet selection')).toBe('rivets'); }); it('should detect new local development targets', () => { expect(detectNavTarget('take me home')).toBe('home'); expect(detectNavTarget('adjust the size')).toBe('size'); expect(detectNavTarget('change theme')).toBe('theme'); }); it('should detect new sendcloud targets', () => { expect(detectNavTarget('parcel tracking')).toBe('tracking'); expect(detectNavTarget('shipping solution')).toBe('tracking'); expect(detectNavTarget('returns management')).toBe('returns'); expect(detectNavTarget('customer success stories')).toBe('success'); expect(detectNavTarget('testimonials')).toBe('success'); }); it('should return null for non-matching text', () => { expect(detectNavTarget('hello world')).toBeNull(); expect(detectNavTarget('random text')).toBeNull(); expect(detectNavTarget('')).toBeNull(); }); it('should be case insensitive', () => { expect(detectNavTarget('LOCK WASHERS')).toBe('locking-washers'); expect(detectNavTarget('Tracking Solution')).toBe('tracking'); expect(detectNavTarget('Customer Success')).toBe('success'); }); }); describe('urlForNavTarget (mocked)', () => { it('should return appropriate URLs for all targets', () => { const targets = [ 'locking-washers', 'rivets', 'home', 'size', 'theme', 'tracking', 'returns', 'success' ] as NavTarget[]; targets.forEach(target => { const url = urlForNavTarget(target); expect(url).toBeTruthy(); expect(typeof url).toBe('string'); expect(url.length).toBeGreaterThan(0); }); }); it('should return specific URLs for each target', () => { expect(urlForNavTarget('tracking')).toBe('https://www.sendcloud.com/parcel-tracking-solution/'); expect(urlForNavTarget('returns')).toBe('https://www.sendcloud.com/returns-management-software/'); expect(urlForNavTarget('success')).toBe('https://www.sendcloud.com/customer-success/'); expect(urlForNavTarget('locking-washers')).toBe('https://www.accu.co.uk/shop/precision-components/washers-spacers/locking-washers/external-fine-tooth-locking-washers?page=1'); expect(urlForNavTarget('rivets')).toBe('https://www.accu.co.uk/shop/precision-components/rivets/plastic-rivets'); }); }); describe('getNavigationResponse', () => { it('should return appropriate responses for all targets', () => { const responses = [ 'locking-washers', 'rivets', 'home', 'size', 'theme', 'tracking', 'returns', 'success' ] as NavTarget[]; responses.forEach(target => { const response = getNavigationResponse(target); expect(response).toBeTruthy(); expect(typeof response).toBe('string'); expect(response.length).toBeGreaterThan(0); }); }); it('should return specific responses for each target', () => { expect(getNavigationResponse('tracking')).toContain('parcel tracking solution'); expect(getNavigationResponse('returns')).toContain('returns management software'); expect(getNavigationResponse('success')).toContain('customer success stories'); expect(getNavigationResponse('locking-washers')).toContain('locking washers'); expect(getNavigationResponse('rivets')).toContain('plastic rivets'); }); }); describe('Integration Test', () => { it('should handle complete navigation flow', () => { const userMessage = 'I want to go home'; const target = detectNavTarget(userMessage); expect(target).toBe('home'); const url = urlForNavTarget(target!); expect(url).toBe('http://localhost:8083/chatbot/dev/index.html#'); const response = getNavigationResponse(target!); expect(response).toContain('home page'); }); it('should handle sendcloud navigation flow', () => { const userMessage = 'Show me tracking solutions'; const target = detectNavTarget(userMessage); expect(target).toBe('tracking'); const url = urlForNavTarget(target!); expect(url).toBe('https://www.sendcloud.com/parcel-tracking-solution/'); const response = getNavigationResponse(target!); expect(response).toContain('parcel tracking solution'); }); }); });