@@ -16,7 +16,7 @@ import * as HttpServerResponse from "effect/http/HttpServerResponse";
1616import * as HostProcess from "@t3tools/shared/HostProcess" ;
1717
1818import * as ServerConfig from "../config.ts" ;
19- import { getTelemetryIdentifier } from "./Identify.ts" ;
19+ import { getTelemetryIdentity } from "./Identify.ts" ;
2020import * as AnalyticsService from "./AnalyticsService.ts" ;
2121
2222interface RecordedBatchRequest {
@@ -57,7 +57,14 @@ const SentBatch = Schema.fromJsonString(
5757 batch : Schema . Array (
5858 Schema . Struct ( {
5959 uuid : Schema . String ,
60- properties : Schema . Struct ( { telemetryChannel : Schema . String } ) ,
60+ event : Schema . String ,
61+ distinct_id : Schema . String ,
62+ properties : Schema . Struct ( {
63+ telemetryChannel : Schema . String ,
64+ installId : Schema . optionalKey ( Schema . String ) ,
65+ ownerSource : Schema . optionalKey ( Schema . String ) ,
66+ observedBy : Schema . optionalKey ( Schema . String ) ,
67+ } ) ,
6168 } ) ,
6269 ) ,
6370 } ) ,
@@ -81,11 +88,26 @@ const respondWithStatus =
8188 ( request ) =>
8289 Effect . succeed ( HttpClientResponse . fromWeb ( request , new Response ( null , { status } ) ) ) ;
8390
91+ const recordTwentyEvents = Effect . gen ( function * ( ) {
92+ const analytics = yield * AnalyticsService . AnalyticsService ;
93+ for ( let index = 0 ; index < 20 ; index += 1 ) {
94+ yield * analytics . record ( "test.retry" , { index } ) ;
95+ }
96+ } ) ;
97+
8498/**
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.
99+ * Runs `record` (20 events by default), then lets ten minutes of background
100+ * flushes run against a client that answers every send with `respond`.
101+ * Returns the sent batches.
87102 */
88- const sendForTenMinutes = ( respond : Respond ) =>
103+ const sendForTenMinutes = (
104+ respond : Respond ,
105+ record : Effect . Effect <
106+ void ,
107+ never ,
108+ AnalyticsService . AnalyticsService | Effect . Services < typeof getTelemetryIdentity >
109+ > = recordTwentyEvents ,
110+ ) =>
89111 Effect . gen ( function * ( ) {
90112 const batches : Array < ReadonlyArray < SentEvent > > = [ ] ;
91113 const client = HttpClient . make ( ( request ) =>
@@ -122,10 +144,7 @@ const sendForTenMinutes = (respond: Respond) =>
122144 ) ;
123145
124146 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- }
147+ yield * record ;
129148 for ( let second = 0 ; second < 600 ; second += 1 ) {
130149 yield * TestClock . adjust ( "1 second" ) ;
131150 }
@@ -181,6 +200,71 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
181200 } ) . pipe ( Effect . ensuring ( Effect . sync ( ( ) => vi . unstubAllGlobals ( ) ) ) ) ,
182201 ) ;
183202
203+ it . effect ( "each owner is sent once per process, and every event carries the install id" , ( ) =>
204+ Effect . gen ( function * ( ) {
205+ let identity : Effect . Success < typeof getTelemetryIdentity > | null = null ;
206+ const batches = yield * sendForTenMinutes (
207+ respondWithStatus ( 204 ) ,
208+ Effect . gen ( function * ( ) {
209+ identity = yield * getTelemetryIdentity ;
210+ const analytics = yield * AnalyticsService . AnalyticsService ;
211+ const connect = { source : "connect" , ownerId : "a" . repeat ( 64 ) } as const ;
212+ yield * analytics . recordOwners ( [ connect , connect ] , "server" ) ;
213+ yield * analytics . recordOwners (
214+ [ connect , { source : "device" , ownerId : "b" . repeat ( 64 ) } ] ,
215+ "client" ,
216+ ) ;
217+ } ) ,
218+ ) ;
219+ const events = batches . flat ( ) ;
220+
221+ assert . deepEqual (
222+ events . map ( ( event ) => [
223+ event . event ,
224+ event . properties . ownerSource ,
225+ event . properties . observedBy ,
226+ ] ) ,
227+ [
228+ [ "install.owner_observed" , "connect" , "server" ] ,
229+ [ "install.owner_observed" , "connect" , "client" ] ,
230+ [ "install.owner_observed" , "device" , "client" ] ,
231+ ] ,
232+ ) ;
233+ const { distinctId, installId } = identity ! ;
234+ assert . isString ( installId ) ;
235+ assert . isTrue (
236+ events . every (
237+ ( event ) => event . distinct_id === distinctId && event . properties . installId === installId ,
238+ ) ,
239+ ) ;
240+ } ) ,
241+ ) ;
242+
243+ it . effect ( "client owner reports cannot use up the server's owner cap" , ( ) =>
244+ Effect . gen ( function * ( ) {
245+ const batches = yield * sendForTenMinutes (
246+ respondWithStatus ( 204 ) ,
247+ Effect . gen ( function * ( ) {
248+ const analytics = yield * AnalyticsService . AnalyticsService ;
249+ yield * analytics . recordOwners (
250+ Array . from ( { length : 1_001 } , ( _ , index ) => ( {
251+ source : "device" as const ,
252+ ownerId : index . toString ( 16 ) . padStart ( 64 , "0" ) ,
253+ } ) ) ,
254+ "client" ,
255+ ) ;
256+ // The event buffer also holds 1,000, so send the client events first.
257+ yield * analytics . flush ;
258+ yield * analytics . recordOwners ( [ { source : "connect" , ownerId : "a" . repeat ( 64 ) } ] , "server" ) ;
259+ } ) ,
260+ ) ;
261+ const owners = batches . flat ( ) . map ( ( event ) => event . properties . observedBy ) ;
262+
263+ assert . equal ( owners . filter ( ( observedBy ) => observedBy === "client" ) . length , 1_000 ) ;
264+ assert . equal ( owners . filter ( ( observedBy ) => observedBy === "server" ) . length , 1 ) ;
265+ } ) ,
266+ ) ;
267+
184268 it . effect ( "flush drains all buffered events across multiple batches" , ( ) =>
185269 Effect . gen ( function * ( ) {
186270 const capturedRequests : Array < RecordedBatchRequest > = [ ] ;
@@ -227,8 +311,8 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
227311
228312 yield * Effect . gen ( function * ( ) {
229313 yield * Layer . launch ( layerBatchServer ) . pipe ( Effect . forkScoped ) ;
230- const telemetryIdentifier = yield * getTelemetryIdentifier ;
231- assert . equal ( telemetryIdentifier !== null , true ) ;
314+ const { distinctId } = yield * getTelemetryIdentity ;
315+ assert . equal ( distinctId !== null , true ) ;
232316 const analytics = yield * AnalyticsService . AnalyticsService ;
233317
234318 for ( let index = 0 ; index < 45 ; index += 1 ) {
0 commit comments