@@ -9,11 +9,22 @@ import type { WebhookTarget } from "./monitor-types.js";
99import type { GoogleChatEvent } from "./types.js" ;
1010
1111const resolveApprovalOverGateway = vi . hoisted ( ( ) => vi . fn ( ) ) ;
12+ const isApprovalNotFoundError = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1213
1314vi . mock ( "openclaw/plugin-sdk/approval-gateway-runtime" , ( ) => ( {
1415 resolveApprovalOverGateway,
1516} ) ) ;
1617
18+ vi . mock ( "openclaw/plugin-sdk/error-runtime" , ( ) => ( {
19+ isApprovalNotFoundError,
20+ } ) ) ;
21+
22+ function createApprovalNotFoundError ( ) : Error {
23+ const err = new Error ( "unknown or expired approval id" ) ;
24+ ( err as { gatewayCode ?: string } ) . gatewayCode = "APPROVAL_NOT_FOUND" ;
25+ return err ;
26+ }
27+
1728function createTarget ( ) : WebhookTarget {
1829 return {
1930 account : {
@@ -276,4 +287,47 @@ describe("maybeHandleGoogleChatApprovalCardClick", () => {
276287
277288 expect ( resolveApprovalOverGateway ) . toHaveBeenCalledTimes ( 2 ) ;
278289 } ) ;
290+
291+ it ( "consumes the token when gateway returns APPROVAL_NOT_FOUND" , async ( ) => {
292+ registerGoogleChatApprovalCardBinding ( {
293+ token : "token-not-found" ,
294+ accountId : "default" ,
295+ approvalId : "approval-expired" ,
296+ approvalKind : "exec" ,
297+ decision : "allow-once" ,
298+ allowedDecisions : [ "allow-once" , "deny" ] ,
299+ spaceName : "spaces/AAA" ,
300+ messageName : "spaces/AAA/messages/msg-1" ,
301+ expiresAtMs : Date . now ( ) + 60_000 ,
302+ } ) ;
303+
304+ const notFoundError = createApprovalNotFoundError ( ) ;
305+ resolveApprovalOverGateway . mockRejectedValueOnce ( notFoundError ) ;
306+ isApprovalNotFoundError . mockReturnValueOnce ( true ) ;
307+
308+ const target = createTarget ( ) ;
309+ await expect (
310+ maybeHandleGoogleChatApprovalCardClick ( {
311+ event : createCardClickEvent ( "token-not-found" ) ,
312+ target,
313+ } ) ,
314+ ) . resolves . toBe ( true ) ;
315+
316+ // Token should be consumed, not released for retry
317+ expect ( target . runtime . log ) . toHaveBeenCalledWith (
318+ expect . stringContaining ( "googlechat approval expired/stale" ) ,
319+ ) ;
320+
321+ // Subsequent click should be ignored without calling gateway
322+ isApprovalNotFoundError . mockReturnValue ( false ) ;
323+ await expect (
324+ maybeHandleGoogleChatApprovalCardClick ( {
325+ event : createCardClickEvent ( "token-not-found" ) ,
326+ target : createTarget ( ) ,
327+ } ) ,
328+ ) . resolves . toBe ( true ) ;
329+
330+ // Gateway should only have been called once
331+ expect ( resolveApprovalOverGateway ) . toHaveBeenCalledTimes ( 1 ) ;
332+ } ) ;
279333} ) ;
0 commit comments