@@ -3,9 +3,13 @@ import type { RelayAgentActivityState } from "@t3tools/contracts/relay";
33import { describe , expect , it } from "@effect/vitest" ;
44import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto" ;
55import * as Effect from "effect/Effect" ;
6+ import * as DateTime from "effect/DateTime" ;
67import * as Layer from "effect/Layer" ;
78import * as Redacted from "effect/Redacted" ;
89import * as Schema from "effect/Schema" ;
10+ import * as Stream from "effect/Stream" ;
11+ import type * as Cloudflare from "alchemy/Cloudflare" ;
12+ import * as FcmDeliveryQueueConsumer from "./FcmDeliveryQueueConsumer.ts" ;
913
1014import { RelayConfiguration } from "../Config.ts" ;
1115import { RelayDb } from "../db.ts" ;
@@ -757,3 +761,50 @@ it("stops reducing five-character row fields and fits the remaining alert", () =
757761 expect ( data . activity_line_0 ) . toBe ( "Approval\taaaaa\tbbbbb" ) ;
758762 expect ( new TextEncoder ( ) . encode ( encodeJson ( data ) ) . length ) . toBeLessThanOrEqual ( 3800 ) ;
759763} ) ;
764+
765+ describe ( "FCM queue message isolation" , ( ) => {
766+ for ( const failure of [ "invalid-job" , "fcm-rejection" ] as const ) {
767+ it . effect ( `retries only the ${ failure } message and delivers the rest of its batch` , ( ) => {
768+ const h = harness ( ) ;
769+ const outcomes = new Map < string , "ack" | "retry" > ( ) ;
770+ const message = ( id : string , body : unknown ) : Cloudflare . Queues . Message < unknown > => ( {
771+ id,
772+ body,
773+ timestamp : DateTime . toDateUtc ( DateTime . makeUnsafe ( 0 ) ) ,
774+ attempts : 1 ,
775+ ack : ( ) => {
776+ if ( ! outcomes . has ( id ) ) outcomes . set ( id , "ack" ) ;
777+ } ,
778+ retry : ( ) => {
779+ if ( ! outcomes . has ( id ) ) outcomes . set ( id , "retry" ) ;
780+ } ,
781+ } ) ;
782+ const batch = [
783+ message ( "failed" , failure === "invalid-job" ? { } : h . job ) ,
784+ message ( "healthy" , h . job ) ,
785+ ] ;
786+ return Effect . gen ( function * ( ) {
787+ yield * Stream . fromIterable ( batch ) . pipe (
788+ Stream . tap ( ( item ) =>
789+ Effect . sync ( ( ) => {
790+ h . current . deliveryFailure =
791+ item . id === "failed" && failure === "fcm-rejection"
792+ ? new FcmClientError ( { operation : "send" , status : 400 } )
793+ : null ;
794+ } ) ,
795+ ) ,
796+ Stream . runForEach ( FcmDeliveryQueueConsumer . processMessage ) ,
797+ ) ;
798+ // Alchemy acknowledges the batch after a successful stream. Cloudflare
799+ // ignores those acknowledgements for messages explicitly retried earlier.
800+ for ( const item of batch ) item . ack ( ) ;
801+ expect ( [ ...outcomes ] ) . toEqual ( [
802+ [ "failed" , "retry" ] ,
803+ [ "healthy" , "ack" ] ,
804+ ] ) ;
805+ expect ( h . sent ) . toHaveLength ( 1 ) ;
806+ expect ( h . marked ) . toHaveLength ( 1 ) ;
807+ } ) . pipe ( Effect . provide ( h . layer ) ) ;
808+ } ) ;
809+ }
810+ } ) ;
0 commit comments