77 * - sessionStorage (anonymous/auto-login shareable playground)
88 */
99
10+ import { renderHook } from "@testing-library/react" ;
11+
1012const FLOW_ID = "virtual-flow-id-123" ;
1113const SOURCE_FLOW_ID = "real-flow-id-456" ;
1214const MOCK_MESSAGES = [
@@ -51,6 +53,7 @@ jest.mock("@/modals/IOModal/helpers/playground-auth", () => ({
5153} ) ) ;
5254
5355const mockApiGet = jest . fn ( ) ;
56+ const mockQueryKeys : unknown [ ] = [ ] ;
5457jest . mock ( "@/controllers/API/api" , ( ) => ( {
5558 api : { get : ( ...args : unknown [ ] ) => mockApiGet ( ...args ) } ,
5659} ) ) ;
@@ -61,12 +64,13 @@ jest.mock("@/controllers/API/helpers/constants", () => ({
6164
6265jest . mock ( "@/utils/utils" , ( ) => ( {
6366 extractColumnsFromRows : jest . fn ( ( ) => [ ] ) ,
64- prepareSessionIdForAPI : ( id : string ) => id ,
67+ prepareSessionIdForAPI : ( id : string ) => encodeURIComponent ( id ) ,
6568} ) ) ;
6669
6770jest . mock ( "@/controllers/API/services/request-processor" , ( ) => ( {
6871 UseRequestProcessor : jest . fn ( ( ) => ( {
69- query : jest . fn ( ( _key : unknown , fn : ( ) => Promise < unknown > ) => {
72+ query : jest . fn ( ( key : unknown , fn : ( ) => Promise < unknown > ) => {
73+ mockQueryKeys . push ( key ) ;
7074 void fn ( ) ;
7175 return { data : null , isFetched : false , refetch : jest . fn ( ) } ;
7276 } ) ,
@@ -76,7 +80,7 @@ jest.mock("@/controllers/API/services/request-processor", () => ({
7680import { isAuthenticatedPlayground } from "@/modals/IOModal/helpers/playground-auth" ;
7781import useFlowStore from "@/stores/flowStore" ;
7882import { useMessagesStore } from "@/stores/messagesStore" ;
79- import { useGetMessagesQuery } from "../use-get-messages" ;
83+ import { getMessages , useGetMessagesQuery } from "../use-get-messages" ;
8084
8185const mockFlowStore = useFlowStore as unknown as { getState : jest . Mock } ;
8286const mockIsAuth = isAuthenticatedPlayground as jest . MockedFunction <
@@ -89,6 +93,7 @@ describe("useGetMessagesQuery - Routing Logic", () => {
8993 mockApiGet . mockResolvedValue ( { data : MOCK_MESSAGES } ) ;
9094 mockFlowStore . getState . mockReturnValue ( { playgroundPage : false } ) ;
9195 mockIsAuth . mockReturnValue ( false ) ;
96+ mockQueryKeys . length = 0 ;
9297 window . sessionStorage . clear ( ) ;
9398 } ) ;
9499
@@ -172,6 +177,86 @@ describe("useGetMessagesQuery - Routing Logic", () => {
172177 expect ( mockApiGet ) . not . toHaveBeenCalled ( ) ;
173178 } ) ;
174179
180+ it ( "should_filter_order_and_paginate_anonymous_sessionStorage" , async ( ) => {
181+ mockFlowStore . getState . mockReturnValue ( { playgroundPage : true } ) ;
182+ mockIsAuth . mockReturnValue ( false ) ;
183+ window . sessionStorage . setItem (
184+ FLOW_ID ,
185+ JSON . stringify ( [
186+ {
187+ id : "oldest" ,
188+ session_id : "session one" ,
189+ timestamp : "2026-01-01T00:00:00Z" ,
190+ } ,
191+ {
192+ id : "other-session" ,
193+ session_id : "session-two" ,
194+ timestamp : "2026-01-01T00:03:00Z" ,
195+ } ,
196+ {
197+ id : "newest" ,
198+ session_id : "session one" ,
199+ timestamp : "2026-01-01T00:02:00Z" ,
200+ } ,
201+ {
202+ id : "middle" ,
203+ session_id : "session one" ,
204+ timestamp : "2026-01-01T00:01:00Z" ,
205+ } ,
206+ ] ) ,
207+ ) ;
208+
209+ const response = await getMessages ( FLOW_ID , {
210+ session_id : "session one" ,
211+ order : "DESC" ,
212+ offset : 1 ,
213+ limit : 1 ,
214+ } ) ;
215+
216+ expect ( response . data . map ( ( message ) => message . id ) ) . toEqual ( [ "middle" ] ) ;
217+ expect ( mockApiGet ) . not . toHaveBeenCalled ( ) ;
218+ } ) ;
219+
220+ it ( "uses a distinct query key when request params change" , ( ) => {
221+ const { rerender } = renderHook (
222+ ( { sessionId } ) =>
223+ useGetMessagesQuery (
224+ {
225+ id : FLOW_ID ,
226+ mode : "union" ,
227+ excludedFields : [ "properties" ] ,
228+ params : { session_id : sessionId , limit : 20 , order : "DESC" } ,
229+ } ,
230+ { } ,
231+ ) ,
232+ { initialProps : { sessionId : "session-1" } } ,
233+ ) ;
234+ const firstKey = mockQueryKeys [ mockQueryKeys . length - 1 ] ;
235+
236+ rerender ( { sessionId : "session-2" } ) ;
237+ const secondKey = mockQueryKeys [ mockQueryKeys . length - 1 ] ;
238+
239+ expect ( firstKey ) . toEqual ( [
240+ "useGetMessagesQuery" ,
241+ {
242+ id : FLOW_ID ,
243+ mode : "union" ,
244+ excludedFields : [ "properties" ] ,
245+ params : { session_id : "session-1" , limit : 20 , order : "DESC" } ,
246+ } ,
247+ ] ) ;
248+ expect ( secondKey ) . toEqual ( [
249+ "useGetMessagesQuery" ,
250+ {
251+ id : FLOW_ID ,
252+ mode : "union" ,
253+ excludedFields : [ "properties" ] ,
254+ params : { session_id : "session-2" , limit : 20 , order : "DESC" } ,
255+ } ,
256+ ] ) ;
257+ expect ( secondKey ) . not . toEqual ( firstKey ) ;
258+ } ) ;
259+
175260 it ( "should_sync_messages_to_zustand_store_when_authenticated_playground" , async ( ) => {
176261 mockFlowStore . getState . mockReturnValue ( { playgroundPage : true } ) ;
177262 mockIsAuth . mockReturnValue ( true ) ;
0 commit comments