-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Enhancement: Allow escaping of context mentions #3088
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a29c488
allow escaping of context mentions
cannuri 9215a24
Add escape button for @ mentions in context menu
cannuri a03f34b
Fix TypeScript null check for textAreaRef in escape handling
cannuri 9f3329c
Fix: Update context menu test and webview-ui lint script
daniel-lxs 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import { jest } from "@jest/globals" | ||
| import type { AskApproval, HandleError } from "../../../shared/tools" // Import the types | ||
|
|
||
| // Mock dependencies before importing the module under test | ||
| // Explicitly type the mock functions | ||
| const mockAskApproval = jest.fn<AskApproval>() | ||
| const mockHandleError = jest.fn<HandleError>() // Explicitly type HandleError | ||
| const mockPushToolResult = jest.fn() | ||
| const mockRemoveClosingTag = jest.fn((_name: string, value: string | undefined) => value ?? "") // Simple mock | ||
| const mockGetModeBySlug = jest.fn() | ||
| // Define a minimal type for the resolved value | ||
| type MockClineInstance = { taskId: string } | ||
| // Make initClineWithTask return a mock Cline-like object with taskId, providing type hint | ||
| const mockInitClineWithTask = jest | ||
| .fn<() => Promise<MockClineInstance>>() | ||
| .mockResolvedValue({ taskId: "mock-subtask-id" }) | ||
| const mockEmit = jest.fn() | ||
| const mockRecordToolError = jest.fn() | ||
| const mockSayAndCreateMissingParamError = jest.fn() | ||
|
|
||
| // Mock the Cline instance and its methods/properties | ||
| const mockCline = { | ||
| ask: jest.fn(), | ||
| sayAndCreateMissingParamError: mockSayAndCreateMissingParamError, | ||
| emit: mockEmit, | ||
| recordToolError: mockRecordToolError, | ||
| consecutiveMistakeCount: 0, | ||
| isPaused: false, | ||
| pausedModeSlug: "ask", // Default or mock value | ||
| providerRef: { | ||
| deref: jest.fn(() => ({ | ||
| getState: jest.fn(() => ({ customModes: [], mode: "ask" })), // Mock provider state | ||
| handleModeSwitch: jest.fn(), | ||
| initClineWithTask: mockInitClineWithTask, | ||
| })), | ||
| }, | ||
| } | ||
|
|
||
| // Mock other modules | ||
| jest.mock("delay", () => jest.fn(() => Promise.resolve())) // Mock delay to resolve immediately | ||
| jest.mock("../../../shared/modes", () => ({ | ||
| // Corrected path | ||
| getModeBySlug: mockGetModeBySlug, | ||
| defaultModeSlug: "ask", | ||
| })) | ||
| jest.mock("../../prompts/responses", () => ({ | ||
| // Corrected path | ||
| formatResponse: { | ||
| toolError: jest.fn((msg: string) => `Tool Error: ${msg}`), // Simple mock | ||
| }, | ||
| })) | ||
|
|
||
| // Import the function to test AFTER mocks are set up | ||
| import { newTaskTool } from "../newTaskTool" | ||
| import type { ToolUse } from "../../../shared/tools" | ||
|
|
||
| describe("newTaskTool", () => { | ||
| beforeEach(() => { | ||
| // Reset mocks before each test | ||
| jest.clearAllMocks() | ||
| mockAskApproval.mockResolvedValue(true) // Default to approved | ||
| mockGetModeBySlug.mockReturnValue({ slug: "code", name: "Code Mode" }) // Default valid mode | ||
| mockCline.consecutiveMistakeCount = 0 | ||
| mockCline.isPaused = false | ||
| }) | ||
|
|
||
| it("should correctly un-escape \\\\@ to \\@ in the message passed to the new task", async () => { | ||
| const block: ToolUse = { | ||
| type: "tool_use", // Add required 'type' property | ||
| name: "new_task", // Correct property name | ||
| params: { | ||
| mode: "code", | ||
| message: "Review this: \\\\@file1.txt and also \\\\\\\\@file2.txt", // Input with \\@ and \\\\@ | ||
| }, | ||
| partial: false, | ||
| } | ||
|
|
||
| await newTaskTool( | ||
| mockCline as any, // Use 'as any' for simplicity in mocking complex type | ||
| block, | ||
| mockAskApproval, // Now correctly typed | ||
| mockHandleError, | ||
| mockPushToolResult, | ||
| mockRemoveClosingTag, | ||
| ) | ||
|
|
||
| // Verify askApproval was called | ||
| expect(mockAskApproval).toHaveBeenCalled() | ||
|
|
||
| // Verify the message passed to initClineWithTask reflects the code's behavior in unit tests | ||
| expect(mockInitClineWithTask).toHaveBeenCalledWith( | ||
| "Review this: \\@file1.txt and also \\\\\\@file2.txt", // Unit Test Expectation: \\@ -> \@, \\\\@ -> \\\\@ | ||
| undefined, | ||
| mockCline, | ||
| ) | ||
|
|
||
| // Verify side effects | ||
| expect(mockCline.emit).toHaveBeenCalledWith("taskSpawned", expect.any(String)) // Assuming initCline returns a mock task ID | ||
| expect(mockCline.isPaused).toBe(true) | ||
| expect(mockCline.emit).toHaveBeenCalledWith("taskPaused") | ||
| expect(mockPushToolResult).toHaveBeenCalledWith(expect.stringContaining("Successfully created new task")) | ||
| }) | ||
|
|
||
| it("should not un-escape single escaped \@", async () => { | ||
| const block: ToolUse = { | ||
| type: "tool_use", // Add required 'type' property | ||
| name: "new_task", // Correct property name | ||
| params: { | ||
| mode: "code", | ||
| message: "This is already unescaped: \\@file1.txt", | ||
| }, | ||
| partial: false, | ||
| } | ||
|
|
||
| await newTaskTool( | ||
| mockCline as any, | ||
| block, | ||
| mockAskApproval, // Now correctly typed | ||
| mockHandleError, | ||
| mockPushToolResult, | ||
| mockRemoveClosingTag, | ||
| ) | ||
|
|
||
| expect(mockInitClineWithTask).toHaveBeenCalledWith( | ||
| "This is already unescaped: \\@file1.txt", // Expected: \@ remains \@ | ||
| undefined, | ||
| mockCline, | ||
| ) | ||
| }) | ||
|
|
||
| it("should not un-escape non-escaped @", async () => { | ||
| const block: ToolUse = { | ||
| type: "tool_use", // Add required 'type' property | ||
| name: "new_task", // Correct property name | ||
| params: { | ||
| mode: "code", | ||
| message: "A normal mention @file1.txt", | ||
| }, | ||
| partial: false, | ||
| } | ||
|
|
||
| await newTaskTool( | ||
| mockCline as any, | ||
| block, | ||
| mockAskApproval, // Now correctly typed | ||
| mockHandleError, | ||
| mockPushToolResult, | ||
| mockRemoveClosingTag, | ||
| ) | ||
|
|
||
| expect(mockInitClineWithTask).toHaveBeenCalledWith( | ||
| "A normal mention @file1.txt", // Expected: @ remains @ | ||
| undefined, | ||
| mockCline, | ||
| ) | ||
| }) | ||
|
|
||
| it("should handle mixed escaping scenarios", async () => { | ||
| const block: ToolUse = { | ||
| type: "tool_use", // Add required 'type' property | ||
| name: "new_task", // Correct property name | ||
| params: { | ||
| mode: "code", | ||
| message: "Mix: @file0.txt, \\@file1.txt, \\\\@file2.txt, \\\\\\\\@file3.txt", | ||
| }, | ||
| partial: false, | ||
| } | ||
|
|
||
| await newTaskTool( | ||
| mockCline as any, | ||
| block, | ||
| mockAskApproval, // Now correctly typed | ||
| mockHandleError, | ||
| mockPushToolResult, | ||
| mockRemoveClosingTag, | ||
| ) | ||
|
|
||
| expect(mockInitClineWithTask).toHaveBeenCalledWith( | ||
| "Mix: @file0.txt, \\@file1.txt, \\@file2.txt, \\\\\\@file3.txt", // Unit Test Expectation: @->@, \@->\@, \\@->\@, \\\\@->\\\\@ | ||
| undefined, | ||
| mockCline, | ||
| ) | ||
| }) | ||
|
|
||
| // Add more tests for error handling (missing params, invalid mode, approval denied) if needed | ||
| }) | ||
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding additional tests for error handling scenarios such as missing
modeormessage, invalid mode, and approval denial. This would improve test coverage and ensure robust error handling as per our standards.This comment was generated because it violated the following rules: mrule_oAUXVfj5l9XxF01R and mrule_OR1S8PRRHcvbdFib.