@copilotkit/react-core 1.54.1 → 1.55.0-next.7
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/CHANGELOG.md +117 -116
- package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
- package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
- package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
- package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
- package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
- package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
- package/dist/index.cjs +27 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -5
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1941 -35
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +77 -7
- package/dist/v2/index.css +1 -2
- package/dist/v2/index.d.cts +6 -4
- package/dist/v2/index.d.mts +6 -4
- package/dist/v2/index.mjs +7 -4
- package/dist/v2/index.umd.js +5725 -24
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +37 -9
- package/scripts/scope-preflight.mjs +101 -0
- package/src/components/CopilotListeners.tsx +2 -6
- package/src/components/copilot-provider/copilot-messages.tsx +1 -1
- package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
- package/src/components/copilot-provider/copilotkit.tsx +4 -4
- package/src/context/copilot-messages-context.tsx +1 -1
- package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
- package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
- package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
- package/src/hooks/use-agent-nodename.ts +1 -1
- package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
- package/src/hooks/use-coagent.ts +1 -1
- package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat_internal.ts +2 -2
- package/src/hooks/use-copilot-readable.ts +1 -1
- package/src/hooks/use-frontend-tool.ts +2 -2
- package/src/hooks/use-human-in-the-loop.ts +2 -2
- package/src/hooks/use-langgraph-interrupt.ts +2 -5
- package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
- package/src/hooks/use-render-tool-call.ts +1 -1
- package/src/lib/copilot-task.ts +1 -1
- package/src/setupTests.ts +18 -14
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
- package/src/v2/__tests__/globalSetup.ts +14 -0
- package/src/v2/__tests__/setup.ts +93 -0
- package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
- package/src/v2/components/CopilotKitInspector.tsx +50 -0
- package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
- package/src/v2/components/WildcardToolCallRender.tsx +86 -0
- package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
- package/src/v2/components/chat/CopilotChat.tsx +431 -0
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
- package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
- package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
- package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
- package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
- package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
- package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
- package/src/v2/components/chat/CopilotChatView.tsx +598 -0
- package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
- package/src/v2/components/chat/CopilotPopup.tsx +81 -0
- package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
- package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
- package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
- package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
- package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
- package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
- package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
- package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
- package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
- package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
- package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
- package/src/v2/components/chat/__tests__/setup.ts +1 -0
- package/src/v2/components/chat/index.ts +79 -0
- package/src/v2/components/index.ts +7 -0
- package/src/v2/components/license-warning-banner.tsx +198 -0
- package/src/v2/components/ui/button.tsx +123 -0
- package/src/v2/components/ui/dropdown-menu.tsx +258 -0
- package/src/v2/components/ui/tooltip.tsx +60 -0
- package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
- package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
- package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
- package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
- package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
- package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
- package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
- package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
- package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
- package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
- package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
- package/src/v2/hooks/index.ts +18 -0
- package/src/v2/hooks/use-agent-context.tsx +45 -0
- package/src/v2/hooks/use-agent.tsx +155 -0
- package/src/v2/hooks/use-component.tsx +89 -0
- package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
- package/src/v2/hooks/use-default-render-tool.tsx +254 -0
- package/src/v2/hooks/use-frontend-tool.tsx +43 -0
- package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
- package/src/v2/hooks/use-interrupt.tsx +305 -0
- package/src/v2/hooks/use-keyboard-height.tsx +67 -0
- package/src/v2/hooks/use-render-activity-message.tsx +73 -0
- package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
- package/src/v2/hooks/use-render-tool-call.tsx +175 -0
- package/src/v2/hooks/use-render-tool.tsx +181 -0
- package/src/v2/hooks/use-suggestions.tsx +91 -0
- package/src/v2/hooks/use-threads.tsx +256 -0
- package/src/v2/hooks/useKatexStyles.ts +27 -0
- package/src/v2/index.css +1 -1
- package/src/v2/index.ts +18 -2
- package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
- package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
- package/src/v2/lib/react-core.ts +156 -0
- package/src/v2/lib/slots.tsx +143 -0
- package/src/v2/lib/transcription-client.ts +184 -0
- package/src/v2/lib/utils.ts +8 -0
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
- package/src/v2/providers/CopilotKitProvider.tsx +600 -0
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
- package/src/v2/providers/index.ts +14 -0
- package/src/v2/styles/globals.css +230 -0
- package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
- package/src/v2/types/defineToolCallRenderer.ts +65 -0
- package/src/v2/types/frontend-tool.ts +8 -0
- package/src/v2/types/human-in-the-loop.ts +33 -0
- package/src/v2/types/index.ts +7 -0
- package/src/v2/types/interrupt.ts +15 -0
- package/src/v2/types/react-activity-message-renderer.ts +27 -0
- package/src/v2/types/react-custom-message-renderer.ts +17 -0
- package/src/v2/types/react-tool-call-renderer.ts +32 -0
- package/tsdown.config.ts +34 -10
- package/vitest.config.mjs +4 -3
- package/LICENSE +0 -21
- package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
- package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
- package/dist/copilotkit-C94ayZbs.cjs +0 -2161
- package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
- package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
- package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
- package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
- package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
- package/dist/v2/index.css.map +0 -1
|
@@ -0,0 +1,986 @@
|
|
|
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 mockLayoutMetrics = (
|
|
39
|
+
container: HTMLElement,
|
|
40
|
+
options?: { gridWidth?: number; addWidth?: number; actionsWidth?: number },
|
|
41
|
+
) => {
|
|
42
|
+
const grid = container.querySelector("div.cpk\\:grid") as HTMLElement | null;
|
|
43
|
+
if (!grid) {
|
|
44
|
+
throw new Error("Grid container not found in CopilotChatInput layout");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { gridWidth = 640, addWidth = 48, actionsWidth = 96 } = options ?? {};
|
|
48
|
+
|
|
49
|
+
Object.defineProperty(grid, "clientWidth", {
|
|
50
|
+
value: gridWidth,
|
|
51
|
+
configurable: true,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const rectFactory = (width: number) => () => ({
|
|
55
|
+
width,
|
|
56
|
+
height: 40,
|
|
57
|
+
top: 0,
|
|
58
|
+
left: 0,
|
|
59
|
+
right: width,
|
|
60
|
+
bottom: 40,
|
|
61
|
+
x: 0,
|
|
62
|
+
y: 0,
|
|
63
|
+
toJSON: () => ({}),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const addContainer = grid.children[0] as HTMLElement;
|
|
67
|
+
const actionsContainer = grid.children[2] as HTMLElement;
|
|
68
|
+
|
|
69
|
+
Object.defineProperty(addContainer, "getBoundingClientRect", {
|
|
70
|
+
value: rectFactory(addWidth),
|
|
71
|
+
configurable: true,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
Object.defineProperty(actionsContainer, "getBoundingClientRect", {
|
|
75
|
+
value: rectFactory(actionsWidth),
|
|
76
|
+
configurable: true,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// Mock scrollHeight for textareas since jsdom doesn't support it properly
|
|
81
|
+
Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", {
|
|
82
|
+
configurable: true,
|
|
83
|
+
get: function (this: HTMLTextAreaElement) {
|
|
84
|
+
const text = this.value || "";
|
|
85
|
+
|
|
86
|
+
// Calculate lines based on explicit newlines
|
|
87
|
+
const explicitLines = text.split("\n").length;
|
|
88
|
+
|
|
89
|
+
// Simulate text wrapping for long lines (rough approximation)
|
|
90
|
+
// Assume ~50 characters per line as a rough width threshold
|
|
91
|
+
let wrappedLines = 0;
|
|
92
|
+
text.split("\n").forEach((line) => {
|
|
93
|
+
const lineWraps = Math.ceil(line.length / 50);
|
|
94
|
+
wrappedLines += Math.max(1, lineWraps);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const totalLines = Math.max(explicitLines, wrappedLines);
|
|
98
|
+
return totalLines * 24; // 24px per line
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Clear mocks before each test
|
|
103
|
+
beforeEach(() => {
|
|
104
|
+
mockOnSubmitMessage.mockClear();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("CopilotChatInput", () => {
|
|
108
|
+
it("renders with default components and styling", () => {
|
|
109
|
+
const mockOnChange = vi.fn();
|
|
110
|
+
const { container } = renderWithProvider(
|
|
111
|
+
<CopilotChatInput
|
|
112
|
+
value=""
|
|
113
|
+
onChange={mockOnChange}
|
|
114
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
115
|
+
/>,
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const input = screen.getByPlaceholderText("Type a message...");
|
|
119
|
+
const sendButton = getSendButton(container);
|
|
120
|
+
|
|
121
|
+
expect(input).toBeDefined();
|
|
122
|
+
expect(sendButton).not.toBeNull();
|
|
123
|
+
expect(sendButton?.disabled).toBe(true); // Should be disabled when input is empty
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("calls onSubmitMessage with trimmed text when Enter is pressed", () => {
|
|
127
|
+
const mockOnChange = vi.fn();
|
|
128
|
+
renderWithProvider(
|
|
129
|
+
<CopilotChatInput
|
|
130
|
+
value=" hello world "
|
|
131
|
+
onChange={mockOnChange}
|
|
132
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
133
|
+
/>,
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const input = screen.getByPlaceholderText("Type a message...");
|
|
137
|
+
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
138
|
+
|
|
139
|
+
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello world");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("calls onSubmitMessage when button is clicked", () => {
|
|
143
|
+
const mockOnChange = vi.fn();
|
|
144
|
+
const { container } = renderWithProvider(
|
|
145
|
+
<CopilotChatInput
|
|
146
|
+
value="test message"
|
|
147
|
+
onChange={mockOnChange}
|
|
148
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
149
|
+
/>,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const sendButton = getSendButton(container);
|
|
153
|
+
expect(sendButton).not.toBeNull();
|
|
154
|
+
fireEvent.click(sendButton!);
|
|
155
|
+
|
|
156
|
+
expect(mockOnSubmitMessage).toHaveBeenCalledWith("test message");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("manages text state internally when uncontrolled", () => {
|
|
160
|
+
const { container } = renderWithProvider(
|
|
161
|
+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
const input = screen.getByPlaceholderText("Type a message...");
|
|
165
|
+
const sendButton = getSendButton(container);
|
|
166
|
+
|
|
167
|
+
fireEvent.change(input, { target: { value: "hello" } });
|
|
168
|
+
fireEvent.click(sendButton!);
|
|
169
|
+
|
|
170
|
+
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello");
|
|
171
|
+
expect((input as HTMLTextAreaElement).value).toBe("");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("does not send when Enter is pressed with Shift key", () => {
|
|
175
|
+
const mockOnChange = vi.fn();
|
|
176
|
+
renderWithProvider(
|
|
177
|
+
<CopilotChatInput
|
|
178
|
+
value="test"
|
|
179
|
+
onChange={mockOnChange}
|
|
180
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
181
|
+
/>,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const input = screen.getByPlaceholderText("Type a message...");
|
|
185
|
+
fireEvent.keyDown(input, { key: "Enter", shiftKey: true });
|
|
186
|
+
|
|
187
|
+
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("does not send empty or whitespace-only messages", () => {
|
|
191
|
+
const mockOnChange = vi.fn();
|
|
192
|
+
|
|
193
|
+
// Test empty string
|
|
194
|
+
const { container, rerender } = renderWithProvider(
|
|
195
|
+
<CopilotChatInput
|
|
196
|
+
value=""
|
|
197
|
+
onChange={mockOnChange}
|
|
198
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
199
|
+
/>,
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
let sendButton = getSendButton(container);
|
|
203
|
+
fireEvent.click(sendButton!);
|
|
204
|
+
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
205
|
+
|
|
206
|
+
// Test whitespace only
|
|
207
|
+
rerender(
|
|
208
|
+
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
209
|
+
<CopilotChatInput
|
|
210
|
+
value=" "
|
|
211
|
+
onChange={mockOnChange}
|
|
212
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
213
|
+
/>
|
|
214
|
+
</CopilotChatConfigurationProvider>,
|
|
215
|
+
);
|
|
216
|
+
sendButton = getSendButton(container);
|
|
217
|
+
fireEvent.click(sendButton!);
|
|
218
|
+
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("keeps input value when no submit handler is provided", () => {
|
|
222
|
+
renderWithProvider(<CopilotChatInput />);
|
|
223
|
+
|
|
224
|
+
const input = screen.getByPlaceholderText("Type a message...");
|
|
225
|
+
|
|
226
|
+
fireEvent.change(input, { target: { value: "draft" } });
|
|
227
|
+
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
228
|
+
|
|
229
|
+
expect((input as HTMLTextAreaElement).value).toBe("draft");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("enables button based on value prop", () => {
|
|
233
|
+
const mockOnChange = vi.fn();
|
|
234
|
+
|
|
235
|
+
// Test with empty value
|
|
236
|
+
const { container, rerender } = renderWithProvider(
|
|
237
|
+
<CopilotChatInput
|
|
238
|
+
value=""
|
|
239
|
+
onChange={mockOnChange}
|
|
240
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
241
|
+
/>,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
let sendButton = getSendButton(container);
|
|
245
|
+
expect(sendButton?.disabled).toBe(true);
|
|
246
|
+
|
|
247
|
+
// Test with non-empty value
|
|
248
|
+
rerender(
|
|
249
|
+
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
250
|
+
<CopilotChatInput
|
|
251
|
+
value="hello"
|
|
252
|
+
onChange={mockOnChange}
|
|
253
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
254
|
+
/>
|
|
255
|
+
</CopilotChatConfigurationProvider>,
|
|
256
|
+
);
|
|
257
|
+
sendButton = getSendButton(container);
|
|
258
|
+
expect(sendButton?.disabled).toBe(false);
|
|
259
|
+
|
|
260
|
+
// Test with empty value again
|
|
261
|
+
rerender(
|
|
262
|
+
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
263
|
+
<CopilotChatInput
|
|
264
|
+
value=""
|
|
265
|
+
onChange={mockOnChange}
|
|
266
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
267
|
+
/>
|
|
268
|
+
</CopilotChatConfigurationProvider>,
|
|
269
|
+
);
|
|
270
|
+
sendButton = getSendButton(container);
|
|
271
|
+
expect(sendButton?.disabled).toBe(true);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("accepts custom slot classes", () => {
|
|
275
|
+
const mockOnChange = vi.fn();
|
|
276
|
+
const { container } = renderWithProvider(
|
|
277
|
+
<CopilotChatInput
|
|
278
|
+
value=""
|
|
279
|
+
onChange={mockOnChange}
|
|
280
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
281
|
+
className="custom-container"
|
|
282
|
+
textArea="custom-textarea"
|
|
283
|
+
sendButton="custom-button"
|
|
284
|
+
/>,
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
const containerDiv = container.firstChild as HTMLElement;
|
|
288
|
+
const input = screen.getByPlaceholderText("Type a message...");
|
|
289
|
+
const sendButton = getSendButton(container);
|
|
290
|
+
|
|
291
|
+
expect(containerDiv.classList.contains("custom-container")).toBe(true);
|
|
292
|
+
expect(input.classList.contains("custom-textarea")).toBe(true);
|
|
293
|
+
expect(sendButton?.classList.contains("custom-button")).toBe(true);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("accepts custom components via slots", () => {
|
|
297
|
+
const mockOnChange = vi.fn();
|
|
298
|
+
const CustomButton = (
|
|
299
|
+
props: React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
300
|
+
) => (
|
|
301
|
+
<button {...props} data-testid="custom-button">
|
|
302
|
+
Send Now
|
|
303
|
+
</button>
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
renderWithProvider(
|
|
307
|
+
<CopilotChatInput
|
|
308
|
+
value=""
|
|
309
|
+
onChange={mockOnChange}
|
|
310
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
311
|
+
sendButton={CustomButton}
|
|
312
|
+
/>,
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
const customButton = screen.getByTestId("custom-button");
|
|
316
|
+
expect(customButton).toBeDefined();
|
|
317
|
+
expect(customButton.textContent?.includes("Send Now")).toBe(true);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("supports custom layout via children render prop", () => {
|
|
321
|
+
const mockOnChange = vi.fn();
|
|
322
|
+
renderWithProvider(
|
|
323
|
+
<CopilotChatInput
|
|
324
|
+
value=""
|
|
325
|
+
onChange={mockOnChange}
|
|
326
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
327
|
+
>
|
|
328
|
+
{({ textArea: TextArea, sendButton: SendButton }) => (
|
|
329
|
+
<div data-testid="custom-layout">
|
|
330
|
+
Custom Layout:
|
|
331
|
+
{SendButton}
|
|
332
|
+
{TextArea}
|
|
333
|
+
</div>
|
|
334
|
+
)}
|
|
335
|
+
</CopilotChatInput>,
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
const customLayout = screen.getByTestId("custom-layout");
|
|
339
|
+
expect(customLayout).toBeDefined();
|
|
340
|
+
expect(customLayout.textContent?.includes("Custom Layout:")).toBe(true);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("passes containerRef to children render prop", () => {
|
|
344
|
+
const ref = React.createRef<HTMLDivElement>();
|
|
345
|
+
renderWithProvider(
|
|
346
|
+
<CopilotChatInput
|
|
347
|
+
value=""
|
|
348
|
+
onChange={vi.fn()}
|
|
349
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
350
|
+
containerRef={ref}
|
|
351
|
+
>
|
|
352
|
+
{({ containerRef }) => (
|
|
353
|
+
<div ref={containerRef} data-testid="custom-container">
|
|
354
|
+
Custom Container
|
|
355
|
+
</div>
|
|
356
|
+
)}
|
|
357
|
+
</CopilotChatInput>,
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
const container = screen.getByTestId("custom-container");
|
|
361
|
+
expect(container).toBeDefined();
|
|
362
|
+
expect(ref.current).toBe(container);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("updates its internal layout data attribute when content expands", async () => {
|
|
366
|
+
renderWithProvider(
|
|
367
|
+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
const textarea = screen.getByRole("textbox");
|
|
371
|
+
const grid = textarea.closest("[data-layout]") as HTMLElement | null;
|
|
372
|
+
expect(grid?.getAttribute("data-layout")).toBe("compact");
|
|
373
|
+
|
|
374
|
+
fireEvent.change(textarea, { target: { value: "line one\nline two" } });
|
|
375
|
+
|
|
376
|
+
await waitFor(() => {
|
|
377
|
+
expect(grid?.getAttribute("data-layout")).toBe("expanded");
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("executes slash commands via keyboard selection", async () => {
|
|
382
|
+
const handleFirst = vi.fn();
|
|
383
|
+
const handleSecond = vi.fn();
|
|
384
|
+
|
|
385
|
+
renderWithProvider(
|
|
386
|
+
<CopilotChatInput
|
|
387
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
388
|
+
toolsMenu={[
|
|
389
|
+
{ label: "Say hi", action: handleFirst },
|
|
390
|
+
{ label: "Open docs", action: handleSecond },
|
|
391
|
+
]}
|
|
392
|
+
/>,
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
const textarea = screen.getByRole("textbox");
|
|
396
|
+
fireEvent.change(textarea, { target: { value: "/" } });
|
|
397
|
+
|
|
398
|
+
const menu = await screen.findByTestId("copilot-slash-menu");
|
|
399
|
+
expect(menu).not.toBeNull();
|
|
400
|
+
expect(screen.queryByText("Say hi")).not.toBeNull();
|
|
401
|
+
expect(screen.queryByText("Open docs")).not.toBeNull();
|
|
402
|
+
|
|
403
|
+
fireEvent.keyDown(textarea, {
|
|
404
|
+
key: "ArrowDown",
|
|
405
|
+
code: "ArrowDown",
|
|
406
|
+
keyCode: 40,
|
|
407
|
+
});
|
|
408
|
+
fireEvent.keyDown(textarea, { key: "Enter", code: "Enter", keyCode: 13 });
|
|
409
|
+
|
|
410
|
+
expect(handleSecond).toHaveBeenCalledTimes(1);
|
|
411
|
+
expect(handleFirst).not.toHaveBeenCalled();
|
|
412
|
+
|
|
413
|
+
await waitFor(() => {
|
|
414
|
+
expect(screen.queryByTestId("copilot-slash-menu")).toBeNull();
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
expect((textarea as HTMLTextAreaElement).value).toBe("");
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("prioritizes prefix matches when filtering slash commands", async () => {
|
|
421
|
+
renderWithProvider(
|
|
422
|
+
<CopilotChatInput
|
|
423
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
424
|
+
toolsMenu={[
|
|
425
|
+
{ label: "Reopen previous chat", action: vi.fn() },
|
|
426
|
+
{ label: "Open CopilotKit", action: vi.fn() },
|
|
427
|
+
{ label: "Help me operate", action: vi.fn() },
|
|
428
|
+
]}
|
|
429
|
+
/>,
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
const textarea = screen.getByRole("textbox");
|
|
433
|
+
fireEvent.change(textarea, { target: { value: "/op" } });
|
|
434
|
+
|
|
435
|
+
const menu = await screen.findByTestId("copilot-slash-menu");
|
|
436
|
+
const options = within(menu).getAllByRole("option");
|
|
437
|
+
|
|
438
|
+
expect(options[0]?.textContent?.includes("Open CopilotKit")).toBe(true);
|
|
439
|
+
expect(options[0]?.getAttribute("aria-selected")).toBe("true");
|
|
440
|
+
|
|
441
|
+
fireEvent.keyDown(textarea, {
|
|
442
|
+
key: "ArrowDown",
|
|
443
|
+
code: "ArrowDown",
|
|
444
|
+
keyCode: 40,
|
|
445
|
+
});
|
|
446
|
+
await waitFor(() => {
|
|
447
|
+
const updated = within(menu).getAllByRole("option");
|
|
448
|
+
expect(updated[1]?.getAttribute("aria-selected")).toBe("true");
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
fireEvent.change(textarea, { target: { value: "/ope" } });
|
|
452
|
+
|
|
453
|
+
await waitFor(() => {
|
|
454
|
+
const updatedOptions = within(menu).getAllByRole("option");
|
|
455
|
+
expect(updatedOptions[0]?.getAttribute("aria-selected")).toBe("true");
|
|
456
|
+
expect(
|
|
457
|
+
updatedOptions[0]?.textContent?.startsWith("Open CopilotKit"),
|
|
458
|
+
).toBe(true);
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("limits slash menu height when commands exceed five items", async () => {
|
|
463
|
+
const tools = Array.from({ length: 6 }, (_, index) => ({
|
|
464
|
+
label: `Command ${index + 1}`,
|
|
465
|
+
action: vi.fn(),
|
|
466
|
+
}));
|
|
467
|
+
|
|
468
|
+
renderWithProvider(
|
|
469
|
+
<CopilotChatInput
|
|
470
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
471
|
+
toolsMenu={tools}
|
|
472
|
+
/>,
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
const textarea = screen.getByRole("textbox");
|
|
476
|
+
fireEvent.change(textarea, { target: { value: "/" } });
|
|
477
|
+
|
|
478
|
+
const menu = await screen.findByTestId("copilot-slash-menu");
|
|
479
|
+
|
|
480
|
+
await waitFor(() => {
|
|
481
|
+
expect(menu.style.maxHeight).toBe("200px");
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
const options = within(menu).getAllByRole("option");
|
|
485
|
+
expect(options.length).toBe(6);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it("allows slash command actions to populate the input", async () => {
|
|
489
|
+
const greeting = "Hello Copilot! 👋 Could you help me with something?";
|
|
490
|
+
const label = "Say hi to CopilotKit";
|
|
491
|
+
|
|
492
|
+
renderWithProvider(
|
|
493
|
+
<CopilotChatInput
|
|
494
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
495
|
+
toolsMenu={[
|
|
496
|
+
{
|
|
497
|
+
label,
|
|
498
|
+
action: () => {
|
|
499
|
+
const textareaElement =
|
|
500
|
+
document.querySelector<HTMLTextAreaElement>("textarea");
|
|
501
|
+
if (!textareaElement) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const nativeSetter = Object.getOwnPropertyDescriptor(
|
|
506
|
+
window.HTMLTextAreaElement.prototype,
|
|
507
|
+
"value",
|
|
508
|
+
)?.set;
|
|
509
|
+
nativeSetter?.call(textareaElement, greeting);
|
|
510
|
+
textareaElement.dispatchEvent(
|
|
511
|
+
new Event("input", { bubbles: true }),
|
|
512
|
+
);
|
|
513
|
+
},
|
|
514
|
+
},
|
|
515
|
+
]}
|
|
516
|
+
/>,
|
|
517
|
+
);
|
|
518
|
+
|
|
519
|
+
const textarea = screen.getByRole("textbox");
|
|
520
|
+
fireEvent.change(textarea, { target: { value: "/" } });
|
|
521
|
+
|
|
522
|
+
const option = await screen.findByRole("option", { name: label });
|
|
523
|
+
fireEvent.mouseDown(option);
|
|
524
|
+
|
|
525
|
+
await waitFor(() => {
|
|
526
|
+
expect((textarea as HTMLTextAreaElement).value).toBe(greeting);
|
|
527
|
+
});
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it("shows cancel and finish buttons in transcribe mode", () => {
|
|
531
|
+
const { container } = renderWithProvider(
|
|
532
|
+
<CopilotChatInput
|
|
533
|
+
mode="transcribe"
|
|
534
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
535
|
+
onStartTranscribe={() => {}}
|
|
536
|
+
onCancelTranscribe={() => {}}
|
|
537
|
+
onFinishTranscribe={() => {}}
|
|
538
|
+
onAddFile={() => {}}
|
|
539
|
+
/>,
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
// Should show cancel button (X icon) - find by svg class
|
|
543
|
+
const cancelIcon = container.querySelector("svg.lucide-x");
|
|
544
|
+
expect(cancelIcon).toBeDefined();
|
|
545
|
+
|
|
546
|
+
// Should show finish button (checkmark icon) - find by svg class
|
|
547
|
+
const finishIcon = container.querySelector("svg.lucide-check");
|
|
548
|
+
expect(finishIcon).toBeDefined();
|
|
549
|
+
|
|
550
|
+
// Should show cancel button (X icon) and finish button (check icon)
|
|
551
|
+
const cancelButton = container.querySelector("svg.lucide-x");
|
|
552
|
+
const finishButton = container.querySelector("svg.lucide-check");
|
|
553
|
+
expect(cancelButton).toBeDefined();
|
|
554
|
+
expect(finishButton).toBeDefined();
|
|
555
|
+
|
|
556
|
+
// Should NOT show transcribe button (mic icon) in transcribe mode
|
|
557
|
+
const transcribeIcon = container.querySelector("svg.lucide-mic");
|
|
558
|
+
expect(transcribeIcon).toBeNull();
|
|
559
|
+
|
|
560
|
+
// Should NOT show send button (arrow-up icon) in transcribe mode
|
|
561
|
+
const sendIcon = container.querySelector("svg.lucide-arrow-up");
|
|
562
|
+
expect(sendIcon).toBeNull();
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("disables add menu button in transcribe mode", () => {
|
|
566
|
+
const { container } = renderWithProvider(
|
|
567
|
+
<CopilotChatInput
|
|
568
|
+
mode="transcribe"
|
|
569
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
570
|
+
onStartTranscribe={() => {}}
|
|
571
|
+
onCancelTranscribe={() => {}}
|
|
572
|
+
onFinishTranscribe={() => {}}
|
|
573
|
+
onAddFile={() => {}}
|
|
574
|
+
toolsMenu={[{ label: "Test Tool", action: () => {} }]}
|
|
575
|
+
/>,
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
// Add button should be disabled (find by Plus icon)
|
|
579
|
+
const addIcon = container.querySelector("svg.lucide-plus");
|
|
580
|
+
const addButton = addIcon?.closest("button") as HTMLButtonElement | null;
|
|
581
|
+
expect(addButton).not.toBeNull();
|
|
582
|
+
expect(addButton?.disabled).toBe(true);
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
it("shows recording indicator instead of textarea in transcribe mode", () => {
|
|
586
|
+
const { container } = renderWithProvider(
|
|
587
|
+
<CopilotChatInput
|
|
588
|
+
mode="transcribe"
|
|
589
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
590
|
+
onStartTranscribe={() => {}}
|
|
591
|
+
onCancelTranscribe={() => {}}
|
|
592
|
+
onFinishTranscribe={() => {}}
|
|
593
|
+
onAddFile={() => {}}
|
|
594
|
+
/>,
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
// Should show recording indicator (canvas element)
|
|
598
|
+
const recordingIndicator = container.querySelector("canvas");
|
|
599
|
+
expect(recordingIndicator).toBeDefined();
|
|
600
|
+
|
|
601
|
+
// Should NOT show textarea in transcribe mode
|
|
602
|
+
expect(screen.queryByRole("textbox")).toBeNull();
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
it("shows textarea in input mode", () => {
|
|
606
|
+
const { container } = renderWithProvider(
|
|
607
|
+
<CopilotChatInput
|
|
608
|
+
mode="input"
|
|
609
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
610
|
+
onStartTranscribe={() => {}}
|
|
611
|
+
onCancelTranscribe={() => {}}
|
|
612
|
+
onFinishTranscribe={() => {}}
|
|
613
|
+
onAddFile={() => {}}
|
|
614
|
+
/>,
|
|
615
|
+
);
|
|
616
|
+
|
|
617
|
+
// Should show textarea in input mode
|
|
618
|
+
expect(screen.getByRole("textbox")).toBeDefined();
|
|
619
|
+
|
|
620
|
+
// Should NOT show recording indicator (red div)
|
|
621
|
+
const recordingIndicator = container.querySelector(".bg-red-500");
|
|
622
|
+
expect(recordingIndicator).toBeNull();
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
it("positions the textarea next to the add menu button when single line", () => {
|
|
626
|
+
renderWithProvider(
|
|
627
|
+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
628
|
+
);
|
|
629
|
+
|
|
630
|
+
const textarea = screen.getByRole("textbox");
|
|
631
|
+
const layoutCell = textarea.parentElement as HTMLElement;
|
|
632
|
+
const gridContainer = layoutCell?.parentElement as HTMLElement;
|
|
633
|
+
|
|
634
|
+
expect(layoutCell.className).toContain("col-start-2");
|
|
635
|
+
expect(layoutCell.className).not.toContain("col-span-3");
|
|
636
|
+
expect(gridContainer.className).toContain("items-center");
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it("toggles textarea padding based on multiline state", async () => {
|
|
640
|
+
const { container } = renderWithProvider(
|
|
641
|
+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
642
|
+
);
|
|
643
|
+
|
|
644
|
+
mockLayoutMetrics(container);
|
|
645
|
+
|
|
646
|
+
const textarea = screen.getByRole("textbox");
|
|
647
|
+
expect(textarea.className).toContain("pr-5");
|
|
648
|
+
expect(textarea.className).not.toContain("px-5");
|
|
649
|
+
|
|
650
|
+
fireEvent.change(textarea, {
|
|
651
|
+
target: {
|
|
652
|
+
value:
|
|
653
|
+
"a very long line that should wrap once it exceeds the width of the input",
|
|
654
|
+
},
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
await waitFor(() => {
|
|
658
|
+
expect(textarea.className).toContain("px-5");
|
|
659
|
+
expect(textarea.className).not.toContain("pr-5");
|
|
660
|
+
});
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
it("returns to the compact layout when text no longer needs extra space", async () => {
|
|
664
|
+
const { container } = renderWithProvider(
|
|
665
|
+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
666
|
+
);
|
|
667
|
+
|
|
668
|
+
mockLayoutMetrics(container);
|
|
669
|
+
|
|
670
|
+
const textarea = screen.getByRole("textbox");
|
|
671
|
+
const layoutCell = textarea.parentElement as HTMLElement;
|
|
672
|
+
|
|
673
|
+
fireEvent.change(textarea, {
|
|
674
|
+
target: {
|
|
675
|
+
value:
|
|
676
|
+
"this is a very long line that should expand the layout before it wraps so we can see the stacked arrangement",
|
|
677
|
+
},
|
|
678
|
+
});
|
|
679
|
+
|
|
680
|
+
await waitFor(() => {
|
|
681
|
+
expect(layoutCell.className).toContain("col-span-3");
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
fireEvent.change(textarea, { target: { value: "short" } });
|
|
685
|
+
|
|
686
|
+
await waitFor(() => {
|
|
687
|
+
expect(layoutCell.className).toContain("col-start-2");
|
|
688
|
+
expect(layoutCell.className).not.toContain("col-span-3");
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it("moves the textarea above the add menu button when multiple lines", async () => {
|
|
693
|
+
renderWithProvider(
|
|
694
|
+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
695
|
+
);
|
|
696
|
+
|
|
697
|
+
const textarea = screen.getByRole("textbox");
|
|
698
|
+
|
|
699
|
+
fireEvent.change(textarea, {
|
|
700
|
+
target: { value: "first line\nsecond line" },
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
await waitFor(() => {
|
|
704
|
+
const layoutCell = textarea.parentElement as HTMLElement;
|
|
705
|
+
expect(layoutCell.className).toContain("col-span-3");
|
|
706
|
+
expect(layoutCell.className).not.toContain("col-start-2");
|
|
707
|
+
});
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
it("disables the add menu button when no menu items are provided", () => {
|
|
711
|
+
const { container } = renderWithProvider(
|
|
712
|
+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
const addButton = getAddMenuButton(container);
|
|
716
|
+
|
|
717
|
+
expect(addButton).not.toBeNull();
|
|
718
|
+
expect(addButton?.disabled).toBe(true);
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
it("opens the add menu and runs onAddFile when the default item is clicked", async () => {
|
|
722
|
+
const handleAddFile = vi.fn();
|
|
723
|
+
|
|
724
|
+
const { container } = renderWithProvider(
|
|
725
|
+
<CopilotChatInput
|
|
726
|
+
onAddFile={handleAddFile}
|
|
727
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
728
|
+
/>,
|
|
729
|
+
);
|
|
730
|
+
|
|
731
|
+
mockLayoutMetrics(container);
|
|
732
|
+
|
|
733
|
+
const addButton = getAddMenuButton(container);
|
|
734
|
+
expect(addButton).not.toBeNull();
|
|
735
|
+
expect(addButton?.disabled).toBe(false);
|
|
736
|
+
|
|
737
|
+
const user = userEvent.setup();
|
|
738
|
+
await user.click(addButton!);
|
|
739
|
+
|
|
740
|
+
await waitFor(() => {
|
|
741
|
+
expect(addButton?.getAttribute("data-state")).toBe("open");
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
const menuItem = await screen.findByRole("menuitem", {
|
|
745
|
+
name: "Add photos or files",
|
|
746
|
+
});
|
|
747
|
+
fireEvent.click(menuItem);
|
|
748
|
+
|
|
749
|
+
expect(handleAddFile).toHaveBeenCalledTimes(1);
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
it("renders additional custom menu items from the tools menu", async () => {
|
|
753
|
+
const handleCustom = vi.fn();
|
|
754
|
+
|
|
755
|
+
const { container } = renderWithProvider(
|
|
756
|
+
<CopilotChatInput
|
|
757
|
+
toolsMenu={[{ label: "Custom action", action: handleCustom }]}
|
|
758
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
759
|
+
/>,
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
mockLayoutMetrics(container);
|
|
763
|
+
|
|
764
|
+
const addButton = getAddMenuButton(container);
|
|
765
|
+
expect(addButton).not.toBeNull();
|
|
766
|
+
const user = userEvent.setup();
|
|
767
|
+
await user.click(addButton!);
|
|
768
|
+
|
|
769
|
+
await waitFor(() => {
|
|
770
|
+
expect(addButton?.getAttribute("data-state")).toBe("open");
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
const menuItem = await screen.findByRole("menuitem", {
|
|
774
|
+
name: "Custom action",
|
|
775
|
+
});
|
|
776
|
+
fireEvent.click(menuItem);
|
|
777
|
+
|
|
778
|
+
expect(handleCustom).toHaveBeenCalledTimes(1);
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
// Controlled component tests
|
|
782
|
+
describe("Controlled component behavior", () => {
|
|
783
|
+
it("displays the provided value prop", () => {
|
|
784
|
+
const mockOnChange = vi.fn();
|
|
785
|
+
const mockOnSubmitMessage = vi.fn();
|
|
786
|
+
|
|
787
|
+
renderWithProvider(
|
|
788
|
+
<CopilotChatInput
|
|
789
|
+
value="test value"
|
|
790
|
+
onChange={mockOnChange}
|
|
791
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
792
|
+
/>,
|
|
793
|
+
);
|
|
794
|
+
|
|
795
|
+
const input = screen.getByRole("textbox");
|
|
796
|
+
expect((input as HTMLTextAreaElement).value).toBe("test value");
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
it("calls onChange when user types", () => {
|
|
800
|
+
const mockOnChange = vi.fn();
|
|
801
|
+
const mockOnSubmitMessage = vi.fn();
|
|
802
|
+
|
|
803
|
+
renderWithProvider(
|
|
804
|
+
<CopilotChatInput
|
|
805
|
+
value=""
|
|
806
|
+
onChange={mockOnChange}
|
|
807
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
808
|
+
/>,
|
|
809
|
+
);
|
|
810
|
+
|
|
811
|
+
const input = screen.getByRole("textbox");
|
|
812
|
+
fireEvent.change(input, { target: { value: "new text" } });
|
|
813
|
+
|
|
814
|
+
expect(mockOnChange).toHaveBeenCalledWith("new text");
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
it("calls onSubmitMessage when form is submitted", () => {
|
|
818
|
+
const mockOnChange = vi.fn();
|
|
819
|
+
const mockOnSubmitMessage = vi.fn();
|
|
820
|
+
|
|
821
|
+
renderWithProvider(
|
|
822
|
+
<CopilotChatInput
|
|
823
|
+
value="hello world"
|
|
824
|
+
onChange={mockOnChange}
|
|
825
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
826
|
+
/>,
|
|
827
|
+
);
|
|
828
|
+
|
|
829
|
+
const input = screen.getByRole("textbox");
|
|
830
|
+
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
831
|
+
|
|
832
|
+
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello world");
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
it("calls onSubmitMessage when send button is clicked", () => {
|
|
836
|
+
const mockOnChange = vi.fn();
|
|
837
|
+
const mockOnSubmitMessage = vi.fn();
|
|
838
|
+
|
|
839
|
+
const { container } = renderWithProvider(
|
|
840
|
+
<CopilotChatInput
|
|
841
|
+
value="test message"
|
|
842
|
+
onChange={mockOnChange}
|
|
843
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
844
|
+
/>,
|
|
845
|
+
);
|
|
846
|
+
|
|
847
|
+
const sendButton = getSendButton(container);
|
|
848
|
+
expect(sendButton).not.toBeNull();
|
|
849
|
+
fireEvent.click(sendButton!);
|
|
850
|
+
|
|
851
|
+
expect(mockOnSubmitMessage).toHaveBeenCalledWith("test message");
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
it("trims whitespace when submitting", () => {
|
|
855
|
+
const mockOnChange = vi.fn();
|
|
856
|
+
const mockOnSubmitMessage = vi.fn();
|
|
857
|
+
|
|
858
|
+
renderWithProvider(
|
|
859
|
+
<CopilotChatInput
|
|
860
|
+
value=" hello world "
|
|
861
|
+
onChange={mockOnChange}
|
|
862
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
863
|
+
/>,
|
|
864
|
+
);
|
|
865
|
+
|
|
866
|
+
const input = screen.getByRole("textbox");
|
|
867
|
+
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
|
|
868
|
+
|
|
869
|
+
expect(mockOnSubmitMessage).toHaveBeenCalledWith("hello world");
|
|
870
|
+
});
|
|
871
|
+
|
|
872
|
+
it("does not submit empty or whitespace-only messages", () => {
|
|
873
|
+
const mockOnChange = vi.fn();
|
|
874
|
+
const mockOnSubmitMessage = vi.fn();
|
|
875
|
+
|
|
876
|
+
const { container } = renderWithProvider(
|
|
877
|
+
<CopilotChatInput
|
|
878
|
+
value=" "
|
|
879
|
+
onChange={mockOnChange}
|
|
880
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
881
|
+
/>,
|
|
882
|
+
);
|
|
883
|
+
|
|
884
|
+
const sendButton = getSendButton(container);
|
|
885
|
+
expect(sendButton).not.toBeNull();
|
|
886
|
+
fireEvent.click(sendButton!);
|
|
887
|
+
|
|
888
|
+
expect(mockOnSubmitMessage).not.toHaveBeenCalled();
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
it("disables send button when onSubmitMessage is not provided", () => {
|
|
892
|
+
const mockOnChange = vi.fn();
|
|
893
|
+
|
|
894
|
+
const { container } = renderWithProvider(
|
|
895
|
+
<CopilotChatInput value="some text" onChange={mockOnChange} />,
|
|
896
|
+
);
|
|
897
|
+
|
|
898
|
+
const sendButton = getSendButton(container);
|
|
899
|
+
expect(sendButton?.disabled).toBe(true);
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
it("disables send button when value is empty", () => {
|
|
903
|
+
const mockOnChange = vi.fn();
|
|
904
|
+
const mockOnSubmitMessage = vi.fn();
|
|
905
|
+
|
|
906
|
+
const { container } = renderWithProvider(
|
|
907
|
+
<CopilotChatInput
|
|
908
|
+
value=""
|
|
909
|
+
onChange={mockOnChange}
|
|
910
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
911
|
+
/>,
|
|
912
|
+
);
|
|
913
|
+
|
|
914
|
+
const sendButton = getSendButton(container);
|
|
915
|
+
expect(sendButton?.disabled).toBe(true);
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
it("enables send button when value has content and onSubmitMessage is provided", () => {
|
|
919
|
+
const mockOnChange = vi.fn();
|
|
920
|
+
const mockOnSubmitMessage = vi.fn();
|
|
921
|
+
|
|
922
|
+
const { container } = renderWithProvider(
|
|
923
|
+
<CopilotChatInput
|
|
924
|
+
value="hello"
|
|
925
|
+
onChange={mockOnChange}
|
|
926
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
927
|
+
/>,
|
|
928
|
+
);
|
|
929
|
+
|
|
930
|
+
const sendButton = getSendButton(container);
|
|
931
|
+
expect(sendButton?.disabled).toBe(false);
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
it("works as a fully controlled component", () => {
|
|
935
|
+
const mockOnChange = vi.fn();
|
|
936
|
+
const mockOnSubmitMessage = vi.fn();
|
|
937
|
+
|
|
938
|
+
const { rerender } = renderWithProvider(
|
|
939
|
+
<CopilotChatInput
|
|
940
|
+
value="initial"
|
|
941
|
+
onChange={mockOnChange}
|
|
942
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
943
|
+
/>,
|
|
944
|
+
);
|
|
945
|
+
|
|
946
|
+
const input = screen.getByRole("textbox");
|
|
947
|
+
expect((input as HTMLTextAreaElement).value).toBe("initial");
|
|
948
|
+
|
|
949
|
+
// Simulate parent component updating the value
|
|
950
|
+
rerender(
|
|
951
|
+
<CopilotChatConfigurationProvider threadId={TEST_THREAD_ID}>
|
|
952
|
+
<CopilotChatInput
|
|
953
|
+
value="updated"
|
|
954
|
+
onChange={mockOnChange}
|
|
955
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
956
|
+
/>
|
|
957
|
+
</CopilotChatConfigurationProvider>,
|
|
958
|
+
);
|
|
959
|
+
|
|
960
|
+
expect((input as HTMLTextAreaElement).value).toBe("updated");
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
it("does not clear input after submission when controlled", () => {
|
|
964
|
+
const mockOnChange = vi.fn();
|
|
965
|
+
const mockOnSubmitMessage = vi.fn();
|
|
966
|
+
|
|
967
|
+
const { container } = renderWithProvider(
|
|
968
|
+
<CopilotChatInput
|
|
969
|
+
value="test message"
|
|
970
|
+
onChange={mockOnChange}
|
|
971
|
+
onSubmitMessage={mockOnSubmitMessage}
|
|
972
|
+
/>,
|
|
973
|
+
);
|
|
974
|
+
|
|
975
|
+
const input = screen.getByRole("textbox");
|
|
976
|
+
const sendButton = getSendButton(container);
|
|
977
|
+
|
|
978
|
+
fireEvent.click(sendButton!);
|
|
979
|
+
|
|
980
|
+
// In controlled mode, the component should not clear the input
|
|
981
|
+
// It's up to the parent to manage the value
|
|
982
|
+
expect((input as HTMLTextAreaElement).value).toBe("test message");
|
|
983
|
+
expect(mockOnSubmitMessage).toHaveBeenCalledWith("test message");
|
|
984
|
+
});
|
|
985
|
+
});
|
|
986
|
+
});
|