33 */
44import { beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
55import type { ExecApprovalRequest , ExecApprovalResolved } from "../infra/exec-approvals.js" ;
6+ import type { PluginApprovalRequest , PluginApprovalResolved } from "../infra/plugin-approvals.js" ;
67import { createDeferred } from "../test-utils/deferred.js" ;
78
89const listDevicePairingMock = vi . fn ( ) ;
@@ -12,7 +13,10 @@ const resolveApnsAuthConfigFromEnvMock = vi.fn();
1213const resolveApnsRelayConfigFromEnvMock = vi . fn ( ) ;
1314const sendApnsExecApprovalAlertMock = vi . fn ( ) ;
1415const sendApnsExecApprovalResolvedWakeMock = vi . fn ( ) ;
16+ const sendApnsPluginApprovalAlertMock = vi . fn ( ) ;
17+ const sendApnsPluginApprovalResolvedWakeMock = vi . fn ( ) ;
1518let createExecApprovalIosPushDelivery : typeof import ( "./exec-approval-ios-push.js" ) . createExecApprovalIosPushDelivery ;
19+ let createPluginApprovalIosPushDelivery : typeof import ( "./exec-approval-ios-push.js" ) . createPluginApprovalIosPushDelivery ;
1620
1721function apnsRegistration ( nodeId = "ios-device-1" ) {
1822 return {
@@ -25,6 +29,21 @@ function apnsRegistration(nodeId = "ios-device-1") {
2529 } ;
2630}
2731
32+ function relayApnsRegistration ( nodeId = "ios-device-1" ) {
33+ return {
34+ nodeId,
35+ transport : "relay" ,
36+ relayHandle : `relay-${ nodeId } ` ,
37+ sendGrant : `grant-${ nodeId } ` ,
38+ installationId : `installation-${ nodeId } ` ,
39+ topic : "ai.openclaw.ios.test" ,
40+ environment : "sandbox" ,
41+ distribution : "official" ,
42+ relayOrigin : "https://relay.example.test" ,
43+ updatedAtMs : 1 ,
44+ } ;
45+ }
46+
2847function successfulApnsPushResult ( ) {
2948 return {
3049 ok : true ,
@@ -60,6 +79,28 @@ function approvalResolved(id: string): ExecApprovalResolved {
6079 } ;
6180}
6281
82+ function pluginApprovalRequest ( id : string ) : PluginApprovalRequest {
83+ return {
84+ id,
85+ request : {
86+ title : "Install plugin update" ,
87+ description : "Allow the plugin to update its managed package." ,
88+ severity : "warning" ,
89+ toolName : "plugins.update" ,
90+ } ,
91+ createdAtMs : 1 ,
92+ expiresAtMs : 2 ,
93+ } ;
94+ }
95+
96+ function pluginApprovalResolved ( id : string ) : PluginApprovalResolved {
97+ return {
98+ id,
99+ decision : "allow-once" ,
100+ ts : 1 ,
101+ } ;
102+ }
103+
63104function pairedIosOperator ( options : {
64105 deviceId ?: string ;
65106 publicKey ?: string ;
@@ -126,13 +167,16 @@ vi.mock("../infra/push-apns.js", () => ({
126167 resolveApnsRelayConfigFromEnv : resolveApnsRelayConfigFromEnvMock ,
127168 sendApnsExecApprovalAlert : sendApnsExecApprovalAlertMock ,
128169 sendApnsExecApprovalResolvedWake : sendApnsExecApprovalResolvedWakeMock ,
170+ sendApnsPluginApprovalAlert : sendApnsPluginApprovalAlertMock ,
171+ sendApnsPluginApprovalResolvedWake : sendApnsPluginApprovalResolvedWakeMock ,
129172 clearApnsRegistrationIfCurrent : vi . fn ( ) ,
130173 shouldClearStoredApnsRegistration : vi . fn ( ( ) => false ) ,
131174} ) ) ;
132175
133176describe ( "createExecApprovalIosPushDelivery" , ( ) => {
134177 beforeAll ( async ( ) => {
135- ( { createExecApprovalIosPushDelivery } = await import ( "./exec-approval-ios-push.js" ) ) ;
178+ ( { createExecApprovalIosPushDelivery, createPluginApprovalIosPushDelivery } =
179+ await import ( "./exec-approval-ios-push.js" ) ) ;
136180 } ) ;
137181
138182 beforeEach ( ( ) => {
@@ -153,6 +197,8 @@ describe("createExecApprovalIosPushDelivery", () => {
153197 resolveApnsRelayConfigFromEnvMock . mockReturnValue ( { ok : false , error : "unused" } ) ;
154198 sendApnsExecApprovalAlertMock . mockResolvedValue ( successfulApnsPushResult ( ) ) ;
155199 sendApnsExecApprovalResolvedWakeMock . mockResolvedValue ( successfulApnsPushResult ( ) ) ;
200+ sendApnsPluginApprovalAlertMock . mockResolvedValue ( successfulApnsPushResult ( ) ) ;
201+ sendApnsPluginApprovalResolvedWakeMock . mockResolvedValue ( successfulApnsPushResult ( ) ) ;
156202 } ) ;
157203
158204 it ( "does not target iOS devices whose active operator token lacks operator.approvals" , async ( ) => {
@@ -328,4 +374,92 @@ describe("createExecApprovalIosPushDelivery", () => {
328374 expect ( loadApnsRegistrationsMock ) . toHaveBeenCalledWith ( [ "ios-device-1" ] ) ;
329375 expect ( sendApnsExecApprovalResolvedWakeMock ) . toHaveBeenCalledTimes ( 1 ) ;
330376 } ) ;
377+
378+ describe ( "createPluginApprovalIosPushDelivery" , ( ) => {
379+ it ( "targets only paired iOS operators with approval and read scopes" , async ( ) => {
380+ mockPairedIosOperators (
381+ pairedIosOperator ( {
382+ deviceId : "ios-approved" ,
383+ scopes : [ "operator.approvals" , "operator.read" ] ,
384+ } ) ,
385+ pairedIosOperator ( {
386+ deviceId : "ios-read-only" ,
387+ scopes : [ "operator.read" ] ,
388+ } ) ,
389+ ) ;
390+
391+ const delivery = createPluginApprovalIosPushDelivery ( { log : { } } ) ;
392+ const accepted = await delivery . handleRequested ( pluginApprovalRequest ( "plugin:direct" ) ) ;
393+
394+ expect ( accepted ) . toBe ( true ) ;
395+ expect ( loadApnsRegistrationsMock ) . toHaveBeenCalledWith ( [ "ios-approved" ] ) ;
396+ expect ( sendApnsPluginApprovalAlertMock ) . toHaveBeenCalledTimes ( 1 ) ;
397+ expect ( sendApnsPluginApprovalAlertMock ) . toHaveBeenCalledWith (
398+ expect . objectContaining ( {
399+ nodeId : "ios-approved" ,
400+ approvalId : "plugin:direct" ,
401+ gatewayDeviceId : "gateway-device-1" ,
402+ title : "Install plugin update" ,
403+ description : "Allow the plugin to update its managed package." ,
404+ } ) ,
405+ ) ;
406+ } ) ;
407+
408+ it ( "uses the shared relay delivery plan for plugin alerts" , async ( ) => {
409+ const relayConfig = { baseUrl : "https://relay.example.test" , timeoutMs : 10_000 } ;
410+ mockPairedIosOperator ( [ "operator.approvals" , "operator.read" ] ) ;
411+ loadApnsRegistrationMock . mockResolvedValue ( relayApnsRegistration ( ) ) ;
412+ resolveApnsRelayConfigFromEnvMock . mockReturnValue ( { ok : true , value : relayConfig } ) ;
413+
414+ const delivery = createPluginApprovalIosPushDelivery ( { log : { } } ) ;
415+ const accepted = await delivery . handleRequested ( pluginApprovalRequest ( "plugin:relay" ) ) ;
416+
417+ expect ( accepted ) . toBe ( true ) ;
418+ expect ( sendApnsPluginApprovalAlertMock ) . toHaveBeenCalledWith (
419+ expect . objectContaining ( {
420+ registration : expect . objectContaining ( { transport : "relay" } ) ,
421+ relayConfig,
422+ approvalId : "plugin:relay" ,
423+ } ) ,
424+ ) ;
425+ expect ( resolveApnsAuthConfigFromEnvMock ) . not . toHaveBeenCalled ( ) ;
426+ } ) ;
427+
428+ it ( "sends plugin cleanup wakes for resolved and expired requests" , async ( ) => {
429+ mockPairedIosOperator ( [ "operator.approvals" , "operator.read" ] ) ;
430+ const delivery = createPluginApprovalIosPushDelivery ( { log : { } } ) ;
431+ const resolvedRequest = pluginApprovalRequest ( "plugin:resolved" ) ;
432+ const expiredRequest = pluginApprovalRequest ( "plugin:expired" ) ;
433+
434+ await delivery . handleRequested ( resolvedRequest ) ;
435+ await delivery . handleRequested ( expiredRequest ) ;
436+ await delivery . handleResolved ( pluginApprovalResolved ( resolvedRequest . id ) ) ;
437+ await delivery . handleExpired ( expiredRequest ) ;
438+
439+ expect ( sendApnsPluginApprovalResolvedWakeMock ) . toHaveBeenCalledTimes ( 2 ) ;
440+ expect ( sendApnsPluginApprovalResolvedWakeMock ) . toHaveBeenCalledWith (
441+ expect . objectContaining ( { approvalId : "plugin:resolved" } ) ,
442+ ) ;
443+ expect ( sendApnsPluginApprovalResolvedWakeMock ) . toHaveBeenCalledWith (
444+ expect . objectContaining ( { approvalId : "plugin:expired" } ) ,
445+ ) ;
446+ } ) ;
447+
448+ it ( "routes exec and plugin factories through the same paired-target resolver" , async ( ) => {
449+ mockPairedIosOperator ( [ "operator.approvals" , "operator.read" ] ) ;
450+
451+ await createExecApprovalIosPushDelivery ( { log : { } } ) . handleRequested (
452+ approvalRequest ( "exec-shared-target" ) ,
453+ ) ;
454+ await createPluginApprovalIosPushDelivery ( { log : { } } ) . handleRequested (
455+ pluginApprovalRequest ( "plugin:shared-target" ) ,
456+ ) ;
457+
458+ expect ( listDevicePairingMock ) . toHaveBeenCalledTimes ( 2 ) ;
459+ expect ( loadApnsRegistrationsMock ) . toHaveBeenNthCalledWith ( 1 , [ "ios-device-1" ] ) ;
460+ expect ( loadApnsRegistrationsMock ) . toHaveBeenNthCalledWith ( 2 , [ "ios-device-1" ] ) ;
461+ expect ( sendApnsExecApprovalAlertMock ) . toHaveBeenCalledTimes ( 1 ) ;
462+ expect ( sendApnsPluginApprovalAlertMock ) . toHaveBeenCalledTimes ( 1 ) ;
463+ } ) ;
464+ } ) ;
331465} ) ;
0 commit comments