1- // Push method tests cover APNs direct/relay registrations, alert delivery,
2- // stale registration cleanup, config resolution, and error mapping.
1+ // Push method tests cover APNs direct/relay registrations, Web Push delivery
2+ // outcomes, stale registration cleanup, config resolution, and error mapping.
33
44import { expectDefined } from "@openclaw/normalization-core" ;
55import { beforeEach , describe , expect , it , vi } from "vitest" ;
@@ -24,6 +24,13 @@ vi.mock("../../infra/push-apns.js", () => ({
2424 shouldClearStoredApnsRegistration : vi . fn ( ) ,
2525} ) ) ;
2626
27+ vi . mock ( "../../infra/push-web.js" , ( ) => ( {
28+ broadcastWebPush : vi . fn ( ) ,
29+ clearWebPushSubscriptionByEndpoint : vi . fn ( ) ,
30+ registerWebPushSubscription : vi . fn ( ) ,
31+ resolveVapidKeys : vi . fn ( ) ,
32+ } ) ) ;
33+
2734import {
2835 type ApnsRegistration ,
2936 clearApnsRegistrationIfCurrent ,
@@ -34,10 +41,12 @@ import {
3441 sendApnsAlert ,
3542 shouldClearStoredApnsRegistration ,
3643} from "../../infra/push-apns.js" ;
44+ import { broadcastWebPush } from "../../infra/push-web.js" ;
3745
3846type ApnsPushResult = Awaited < ReturnType < typeof sendApnsAlert > > ;
47+ type WebPushResults = Awaited < ReturnType < typeof broadcastWebPush > > ;
3948
40- type RespondCall = [ boolean , unknown ?, { code : number ; message : string } ?] ;
49+ type RespondCall = [ boolean , unknown ?, { code : string ; message : string ; details ?: unknown } ?] ;
4150
4251const DEFAULT_DIRECT_REGISTRATION = {
4352 nodeId : "ios-node-1" ,
@@ -115,6 +124,25 @@ function createInvokeParams(params: Record<string, unknown>) {
115124 } ;
116125}
117126
127+ function createWebPushTestInvokeParams ( params : Record < string , unknown > = { } ) {
128+ const respond = vi . fn ( ) ;
129+ return {
130+ respond,
131+ invoke : async ( ) =>
132+ await expectDefined (
133+ pushHandlers [ "push.web.test" ] ,
134+ 'pushHandlers["push.web.test"] test invariant' ,
135+ ) ( {
136+ params,
137+ respond : respond as never ,
138+ context : { } as never ,
139+ client : null ,
140+ req : { type : "req" , id : "req-1" , method : "push.web.test" } ,
141+ isWebchatConnect : ( ) => false ,
142+ } ) ,
143+ } ;
144+ }
145+
118146function expectInvalidRequestResponse (
119147 respond : ReturnType < typeof vi . fn > ,
120148 expectedMessagePart : string ,
@@ -334,3 +362,69 @@ describe("push.test handler", () => {
334362 expect ( clearApnsRegistrationIfCurrent ) . not . toHaveBeenCalled ( ) ;
335363 } ) ;
336364} ) ;
365+
366+ describe ( "push.web.test handler" , ( ) => {
367+ beforeEach ( ( ) => {
368+ vi . mocked ( broadcastWebPush ) . mockReset ( ) ;
369+ } ) ;
370+
371+ it ( "returns unavailable with delivery results when every attempt fails" , async ( ) => {
372+ const results : WebPushResults = [
373+ {
374+ ok : false ,
375+ subscriptionId : "expired-subscription" ,
376+ statusCode : 410 ,
377+ error : "Gone" ,
378+ } ,
379+ {
380+ ok : false ,
381+ subscriptionId : "rejected-subscription" ,
382+ statusCode : 503 ,
383+ error : "Service Unavailable" ,
384+ } ,
385+ ] ;
386+ vi . mocked ( broadcastWebPush ) . mockResolvedValue ( results ) ;
387+
388+ const { respond, invoke } = createWebPushTestInvokeParams ( ) ;
389+ await invoke ( ) ;
390+
391+ expect ( respond ) . toHaveBeenCalledTimes ( 1 ) ;
392+ const call = firstRespondCall ( respond ) ;
393+ expect ( call ?. [ 0 ] ) . toBe ( false ) ;
394+ expect ( call ?. [ 1 ] ) . toBeUndefined ( ) ;
395+ expect ( call ?. [ 2 ] ?. code ) . toBe ( ErrorCodes . UNAVAILABLE ) ;
396+ expect ( call ?. [ 2 ] ?. details ) . toEqual ( { results } ) ;
397+ } ) ;
398+
399+ it ( "returns all delivery results when at least one attempt succeeds" , async ( ) => {
400+ const results : WebPushResults = [
401+ {
402+ ok : true ,
403+ subscriptionId : "active-subscription" ,
404+ statusCode : 201 ,
405+ } ,
406+ {
407+ ok : false ,
408+ subscriptionId : "expired-subscription" ,
409+ statusCode : 410 ,
410+ error : "Gone" ,
411+ } ,
412+ ] ;
413+ vi . mocked ( broadcastWebPush ) . mockResolvedValue ( results ) ;
414+
415+ const { respond, invoke } = createWebPushTestInvokeParams ( ) ;
416+ await invoke ( ) ;
417+
418+ expect ( respond ) . toHaveBeenCalledTimes ( 1 ) ;
419+ expect ( firstRespondCall ( respond ) ) . toEqual ( [ true , { results } , undefined ] ) ;
420+ } ) ;
421+
422+ it ( "returns invalid request when no subscriptions are registered" , async ( ) => {
423+ vi . mocked ( broadcastWebPush ) . mockResolvedValue ( [ ] ) ;
424+
425+ const { respond, invoke } = createWebPushTestInvokeParams ( ) ;
426+ await invoke ( ) ;
427+
428+ expectInvalidRequestResponse ( respond , "no web push subscriptions registered" ) ;
429+ } ) ;
430+ } ) ;
0 commit comments