@langwatch/server 3.1.0-beta.3 → 3.1.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
  2. package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
  3. package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
  4. package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
  5. package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
  6. package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
  7. package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
  8. package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
  9. package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
  10. package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
  11. package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
  12. package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
  13. package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
  14. package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
  15. package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
  16. package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
  17. package/langwatch/ee/admin/impersonation.service.ts +170 -0
  18. package/langwatch/ee/admin/isAdmin.ts +9 -0
  19. package/langwatch/ee/admin/routes/admin.ts +531 -0
  20. package/langwatch/ee/admin/safeSelects.ts +64 -0
  21. package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
  22. package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
  23. package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
  24. package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
  25. package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
  26. package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
  27. package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
  28. package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
  29. package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
  30. package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
  31. package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
  32. package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
  33. package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
  34. package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
  35. package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
  36. package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
  37. package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
  38. package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
  39. package/langwatch/ee/billing/currencyRouter.ts +18 -0
  40. package/langwatch/ee/billing/errors.ts +129 -0
  41. package/langwatch/ee/billing/index.ts +67 -0
  42. package/langwatch/ee/billing/middleware.ts +17 -0
  43. package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
  44. package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
  45. package/langwatch/ee/billing/notifications/types.ts +9 -0
  46. package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
  47. package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
  48. package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
  49. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
  50. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
  51. package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
  52. package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
  53. package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
  54. package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
  55. package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
  56. package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
  57. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
  58. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
  59. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
  60. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
  61. package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
  62. package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
  63. package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
  64. package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
  65. package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
  66. package/langwatch/ee/billing/nurturing/types.ts +125 -0
  67. package/langwatch/ee/billing/planLimits.ts +190 -0
  68. package/langwatch/ee/billing/planProvider.ts +119 -0
  69. package/langwatch/ee/billing/planTypes.ts +53 -0
  70. package/langwatch/ee/billing/pricing.ts +57 -0
  71. package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
  72. package/langwatch/ee/billing/services/customerService.ts +90 -0
  73. package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
  74. package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
  75. package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
  76. package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
  77. package/langwatch/ee/billing/services/subscription.service.ts +549 -0
  78. package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
  79. package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
  80. package/langwatch/ee/billing/services/webhookService.ts +858 -0
  81. package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
  82. package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
  83. package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
  84. package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
  85. package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
  86. package/langwatch/ee/billing/types.ts +102 -0
  87. package/langwatch/ee/billing/utils/currency.ts +99 -0
  88. package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
  89. package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
  90. package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
  91. package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
  92. package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
  93. package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
  94. package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
  95. package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
  96. package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
  97. package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
  98. package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
  99. package/langwatch/ee/licensing/client.ts +29 -0
  100. package/langwatch/ee/licensing/constants.ts +195 -0
  101. package/langwatch/ee/licensing/defaults.ts +55 -0
  102. package/langwatch/ee/licensing/errors.ts +11 -0
  103. package/langwatch/ee/licensing/index.ts +52 -0
  104. package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
  105. package/langwatch/ee/licensing/licenseHandler.ts +308 -0
  106. package/langwatch/ee/licensing/planInfo.ts +46 -0
  107. package/langwatch/ee/licensing/planMapping.ts +37 -0
  108. package/langwatch/ee/licensing/planTemplates.ts +109 -0
  109. package/langwatch/ee/licensing/server.ts +43 -0
  110. package/langwatch/ee/licensing/signing.ts +43 -0
  111. package/langwatch/ee/licensing/types.ts +156 -0
  112. package/langwatch/ee/licensing/validation.ts +107 -0
  113. package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
  114. package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
  115. package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
  116. package/langwatch/package.json +1 -1
  117. package/langwatch_nlp/uv.lock +179 -5
  118. package/package.json +1 -1
  119. package/packages/server/dist/cli.cjs +192 -142
