Skip to content

Commit a18be7d

Browse files
committed
fix: update tests to match new CommandPatternSelector interface
- Fixed CommandExecution tests to use pattern selector correctly - Fixed CommandPatternSelector test for edited pattern values - Updated command-parser to properly stop at flags, paths, and file extensions - Fixed linting error by removing unused patterns parameter
1 parent 19c2c49 commit a18be7d

File tree

3 files changed

+60
-33
lines changed

3 files changed

+60
-33
lines changed

webview-ui/src/components/chat/__tests__/CommandExecution.spec.tsx

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ vi.mock("../../common/CodeBlock", () => ({
2222
}))
2323

2424
vi.mock("../CommandPatternSelector", () => ({
25-
CommandPatternSelector: ({ command, onAllowCommandChange, onDenyCommandChange }: any) => (
25+
CommandPatternSelector: ({ command, onAllowPatternChange, onDenyPatternChange }: any) => (
2626
<div data-testid="command-pattern-selector">
2727
<span>{command}</span>
28-
<button onClick={() => onAllowCommandChange(command)}>Allow {command}</button>
29-
<button onClick={() => onDenyCommandChange(command)}>Deny {command}</button>
28+
<button onClick={() => onAllowPatternChange(command)}>Allow {command}</button>
29+
<button onClick={() => onDenyPatternChange(command)}>Deny {command}</button>
3030
</div>
3131
),
3232
}))
@@ -92,7 +92,9 @@ describe("CommandExecution", () => {
9292
)
9393

9494
expect(screen.getByTestId("command-pattern-selector")).toBeInTheDocument()
95-
expect(screen.getByText("npm install express")).toBeInTheDocument()
95+
// Check that the command is shown in the pattern selector
96+
const selector = screen.getByTestId("command-pattern-selector")
97+
expect(selector).toHaveTextContent("npm install express")
9698
})
9799

98100
it("should handle allow command change", () => {
@@ -206,9 +208,10 @@ Suggested patterns: npm, npm install, npm run`
206208
expect(codeBlocks[0]).toHaveTextContent("npm install")
207209
expect(codeBlocks[1]).toHaveTextContent("Suggested patterns: npm, npm install, npm run")
208210

209-
expect(screen.getByTestId("command-pattern-selector")).toBeInTheDocument()
210-
// Should show the full command
211-
expect(screen.getByText("npm install")).toBeInTheDocument()
211+
const selector = screen.getByTestId("command-pattern-selector")
212+
expect(selector).toBeInTheDocument()
213+
// Should show the full command in the selector
214+
expect(selector).toHaveTextContent("npm install")
212215
})
213216

214217
it("should handle commands with pipes", () => {
@@ -218,8 +221,9 @@ Suggested patterns: npm, npm install, npm run`
218221
</ExtensionStateWrapper>,
219222
)
220223

221-
expect(screen.getByTestId("command-pattern-selector")).toBeInTheDocument()
222-
expect(screen.getByText("ls -la | grep test")).toBeInTheDocument()
224+
const selector = screen.getByTestId("command-pattern-selector")
225+
expect(selector).toBeInTheDocument()
226+
expect(selector).toHaveTextContent("ls -la | grep test")
223227
})
224228

225229
it("should handle commands with && operator", () => {
@@ -229,8 +233,9 @@ Suggested patterns: npm, npm install, npm run`
229233
</ExtensionStateWrapper>,
230234
)
231235

232-
expect(screen.getByTestId("command-pattern-selector")).toBeInTheDocument()
233-
expect(screen.getByText("npm install && npm test")).toBeInTheDocument()
236+
const selector = screen.getByTestId("command-pattern-selector")
237+
expect(selector).toBeInTheDocument()
238+
expect(selector).toHaveTextContent("npm install && npm test")
234239
})
235240

236241
it("should not show pattern selector for empty commands", () => {
@@ -316,7 +321,7 @@ Output here`
316321

317322
const selector = screen.getByTestId("command-pattern-selector")
318323
expect(selector).toBeInTheDocument()
319-
expect(screen.getByText("npm install && npm test || echo 'failed'")).toBeInTheDocument()
324+
expect(selector).toHaveTextContent("npm install && npm test || echo 'failed'")
320325
})
321326

322327
it("should handle commands with output", () => {
@@ -338,8 +343,8 @@ Other output here`
338343

339344
const selector = screen.getByTestId("command-pattern-selector")
340345
expect(selector).toBeInTheDocument()
341-
// Should show the command
342-
expect(screen.getByText("npm install")).toBeInTheDocument()
346+
// Should show the command in the selector
347+
expect(selector).toHaveTextContent("npm install")
343348
})
344349

345350
it("should handle commands with subshells", () => {
@@ -351,7 +356,7 @@ Other output here`
351356

352357
const selector = screen.getByTestId("command-pattern-selector")
353358
expect(selector).toBeInTheDocument()
354-
expect(screen.getByText("echo $(whoami) && git status")).toBeInTheDocument()
359+
expect(selector).toHaveTextContent("echo $(whoami) && git status")
355360
})
356361

