@copilotkit/react-core 1.57.3 → 1.58.0-canary.thread-id-propagation
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/{copilotkit-CtXcs1ea.cjs → copilotkit-B4ouY7qC.cjs} +14 -3
- package/dist/copilotkit-B4ouY7qC.cjs.map +1 -0
- package/dist/copilotkit-BK9CVq9A.d.cts.map +1 -1
- package/dist/{copilotkit-CC8DjOiC.mjs → copilotkit-L4mM_JqG.mjs} +14 -3
- package/dist/copilotkit-L4mM_JqG.mjs.map +1 -0
- package/dist/copilotkit-WlmeVijs.d.mts.map +1 -1
- package/dist/index.cjs +3 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +3 -77
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +15 -78
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/headless.cjs +11 -0
- package/dist/v2/headless.cjs.map +1 -1
- package/dist/v2/headless.d.cts.map +1 -1
- package/dist/v2/headless.d.mts.map +1 -1
- package/dist/v2/headless.mjs +11 -0
- package/dist/v2/headless.mjs.map +1 -1
- package/dist/v2/index.cjs +1 -1
- package/dist/v2/index.mjs +1 -1
- package/dist/v2/index.umd.js +13 -2
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +12 -13
- package/skills/react-core/SKILL.md +108 -0
- package/skills/react-core/references/agent-access.md +288 -0
- package/skills/react-core/references/attachments.md +291 -0
- package/skills/react-core/references/capabilities.md +138 -0
- package/skills/react-core/references/chat-components.md +221 -0
- package/skills/react-core/references/client-side-tools.md +358 -0
- package/skills/react-core/references/custom-message-renderers.md +226 -0
- package/skills/react-core/references/debug-mode.md +153 -0
- package/skills/react-core/references/human-in-the-loop.md +312 -0
- package/skills/react-core/references/provider-setup.md +326 -0
- package/skills/react-core/references/rendering-activity-messages.md +207 -0
- package/skills/react-core/references/rendering-tool-calls.md +319 -0
- package/skills/react-core/references/suggestions.md +211 -0
- package/skills/react-core/references/switching-agents-recipes.md +160 -0
- package/skills/react-core/references/switching-agents.md +231 -0
- package/skills/react-core/references/threads.md +226 -0
- package/.attw.json +0 -3
- package/CHANGELOG.md +0 -5043
- package/dist/copilotkit-CC8DjOiC.mjs.map +0 -1
- package/dist/copilotkit-CtXcs1ea.cjs.map +0 -1
- package/scripts/scope-preflight.mjs +0 -100
- package/src/components/CopilotListeners.tsx +0 -137
- package/src/components/__tests__/CopilotListeners.test.tsx +0 -38
- package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +0 -92
- package/src/components/copilot-provider/__tests__/copilotkit-error.test.tsx +0 -77
- package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
- package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +0 -107
- package/src/components/copilot-provider/copilot-messages.tsx +0 -314
- package/src/components/copilot-provider/copilotkit-props.tsx +0 -214
- package/src/components/copilot-provider/copilotkit.tsx +0 -853
- package/src/components/copilot-provider/index.ts +0 -3
- package/src/components/dev-console/console-trigger.tsx +0 -283
- package/src/components/dev-console/developer-console-modal.tsx +0 -1016
- package/src/components/dev-console/icons.tsx +0 -106
- package/src/components/error-boundary/error-boundary.tsx +0 -99
- package/src/components/error-boundary/error-utils.tsx +0 -105
- package/src/components/index.ts +0 -1
- package/src/components/toast/exclamation-mark-icon.tsx +0 -27
- package/src/components/toast/toast-provider.tsx +0 -448
- package/src/components/usage-banner.tsx +0 -266
- package/src/context/__tests__/threads-context.test.tsx +0 -141
- package/src/context/coagent-state-renders-context.tsx +0 -89
- package/src/context/copilot-context.tsx +0 -365
- package/src/context/copilot-messages-context.tsx +0 -35
- package/src/context/index.ts +0 -22
- package/src/context/threads-context.tsx +0 -69
- package/src/hooks/__tests__/use-coagent-config.test.ts +0 -352
- package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +0 -107
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +0 -1209
- package/src/hooks/__tests__/use-coagent-state-render.test.tsx +0 -356
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +0 -241
- package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -72
- package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +0 -102
- package/src/hooks/index.ts +0 -33
- package/src/hooks/use-agent-nodename.ts +0 -33
- package/src/hooks/use-coagent-state-render-bridge.helpers.ts +0 -345
- package/src/hooks/use-coagent-state-render-bridge.tsx +0 -222
- package/src/hooks/use-coagent-state-render-registry.ts +0 -230
- package/src/hooks/use-coagent-state-render.ts +0 -163
- package/src/hooks/use-coagent.ts +0 -377
- package/src/hooks/use-configure-chat-suggestions.tsx +0 -96
- package/src/hooks/use-copilot-action.ts +0 -245
- package/src/hooks/use-copilot-additional-instructions.ts +0 -98
- package/src/hooks/use-copilot-authenticated-action.ts +0 -73
- package/src/hooks/use-copilot-chat-headless_c.ts +0 -264
- package/src/hooks/use-copilot-chat-suggestions.tsx +0 -134
- package/src/hooks/use-copilot-chat.ts +0 -132
- package/src/hooks/use-copilot-chat_internal.ts +0 -875
- package/src/hooks/use-copilot-readable.ts +0 -135
- package/src/hooks/use-copilot-runtime-client.ts +0 -178
- package/src/hooks/use-default-tool.ts +0 -13
- package/src/hooks/use-flat-category-store.ts +0 -109
- package/src/hooks/use-frontend-tool.ts +0 -113
- package/src/hooks/use-human-in-the-loop.ts +0 -138
- package/src/hooks/use-langgraph-interrupt.ts +0 -103
- package/src/hooks/use-lazy-tool-renderer.tsx +0 -30
- package/src/hooks/use-make-copilot-document-readable.ts +0 -30
- package/src/hooks/use-render-tool-call.ts +0 -89
- package/src/hooks/use-tree.ts +0 -222
- package/src/index.tsx +0 -7
- package/src/lib/copilot-task.ts +0 -215
- package/src/lib/index.ts +0 -1
- package/src/lib/status-checker.ts +0 -67
- package/src/setupTests.ts +0 -37
- package/src/test-helpers/copilot-context.ts +0 -91
- package/src/types/chat-suggestion-configuration.ts +0 -23
- package/src/types/coagent-action.ts +0 -35
- package/src/types/coagent-state.ts +0 -13
- package/src/types/crew.ts +0 -89
- package/src/types/document-pointer.ts +0 -7
- package/src/types/frontend-action.ts +0 -213
- package/src/types/index.ts +0 -17
- package/src/types/interrupt-action.ts +0 -58
- package/src/types/system-message.ts +0 -4
- package/src/utils/dev-console.ts +0 -19
- package/src/utils/index.ts +0 -2
- package/src/utils/suggestions-constants.ts +0 -8
- package/src/utils/utils.test.ts +0 -7
- package/src/utils/utils.ts +0 -6
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +0 -240
- package/src/v2/__tests__/globalSetup.ts +0 -14
- package/src/v2/__tests__/setup.ts +0 -93
- package/src/v2/__tests__/utils/test-helpers.tsx +0 -570
- package/src/v2/a2ui/A2UICatalogContext.tsx +0 -79
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +0 -294
- package/src/v2/a2ui/A2UIToolCallRenderer.tsx +0 -290
- package/src/v2/components/CopilotKitInspector.tsx +0 -52
- package/src/v2/components/MCPAppsActivityRenderer.tsx +0 -815
- package/src/v2/components/OpenGenerativeUIRenderer.tsx +0 -598
- package/src/v2/components/WildcardToolCallRender.tsx +0 -86
- package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +0 -665
- package/src/v2/components/chat/CopilotChat.tsx +0 -664
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +0 -393
- package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +0 -374
- package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +0 -159
- package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +0 -350
- package/src/v2/components/chat/CopilotChatInput.tsx +0 -1412
- package/src/v2/components/chat/CopilotChatMessageView.tsx +0 -716
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +0 -265
- package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +0 -59
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +0 -134
- package/src/v2/components/chat/CopilotChatToggleButton.tsx +0 -171
- package/src/v2/components/chat/CopilotChatToolCallsView.tsx +0 -40
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +0 -445
- package/src/v2/components/chat/CopilotChatView.tsx +0 -890
- package/src/v2/components/chat/CopilotModalHeader.tsx +0 -129
- package/src/v2/components/chat/CopilotPopup.tsx +0 -81
- package/src/v2/components/chat/CopilotPopupView.tsx +0 -317
- package/src/v2/components/chat/CopilotSidebar.tsx +0 -80
- package/src/v2/components/chat/CopilotSidebarView.tsx +0 -269
- package/src/v2/components/chat/Lightbox.tsx +0 -103
- package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +0 -66
- package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +0 -168
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +0 -1239
- package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +0 -73
- package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +0 -432
- package/src/v2/components/chat/__tests__/CopilotChat.suggestionsAlways.test.tsx +0 -183
- package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +0 -184
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -649
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +0 -624
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +0 -702
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +0 -72
- package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
- package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +0 -107
- package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +0 -929
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +0 -1567
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +0 -1004
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +0 -279
- package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +0 -336
- package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +0 -249
- package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +0 -530
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +0 -785
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +0 -2416
- package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +0 -621
- package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +0 -56
- package/src/v2/components/chat/__tests__/CopilotChatView.inputOverlay.test.tsx +0 -264
- package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +0 -853
- package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +0 -94
- package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +0 -1050
- package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +0 -484
- package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +0 -612
- package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +0 -159
- package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +0 -502
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +0 -1068
- package/src/v2/components/chat/__tests__/MCPAppsProxy.e2e.test.tsx +0 -589
- package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +0 -403
- package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -137
- package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +0 -37
- package/src/v2/components/chat/__tests__/setup.ts +0 -1
- package/src/v2/components/chat/index.ts +0 -90
- package/src/v2/components/chat/last-user-message-context.ts +0 -21
- package/src/v2/components/chat/normalize-auto-scroll.ts +0 -17
- package/src/v2/components/chat/scroll-element-context.ts +0 -13
- package/src/v2/components/index.ts +0 -8
- package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +0 -286
- package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +0 -464
- package/src/v2/components/intelligence-indicator/index.ts +0 -2
- package/src/v2/components/license-warning-banner.tsx +0 -217
- package/src/v2/components/ui/button.tsx +0 -124
- package/src/v2/components/ui/dropdown-menu.tsx +0 -258
- package/src/v2/components/ui/tooltip.tsx +0 -60
- package/src/v2/context.ts +0 -62
- package/src/v2/headless.ts +0 -64
- package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +0 -152
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +0 -282
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -140
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +0 -401
- package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +0 -44
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +0 -211
- package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +0 -1029
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +0 -159
- package/src/v2/hooks/__tests__/use-attachments.test.tsx +0 -169
- package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
- package/src/v2/hooks/__tests__/use-component.test.tsx +0 -126
- package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +0 -696
- package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +0 -153
- package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -167
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +0 -2148
- package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +0 -1261
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +0 -397
- package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +0 -56
- package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +0 -192
- package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +0 -219
- package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +0 -55
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +0 -259
- package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +0 -524
- package/src/v2/hooks/__tests__/use-threads.test.tsx +0 -757
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +0 -311
- package/src/v2/hooks/index.ts +0 -24
- package/src/v2/hooks/use-agent-context.tsx +0 -45
- package/src/v2/hooks/use-agent.tsx +0 -227
- package/src/v2/hooks/use-attachments.tsx +0 -269
- package/src/v2/hooks/use-capabilities.tsx +0 -25
- package/src/v2/hooks/use-component.tsx +0 -91
- package/src/v2/hooks/use-configure-suggestions.tsx +0 -236
- package/src/v2/hooks/use-default-render-tool.tsx +0 -271
- package/src/v2/hooks/use-frontend-tool.tsx +0 -46
- package/src/v2/hooks/use-human-in-the-loop.tsx +0 -81
- package/src/v2/hooks/use-interrupt.tsx +0 -305
- package/src/v2/hooks/use-keyboard-height.tsx +0 -67
- package/src/v2/hooks/use-pin-to-send.ts +0 -94
- package/src/v2/hooks/use-render-activity-message.tsx +0 -72
- package/src/v2/hooks/use-render-custom-messages.tsx +0 -93
- package/src/v2/hooks/use-render-tool-call.tsx +0 -208
- package/src/v2/hooks/use-render-tool.tsx +0 -184
- package/src/v2/hooks/use-suggestions.tsx +0 -91
- package/src/v2/hooks/use-threads.tsx +0 -325
- package/src/v2/hooks/useKatexStyles.ts +0 -27
- package/src/v2/index.css +0 -1
- package/src/v2/index.ts +0 -27
- package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +0 -495
- package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -112
- package/src/v2/lib/__tests__/renderSlot.test.tsx +0 -588
- package/src/v2/lib/__tests__/slots.test.ts +0 -56
- package/src/v2/lib/processPartialHtml.ts +0 -45
- package/src/v2/lib/react-core.ts +0 -156
- package/src/v2/lib/slots.tsx +0 -184
- package/src/v2/lib/transcription-client.ts +0 -184
- package/src/v2/lib/utils.ts +0 -8
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +0 -196
- package/src/v2/providers/CopilotKitProvider.tsx +0 -800
- package/src/v2/providers/SandboxFunctionsContext.ts +0 -10
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +0 -652
- package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +0 -101
- package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +0 -69
- package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +0 -881
- package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +0 -198
- package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +0 -740
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +0 -713
- package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +0 -294
- package/src/v2/providers/index.ts +0 -21
- package/src/v2/styles/globals.css +0 -349
- package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +0 -525
- package/src/v2/types/defineToolCallRenderer.ts +0 -68
- package/src/v2/types/frontend-tool.ts +0 -8
- package/src/v2/types/human-in-the-loop.ts +0 -33
- package/src/v2/types/index.ts +0 -8
- package/src/v2/types/interrupt.ts +0 -15
- package/src/v2/types/react-activity-message-renderer.ts +0 -27
- package/src/v2/types/react-custom-message-renderer.ts +0 -17
- package/src/v2/types/react-tool-call-renderer.ts +0 -35
- package/src/v2/types/sandbox-function.ts +0 -11
- package/tsconfig.json +0 -8
- package/tsdown.config.ts +0 -193
- package/typedoc.json +0 -4
- package/vitest.config.mjs +0 -31
|
@@ -1,1567 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import {
|
|
3
|
-
render,
|
|
4
|
-
screen,
|
|
5
|
-
fireEvent,
|
|
6
|
-
waitFor,
|
|
7
|
-
within,
|
|
8
|
-
} from "@testing-library/react";
|
|
9
|
-
import userEvent from "@testing-library/user-event";
|
|
10
|
-
import { vi } from "vitest";
|
|
11
|
-
import { CopilotChatInput } from "../CopilotChatInput";
|
|
12
|
-
import { CopilotChatConfigurationProvider } from "../../../providers/CopilotChatConfigurationProvider";
|
|
13
|
-
|
|
14
|
-
// Mock onSubmitMessage function to track calls
|
|
15
|
-
const mockOnSubmitMessage = vi.fn();
|
|
16
|
-
|
|
17
|
-
const TEST_THREAD_ID = "test-thread";
|
|
18
|
-
|
|
19
|
-
// Helper to render components with context provider
|
|
20
|
-
const renderWithProvider = (component: React.ReactElement) => {
|
|
21
|
-
return render(
|
|
22
|
-
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
23
|
-
{component}
|
|
24
|
-
</CopilotChatConfigurationProvider>,
|
|
25
|
-
);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const getSendButton = (container: HTMLElement) =>
|
|
29
|
-
container
|
|
30
|
-
.querySelector("svg.lucide-arrow-up")
|
|
31
|
-
?.closest("button") as HTMLButtonElement | null;
|
|
32
|
-
|
|
33
|
-
const getAddMenuButton = (container: HTMLElement) =>
|
|
34
|
-
container
|
|
35
|
-
.querySelector("svg.lucide-plus")
|
|
36
|
-
?.closest("button") as HTMLButtonElement | null;
|
|
37
|
-
|
|
38
|
-
const getLayoutGrid = (textarea: HTMLElement) =>
|
|
39
|
-
textarea.closest("[data-layout]") as HTMLElement;
|
|
40
|
-
|
|
41
|
-
const mockLayoutMetrics = (
|
|
42
|
-
container: HTMLElement,
|
|
43
|
-
options?: { gridWidth?: number; addWidth?: number; actionsWidth?: number },
|
|
44
|
-
) => {
|
|
45
|
-
const grid = container.querySelector("div.cpk\\:grid") as HTMLElement | null;
|
|
46
|
-
if (!grid) {
|
|
47
|
-
throw new Error("Grid container not found in CopilotChatInput layout");
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const { gridWidth = 640, addWidth = 48, actionsWidth = 96 } = options ?? {};
|
|
51
|
-
|
|
52
|
-
Object.defineProperty(grid, "clientWidth", {
|
|
53
|
-
value: gridWidth,
|
|
54
|
-
configurable: true,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
const rectFactory = (width: number) => () => ({
|
|
58
|
-
width,
|
|
59
|
-
height: 40,
|
|
60
|
-
top: 0,
|
|
61
|
-
left: 0,
|
|
62
|
-
right: width,
|
|
63
|
-
bottom: 40,
|
|
64
|
-
x: 0,
|
|
65
|
-
y: 0,
|
|
66
|
-
toJSON: () => ({}),
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
const addContainer = grid.children[0] as HTMLElement;
|
|
70
|
-
const actionsContainer = grid.children[2] as HTMLElement;
|
|
71
|
-
|
|
72
|
-
Object.defineProperty(addContainer, "getBoundingClientRect", {
|
|
73
|
-
value: rectFactory(addWidth),
|
|
74
|
-
configurable: true,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
Object.defineProperty(actionsContainer, "getBoundingClientRect", {
|
|
78
|
-
value: rectFactory(actionsWidth),
|
|
79
|
-
configurable: true,
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// Mock scrollHeight for textareas since jsdom doesn't support it properly
|
|
84
|
-
Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", {
|
|
85
|
-
configurable: true,
|
|
86
|
-
get: function (this: HTMLTextAreaElement) {
|
|
87
|
-
const text = this.value || "";
|
|
88
|
-
|
|
89
|
-
// Calculate lines based on explicit newlines
|
|
90
|
-
const explicitLines = text.split("\n").length;
|
|
91
|
-
|
|
92
|
-
// Simulate text wrapping for long lines (rough approximation)
|
|
93
|
-
// Assume ~50 characters per line as a rough width threshold
|
|
94
|
-
let wrappedLines = 0;
|
|
95
|
-
text.split("\n").forEach((line) => {
|
|
96
|
-
const lineWraps = Math.ceil(line.length / 50);
|
|
97
|
-
wrappedLines += Math.max(1, lineWraps);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const totalLines = Math.max(explicitLines, wrappedLines);
|
|
101
|
-
return totalLines * 24; // 24px per line
|
|
102
|
-
},
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// Clear mocks before each test
|
|
106
|
-
beforeEach(() => {
|
|
107
|
-
mockOnSubmitMessage.mockClear();
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
describe("CopilotChatInput", () => {
|
|
111
|
-
it("renders with default components and styling", () => {
|
|
112
|
-
const mockOnChange = vi.fn();
|
|
113
|
-
const { container } = renderWithProvider(
|
|
114
|
-
<CopilotChatInput
|
|
115
|
-
value=""
|
|
116
|
-
onChange={mockOnChange}
|
|
117
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
118
|
-
/>,
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
const input = screen.getByPlaceholderText("Type a message...");
|
|
122
|
-
const sendButton = getSendButton(container);
|
|
123
|
-
|
|
124
|
-
expect(input).toBeDefined();
|
|
125
|
-
expect(sendButton).not.toBeNull();
|
|
126
|
-
expect(sendButton?.disabled).toBe(true); // Should be disabled when input is empty
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it("calls onSubmitMessage with trimmed text when Enter is pressed", () => {
|
|
130
|
-
const mockOnChange = vi.fn();
|
|
131
|
-
renderWithProvider(
|
|
132
|
-
<CopilotChatInput
|
|
133
|
-
value=" hello world "
|
|
134
|
-
onChange={mockOnChange}
|
|
135
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
136
|
-
/>,
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
const input = screen.getByPlaceholderText("Type a message...");
|
|
140
|
-
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
141
|
-
|
|
142
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello world");
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it("calls onSubmitMessage when button is clicked", () => {
|
|
146
|
-
const mockOnChange = vi.fn();
|
|
147
|
-
const { container } = renderWithProvider(
|
|
148
|
-
<CopilotChatInput
|
|
149
|
-
value="test message"
|
|
150
|
-
onChange={mockOnChange}
|
|
151
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
152
|
-
/>,
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
const sendButton = getSendButton(container);
|
|
156
|
-
expect(sendButton).not.toBeNull();
|
|
157
|
-
fireEvent.click(sendButton!);
|
|
158
|
-
|
|
159
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("test message");
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it("manages text state internally when uncontrolled", () => {
|
|
163
|
-
const { container } = renderWithProvider(
|
|
164
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
const input = screen.getByPlaceholderText("Type a message...");
|
|
168
|
-
const sendButton = getSendButton(container);
|
|
169
|
-
|
|
170
|
-
fireEvent.change(input, { target: { value: "hello" } });
|
|
171
|
-
fireEvent.click(sendButton!);
|
|
172
|
-
|
|
173
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello");
|
|
174
|
-
expect((input as HTMLTextAreaElement).value).toBe("");
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it("does not send when Enter is pressed with Shift key", () => {
|
|
178
|
-
const mockOnChange = vi.fn();
|
|
179
|
-
renderWithProvider(
|
|
180
|
-
<CopilotChatInput
|
|
181
|
-
value="test"
|
|
182
|
-
onChange={mockOnChange}
|
|
183
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
184
|
-
/>,
|
|
185
|
-
);
|
|
186
|
-
|
|
187
|
-
const input = screen.getByPlaceholderText("Type a message...");
|
|
188
|
-
fireEvent.keyDown(input, { key: "Enter", shiftKey: true });
|
|
189
|
-
|
|
190
|
-
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it("does not send empty or whitespace-only messages", () => {
|
|
194
|
-
const mockOnChange = vi.fn();
|
|
195
|
-
|
|
196
|
-
// Test empty string
|
|
197
|
-
const { container, rerender } = renderWithProvider(
|
|
198
|
-
<CopilotChatInput
|
|
199
|
-
value=""
|
|
200
|
-
onChange={mockOnChange}
|
|
201
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
202
|
-
/>,
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
let sendButton = getSendButton(container);
|
|
206
|
-
fireEvent.click(sendButton!);
|
|
207
|
-
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
208
|
-
|
|
209
|
-
// Test whitespace only
|
|
210
|
-
rerender(
|
|
211
|
-
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
212
|
-
<CopilotChatInput
|
|
213
|
-
value=" "
|
|
214
|
-
onChange={mockOnChange}
|
|
215
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
216
|
-
/>
|
|
217
|
-
</CopilotChatConfigurationProvider>,
|
|
218
|
-
);
|
|
219
|
-
sendButton = getSendButton(container);
|
|
220
|
-
fireEvent.click(sendButton!);
|
|
221
|
-
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("keeps input value when no submit handler is provided", () => {
|
|
225
|
-
renderWithProvider(<CopilotChatInput />);
|
|
226
|
-
|
|
227
|
-
const input = screen.getByPlaceholderText("Type a message...");
|
|
228
|
-
|
|
229
|
-
fireEvent.change(input, { target: { value: "draft" } });
|
|
230
|
-
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
231
|
-
|
|
232
|
-
expect((input as HTMLTextAreaElement).value).toBe("draft");
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it("enables button based on value prop", () => {
|
|
236
|
-
const mockOnChange = vi.fn();
|
|
237
|
-
|
|
238
|
-
// Test with empty value
|
|
239
|
-
const { container, rerender } = renderWithProvider(
|
|
240
|
-
<CopilotChatInput
|
|
241
|
-
value=""
|
|
242
|
-
onChange={mockOnChange}
|
|
243
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
244
|
-
/>,
|
|
245
|
-
);
|
|
246
|
-
|
|
247
|
-
let sendButton = getSendButton(container);
|
|
248
|
-
expect(sendButton?.disabled).toBe(true);
|
|
249
|
-
|
|
250
|
-
// Test with non-empty value
|
|
251
|
-
rerender(
|
|
252
|
-
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
253
|
-
<CopilotChatInput
|
|
254
|
-
value="hello"
|
|
255
|
-
onChange={mockOnChange}
|
|
256
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
257
|
-
/>
|
|
258
|
-
</CopilotChatConfigurationProvider>,
|
|
259
|
-
);
|
|
260
|
-
sendButton = getSendButton(container);
|
|
261
|
-
expect(sendButton?.disabled).toBe(false);
|
|
262
|
-
|
|
263
|
-
// Test with empty value again
|
|
264
|
-
rerender(
|
|
265
|
-
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
266
|
-
<CopilotChatInput
|
|
267
|
-
value=""
|
|
268
|
-
onChange={mockOnChange}
|
|
269
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
270
|
-
/>
|
|
271
|
-
</CopilotChatConfigurationProvider>,
|
|
272
|
-
);
|
|
273
|
-
sendButton = getSendButton(container);
|
|
274
|
-
expect(sendButton?.disabled).toBe(true);
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
it("accepts custom slot classes", () => {
|
|
278
|
-
const mockOnChange = vi.fn();
|
|
279
|
-
const { container } = renderWithProvider(
|
|
280
|
-
<CopilotChatInput
|
|
281
|
-
value=""
|
|
282
|
-
onChange={mockOnChange}
|
|
283
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
284
|
-
className="custom-container"
|
|
285
|
-
textArea="custom-textarea"
|
|
286
|
-
sendButton="custom-button"
|
|
287
|
-
/>,
|
|
288
|
-
);
|
|
289
|
-
|
|
290
|
-
const containerDiv = container.firstChild as HTMLElement;
|
|
291
|
-
const input = screen.getByPlaceholderText("Type a message...");
|
|
292
|
-
const sendButton = getSendButton(container);
|
|
293
|
-
|
|
294
|
-
expect(containerDiv.classList.contains("custom-container")).toBe(true);
|
|
295
|
-
expect(input.classList.contains("custom-textarea")).toBe(true);
|
|
296
|
-
expect(sendButton?.classList.contains("custom-button")).toBe(true);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
it("accepts custom components via slots", () => {
|
|
300
|
-
const mockOnChange = vi.fn();
|
|
301
|
-
const CustomButton = (
|
|
302
|
-
props: React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
303
|
-
) => (
|
|
304
|
-
<button {...props} data-testid="custom-button">
|
|
305
|
-
Send Now
|
|
306
|
-
</button>
|
|
307
|
-
);
|
|
308
|
-
|
|
309
|
-
renderWithProvider(
|
|
310
|
-
<CopilotChatInput
|
|
311
|
-
value=""
|
|
312
|
-
onChange={mockOnChange}
|
|
313
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
314
|
-
sendButton={CustomButton}
|
|
315
|
-
/>,
|
|
316
|
-
);
|
|
317
|
-
|
|
318
|
-
const customButton = screen.getByTestId("custom-button");
|
|
319
|
-
expect(customButton).toBeDefined();
|
|
320
|
-
expect(customButton.textContent?.includes("Send Now")).toBe(true);
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
it("supports custom layout via children render prop", () => {
|
|
324
|
-
const mockOnChange = vi.fn();
|
|
325
|
-
renderWithProvider(
|
|
326
|
-
<CopilotChatInput
|
|
327
|
-
value=""
|
|
328
|
-
onChange={mockOnChange}
|
|
329
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
330
|
-
>
|
|
331
|
-
{({ textArea: TextArea, sendButton: SendButton }) => (
|
|
332
|
-
<div data-testid="custom-layout">
|
|
333
|
-
Custom Layout:
|
|
334
|
-
{SendButton}
|
|
335
|
-
{TextArea}
|
|
336
|
-
</div>
|
|
337
|
-
)}
|
|
338
|
-
</CopilotChatInput>,
|
|
339
|
-
);
|
|
340
|
-
|
|
341
|
-
const customLayout = screen.getByTestId("custom-layout");
|
|
342
|
-
expect(customLayout).toBeDefined();
|
|
343
|
-
expect(customLayout.textContent?.includes("Custom Layout:")).toBe(true);
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
it("passes containerRef to children render prop", () => {
|
|
347
|
-
const ref = React.createRef<HTMLDivElement>();
|
|
348
|
-
renderWithProvider(
|
|
349
|
-
<CopilotChatInput
|
|
350
|
-
value=""
|
|
351
|
-
onChange={vi.fn()}
|
|
352
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
353
|
-
containerRef={ref}
|
|
354
|
-
>
|
|
355
|
-
{({ containerRef }) => (
|
|
356
|
-
<div ref={containerRef} data-testid="custom-container">
|
|
357
|
-
Custom Container
|
|
358
|
-
</div>
|
|
359
|
-
)}
|
|
360
|
-
</CopilotChatInput>,
|
|
361
|
-
);
|
|
362
|
-
|
|
363
|
-
const container = screen.getByTestId("custom-container");
|
|
364
|
-
expect(container).toBeDefined();
|
|
365
|
-
expect(ref.current).toBe(container);
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
it("updates its internal layout data attribute when content expands", async () => {
|
|
369
|
-
renderWithProvider(
|
|
370
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
371
|
-
);
|
|
372
|
-
|
|
373
|
-
const textarea = screen.getByRole("textbox");
|
|
374
|
-
const grid = textarea.closest("[data-layout]") as HTMLElement | null;
|
|
375
|
-
expect(grid?.getAttribute("data-layout")).toBe("compact");
|
|
376
|
-
|
|
377
|
-
fireEvent.change(textarea, { target: { value: "line one\nline two" } });
|
|
378
|
-
|
|
379
|
-
await waitFor(() => {
|
|
380
|
-
expect(grid?.getAttribute("data-layout")).toBe("expanded");
|
|
381
|
-
});
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
it("executes slash commands via keyboard selection", async () => {
|
|
385
|
-
const handleFirst = vi.fn();
|
|
386
|
-
const handleSecond = vi.fn();
|
|
387
|
-
|
|
388
|
-
renderWithProvider(
|
|
389
|
-
<CopilotChatInput
|
|
390
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
391
|
-
toolsMenu={[
|
|
392
|
-
{ label: "Say hi", action: handleFirst },
|
|
393
|
-
{ label: "Open docs", action: handleSecond },
|
|
394
|
-
]}
|
|
395
|
-
/>,
|
|
396
|
-
);
|
|
397
|
-
|
|
398
|
-
const textarea = screen.getByRole("textbox");
|
|
399
|
-
fireEvent.change(textarea, { target: { value: "/" } });
|
|
400
|
-
|
|
401
|
-
const menu = await screen.findByTestId("copilot-slash-menu");
|
|
402
|
-
expect(menu).not.toBeNull();
|
|
403
|
-
expect(screen.queryByText("Say hi")).not.toBeNull();
|
|
404
|
-
expect(screen.queryByText("Open docs")).not.toBeNull();
|
|
405
|
-
|
|
406
|
-
fireEvent.keyDown(textarea, {
|
|
407
|
-
key: "ArrowDown",
|
|
408
|
-
code: "ArrowDown",
|
|
409
|
-
keyCode: 40,
|
|
410
|
-
});
|
|
411
|
-
fireEvent.keyDown(textarea, { key: "Enter", code: "Enter", keyCode: 13 });
|
|
412
|
-
|
|
413
|
-
expect(handleSecond).toHaveBeenCalledTimes(1);
|
|
414
|
-
expect(handleFirst).not.toHaveBeenCalled();
|
|
415
|
-
|
|
416
|
-
await waitFor(() => {
|
|
417
|
-
expect(screen.queryByTestId("copilot-slash-menu")).toBeNull();
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
expect((textarea as HTMLTextAreaElement).value).toBe("");
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
it("prioritizes prefix matches when filtering slash commands", async () => {
|
|
424
|
-
renderWithProvider(
|
|
425
|
-
<CopilotChatInput
|
|
426
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
427
|
-
toolsMenu={[
|
|
428
|
-
{ label: "Reopen previous chat", action: vi.fn() },
|
|
429
|
-
{ label: "Open CopilotKit", action: vi.fn() },
|
|
430
|
-
{ label: "Help me operate", action: vi.fn() },
|
|
431
|
-
]}
|
|
432
|
-
/>,
|
|
433
|
-
);
|
|
434
|
-
|
|
435
|
-
const textarea = screen.getByRole("textbox");
|
|
436
|
-
fireEvent.change(textarea, { target: { value: "/op" } });
|
|
437
|
-
|
|
438
|
-
const menu = await screen.findByTestId("copilot-slash-menu");
|
|
439
|
-
const options = within(menu).getAllByRole("option");
|
|
440
|
-
|
|
441
|
-
expect(options[0]?.textContent?.includes("Open CopilotKit")).toBe(true);
|
|
442
|
-
expect(options[0]?.getAttribute("aria-selected")).toBe("true");
|
|
443
|
-
|
|
444
|
-
fireEvent.keyDown(textarea, {
|
|
445
|
-
key: "ArrowDown",
|
|
446
|
-
code: "ArrowDown",
|
|
447
|
-
keyCode: 40,
|
|
448
|
-
});
|
|
449
|
-
await waitFor(() => {
|
|
450
|
-
const updated = within(menu).getAllByRole("option");
|
|
451
|
-
expect(updated[1]?.getAttribute("aria-selected")).toBe("true");
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
fireEvent.change(textarea, { target: { value: "/ope" } });
|
|
455
|
-
|
|
456
|
-
await waitFor(() => {
|
|
457
|
-
const updatedOptions = within(menu).getAllByRole("option");
|
|
458
|
-
expect(updatedOptions[0]?.getAttribute("aria-selected")).toBe("true");
|
|
459
|
-
expect(
|
|
460
|
-
updatedOptions[0]?.textContent?.startsWith("Open CopilotKit"),
|
|
461
|
-
).toBe(true);
|
|
462
|
-
});
|
|
463
|
-
});
|
|
464
|
-
|
|
465
|
-
it("limits slash menu height when commands exceed five items", async () => {
|
|
466
|
-
const tools = Array.from({ length: 6 }, (_, index) => ({
|
|
467
|
-
label: `Command ${index + 1}`,
|
|
468
|
-
action: vi.fn(),
|
|
469
|
-
}));
|
|
470
|
-
|
|
471
|
-
renderWithProvider(
|
|
472
|
-
<CopilotChatInput
|
|
473
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
474
|
-
toolsMenu={tools}
|
|
475
|
-
/>,
|
|
476
|
-
);
|
|
477
|
-
|
|
478
|
-
const textarea = screen.getByRole("textbox");
|
|
479
|
-
fireEvent.change(textarea, { target: { value: "/" } });
|
|
480
|
-
|
|
481
|
-
const menu = await screen.findByTestId("copilot-slash-menu");
|
|
482
|
-
|
|
483
|
-
await waitFor(() => {
|
|
484
|
-
expect(menu.style.maxHeight).toBe("200px");
|
|
485
|
-
});
|
|
486
|
-
|
|
487
|
-
const options = within(menu).getAllByRole("option");
|
|
488
|
-
expect(options.length).toBe(6);
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
it("allows slash command actions to populate the input", async () => {
|
|
492
|
-
const greeting = "Hello Copilot! 👋 Could you help me with something?";
|
|
493
|
-
const label = "Say hi to CopilotKit";
|
|
494
|
-
|
|
495
|
-
renderWithProvider(
|
|
496
|
-
<CopilotChatInput
|
|
497
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
498
|
-
toolsMenu={[
|
|
499
|
-
{
|
|
500
|
-
label,
|
|
501
|
-
action: () => {
|
|
502
|
-
const textareaElement =
|
|
503
|
-
document.querySelector<HTMLTextAreaElement>("textarea");
|
|
504
|
-
if (!textareaElement) {
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
const nativeSetter = Object.getOwnPropertyDescriptor(
|
|
509
|
-
window.HTMLTextAreaElement.prototype,
|
|
510
|
-
"value",
|
|
511
|
-
)?.set;
|
|
512
|
-
nativeSetter?.call(textareaElement, greeting);
|
|
513
|
-
textareaElement.dispatchEvent(
|
|
514
|
-
new Event("input", { bubbles: true }),
|
|
515
|
-
);
|
|
516
|
-
},
|
|
517
|
-
},
|
|
518
|
-
]}
|
|
519
|
-
/>,
|
|
520
|
-
);
|
|
521
|
-
|
|
522
|
-
const textarea = screen.getByRole("textbox");
|
|
523
|
-
fireEvent.change(textarea, { target: { value: "/" } });
|
|
524
|
-
|
|
525
|
-
const option = await screen.findByRole("option", { name: label });
|
|
526
|
-
fireEvent.mouseDown(option);
|
|
527
|
-
|
|
528
|
-
await waitFor(() => {
|
|
529
|
-
expect((textarea as HTMLTextAreaElement).value).toBe(greeting);
|
|
530
|
-
});
|
|
531
|
-
});
|
|
532
|
-
|
|
533
|
-
it("shows cancel and finish buttons in transcribe mode", () => {
|
|
534
|
-
const { container } = renderWithProvider(
|
|
535
|
-
<CopilotChatInput
|
|
536
|
-
mode="transcribe"
|
|
537
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
538
|
-
onStartTranscribe={() => {}}
|
|
539
|
-
onCancelTranscribe={() => {}}
|
|
540
|
-
onFinishTranscribe={() => {}}
|
|
541
|
-
onAddFile={() => {}}
|
|
542
|
-
/>,
|
|
543
|
-
);
|
|
544
|
-
|
|
545
|
-
// Should show cancel button (X icon) - find by svg class
|
|
546
|
-
const cancelIcon = container.querySelector("svg.lucide-x");
|
|
547
|
-
expect(cancelIcon).toBeDefined();
|
|
548
|
-
|
|
549
|
-
// Should show finish button (checkmark icon) - find by svg class
|
|
550
|
-
const finishIcon = container.querySelector("svg.lucide-check");
|
|
551
|
-
expect(finishIcon).toBeDefined();
|
|
552
|
-
|
|
553
|
-
// Should show cancel button (X icon) and finish button (check icon)
|
|
554
|
-
const cancelButton = container.querySelector("svg.lucide-x");
|
|
555
|
-
const finishButton = container.querySelector("svg.lucide-check");
|
|
556
|
-
expect(cancelButton).toBeDefined();
|
|
557
|
-
expect(finishButton).toBeDefined();
|
|
558
|
-
|
|
559
|
-
// Should NOT show transcribe button (mic icon) in transcribe mode
|
|
560
|
-
const transcribeIcon = container.querySelector("svg.lucide-mic");
|
|
561
|
-
expect(transcribeIcon).toBeNull();
|
|
562
|
-
|
|
563
|
-
// Should NOT show send button (arrow-up icon) in transcribe mode
|
|
564
|
-
const sendIcon = container.querySelector("svg.lucide-arrow-up");
|
|
565
|
-
expect(sendIcon).toBeNull();
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
it("disables add menu button in transcribe mode", () => {
|
|
569
|
-
const { container } = renderWithProvider(
|
|
570
|
-
<CopilotChatInput
|
|
571
|
-
mode="transcribe"
|
|
572
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
573
|
-
onStartTranscribe={() => {}}
|
|
574
|
-
onCancelTranscribe={() => {}}
|
|
575
|
-
onFinishTranscribe={() => {}}
|
|
576
|
-
onAddFile={() => {}}
|
|
577
|
-
toolsMenu={[{ label: "Test Tool", action: () => {} }]}
|
|
578
|
-
/>,
|
|
579
|
-
);
|
|
580
|
-
|
|
581
|
-
// Add button should be disabled (find by Plus icon)
|
|
582
|
-
const addIcon = container.querySelector("svg.lucide-plus");
|
|
583
|
-
const addButton = addIcon?.closest("button") as HTMLButtonElement | null;
|
|
584
|
-
expect(addButton).not.toBeNull();
|
|
585
|
-
expect(addButton?.disabled).toBe(true);
|
|
586
|
-
});
|
|
587
|
-
|
|
588
|
-
it("shows recording indicator instead of textarea in transcribe mode", () => {
|
|
589
|
-
const { container } = renderWithProvider(
|
|
590
|
-
<CopilotChatInput
|
|
591
|
-
mode="transcribe"
|
|
592
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
593
|
-
onStartTranscribe={() => {}}
|
|
594
|
-
onCancelTranscribe={() => {}}
|
|
595
|
-
onFinishTranscribe={() => {}}
|
|
596
|
-
onAddFile={() => {}}
|
|
597
|
-
/>,
|
|
598
|
-
);
|
|
599
|
-
|
|
600
|
-
// Should show recording indicator (canvas element)
|
|
601
|
-
const recordingIndicator = container.querySelector("canvas");
|
|
602
|
-
expect(recordingIndicator).toBeDefined();
|
|
603
|
-
|
|
604
|
-
// Should NOT show textarea in transcribe mode
|
|
605
|
-
expect(screen.queryByRole("textbox")).toBeNull();
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
it("shows textarea in input mode", () => {
|
|
609
|
-
const { container } = renderWithProvider(
|
|
610
|
-
<CopilotChatInput
|
|
611
|
-
mode="input"
|
|
612
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
613
|
-
onStartTranscribe={() => {}}
|
|
614
|
-
onCancelTranscribe={() => {}}
|
|
615
|
-
onFinishTranscribe={() => {}}
|
|
616
|
-
onAddFile={() => {}}
|
|
617
|
-
/>,
|
|
618
|
-
);
|
|
619
|
-
|
|
620
|
-
// Should show textarea in input mode
|
|
621
|
-
expect(screen.getByRole("textbox")).toBeDefined();
|
|
622
|
-
|
|
623
|
-
// Should NOT show recording indicator (red div)
|
|
624
|
-
const recordingIndicator = container.querySelector(".bg-red-500");
|
|
625
|
-
expect(recordingIndicator).toBeNull();
|
|
626
|
-
});
|
|
627
|
-
|
|
628
|
-
it("positions the textarea next to the add menu button when single line", () => {
|
|
629
|
-
renderWithProvider(
|
|
630
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
631
|
-
);
|
|
632
|
-
|
|
633
|
-
const textarea = screen.getByRole("textbox");
|
|
634
|
-
const layoutCell = textarea.parentElement as HTMLElement;
|
|
635
|
-
const gridContainer = layoutCell?.parentElement as HTMLElement;
|
|
636
|
-
|
|
637
|
-
expect(layoutCell.className).toContain("col-start-2");
|
|
638
|
-
expect(layoutCell.className).not.toContain("col-span-3");
|
|
639
|
-
expect(gridContainer.className).toContain("items-center");
|
|
640
|
-
});
|
|
641
|
-
|
|
642
|
-
it("toggles textarea padding based on multiline state", async () => {
|
|
643
|
-
const { container } = renderWithProvider(
|
|
644
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
645
|
-
);
|
|
646
|
-
|
|
647
|
-
mockLayoutMetrics(container);
|
|
648
|
-
|
|
649
|
-
const textarea = screen.getByRole("textbox");
|
|
650
|
-
expect(textarea.className).toContain("pr-5");
|
|
651
|
-
expect(textarea.className).not.toContain("px-5");
|
|
652
|
-
|
|
653
|
-
fireEvent.change(textarea, {
|
|
654
|
-
target: {
|
|
655
|
-
value:
|
|
656
|
-
"a very long line that should wrap once it exceeds the width of the input",
|
|
657
|
-
},
|
|
658
|
-
});
|
|
659
|
-
|
|
660
|
-
await waitFor(() => {
|
|
661
|
-
expect(textarea.className).toContain("px-5");
|
|
662
|
-
expect(textarea.className).not.toContain("pr-5");
|
|
663
|
-
});
|
|
664
|
-
});
|
|
665
|
-
|
|
666
|
-
it("returns to the compact layout when text no longer needs extra space", async () => {
|
|
667
|
-
const { container } = renderWithProvider(
|
|
668
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
669
|
-
);
|
|
670
|
-
|
|
671
|
-
mockLayoutMetrics(container);
|
|
672
|
-
|
|
673
|
-
const textarea = screen.getByRole("textbox");
|
|
674
|
-
const layoutCell = textarea.parentElement as HTMLElement;
|
|
675
|
-
|
|
676
|
-
fireEvent.change(textarea, {
|
|
677
|
-
target: {
|
|
678
|
-
value:
|
|
679
|
-
"this is a very long line that should expand the layout before it wraps so we can see the stacked arrangement",
|
|
680
|
-
},
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
await waitFor(() => {
|
|
684
|
-
expect(layoutCell.className).toContain("col-span-3");
|
|
685
|
-
});
|
|
686
|
-
|
|
687
|
-
fireEvent.change(textarea, { target: { value: "short" } });
|
|
688
|
-
|
|
689
|
-
await waitFor(() => {
|
|
690
|
-
expect(layoutCell.className).toContain("col-start-2");
|
|
691
|
-
expect(layoutCell.className).not.toContain("col-span-3");
|
|
692
|
-
});
|
|
693
|
-
});
|
|
694
|
-
|
|
695
|
-
it("moves the textarea above the add menu button when multiple lines", async () => {
|
|
696
|
-
renderWithProvider(
|
|
697
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
698
|
-
);
|
|
699
|
-
|
|
700
|
-
const textarea = screen.getByRole("textbox");
|
|
701
|
-
|
|
702
|
-
fireEvent.change(textarea, {
|
|
703
|
-
target: { value: "first line\nsecond line" },
|
|
704
|
-
});
|
|
705
|
-
|
|
706
|
-
await waitFor(() => {
|
|
707
|
-
const layoutCell = textarea.parentElement as HTMLElement;
|
|
708
|
-
expect(layoutCell.className).toContain("col-span-3");
|
|
709
|
-
expect(layoutCell.className).not.toContain("col-start-2");
|
|
710
|
-
});
|
|
711
|
-
});
|
|
712
|
-
|
|
713
|
-
it("disables the add menu button when no menu items are provided", () => {
|
|
714
|
-
const { container } = renderWithProvider(
|
|
715
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
716
|
-
);
|
|
717
|
-
|
|
718
|
-
const addButton = getAddMenuButton(container);
|
|
719
|
-
|
|
720
|
-
expect(addButton).not.toBeNull();
|
|
721
|
-
expect(addButton?.disabled).toBe(true);
|
|
722
|
-
});
|
|
723
|
-
|
|
724
|
-
it("opens the add menu and runs onAddFile when the default item is clicked", async () => {
|
|
725
|
-
const handleAddFile = vi.fn();
|
|
726
|
-
|
|
727
|
-
const { container } = renderWithProvider(
|
|
728
|
-
<CopilotChatInput
|
|
729
|
-
onAddFile={handleAddFile}
|
|
730
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
731
|
-
/>,
|
|
732
|
-
);
|
|
733
|
-
|
|
734
|
-
mockLayoutMetrics(container);
|
|
735
|
-
|
|
736
|
-
const addButton = getAddMenuButton(container);
|
|
737
|
-
expect(addButton).not.toBeNull();
|
|
738
|
-
expect(addButton?.disabled).toBe(false);
|
|
739
|
-
|
|
740
|
-
const user = userEvent.setup();
|
|
741
|
-
await user.click(addButton!);
|
|
742
|
-
|
|
743
|
-
await waitFor(() => {
|
|
744
|
-
expect(addButton?.getAttribute("data-state")).toBe("open");
|
|
745
|
-
});
|
|
746
|
-
|
|
747
|
-
const menuItem = await screen.findByRole("menuitem", {
|
|
748
|
-
name: "Add attachments",
|
|
749
|
-
});
|
|
750
|
-
fireEvent.click(menuItem);
|
|
751
|
-
|
|
752
|
-
expect(handleAddFile).toHaveBeenCalledTimes(1);
|
|
753
|
-
});
|
|
754
|
-
|
|
755
|
-
it("renders additional custom menu items from the tools menu", async () => {
|
|
756
|
-
const handleCustom = vi.fn();
|
|
757
|
-
|
|
758
|
-
const { container } = renderWithProvider(
|
|
759
|
-
<CopilotChatInput
|
|
760
|
-
toolsMenu={[{ label: "Custom action", action: handleCustom }]}
|
|
761
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
762
|
-
/>,
|
|
763
|
-
);
|
|
764
|
-
|
|
765
|
-
mockLayoutMetrics(container);
|
|
766
|
-
|
|
767
|
-
const addButton = getAddMenuButton(container);
|
|
768
|
-
expect(addButton).not.toBeNull();
|
|
769
|
-
const user = userEvent.setup();
|
|
770
|
-
await user.click(addButton!);
|
|
771
|
-
|
|
772
|
-
await waitFor(() => {
|
|
773
|
-
expect(addButton?.getAttribute("data-state")).toBe("open");
|
|
774
|
-
});
|
|
775
|
-
|
|
776
|
-
const menuItem = await screen.findByRole("menuitem", {
|
|
777
|
-
name: "Custom action",
|
|
778
|
-
});
|
|
779
|
-
fireEvent.click(menuItem);
|
|
780
|
-
|
|
781
|
-
expect(handleCustom).toHaveBeenCalledTimes(1);
|
|
782
|
-
});
|
|
783
|
-
|
|
784
|
-
// Controlled component tests
|
|
785
|
-
describe("Controlled component behavior", () => {
|
|
786
|
-
it("displays the provided value prop", () => {
|
|
787
|
-
const mockOnChange = vi.fn();
|
|
788
|
-
const mockOnSubmitMessage = vi.fn();
|
|
789
|
-
|
|
790
|
-
renderWithProvider(
|
|
791
|
-
<CopilotChatInput
|
|
792
|
-
value="test value"
|
|
793
|
-
onChange={mockOnChange}
|
|
794
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
795
|
-
/>,
|
|
796
|
-
);
|
|
797
|
-
|
|
798
|
-
const input = screen.getByRole("textbox");
|
|
799
|
-
expect((input as HTMLTextAreaElement).value).toBe("test value");
|
|
800
|
-
});
|
|
801
|
-
|
|
802
|
-
it("calls onChange when user types", () => {
|
|
803
|
-
const mockOnChange = vi.fn();
|
|
804
|
-
const mockOnSubmitMessage = vi.fn();
|
|
805
|
-
|
|
806
|
-
renderWithProvider(
|
|
807
|
-
<CopilotChatInput
|
|
808
|
-
value=""
|
|
809
|
-
onChange={mockOnChange}
|
|
810
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
811
|
-
/>,
|
|
812
|
-
);
|
|
813
|
-
|
|
814
|
-
const input = screen.getByRole("textbox");
|
|
815
|
-
fireEvent.change(input, { target: { value: "new text" } });
|
|
816
|
-
|
|
817
|
-
expect(mockOnChange).toHaveBeenCalledWith("new text");
|
|
818
|
-
});
|
|
819
|
-
|
|
820
|
-
it("calls onSubmitMessage when form is submitted", () => {
|
|
821
|
-
const mockOnChange = vi.fn();
|
|
822
|
-
const mockOnSubmitMessage = vi.fn();
|
|
823
|
-
|
|
824
|
-
renderWithProvider(
|
|
825
|
-
<CopilotChatInput
|
|
826
|
-
value="hello world"
|
|
827
|
-
onChange={mockOnChange}
|
|
828
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
829
|
-
/>,
|
|
830
|
-
);
|
|
831
|
-
|
|
832
|
-
const input = screen.getByRole("textbox");
|
|
833
|
-
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
834
|
-
|
|
835
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello world");
|
|
836
|
-
});
|
|
837
|
-
|
|
838
|
-
it("calls onSubmitMessage when send button is clicked", () => {
|
|
839
|
-
const mockOnChange = vi.fn();
|
|
840
|
-
const mockOnSubmitMessage = vi.fn();
|
|
841
|
-
|
|
842
|
-
const { container } = renderWithProvider(
|
|
843
|
-
<CopilotChatInput
|
|
844
|
-
value="test message"
|
|
845
|
-
onChange={mockOnChange}
|
|
846
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
847
|
-
/>,
|
|
848
|
-
);
|
|
849
|
-
|
|
850
|
-
const sendButton = getSendButton(container);
|
|
851
|
-
expect(sendButton).not.toBeNull();
|
|
852
|
-
fireEvent.click(sendButton!);
|
|
853
|
-
|
|
854
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("test message");
|
|
855
|
-
});
|
|
856
|
-
|
|
857
|
-
it("trims whitespace when submitting", () => {
|
|
858
|
-
const mockOnChange = vi.fn();
|
|
859
|
-
const mockOnSubmitMessage = vi.fn();
|
|
860
|
-
|
|
861
|
-
renderWithProvider(
|
|
862
|
-
<CopilotChatInput
|
|
863
|
-
value=" hello world "
|
|
864
|
-
onChange={mockOnChange}
|
|
865
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
866
|
-
/>,
|
|
867
|
-
);
|
|
868
|
-
|
|
869
|
-
const input = screen.getByRole("textbox");
|
|
870
|
-
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
871
|
-
|
|
872
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello world");
|
|
873
|
-
});
|
|
874
|
-
|
|
875
|
-
it("does not submit empty or whitespace-only messages", () => {
|
|
876
|
-
const mockOnChange = vi.fn();
|
|
877
|
-
const mockOnSubmitMessage = vi.fn();
|
|
878
|
-
|
|
879
|
-
const { container } = renderWithProvider(
|
|
880
|
-
<CopilotChatInput
|
|
881
|
-
value=" "
|
|
882
|
-
onChange={mockOnChange}
|
|
883
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
884
|
-
/>,
|
|
885
|
-
);
|
|
886
|
-
|
|
887
|
-
const sendButton = getSendButton(container);
|
|
888
|
-
expect(sendButton).not.toBeNull();
|
|
889
|
-
fireEvent.click(sendButton!);
|
|
890
|
-
|
|
891
|
-
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
892
|
-
});
|
|
893
|
-
|
|
894
|
-
it("disables send button when onSubmitMessage is not provided", () => {
|
|
895
|
-
const mockOnChange = vi.fn();
|
|
896
|
-
|
|
897
|
-
const { container } = renderWithProvider(
|
|
898
|
-
<CopilotChatInput value="some text" onChange={mockOnChange} />,
|
|
899
|
-
);
|
|
900
|
-
|
|
901
|
-
const sendButton = getSendButton(container);
|
|
902
|
-
expect(sendButton?.disabled).toBe(true);
|
|
903
|
-
});
|
|
904
|
-
|
|
905
|
-
it("disables send button when value is empty", () => {
|
|
906
|
-
const mockOnChange = vi.fn();
|
|
907
|
-
const mockOnSubmitMessage = vi.fn();
|
|
908
|
-
|
|
909
|
-
const { container } = renderWithProvider(
|
|
910
|
-
<CopilotChatInput
|
|
911
|
-
value=""
|
|
912
|
-
onChange={mockOnChange}
|
|
913
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
914
|
-
/>,
|
|
915
|
-
);
|
|
916
|
-
|
|
917
|
-
const sendButton = getSendButton(container);
|
|
918
|
-
expect(sendButton?.disabled).toBe(true);
|
|
919
|
-
});
|
|
920
|
-
|
|
921
|
-
it("enables send button when value has content and onSubmitMessage is provided", () => {
|
|
922
|
-
const mockOnChange = vi.fn();
|
|
923
|
-
const mockOnSubmitMessage = vi.fn();
|
|
924
|
-
|
|
925
|
-
const { container } = renderWithProvider(
|
|
926
|
-
<CopilotChatInput
|
|
927
|
-
value="hello"
|
|
928
|
-
onChange={mockOnChange}
|
|
929
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
930
|
-
/>,
|
|
931
|
-
);
|
|
932
|
-
|
|
933
|
-
const sendButton = getSendButton(container);
|
|
934
|
-
expect(sendButton?.disabled).toBe(false);
|
|
935
|
-
});
|
|
936
|
-
|
|
937
|
-
it("works as a fully controlled component", () => {
|
|
938
|
-
const mockOnChange = vi.fn();
|
|
939
|
-
const mockOnSubmitMessage = vi.fn();
|
|
940
|
-
|
|
941
|
-
const { rerender } = renderWithProvider(
|
|
942
|
-
<CopilotChatInput
|
|
943
|
-
value="initial"
|
|
944
|
-
onChange={mockOnChange}
|
|
945
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
946
|
-
/>,
|
|
947
|
-
);
|
|
948
|
-
|
|
949
|
-
const input = screen.getByRole("textbox");
|
|
950
|
-
expect((input as HTMLTextAreaElement).value).toBe("initial");
|
|
951
|
-
|
|
952
|
-
// Simulate parent component updating the value
|
|
953
|
-
rerender(
|
|
954
|
-
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
955
|
-
<CopilotChatInput
|
|
956
|
-
value="updated"
|
|
957
|
-
onChange={mockOnChange}
|
|
958
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
959
|
-
/>
|
|
960
|
-
</CopilotChatConfigurationProvider>,
|
|
961
|
-
);
|
|
962
|
-
|
|
963
|
-
expect((input as HTMLTextAreaElement).value).toBe("updated");
|
|
964
|
-
});
|
|
965
|
-
|
|
966
|
-
it("does not clear input after submission when controlled", () => {
|
|
967
|
-
const mockOnChange = vi.fn();
|
|
968
|
-
const mockOnSubmitMessage = vi.fn();
|
|
969
|
-
|
|
970
|
-
const { container } = renderWithProvider(
|
|
971
|
-
<CopilotChatInput
|
|
972
|
-
value="test message"
|
|
973
|
-
onChange={mockOnChange}
|
|
974
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
975
|
-
/>,
|
|
976
|
-
);
|
|
977
|
-
|
|
978
|
-
const input = screen.getByRole("textbox");
|
|
979
|
-
const sendButton = getSendButton(container);
|
|
980
|
-
|
|
981
|
-
fireEvent.click(sendButton!);
|
|
982
|
-
|
|
983
|
-
// In controlled mode, the component should not clear the input
|
|
984
|
-
// It's up to the parent to manage the value
|
|
985
|
-
expect((input as HTMLTextAreaElement).value).toBe("test message");
|
|
986
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("test message");
|
|
987
|
-
});
|
|
988
|
-
|
|
989
|
-
it("calls onChange with empty string after submission in controlled mode", () => {
|
|
990
|
-
const mockOnChange = vi.fn();
|
|
991
|
-
const mockOnSubmitMessage = vi.fn();
|
|
992
|
-
|
|
993
|
-
const { container } = renderWithProvider(
|
|
994
|
-
<CopilotChatInput
|
|
995
|
-
value="test message"
|
|
996
|
-
onChange={mockOnChange}
|
|
997
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
998
|
-
/>,
|
|
999
|
-
);
|
|
1000
|
-
|
|
1001
|
-
const sendButton = getSendButton(container);
|
|
1002
|
-
fireEvent.click(sendButton!);
|
|
1003
|
-
|
|
1004
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("test message");
|
|
1005
|
-
expect(mockOnChange).toHaveBeenCalledWith("");
|
|
1006
|
-
});
|
|
1007
|
-
|
|
1008
|
-
it("calls onChange with empty string after Enter submission in controlled mode", () => {
|
|
1009
|
-
const mockOnChange = vi.fn();
|
|
1010
|
-
const mockOnSubmitMessage = vi.fn();
|
|
1011
|
-
|
|
1012
|
-
renderWithProvider(
|
|
1013
|
-
<CopilotChatInput
|
|
1014
|
-
value="hello world"
|
|
1015
|
-
onChange={mockOnChange}
|
|
1016
|
-
onSubmitMessage={mockOnSubmitMessage}
|
|
1017
|
-
/>,
|
|
1018
|
-
);
|
|
1019
|
-
|
|
1020
|
-
const input = screen.getByRole("textbox");
|
|
1021
|
-
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
1022
|
-
|
|
1023
|
-
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello world");
|
|
1024
|
-
expect(mockOnChange).toHaveBeenCalledWith("");
|
|
1025
|
-
});
|
|
1026
|
-
});
|
|
1027
|
-
|
|
1028
|
-
describe("Container dimension cache", () => {
|
|
1029
|
-
const OriginalResizeObserver = globalThis.ResizeObserver;
|
|
1030
|
-
|
|
1031
|
-
class MockResizeObserver {
|
|
1032
|
-
static instances: MockResizeObserver[] = [];
|
|
1033
|
-
callback: (entries: Array<{ target: Element }>) => void;
|
|
1034
|
-
observedTargets = new Set<Element>();
|
|
1035
|
-
|
|
1036
|
-
constructor(cb: (entries: Array<{ target: Element }>) => void) {
|
|
1037
|
-
this.callback = cb;
|
|
1038
|
-
MockResizeObserver.instances.push(this);
|
|
1039
|
-
}
|
|
1040
|
-
|
|
1041
|
-
observe = vi.fn((target: Element) => {
|
|
1042
|
-
this.observedTargets.add(target);
|
|
1043
|
-
});
|
|
1044
|
-
|
|
1045
|
-
unobserve = vi.fn((target: Element) => {
|
|
1046
|
-
this.observedTargets.delete(target);
|
|
1047
|
-
});
|
|
1048
|
-
|
|
1049
|
-
disconnect = vi.fn(() => {
|
|
1050
|
-
this.observedTargets.clear();
|
|
1051
|
-
});
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
|
-
const DEFAULT_LAYOUT_OPTIONS = {
|
|
1055
|
-
gridWidth: 640,
|
|
1056
|
-
addWidth: 48,
|
|
1057
|
-
actionsWidth: 96,
|
|
1058
|
-
gridPadding: 16,
|
|
1059
|
-
columnGap: 8,
|
|
1060
|
-
textareaPadding: 20,
|
|
1061
|
-
} as const;
|
|
1062
|
-
|
|
1063
|
-
/** Trigger all observers with all their observed targets. */
|
|
1064
|
-
const triggerAllResizeObservers = () => {
|
|
1065
|
-
for (const instance of MockResizeObserver.instances) {
|
|
1066
|
-
const entries = [...instance.observedTargets].map((target) => ({
|
|
1067
|
-
target,
|
|
1068
|
-
}));
|
|
1069
|
-
if (entries.length > 0) {
|
|
1070
|
-
instance.callback(entries);
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
};
|
|
1074
|
-
|
|
1075
|
-
/** Trigger observers that watch the given targets, with only those targets as entries. */
|
|
1076
|
-
const triggerResizeForTargets = (...targets: Element[]) => {
|
|
1077
|
-
for (const instance of MockResizeObserver.instances) {
|
|
1078
|
-
const matching = targets.filter((t) => instance.observedTargets.has(t));
|
|
1079
|
-
if (matching.length > 0) {
|
|
1080
|
-
instance.callback(matching.map((t) => ({ target: t })));
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
};
|
|
1084
|
-
|
|
1085
|
-
beforeEach(() => {
|
|
1086
|
-
MockResizeObserver.instances = [];
|
|
1087
|
-
// Double cast required: MockResizeObserver's callback signature uses a
|
|
1088
|
-
// simplified `{ target: Element }` entry instead of the full
|
|
1089
|
-
// ResizeObserverEntry (which includes contentRect, borderBoxSize, etc.)
|
|
1090
|
-
// that the real ResizeObserverCallback demands.
|
|
1091
|
-
globalThis.ResizeObserver =
|
|
1092
|
-
MockResizeObserver as unknown as typeof ResizeObserver;
|
|
1093
|
-
});
|
|
1094
|
-
|
|
1095
|
-
afterEach(() => {
|
|
1096
|
-
vi.restoreAllMocks();
|
|
1097
|
-
globalThis.ResizeObserver = OriginalResizeObserver;
|
|
1098
|
-
});
|
|
1099
|
-
|
|
1100
|
-
/**
|
|
1101
|
-
* Extends mockLayoutMetrics with getComputedStyle mocks so that
|
|
1102
|
-
* updateContainerCache can compute real compactWidth and font values,
|
|
1103
|
-
* exercising the canvas-based text measurement path.
|
|
1104
|
-
*/
|
|
1105
|
-
const mockLayoutMetricsWithComputedStyle = (
|
|
1106
|
-
container: HTMLElement,
|
|
1107
|
-
options?: {
|
|
1108
|
-
gridWidth?: number;
|
|
1109
|
-
addWidth?: number;
|
|
1110
|
-
actionsWidth?: number;
|
|
1111
|
-
gridPadding?: number;
|
|
1112
|
-
columnGap?: number;
|
|
1113
|
-
textareaPadding?: number;
|
|
1114
|
-
font?: string;
|
|
1115
|
-
},
|
|
1116
|
-
) => {
|
|
1117
|
-
mockLayoutMetrics(container, {
|
|
1118
|
-
gridWidth: options?.gridWidth,
|
|
1119
|
-
addWidth: options?.addWidth,
|
|
1120
|
-
actionsWidth: options?.actionsWidth,
|
|
1121
|
-
});
|
|
1122
|
-
|
|
1123
|
-
const {
|
|
1124
|
-
gridPadding = 16,
|
|
1125
|
-
columnGap = 8,
|
|
1126
|
-
textareaPadding = 20,
|
|
1127
|
-
font = "16px sans-serif",
|
|
1128
|
-
} = options ?? {};
|
|
1129
|
-
|
|
1130
|
-
const grid = container.querySelector(
|
|
1131
|
-
"div.cpk\\:grid",
|
|
1132
|
-
) as HTMLElement | null;
|
|
1133
|
-
const textarea = container.querySelector(
|
|
1134
|
-
"textarea",
|
|
1135
|
-
) as HTMLElement | null;
|
|
1136
|
-
|
|
1137
|
-
const originalGetComputedStyle = window.getComputedStyle;
|
|
1138
|
-
// Cast needed: spreading a CSSStyleDeclaration loses the indexed-property
|
|
1139
|
-
// accessors and prototype methods, so the result doesn't satisfy the full
|
|
1140
|
-
// CSSStyleDeclaration interface. Only the properties the SUT reads matter.
|
|
1141
|
-
vi.spyOn(window, "getComputedStyle").mockImplementation((el) => {
|
|
1142
|
-
if (el === grid) {
|
|
1143
|
-
return {
|
|
1144
|
-
...originalGetComputedStyle(el),
|
|
1145
|
-
paddingLeft: `${gridPadding}px`,
|
|
1146
|
-
paddingRight: `${gridPadding}px`,
|
|
1147
|
-
columnGap: `${columnGap}px`,
|
|
1148
|
-
} as CSSStyleDeclaration;
|
|
1149
|
-
}
|
|
1150
|
-
if (el === textarea) {
|
|
1151
|
-
return {
|
|
1152
|
-
...originalGetComputedStyle(el),
|
|
1153
|
-
paddingLeft: `${textareaPadding}px`,
|
|
1154
|
-
paddingRight: `${textareaPadding}px`,
|
|
1155
|
-
paddingTop: "12px",
|
|
1156
|
-
paddingBottom: "12px",
|
|
1157
|
-
font,
|
|
1158
|
-
fontStyle: "normal",
|
|
1159
|
-
fontVariant: "normal",
|
|
1160
|
-
fontWeight: "400",
|
|
1161
|
-
fontSize: "16px",
|
|
1162
|
-
lineHeight: "24px",
|
|
1163
|
-
fontFamily: "sans-serif",
|
|
1164
|
-
} as CSSStyleDeclaration;
|
|
1165
|
-
}
|
|
1166
|
-
return originalGetComputedStyle(el);
|
|
1167
|
-
});
|
|
1168
|
-
};
|
|
1169
|
-
|
|
1170
|
-
/**
|
|
1171
|
-
* Mocks canvas measureText to return a deterministic width per character.
|
|
1172
|
-
*/
|
|
1173
|
-
const mockCanvasMeasureText = (charWidth: number) => {
|
|
1174
|
-
const originalGetContext = HTMLCanvasElement.prototype.getContext;
|
|
1175
|
-
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockImplementation(
|
|
1176
|
-
// Double cast required: getContext has 6+ overloaded signatures
|
|
1177
|
-
// (one per context type) and vitest's mockImplementation cannot
|
|
1178
|
-
// unify them into a single callable type.
|
|
1179
|
-
function (
|
|
1180
|
-
this: HTMLCanvasElement,
|
|
1181
|
-
contextId: string,
|
|
1182
|
-
...args: unknown[]
|
|
1183
|
-
) {
|
|
1184
|
-
if (contextId === "2d") {
|
|
1185
|
-
const ctx = originalGetContext.call(
|
|
1186
|
-
this,
|
|
1187
|
-
"2d",
|
|
1188
|
-
...(args as [unknown]),
|
|
1189
|
-
) as CanvasRenderingContext2D | null;
|
|
1190
|
-
if (ctx) {
|
|
1191
|
-
// Cast needed: TextMetrics has many readonly properties
|
|
1192
|
-
// (actualBoundingBoxAscent, etc.) that are irrelevant to the
|
|
1193
|
-
// SUT — only `width` is read.
|
|
1194
|
-
ctx.measureText = (text: string) =>
|
|
1195
|
-
({ width: text.length * charWidth }) as TextMetrics;
|
|
1196
|
-
}
|
|
1197
|
-
return ctx;
|
|
1198
|
-
}
|
|
1199
|
-
return originalGetContext.call(
|
|
1200
|
-
this,
|
|
1201
|
-
contextId,
|
|
1202
|
-
...(args as [unknown]),
|
|
1203
|
-
);
|
|
1204
|
-
} as unknown as typeof HTMLCanvasElement.prototype.getContext,
|
|
1205
|
-
);
|
|
1206
|
-
};
|
|
1207
|
-
|
|
1208
|
-
/**
|
|
1209
|
-
* Sets up all DOM mocks and invalidates the stale cache from the initial
|
|
1210
|
-
* render by triggering the ResizeObserver callback.
|
|
1211
|
-
*/
|
|
1212
|
-
const setupMocksAndInvalidateCache = (
|
|
1213
|
-
container: HTMLElement,
|
|
1214
|
-
options?: Parameters<typeof mockLayoutMetricsWithComputedStyle>[1],
|
|
1215
|
-
charWidth = 10,
|
|
1216
|
-
) => {
|
|
1217
|
-
mockLayoutMetricsWithComputedStyle(container, options);
|
|
1218
|
-
mockCanvasMeasureText(charWidth);
|
|
1219
|
-
// Invalidate the stale cache populated during the initial render
|
|
1220
|
-
// so the next evaluateLayout call re-measures with our mocked values.
|
|
1221
|
-
triggerAllResizeObservers();
|
|
1222
|
-
};
|
|
1223
|
-
|
|
1224
|
-
/**
|
|
1225
|
-
* Render CopilotChatInput, set up layout mocks, type text, and wait for
|
|
1226
|
-
* the layout to settle. Returns the resulting data-layout attribute value.
|
|
1227
|
-
*/
|
|
1228
|
-
const renderTypeAndExpectLayout = async (
|
|
1229
|
-
text: string,
|
|
1230
|
-
options?: Parameters<typeof mockLayoutMetricsWithComputedStyle>[1],
|
|
1231
|
-
charWidth = 10,
|
|
1232
|
-
) => {
|
|
1233
|
-
const { container } = renderWithProvider(
|
|
1234
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
1235
|
-
);
|
|
1236
|
-
setupMocksAndInvalidateCache(container, options, charWidth);
|
|
1237
|
-
|
|
1238
|
-
const textarea = screen.getByRole("textbox");
|
|
1239
|
-
fireEvent.change(textarea, { target: { value: text } });
|
|
1240
|
-
|
|
1241
|
-
const grid = await waitFor(() => {
|
|
1242
|
-
const g = getLayoutGrid(textarea);
|
|
1243
|
-
expect(g).not.toBeNull();
|
|
1244
|
-
return g;
|
|
1245
|
-
});
|
|
1246
|
-
return {
|
|
1247
|
-
container,
|
|
1248
|
-
textarea,
|
|
1249
|
-
grid,
|
|
1250
|
-
layout: grid.getAttribute("data-layout"),
|
|
1251
|
-
};
|
|
1252
|
-
};
|
|
1253
|
-
|
|
1254
|
-
it("expands layout via canvas text measurement when a single long line exceeds compact width", async () => {
|
|
1255
|
-
// gridWidth=640, gridPadding=16 each side, columnGap=8, addWidth=48, actionsWidth=96
|
|
1256
|
-
// compactWidth = (640 - 32) - 48 - 96 - 16 = 448
|
|
1257
|
-
// compactInnerWidth = 448 - 20 - 20 = 408
|
|
1258
|
-
// With charWidth=10, text of length 50 = width 500, which > 408 → expand
|
|
1259
|
-
const { layout } = await renderTypeAndExpectLayout(
|
|
1260
|
-
"a".repeat(50),
|
|
1261
|
-
DEFAULT_LAYOUT_OPTIONS,
|
|
1262
|
-
);
|
|
1263
|
-
expect(layout).toBe("expanded");
|
|
1264
|
-
});
|
|
1265
|
-
|
|
1266
|
-
it("stays compact when single-line text fits within the cached compact width", async () => {
|
|
1267
|
-
// Width = 10 * 10 = 100, well within compactInnerWidth of 408
|
|
1268
|
-
const { layout } = await renderTypeAndExpectLayout(
|
|
1269
|
-
"a".repeat(10),
|
|
1270
|
-
DEFAULT_LAYOUT_OPTIONS,
|
|
1271
|
-
);
|
|
1272
|
-
expect(layout).toBe("compact");
|
|
1273
|
-
});
|
|
1274
|
-
|
|
1275
|
-
it("re-evaluates layout correctly after container resize invalidates the cache", async () => {
|
|
1276
|
-
// Phase 1: wide container — 30 chars fit in compact
|
|
1277
|
-
const { container, textarea } = await renderTypeAndExpectLayout(
|
|
1278
|
-
"a".repeat(30),
|
|
1279
|
-
DEFAULT_LAYOUT_OPTIONS,
|
|
1280
|
-
);
|
|
1281
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1282
|
-
"compact",
|
|
1283
|
-
);
|
|
1284
|
-
|
|
1285
|
-
// Phase 2: simulate a container resize to a much narrower width.
|
|
1286
|
-
// compactWidth = (300 - 32) - 48 - 96 - 16 = 108
|
|
1287
|
-
// compactInnerWidth = 108 - 20 - 20 = 68
|
|
1288
|
-
// Text width = 30 * 10 = 300 > 68 → should expand
|
|
1289
|
-
vi.restoreAllMocks();
|
|
1290
|
-
mockLayoutMetricsWithComputedStyle(container, {
|
|
1291
|
-
...DEFAULT_LAYOUT_OPTIONS,
|
|
1292
|
-
gridWidth: 300,
|
|
1293
|
-
});
|
|
1294
|
-
mockCanvasMeasureText(10);
|
|
1295
|
-
|
|
1296
|
-
// Trigger resize to invalidate the cache with the new narrow dimensions
|
|
1297
|
-
triggerAllResizeObservers();
|
|
1298
|
-
|
|
1299
|
-
// Trigger re-evaluation
|
|
1300
|
-
fireEvent.change(textarea, {
|
|
1301
|
-
target: { value: "a".repeat(30) + " " },
|
|
1302
|
-
});
|
|
1303
|
-
|
|
1304
|
-
await waitFor(() => {
|
|
1305
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1306
|
-
"expanded",
|
|
1307
|
-
);
|
|
1308
|
-
});
|
|
1309
|
-
});
|
|
1310
|
-
|
|
1311
|
-
it("stays compact when textarea font cannot be resolved (empty font fallback)", async () => {
|
|
1312
|
-
const { container } = renderWithProvider(
|
|
1313
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
1314
|
-
);
|
|
1315
|
-
|
|
1316
|
-
mockLayoutMetrics(container, {
|
|
1317
|
-
gridWidth: DEFAULT_LAYOUT_OPTIONS.gridWidth,
|
|
1318
|
-
addWidth: DEFAULT_LAYOUT_OPTIONS.addWidth,
|
|
1319
|
-
actionsWidth: DEFAULT_LAYOUT_OPTIONS.actionsWidth,
|
|
1320
|
-
});
|
|
1321
|
-
|
|
1322
|
-
const originalGetComputedStyle = window.getComputedStyle;
|
|
1323
|
-
// Cast needed: see comment in mockLayoutMetricsWithComputedStyle above.
|
|
1324
|
-
vi.spyOn(window, "getComputedStyle").mockImplementation((el) => {
|
|
1325
|
-
return {
|
|
1326
|
-
...originalGetComputedStyle(el),
|
|
1327
|
-
font: "",
|
|
1328
|
-
fontStyle: "",
|
|
1329
|
-
fontVariant: "",
|
|
1330
|
-
fontWeight: "",
|
|
1331
|
-
fontSize: "",
|
|
1332
|
-
lineHeight: "",
|
|
1333
|
-
fontFamily: "",
|
|
1334
|
-
paddingLeft: "16px",
|
|
1335
|
-
paddingRight: "16px",
|
|
1336
|
-
columnGap: "8px",
|
|
1337
|
-
} as CSSStyleDeclaration;
|
|
1338
|
-
});
|
|
1339
|
-
|
|
1340
|
-
// Invalidate cache so it tries to rebuild with the empty font mock
|
|
1341
|
-
triggerAllResizeObservers();
|
|
1342
|
-
|
|
1343
|
-
const textarea = screen.getByRole("textbox");
|
|
1344
|
-
|
|
1345
|
-
// Single-line text (under 50-char mock wrap threshold) that would exceed
|
|
1346
|
-
// compact width via canvas measurement, but font is empty → skipped → compact
|
|
1347
|
-
fireEvent.change(textarea, {
|
|
1348
|
-
target: { value: "a".repeat(45) },
|
|
1349
|
-
});
|
|
1350
|
-
|
|
1351
|
-
await waitFor(() => {
|
|
1352
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1353
|
-
"compact",
|
|
1354
|
-
);
|
|
1355
|
-
});
|
|
1356
|
-
});
|
|
1357
|
-
|
|
1358
|
-
it("stays compact when canvas.getContext returns null (no text-width measurement)", async () => {
|
|
1359
|
-
const { container } = renderWithProvider(
|
|
1360
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
1361
|
-
);
|
|
1362
|
-
|
|
1363
|
-
setupMocksAndInvalidateCache(container, DEFAULT_LAYOUT_OPTIONS);
|
|
1364
|
-
|
|
1365
|
-
// Override getContext to return null for "2d"
|
|
1366
|
-
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue(null);
|
|
1367
|
-
|
|
1368
|
-
// Invalidate cache again so the next evaluateLayout uses the null context
|
|
1369
|
-
triggerAllResizeObservers();
|
|
1370
|
-
|
|
1371
|
-
const textarea = screen.getByRole("textbox");
|
|
1372
|
-
|
|
1373
|
-
// Single-line text (under 50-char mock wrap threshold) that would exceed
|
|
1374
|
-
// compact width via canvas, but getContext is null → stays compact
|
|
1375
|
-
fireEvent.change(textarea, {
|
|
1376
|
-
target: { value: "a".repeat(45) },
|
|
1377
|
-
});
|
|
1378
|
-
|
|
1379
|
-
await waitFor(() => {
|
|
1380
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1381
|
-
"compact",
|
|
1382
|
-
);
|
|
1383
|
-
});
|
|
1384
|
-
});
|
|
1385
|
-
|
|
1386
|
-
/**
|
|
1387
|
-
* Render, set up all mocks, populate the cache with a short keystroke,
|
|
1388
|
-
* and wait for compact layout. Returns container, textarea, and grid.
|
|
1389
|
-
*/
|
|
1390
|
-
const renderAndWarmCache = async () => {
|
|
1391
|
-
const { container } = renderWithProvider(
|
|
1392
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
1393
|
-
);
|
|
1394
|
-
setupMocksAndInvalidateCache(container, DEFAULT_LAYOUT_OPTIONS);
|
|
1395
|
-
|
|
1396
|
-
const textarea = screen.getByRole("textbox");
|
|
1397
|
-
fireEvent.change(textarea, { target: { value: "a" } });
|
|
1398
|
-
|
|
1399
|
-
await waitFor(() => {
|
|
1400
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1401
|
-
"compact",
|
|
1402
|
-
);
|
|
1403
|
-
});
|
|
1404
|
-
|
|
1405
|
-
return {
|
|
1406
|
-
container,
|
|
1407
|
-
textarea,
|
|
1408
|
-
grid: container.querySelector("div.cpk\\:grid") as HTMLElement,
|
|
1409
|
-
};
|
|
1410
|
-
};
|
|
1411
|
-
|
|
1412
|
-
/**
|
|
1413
|
-
* Install a counting spy on an element's getBoundingClientRect,
|
|
1414
|
-
* delegating to the original implementation. Returns the spy.
|
|
1415
|
-
*/
|
|
1416
|
-
const installBoundingRectSpy = (element: HTMLElement) => {
|
|
1417
|
-
const spy = vi.fn(element.getBoundingClientRect.bind(element));
|
|
1418
|
-
Object.defineProperty(element, "getBoundingClientRect", {
|
|
1419
|
-
value: spy,
|
|
1420
|
-
configurable: true,
|
|
1421
|
-
});
|
|
1422
|
-
return spy;
|
|
1423
|
-
};
|
|
1424
|
-
|
|
1425
|
-
it("does not re-read container dimensions on keystroke when cache is warm", async () => {
|
|
1426
|
-
const { textarea, grid } = await renderAndWarmCache();
|
|
1427
|
-
|
|
1428
|
-
const addRectSpy = installBoundingRectSpy(
|
|
1429
|
-
grid.children[0] as HTMLElement,
|
|
1430
|
-
);
|
|
1431
|
-
const actionsRectSpy = installBoundingRectSpy(
|
|
1432
|
-
grid.children[2] as HTMLElement,
|
|
1433
|
-
);
|
|
1434
|
-
|
|
1435
|
-
// Type more — should NOT call getBoundingClientRect since cache is warm
|
|
1436
|
-
fireEvent.change(textarea, { target: { value: "ab" } });
|
|
1437
|
-
fireEvent.change(textarea, { target: { value: "abc" } });
|
|
1438
|
-
|
|
1439
|
-
await waitFor(() => {
|
|
1440
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1441
|
-
"compact",
|
|
1442
|
-
);
|
|
1443
|
-
});
|
|
1444
|
-
|
|
1445
|
-
expect(addRectSpy).not.toHaveBeenCalled();
|
|
1446
|
-
expect(actionsRectSpy).not.toHaveBeenCalled();
|
|
1447
|
-
});
|
|
1448
|
-
|
|
1449
|
-
it("does not invalidate cache when only the textarea resizes", async () => {
|
|
1450
|
-
const { textarea, grid } = await renderAndWarmCache();
|
|
1451
|
-
|
|
1452
|
-
const addRectSpy = installBoundingRectSpy(
|
|
1453
|
-
grid.children[0] as HTMLElement,
|
|
1454
|
-
);
|
|
1455
|
-
|
|
1456
|
-
// Trigger textarea-only resize — should NOT invalidate cache
|
|
1457
|
-
triggerResizeForTargets(textarea);
|
|
1458
|
-
fireEvent.change(textarea, { target: { value: "ab" } });
|
|
1459
|
-
|
|
1460
|
-
await waitFor(() => {
|
|
1461
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1462
|
-
"compact",
|
|
1463
|
-
);
|
|
1464
|
-
});
|
|
1465
|
-
|
|
1466
|
-
expect(addRectSpy).not.toHaveBeenCalled();
|
|
1467
|
-
});
|
|
1468
|
-
|
|
1469
|
-
it("invalidates cache when container targets resize", async () => {
|
|
1470
|
-
const { textarea, grid } = await renderAndWarmCache();
|
|
1471
|
-
|
|
1472
|
-
const addRectSpy = installBoundingRectSpy(
|
|
1473
|
-
grid.children[0] as HTMLElement,
|
|
1474
|
-
);
|
|
1475
|
-
|
|
1476
|
-
// Trigger container resize — SHOULD invalidate cache
|
|
1477
|
-
triggerResizeForTargets(grid);
|
|
1478
|
-
fireEvent.change(textarea, { target: { value: "ab" } });
|
|
1479
|
-
|
|
1480
|
-
await waitFor(() => {
|
|
1481
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1482
|
-
"compact",
|
|
1483
|
-
);
|
|
1484
|
-
});
|
|
1485
|
-
|
|
1486
|
-
// Cache was invalidated, so updateContainerCache called getBoundingClientRect
|
|
1487
|
-
expect(addRectSpy).toHaveBeenCalled();
|
|
1488
|
-
});
|
|
1489
|
-
|
|
1490
|
-
it("keeps cache warm during layout toggle (ignoreResizeRef path)", async () => {
|
|
1491
|
-
const { textarea, grid } = await renderAndWarmCache();
|
|
1492
|
-
|
|
1493
|
-
// Trigger expansion — ignoreResizeRef set to true by updateLayout
|
|
1494
|
-
fireEvent.change(textarea, { target: { value: "line1\nline2" } });
|
|
1495
|
-
await waitFor(() => {
|
|
1496
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1497
|
-
"expanded",
|
|
1498
|
-
);
|
|
1499
|
-
});
|
|
1500
|
-
|
|
1501
|
-
// Simulate observer firing from the layout toggle.
|
|
1502
|
-
// Self-inflicted resize: ignoreResizeRef is consumed, cache stays warm.
|
|
1503
|
-
triggerAllResizeObservers();
|
|
1504
|
-
|
|
1505
|
-
// Go back to short text — cache should still be warm from before the toggle.
|
|
1506
|
-
const addRectSpy = installBoundingRectSpy(
|
|
1507
|
-
grid.children[0] as HTMLElement,
|
|
1508
|
-
);
|
|
1509
|
-
|
|
1510
|
-
fireEvent.change(textarea, { target: { value: "short" } });
|
|
1511
|
-
await waitFor(() => {
|
|
1512
|
-
expect(getLayoutGrid(textarea).getAttribute("data-layout")).toBe(
|
|
1513
|
-
"compact",
|
|
1514
|
-
);
|
|
1515
|
-
});
|
|
1516
|
-
|
|
1517
|
-
// Cache was NOT invalidated — no getBoundingClientRect needed
|
|
1518
|
-
expect(addRectSpy).not.toHaveBeenCalled();
|
|
1519
|
-
});
|
|
1520
|
-
});
|
|
1521
|
-
|
|
1522
|
-
describe("Scroll behavior", () => {
|
|
1523
|
-
it("does not call scrollIntoView when the textarea receives focus", async () => {
|
|
1524
|
-
const scrollIntoViewMock = vi.fn();
|
|
1525
|
-
HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;
|
|
1526
|
-
|
|
1527
|
-
renderWithProvider(
|
|
1528
|
-
<CopilotChatInput autoFocus onSubmitMessage={mockOnSubmitMessage} />,
|
|
1529
|
-
);
|
|
1530
|
-
|
|
1531
|
-
const textarea = screen.getByRole("textbox");
|
|
1532
|
-
|
|
1533
|
-
// Trigger focus explicitly (autoFocus also triggers it, but let's be explicit)
|
|
1534
|
-
fireEvent.focus(textarea);
|
|
1535
|
-
|
|
1536
|
-
// Wait long enough for the 300ms setTimeout inside the focus handler
|
|
1537
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1538
|
-
|
|
1539
|
-
expect(scrollIntoViewMock).not.toHaveBeenCalled();
|
|
1540
|
-
|
|
1541
|
-
// Clean up
|
|
1542
|
-
delete (HTMLElement.prototype as any).scrollIntoView;
|
|
1543
|
-
});
|
|
1544
|
-
|
|
1545
|
-
it("does not auto-focus the textarea by default", () => {
|
|
1546
|
-
const focusSpy = vi.spyOn(HTMLElement.prototype, "focus");
|
|
1547
|
-
|
|
1548
|
-
renderWithProvider(
|
|
1549
|
-
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
1550
|
-
);
|
|
1551
|
-
|
|
1552
|
-
expect(focusSpy).not.toHaveBeenCalled();
|
|
1553
|
-
focusSpy.mockRestore();
|
|
1554
|
-
});
|
|
1555
|
-
|
|
1556
|
-
it("auto-focuses with preventScroll when autoFocus is true", () => {
|
|
1557
|
-
const focusSpy = vi.spyOn(HTMLElement.prototype, "focus");
|
|
1558
|
-
|
|
1559
|
-
renderWithProvider(
|
|
1560
|
-
<CopilotChatInput autoFocus onSubmitMessage={mockOnSubmitMessage} />,
|
|
1561
|
-
);
|
|
1562
|
-
|
|
1563
|
-
expect(focusSpy).toHaveBeenCalledWith({ preventScroll: true });
|
|
1564
|
-
focusSpy.mockRestore();
|
|
1565
|
-
});
|
|
1566
|
-
});
|
|
1567
|
-
});
|