diff --git a/site/src/pages/AgentsPage/AgentChatPage.tsx b/site/src/pages/AgentsPage/AgentChatPage.tsx index 9f505e078ca..ccaf167ee75 100644 --- a/site/src/pages/AgentsPage/AgentChatPage.tsx +++ b/site/src/pages/AgentsPage/AgentChatPage.tsx @@ -405,17 +405,6 @@ const AgentChatPage: React.FC = () => { username: currentUser.username, }); - const handleCommit = (repoRoot: string) => { - const commitPrompt = `Commit and push the working changes in ${repoRoot}. If there are unstaged files, commit them too.`; - const current = inputValueRef.current; - if (current.includes(commitPrompt)) { - return; - } - const prefix = current.trim() ? "\n\n" : ""; - chatInputRef.current?.insertText(prefix + commitPrompt); - chatInputRef.current?.focus(); - }; - // Validate explicit and historical choices against organization options. // Prefer the usable organization default before another organization model. const effectiveSelectedModel = (() => { @@ -783,7 +772,6 @@ const AgentChatPage: React.FC = () => { debugLoggingEnabled={debugLoggingEnabled} gitWatcher={gitWatcher} sshCommand={sshCommand} - handleCommit={handleCommit} handleInterrupt={handleInterrupt} handleDeleteQueuedMessage={handleDeleteQueuedMessage} handlePromoteQueuedMessage={handlePromoteQueuedMessage} diff --git a/site/src/pages/AgentsPage/AgentChatPageView.stories.tsx b/site/src/pages/AgentsPage/AgentChatPageView.stories.tsx index 719fd7c54e2..ceb32a59b42 100644 --- a/site/src/pages/AgentsPage/AgentChatPageView.stories.tsx +++ b/site/src/pages/AgentsPage/AgentChatPageView.stories.tsx @@ -186,7 +186,6 @@ const StoryAgentChatPageView: React.FC = ({ debugLoggingEnabled: false, gitWatcher: buildGitWatcher(), sshCommand: undefined as string | undefined, - handleCommit: fn(), handleInterrupt: fn(), handleDeleteQueuedMessage: fn(), handlePromoteQueuedMessage: fn(), diff --git a/site/src/pages/AgentsPage/AgentChatPageView.tsx b/site/src/pages/AgentsPage/AgentChatPageView.tsx index f9fa8c16c66..627ed5fc3e4 100644 --- a/site/src/pages/AgentsPage/AgentChatPageView.tsx +++ b/site/src/pages/AgentsPage/AgentChatPageView.tsx @@ -154,7 +154,6 @@ type AgentChatPageViewProps = { // Workspace action handlers. sshCommand: string | undefined; - handleCommit: (repoRoot: string) => void; // Chat action handlers. handleInterrupt: () => void; @@ -304,7 +303,6 @@ export const AgentChatPageView: React.FC = ({ debugLoggingEnabled, gitWatcher, sshCommand, - handleCommit, handleInterrupt, handleDeleteQueuedMessage, handlePromoteQueuedMessage, @@ -684,7 +682,6 @@ export const AgentChatPageView: React.FC = ({ !gitWatcher.hasReceivedChanges } onRefresh={handleRefresh} - onCommit={handleCommit} isExpanded={visualExpanded} remoteDiffStats={chat.diff_status} chatInputRef={editing.chatInputRef} diff --git a/site/src/pages/AgentsPage/components/GitPanel/GitPanel.stories.tsx b/site/src/pages/AgentsPage/components/GitPanel/GitPanel.stories.tsx index 755f3bc5289..6bfdda2d372 100644 --- a/site/src/pages/AgentsPage/components/GitPanel/GitPanel.stories.tsx +++ b/site/src/pages/AgentsPage/components/GitPanel/GitPanel.stories.tsx @@ -101,7 +101,6 @@ const meta: Meta = { component: GitPanel, args: { onRefresh: fn().mockReturnValue(true), - onCommit: fn(), repositories: new Map(), }, decorators: [ diff --git a/site/src/pages/AgentsPage/components/GitPanel/GitPanel.test.tsx b/site/src/pages/AgentsPage/components/GitPanel/GitPanel.test.tsx new file mode 100644 index 00000000000..b2ec8c4a3d0 --- /dev/null +++ b/site/src/pages/AgentsPage/components/GitPanel/GitPanel.test.tsx @@ -0,0 +1,95 @@ +import { render, screen } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { describe, expect, it, vi } from "vitest"; +import type { WorkspaceAgentRepoChanges } from "#/api/typesGenerated"; +import type { ChatMessageInputRef } from "../AgentChatInput"; +import { GitPanel } from "./GitPanel"; + +vi.mock("../DiffViewer/DiffViewer", () => ({ + loadDiffStyle: () => "unified", + saveDiffStyle: () => {}, +})); + +vi.mock("../DiffViewer/LocalDiffPanel", () => ({ + LocalDiffPanel: () => null, +})); + +vi.mock("../DiffViewer/RemoteDiffPanel", () => ({ + RemoteDiffPanel: () => null, +})); + +const repoRoot = "/workspace/special-repo"; +const commitPrompt = `Commit and push the working changes in ${repoRoot}. If there are unstaged files, commit them too.`; + +const repo: WorkspaceAgentRepoChanges = { + repo_root: repoRoot, + branch: "feat/example", + unified_diff: `diff --git a/src/main.ts b/src/main.ts +index abc1234..def5678 100644 +--- a/src/main.ts ++++ b/src/main.ts +@@ -1 +1,2 @@ + existing ++added +`, +}; + +function mockComposer(value = ""): ChatMessageInputRef { + let current = value; + return { + getValue: () => current, + insertText: vi.fn((text: string) => { + current += text; + }), + setValue: vi.fn(), + clear: vi.fn(), + focus: vi.fn(), + addFileReference: vi.fn(), + getContentParts: vi.fn(() => []), + }; +} + +function renderGitPanel(input: ChatMessageInputRef) { + render( + true} + chatInputRef={{ current: input }} + />, + ); +} + +describe("GitPanel", () => { + it("inserts a commit prompt for the active repo when Commit is clicked", async () => { + const user = userEvent.setup(); + const input = mockComposer(); + renderGitPanel(input); + + await user.click(screen.getByRole("button", { name: "Commit" })); + + expect(input.insertText).toHaveBeenCalledWith(commitPrompt); + expect(input.focus).toHaveBeenCalled(); + }); + + it("does not insert the prompt twice for the same repo", async () => { + const user = userEvent.setup(); + const input = mockComposer(); + renderGitPanel(input); + + const commit = screen.getByRole("button", { name: "Commit" }); + await user.click(commit); + await user.click(commit); + + expect(input.insertText).toHaveBeenCalledTimes(1); + }); + + it("separates the prompt from existing composer text", async () => { + const user = userEvent.setup(); + const input = mockComposer("please review"); + renderGitPanel(input); + + await user.click(screen.getByRole("button", { name: "Commit" })); + + expect(input.insertText).toHaveBeenCalledWith(`\n\n${commitPrompt}`); + }); +}); diff --git a/site/src/pages/AgentsPage/components/GitPanel/GitPanel.tsx b/site/src/pages/AgentsPage/components/GitPanel/GitPanel.tsx index ac962e3ab23..fbc79e65a2c 100644 --- a/site/src/pages/AgentsPage/components/GitPanel/GitPanel.tsx +++ b/site/src/pages/AgentsPage/components/GitPanel/GitPanel.tsx @@ -65,15 +65,13 @@ type GitPanelProps = { repositories: ReadonlyMap; /** Callback to send a refresh to the git watcher. Returns false when disconnected. */ onRefresh: () => boolean; - /** Called when the user clicks the Commit button for a working repo. */ - onCommit: (repoRoot: string) => void; /** Whether the panel is in expanded/fullscreen mode. */ isExpanded?: boolean; /** Whether the watcher is loading its initial repository state. */ isGitStatusLoading?: boolean; /** Diff status for the remote/branch view (includes PR metadata). */ remoteDiffStats?: ChatDiffStatus; - /** Ref to the chat input, forwarded to RemoteDiffPanel. */ + /** Chat composer, used to insert commit prompts and file comments. */ chatInputRef?: React.RefObject; /** * Repo roots that have been dirty at some point during this session. @@ -111,7 +109,6 @@ export const GitPanel: React.FC = ({ prTab, repositories, onRefresh, - onCommit, isExpanded, isGitStatusLoading = false, remoteDiffStats, @@ -322,6 +319,19 @@ export const GitPanel: React.FC = ({ } }; + const handleCommit = (repoRoot: string) => { + const input = chatInputRef?.current; + if (!input) { + return; + } + const prompt = `Commit and push the working changes in ${repoRoot}. If there are unstaged files, commit them too.`; + const current = input.getValue(); + if (!current.includes(prompt)) { + input.insertText(current.trim() ? `\n\n${prompt}` : prompt); + } + input.focus(); + }; + return (
{/* Toolbar */} @@ -440,7 +450,7 @@ export const GitPanel: React.FC = ({ deletions: 0, } } - onCommit={onCommit} + onCommit={handleCommit} isExpanded={isExpanded} diffStyle={diffStyle} chatInputRef={chatInputRef}