357362
it("should handle commands with backtick subshells", () => {
@@ -363,7 +368,7 @@ Other output here`
363368

364369
const selector = screen.getByTestId("command-pattern-selector")
365370
expect(selector).toBeInTheDocument()
366-
expect(screen.getByText("git commit -m `date`")).toBeInTheDocument()
371+
expect(selector).toHaveTextContent("git commit -m `date`")
367372
})
368373

369374
it("should handle commands with special characters", () => {
@@ -375,7 +380,7 @@ Other output here`
375380

376381
const selector = screen.getByTestId("command-pattern-selector")
377382
expect(selector).toBeInTheDocument()
378-
expect(screen.getByText("cd ~/projects && npm start")).toBeInTheDocument()
383+
expect(selector).toHaveTextContent("cd ~/projects && npm start")
379384
})
380385

381386
it("should handle commands with mixed content including output", () => {
@@ -398,8 +403,8 @@ Running tests...
398403

399404
const selector = screen.getByTestId("command-pattern-selector")
400405
expect(selector).toBeInTheDocument()
401-
// Should show the command
402-
expect(screen.getByText("npm test")).toBeInTheDocument()
406+
// Should show the command in the selector
407+
expect(selector).toHaveTextContent("npm test")
403408
})
404409

405410
it("should update both allowed and denied lists when commands conflict", () => {
@@ -438,8 +443,9 @@ Running tests...
438443
expect(screen.getByTestId("code-block")).toHaveTextContent("echo 'test with unclosed quote")
439444

440445
// Should show pattern selector with the full command
441-
expect(screen.getByTestId("command-pattern-selector")).toBeInTheDocument()
442-
expect(screen.getByText("echo 'test with unclosed quote")).toBeInTheDocument()
446+
const selector = screen.getByTestId("command-pattern-selector")
447+
expect(selector).toBeInTheDocument()
448+
expect(selector).toHaveTextContent("echo 'test with unclosed quote")
443449
})
444450

445451
it("should handle empty or whitespace-only commands", () => {
@@ -488,8 +494,9 @@ Without any command prefix`
488494
expect(screen.getByTestId("code-block")).toHaveTextContent("docker build .")
489495

490496
// Should show pattern selector with the full command
491-
expect(screen.getByTestId("command-pattern-selector")).toBeInTheDocument()
492-
expect(screen.getByText("docker build .")).toBeInTheDocument()
497+
const selector = screen.getByTestId("command-pattern-selector")
498+
expect(selector).toBeInTheDocument()
499+
expect(selector).toHaveTextContent("docker build .")
493500

494501
// Verify no output is shown (since there's no Output: separator)
495502
const codeBlocks = screen.getAllByTestId("code-block")
@@ -518,8 +525,8 @@ Output:
518525
const selector = screen.getByTestId("command-pattern-selector")
519526
expect(selector).toBeInTheDocument()
520527

521-
// Should show the full command
522-
expect(screen.getByText("wc -l *.go *.java")).toBeInTheDocument()
528+
// Should show the full command in the selector
529+
expect(selector).toHaveTextContent("wc -l *.go *.java")
523530

524531
// The output should still be displayed in the code block
525532
expect(codeBlocks.length).toBeGreaterThan(1)
@@ -541,8 +548,8 @@ Output:
541548
const selector = screen.getByTestId("command-pattern-selector")
542549
expect(selector).toBeInTheDocument()
543550

544-
// Should show the full command
545-
expect(screen.getByText("wc -l *.go *.java")).toBeInTheDocument()
551+
// Should show the full command in the selector
552+
expect(selector).toHaveTextContent("wc -l *.go *.java")
546553

547554
// The output should still be displayed in the code block
548555
const codeBlocks = screen.getAllByTestId("code-block")

webview-ui/src/components/chat/__tests__/CommandPatternSelector.spec.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,15 @@ describe("CommandPatternSelector", () => {
228228
const input = screen.getByDisplayValue("npm install express") as HTMLInputElement
229229
fireEvent.change(input, { target: { value: "npm install react" } })
230230

231-
// Press Enter to confirm edit
232-
fireEvent.keyDown(input, { key: "Enter" })
231+
// Don't press Enter or blur - just click the button while still editing
232+
// This simulates the user clicking the button while the input is still focused
233233

234-
// Click the allow button
235-
const fullCommandPattern = screen.getByText("npm install react").closest(".ml-5")
236-
const allowButton = fullCommandPattern?.querySelector('button[aria-label*="addToAllowed"]')
234+
// Find the allow button in the same row as the input
235+
const patternRow = input.closest(".ml-5")
236+
const allowButton = patternRow?.querySelector('button[aria-label*="addToAllowed"]')
237+
expect(allowButton).toBeInTheDocument()
238+
239+
// Click the allow button - this should use the current edited value
237240
fireEvent.click(allowButton!)
238241

239242
// Check that the callback was called with the edited pattern

webview-ui/src/utils/command-parser.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,24 @@ function extractFromTokens(tokens: string[], patterns: Set<string>): void {
5454
patterns.add(pattern)
5555

5656
for (let i = 1; i < Math.min(tokens.length, 3); i++) {
57-
pattern += ` ${tokens[i]}`
57+
const token = tokens[i]
58+
59+
// Stop at flags (starting with -)
60+
if (token.startsWith("-")) break
61+
62+
// Stop at paths (starting with / or ~)
63+
if (token.startsWith("/") || token.startsWith("~")) break
64+
65+
// Stop at file extensions
66+
if (token.includes(".") && /\.\w+$/.test(token)) break
67+
68+
// Stop at colons (like image:tag)
69+
if (token.includes(":")) break
70+
71+
// Stop at dots (like . for current directory)
72+
if (token === ".") break
73+
74+
pattern += ` ${token}`
5875
patterns.add(pattern)
5976
}
6077
}

0 commit comments

Comments
 (0)