@copilotkit/react-core 1.54.1 → 1.55.0-next.8

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.
Files changed (183) hide show
  1. package/CHANGELOG.md +127 -116
  2. package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
  3. package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
  4. package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
  5. package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
  6. package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
  7. package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
  8. package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
  9. package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
  10. package/dist/index.cjs +27 -28
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +3 -3
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.mts +3 -3
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +4 -5
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/index.umd.js +1941 -35
  19. package/dist/index.umd.js.map +1 -1
  20. package/dist/v2/index.cjs +77 -7
  21. package/dist/v2/index.css +1 -2
  22. package/dist/v2/index.d.cts +6 -4
  23. package/dist/v2/index.d.mts +6 -4
  24. package/dist/v2/index.mjs +7 -4
  25. package/dist/v2/index.umd.js +5725 -24
  26. package/dist/v2/index.umd.js.map +1 -1
  27. package/package.json +37 -9
  28. package/scripts/scope-preflight.mjs +101 -0
  29. package/src/components/CopilotListeners.tsx +2 -6
  30. package/src/components/copilot-provider/copilot-messages.tsx +1 -1
  31. package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
  32. package/src/components/copilot-provider/copilotkit.tsx +4 -4
  33. package/src/context/copilot-messages-context.tsx +1 -1
  34. package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
  35. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
  36. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
  37. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
  38. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
  39. package/src/hooks/use-agent-nodename.ts +1 -1
  40. package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
  41. package/src/hooks/use-coagent.ts +1 -1
  42. package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
  43. package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
  44. package/src/hooks/use-copilot-chat_internal.ts +2 -2
  45. package/src/hooks/use-copilot-readable.ts +1 -1
  46. package/src/hooks/use-frontend-tool.ts +2 -2
  47. package/src/hooks/use-human-in-the-loop.ts +2 -2
  48. package/src/hooks/use-langgraph-interrupt.ts +2 -5
  49. package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
  50. package/src/hooks/use-render-tool-call.ts +1 -1
  51. package/src/lib/copilot-task.ts +1 -1
  52. package/src/setupTests.ts +18 -14
  53. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
  54. package/src/v2/__tests__/globalSetup.ts +14 -0
  55. package/src/v2/__tests__/setup.ts +93 -0
  56. package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
  57. package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
  58. package/src/v2/components/CopilotKitInspector.tsx +50 -0
  59. package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
  60. package/src/v2/components/WildcardToolCallRender.tsx +86 -0
  61. package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
  62. package/src/v2/components/chat/CopilotChat.tsx +431 -0
  63. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
  64. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
  65. package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
  66. package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
  67. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
  68. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
  69. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
  70. package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
  71. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
  72. package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
  73. package/src/v2/components/chat/CopilotChatView.tsx +598 -0
  74. package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
  75. package/src/v2/components/chat/CopilotPopup.tsx +81 -0
  76. package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
  77. package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
  78. package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
  79. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
  80. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
  81. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
  82. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
  83. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
  84. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
  85. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
  86. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
  87. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
  88. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
  89. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
  90. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
  91. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
  92. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
  93. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
  94. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
  95. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
  96. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
  97. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
  98. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
  99. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
  100. package/src/v2/components/chat/__tests__/setup.ts +1 -0
  101. package/src/v2/components/chat/index.ts +79 -0
  102. package/src/v2/components/index.ts +7 -0
  103. package/src/v2/components/license-warning-banner.tsx +198 -0
  104. package/src/v2/components/ui/button.tsx +123 -0
  105. package/src/v2/components/ui/dropdown-menu.tsx +258 -0
  106. package/src/v2/components/ui/tooltip.tsx +60 -0
  107. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
  108. package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
  109. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
  110. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
  111. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
  112. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
  113. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
  114. package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
  115. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
  116. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
  117. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
  118. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
  119. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
  120. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
  121. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
  122. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
  123. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
  124. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
  125. package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
  126. package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
  127. package/src/v2/hooks/index.ts +18 -0
  128. package/src/v2/hooks/use-agent-context.tsx +45 -0
  129. package/src/v2/hooks/use-agent.tsx +155 -0
  130. package/src/v2/hooks/use-component.tsx +89 -0
  131. package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
  132. package/src/v2/hooks/use-default-render-tool.tsx +254 -0
  133. package/src/v2/hooks/use-frontend-tool.tsx +43 -0
  134. package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
  135. package/src/v2/hooks/use-interrupt.tsx +305 -0
  136. package/src/v2/hooks/use-keyboard-height.tsx +67 -0
  137. package/src/v2/hooks/use-render-activity-message.tsx +73 -0
  138. package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
  139. package/src/v2/hooks/use-render-tool-call.tsx +175 -0
  140. package/src/v2/hooks/use-render-tool.tsx +181 -0
  141. package/src/v2/hooks/use-suggestions.tsx +91 -0
  142. package/src/v2/hooks/use-threads.tsx +256 -0
  143. package/src/v2/hooks/useKatexStyles.ts +27 -0
  144. package/src/v2/index.css +1 -1
  145. package/src/v2/index.ts +18 -2
  146. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
  147. package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
  148. package/src/v2/lib/react-core.ts +156 -0
  149. package/src/v2/lib/slots.tsx +143 -0
  150. package/src/v2/lib/transcription-client.ts +184 -0
  151. package/src/v2/lib/utils.ts +8 -0
  152. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
  153. package/src/v2/providers/CopilotKitProvider.tsx +600 -0
  154. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
  155. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
  156. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
  157. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
  158. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
  159. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
  160. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
  161. package/src/v2/providers/index.ts +14 -0
  162. package/src/v2/styles/globals.css +230 -0
  163. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
  164. package/src/v2/types/defineToolCallRenderer.ts +65 -0
  165. package/src/v2/types/frontend-tool.ts +8 -0
  166. package/src/v2/types/human-in-the-loop.ts +33 -0
  167. package/src/v2/types/index.ts +7 -0
  168. package/src/v2/types/interrupt.ts +15 -0
  169. package/src/v2/types/react-activity-message-renderer.ts +27 -0
  170. package/src/v2/types/react-custom-message-renderer.ts +17 -0
  171. package/src/v2/types/react-tool-call-renderer.ts +32 -0
  172. package/tsdown.config.ts +34 -10
  173. package/vitest.config.mjs +4 -3
  174. package/LICENSE +0 -21
  175. package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
  176. package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
  177. package/dist/copilotkit-C94ayZbs.cjs +0 -2161
  178. package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
  179. package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
  180. package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
  181. package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
  182. package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
  183. package/dist/v2/index.css.map +0 -1
