427
it.effect('keys post-login events on the persisted apollo_user_id', () => {
428
const home = tempy.temporaryDirectory();
429
const scriptPath = `${home}/composio.ts`;
430
- enableTelemetry('');
430
+ enableTelemetry('uak_persisted');
431
process.argv[1] = scriptPath;
432
433
return Effect.gen(function* () {
434
const fs = yield* FileSystem.FileSystem;
435
- const path = yield* Path.Path;
435
yield* fs.writeFileString(scriptPath, '');
437
- const composioDir = path.join(home, '.composio');
438
- yield* fs.makeDirectory(composioDir, { recursive: true });
439
- yield* fs.writeFileString(
440
- path.join(composioDir, 'analytics.json'),
441
- JSON.stringify({ install_id: 'install_persisted', apollo_user_id: 'om_apollo_123' })
442
- );
436
+
437
+ // Establish the identity under this credential; the id is only trusted
438
+ // while that same credential is active.
439
+ yield* linkApolloIdentityForAnalytics('om_apollo_123');
440
+ childProcessMocks.spawn.mockClear();
441
442
yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_SUCCEEDED });
443
444
expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
445
const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
448
- const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
446
+ const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
447
expect(payload.distinctId).toBe('om_apollo_123');
450
- expect(payload.installId).toBe('install_persisted');
448
}).pipe(Effect.provide(makePlatformLayer(home)));
449
});
450
654
expect(fetchSpy).not.toHaveBeenCalled();
655
});
656
});
657
+
658
+ describe('CLI analytics identity safety', () => {
659
+ it.effect('does not attribute events to a stale identity when the active key differs', () => {
660
+ const home = tempy.temporaryDirectory();
661
+ const scriptPath = `${home}/composio.ts`;
662
+ enableTelemetry('uak_user_a');
663
+ process.argv[1] = scriptPath;
664
+
665
+ return Effect.gen(function* () {
666
+ const fs = yield* FileSystem.FileSystem;
667
+ yield* fs.writeFileString(scriptPath, '');
668
+ yield* linkApolloIdentityForAnalytics('om_user_a');
669
+ childProcessMocks.spawn.mockClear();
670
+
671
+ // A script running under a different credential must not inherit user A.
672
+ vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_user_b');
673
+ yield* trackCliEventEffect({ name: 'producer_event' });
674
+
675
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
676
+ const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
677
+ expect(payload.distinctId).not.toBe('om_user_a');
678
+ expect(payload.distinctId).toBe(payload.installId);
679
+ }).pipe(Effect.provide(makePlatformLayer(home)));
680
+ });
681
+
682
+ it.effect('does not create the identity file when logging out with telemetry disabled', () => {
683
+ const home = tempy.temporaryDirectory();
684
+ enableTelemetry();
685
+ vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'true');
686
+
687
+ return Effect.gen(function* () {
688
+ yield* clearApolloIdentityForAnalytics;
689
+
690
+ const fs = yield* FileSystem.FileSystem;
691
+ const path = yield* Path.Path;
692
+ const exists = yield* fs.exists(path.join(home, '.composio', 'analytics.json'));
693
+ expect(exists).toBe(false);
694
+ }).pipe(Effect.provide(makePlatformLayer(home)));
695
+ });
696
+
697
+ it.effect('never re-aliases one install onto a second Apollo user', () => {
698
+ const home = tempy.temporaryDirectory();
699
+ const scriptPath = `${home}/composio.ts`;
700
+ enableTelemetry();
701
+ process.argv[1] = scriptPath;
702
+
703
+ return Effect.gen(function* () {
704
+ const fs = yield* FileSystem.FileSystem;
705
+ yield* fs.writeFileString(scriptPath, '');
706
+
707
+ yield* linkApolloIdentityForAnalytics('om_first_user');
708
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
709
+
710
+ // Logout, then log in as a different user on the same device.
711
+ yield* clearApolloIdentityForAnalytics;
712
+ yield* linkApolloIdentityForAnalytics('om_second_user');
713
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
714
+ }).pipe(Effect.provide(makePlatformLayer(home)));
715
+ });
716
+ });
717
});