Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 10 additions & 36 deletions site/src/pages/AgentsPage/components/AgentChatInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
MockWorkspaceAgent,
} from "#/testHelpers/entities";
import { createMockFile } from "#/testHelpers/files";
import { setupMatchMedia } from "#/testHelpers/matchMedia";
import {
withDashboardProvider,
withProxyProvider,
Expand Down Expand Up @@ -217,44 +218,17 @@ export const ModifierEnterSendsWhenRequired: Story = {
},
};

/**
* CODAGT-210: On mobile viewports, Enter must insert a newline rather
* than submit the message, because Shift+Enter is cumbersome on
* on-screen keyboards. Users submit via the send button instead.
*/
export const MobileEnterInsertsNewline: Story = {
args: {
onSend: fn(),
initialValue: "Line one",
parameters: {
viewport: { defaultViewport: "mobile1" },
},
play: async ({ canvasElement, args }) => {
const originalMatchMedia = window.matchMedia;
window.matchMedia = (query: string) =>
({
matches: query === "(max-width: 639px)",
media: query,
onchange: null,
addEventListener: () => undefined,
removeEventListener: () => undefined,
dispatchEvent: () => true,
addListener: () => undefined,
removeListener: () => undefined,
}) as MediaQueryList;

try {
const canvas = within(canvasElement);
const editor = canvas.getByTestId("chat-message-input");
await waitFor(() => {
expect(editor.textContent).toBe("Line one");
});

await userEvent.click(editor);
await userEvent.keyboard("{Enter}");

expect(args.onSend).not.toHaveBeenCalled();
} finally {
window.matchMedia = originalMatchMedia;
}
beforeEach: () => setupMatchMedia({ "(pointer: coarse)": true }).restore,
play: async ({ canvasElement }) => {
const editor = within(canvasElement).getByRole("textbox", {
name: "Chat message",
});
await userEvent.click(editor);
await userEvent.keyboard("Line one{Enter}Line two");
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {
createRef,
type FC,
type ReactNode,
useLayoutEffect,
Expand All @@ -8,6 +10,7 @@ import {
} from "react";
import { type QueryClient, QueryClientProvider } from "react-query";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import type { AgentChatSendShortcut } from "#/api/typesGenerated";
import { createTestQueryClient } from "#/testHelpers/renderHelpers";
import { ChatMessageInput, type ChatMessageInputRef } from "./ChatMessageInput";

Expand Down Expand Up @@ -76,6 +79,107 @@ beforeAll(() => {
describe("ChatMessageInput", () => {
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});

describe.each([390, 640, 1280])("at %ipx wide", (width) => {
describe.each([false, true])("with coarse pointer %s", (coarsePointer) => {
it.each<{
shortcut: AgentChatSendShortcut;
keys: string;
modifier: boolean;
shift: boolean;
}>([
{ shortcut: "enter", keys: "{Enter}", modifier: false, shift: false },
{
shortcut: "enter",
keys: "{Meta>}{Enter}{/Meta}",
modifier: true,
shift: false,
},
{
shortcut: "enter",
keys: "{Control>}{Enter}{/Control}",
modifier: true,
shift: false,
},
{
shortcut: "enter",
keys: "{Shift>}{Enter}{/Shift}",
modifier: false,
shift: true,
},
{
shortcut: "modifier_enter",
keys: "{Enter}",
modifier: false,
shift: false,
},
{
shortcut: "modifier_enter",
keys: "{Meta>}{Enter}{/Meta}",
modifier: true,
shift: false,
},
{
shortcut: "modifier_enter",
keys: "{Control>}{Enter}{/Control}",
modifier: true,
shift: false,
},
{
shortcut: "modifier_enter",
keys: "{Shift>}{Meta>}{Enter}{/Meta}{/Shift}",
modifier: true,
shift: true,
},
])(
"handles $keys with the $shortcut preference",
async ({ shortcut, keys, modifier, shift }) => {
vi.stubGlobal(
"matchMedia",
(query: string): MediaQueryList => ({
matches:
query === "(pointer: coarse)"
? coarsePointer
: query === "(max-width: 639px)" && width < 640,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
}),
);
const user = userEvent.setup();
const inputRef = createRef<ChatMessageInputRef>();
const onEnter = vi.fn();
renderWithQueryClient(
<ChatMessageInput
ref={inputRef}
aria-label="Chat message input"
sendShortcut={shortcut}
onEnter={onEnter}
/>,
);
await user.click(
screen.getByRole("textbox", { name: "Chat message input" }),
);
await user.paste("Draft");
await user.keyboard(keys);

const shouldSend =
!shift && (modifier || (!coarsePointer && shortcut === "enter"));
await waitFor(() => {
expect(inputRef.current?.getValue()).toBe(
shouldSend ? "Draft" : "Draft\n",
);
});
expect(onEnter).toHaveBeenCalledTimes(shouldSend ? 1 : 0);
},
);
});
});

it("returns the initial draft before the editor visually hydrates", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
import { useQuery } from "react-query";
import { userSkills } from "#/api/queries/userSkills";
import type * as TypesGen from "#/api/typesGenerated";
import { isMobileViewport } from "#/utils/mobile";
import {
DEFAULT_AGENT_CHAT_SEND_SHORTCUT,
MODIFIER_AGENT_CHAT_SEND_SHORTCUT,
Expand Down Expand Up @@ -284,12 +283,9 @@ const PasteSanitizationPlugin: FC<{
return null;
};

// Handles Enter key behavior. By default, plain Enter submits via
// the onEnter callback, and Shift+Enter inserts a newline. When the
// modifier shortcut is selected, Cmd/Ctrl+Enter submits instead, and
// plain Enter inserts a newline. On mobile viewports, Enter always
// inserts a newline; users submit via the send button because
// Shift+Enter is cumbersome on touch keyboards (CODAGT-210).
// Touch keyboards need plain Enter for newlines (CODAGT-210). Pointer
// capability, not viewport width, keeps narrow desktop windows usable.
// Cmd/Ctrl+Enter submits on either input type; Shift+Enter stays a newline.
const EnterKeyPlugin: FC<{
onEnter?: () => void;
sendShortcut: TypesGen.AgentChatSendShortcut;
Expand All @@ -302,9 +298,9 @@ const EnterKeyPlugin: FC<{
(event: KeyboardEvent | null) => {
const shouldInsertLineBreak =
event?.shiftKey ||
isMobileViewport() ||
(sendShortcut === MODIFIER_AGENT_CHAT_SEND_SHORTCUT &&
!(event?.metaKey || event?.ctrlKey));
(!(event?.metaKey || event?.ctrlKey) &&
(window.matchMedia("(pointer: coarse)").matches ||
sendShortcut === MODIFIER_AGENT_CHAT_SEND_SHORTCUT));
if (shouldInsertLineBreak) {
event?.preventDefault();
editor.update(() => {
Expand Down
Loading