11import type { FrontendWorkspaceMetadata } from "@/common/types/workspace" ;
2- import type { WorkspaceChatMessage } from "@/common/orpc/ types" ;
2+ import type { WorkspaceChatMessage } from "@/common/types/ipc " ;
33import { createMuxMessage } from "@/common/types/message" ;
44import type { BashToolResult } from "@/common/types/tools" ;
55import { DEFAULT_RUNTIME_CONFIG } from "@/common/constants/workspace" ;
@@ -43,14 +43,8 @@ global.window = mockWindow as unknown as Window & typeof globalThis;
4343// Mock dispatchEvent
4444global . window . dispatchEvent = jest . fn ( ) ;
4545
46- // Helper to get IPC callback in a type-safe way
47- function getOnChatCallback < T = { type : string } > ( ) : ( data : T ) => void {
48- const mock = mockWindow . api . workspace . onChat as jest . Mock <
49- ( ) => void ,
50- [ string , ( data : T ) => void ]
51- > ;
52- return mock . mock . calls [ 0 ] [ 1 ] ;
53- }
46+ // Reference to mock for easier access
47+ const mockOnChat = mockWindow . api . workspace . onChat as jest . Mock ;
5448
5549// Helper to create and add a workspace
5650function createAndAddWorkspace (
@@ -72,36 +66,24 @@ function createAndAddWorkspace(
7266}
7367
7468// Helper to get callback from mock for pushing messages
75- let pendingMessages : WorkspaceChatMessage [ ] = [ ] ;
76- let resolvers : Array < ( msg : WorkspaceChatMessage ) => void > = [ ] ;
77-
78- function getOnChatCallback < T extends WorkspaceChatMessage > ( ) : ( msg : T ) => void {
79- return ( msg : T ) => {
80- if ( resolvers . length > 0 ) {
81- const resolver = resolvers . shift ( ) ! ;
82- resolver ( msg ) ;
83- } else {
84- pendingMessages . push ( msg ) ;
85- }
86- } ;
87- }
8869
89- // Set up mock to use push-based message queue
90- mockOnChat . mockImplementation ( async function * ( ) : AsyncGenerator <
91- WorkspaceChatMessage ,
92- void ,
93- unknown
94- > {
95- while ( true ) {
96- if ( pendingMessages . length > 0 ) {
97- yield pendingMessages . shift ( ) ! ;
98- } else {
99- const msg = await new Promise < WorkspaceChatMessage > ( ( resolve ) => {
100- resolvers . push ( resolve ) ;
101- } ) ;
102- yield msg ;
103- }
70+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
71+ function getOnChatCallback < T = any > ( ) : ( msg : T ) => void {
72+ if ( ! currentChatCallback ) {
73+ throw new Error ( "No chat callback registered - was addWorkspace called?" ) ;
10474 }
75+ return currentChatCallback as ( msg : T ) => void ;
76+ }
77+
78+ // Track current chat callback for tests to push messages
79+ let currentChatCallback : ( ( msg : WorkspaceChatMessage ) => void ) | null = null ;
80+
81+ // Set up mock to capture the callback and allow tests to push messages
82+ mockOnChat . mockImplementation ( ( _workspaceId : string , callback : ( msg : WorkspaceChatMessage ) => void ) => {
83+ currentChatCallback = callback ;
84+ return ( ) => {
85+ currentChatCallback = null ;
86+ } ;
10587} ) ;
10688
10789describe ( "WorkspaceStore" , ( ) => {
@@ -112,8 +94,7 @@ describe("WorkspaceStore", () => {
11294 jest . clearAllMocks ( ) ;
11395 mockExecuteBash . mockClear ( ) ;
11496 mockOnChat . mockClear ( ) ;
115- pendingMessages = [ ] ;
116- resolvers = [ ] ;
97+ currentChatCallback = null ;
11798 mockOnModelUsed = jest . fn ( ) ;
11899 store = new WorkspaceStore ( mockOnModelUsed ) ;
119100 } ) ;
@@ -279,16 +260,15 @@ describe("WorkspaceStore", () => {
279260 runtimeConfig : DEFAULT_RUNTIME_CONFIG ,
280261 } ;
281262
282- // Add workspace
263+ // Add workspace - this will set currentChatCallback
283264 store . addWorkspace ( metadata1 ) ;
284- const unsubscribeSpy = jest . fn ( ) ;
285- ( mockWindow . api . workspace . onChat as jest . Mock ) . mockReturnValue ( unsubscribeSpy ) ;
286265
287266 // Sync with empty map (removes all workspaces)
267+ // This should unsubscribe from the workspace
288268 store . syncWorkspaces ( new Map ( ) ) ;
289269
290- // Note: The unsubscribe function from the first add won't be captured
291- // since we mocked it before. In real usage, this would be called.
270+ // Verify workspace was removed by checking states
271+ expect ( store . getAllStates ( ) . size ) . toBe ( 0 ) ;
292272 } ) ;
293273 } ) ;
294274
0 commit comments