-
Notifications
You must be signed in to change notification settings - Fork 334
feat(ask): start chat from selected code range #1553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikhil008-git
wants to merge
4
commits into
sourcebot-dev:main
Choose a base branch
from
nikhil008-git:feat/ask-selected-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
865aac8
feat(ask): start chat from selected code range
nikhil008-git eedef3a
fix(ask): type ranged prompt sources
nikhil008-git 35956de
fix(chat): preserve MCP preferences when asking from selection
nikhil008-git 2b7bc80
fix(chat): preserve MCP preferences and prevent duplicate source chats
nikhil008-git File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/web/src/features/chat/useCreateNewChatThread.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { act, renderHook } from "@testing-library/react"; | ||
| import { afterEach, expect, test, vi } from "vitest"; | ||
| import type { Source } from "./types"; | ||
| import { DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY } from "./constants"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| createChat: vi.fn(), | ||
| createUIMessage: vi.fn(), | ||
| push: vi.fn(), | ||
| setChatState: vi.fn(), | ||
| toast: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("next/navigation", () => ({ | ||
| useRouter: () => ({ push: mocks.push }), | ||
| })); | ||
|
|
||
| vi.mock("@/components/hooks/use-toast", () => ({ | ||
| useToast: () => ({ toast: mocks.toast }), | ||
| })); | ||
|
|
||
| vi.mock("./actions", () => ({ | ||
| createChat: mocks.createChat, | ||
| })); | ||
|
|
||
| vi.mock("@/lib/utils", () => ({ | ||
| isServiceError: () => false, | ||
| createPathWithQueryParams: (path: string) => path, | ||
| })); | ||
|
|
||
| vi.mock("./utils", () => ({ | ||
| createUIMessage: mocks.createUIMessage, | ||
| getAllMentionElements: () => [], | ||
| slateContentToString: () => "", | ||
| })); | ||
|
|
||
| vi.mock("usehooks-ts", () => ({ | ||
| useSessionStorage: () => [null, mocks.setChatState], | ||
| })); | ||
|
|
||
| const { useCreateNewChatThread } = await import("./useCreateNewChatThread"); | ||
|
|
||
| afterEach(() => { | ||
| window.localStorage.clear(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| test("createChatFromSource preserves disabled MCP servers from local storage", async () => { | ||
| const disabledMcpServerIds = ["linear", "github"]; | ||
| window.localStorage.setItem( | ||
| DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY, | ||
| JSON.stringify(disabledMcpServerIds), | ||
| ); | ||
| mocks.createChat.mockResolvedValue({ id: "chat-1" }); | ||
| mocks.createUIMessage.mockReturnValue({ id: "initial-message" }); | ||
| const { result } = renderHook(() => useCreateNewChatThread()); | ||
| const source: Source = { | ||
| type: "file", | ||
| repo: "github.com/sourcebot-dev/sourcebot", | ||
| path: "packages/web/src/auth.ts", | ||
| name: "auth.ts", | ||
| revision: "main", | ||
| }; | ||
|
|
||
| await act(async () => { | ||
| await result.current.createChatFromSource(source); | ||
| }); | ||
|
|
||
| expect(mocks.createUIMessage).toHaveBeenCalledWith( | ||
| "Explain this selected code.", | ||
| [], | ||
| [], | ||
| disabledMcpServerIds, | ||
| [], | ||
| [source], | ||
| ); | ||
| expect(mocks.setChatState).toHaveBeenCalledWith({ | ||
| inputMessage: { id: "initial-message" }, | ||
| selectedSearchScopes: [], | ||
| disabledMcpServerIds, | ||
| }); | ||
| }); | ||
|
|
||
| test("createChatFromSource ignores duplicate calls while chat creation is pending", async () => { | ||
| let resolveCreateChat: ((value: { id: string }) => void) | undefined; | ||
| mocks.createChat.mockImplementation(() => new Promise((resolve) => { | ||
| resolveCreateChat = resolve; | ||
| })); | ||
| mocks.createUIMessage.mockReturnValue({ id: "initial-message" }); | ||
| const { result } = renderHook(() => useCreateNewChatThread()); | ||
| const source: Source = { | ||
| type: "file", | ||
| repo: "github.com/sourcebot-dev/sourcebot", | ||
| path: "packages/web/src/auth.ts", | ||
| name: "auth.ts", | ||
| revision: "main", | ||
| }; | ||
|
|
||
| let firstCreate: Promise<void> | undefined; | ||
| let secondCreate: Promise<void> | undefined; | ||
| act(() => { | ||
| firstCreate = result.current.createChatFromSource(source); | ||
| secondCreate = result.current.createChatFromSource(source); | ||
| }); | ||
|
|
||
| expect(mocks.createChat).toHaveBeenCalledTimes(1); | ||
|
|
||
| resolveCreateChat?.({ id: "chat-1" }); | ||
| await act(async () => { | ||
| await Promise.all([firstCreate, secondCreate]); | ||
| }); | ||
|
|
||
| expect(mocks.push).toHaveBeenCalledTimes(1); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.