11import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer" ;
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
3- import { assert , it } from "@effect/vitest" ;
3+ import { assert , it , vi } from "@effect/vitest" ;
44import * as ConfigProvider from "effect/ConfigProvider" ;
55import * as Effect from "effect/Effect" ;
66import * as Layer from "effect/Layer" ;
77import * as Schema from "effect/Schema" ;
88import * as TestClock from "effect/testing/TestClock" ;
99import * as HttpClient from "effect/http/HttpClient" ;
1010import * as HttpClientError from "effect/http/HttpClientError" ;
11+ import * as HttpClientRequest from "effect/http/HttpClientRequest" ;
12+ import * as HttpClientResponse from "effect/http/HttpClientResponse" ;
1113import * as HttpServer from "effect/http/HttpServer" ;
1214import * as HttpServerRequest from "effect/http/HttpServerRequest" ;
1315import * as HttpServerResponse from "effect/http/HttpServerResponse" ;
@@ -52,27 +54,84 @@ interface RecordedBatchBody {
5254
5355const SentBatch = Schema . fromJsonString (
5456 Schema . Struct ( {
55- batch : Schema . Array ( Schema . Struct ( { uuid : Schema . String } ) ) ,
57+ batch : Schema . Array (
58+ Schema . Struct ( {
59+ uuid : Schema . String ,
60+ properties : Schema . Struct ( { telemetryChannel : Schema . String } ) ,
61+ } ) ,
62+ ) ,
5663 } ) ,
5764) ;
65+ const decodeSentBatch = Schema . decodeEffect ( SentBatch ) ;
66+ type SentEvent = ( typeof SentBatch . Type ) [ "batch" ] [ number ] ;
67+ type Respond = (
68+ request : HttpClientRequest . HttpClientRequest ,
69+ ) => Effect . Effect < HttpClientResponse . HttpClientResponse , HttpClientError . HttpClientError > ;
70+
71+ /** The connection drops after the endpoint read the batch, so it may be stored. */
72+ const connectionReset : Respond = ( request ) =>
73+ Effect . fail (
74+ new HttpClientError . HttpClientError ( {
75+ reason : new HttpClientError . TransportError ( { request, cause : "connection reset" } ) ,
76+ } ) ,
77+ ) ;
5878
59- const layerAcceptThenFailClient = ( batches : Array < ReadonlyArray < { readonly uuid : string } > > ) =>
60- Layer . succeed (
61- HttpClient . HttpClient ,
62- HttpClient . make ( ( request ) =>
79+ const respondWithStatus =
80+ ( status : number ) : Respond =>
81+ ( request ) =>
82+ Effect . succeed ( HttpClientResponse . fromWeb ( request , new Response ( null , { status } ) ) ) ;
83+
84+ /**
85+ * Records 20 events, then lets ten minutes of background flushes run against
86+ * a client that answers every send with `respond`. Returns the sent batches.
87+ */
88+ const sendForTenMinutes = ( respond : Respond ) =>
89+ Effect . gen ( function * ( ) {
90+ const batches : Array < ReadonlyArray < SentEvent > > = [ ] ;
91+ const client = HttpClient . make ( ( request ) =>
6392 Effect . gen ( function * ( ) {
6493 if ( request . body . _tag === "Uint8Array" ) {
65- const body = yield * Schema . decodeEffect ( SentBatch ) (
66- new TextDecoder ( ) . decode ( request . body . body ) ,
67- ) . pipe ( Effect . orDie ) ;
94+ const body = yield * decodeSentBatch ( new TextDecoder ( ) . decode ( request . body . body ) ) . pipe (
95+ Effect . orDie ,
96+ ) ;
6897 batches . push ( body . batch ) ;
6998 }
70- return yield * new HttpClientError . HttpClientError ( {
71- reason : new HttpClientError . TransportError ( { request, cause : "connection reset" } ) ,
72- } ) ;
99+ return yield * respond ( request ) ;
73100 } ) ,
74- ) ,
75- ) ;
101+ ) ;
102+ const runtimeLayer = AnalyticsService . layer . pipe (
103+ Layer . provideMerge (
104+ ServerConfig . ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-telemetry-retry-" } ) ,
105+ ) ,
106+ Layer . provide (
107+ ConfigProvider . layer (
108+ ConfigProvider . fromUnknown ( {
109+ T3CODE_TELEMETRY_ENABLED : true ,
110+ T3CODE_POSTHOG_KEY : "phc_test_key" ,
111+ T3CODE_POSTHOG_HOST : "http://localhost" ,
112+ } ) ,
113+ ) ,
114+ ) ,
115+ Layer . provide (
116+ Layer . mergeAll (
117+ Layer . succeed ( HostProcess . Platform , "win32" ) ,
118+ Layer . succeed ( HostProcess . Architecture , "x64" ) ,
119+ Layer . succeed ( HttpClient . HttpClient , client ) ,
120+ ) ,
121+ ) ,
122+ ) ;
123+
124+ yield * Effect . gen ( function * ( ) {
125+ const analytics = yield * AnalyticsService . AnalyticsService ;
126+ for ( let index = 0 ; index < 20 ; index += 1 ) {
127+ yield * analytics . record ( "test.retry" , { index } ) ;
128+ }
129+ for ( let second = 0 ; second < 600 ; second += 1 ) {
130+ yield * TestClock . adjust ( "1 second" ) ;
131+ }
132+ } ) . pipe ( Effect . provide ( runtimeLayer ) ) ;
133+ return batches ;
134+ } ) ;
76135
77136it ( "retryDelayMs doubles from 2s and stays under the 5 minute cap" , ( ) => {
78137 assert . equal ( AnalyticsService . retryDelayMs ( 1 , 0 ) , 1_000 ) ;
@@ -82,48 +141,46 @@ it("retryDelayMs doubles from 2s and stays under the 5 minute cap", () => {
82141} ) ;
83142
84143it . layer ( NodeServices . layer ) ( "AnalyticsService test" , ( it ) => {
85- it . effect ( "a batch that keeps failing is retried with backoff , then dropped" , ( ) =>
144+ it . effect ( "a batch that keeps failing is retried with the same uuids , then dropped" , ( ) =>
86145 Effect . gen ( function * ( ) {
87- const batches : Array < ReadonlyArray < { readonly uuid : string } > > = [ ] ;
88- const layerRuntime = AnalyticsService . layer . pipe (
89- Layer . provideMerge (
90- ServerConfig . ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-telemetry-retry-" } ) ,
91- ) ,
92- Layer . provide (
93- ConfigProvider . layer (
94- ConfigProvider . fromUnknown ( {
95- T3CODE_TELEMETRY_ENABLED : true ,
96- T3CODE_POSTHOG_KEY : "phc_test_key" ,
97- T3CODE_POSTHOG_HOST : "http://localhost" ,
98- } ) ,
99- ) ,
100- ) ,
101- Layer . provide (
102- Layer . mergeAll (
103- Layer . succeed ( HostProcess . Platform , "win32" ) ,
104- Layer . succeed ( HostProcess . Architecture , "x64" ) ,
105- layerAcceptThenFailClient ( batches ) ,
106- ) ,
107- ) ,
108- ) ;
109-
110- yield * Effect . gen ( function * ( ) {
111- const analytics = yield * AnalyticsService . AnalyticsService ;
112- for ( let index = 0 ; index < 20 ; index += 1 ) {
113- yield * analytics . record ( "test.retry" , { index } ) ;
114- }
115- for ( let second = 0 ; second < 600 ; second += 1 ) {
116- yield * TestClock . adjust ( "1 second" ) ;
117- }
118- } ) . pipe ( Effect . provide ( layerRuntime ) ) ;
146+ const failures = {
147+ "connection reset" : connectionReset ,
148+ "408" : respondWithStatus ( 408 ) ,
149+ "429" : respondWithStatus ( 429 ) ,
150+ "503" : respondWithStatus ( 503 ) ,
151+ } ;
152+ for ( const [ failure , respond ] of Object . entries ( failures ) ) {
153+ const batches = yield * sendForTenMinutes ( respond ) ;
154+ assert . equal ( batches . length , 5 , failure ) ;
155+ const uuids = batches . map ( ( batch ) => batch . map ( ( event ) => event . uuid ) . join ( "," ) ) ;
156+ assert . equal ( new Set ( uuids ) . size , 1 , `${ failure } : every retry carries the same uuids` ) ;
157+ assert . equal ( new Set ( batches [ 0 ] ?. map ( ( event ) => event . uuid ) ) . size , 20 , failure ) ;
158+ }
159+ } ) ,
160+ ) ;
119161
120- assert . equal ( batches . length , 5 ) ;
121- const uuids = batches . map ( ( batch ) => batch . map ( ( event ) => event . uuid ) . join ( "," ) ) ;
122- assert . equal ( new Set ( uuids ) . size , 1 , "every retry carries the same uuids" ) ;
123- assert . equal ( new Set ( batches [ 0 ] ?. map ( ( event ) => event . uuid ) ) . size , 20 ) ;
162+ it . effect ( "a batch the endpoint rejects is dropped without a retry" , ( ) =>
163+ Effect . gen ( function * ( ) {
164+ for ( const status of [ 400 , 401 , 413 ] ) {
165+ const batches = yield * sendForTenMinutes ( respondWithStatus ( status ) ) ;
166+ assert . equal ( batches . length , 1 , `status ${ status } ` ) ;
167+ }
124168 } ) ,
125169 ) ;
126170
171+ it . effect ( "events carry the build's telemetry channel, or dev when none is set" , ( ) =>
172+ Effect . gen ( function * ( ) {
173+ const channels = ( batches : ReadonlyArray < ReadonlyArray < SentEvent > > ) => [
174+ ...new Set ( batches . flat ( ) . map ( ( event ) => event . properties . telemetryChannel ) ) ,
175+ ] ;
176+ assert . deepEqual ( channels ( yield * sendForTenMinutes ( respondWithStatus ( 204 ) ) ) , [ "dev" ] ) ;
177+
178+ // Stands in for the value the bundler writes in at build time.
179+ vi . stubGlobal ( "__T3CODE_TELEMETRY_CHANNEL__" , "nightly" ) ;
180+ assert . deepEqual ( channels ( yield * sendForTenMinutes ( respondWithStatus ( 204 ) ) ) , [ "nightly" ] ) ;
181+ } ) . pipe ( Effect . ensuring ( Effect . sync ( ( ) => vi . unstubAllGlobals ( ) ) ) ) ,
182+ ) ;
183+
127184 it . effect ( "flush drains all buffered events across multiple batches" , ( ) =>
128185 Effect . gen ( function * ( ) {
129186 const capturedRequests : Array < RecordedBatchRequest > = [ ] ;
0 commit comments