|
| 1 | +<!doctype html> |
| 2 | +<html lang="en-US"> |
| 3 | + <head> |
| 4 | + <link href="/assets/index.css" rel="stylesheet" type="text/css" /> |
| 5 | + </head> |
| 6 | + <body> |
| 7 | + <main id="webchat"></main> |
| 8 | + <script type="importmap"> |
| 9 | + { |
| 10 | + "imports": { |
| 11 | + "@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs", |
| 12 | + "botframework-webchat": "/__dist__/packages/bundle/static/botframework-webchat.js", |
| 13 | + "botframework-webchat/component": "/__dist__/packages/bundle/static/botframework-webchat/component.js", |
| 14 | + "botframework-webchat/hook": "/__dist__/packages/bundle/static/botframework-webchat/hook.js", |
| 15 | + "react": "https://esm.sh/react@18", |
| 16 | + "react-dom": "https://esm.sh/react-dom@18", |
| 17 | + "react-dom/": "https://esm.sh/react-dom@18/" |
| 18 | + } |
| 19 | + } |
| 20 | + </script> |
| 21 | + <script type="module"> |
| 22 | + import '/test-harness.mjs'; |
| 23 | + import '/test-page-object.mjs'; |
| 24 | + |
| 25 | + import { waitFor } from '@testduet/wait-for'; |
| 26 | + import { createStoreWithOptions, testIds } from 'botframework-webchat'; |
| 27 | + import { useCapabilities } from 'botframework-webchat/hook'; |
| 28 | + import createRenderHook from '/assets/esm/createRenderHook.js'; |
| 29 | + |
| 30 | + const { createDirectLineEmulator } = window.testHelpers; |
| 31 | + |
| 32 | + window.WebChat = { createStoreWithOptions, testIds }; |
| 33 | + |
| 34 | + run(async function () { |
| 35 | + // TEST 1: Initial fetch on mount - capabilities should be fetched when directLine is available |
| 36 | + const { directLine, store } = createDirectLineEmulator(); |
| 37 | + |
| 38 | + // Set initial capability BEFORE mount (simulating adapter already having capability) |
| 39 | + directLine.setCapability('getVoiceConfiguration', { voice: 'en-US', speed: 1.0 }, { emitEvent: false }); |
| 40 | + |
| 41 | + const renderHook = createRenderHook( |
| 42 | + document.getElementById('webchat'), |
| 43 | + { directLine, store }, |
| 44 | + { renderWebChat: true } |
| 45 | + ); |
| 46 | + |
| 47 | + await renderHook(); |
| 48 | + await pageConditions.uiConnected(); |
| 49 | + |
| 50 | + // Get initial voiceConfiguration using selector |
| 51 | + const initialVoiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 52 | + |
| 53 | + expect(initialVoiceConfig).toEqual({ voice: 'en-US', speed: 1.0 }); |
| 54 | + |
| 55 | + // TEST 2: Regular activity should NOT trigger capability re-calculation |
| 56 | + // Store reference to current voiceConfiguration |
| 57 | + const preActivityVoiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 58 | + |
| 59 | + // Send a regular message (not capabilitiesChanged event) |
| 60 | + await directLine.emulateIncomingActivity({ |
| 61 | + type: 'message', |
| 62 | + text: 'Hello! This is a regular message.', |
| 63 | + from: { id: 'bot', role: 'bot' } |
| 64 | + }); |
| 65 | + |
| 66 | + // Wait for activity to be processed |
| 67 | + await new Promise(resolve => setTimeout(resolve, 200)); |
| 68 | + |
| 69 | + // Get voiceConfiguration after regular activity |
| 70 | + const postActivityVoiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 71 | + |
| 72 | + // Reference should be the same (no re-calculation for regular activities) |
| 73 | + expect(postActivityVoiceConfig).toBe(preActivityVoiceConfig); |
| 74 | + |
| 75 | + // TEST 3: capabilitiesChanged event SHOULD trigger re-calculation |
| 76 | + const preChangeVoiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 77 | + |
| 78 | + // Update capability and emit event |
| 79 | + directLine.setCapability('getVoiceConfiguration', { voice: 'en-GB', speed: 1.5 }, { emitEvent: true }); |
| 80 | + |
| 81 | + // Wait for event to be processed |
| 82 | + await waitFor(async () => { |
| 83 | + const voiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 84 | + return voiceConfig?.voice === 'en-GB'; |
| 85 | + }, { timeout: 2000 }); |
| 86 | + |
| 87 | + const postChangeVoiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 88 | + |
| 89 | + expect(postChangeVoiceConfig).toEqual({ voice: 'en-GB', speed: 1.5 }); |
| 90 | + expect(postChangeVoiceConfig).not.toBe(preChangeVoiceConfig); |
| 91 | + |
| 92 | + // TEST 4: Same value should reuse reference (shallow equality check) |
| 93 | + const preNoChangeVoiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 94 | + |
| 95 | + // Set same value and emit event |
| 96 | + directLine.setCapability('getVoiceConfiguration', { voice: 'en-GB', speed: 1.5 }, { emitEvent: true }); |
| 97 | + |
| 98 | + // Wait for event to be processed |
| 99 | + await new Promise(resolve => setTimeout(resolve, 200)); |
| 100 | + |
| 101 | + const postNoChangeVoiceConfig = await renderHook(() => useCapabilities(caps => caps.voiceConfiguration)); |
| 102 | + |
| 103 | + // Reference should be the same when values are equal |
| 104 | + expect(postNoChangeVoiceConfig).toBe(preNoChangeVoiceConfig); |
| 105 | + expect(postNoChangeVoiceConfig).toEqual({ voice: 'en-GB', speed: 1.5 }); |
| 106 | + }); |
| 107 | + </script> |
| 108 | + </body> |
| 109 | +</html> |
0 commit comments