@@ -0,0 +1,702 @@
1
+ import React from "react";
2
+ import { render, screen, fireEvent, waitFor } from "@testing-library/react";
3
+ import { vi } from "vitest";
4
+ import { CopilotChatAssistantMessage } from "../CopilotChatAssistantMessage";
5
+ import { CopilotChatConfigurationProvider } from "../../../providers/CopilotChatConfigurationProvider";
6
+ import { CopilotKitProvider } from "../../../providers/CopilotKitProvider";
7
+ import { AssistantMessage } from "@ag-ui/core";
8
+
9
+ // No mocks needed - Vitest handles ES modules natively!
10
+
11
+ // Mock navigator.clipboard
12
+ const mockWriteText = vi.fn();
13
+ Object.assign(navigator, {
14
+ clipboard: {
15
+ writeText: mockWriteText,
16
+ },
17
+ });
18
+
19
+ // Mock callback functions
20
+ const mockOnThumbsUp = vi.fn();
21
+ const mockOnThumbsDown = vi.fn();
22
+ const mockOnReadAloud = vi.fn();
23
+ const mockOnRegenerate = vi.fn();
24
+
25
+ // Helper to render components with context providers
26
+ const TEST_THREAD_ID = "test-thread";
27
+
28
+ const renderWithProvider = (component: React.ReactElement) => {
29
+ return render(
30
+ <CopilotKitProvider>
31
+ <CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
32
+ {component}
33
+ </CopilotChatConfigurationProvider>
34
+ </CopilotKitProvider>,
35
+ );
36
+ };
37
+
38
+ // Clear mocks before each test
39
+ beforeEach(() => {
40
+ mockWriteText.mockClear();
41
+ mockOnThumbsUp.mockClear();
42
+ mockOnThumbsDown.mockClear();
43
+ mockOnReadAloud.mockClear();
44
+ mockOnRegenerate.mockClear();
45
+ });
46
+
47
+ describe("CopilotChatAssistantMessage", () => {
48
+ const basicMessage: AssistantMessage = {
49
+ role: "assistant",
50
+ content: "Hello, this is a test message from the assistant.",
51
+ id: "test-message-1",
52
+ };
53
+
54
+ describe("Basic rendering", () => {
55
+ it("renders with default components and styling", () => {
56
+ renderWithProvider(
57
+ <CopilotChatAssistantMessage message={basicMessage} />,
58
+ );
59
+
60
+ // Check if elements exist (getBy throws if not found, so this is sufficient)
61
+ // Note: Since markdown may not render in test environment, let's check the component structure
62
+ const copyButton = screen.getByRole("button", { name: /copy/i });
63
+ expect(copyButton).toBeDefined();
64
+ });
65
+
66
+ it("renders empty message gracefully", () => {
67
+ const emptyMessage: AssistantMessage = {
68
+ role: "assistant",
69
+ content: "",
70
+ id: "empty-message",
71
+ };
72
+
73
+ renderWithProvider(
74
+ <CopilotChatAssistantMessage message={emptyMessage} />,
75
+ );
76
+
77
+ // Should render the component structure but NOT show toolbar for empty content
78
+ const container = document.querySelector(
79
+ '[data-message-id="empty-message"]',
80
+ );
81
+ expect(container).toBeDefined();
82
+
83
+ // Should NOT have a copy button since there's no content to copy
84
+ expect(screen.queryByRole("button", { name: /copy/i })).toBeNull();
85
+ });
86
+ });
87
+
88
+ describe("Callback functionality", () => {
89
+ it("renders only copy button when no callbacks provided", () => {
90
+ renderWithProvider(
91
+ <CopilotChatAssistantMessage message={basicMessage} />,
92
+ );
93
+
94
+ expect(screen.getByRole("button", { name: /copy/i })).toBeDefined();
95
+ expect(screen.queryByRole("button", { name: /thumbs up/i })).toBeNull();
96
+ expect(screen.queryByRole("button", { name: /thumbs down/i })).toBeNull();
97
+ expect(screen.queryByRole("button", { name: /read aloud/i })).toBeNull();
98
+ expect(screen.queryByRole("button", { name: /regenerate/i })).toBeNull();
99
+ });
100
+
101
+ it("renders all buttons when all callbacks provided", () => {
102
+ renderWithProvider(
103
+ <CopilotChatAssistantMessage
104
+ message={basicMessage}
105
+ onThumbsUp={mockOnThumbsUp}
106
+ onThumbsDown={mockOnThumbsDown}
107
+ onReadAloud={mockOnReadAloud}
108
+ onRegenerate={mockOnRegenerate}
109
+ />,
110
+ );
111
+
112
+ expect(screen.getByRole("button", { name: /copy/i })).toBeDefined();
113
+ expect(
114
+ screen.getByRole("button", { name: /good response/i }),
115
+ ).toBeDefined();
116
+ expect(
117
+ screen.getByRole("button", { name: /bad response/i }),
118
+ ).toBeDefined();
119
+ expect(screen.getByRole("button", { name: /read aloud/i })).toBeDefined();
120
+ expect(screen.getByRole("button", { name: /regenerate/i })).toBeDefined();
121
+ });
122
+
123
+ it("calls copy functionality when copy button clicked", async () => {
124
+ renderWithProvider(
125
+ <CopilotChatAssistantMessage message={basicMessage} />,
126
+ );
127
+
128
+ const copyButton = screen.getByRole("button", { name: /copy/i });
129
+ fireEvent.click(copyButton);
130
+
131
+ await waitFor(() => {
132
+ expect(mockWriteText).toHaveBeenCalledWith(basicMessage.content!);
133
+ });
134
+ });
135
+
136
+ it("calls thumbs up callback when thumbs up button clicked", () => {
137
+ renderWithProvider(
138
+ <CopilotChatAssistantMessage
139
+ message={basicMessage}
140
+ onThumbsUp={mockOnThumbsUp}
141
+ />,
142
+ );
143
+
144
+ const thumbsUpButton = screen.getByRole("button", {
145
+ name: /good response/i,
146
+ });
147
+ fireEvent.click(thumbsUpButton);
148
+
149
+ expect(mockOnThumbsUp).toHaveBeenCalledTimes(1);
150
+ });
151
+
152
+ it("calls thumbs down callback when thumbs down button clicked", () => {
153
+ renderWithProvider(
154
+ <CopilotChatAssistantMessage
155
+ message={basicMessage}
156
+ onThumbsDown={mockOnThumbsDown}
157
+ />,
158
+ );
159
+
160
+ const thumbsDownButton = screen.getByRole("button", {
161
+ name: /bad response/i,
162
+ });
163
+ fireEvent.click(thumbsDownButton);
164
+
165
+ expect(mockOnThumbsDown).toHaveBeenCalledTimes(1);
166
+ });
167
+
168
+ it("calls read aloud callback when read aloud button clicked", () => {
169
+ renderWithProvider(
170
+ <CopilotChatAssistantMessage
171
+ message={basicMessage}
172
+ onReadAloud={mockOnReadAloud}
173
+ />,
174
+ );
175
+
176
+ const readAloudButton = screen.getByRole("button", {
177
+ name: /read aloud/i,
178
+ });
179
+ fireEvent.click(readAloudButton);
180
+
181
+ expect(mockOnReadAloud).toHaveBeenCalledTimes(1);
182
+ });
183
+
184
+ it("calls regenerate callback when regenerate button clicked", () => {
185
+ renderWithProvider(
186
+ <CopilotChatAssistantMessage
187
+ message={basicMessage}
188
+ onRegenerate={mockOnRegenerate}
189
+ />,
190
+ );
191
+
192
+ const regenerateButton = screen.getByRole("button", {
193
+ name: /regenerate/i,
194
+ });
195
+ fireEvent.click(regenerateButton);
196
+
197
+ expect(mockOnRegenerate).toHaveBeenCalledTimes(1);
198
+ });
199
+ });
200
+
201
+ describe("Additional toolbar items", () => {
202
+ it("renders additional toolbar items", () => {
203
+ const additionalItems = (
204
+ <button data-testid="custom-toolbar-item">Custom Action</button>
205
+ );
206
+
207
+ renderWithProvider(
208
+ <CopilotChatAssistantMessage
209
+ message={basicMessage}
210
+ additionalToolbarItems={additionalItems}
211
+ />,
212
+ );
213
+
214
+ expect(screen.getByTestId("custom-toolbar-item")).toBeDefined();
215
+ });
216
+ });
217
+
218
+ describe("Slot functionality - Custom Components", () => {
219
+ it("accepts custom MarkdownRenderer component", () => {
220
+ const CustomMarkdownRenderer = ({ content }: { content: string }) => (
221
+ <div data-testid="custom-markdown">{content.toUpperCase()}</div>
222
+ );
223
+
224
+ renderWithProvider(
225
+ <CopilotChatAssistantMessage
226
+ message={basicMessage}
227
+ markdownRenderer={CustomMarkdownRenderer}
228
+ />,
229
+ );
230
+
231
+ expect(screen.getByTestId("custom-markdown")).toBeDefined();
232
+ expect(
233
+ screen
234
+ .getByTestId("custom-markdown")
235
+ .textContent?.includes(basicMessage.content!.toUpperCase()),
236
+ ).toBe(true);
237
+ });
238
+
239
+ it("accepts custom Toolbar component", () => {
240
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
241
+ const CustomToolbar = ({ children, ...props }: any) => (
242
+ <div data-testid="custom-toolbar" {...props}>
243
+ Custom Toolbar: {children}
244
+ </div>
245
+ );
246
+
247
+ renderWithProvider(
248
+ <CopilotChatAssistantMessage
249
+ message={basicMessage}
250
+ toolbar={CustomToolbar}
251
+ />,
252
+ );
253
+
254
+ expect(screen.getByTestId("custom-toolbar")).toBeDefined();
255
+ expect(
256
+ screen
257
+ .getByTestId("custom-toolbar")
258
+ .textContent?.includes("Custom Toolbar:"),
259
+ ).toBe(true);
260
+ });
261
+
262
+ it("accepts custom CopyButton component", () => {
263
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
264
+ const CustomCopyButton = (props: any) => (
265
+ <button data-testid="custom-copy-button" {...props}>
266
+ Custom Copy
267
+ </button>
268
+ );
269
+
270
+ renderWithProvider(
271
+ <CopilotChatAssistantMessage
272
+ message={basicMessage}
273
+ copyButton={CustomCopyButton}
274
+ />,
275
+ );
276
+
277
+ expect(screen.getByTestId("custom-copy-button")).toBeDefined();
278
+ expect(
279
+ screen
280
+ .getByTestId("custom-copy-button")
281
+ .textContent?.includes("Custom Copy"),
282
+ ).toBe(true);
283
+ });
284
+
285
+ it("accepts custom ThumbsUpButton component", () => {
286
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
287
+ const CustomThumbsUpButton = (props: any) => (
288
+ <button data-testid="custom-thumbs-up" {...props}>
289
+ Custom Like
290
+ </button>
291
+ );
292
+
293
+ renderWithProvider(
294
+ <CopilotChatAssistantMessage
295
+ message={basicMessage}
296
+ onThumbsUp={mockOnThumbsUp}
297
+ thumbsUpButton={CustomThumbsUpButton}
298
+ />,
299
+ );
300
+
301
+ expect(screen.getByTestId("custom-thumbs-up")).toBeDefined();
302
+ expect(
303
+ screen
304
+ .getByTestId("custom-thumbs-up")
305
+ .textContent?.includes("Custom Like"),
306
+ ).toBe(true);
307
+ });
308
+
309
+ it("accepts custom ThumbsDownButton component", () => {
310
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
311
+ const CustomThumbsDownButton = (props: any) => (
312
+ <button data-testid="custom-thumbs-down" {...props}>
313
+ Custom Dislike
314
+ </button>
315
+ );
316
+
317
+ renderWithProvider(
318
+ <CopilotChatAssistantMessage
319
+ message={basicMessage}
320
+ onThumbsDown={mockOnThumbsDown}
321
+ thumbsDownButton={CustomThumbsDownButton}
322
+ />,
323
+ );
324
+
325
+ expect(screen.getByTestId("custom-thumbs-down")).toBeDefined();
326
+ expect(
327
+ screen
328
+ .getByTestId("custom-thumbs-down")
329
+ .textContent?.includes("Custom Dislike"),
330
+ ).toBe(true);
331
+ });
332
+
333
+ it("accepts custom ReadAloudButton component", () => {
334
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
335
+ const CustomReadAloudButton = (props: any) => (
336
+ <button data-testid="custom-read-aloud" {...props}>
337
+ Custom Speak
338
+ </button>
339
+ );
340
+
341
+ renderWithProvider(
342
+ <CopilotChatAssistantMessage
343
+ message={basicMessage}
344
+ onReadAloud={mockOnReadAloud}
345
+ readAloudButton={CustomReadAloudButton}
346
+ />,
347
+ );
348
+
349
+ expect(screen.getByTestId("custom-read-aloud")).toBeDefined();
350
+ expect(
351
+ screen
352
+ .getByTestId("custom-read-aloud")
353
+ .textContent?.includes("Custom Speak"),
354
+ ).toBe(true);
355
+ });
356
+
357
+ it("accepts custom RegenerateButton component", () => {
358
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
359
+ const CustomRegenerateButton = (props: any) => (
360
+ <button data-testid="custom-regenerate" {...props}>
361
+ Custom Retry
362
+ </button>
363
+ );
364
+
365
+ renderWithProvider(
366
+ <CopilotChatAssistantMessage
367
+ message={basicMessage}
368
+ onRegenerate={mockOnRegenerate}
369
+ regenerateButton={CustomRegenerateButton}
370
+ />,
371
+ );
372
+
373
+ expect(screen.getByTestId("custom-regenerate")).toBeDefined();
374
+ expect(
375
+ screen
376
+ .getByTestId("custom-regenerate")
377
+ .textContent?.includes("Custom Retry"),
378
+ ).toBe(true);
379
+ });
380
+ });
381
+
382
+ describe("Slot functionality - Custom Classes", () => {
383
+ it("applies custom className to component", () => {
384
+ const { container } = renderWithProvider(
385
+ <CopilotChatAssistantMessage
386
+ message={basicMessage}
387
+ className="custom-container-class"
388
+ />,
389
+ );
390
+
391
+ const containerElement = container.querySelector(
392
+ ".custom-container-class",
393
+ );
394
+ expect(containerElement).toBeDefined();
395
+ });
396
+
397
+ it("applies custom className to MarkdownRenderer slot", () => {
398
+ const { container } = renderWithProvider(
399
+ <CopilotChatAssistantMessage
400
+ message={basicMessage}
401
+ markdownRenderer="custom-markdown-class"
402
+ />,
403
+ );
404
+
405
+ const markdownElement = container.querySelector(".custom-markdown-class");
406
+ expect(markdownElement).toBeDefined();
407
+ });
408
+
409
+ it("applies custom className to Toolbar slot", () => {
410
+ const { container } = renderWithProvider(
411
+ <CopilotChatAssistantMessage
412
+ message={basicMessage}
413
+ toolbar="custom-toolbar-class"
414
+ />,
415
+ );
416
+
417
+ const toolbarElement = container.querySelector(".custom-toolbar-class");
418
+ expect(toolbarElement).toBeDefined();
419
+ });
420
+
421
+ it("applies custom className to CopyButton slot", () => {
422
+ const { container } = renderWithProvider(
423
+ <CopilotChatAssistantMessage
424
+ message={basicMessage}
425
+ copyButton="custom-copy-button-class"
426
+ />,
427
+ );
428
+
429
+ const copyButtonElement = container.querySelector(
430
+ ".custom-copy-button-class",
431
+ );
432
+ expect(copyButtonElement).toBeDefined();
433
+ });
434
+ });
435
+
436
+ describe("Children render prop functionality", () => {
437
+ it("supports custom layout via children render prop", () => {
438
+ renderWithProvider(
439
+ <CopilotChatAssistantMessage message={basicMessage}>
440
+ {({
441
+ markdownRenderer: MarkdownRenderer,
442
+ toolbar: Toolbar,
443
+ message,
444
+ }) => (
445
+ <div data-testid="custom-layout">
446
+ <h2>Custom Layout for: {message.id}</h2>
447
+ {MarkdownRenderer}
448
+ <div data-testid="custom-toolbar-wrapper">{Toolbar}</div>
449
+ </div>
450
+ )}
451
+ </CopilotChatAssistantMessage>,
452
+ );
453
+
454
+ expect(screen.getByTestId("custom-layout")).toBeDefined();
455
+ expect(
456
+ screen.getByText(`Custom Layout for: ${basicMessage.id}`),
457
+ ).toBeDefined();
458
+ expect(screen.getByTestId("custom-toolbar-wrapper")).toBeDefined();
459
+ // Note: Markdown content may not render in test environment, check toolbar instead
460
+ expect(screen.getByTestId("custom-toolbar-wrapper")).toBeDefined();
461
+ });
462
+
463
+ it("provides all slot components to children render prop", () => {
464
+ renderWithProvider(
465
+ <CopilotChatAssistantMessage
466
+ message={basicMessage}
467
+ onThumbsUp={mockOnThumbsUp}
468
+ onThumbsDown={mockOnThumbsDown}
469
+ onReadAloud={mockOnReadAloud}
470
+ onRegenerate={mockOnRegenerate}
471
+ >
472
+ {({
473
+ markdownRenderer: MarkdownRenderer,
474
+ toolbar: Toolbar,
475
+ copyButton: CopyButton,
476
+ thumbsUpButton: ThumbsUpButton,
477
+ thumbsDownButton: ThumbsDownButton,
478
+ readAloudButton: ReadAloudButton,
479
+ regenerateButton: RegenerateButton,
480
+ }) => (
481
+ <div data-testid="all-slots-layout">
482
+ {MarkdownRenderer}
483
+ {Toolbar}
484
+ <div data-testid="individual-buttons">
485
+ {CopyButton}
486
+ {ThumbsUpButton}
487
+ {ThumbsDownButton}
488
+ {ReadAloudButton}
489
+ {RegenerateButton}
490
+ </div>
491
+ </div>
492
+ )}
493
+ </CopilotChatAssistantMessage>,
494
+ );
495
+
496
+ expect(screen.getByTestId("all-slots-layout")).toBeDefined();
497
+ expect(screen.getByTestId("individual-buttons")).toBeDefined();
498
+
499
+ // Verify all buttons are rendered
500
+ const buttons = screen.getAllByRole("button");
501
+ expect(buttons.length).toBeGreaterThanOrEqual(5); // At least copy, thumbs up, thumbs down, read aloud, regenerate
502
+ });
503
+
504
+ it("provides callback props to children render prop", () => {
505
+ renderWithProvider(
506
+ <CopilotChatAssistantMessage
507
+ message={basicMessage}
508
+ onThumbsUp={mockOnThumbsUp}
509
+ onThumbsDown={mockOnThumbsDown}
510
+ onReadAloud={mockOnReadAloud}
511
+ onRegenerate={mockOnRegenerate}
512
+ >
513
+ {({ onThumbsUp, onThumbsDown, onReadAloud, onRegenerate }) => (
514
+ <div data-testid="callback-test">
515
+ <button
516
+ onClick={() => onThumbsUp?.(basicMessage)}
517
+ data-testid="custom-thumbs-up"
518
+ >
519
+ Custom Thumbs Up
520
+ </button>
521
+ <button
522
+ onClick={() => onThumbsDown?.(basicMessage)}
523
+ data-testid="custom-thumbs-down"
524
+ >
525
+ Custom Thumbs Down
526
+ </button>
527
+ <button
528
+ onClick={() => onReadAloud?.(basicMessage)}
529
+ data-testid="custom-read-aloud"
530
+ >
531
+ Custom Read Aloud
532
+ </button>
533
+ <button
534
+ onClick={() => onRegenerate?.(basicMessage)}
535
+ data-testid="custom-regenerate"
536
+ >
537
+ Custom Regenerate
538
+ </button>
539
+ </div>
540
+ )}
541
+ </CopilotChatAssistantMessage>,
542
+ );
543
+
544
+ fireEvent.click(screen.getByTestId("custom-thumbs-up"));
545
+ fireEvent.click(screen.getByTestId("custom-thumbs-down"));
546
+ fireEvent.click(screen.getByTestId("custom-read-aloud"));
547
+ fireEvent.click(screen.getByTestId("custom-regenerate"));
548
+
549
+ expect(mockOnThumbsUp).toHaveBeenCalledTimes(1);
550
+ expect(mockOnThumbsDown).toHaveBeenCalledTimes(1);
551
+ expect(mockOnReadAloud).toHaveBeenCalledTimes(1);
552
+ expect(mockOnRegenerate).toHaveBeenCalledTimes(1);
553
+ });
554
+ });
555
+
556
+ describe("Toolbar visibility functionality", () => {
557
+ it("shows toolbar by default (toolbarVisible = true by default)", () => {
558
+ renderWithProvider(
559
+ <CopilotChatAssistantMessage message={basicMessage} />,
560
+ );
561
+
562
+ expect(screen.getByRole("button", { name: /copy/i })).toBeDefined();
563
+ });
564
+
565
+ it("shows toolbar when toolbarVisible is explicitly true", () => {
566
+ renderWithProvider(
567
+ <CopilotChatAssistantMessage
568
+ message={basicMessage}
569
+ toolbarVisible={true}
570
+ />,
571
+ );
572
+
573
+ expect(screen.getByRole("button", { name: /copy/i })).toBeDefined();
574
+ });
575
+
576
+ it("hides toolbar when toolbarVisible is false", () => {
577
+ renderWithProvider(
578
+ <CopilotChatAssistantMessage
579
+ message={basicMessage}
580
+ toolbarVisible={false}
581
+ />,
582
+ );
583
+
584
+ expect(screen.queryByRole("button", { name: /copy/i })).toBeNull();
585
+ });
586
+
587
+ it("always passes toolbar and toolbarVisible to children render prop", () => {
588
+ const childrenSpy = vi.fn(() => <div data-testid="children-render" />);
589
+
590
+ renderWithProvider(
591
+ <CopilotChatAssistantMessage
592
+ message={basicMessage}
593
+ toolbarVisible={false}
594
+ >
595
+ {childrenSpy}
596
+ </CopilotChatAssistantMessage>,
597
+ );
598
+
599
+ expect(childrenSpy).toHaveBeenCalledWith(
600
+ expect.objectContaining({
601
+ toolbar: expect.anything(),
602
+ toolbarVisible: false,
603
+ message: basicMessage,
604
+ }),
605
+ );
606
+ expect(screen.getByTestId("children-render")).toBeDefined();
607
+ });
608
+
609
+ it("passes toolbarVisible true to children render prop by default", () => {
610
+ const childrenSpy = vi.fn(() => <div data-testid="children-render" />);
611
+
612
+ renderWithProvider(
613
+ <CopilotChatAssistantMessage message={basicMessage}>
614
+ {childrenSpy}
615
+ </CopilotChatAssistantMessage>,
616
+ );
617
+
618
+ expect(childrenSpy).toHaveBeenCalledWith(
619
+ expect.objectContaining({
620
+ toolbar: expect.anything(),
621
+ toolbarVisible: true,
622
+ message: basicMessage,
623
+ }),
624
+ );
625
+ });
626
+
627
+ it("children can use toolbarVisible to conditionally render toolbar", () => {
628
+ renderWithProvider(
629
+ <CopilotChatAssistantMessage
630
+ message={basicMessage}
631
+ toolbarVisible={false}
632
+ >
633
+ {({ toolbar, toolbarVisible }) => (
634
+ <div data-testid="custom-layout">
635
+ <div data-testid="content">Custom content</div>
636
+ {toolbarVisible && (
637
+ <div data-testid="conditional-toolbar">{toolbar}</div>
638
+ )}
639
+ {!toolbarVisible && (
640
+ <div data-testid="no-toolbar">No toolbar</div>
641
+ )}
642
+ </div>
643
+ )}
644
+ </CopilotChatAssistantMessage>,
645
+ );
646
+
647
+ expect(screen.getByTestId("custom-layout")).toBeDefined();
648
+ expect(screen.getByTestId("content")).toBeDefined();
649
+ expect(screen.queryByTestId("conditional-toolbar")).toBeNull();
650
+ expect(screen.getByTestId("no-toolbar")).toBeDefined();
651
+ });
652
+ });
653
+
654
+ describe("Error handling", () => {
655
+ it("handles copy errors gracefully", async () => {
656
+ // Mock clipboard to throw an error
657
+ mockWriteText.mockRejectedValueOnce(new Error("Clipboard error"));
658
+
659
+ const consoleSpy = vi
660
+ .spyOn(console, "error")
661
+ .mockImplementation(() => {});
662
+
663
+ renderWithProvider(
664
+ <CopilotChatAssistantMessage message={basicMessage} />,
665
+ );
666
+
667
+ const copyButton = screen.getByRole("button", { name: /copy/i });
668
+ fireEvent.click(copyButton);
669
+
670
+ await waitFor(() => {
671
+ expect(consoleSpy).toHaveBeenCalledWith(
672
+ "Failed to copy message:",
673
+ expect.any(Error),
674
+ );
675
+ });
676
+
677
+ consoleSpy.mockRestore();
678
+ });
679
+
680
+ it("handles null message content gracefully", () => {
681
+ const nullContentMessage: AssistantMessage = {
682
+ role: "assistant",
683
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
684
+ content: null as any,
685
+ id: "null-content",
686
+ };
687
+
688
+ renderWithProvider(
689
+ <CopilotChatAssistantMessage message={nullContentMessage} />,
690
+ );
691
+
692
+ // Should render the component structure but NOT show toolbar for empty content
693
+ const container = document.querySelector(
694
+ '[data-message-id="null-content"]',
695
+ );
696
+ expect(container).toBeDefined();
697
+
698
+ // Should NOT have a copy button since there's no content to copy
699
+ expect(screen.queryByRole("button", { name: /copy/i })).toBeNull();
700
+ });
701
+ });
702
+ });