@@ -13,7 +13,10 @@ import * as HttpClientResponse from "effect/http/HttpClientResponse";
1313
1414import type { ApnsCredentials } from "../Config.ts" ;
1515import * as ApnsClient from "./ApnsClient.ts" ;
16- import { sanitizeApnsNotificationPayload } from "./agentActivityPayloads.ts" ;
16+ import {
17+ sanitizeAgentActivityAggregateState ,
18+ sanitizeApnsNotificationPayload ,
19+ } from "./agentActivityPayloads.ts" ;
1720import * as ApnsProviderTokens from "./ApnsProviderTokens.ts" ;
1821
1922const layerTest = ApnsClient . layer . pipe (
@@ -169,6 +172,81 @@ describe("ApnsClient", () => {
169172 } ) . pipe ( Effect . provide ( layerTest ) ) ,
170173 ) ;
171174
175+ it . effect ( "shortens multibyte row titles so Live Activity updates fit within 4096 bytes" , ( ) =>
176+ Effect . gen ( function * ( ) {
177+ const apns = yield * ApnsClient . ApnsClient ;
178+ const longState = sanitizeAgentActivityAggregateState ( {
179+ ...state ,
180+ activeCount : 5 ,
181+ activities : [ "a" , "b" , "c" , "d" , "e" ] . map ( ( id ) => ( {
182+ ...state . activities [ 0 ] ! ,
183+ threadId : ThreadId . make ( id ) ,
184+ projectTitle : "界" . repeat ( 200 ) ,
185+ threadTitle : "界" . repeat ( 200 ) ,
186+ } ) ) ,
187+ } ) ;
188+ const request = apns . makeLiveActivityRequest ( {
189+ event : "update" ,
190+ token : "token" ,
191+ state : longState ,
192+ alert : { title : "界" . repeat ( 120 ) , body : "界" . repeat ( 120 ) } ,
193+ nowEpochSeconds : Math . floor ( now . epochMilliseconds / 1_000 ) ,
194+ nowIso : DateTime . formatIso ( now ) ,
195+ } ) ;
196+
197+ expect (
198+ new TextEncoder ( ) . encode ( JSON . stringify ( request . payload ) ) . byteLength ,
199+ ) . toBeLessThanOrEqual ( 4096 ) ;
200+ const payload = request . payload as { aps : { "content-state" : { props : string } } } ;
201+ const sent = JSON . parse ( payload . aps [ "content-state" ] . props ) as typeof longState ;
202+ expect ( sent . activities . map ( ( row ) => row . threadId ) ) . toEqual ( [ "a" , "b" , "c" , "d" , "e" ] ) ;
203+ expect ( sent . activities [ 0 ] ?. threadTitle ) . toMatch ( / ^ 界 + \. \. \. $ / ) ;
204+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
205+ ) ;
206+
207+ it . effect ( "fits Live Activity updates at every sanitizer cap within 4096 bytes" , ( ) =>
208+ Effect . gen ( function * ( ) {
209+ const apns = yield * ApnsClient . ApnsClient ;
210+ const ids = [ "a" , "b" , "c" , "d" , "e" ] ;
211+ const cappedState = sanitizeAgentActivityAggregateState ( {
212+ ...state ,
213+ activeCount : 5 ,
214+ activities : ids . map ( ( id ) => ( {
215+ ...state . activities [ 0 ] ! ,
216+ threadId : ThreadId . make ( id ) ,
217+ projectTitle : "界" . repeat ( 120 ) ,
218+ threadTitle : "界" . repeat ( 120 ) ,
219+ deepLink : `/${ "d" . repeat ( 511 ) } ` ,
220+ } ) ) ,
221+ } ) ;
222+ const alert = {
223+ title : "界" . repeat ( 120 ) ,
224+ body : `Working: ${ "界" . repeat ( 120 ) } ` ,
225+ } ;
226+
227+ for ( const event of [ "update" ] as const ) {
228+ const request = apns . makeLiveActivityRequest ( {
229+ event,
230+ token : "token" ,
231+ state : cappedState ,
232+ alert,
233+ nowEpochSeconds : Math . floor ( now . epochMilliseconds / 1_000 ) ,
234+ nowIso : DateTime . formatIso ( now ) ,
235+ } ) ;
236+
237+ expect (
238+ new TextEncoder ( ) . encode ( JSON . stringify ( request . payload ) ) . byteLength ,
239+ ) . toBeLessThanOrEqual ( 4096 ) ;
240+ const payload = request . payload as { aps : { "content-state" : { props : string } } } ;
241+ const sent = JSON . parse ( payload . aps [ "content-state" ] . props ) as typeof cappedState ;
242+ expect ( sent . activities . length ) . toBeGreaterThan ( 0 ) ;
243+ expect ( sent . activities . map ( ( row ) => row . threadId ) ) . toEqual (
244+ ids . slice ( 0 , sent . activities . length ) ,
245+ ) ;
246+ }
247+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
248+ ) ;
249+
172250 it . effect ( "reuses the signed provider JWT across pushes within the reuse window" , ( ) => {
173251 ApnsProviderTokens . __resetApnsProviderTokenCacheForTest ( ) ;
174252 const { privateKey } = NodeCrypto . generateKeyPairSync ( "ec" , {
0 commit comments