11// pnpm --filter @roo-code/vscode-webview test src/components/chat/__tests__/CommandExecution.spec.tsx
22
33import React from "react"
4- import { render , screen , fireEvent } from "@testing-library/react"
4+ import { render , screen , fireEvent , act } from "@testing-library/react"
55
66import { CommandExecution } from "../CommandExecution"
77import { ExtensionStateContext } from "../../../context/ExtensionStateContext"
@@ -11,6 +11,7 @@ vi.mock("react-use", () => ({
1111 useEvent : vi . fn ( ) ,
1212} ) )
1313
14+ import { useEvent } from "react-use"
1415import { vscode } from "../../../utils/vscode"
1516
1617vi . mock ( "../../../utils/vscode" , ( ) => ( {
@@ -23,6 +24,11 @@ vi.mock("../../common/CodeBlock", () => ({
2324 default : ( { source } : { source : string } ) => < div data-testid = "code-block" > { source } </ div > ,
2425} ) )
2526
27+ vi . mock ( "@src/components/ui" , async ( importOriginal ) => ( {
28+ ...( await importOriginal < typeof import ( "@src/components/ui" ) > ( ) ) ,
29+ StandardTooltip : ( { children } : { children : React . ReactNode } ) => < > { children } </ > ,
30+ } ) )
31+
2632// Mock TerminalOutput
2733vi . mock ( "../TerminalOutput" , ( ) => ( {
2834 TerminalOutput : ( { content } : { content : string } ) => < div data-testid = "terminal-output" > { content } </ div > ,
@@ -605,4 +611,75 @@ Output:
605611 expect ( terminalOutput ) . toHaveTextContent ( "0 total" )
606612 } )
607613 } )
614+
615+ describe ( "running status indicator" , ( ) => {
616+ // Since useEvent is mocked as a no-op vi.fn(), the component's onMessage
617+ // handler is recorded in mock.calls[last][1]. We invoke it directly to
618+ // simulate an incoming extension message. event.data must be an
619+ // ExtensionMessage with type "commandExecutionStatus" and text holding
620+ // the JSON-serialised CommandExecutionStatus payload.
621+ const sendStatusMessage = ( executionId : string , payload : Record < string , unknown > ) => {
622+ const mockedUseEvent = useEvent as unknown as ReturnType < typeof vi . fn >
623+ const lastCall = mockedUseEvent . mock . calls [ mockedUseEvent . mock . calls . length - 1 ]
624+ const handler = lastCall ?. [ 1 ] as ( ( e : MessageEvent ) => void ) | undefined
625+ const data = { type : "commandExecutionStatus" , text : JSON . stringify ( { executionId, ...payload } ) }
626+ handler ?.( new MessageEvent ( "message" , { data } ) )
627+ }
628+
629+ it ( "should show the pulsing dot when status is started" , async ( ) => {
630+ render (
631+ < ExtensionStateWrapper >
632+ < CommandExecution executionId = "exec-status-1" text = "npm start" />
633+ </ ExtensionStateWrapper > ,
634+ )
635+
636+ // Dot absent before any status message
637+ expect ( document . querySelector ( ".animate-pulse" ) ) . not . toBeInTheDocument ( )
638+
639+ await act ( async ( ) => {
640+ // "started" schema requires executionId, status, command (pid optional)
641+ sendStatusMessage ( "exec-status-1" , { status : "started" , command : "npm start" , pid : 1234 } )
642+ } )
643+
644+ // Pulsing dot must appear after the started status arrives
645+ expect ( document . querySelector ( ".animate-pulse" ) ) . toBeInTheDocument ( )
646+ } )
647+
648+ it ( "should not show the pulsing dot for a different executionId" , async ( ) => {
649+ render (
650+ < ExtensionStateWrapper >
651+ < CommandExecution executionId = "exec-status-2" text = "npm start" />
652+ </ ExtensionStateWrapper > ,
653+ )
654+
655+ await act ( async ( ) => {
656+ sendStatusMessage ( "other-id" , { status : "started" , command : "npm start" , pid : 9999 } )
657+ } )
658+
659+ // Dot should remain absent -- wrong execution ID
660+ expect ( document . querySelector ( ".animate-pulse" ) ) . not . toBeInTheDocument ( )
661+ } )
662+
663+ it ( "should remove the pulsing dot when status transitions to exited" , async ( ) => {
664+ render (
665+ < ExtensionStateWrapper >
666+ < CommandExecution executionId = "exec-status-3" text = "npm start" />
667+ </ ExtensionStateWrapper > ,
668+ )
669+
670+ await act ( async ( ) => {
671+ sendStatusMessage ( "exec-status-3" , { status : "started" , command : "npm start" , pid : 1234 } )
672+ } )
673+
674+ // Dot present while running
675+ expect ( document . querySelector ( ".animate-pulse" ) ) . toBeInTheDocument ( )
676+
677+ await act ( async ( ) => {
678+ sendStatusMessage ( "exec-status-3" , { status : "exited" , exitCode : 0 } )
679+ } )
680+
681+ // Pulsing dot removed after process exits
682+ expect ( document . querySelector ( ".animate-pulse" ) ) . not . toBeInTheDocument ( )
683+ } )
684+ } )
608685} )
0 commit comments