@@ -70,7 +70,7 @@ import {MARKDOWN_COMPONENT} from '../markdown/markdown.component.interface';
7070import { MockMarkdownComponent } from '../markdown/testing/mock-markdown.component' ;
7171import { SidePanelComponent } from '../side-panel/side-panel.component' ;
7272
73- import { ChatComponent } from './chat.component' ;
73+ import { ChatComponent , INITIAL_USER_INPUT_QUERY_PARAM } from './chat.component' ;
7474
7575// Mock EvalTabComponent to satisfy the required viewChild in ChatComponent
7676@Component ( {
@@ -179,10 +179,13 @@ describe('ChatComponent', () => {
179179
180180 mockDialog = jasmine . createSpyObj ( 'MatDialog' , [ 'open' ] ) ;
181181 mockSnackBar = jasmine . createSpyObj ( 'MatSnackBar' , [ 'open' ] ) ;
182- mockRouter = jasmine . createSpyObj ( 'Router' , [ 'navigate' , 'createUrlTree' ] , {
183- events : of ( new NavigationEnd ( 1 , '' , '' ) ) ,
184- } ) ;
185- mockLocation = jasmine . createSpyObj ( 'Location' , [ 'replaceState' ] ) ;
182+ mockRouter = jasmine . createSpyObj (
183+ 'Router' ,
184+ [ 'navigate' , 'createUrlTree' , 'parseUrl' , 'navigateByUrl' ] ,
185+ { events : of ( new NavigationEnd ( 1 , '' , '' ) ) } ,
186+ ) ;
187+ mockRouter . parseUrl . and . returnValue ( { queryParams : { } } as any ) ;
188+ mockLocation = jasmine . createSpyObj ( 'Location' , [ 'replaceState' , 'path' ] ) ;
186189 mockAgentBuilderService = jasmine . createSpyObj (
187190 'AgentBuilderService' , [ 'clear' , 'setLoadedAgentData' ] ) ;
188191
@@ -267,6 +270,25 @@ describe('ChatComponent', () => {
267270 expect ( component ) . toBeTruthy ( ) ;
268271 } ) ;
269272
273+ it (
274+ 'should pre-fill user input from "q" query param only when app is selected' ,
275+ fakeAsync ( ( ) => {
276+ mockAgentService . setApp ( '' ) ; // Initially no app
277+ mockActivatedRoute . snapshot !
278+ . queryParams = { [ INITIAL_USER_INPUT_QUERY_PARAM ] : 'hello' } ;
279+ mockActivatedRoute . queryParams =
280+ of ( { [ INITIAL_USER_INPUT_QUERY_PARAM ] : 'hello' } ) ;
281+ fixture = TestBed . createComponent ( ChatComponent ) ;
282+ component = fixture . componentInstance ;
283+ fixture . detectChanges ( ) ;
284+ expect ( component . userInput ) . toBe ( '' ) ; // Should be empty initially
285+
286+ mockAgentService . setApp ( TEST_APP_1_NAME ) ;
287+ tick ( ) ;
288+
289+ expect ( component . userInput ) . toBe ( 'hello' ) ; // Should be set now
290+ } ) ) ;
291+
270292 it (
271293 'should project content into adk-web-chat-container-top' , ( ) => {
272294 const hostFixture = TestBed . createComponent ( TestHostComponent ) ;
@@ -912,6 +934,35 @@ describe('ChatComponent', () => {
912934 . toContain ( TEST_MESSAGE ) ;
913935 } ) ;
914936
937+ it (
938+ 'should clear "q" query param when message is sent' ,
939+ fakeAsync ( ( ) => {
940+ mockAgentService . setApp ( '' ) ; // Initially no app
941+ mockActivatedRoute . snapshot !
942+ . queryParams = { [ INITIAL_USER_INPUT_QUERY_PARAM ] : 'hello' } ;
943+ mockActivatedRoute . queryParams =
944+ of ( { [ INITIAL_USER_INPUT_QUERY_PARAM ] : 'hello' } ) ;
945+ const urlTree = {
946+ queryParams : { [ INITIAL_USER_INPUT_QUERY_PARAM ] : 'hello' } ,
947+ } ;
948+ mockRouter . parseUrl . and . returnValue ( urlTree as any ) ;
949+ mockLocation . path . and . returnValue ( '/?q=hello' ) ;
950+ fixture = TestBed . createComponent ( ChatComponent ) ;
951+ component = fixture . componentInstance ;
952+ fixture . detectChanges ( ) ;
953+ mockAgentService . setApp ( TEST_APP_1_NAME ) ;
954+ tick ( ) ;
955+
956+ component . sendMessage ( new KeyboardEvent ( 'keydown' , { key : 'Enter' } ) ) ;
957+ tick ( ) ;
958+
959+ expect ( mockLocation . path ) . toHaveBeenCalled ( ) ;
960+ expect ( mockRouter . parseUrl ) . toHaveBeenCalledWith ( '/?q=hello' ) ;
961+ expect ( urlTree . queryParams ) . toEqual ( { } as any ) ; // q param should be
962+ // deleted.
963+ expect ( mockRouter . navigateByUrl ) . toHaveBeenCalledWith ( urlTree as any ) ;
964+ } ) ) ;
965+
915966 describe ( 'when event contains multiple text parts' , ( ) => {
916967 it (
917968 'should combine consecutive text parts into a single message' ,
0 commit comments