import { LocalTestTransport } from '../chatbot/src/widget/transports'; import { detectNavTarget, urlForNavTarget, getNavigationResponse } from '../shared/utils/keyword-navigation'; // Mock the navigation functions jest.mock('../shared/utils/keyword-navigation', () => ({ detectNavTarget: jest.fn(), urlForNavTarget: jest.fn(), getNavigationResponse: jest.fn(), })); describe('LocalTestTransport', () => { let transport: LocalTestTransport; let mockOnNavigate: jest.Mock; beforeEach(() => { mockOnNavigate = jest.fn(); transport = new LocalTestTransport(mockOnNavigate); // Reset mocks jest.clearAllMocks(); }); describe('Navigation Messages', () => { it('should handle navigation keywords and trigger navigation', async () => { // Mock the navigation functions (detectNavTarget as jest.Mock).mockReturnValue('tracking'); (urlForNavTarget as jest.Mock).mockReturnValue('https://www.sendcloud.com/parcel-tracking-solution/'); (getNavigationResponse as jest.Mock).mockReturnValue('Navigating to tracking solution...'); const result = await transport.send('show me tracking'); expect(detectNavTarget).toHaveBeenCalledWith('show me tracking'); expect(urlForNavTarget).toHaveBeenCalledWith('tracking'); expect(getNavigationResponse).toHaveBeenCalledWith('tracking'); expect(mockOnNavigate).toHaveBeenCalledWith('https://www.sendcloud.com/parcel-tracking-solution/'); expect(result.role).toBe('assistant'); expect(result.text).toBe('Navigating to tracking solution...'); expect(result.id).toContain('-nav'); }); it('should handle local development keywords', async () => { (detectNavTarget as jest.Mock).mockReturnValue('home'); (urlForNavTarget as jest.Mock).mockReturnValue('http://localhost:8083/chatbot/dev/index.html#'); (getNavigationResponse as jest.Mock).mockReturnValue('Taking you home...'); const result = await transport.send('take me home'); expect(mockOnNavigate).toHaveBeenCalledWith('http://localhost:8083/chatbot/dev/index.html#'); expect(result.text).toBe('Taking you home...'); }); }); describe('Non-Navigation Messages', () => { it('should echo non-navigation messages with helpful hints', async () => { (detectNavTarget as jest.Mock).mockReturnValue(null); const result = await transport.send('hello world'); expect(detectNavTarget).toHaveBeenCalledWith('hello world'); expect(mockOnNavigate).not.toHaveBeenCalled(); expect(result.role).toBe('assistant'); expect(result.text).toContain('I received your message: "hello world"'); expect(result.text).toContain('local test mode'); expect(result.text).toContain('tracking'); expect(result.text).toContain('returns'); expect(result.text).toContain('success'); expect(result.text).toContain('home'); expect(result.text).toContain('size'); expect(result.text).toContain('theme'); expect(result.id).toContain('-echo'); }); it('should handle empty messages', async () => { (detectNavTarget as jest.Mock).mockReturnValue(null); const result = await transport.send(''); expect(result.text).toContain('I received your message: ""'); }); }); describe('Without Navigation Callback', () => { it('should work without onNavigate callback', async () => { const transportWithoutCallback = new LocalTestTransport(); (detectNavTarget as jest.Mock).mockReturnValue('tracking'); (urlForNavTarget as jest.Mock).mockReturnValue('https://example.com'); (getNavigationResponse as jest.Mock).mockReturnValue('Navigating...'); const result = await transportWithoutCallback.send('show tracking'); expect(result.role).toBe('assistant'); expect(result.text).toBe('Navigating...'); }); }); describe('Response Timing', () => { it('should have a small delay to simulate network', async () => { (detectNavTarget as jest.Mock).mockReturnValue(null); const startTime = Date.now(); await transport.send('test message'); const endTime = Date.now(); // Should have at least 200ms delay expect(endTime - startTime).toBeGreaterThanOrEqual(200); }); }); describe('Integration Test', () => { it('should handle mixed navigation and non-navigation messages', async () => { // First message - navigation (detectNavTarget as jest.Mock).mockReturnValueOnce('tracking'); (urlForNavTarget as jest.Mock).mockReturnValueOnce('https://example.com'); (getNavigationResponse as jest.Mock).mockReturnValueOnce('Navigating...'); const navResult = await transport.send('show tracking'); expect(navResult.text).toBe('Navigating...'); expect(mockOnNavigate).toHaveBeenCalledTimes(1); // Second message - non-navigation (detectNavTarget as jest.Mock).mockReturnValueOnce(null); const echoResult = await transport.send('hello'); expect(echoResult.text).toContain('I received your message: "hello"'); expect(mockOnNavigate).toHaveBeenCalledTimes(1); // Should not be called again }); }); });