@langwatch/server 3.1.0-beta.4 → 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 (118) 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/package.json +1 -1
  118. package/packages/server/dist/cli.cjs +1 -1
@@ -0,0 +1,183 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { createCustomerService } from "../services/customerService";
3
+ import {
4
+ CustomerCreationRaceError,
5
+ OrganizationNotFoundError,
6
+ UserEmailRequiredError,
7
+ } from "../errors";
8
+
9
+ const createMockStripe = () => ({
10
+ customers: {
11
+ create: vi.fn(),
12
+ del: vi.fn(),
13
+ },
14
+ });
15
+
16
+ const createMockDb = () => ({
17
+ organization: {
18
+ findUnique: vi.fn(),
19
+ findUniqueOrThrow: vi.fn(),
20
+ updateMany: vi.fn(),
21
+ },
22
+ });
23
+
24
+ describe("customerService", () => {
25
+ let stripe: ReturnType<typeof createMockStripe>;
26
+ let db: ReturnType<typeof createMockDb>;
27
+ let service: ReturnType<typeof createCustomerService>;
28
+
29
+ beforeEach(() => {
30
+ stripe = createMockStripe();
31
+ db = createMockDb();
32
+ service = createCustomerService({
33
+ stripe: stripe as any,
34
+ db: db as any,
35
+ });
36
+ });
37
+
38
+ describe("getOrCreateCustomerId()", () => {
39
+ describe("when organization not found", () => {
40
+ it("throws OrganizationNotFoundError", async () => {
41
+ db.organization.findUnique.mockResolvedValue(null);
42
+
43
+ await expect(
44
+ service.getOrCreateCustomerId({
45
+ user: { email: "test@example.com" },
46
+ organizationId: "org_missing",
47
+ }),
48
+ ).rejects.toThrow(OrganizationNotFoundError);
49
+ });
50
+ });
51
+
52
+ describe("when organization already has a Stripe customer", () => {
53
+ it("returns existing customer ID", async () => {
54
+ db.organization.findUnique.mockResolvedValue({
55
+ id: "org_123",
56
+ name: "Acme",
57
+ stripeCustomerId: "cus_existing",
58
+ });
59
+
60
+ const result = await service.getOrCreateCustomerId({
61
+ user: { email: "test@example.com" },
62
+ organizationId: "org_123",
63
+ });
64
+
65
+ expect(result).toBe("cus_existing");
66
+ expect(stripe.customers.create).not.toHaveBeenCalled();
67
+ });
68
+ });
69
+
70
+ describe("when user has no email", () => {
71
+ it("throws UserEmailRequiredError", async () => {
72
+ db.organization.findUnique.mockResolvedValue({
73
+ id: "org_123",
74
+ name: "Acme",
75
+ stripeCustomerId: null,
76
+ });
77
+
78
+ await expect(
79
+ service.getOrCreateCustomerId({
80
+ user: { email: null },
81
+ organizationId: "org_123",
82
+ }),
83
+ ).rejects.toThrow(UserEmailRequiredError);
84
+ });
85
+ });
86
+
87
+ describe("when creating a new customer", () => {
88
+ it("creates customer in Stripe and stores ID", async () => {
89
+ db.organization.findUnique.mockResolvedValue({
90
+ id: "org_123",
91
+ name: "Acme",
92
+ stripeCustomerId: null,
93
+ });
94
+ stripe.customers.create.mockResolvedValue({ id: "cus_new" });
95
+ db.organization.updateMany.mockResolvedValue({ count: 1 });
96
+
97
+ const result = await service.getOrCreateCustomerId({
98
+ user: { email: "test@example.com" },
99
+ organizationId: "org_123",
100
+ });
101
+
102
+ expect(result).toBe("cus_new");
103
+ expect(stripe.customers.create).toHaveBeenCalledWith({
104
+ email: "test@example.com",
105
+ name: "Acme",
106
+ });
107
+ expect(db.organization.updateMany).toHaveBeenCalledWith({
108
+ where: { id: "org_123", stripeCustomerId: null },
109
+ data: { stripeCustomerId: "cus_new" },
110
+ });
111
+ });
112
+ });
113
+
114
+ describe("when a race condition occurs", () => {
115
+ it("cleans up orphan and returns existing customer ID", async () => {
116
+ db.organization.findUnique.mockResolvedValue({
117
+ id: "org_123",
118
+ name: "Acme",
119
+ stripeCustomerId: null,
120
+ });
121
+ stripe.customers.create.mockResolvedValue({ id: "cus_orphan" });
122
+ db.organization.updateMany.mockResolvedValue({ count: 0 });
123
+ stripe.customers.del.mockResolvedValue({ deleted: true });
124
+ db.organization.findUniqueOrThrow.mockResolvedValue({
125
+ id: "org_123",
126
+ stripeCustomerId: "cus_winner",
127
+ });
128
+
129
+ const result = await service.getOrCreateCustomerId({
130
+ user: { email: "test@example.com" },
131
+ organizationId: "org_123",
132
+ });
133
+
134
+ expect(result).toBe("cus_winner");
135
+ expect(stripe.customers.del).toHaveBeenCalledWith("cus_orphan");
136
+ });
137
+
138
+ it("handles orphan cleanup failure gracefully", async () => {
139
+ db.organization.findUnique.mockResolvedValue({
140
+ id: "org_123",
141
+ name: "Acme",
142
+ stripeCustomerId: null,
143
+ });
144
+ stripe.customers.create.mockResolvedValue({ id: "cus_orphan" });
145
+ db.organization.updateMany.mockResolvedValue({ count: 0 });
146
+ stripe.customers.del.mockRejectedValue(new Error("Stripe API error"));
147
+ db.organization.findUniqueOrThrow.mockResolvedValue({
148
+ id: "org_123",
149
+ stripeCustomerId: "cus_winner",
150
+ });
151
+
152
+ const result = await service.getOrCreateCustomerId({
153
+ user: { email: "test@example.com" },
154
+ organizationId: "org_123",
155
+ });
156
+
157
+ expect(result).toBe("cus_winner");
158
+ });
159
+
160
+ it("throws CustomerCreationRaceError when refreshed org has no customer ID", async () => {
161
+ db.organization.findUnique.mockResolvedValue({
162
+ id: "org_123",
163
+ name: "Acme",
164
+ stripeCustomerId: null,
165
+ });
166
+ stripe.customers.create.mockResolvedValue({ id: "cus_orphan" });
167
+ db.organization.updateMany.mockResolvedValue({ count: 0 });
168
+ stripe.customers.del.mockResolvedValue({ deleted: true });
169
+ db.organization.findUniqueOrThrow.mockResolvedValue({
170
+ id: "org_123",
171
+ stripeCustomerId: null,
172
+ });
173
+
174
+ await expect(
175
+ service.getOrCreateCustomerId({
176
+ user: { email: "test@example.com" },
177
+ organizationId: "org_123",
178
+ }),
179
+ ).rejects.toThrow(CustomerCreationRaceError);
180
+ });
181
+ });
182
+ });
183
+ });
@@ -0,0 +1,213 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ vi.mock("../stripe/stripePriceCatalog", () => ({
4
+ prices: {
5
+ GROWTH_SEAT_EUR_MONTHLY: "price_seat_eur_monthly",
6
+ GROWTH_SEAT_EUR_ANNUAL: "price_seat_eur_annual",
7
+ GROWTH_SEAT_USD_MONTHLY: "price_seat_usd_monthly",
8
+ GROWTH_SEAT_USD_ANNUAL: "price_seat_usd_annual",
9
+ GROWTH_EVENTS_EUR_MONTHLY: "price_events_eur_monthly",
10
+ GROWTH_EVENTS_EUR_ANNUAL: "price_events_eur_annual",
11
+ GROWTH_EVENTS_USD_MONTHLY: "price_events_usd_monthly",
12
+ GROWTH_EVENTS_USD_ANNUAL: "price_events_usd_annual",
13
+ GROWTH_EVENTS_EUR_MONTHLY_UNTIL_MAR_2026: "price_events_eur_monthly_until_mar_2026",
14
+ GROWTH_EVENTS_EUR_ANNUAL_UNTIL_MAR_2026: "price_events_eur_annual_until_mar_2026",
15
+ GROWTH_EVENTS_USD_MONTHLY_UNTIL_MAR_2026: "price_events_usd_monthly_until_mar_2026",
16
+ GROWTH_EVENTS_USD_ANNUAL_UNTIL_MAR_2026: "price_events_usd_annual_until_mar_2026",
17
+ },
18
+ }));
19
+
20
+ import {
21
+ isGrowthSeatPrice,
22
+ isGrowthEventsPrice,
23
+ resolveGrowthSeatPriceId,
24
+ resolveGrowthEventsPriceId,
25
+ createCheckoutLineItems,
26
+ } from "../utils/growthSeatEvent";
27
+
28
+ describe("growthSeatEvent", () => {
29
+ describe("isGrowthSeatPrice()", () => {
30
+ describe("when given a growth seat price ID", () => {
31
+ it("returns true for EUR monthly", () => {
32
+ expect(isGrowthSeatPrice("price_seat_eur_monthly")).toBe(true);
33
+ });
34
+
35
+ it("returns true for EUR annual", () => {
36
+ expect(isGrowthSeatPrice("price_seat_eur_annual")).toBe(true);
37
+ });
38
+
39
+ it("returns true for USD monthly", () => {
40
+ expect(isGrowthSeatPrice("price_seat_usd_monthly")).toBe(true);
41
+ });
42
+
43
+ it("returns true for USD annual", () => {
44
+ expect(isGrowthSeatPrice("price_seat_usd_annual")).toBe(true);
45
+ });
46
+ });
47
+
48
+ describe("when given a non-seat price ID", () => {
49
+ it("returns false for an events price", () => {
50
+ expect(isGrowthSeatPrice("price_events_eur_monthly")).toBe(false);
51
+ });
52
+
53
+ it("returns false for an unknown price", () => {
54
+ expect(isGrowthSeatPrice("price_unknown")).toBe(false);
55
+ });
56
+ });
57
+ });
58
+
59
+ describe("isGrowthEventsPrice()", () => {
60
+ describe("when given a growth events price ID", () => {
61
+ it("returns true for EUR monthly", () => {
62
+ expect(isGrowthEventsPrice("price_events_eur_monthly")).toBe(true);
63
+ });
64
+
65
+ it("returns true for EUR annual", () => {
66
+ expect(isGrowthEventsPrice("price_events_eur_annual")).toBe(true);
67
+ });
68
+
69
+ it("returns true for USD monthly", () => {
70
+ expect(isGrowthEventsPrice("price_events_usd_monthly")).toBe(true);
71
+ });
72
+
73
+ it("returns true for USD annual", () => {
74
+ expect(isGrowthEventsPrice("price_events_usd_annual")).toBe(true);
75
+ });
76
+ });
77
+
78
+ describe("when given a pre-March 2026 growth events price ID", () => {
79
+ it("returns true for EUR monthly pre-March 2026", () => {
80
+ expect(isGrowthEventsPrice("price_events_eur_monthly_until_mar_2026")).toBe(true);
81
+ });
82
+
83
+ it("returns true for EUR annual pre-March 2026", () => {
84
+ expect(isGrowthEventsPrice("price_events_eur_annual_until_mar_2026")).toBe(true);
85
+ });
86
+
87
+ it("returns true for USD monthly pre-March 2026", () => {
88
+ expect(isGrowthEventsPrice("price_events_usd_monthly_until_mar_2026")).toBe(true);
89
+ });
90
+
91
+ it("returns true for USD annual pre-March 2026", () => {
92
+ expect(isGrowthEventsPrice("price_events_usd_annual_until_mar_2026")).toBe(true);
93
+ });
94
+ });
95
+
96
+ describe("when given a non-events price ID", () => {
97
+ it("returns false for a seat price", () => {
98
+ expect(isGrowthEventsPrice("price_seat_eur_monthly")).toBe(false);
99
+ });
100
+
101
+ it("returns false for an unknown price", () => {
102
+ expect(isGrowthEventsPrice("price_unknown")).toBe(false);
103
+ });
104
+ });
105
+ });
106
+
107
+ describe("resolveGrowthSeatPriceId()", () => {
108
+ describe("when resolving EUR prices", () => {
109
+ it("returns the monthly EUR seat price", () => {
110
+ expect(resolveGrowthSeatPriceId({ currency: "EUR", interval: "monthly" })).toBe(
111
+ "price_seat_eur_monthly",
112
+ );
113
+ });
114
+
115
+ it("returns the annual EUR seat price", () => {
116
+ expect(resolveGrowthSeatPriceId({ currency: "EUR", interval: "annual" })).toBe(
117
+ "price_seat_eur_annual",
118
+ );
119
+ });
120
+ });
121
+
122
+ describe("when resolving USD prices", () => {
123
+ it("returns the monthly USD seat price", () => {
124
+ expect(resolveGrowthSeatPriceId({ currency: "USD", interval: "monthly" })).toBe(
125
+ "price_seat_usd_monthly",
126
+ );
127
+ });
128
+
129
+ it("returns the annual USD seat price", () => {
130
+ expect(resolveGrowthSeatPriceId({ currency: "USD", interval: "annual" })).toBe(
131
+ "price_seat_usd_annual",
132
+ );
133
+ });
134
+ });
135
+ });
136
+
137
+ describe("resolveGrowthEventsPriceId()", () => {
138
+ describe("when resolving EUR prices", () => {
139
+ it("returns the monthly EUR events price", () => {
140
+ expect(resolveGrowthEventsPriceId({ currency: "EUR", interval: "monthly" })).toBe(
141
+ "price_events_eur_monthly",
142
+ );
143
+ });
144
+
145
+ it("returns the annual EUR events price", () => {
146
+ expect(resolveGrowthEventsPriceId({ currency: "EUR", interval: "annual" })).toBe(
147
+ "price_events_eur_annual",
148
+ );
149
+ });
150
+ });
151
+
152
+ describe("when resolving USD prices", () => {
153
+ it("returns the monthly USD events price", () => {
154
+ expect(resolveGrowthEventsPriceId({ currency: "USD", interval: "monthly" })).toBe(
155
+ "price_events_usd_monthly",
156
+ );
157
+ });
158
+
159
+ it("returns the annual USD events price", () => {
160
+ expect(resolveGrowthEventsPriceId({ currency: "USD", interval: "annual" })).toBe(
161
+ "price_events_usd_annual",
162
+ );
163
+ });
164
+ });
165
+ });
166
+
167
+ describe("createCheckoutLineItems()", () => {
168
+ describe("when creating line items for EUR monthly", () => {
169
+ it("returns seat and events line items with correct quantities", () => {
170
+ const items = createCheckoutLineItems({
171
+ coreMembers: 5,
172
+ currency: "EUR",
173
+ interval: "monthly",
174
+ });
175
+
176
+ expect(items).toEqual([
177
+ { price: "price_seat_eur_monthly", quantity: 5 },
178
+ { price: "price_events_eur_monthly" },
179
+ ]);
180
+ });
181
+ });
182
+
183
+ describe("when creating line items for USD annual", () => {
184
+ it("returns seat and events line items with correct quantities", () => {
185
+ const items = createCheckoutLineItems({
186
+ coreMembers: 3,
187
+ currency: "USD",
188
+ interval: "annual",
189
+ });
190
+
191
+ expect(items).toEqual([
192
+ { price: "price_seat_usd_annual", quantity: 3 },
193
+ { price: "price_events_usd_annual" },
194
+ ]);
195
+ });
196
+ });
197
+
198
+ describe("when creating line items with a single member", () => {
199
+ it("returns seat quantity of 1", () => {
200
+ const items = createCheckoutLineItems({
201
+ coreMembers: 1,
202
+ currency: "EUR",
203
+ interval: "annual",
204
+ });
205
+
206
+ expect(items).toEqual([
207
+ { price: "price_seat_eur_annual", quantity: 1 },
208
+ { price: "price_events_eur_annual" },
209
+ ]);
210
+ });
211
+ });
212
+ });
213
+ });
@@ -0,0 +1,250 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ vi.mock("../../licensing/licenseGenerationService", () => ({
4
+ generateLicenseKey: vi.fn().mockReturnValue({
5
+ licenseKey: "test-license-key-base64",
6
+ licenseData: {
7
+ licenseId: "lic-test-123",
8
+ version: 1,
9
+ organizationName: "Acme Corp",
10
+ email: "buyer@acme.com",
11
+ issuedAt: "2026-03-02T00:00:00.000Z",
12
+ expiresAt: "2027-03-02T00:00:00.000Z",
13
+ plan: { type: "GROWTH", name: "Growth", maxMembers: 5 },
14
+ },
15
+ }),
16
+ }));
17
+
18
+ vi.mock("../../../src/server/mailer/licenseEmail", () => ({
19
+ sendLicenseEmail: vi.fn().mockResolvedValue(undefined),
20
+ }));
21
+
22
+ const mockSendSlackLicensePurchase = vi.fn().mockResolvedValue(undefined);
23
+
24
+ vi.mock("../../../src/server/app-layer/app", () => ({
25
+ getApp: () => ({
26
+ notifications: {
27
+ sendSlackLicensePurchase: mockSendSlackLicensePurchase,
28
+ },
29
+ }),
30
+ }));
31
+
32
+ import { generateLicenseKey } from "../../licensing/licenseGenerationService";
33
+ import { sendLicenseEmail } from "../../../src/server/mailer/licenseEmail";
34
+ import { handleLicensePurchase } from "../services/licensePurchaseHandler";
35
+
36
+ const mockGenerateLicenseKey = generateLicenseKey as ReturnType<typeof vi.fn>;
37
+ const mockSendLicenseEmail = sendLicenseEmail as ReturnType<typeof vi.fn>;
38
+
39
+ const createMockStripe = () => ({
40
+ checkout: {
41
+ sessions: {
42
+ listLineItems: vi.fn().mockResolvedValue({
43
+ data: [{ quantity: 5 }],
44
+ }),
45
+ },
46
+ },
47
+ });
48
+
49
+ const createMockCheckoutSession = (overrides: Record<string, unknown> = {}) =>
50
+ ({
51
+ id: "cs_test_123",
52
+ customer_details: {
53
+ email: "buyer@acme.com",
54
+ name: "Acme Corp",
55
+ },
56
+ amount_total: 14900,
57
+ currency: "usd",
58
+ ...overrides,
59
+ }) as any;
60
+
61
+ describe("licensePurchaseHandler", () => {
62
+ let mockStripe: ReturnType<typeof createMockStripe>;
63
+
64
+ beforeEach(() => {
65
+ vi.clearAllMocks();
66
+ mockStripe = createMockStripe();
67
+ });
68
+
69
+ describe("handleLicensePurchase()", () => {
70
+ describe("when checkout session has valid buyer details", () => {
71
+ it("generates a GROWTH license with the correct seat count", async () => {
72
+ const session = createMockCheckoutSession();
73
+
74
+ await handleLicensePurchase({
75
+ checkoutSession: session,
76
+ stripe: mockStripe as any,
77
+ privateKey: "test-private-key",
78
+ });
79
+
80
+ expect(mockGenerateLicenseKey).toHaveBeenCalledWith({
81
+ organizationName: "Acme Corp",
82
+ email: "buyer@acme.com",
83
+ planType: "GROWTH",
84
+ maxMembers: 5,
85
+ privateKey: "test-private-key",
86
+ });
87
+ });
88
+
89
+ it("sends the license email to the buyer", async () => {
90
+ const session = createMockCheckoutSession();
91
+
92
+ await handleLicensePurchase({
93
+ checkoutSession: session,
94
+ stripe: mockStripe as any,
95
+ privateKey: "test-private-key",
96
+ });
97
+
98
+ expect(mockSendLicenseEmail).toHaveBeenCalledWith({
99
+ email: "buyer@acme.com",
100
+ licenseKey: "test-license-key-base64",
101
+ planType: "GROWTH",
102
+ maxMembers: 5,
103
+ expiresAt: "2027-03-02T00:00:00.000Z",
104
+ organizationName: "Acme Corp",
105
+ });
106
+ });
107
+
108
+ it("notifies Slack with purchase details", async () => {
109
+ const session = createMockCheckoutSession();
110
+
111
+ await handleLicensePurchase({
112
+ checkoutSession: session,
113
+ stripe: mockStripe as any,
114
+ privateKey: "test-private-key",
115
+ });
116
+
117
+ expect(mockSendSlackLicensePurchase).toHaveBeenCalledWith({
118
+ buyerEmail: "buyer@acme.com",
119
+ planType: "GROWTH",
120
+ seats: 5,
121
+ amountPaid: 14900,
122
+ currency: "usd",
123
+ });
124
+ });
125
+ });
126
+
127
+ describe("when checkout session has no email", () => {
128
+ it("throws an error", async () => {
129
+ const session = createMockCheckoutSession({
130
+ customer_details: { email: null, name: "Acme Corp" },
131
+ });
132
+
133
+ await expect(
134
+ handleLicensePurchase({
135
+ checkoutSession: session,
136
+ stripe: mockStripe as any,
137
+ privateKey: "test-private-key",
138
+ }),
139
+ ).rejects.toThrow("No email found in checkout session customer_details");
140
+ });
141
+ });
142
+
143
+ describe("when line items have no quantity", () => {
144
+ it("defaults to 1 seat", async () => {
145
+ const session = createMockCheckoutSession();
146
+ mockStripe.checkout.sessions.listLineItems.mockResolvedValue({
147
+ data: [{ quantity: null }],
148
+ });
149
+
150
+ await handleLicensePurchase({
151
+ checkoutSession: session,
152
+ stripe: mockStripe as any,
153
+ privateKey: "test-private-key",
154
+ });
155
+
156
+ expect(mockGenerateLicenseKey).toHaveBeenCalledWith(
157
+ expect.objectContaining({ maxMembers: 1 }),
158
+ );
159
+ });
160
+ });
161
+
162
+ describe("when line items are empty", () => {
163
+ it("defaults to 1 seat", async () => {
164
+ const session = createMockCheckoutSession();
165
+ mockStripe.checkout.sessions.listLineItems.mockResolvedValue({
166
+ data: [],
167
+ });
168
+
169
+ await handleLicensePurchase({
170
+ checkoutSession: session,
171
+ stripe: mockStripe as any,
172
+ privateKey: "test-private-key",
173
+ });
174
+
175
+ expect(mockGenerateLicenseKey).toHaveBeenCalledWith(
176
+ expect.objectContaining({ maxMembers: 1 }),
177
+ );
178
+ });
179
+ });
180
+
181
+ describe("when business name is missing", () => {
182
+ it("passes empty string to license generation", async () => {
183
+ const session = createMockCheckoutSession({
184
+ customer_details: { email: "buyer@solo.dev", name: null },
185
+ });
186
+
187
+ await handleLicensePurchase({
188
+ checkoutSession: session,
189
+ stripe: mockStripe as any,
190
+ privateKey: "test-private-key",
191
+ });
192
+
193
+ expect(mockGenerateLicenseKey).toHaveBeenCalledWith(
194
+ expect.objectContaining({
195
+ organizationName: "",
196
+ email: "buyer@solo.dev",
197
+ }),
198
+ );
199
+ });
200
+ });
201
+
202
+ describe("when amount_total is null", () => {
203
+ it("uses 0 for Slack notification amount", async () => {
204
+ const session = createMockCheckoutSession({ amount_total: null });
205
+
206
+ await handleLicensePurchase({
207
+ checkoutSession: session,
208
+ stripe: mockStripe as any,
209
+ privateKey: "test-private-key",
210
+ });
211
+
212
+ expect(mockSendSlackLicensePurchase).toHaveBeenCalledWith(
213
+ expect.objectContaining({ amountPaid: 0 }),
214
+ );
215
+ });
216
+ });
217
+
218
+ describe("when currency is null", () => {
219
+ it("defaults to usd for Slack notification", async () => {
220
+ const session = createMockCheckoutSession({ currency: null });
221
+
222
+ await handleLicensePurchase({
223
+ checkoutSession: session,
224
+ stripe: mockStripe as any,
225
+ privateKey: "test-private-key",
226
+ });
227
+
228
+ expect(mockSendSlackLicensePurchase).toHaveBeenCalledWith(
229
+ expect.objectContaining({ currency: "usd" }),
230
+ );
231
+ });
232
+ });
233
+
234
+ describe("when stripe listLineItems is called", () => {
235
+ it("passes the checkout session ID", async () => {
236
+ const session = createMockCheckoutSession({ id: "cs_specific_456" });
237
+
238
+ await handleLicensePurchase({
239
+ checkoutSession: session,
240
+ stripe: mockStripe as any,
241
+ privateKey: "test-private-key",
242
+ });
243
+
244
+ expect(
245
+ mockStripe.checkout.sessions.listLineItems,
246
+ ).toHaveBeenCalledWith("cs_specific_456");
247
+ });
248
+ });
249
+ });
250
+ });