@@ -102,73 +102,81 @@ vi.mock("./sent-message-cache.js", () => ({
102102 clearSentMessageCache : vi . fn ( ) ,
103103} ) ) ;
104104
105- export const useSpy : MockFn < ( arg : unknown ) => void > = vi . fn ( ) ;
106- export const middlewareUseSpy : AnyMock = vi . fn ( ) ;
107- export const onSpy : AnyMock = vi . fn ( ) ;
108- export const stopSpy : AnyMock = vi . fn ( ) ;
109- export const commandSpy : AnyMock = vi . fn ( ) ;
110- export const botCtorSpy : AnyMock = vi . fn ( ) ;
111- export const answerCallbackQuerySpy : AnyAsyncMock = vi . fn ( async ( ) => undefined ) ;
112- export const sendChatActionSpy : AnyMock = vi . fn ( ) ;
113- export const editMessageTextSpy : AnyAsyncMock = vi . fn ( async ( ) => ( { message_id : 88 } ) ) ;
114- export const editMessageReplyMarkupSpy : AnyAsyncMock = vi . fn ( async ( ) => ( { message_id : 88 } ) ) ;
115- export const sendMessageDraftSpy : AnyAsyncMock = vi . fn ( async ( ) => true ) ;
116- export const setMessageReactionSpy : AnyAsyncMock = vi . fn ( async ( ) => undefined ) ;
117- export const setMyCommandsSpy : AnyAsyncMock = vi . fn ( async ( ) => undefined ) ;
118- export const getMeSpy : AnyAsyncMock = vi . fn ( async ( ) => ( {
119- username : "openclaw_bot" ,
120- has_topics_enabled : true ,
105+ // All spy variables used inside vi.mock("grammy", ...) must be created via
106+ // vi.hoisted() so they are available when the hoisted factory runs, regardless
107+ // of module evaluation order across different test files.
108+ const grammySpies = vi . hoisted ( ( ) => ( {
109+ useSpy : vi . fn ( ) as MockFn < ( arg : unknown ) => void > ,
110+ middlewareUseSpy : vi . fn ( ) as AnyMock ,
111+ onSpy : vi . fn ( ) as AnyMock ,
112+ stopSpy : vi . fn ( ) as AnyMock ,
113+ commandSpy : vi . fn ( ) as AnyMock ,
114+ botCtorSpy : vi . fn ( ) as AnyMock ,
115+ answerCallbackQuerySpy : vi . fn ( async ( ) => undefined ) as AnyAsyncMock ,
116+ sendChatActionSpy : vi . fn ( ) as AnyMock ,
117+ editMessageTextSpy : vi . fn ( async ( ) => ( { message_id : 88 } ) ) as AnyAsyncMock ,
118+ editMessageReplyMarkupSpy : vi . fn ( async ( ) => ( { message_id : 88 } ) ) as AnyAsyncMock ,
119+ sendMessageDraftSpy : vi . fn ( async ( ) => true ) as AnyAsyncMock ,
120+ setMessageReactionSpy : vi . fn ( async ( ) => undefined ) as AnyAsyncMock ,
121+ setMyCommandsSpy : vi . fn ( async ( ) => undefined ) as AnyAsyncMock ,
122+ getMeSpy : vi . fn ( async ( ) => ( {
123+ username : "openclaw_bot" ,
124+ has_topics_enabled : true ,
125+ } ) ) as AnyAsyncMock ,
126+ sendMessageSpy : vi . fn ( async ( ) => ( { message_id : 77 } ) ) as AnyAsyncMock ,
127+ sendAnimationSpy : vi . fn ( async ( ) => ( { message_id : 78 } ) ) as AnyAsyncMock ,
128+ sendPhotoSpy : vi . fn ( async ( ) => ( { message_id : 79 } ) ) as AnyAsyncMock ,
129+ getFileSpy : vi . fn ( async ( ) => ( { file_path : "media/file.jpg" } ) ) as AnyAsyncMock ,
121130} ) ) ;
122- export const sendMessageSpy : AnyAsyncMock = vi . fn ( async ( ) => ( { message_id : 77 } ) ) ;
123- export const sendAnimationSpy : AnyAsyncMock = vi . fn ( async ( ) => ( { message_id : 78 } ) ) ;
124- export const sendPhotoSpy : AnyAsyncMock = vi . fn ( async ( ) => ( { message_id : 79 } ) ) ;
125- export const getFileSpy : AnyAsyncMock = vi . fn ( async ( ) => ( { file_path : "media/file.jpg" } ) ) ;
126-
127- type ApiStub = {
128- config : { use : ( arg : unknown ) => void } ;
129- answerCallbackQuery : typeof answerCallbackQuerySpy ;
130- sendChatAction : typeof sendChatActionSpy ;
131- editMessageText : typeof editMessageTextSpy ;
132- editMessageReplyMarkup : typeof editMessageReplyMarkupSpy ;
133- sendMessageDraft : typeof sendMessageDraftSpy ;
134- setMessageReaction : typeof setMessageReactionSpy ;
135- setMyCommands : typeof setMyCommandsSpy ;
136- getMe : typeof getMeSpy ;
137- sendMessage : typeof sendMessageSpy ;
138- sendAnimation : typeof sendAnimationSpy ;
139- sendPhoto : typeof sendPhotoSpy ;
140- getFile : typeof getFileSpy ;
141- } ;
142131
143- const apiStub : ApiStub = {
144- config : { use : useSpy } ,
145- answerCallbackQuery : answerCallbackQuerySpy ,
146- sendChatAction : sendChatActionSpy ,
147- editMessageText : editMessageTextSpy ,
148- editMessageReplyMarkup : editMessageReplyMarkupSpy ,
149- sendMessageDraft : sendMessageDraftSpy ,
150- setMessageReaction : setMessageReactionSpy ,
151- setMyCommands : setMyCommandsSpy ,
152- getMe : getMeSpy ,
153- sendMessage : sendMessageSpy ,
154- sendAnimation : sendAnimationSpy ,
155- sendPhoto : sendPhotoSpy ,
156- getFile : getFileSpy ,
157- } ;
132+ export const {
133+ useSpy,
134+ middlewareUseSpy,
135+ onSpy,
136+ stopSpy,
137+ commandSpy,
138+ botCtorSpy,
139+ answerCallbackQuerySpy,
140+ sendChatActionSpy,
141+ editMessageTextSpy,
142+ editMessageReplyMarkupSpy,
143+ sendMessageDraftSpy,
144+ setMessageReactionSpy,
145+ setMyCommandsSpy,
146+ getMeSpy,
147+ sendMessageSpy,
148+ sendAnimationSpy,
149+ sendPhotoSpy,
150+ getFileSpy,
151+ } = grammySpies ;
158152
159153vi . mock ( "grammy" , ( ) => ( {
160154 Bot : class {
161- api = apiStub ;
162- use = middlewareUseSpy ;
163- on = onSpy ;
164- stop = stopSpy ;
165- command = commandSpy ;
155+ api = {
156+ config : { use : grammySpies . useSpy } ,
157+ answerCallbackQuery : grammySpies . answerCallbackQuerySpy ,
158+ sendChatAction : grammySpies . sendChatActionSpy ,
159+ editMessageText : grammySpies . editMessageTextSpy ,
160+ editMessageReplyMarkup : grammySpies . editMessageReplyMarkupSpy ,
161+ sendMessageDraft : grammySpies . sendMessageDraftSpy ,
162+ setMessageReaction : grammySpies . setMessageReactionSpy ,
163+ setMyCommands : grammySpies . setMyCommandsSpy ,
164+ getMe : grammySpies . getMeSpy ,
165+ sendMessage : grammySpies . sendMessageSpy ,
166+ sendAnimation : grammySpies . sendAnimationSpy ,
167+ sendPhoto : grammySpies . sendPhotoSpy ,
168+ getFile : grammySpies . getFileSpy ,
169+ } ;
170+ use = grammySpies . middlewareUseSpy ;
171+ on = grammySpies . onSpy ;
172+ stop = grammySpies . stopSpy ;
173+ command = grammySpies . commandSpy ;
166174 catch = vi . fn ( ) ;
167175 constructor (
168176 public token : string ,
169177 public options ?: { client ?: { fetch ?: typeof fetch } } ,
170178 ) {
171- botCtorSpy ( token , options ) ;
179+ grammySpies . botCtorSpy ( token , options ) ;
172180 }
173181 } ,
174182 InputFile : class { } ,
0 commit comments