-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathPromptInput.test.tsx
More file actions
188 lines (158 loc) · 5.05 KB
/
Copy pathPromptInput.test.tsx
File metadata and controls
188 lines (158 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Theme } from "@radix-ui/themes";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type React from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
const editorState = vi.hoisted(() => ({ isEmpty: false }));
const settingsState = vi.hoisted(() => ({ slotMachineMode: false }));
vi.mock("../tiptap/useTiptapEditor", () => ({
useTiptapEditor: () => ({
editor: null,
isReady: true,
isEmpty: editorState.isEmpty,
isBashMode: false,
submit: vi.fn(),
focus: vi.fn(),
blur: vi.fn(),
clear: vi.fn(),
getText: vi.fn(),
getContent: vi.fn(),
setContent: vi.fn(),
insertChip: vi.fn(),
removeChipById: vi.fn(),
replaceChipAttrs: vi.fn(),
attachments: [],
addAttachment: vi.fn(),
removeAttachment: vi.fn(),
}),
}));
vi.mock("@posthog/ui/features/settings/settingsStore", () => ({
useSettingsStore: (selector: (s: typeof settingsState) => unknown) =>
selector(settingsState),
}));
vi.mock("../../skills/useSkills", () => ({
useSkills: () => ({ data: [] }),
}));
vi.mock("@posthog/ui/features/feature-flags/useFeatureFlag", () => ({
useFeatureFlag: () => false,
}));
vi.mock("../draftStore", () => ({
useDraftStore: Object.assign(
(selector: (s: unknown) => unknown) =>
selector({ focusRequested: {}, actions: { clearFocusRequest: vi.fn() } }),
{
getState: () => ({
actions: { setCommands: vi.fn(), clearCommands: vi.fn() },
}),
},
),
}));
vi.mock("./AttachmentMenu", () => ({ AttachmentMenu: () => null }));
vi.mock("./AttachmentsBar", () => ({ AttachmentsBar: () => null }));
vi.mock("./SlotMachineSubmit", () => ({
SlotMachineSubmit: ({
disabled,
onSubmit,
}: {
disabled?: boolean;
onSubmit?: () => void;
}) => (
<button
type="button"
aria-label="Slot machine submit"
disabled={disabled}
onClick={onSubmit}
/>
),
}));
vi.mock("@posthog/quill", () => ({
InputGroup: ({ children }: { children: React.ReactNode }) => (
<div>{children}</div>
),
InputGroupAddon: ({ children }: { children: React.ReactNode }) => (
<div>{children}</div>
),
InputGroupButton: ({
children,
...props
}: React.ButtonHTMLAttributes<HTMLButtonElement>) => (
<button type="button" {...props}>
{children}
</button>
),
}));
import { PromptInput } from "./PromptInput";
function renderInput(props: Partial<React.ComponentProps<typeof PromptInput>>) {
return render(
<Theme>
<PromptInput sessionId="s1" {...props} />
</Theme>,
);
}
describe("PromptInput submit/stop affordance", () => {
beforeEach(() => {
vi.clearAllMocks();
editorState.isEmpty = false;
settingsState.slotMachineMode = false;
});
it("shows Stop (not Send) while loading and calls onCancel when clicked", async () => {
const user = userEvent.setup();
const onCancel = vi.fn();
renderInput({ isLoading: true, onCancel });
const stop = screen.getByRole("button", { name: "Stop" });
expect(
screen.queryByRole("button", { name: "Send message" }),
).not.toBeInTheDocument();
await user.click(stop);
expect(onCancel).toHaveBeenCalledOnce();
});
it("keeps Send enabled mid-turn when no cancel handler (queue/steer path)", () => {
// isLoading true but no onCancel => inStopMode is false, so the composer
// must still expose an enabled Send so messages queue/steer mid-turn.
// Regression guard: adding `|| isLoading` to submitBlocked disables this.
renderInput({ isLoading: true });
const send = screen.getByRole("button", { name: "Send message" });
expect(send).toBeEnabled();
});
it("disables Send when the editor is empty", () => {
editorState.isEmpty = true;
renderInput({});
const send = screen.getByRole("button", { name: "Send message" });
expect(send).toBeDisabled();
});
});
describe("PromptInput escape handling", () => {
beforeEach(() => {
vi.clearAllMocks();
editorState.isEmpty = false;
settingsState.slotMachineMode = false;
});
it("cancels the queued-message edit on Escape", async () => {
const user = userEvent.setup();
const onCancelEdit = vi.fn();
renderInput({ isEditingQueued: true, onCancelEdit });
await user.keyboard("{Escape}");
expect(onCancelEdit).toHaveBeenCalledOnce();
});
it("prioritizes cancelling the edit over stopping the run on Escape", async () => {
const user = userEvent.setup();
const onCancel = vi.fn();
const onCancelEdit = vi.fn();
renderInput({
isLoading: true,
onCancel,
isEditingQueued: true,
onCancelEdit,
});
await user.keyboard("{Escape}");
expect(onCancelEdit).toHaveBeenCalledOnce();
expect(onCancel).not.toHaveBeenCalled();
});
it("still stops the run on Escape when not editing", async () => {
const user = userEvent.setup();
const onCancel = vi.fn();
renderInput({ isLoading: true, onCancel, isEditingQueued: false });
await user.keyboard("{Escape}");
expect(onCancel).toHaveBeenCalledOnce();
});
});