@@ -46,6 +46,7 @@ import {
4646 invalidateChatListQueries ,
4747 invalidateChatMessages ,
4848 invalidateChatPrompts ,
49+ invalidateChatSearches ,
4950 invalidateChatsByWorkspace ,
5051 mergeWatchedChatIntoCaches ,
5152 mergeWatchedChatSummary ,
@@ -945,6 +946,7 @@ describe("mutation invalidation scope", () => {
945946 const queryClient = createTestQueryClient ( ) ;
946947 const chatId = "chat-1" ;
947948 seedAllActiveQueries ( queryClient , chatId ) ;
949+ queryClient . setQueryData ( chatSearch ( { q : "alpha" } ) . queryKey , [ ] ) ;
948950
949951 const mutation = createChatMessage ( queryClient , chatId ) ;
950952 await mutation . onSuccess ?.( ) ;
@@ -956,6 +958,14 @@ describe("mutation invalidation scope", () => {
956958 `${ label } should NOT be invalidated by createChatMessage` ,
957959 ) . not . toBe ( true ) ;
958960 }
961+ // The send path invalidates searches through
962+ // useChatStore.upsertCacheMessages; doing it here too would
963+ // double-invalidate every send.
964+ expect (
965+ queryClient . getQueryState ( chatSearch ( { q : "alpha" } ) . queryKey )
966+ ?. isInvalidated ,
967+ "chat searches should NOT be invalidated by createChatMessage" ,
968+ ) . not . toBe ( true ) ;
959969 } ) ;
960970
961971 it ( "createChatMessage invalidates debug runs and chat detail, not messages" , async ( ) => {
@@ -1550,6 +1560,51 @@ describe("mutation invalidation scope", () => {
15501560 "chat list should NOT be invalidated" ,
15511561 ) . not . toBe ( true ) ;
15521562 } ) ;
1563+
1564+ it . each < {
1565+ name : string ;
1566+ settle : ( queryClient : QueryClient ) => unknown ;
1567+ } > ( [
1568+ {
1569+ name : "archiveChat onSettled" ,
1570+ settle : ( queryClient ) =>
1571+ archiveChat ( queryClient ) . onSettled ( undefined , undefined , "chat-1" ) ,
1572+ } ,
1573+ {
1574+ name : "unarchiveChat onSettled" ,
1575+ settle : ( queryClient ) =>
1576+ unarchiveChat ( queryClient ) . onSettled ( undefined , undefined , "chat-1" ) ,
1577+ } ,
1578+ {
1579+ name : "updateChatTitle onSettled" ,
1580+ settle : ( queryClient ) =>
1581+ updateChatTitle ( queryClient ) . onSettled ( undefined , undefined , {
1582+ chatId : "chat-1" ,
1583+ title : "New" ,
1584+ } ) ,
1585+ } ,
1586+ {
1587+ name : "editChatMessage onSettled" ,
1588+ settle : ( queryClient ) =>
1589+ editChatMessage ( queryClient , "chat-1" ) . onSettled ( ) ,
1590+ } ,
1591+ {
1592+ name : "createChat onSuccess" ,
1593+ settle : ( queryClient ) => createChat ( queryClient ) . onSuccess ( ) ,
1594+ } ,
1595+ ] ) ( "$name invalidates chat searches" , async ( { settle } ) => {
1596+ const queryClient = createTestQueryClient ( ) ;
1597+ queryClient . setQueryData ( chatSearch ( { q : "alpha" } ) . queryKey , [ ] ) ;
1598+
1599+ settle ( queryClient ) ;
1600+ await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ;
1601+
1602+ expect (
1603+ queryClient . getQueryState ( chatSearch ( { q : "alpha" } ) . queryKey )
1604+ ?. isInvalidated ,
1605+ "chat search entry should be invalidated" ,
1606+ ) . toBe ( true ) ;
1607+ } ) ;
15531608} ) ;
15541609
15551610describe ( "chatListKey shape" , ( ) => {
@@ -3057,6 +3112,40 @@ describe("semantic cache operations: prefix invalidations", () => {
30573112 "messages entry should NOT be invalidated" ,
30583113 ) . not . toBe ( true ) ;
30593114 } ) ;
3115+
3116+ it ( "invalidateChatSearches touches every search entry and nothing outside the family" , async ( ) => {
3117+ const queryClient = createTestQueryClient ( ) ;
3118+ queryClient . setQueryData ( chatSearch ( { q : "alpha" } ) . queryKey , [ ] ) ;
3119+ queryClient . setQueryData ( chatSearch ( { q : "beta" } ) . queryKey , [ ] ) ;
3120+ seedInfiniteChats ( queryClient , [ makeChat ( "chat-1" ) ] ) ;
3121+ queryClient . setQueryData ( chatsByWorkspace ( [ "ws-1" ] ) . queryKey , { } ) ;
3122+ queryClient . setQueryData ( chatEntityKey ( "chat-1" ) , makeChat ( "chat-1" ) ) ;
3123+ queryClient . setQueryData ( chatMessagesKey ( "chat-1" ) , [ ] ) ;
3124+ queryClient . setQueryData ( chatCostTreeKey ( "chat-1" ) , { } ) ;
3125+
3126+ await invalidateChatSearches ( queryClient ) ;
3127+
3128+ expect (
3129+ queryClient . getQueryState ( chatSearch ( { q : "alpha" } ) . queryKey )
3130+ ?. isInvalidated ,
3131+ ) . toBe ( true ) ;
3132+ expect (
3133+ queryClient . getQueryState ( chatSearch ( { q : "beta" } ) . queryKey )
3134+ ?. isInvalidated ,
3135+ ) . toBe ( true ) ;
3136+ for ( const [ label , key ] of [
3137+ [ "chat list" , infiniteChatsTestKey ] ,
3138+ [ "by-workspace" , chatsByWorkspace ( [ "ws-1" ] ) . queryKey ] ,
3139+ [ "chat detail" , chatEntityKey ( "chat-1" ) ] ,
3140+ [ "messages" , chatMessagesKey ( "chat-1" ) ] ,
3141+ [ "cost tree" , chatCostTreeKey ( "chat-1" ) ] ,
3142+ ] as const ) {
3143+ expect (
3144+ queryClient . getQueryState ( key ) ?. isInvalidated ,
3145+ `${ label } entry should NOT be invalidated` ,
3146+ ) . not . toBe ( true ) ;
3147+ }
3148+ } ) ;
30603149} ) ;
30613150
30623151describe ( "semantic cache operations: cancellation" , ( ) => {
0 commit comments