@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,344 @@
1
+ import Stripe from "stripe";
2
+ import { afterAll, describe, expect, it } from "vitest";
3
+ import { STRIPE_PRICE_NAMES } from "../stripe/stripePrices.types";
4
+ import { getItemsToUpdate, prices } from "../services/subscriptionItemCalculator";
5
+ import { PlanTypes } from "../planTypes";
6
+
7
+ const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
8
+
9
+ if (!STRIPE_SECRET_KEY) {
10
+ console.warn(
11
+ "Skipping Stripe billing integration tests because STRIPE_SECRET_KEY is not set",
12
+ );
13
+ }
14
+
15
+ if (STRIPE_SECRET_KEY && !STRIPE_SECRET_KEY.startsWith("sk_test_")) {
16
+ throw new Error(
17
+ "SAFETY: Stripe integration tests require a TEST key (sk_test_*). " +
18
+ "A live key was detected — aborting to prevent charges on a real account.",
19
+ );
20
+ }
21
+
22
+ const describeIfStripeKey = STRIPE_SECRET_KEY ? describe : describe.skip;
23
+
24
+ describeIfStripeKey("Stripe billing integration", () => {
25
+ const stripe = new Stripe(STRIPE_SECRET_KEY!, { apiVersion: "2024-04-10" });
26
+
27
+ const createdCustomerIds: string[] = [];
28
+ const createdSubscriptionIds: string[] = [];
29
+
30
+ afterAll(async () => {
31
+ for (const subId of createdSubscriptionIds) {
32
+ try {
33
+ await stripe.subscriptions.cancel(subId);
34
+ } catch {
35
+ // already cancelled or cleaned up
36
+ }
37
+ }
38
+ for (const custId of createdCustomerIds) {
39
+ try {
40
+ await stripe.customers.del(custId);
41
+ } catch {
42
+ // already deleted or cleaned up
43
+ }
44
+ }
45
+ });
46
+
47
+ const createTrackedCustomer = async (suffix: string) => {
48
+ const customer = await stripe.customers.create({
49
+ email: `integration-test-${suffix}@langwatch.test`,
50
+ name: `Integration Test ${suffix}`,
51
+ });
52
+ createdCustomerIds.push(customer.id);
53
+ return customer;
54
+ };
55
+
56
+ const attachTestPaymentMethod = async (customerId: string) => {
57
+ const paymentMethod = await stripe.paymentMethods.create({
58
+ type: "card",
59
+ card: { token: "tok_visa" },
60
+ });
61
+ await stripe.paymentMethods.attach(paymentMethod.id, {
62
+ customer: customerId,
63
+ });
64
+ await stripe.customers.update(customerId, {
65
+ invoice_settings: { default_payment_method: paymentMethod.id },
66
+ });
67
+ return paymentMethod;
68
+ };
69
+
70
+ describe("managing customers", () => {
71
+ it("creates a customer with email and name", async () => {
72
+ const customer = await createTrackedCustomer("crud");
73
+
74
+ expect(customer.id).toMatch(/^cus_/);
75
+ expect(customer.email).toBe(
76
+ "integration-test-crud@langwatch.test",
77
+ );
78
+ expect(customer.name).toBe("Integration Test crud");
79
+ });
80
+
81
+ it("deletes a customer", async () => {
82
+ const customer = await createTrackedCustomer("delete");
83
+
84
+ const deleted = await stripe.customers.del(customer.id);
85
+
86
+ expect(deleted.deleted).toBe(true);
87
+ // Remove from cleanup list since it's already deleted
88
+ const idx = createdCustomerIds.indexOf(customer.id);
89
+ if (idx !== -1) createdCustomerIds.splice(idx, 1);
90
+ });
91
+ });
92
+
93
+ describe("price catalog verification", () => {
94
+ it("confirms all mapped price IDs are active in Stripe", async () => {
95
+ for (const priceName of STRIPE_PRICE_NAMES) {
96
+ const priceId = prices[priceName];
97
+ expect(priceId).toBeDefined();
98
+
99
+ const stripePrice = await stripe.prices.retrieve(priceId);
100
+
101
+ expect(stripePrice.active).toBe(true);
102
+ }
103
+ }, 120_000);
104
+
105
+ it("confirms base plan prices are recurring", async () => {
106
+ const basePriceNames = [
107
+ "LAUNCH",
108
+ "LAUNCH_ANNUAL",
109
+ "ACCELERATE",
110
+ "ACCELERATE_ANNUAL",
111
+ "PRO",
112
+ "GROWTH",
113
+ ] as const;
114
+
115
+ for (const name of basePriceNames) {
116
+ const priceId = prices[name];
117
+ const stripePrice = await stripe.prices.retrieve(priceId);
118
+
119
+ expect(stripePrice.type).toBe("recurring");
120
+ expect(stripePrice.recurring).not.toBeNull();
121
+ }
122
+ }, 120_000);
123
+ });
124
+
125
+ describe("subscription lifecycle", () => {
126
+ let customerId: string;
127
+ let subscriptionId: string;
128
+
129
+ it("sets up customer with payment method", async () => {
130
+ const customer = await createTrackedCustomer("lifecycle");
131
+ customerId = customer.id;
132
+ await attachTestPaymentMethod(customerId);
133
+ });
134
+
135
+ it("creates a subscription with LAUNCH base price", async () => {
136
+ const subscription = await stripe.subscriptions.create({
137
+ customer: customerId,
138
+ items: [{ price: prices.LAUNCH, quantity: 1 }],
139
+ });
140
+ subscriptionId = subscription.id;
141
+ createdSubscriptionIds.push(subscriptionId);
142
+
143
+ expect(subscription.status).toBe("active");
144
+ expect(subscription.items.data).toHaveLength(1);
145
+ expect(subscription.items.data[0]!.price.id).toBe(prices.LAUNCH);
146
+ });
147
+
148
+ it("retrieves the subscription and verifies data", async () => {
149
+ const subscription =
150
+ await stripe.subscriptions.retrieve(subscriptionId);
151
+
152
+ expect(subscription.id).toBe(subscriptionId);
153
+ expect(subscription.customer).toBe(customerId);
154
+ expect(subscription.status).toBe("active");
155
+ });
156
+
157
+ it("adds user and traces add-on items", async () => {
158
+ const updated = await stripe.subscriptions.update(subscriptionId, {
159
+ items: [
160
+ { price: prices.LAUNCH_USERS, quantity: 2 },
161
+ { price: prices.LAUNCH_TRACES_10K, quantity: 3 },
162
+ ],
163
+ });
164
+
165
+ const priceIds = updated.items.data.map((item) => item.price.id);
166
+ expect(priceIds).toContain(prices.LAUNCH);
167
+ expect(priceIds).toContain(prices.LAUNCH_USERS);
168
+ expect(priceIds).toContain(prices.LAUNCH_TRACES_10K);
169
+
170
+ const usersItem = updated.items.data.find(
171
+ (item) => item.price.id === prices.LAUNCH_USERS,
172
+ );
173
+ const tracesItem = updated.items.data.find(
174
+ (item) => item.price.id === prices.LAUNCH_TRACES_10K,
175
+ );
176
+ expect(usersItem!.quantity).toBe(2);
177
+ expect(tracesItem!.quantity).toBe(3);
178
+ });
179
+
180
+ it("switches from LAUNCH to ACCELERATE plan", async () => {
181
+ const current =
182
+ await stripe.subscriptions.retrieve(subscriptionId);
183
+ const currentItems = current.items.data;
184
+
185
+ // Delete all current items and add ACCELERATE base
186
+ const itemUpdates: Stripe.SubscriptionUpdateParams.Item[] =
187
+ currentItems.map((item) => ({ id: item.id, deleted: true }));
188
+ itemUpdates.push({ price: prices.ACCELERATE, quantity: 1 });
189
+
190
+ const updated = await stripe.subscriptions.update(subscriptionId, {
191
+ items: itemUpdates,
192
+ });
193
+
194
+ const priceIds = updated.items.data.map((item) => item.price.id);
195
+ expect(priceIds).toContain(prices.ACCELERATE);
196
+ expect(priceIds).not.toContain(prices.LAUNCH);
197
+ expect(priceIds).not.toContain(prices.LAUNCH_USERS);
198
+ expect(priceIds).not.toContain(prices.LAUNCH_TRACES_10K);
199
+ });
200
+
201
+ it("cancels the subscription", async () => {
202
+ const cancelled =
203
+ await stripe.subscriptions.cancel(subscriptionId);
204
+
205
+ expect(cancelled.status).toBe("canceled");
206
+ // Remove from cleanup since already cancelled
207
+ const idx = createdSubscriptionIds.indexOf(subscriptionId);
208
+ if (idx !== -1) createdSubscriptionIds.splice(idx, 1);
209
+ });
210
+ });
211
+
212
+ describe("checkout session creation", () => {
213
+ it("creates a checkout session matching subscriptionRouter.create payload", async () => {
214
+ const customer = await createTrackedCustomer("checkout");
215
+
216
+ const session = await stripe.checkout.sessions.create({
217
+ mode: "subscription",
218
+ customer: customer.id,
219
+ customer_update: {
220
+ address: "auto",
221
+ name: "auto",
222
+ },
223
+ automatic_tax: { enabled: true },
224
+ billing_address_collection: "required",
225
+ tax_id_collection: { enabled: true },
226
+ line_items: [{ price: prices.LAUNCH, quantity: 1 }],
227
+ success_url: "https://app.langwatch.test/settings/subscription?success",
228
+ cancel_url: "https://app.langwatch.test/settings/subscription",
229
+ client_reference_id: "subscription_setup_test-sub-id",
230
+ allow_promotion_codes: true,
231
+ });
232
+
233
+ expect(session.id).toMatch(/^cs_test_/);
234
+ expect(session.url).toContain("checkout.stripe.com");
235
+ expect(session.client_reference_id).toBe(
236
+ "subscription_setup_test-sub-id",
237
+ );
238
+ });
239
+ });
240
+
241
+ describe("billing portal session", () => {
242
+ it("creates a portal session for a customer", async () => {
243
+ const customer = await createTrackedCustomer("portal");
244
+
245
+ const session = await stripe.billingPortal.sessions.create({
246
+ customer: customer.id,
247
+ return_url: "https://app.langwatch.test/settings/subscription",
248
+ });
249
+
250
+ expect(session.url).toContain("billing.stripe.com");
251
+ expect(session.customer).toBe(customer.id);
252
+ });
253
+ });
254
+
255
+ describe("getItemsToUpdate with real Stripe data", () => {
256
+ it("produces output that Stripe accepts for a subscription update", async () => {
257
+ const customer = await createTrackedCustomer("bridge");
258
+ await attachTestPaymentMethod(customer.id);
259
+
260
+ // Create a LAUNCH subscription with add-ons
261
+ const subscription = await stripe.subscriptions.create({
262
+ customer: customer.id,
263
+ items: [
264
+ { price: prices.LAUNCH, quantity: 1 },
265
+ { price: prices.LAUNCH_USERS, quantity: 2 },
266
+ { price: prices.LAUNCH_TRACES_10K, quantity: 1 },
267
+ ],
268
+ });
269
+ createdSubscriptionIds.push(subscription.id);
270
+
271
+ // Feed real subscription items into getItemsToUpdate
272
+ const itemsToUpdate = getItemsToUpdate({
273
+ currentItems: subscription.items.data,
274
+ plan: PlanTypes.LAUNCH,
275
+ tracesToAdd: 50_000,
276
+ membersToAdd: 8,
277
+ });
278
+
279
+ // Apply to Stripe — the real proof it works
280
+ const updated = await stripe.subscriptions.update(subscription.id, {
281
+ items: itemsToUpdate,
282
+ });
283
+
284
+ expect(updated.status).toBe("active");
285
+
286
+ const tracesItem = updated.items.data.find(
287
+ (item) => item.price.id === prices.LAUNCH_TRACES_10K,
288
+ );
289
+ const usersItem = updated.items.data.find(
290
+ (item) => item.price.id === prices.LAUNCH_USERS,
291
+ );
292
+ // 50_000 total - 20_000 included = 30_000 extra / 10_000 = 3
293
+ expect(tracesItem!.quantity).toBe(3);
294
+ // 8 total - 3 included = 5
295
+ expect(usersItem!.quantity).toBe(5);
296
+ });
297
+
298
+ it("handles plan switch from LAUNCH to ACCELERATE via getItemsToUpdate", async () => {
299
+ const customer = await createTrackedCustomer("bridge-switch");
300
+ await attachTestPaymentMethod(customer.id);
301
+
302
+ const subscription = await stripe.subscriptions.create({
303
+ customer: customer.id,
304
+ items: [
305
+ { price: prices.LAUNCH, quantity: 1 },
306
+ { price: prices.LAUNCH_USERS, quantity: 2 },
307
+ { price: prices.LAUNCH_TRACES_10K, quantity: 3 },
308
+ ],
309
+ });
310
+ createdSubscriptionIds.push(subscription.id);
311
+
312
+ const itemsToUpdate = getItemsToUpdate({
313
+ currentItems: subscription.items.data,
314
+ plan: PlanTypes.ACCELERATE,
315
+ tracesToAdd: 250_000,
316
+ membersToAdd: 9,
317
+ });
318
+
319
+ const updated = await stripe.subscriptions.update(subscription.id, {
320
+ items: itemsToUpdate,
321
+ });
322
+
323
+ expect(updated.status).toBe("active");
324
+
325
+ const priceIds = updated.items.data.map((item) => item.price.id);
326
+ expect(priceIds).toContain(prices.ACCELERATE);
327
+ expect(priceIds).not.toContain(prices.LAUNCH);
328
+ expect(priceIds).not.toContain(prices.LAUNCH_USERS);
329
+ expect(priceIds).not.toContain(prices.LAUNCH_TRACES_10K);
330
+
331
+ // 250_000 - 20_000 included = 230_000 / 100_000 = 2
332
+ const tracesItem = updated.items.data.find(
333
+ (item) => item.price.id === prices.ACCELERATE_TRACES_100K,
334
+ );
335
+ expect(tracesItem!.quantity).toBe(2);
336
+
337
+ // 9 - 5 included = 4
338
+ const usersItem = updated.items.data.find(
339
+ (item) => item.price.id === prices.ACCELERATE_USERS,
340
+ );
341
+ expect(usersItem!.quantity).toBe(4);
342
+ });
343
+ });
344
+ });
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { STRIPE_PRICE_NAMES } from "../stripe/stripePrices.types";
3
+
4
+ describe("stripePrices.types", () => {
5
+ it("defines all required billing price keys", () => {
6
+ const requiredKeys = [
7
+ "PRO",
8
+ "GROWTH",
9
+ "LAUNCH",
10
+ "ACCELERATE",
11
+ "LAUNCH_USERS",
12
+ "ACCELERATE_USERS",
13
+ "LAUNCH_TRACES_10K",
14
+ "ACCELERATE_TRACES_100K",
15
+ ];
16
+
17
+ expect(STRIPE_PRICE_NAMES.length).toBeGreaterThanOrEqual(requiredKeys.length);
18
+
19
+ for (const key of requiredKeys) {
20
+ expect(STRIPE_PRICE_NAMES).toContain(key);
21
+ }
22
+ });
23
+
24
+ it("keeps required billing keys unique", () => {
25
+ expect(new Set(STRIPE_PRICE_NAMES).size).toBe(STRIPE_PRICE_NAMES.length);
26
+ });
27
+ });
@@ -0,0 +1,72 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import stripeCatalogData from "../stripe/stripeCatalog.json";
3
+ import {
4
+ getStripeEnvironmentFromNodeEnv,
5
+ parseStripePricesFile,
6
+ resolveStripePriceMap,
7
+ } from "../stripe/stripePriceCatalog";
8
+ import { STRIPE_PRICE_NAMES } from "../stripe/stripePrices.types";
9
+
10
+ describe("stripeCatalog", () => {
11
+ describe("parseStripePricesFile()", () => {
12
+ it("parses the committed stripe catalog file", () => {
13
+ const parsed = parseStripePricesFile(stripeCatalogData);
14
+
15
+ expect(parsed.schemaVersion).toBe(1);
16
+ expect(Object.keys(parsed.mapping)).toHaveLength(STRIPE_PRICE_NAMES.length);
17
+ expect(Object.keys(parsed.prices).length).toBeGreaterThan(0);
18
+ });
19
+
20
+ it("fails when one required key mapping is missing", () => {
21
+ const invalid = {
22
+ ...stripeCatalogData,
23
+ mapping: {
24
+ ...stripeCatalogData.mapping,
25
+ },
26
+ } as Record<string, unknown>;
27
+
28
+ delete (invalid.mapping as Record<string, unknown>).PRO;
29
+
30
+ expect(() => parseStripePricesFile(invalid)).toThrow(/PRO/);
31
+ });
32
+ });
33
+
34
+ describe("resolveStripePriceMap()", () => {
35
+ it("resolves test mode mappings", () => {
36
+ const parsed = parseStripePricesFile(stripeCatalogData);
37
+ const resolved = resolveStripePriceMap(parsed, "test");
38
+
39
+ for (const key of STRIPE_PRICE_NAMES) {
40
+ expect(resolved[key]).toBe(parsed.mapping[key].test);
41
+ }
42
+ });
43
+
44
+ it("resolves live mode mappings", () => {
45
+ const parsed = parseStripePricesFile(stripeCatalogData);
46
+ const resolved = resolveStripePriceMap(parsed, "live");
47
+
48
+ for (const key of STRIPE_PRICE_NAMES) {
49
+ expect(resolved[key]).toBe(parsed.mapping[key].live);
50
+ }
51
+ });
52
+
53
+ it("keeps test and live ids different in the committed catalog file", () => {
54
+ const parsed = parseStripePricesFile(stripeCatalogData);
55
+
56
+ for (const key of STRIPE_PRICE_NAMES) {
57
+ expect(parsed.mapping[key].test).not.toBe(parsed.mapping[key].live);
58
+ }
59
+ });
60
+ });
61
+
62
+ describe("getStripeEnvironmentFromNodeEnv()", () => {
63
+ it("returns live when node env is production", () => {
64
+ expect(getStripeEnvironmentFromNodeEnv("production")).toBe("live");
65
+ });
66
+
67
+ it("returns test when node env is not production", () => {
68
+ expect(getStripeEnvironmentFromNodeEnv("development")).toBe("test");
69
+ expect(getStripeEnvironmentFromNodeEnv("test")).toBe("test");
70
+ });
71
+ });
72
+ });