@@ -0,0 +1,237 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import {
3
+ fireActivityTrackingNurturing,
4
+ resetActivityTrackingCache,
5
+ getActivityTrackingCacheSize,
6
+ } from "./activityTracking";
7
+
8
+ // Suppress logger output
9
+ vi.mock("../../../../src/utils/logger/server", () => ({
10
+ createLogger: () => ({
11
+ info: vi.fn(),
12
+ warn: vi.fn(),
13
+ error: vi.fn(),
14
+ debug: vi.fn(),
15
+ }),
16
+ }));
17
+ vi.mock("../../../../src/utils/posthogErrorCapture", () => ({
18
+ captureException: vi.fn(),
19
+ }));
20
+
21
+ const mockNurturing = {
22
+ identifyUser: vi.fn().mockResolvedValue(undefined),
23
+ trackEvent: vi.fn().mockResolvedValue(undefined),
24
+ groupUser: vi.fn().mockResolvedValue(undefined),
25
+ batch: vi.fn().mockResolvedValue(undefined),
26
+ };
27
+
28
+ let currentNurturing: typeof mockNurturing | undefined = mockNurturing;
29
+
30
+ vi.mock("../../../../src/server/app-layer/app", () => ({
31
+ getApp: () => ({
32
+ get nurturing() {
33
+ return currentNurturing;
34
+ },
35
+ }),
36
+ }));
37
+
38
+ describe("Activity tracking hook", () => {
39
+ describe("given an active NurturingService", () => {
40
+ beforeEach(() => {
41
+ vi.clearAllMocks();
42
+ currentNurturing = mockNurturing;
43
+ resetActivityTrackingCache();
44
+ vi.useRealTimers();
45
+ });
46
+
47
+ describe("when the auth session callback fires", () => {
48
+ it("identifies user with last_active_at set to the current time", () => {
49
+ const now = new Date("2026-03-15T12:00:00.000Z");
50
+ vi.setSystemTime(now);
51
+
52
+ fireActivityTrackingNurturing({ userId: "user-1" });
53
+
54
+ expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
55
+ userId: "user-1",
56
+ traits: { last_active_at: "2026-03-15T12:00:00.000Z" },
57
+ });
58
+
59
+ vi.useRealTimers();
60
+ });
61
+ });
62
+
63
+ describe("when a user refreshes their session multiple times within one hour", () => {
64
+ it("makes at most one Customer.io identify call per hour", () => {
65
+ vi.useFakeTimers();
66
+ vi.setSystemTime(new Date("2026-03-15T12:00:00.000Z"));
67
+
68
+ fireActivityTrackingNurturing({ userId: "user-1" });
69
+ fireActivityTrackingNurturing({ userId: "user-1" });
70
+ fireActivityTrackingNurturing({ userId: "user-1" });
71
+
72
+ expect(mockNurturing.identifyUser).toHaveBeenCalledTimes(1);
73
+
74
+ vi.useRealTimers();
75
+ });
76
+
77
+ it("allows a new call after one hour has passed", () => {
78
+ vi.useFakeTimers();
79
+ vi.setSystemTime(new Date("2026-03-15T12:00:00.000Z"));
80
+
81
+ fireActivityTrackingNurturing({ userId: "user-1" });
82
+ expect(mockNurturing.identifyUser).toHaveBeenCalledTimes(1);
83
+
84
+ // Advance past the 1-hour debounce window
85
+ vi.advanceTimersByTime(60 * 60 * 1000 + 1);
86
+
87
+ fireActivityTrackingNurturing({ userId: "user-1" });
88
+ expect(mockNurturing.identifyUser).toHaveBeenCalledTimes(2);
89
+
90
+ vi.useRealTimers();
91
+ });
92
+
93
+ it("tracks separate users independently", () => {
94
+ fireActivityTrackingNurturing({ userId: "user-1" });
95
+ fireActivityTrackingNurturing({ userId: "user-2" });
96
+
97
+ expect(mockNurturing.identifyUser).toHaveBeenCalledTimes(2);
98
+ });
99
+ });
100
+
101
+ describe("when expired entries accumulate in the debounce cache", () => {
102
+ it("evicts entries older than one hour during the next call", () => {
103
+ vi.useFakeTimers();
104
+ vi.setSystemTime(new Date("2026-03-15T12:00:00.000Z"));
105
+
106
+ // Populate cache with multiple users
107
+ fireActivityTrackingNurturing({ userId: "user-1" });
108
+ fireActivityTrackingNurturing({ userId: "user-2" });
109
+ fireActivityTrackingNurturing({ userId: "user-3" });
110
+ expect(getActivityTrackingCacheSize()).toBe(3);
111
+
112
+ // Advance past the 1-hour window so entries are expired
113
+ vi.advanceTimersByTime(60 * 60 * 1000 + 1);
114
+
115
+ // Next call triggers sweep and evicts expired entries
116
+ fireActivityTrackingNurturing({ userId: "user-4" });
117
+
118
+ // Only user-4 remains (user-1..3 were evicted by sweep)
119
+ expect(getActivityTrackingCacheSize()).toBe(1);
120
+
121
+ vi.useRealTimers();
122
+ });
123
+
124
+ it("does not sweep more than once per hour", () => {
125
+ vi.useFakeTimers();
126
+ vi.setSystemTime(new Date("2026-03-15T12:00:00.000Z"));
127
+
128
+ // First call triggers sweep (lastSweepAt is 0)
129
+ fireActivityTrackingNurturing({ userId: "user-1" });
130
+ expect(getActivityTrackingCacheSize()).toBe(1);
131
+
132
+ // Advance 30 minutes — not enough for another sweep
133
+ vi.advanceTimersByTime(30 * 60 * 1000);
134
+ fireActivityTrackingNurturing({ userId: "user-2" });
135
+ expect(getActivityTrackingCacheSize()).toBe(2);
136
+
137
+ // Advance another 31 minutes (total 61 min from start)
138
+ // user-1 is now expired but sweep hasn't run since 30min mark
139
+ vi.advanceTimersByTime(31 * 60 * 1000);
140
+ fireActivityTrackingNurturing({ userId: "user-3" });
141
+
142
+ // Sweep should have run: user-1 expired (61 min old), user-2 still valid (31 min old)
143
+ // user-3 just added. So cache = user-2 + user-3 = 2
144
+ expect(getActivityTrackingCacheSize()).toBe(2);
145
+
146
+ vi.useRealTimers();
147
+ });
148
+ });
149
+
150
+ describe("when Customer.io API is unavailable", () => {
151
+ it("does not throw (fire-and-forget)", async () => {
152
+ const { captureException } = await import(
153
+ "../../../../src/utils/posthogErrorCapture"
154
+ );
155
+ mockNurturing.identifyUser.mockRejectedValueOnce(
156
+ new Error("CIO unavailable"),
157
+ );
158
+
159
+ expect(() =>
160
+ fireActivityTrackingNurturing({ userId: "user-1" }),
161
+ ).not.toThrow();
162
+
163
+ await vi.waitFor(() => {
164
+ expect(captureException).toHaveBeenCalled();
165
+ });
166
+ });
167
+
168
+ it("clears the cache entry on rejection so the next call can retry", async () => {
169
+ mockNurturing.identifyUser.mockRejectedValueOnce(
170
+ new Error("CIO unavailable"),
171
+ );
172
+
173
+ fireActivityTrackingNurturing({ userId: "user-1" });
174
+ expect(getActivityTrackingCacheSize()).toBe(1);
175
+
176
+ await vi.waitFor(() => {
177
+ expect(getActivityTrackingCacheSize()).toBe(0);
178
+ });
179
+
180
+ // Next call should succeed and re-add to cache
181
+ mockNurturing.identifyUser.mockResolvedValueOnce(undefined);
182
+ fireActivityTrackingNurturing({ userId: "user-1" });
183
+ expect(mockNurturing.identifyUser).toHaveBeenCalledTimes(2);
184
+ });
185
+ });
186
+ });
187
+
188
+ describe("given a user who has not completed onboarding (no organization)", () => {
189
+ beforeEach(() => {
190
+ vi.clearAllMocks();
191
+ currentNurturing = mockNurturing;
192
+ resetActivityTrackingCache();
193
+ });
194
+
195
+ describe("when the auth session callback fires", () => {
196
+ it("does not call identifyUser to avoid creating ghost people in Customer.io", () => {
197
+ fireActivityTrackingNurturing({
198
+ userId: "user-no-org",
199
+ hasOrganization: false,
200
+ });
201
+
202
+ expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
203
+ });
204
+
205
+ it("does not populate the debounce cache", () => {
206
+ fireActivityTrackingNurturing({
207
+ userId: "user-no-org",
208
+ hasOrganization: false,
209
+ });
210
+
211
+ expect(getActivityTrackingCacheSize()).toBe(0);
212
+ });
213
+ });
214
+ });
215
+
216
+ describe("given nurturing is undefined", () => {
217
+ beforeEach(() => {
218
+ vi.clearAllMocks();
219
+ currentNurturing = undefined;
220
+ resetActivityTrackingCache();
221
+ });
222
+
223
+ describe("when the auth session callback fires", () => {
224
+ it("silently skips without calling any nurturing methods", () => {
225
+ fireActivityTrackingNurturing({ userId: "user-1" });
226
+
227
+ expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
228
+ });
229
+
230
+ it("does not populate the debounce cache", () => {
231
+ fireActivityTrackingNurturing({ userId: "user-1" });
232
+
233
+ expect(getActivityTrackingCacheSize()).toBe(0);
234
+ });
235
+ });
236
+ });
237
+ });
@@ -0,0 +1,120 @@
1
+ import { getApp } from "../../../../src/server/app-layer/app";
2
+ import { captureException } from "../../../../src/utils/posthogErrorCapture";
3
+
4
+ /**
5
+ * Fires nurturing calls when a team member is invited.
6
+ *
7
+ * Updates the user's team_member_count trait and tracks the event.
8
+ * All calls are fire-and-forget.
9
+ */
10
+ export function fireTeamMemberInvitedNurturing({
11
+ userId,
12
+ teamMemberCount,
13
+ role,
14
+ }: {
15
+ userId: string;
16
+ teamMemberCount: number;
17
+ role: string;
18
+ }): void {
19
+ const nurturing = getApp().nurturing;
20
+ if (!nurturing) return;
21
+
22
+ void nurturing
23
+ .identifyUser({ userId, traits: { team_member_count: teamMemberCount } })
24
+ .catch(captureException);
25
+
26
+ void nurturing
27
+ .trackEvent({ userId, event: "team_member_invited", properties: {
28
+ role,
29
+ }})
30
+ .catch(captureException);
31
+ }
32
+
33
+ /**
34
+ * Fires nurturing calls when a workflow is created.
35
+ *
36
+ * Updates the user's workflow_count trait and tracks the event.
37
+ * All calls are fire-and-forget.
38
+ */
39
+ export function fireWorkflowCreatedNurturing({
40
+ userId,
41
+ workflowCount,
42
+ workflowId,
43
+ projectId,
44
+ }: {
45
+ userId: string;
46
+ workflowCount: number;
47
+ workflowId: string;
48
+ projectId: string;
49
+ }): void {
50
+ const nurturing = getApp().nurturing;
51
+ if (!nurturing) return;
52
+
53
+ void nurturing
54
+ .identifyUser({ userId, traits: { workflow_count: workflowCount } })
55
+ .catch(captureException);
56
+
57
+ void nurturing
58
+ .trackEvent({ userId, event: "workflow_created", properties: {
59
+ workflow_id: workflowId,
60
+ project_id: projectId,
61
+ }})
62
+ .catch(captureException);
63
+ }
64
+
65
+ /**
66
+ * Fires nurturing calls when a scenario is created.
67
+ *
68
+ * Updates the user's scenario_count trait and tracks the event.
69
+ * All calls are fire-and-forget.
70
+ */
71
+ export function fireScenarioCreatedNurturing({
72
+ userId,
73
+ scenarioCount,
74
+ scenarioId,
75
+ projectId,
76
+ }: {
77
+ userId: string;
78
+ scenarioCount: number;
79
+ scenarioId: string;
80
+ projectId: string;
81
+ }): void {
82
+ const nurturing = getApp().nurturing;
83
+ if (!nurturing) return;
84
+
85
+ void nurturing
86
+ .identifyUser({ userId, traits: { scenario_count: scenarioCount } })
87
+ .catch(captureException);
88
+
89
+ void nurturing
90
+ .trackEvent({ userId, event: "scenario_created", properties: {
91
+ scenario_id: scenarioId,
92
+ project_id: projectId,
93
+ }})
94
+ .catch(captureException);
95
+ }
96
+
97
+ /**
98
+ * Fires nurturing event when an experiment is run.
99
+ *
100
+ * Fire-and-forget.
101
+ */
102
+ export function fireExperimentRanNurturing({
103
+ userId,
104
+ experimentId,
105
+ projectId,
106
+ }: {
107
+ userId: string;
108
+ experimentId?: string;
109
+ projectId: string;
110
+ }): void {
111
+ const nurturing = getApp().nurturing;
112
+ if (!nurturing) return;
113
+
114
+ void nurturing
115
+ .trackEvent({ userId, event: "experiment_ran", properties: {
116
+ experiment_id: experimentId,
117
+ project_id: projectId,
118
+ }})
119
+ .catch(captureException);
120
+ }
@@ -0,0 +1,324 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import {
3
+ fireTeamMemberInvitedNurturing,
4
+ fireWorkflowCreatedNurturing,
5
+ fireScenarioCreatedNurturing,
6
+ fireExperimentRanNurturing,
7
+ } from "./featureAdoption";
8
+
9
+ // Suppress logger output
10
+ vi.mock("../../../../src/utils/logger/server", () => ({
11
+ createLogger: () => ({
12
+ info: vi.fn(),
13
+ warn: vi.fn(),
14
+ error: vi.fn(),
15
+ debug: vi.fn(),
16
+ }),
17
+ }));
18
+ vi.mock("../../../../src/utils/posthogErrorCapture", () => ({
19
+ captureException: vi.fn(),
20
+ }));
21
+
22
+ const mockNurturing = {
23
+ identifyUser: vi.fn().mockResolvedValue(undefined),
24
+ trackEvent: vi.fn().mockResolvedValue(undefined),
25
+ groupUser: vi.fn().mockResolvedValue(undefined),
26
+ batch: vi.fn().mockResolvedValue(undefined),
27
+ };
28
+
29
+ let currentNurturing: typeof mockNurturing | undefined = mockNurturing;
30
+
31
+ vi.mock("../../../../src/server/app-layer/app", () => ({
32
+ getApp: () => ({
33
+ get nurturing() {
34
+ return currentNurturing;
35
+ },
36
+ }),
37
+ }));
38
+
39
+ describe("Feature adoption hooks", () => {
40
+ beforeEach(() => {
41
+ vi.clearAllMocks();
42
+ currentNurturing = mockNurturing;
43
+ });
44
+
45
+ describe("fireTeamMemberInvitedNurturing()", () => {
46
+ describe("when an invite is sent", () => {
47
+ it("identifies user with updated team_member_count", () => {
48
+ fireTeamMemberInvitedNurturing({
49
+ userId: "user-1",
50
+ teamMemberCount: 5,
51
+ role: "member",
52
+ });
53
+
54
+ expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
55
+ userId: "user-1",
56
+ traits: { team_member_count: 5 },
57
+ });
58
+ });
59
+
60
+ it("tracks team_member_invited event with role", () => {
61
+ fireTeamMemberInvitedNurturing({
62
+ userId: "user-1",
63
+ teamMemberCount: 5,
64
+ role: "member",
65
+ });
66
+
67
+ expect(mockNurturing.trackEvent).toHaveBeenCalledWith({
68
+ userId: "user-1",
69
+ event: "team_member_invited",
70
+ properties: { role: "member" },
71
+ });
72
+ });
73
+ });
74
+
75
+ describe("when nurturing is undefined", () => {
76
+ it("silently skips without calling any nurturing methods", () => {
77
+ currentNurturing = undefined;
78
+
79
+ fireTeamMemberInvitedNurturing({
80
+ userId: "user-1",
81
+ teamMemberCount: 5,
82
+ role: "member",
83
+ });
84
+
85
+ expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
86
+ expect(mockNurturing.trackEvent).not.toHaveBeenCalled();
87
+ });
88
+ });
89
+
90
+ describe("when Customer.io API is unavailable", () => {
91
+ it("does not throw (fire-and-forget)", async () => {
92
+ const { captureException } = await import(
93
+ "../../../../src/utils/posthogErrorCapture"
94
+ );
95
+ mockNurturing.identifyUser.mockRejectedValueOnce(
96
+ new Error("CIO unavailable"),
97
+ );
98
+
99
+ expect(() =>
100
+ fireTeamMemberInvitedNurturing({
101
+ userId: "user-1",
102
+ teamMemberCount: 5,
103
+ role: "member",
104
+ }),
105
+ ).not.toThrow();
106
+
107
+ await vi.waitFor(() => {
108
+ expect(captureException).toHaveBeenCalled();
109
+ });
110
+ });
111
+ });
112
+ });
113
+
114
+ describe("fireWorkflowCreatedNurturing()", () => {
115
+ describe("when the workflow is saved", () => {
116
+ it("identifies user with updated workflow_count", () => {
117
+ fireWorkflowCreatedNurturing({
118
+ userId: "user-1",
119
+ workflowCount: 3,
120
+ workflowId: "wf-1",
121
+ projectId: "proj-1",
122
+ });
123
+
124
+ expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
125
+ userId: "user-1",
126
+ traits: { workflow_count: 3 },
127
+ });
128
+ });
129
+
130
+ it("tracks workflow_created event with workflow_id and project_id", () => {
131
+ fireWorkflowCreatedNurturing({
132
+ userId: "user-1",
133
+ workflowCount: 3,
134
+ workflowId: "wf-1",
135
+ projectId: "proj-1",
136
+ });
137
+
138
+ expect(mockNurturing.trackEvent).toHaveBeenCalledWith({
139
+ userId: "user-1",
140
+ event: "workflow_created",
141
+ properties: { workflow_id: "wf-1", project_id: "proj-1" },
142
+ });
143
+ });
144
+ });
145
+
146
+ describe("when nurturing is undefined", () => {
147
+ it("silently skips without calling any nurturing methods", () => {
148
+ currentNurturing = undefined;
149
+
150
+ fireWorkflowCreatedNurturing({
151
+ userId: "user-1",
152
+ workflowCount: 3,
153
+ workflowId: "wf-1",
154
+ projectId: "proj-1",
155
+ });
156
+
157
+ expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
158
+ expect(mockNurturing.trackEvent).not.toHaveBeenCalled();
159
+ });
160
+ });
161
+
162
+ describe("when Customer.io API is unavailable", () => {
163
+ it("does not throw (fire-and-forget)", async () => {
164
+ const { captureException } = await import(
165
+ "../../../../src/utils/posthogErrorCapture"
166
+ );
167
+ mockNurturing.identifyUser.mockRejectedValueOnce(
168
+ new Error("CIO unavailable"),
169
+ );
170
+
171
+ expect(() =>
172
+ fireWorkflowCreatedNurturing({
173
+ userId: "user-1",
174
+ workflowCount: 3,
175
+ workflowId: "wf-1",
176
+ projectId: "proj-1",
177
+ }),
178
+ ).not.toThrow();
179
+
180
+ await vi.waitFor(() => {
181
+ expect(captureException).toHaveBeenCalled();
182
+ });
183
+ });
184
+ });
185
+ });
186
+
187
+ describe("fireScenarioCreatedNurturing()", () => {
188
+ describe("when the scenario is saved", () => {
189
+ it("identifies user with updated scenario_count", () => {
190
+ fireScenarioCreatedNurturing({
191
+ userId: "user-1",
192
+ scenarioCount: 7,
193
+ scenarioId: "sc-1",
194
+ projectId: "proj-1",
195
+ });
196
+
197
+ expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
198
+ userId: "user-1",
199
+ traits: { scenario_count: 7 },
200
+ });
201
+ });
202
+
203
+ it("tracks scenario_created event with scenario_id and project_id", () => {
204
+ fireScenarioCreatedNurturing({
205
+ userId: "user-1",
206
+ scenarioCount: 7,
207
+ scenarioId: "sc-1",
208
+ projectId: "proj-1",
209
+ });
210
+
211
+ expect(mockNurturing.trackEvent).toHaveBeenCalledWith({
212
+ userId: "user-1",
213
+ event: "scenario_created",
214
+ properties: { scenario_id: "sc-1", project_id: "proj-1" },
215
+ });
216
+ });
217
+ });
218
+
219
+ describe("when nurturing is undefined", () => {
220
+ it("silently skips without calling any nurturing methods", () => {
221
+ currentNurturing = undefined;
222
+
223
+ fireScenarioCreatedNurturing({
224
+ userId: "user-1",
225
+ scenarioCount: 7,
226
+ scenarioId: "sc-1",
227
+ projectId: "proj-1",
228
+ });
229
+
230
+ expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
231
+ expect(mockNurturing.trackEvent).not.toHaveBeenCalled();
232
+ });
233
+ });
234
+
235
+ describe("when Customer.io API is unavailable", () => {
236
+ it("does not throw (fire-and-forget)", async () => {
237
+ const { captureException } = await import(
238
+ "../../../../src/utils/posthogErrorCapture"
239
+ );
240
+ mockNurturing.identifyUser.mockRejectedValueOnce(
241
+ new Error("CIO unavailable"),
242
+ );
243
+
244
+ expect(() =>
245
+ fireScenarioCreatedNurturing({
246
+ userId: "user-1",
247
+ scenarioCount: 7,
248
+ scenarioId: "sc-1",
249
+ projectId: "proj-1",
250
+ }),
251
+ ).not.toThrow();
252
+
253
+ await vi.waitFor(() => {
254
+ expect(captureException).toHaveBeenCalled();
255
+ });
256
+ });
257
+ });
258
+ });
259
+
260
+ describe("fireExperimentRanNurturing()", () => {
261
+ describe("when the experiment completes", () => {
262
+ it("tracks experiment_ran event with experiment_id and project_id", () => {
263
+ fireExperimentRanNurturing({
264
+ userId: "user-1",
265
+ experimentId: "exp-1",
266
+ projectId: "proj-1",
267
+ });
268
+
269
+ expect(mockNurturing.trackEvent).toHaveBeenCalledWith({
270
+ userId: "user-1",
271
+ event: "experiment_ran",
272
+ properties: { experiment_id: "exp-1", project_id: "proj-1" },
273
+ });
274
+ });
275
+
276
+ it("does not call identifyUser (no count trait for experiments)", () => {
277
+ fireExperimentRanNurturing({
278
+ userId: "user-1",
279
+ experimentId: "exp-1",
280
+ projectId: "proj-1",
281
+ });
282
+
283
+ expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
284
+ });
285
+ });
286
+
287
+ describe("when nurturing is undefined", () => {
288
+ it("silently skips without calling any nurturing methods", () => {
289
+ currentNurturing = undefined;
290
+
291
+ fireExperimentRanNurturing({
292
+ userId: "user-1",
293
+ experimentId: "exp-1",
294
+ projectId: "proj-1",
295
+ });
296
+
297
+ expect(mockNurturing.trackEvent).not.toHaveBeenCalled();
298
+ });
299
+ });
300
+
301
+ describe("when Customer.io API is unavailable", () => {
302
+ it("does not throw (fire-and-forget)", async () => {
303
+ const { captureException } = await import(
304
+ "../../../../src/utils/posthogErrorCapture"
305
+ );
306
+ mockNurturing.trackEvent.mockRejectedValueOnce(
307
+ new Error("CIO unavailable"),
308
+ );
309
+
310
+ expect(() =>
311
+ fireExperimentRanNurturing({
312
+ userId: "user-1",
313
+ experimentId: "exp-1",
314
+ projectId: "proj-1",
315
+ }),
316
+ ).not.toThrow();
317
+
318
+ await vi.waitFor(() => {
319
+ expect(captureException).toHaveBeenCalled();
320
+ });
321
+ });
322
+ });
323
+ });
324
+ });
@@ -0,0 +1,17 @@
1
+ export { fireSignupNurturingCalls } from "./signupIdentification";
2
+ export {
3
+ fireTeamMemberInvitedNurturing,
4
+ fireWorkflowCreatedNurturing,
5
+ fireScenarioCreatedNurturing,
6
+ fireExperimentRanNurturing,
7
+ } from "./featureAdoption";
8
+ export {
9
+ fireActivityTrackingNurturing,
10
+ resetActivityTrackingCache,
11
+ } from "./activityTracking";
12
+ export { ensureUserSyncedToCio, resetUserSyncCache } from "./userSync";
13
+ export {
14
+ fireIntegrationMethodNurturing,
15
+ mapProductSelectionToIntegrationMethod,
16
+ } from "./productInterest";
17
+ export type { IntegrationMethodValue } from "./productInterest";