@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,515 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { StripeUsageReportingService } from "../services/usageReportingService";
3
+
4
+ const createMockStripe = () => ({
5
+ billing: {
6
+ meterEvents: { create: vi.fn() },
7
+ meters: { listEventSummaries: vi.fn() },
8
+ },
9
+ });
10
+
11
+ const makeEvent = (overrides: Record<string, unknown> = {}) => ({
12
+ eventName: "langwatch_billable_events",
13
+ value: 100,
14
+ timestamp: 1708300800,
15
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
16
+ ...overrides,
17
+ });
18
+
19
+ const makeStripeError = ({
20
+ type,
21
+ rawType,
22
+ code,
23
+ message = "error",
24
+ }: {
25
+ type: string;
26
+ rawType: string;
27
+ code?: string;
28
+ message?: string;
29
+ }) => {
30
+ const err = new Error(message);
31
+ (err as any).type = type;
32
+ (err as any).rawType = rawType;
33
+ (err as any).code = code;
34
+ return err;
35
+ };
36
+
37
+ describe("usageReportingService", () => {
38
+ let stripe: ReturnType<typeof createMockStripe>;
39
+ let service: StripeUsageReportingService;
40
+
41
+ beforeEach(() => {
42
+ stripe = createMockStripe();
43
+ service = new StripeUsageReportingService({
44
+ stripe: stripe as any,
45
+ meterId: "mtr_test_abc123",
46
+ });
47
+ });
48
+
49
+ describe("reportUsageDelta()", () => {
50
+ describe("when given valid events", () => {
51
+ it("calls Stripe API with correct params and returns reported: true", async () => {
52
+ stripe.billing.meterEvents.create.mockResolvedValue({});
53
+
54
+ const results = await service.reportUsageDelta({
55
+ stripeCustomerId: "cus_abc123",
56
+ organizationId: "org_1",
57
+ events: [makeEvent()],
58
+ });
59
+
60
+ expect(results).toEqual([
61
+ {
62
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
63
+ reported: true,
64
+ valueSent: 100,
65
+ },
66
+ ]);
67
+ expect(stripe.billing.meterEvents.create).toHaveBeenCalledWith({
68
+ event_name: "langwatch_billable_events",
69
+ payload: {
70
+ stripe_customer_id: "cus_abc123",
71
+ value: "100",
72
+ },
73
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
74
+ timestamp: 1708300800,
75
+ });
76
+ });
77
+ });
78
+
79
+ describe("when value is zero", () => {
80
+ it("skips and returns reported: false without calling Stripe", async () => {
81
+ const results = await service.reportUsageDelta({
82
+ stripeCustomerId: "cus_abc123",
83
+ organizationId: "org_1",
84
+ events: [makeEvent({ value: 0 })],
85
+ });
86
+
87
+ expect(results).toEqual([
88
+ {
89
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
90
+ reported: false,
91
+ valueSent: 0,
92
+ },
93
+ ]);
94
+ expect(stripe.billing.meterEvents.create).not.toHaveBeenCalled();
95
+ });
96
+ });
97
+
98
+ describe("when batch has multiple events", () => {
99
+ it("processes sequentially and returns ordered results", async () => {
100
+ const callOrder: number[] = [];
101
+ stripe.billing.meterEvents.create.mockImplementation(async () => {
102
+ callOrder.push(callOrder.length + 1);
103
+ return {};
104
+ });
105
+
106
+ const results = await service.reportUsageDelta({
107
+ stripeCustomerId: "cus_abc123",
108
+ organizationId: "org_1",
109
+ events: [
110
+ makeEvent({ value: 10, identifier: "id1" }),
111
+ makeEvent({ value: 20, identifier: "id2" }),
112
+ makeEvent({ value: 30, identifier: "id3" }),
113
+ ],
114
+ });
115
+
116
+ expect(results).toHaveLength(3);
117
+ expect(results[0]!.valueSent).toBe(10);
118
+ expect(results[1]!.valueSent).toBe(20);
119
+ expect(results[2]!.valueSent).toBe(30);
120
+ expect(callOrder).toEqual([1, 2, 3]);
121
+ });
122
+ });
123
+
124
+ describe("when Stripe returns resource_already_exists", () => {
125
+ it("treats as success (duplicate identifier)", async () => {
126
+ stripe.billing.meterEvents.create.mockRejectedValue(
127
+ makeStripeError({
128
+ type: "StripeInvalidRequestError",
129
+ rawType: "invalid_request_error",
130
+ code: "resource_already_exists",
131
+ })
132
+ );
133
+
134
+ const results = await service.reportUsageDelta({
135
+ stripeCustomerId: "cus_abc123",
136
+ organizationId: "org_1",
137
+ events: [makeEvent()],
138
+ });
139
+
140
+ expect(results).toEqual([
141
+ {
142
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
143
+ reported: true,
144
+ valueSent: 100,
145
+ },
146
+ ]);
147
+ });
148
+ });
149
+
150
+ describe("when Stripe returns other StripeInvalidRequestError", () => {
151
+ it("returns reported: false with error message", async () => {
152
+ stripe.billing.meterEvents.create.mockRejectedValue(
153
+ makeStripeError({
154
+ type: "StripeInvalidRequestError",
155
+ rawType: "invalid_request_error",
156
+ code: "meter_not_found",
157
+ message: "No such meter",
158
+ })
159
+ );
160
+
161
+ const results = await service.reportUsageDelta({
162
+ stripeCustomerId: "cus_abc123",
163
+ organizationId: "org_1",
164
+ events: [makeEvent()],
165
+ });
166
+
167
+ expect(results).toEqual([
168
+ {
169
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
170
+ reported: false,
171
+ valueSent: 0,
172
+ error: "No such meter",
173
+ },
174
+ ]);
175
+ });
176
+ });
177
+
178
+ describe("when Stripe returns StripeAuthenticationError", () => {
179
+ it("returns reported: false with error message", async () => {
180
+ stripe.billing.meterEvents.create.mockRejectedValue(
181
+ makeStripeError({
182
+ type: "StripeAuthenticationError",
183
+ rawType: "authentication_error",
184
+ message: "Invalid API Key",
185
+ })
186
+ );
187
+
188
+ const results = await service.reportUsageDelta({
189
+ stripeCustomerId: "cus_abc123",
190
+ organizationId: "org_1",
191
+ events: [makeEvent()],
192
+ });
193
+
194
+ expect(results).toEqual([
195
+ {
196
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
197
+ reported: false,
198
+ valueSent: 0,
199
+ error: "Invalid API Key",
200
+ },
201
+ ]);
202
+ });
203
+ });
204
+
205
+ describe("when Stripe returns StripeRateLimitError", () => {
206
+ it("re-throws for BullMQ retry", async () => {
207
+ const error = makeStripeError({
208
+ type: "StripeRateLimitError",
209
+ rawType: "rate_limit_error",
210
+ message: "Rate limit exceeded",
211
+ });
212
+ stripe.billing.meterEvents.create.mockRejectedValue(error);
213
+
214
+ await expect(
215
+ service.reportUsageDelta({
216
+ stripeCustomerId: "cus_abc123",
217
+ organizationId: "org_1",
218
+ events: [makeEvent()],
219
+ })
220
+ ).rejects.toThrow("Rate limit exceeded");
221
+ });
222
+ });
223
+
224
+ describe("when input validation fails", () => {
225
+ it("throws ZodError for missing stripeCustomerId prefix", async () => {
226
+ await expect(
227
+ service.reportUsageDelta({
228
+ stripeCustomerId: "invalid",
229
+ organizationId: "org_1",
230
+ events: [makeEvent()],
231
+ })
232
+ ).rejects.toThrow();
233
+ });
234
+
235
+ it("throws ZodError for empty events array", async () => {
236
+ await expect(
237
+ service.reportUsageDelta({
238
+ stripeCustomerId: "cus_abc123",
239
+ organizationId: "org_1",
240
+ events: [],
241
+ })
242
+ ).rejects.toThrow();
243
+ });
244
+
245
+ it("throws ZodError for negative value", async () => {
246
+ await expect(
247
+ service.reportUsageDelta({
248
+ stripeCustomerId: "cus_abc123",
249
+ organizationId: "org_1",
250
+ events: [makeEvent({ value: -1 })],
251
+ })
252
+ ).rejects.toThrow();
253
+ });
254
+ });
255
+ });
256
+
257
+ describe("getUsageSummary()", () => {
258
+ describe("when Stripe returns a summary", () => {
259
+ it("returns aggregatedValue from response", async () => {
260
+ stripe.billing.meters.listEventSummaries.mockResolvedValue({
261
+ data: [
262
+ {
263
+ aggregated_value: 42,
264
+ start_time: 1708300800,
265
+ end_time: 1708387200,
266
+ },
267
+ ],
268
+ });
269
+
270
+ const result = await service.getUsageSummary({
271
+ stripeCustomerId: "cus_abc123",
272
+ startTime: 1708300800,
273
+ endTime: 1708387200,
274
+ });
275
+
276
+ expect(result).toEqual({
277
+ aggregatedValue: 42,
278
+ startTime: 1708300800,
279
+ endTime: 1708387200,
280
+ });
281
+ expect(
282
+ stripe.billing.meters.listEventSummaries
283
+ ).toHaveBeenCalledWith("mtr_test_abc123", {
284
+ customer: "cus_abc123",
285
+ start_time: 1708300800,
286
+ end_time: 1708387200,
287
+ });
288
+ });
289
+ });
290
+
291
+ describe("when Stripe returns empty data", () => {
292
+ it("returns aggregatedValue: 0", async () => {
293
+ stripe.billing.meters.listEventSummaries.mockResolvedValue({
294
+ data: [],
295
+ });
296
+
297
+ const result = await service.getUsageSummary({
298
+ stripeCustomerId: "cus_abc123",
299
+ startTime: 1708300800,
300
+ endTime: 1708387200,
301
+ });
302
+
303
+ expect(result).toEqual({
304
+ aggregatedValue: 0,
305
+ startTime: 1708300800,
306
+ endTime: 1708387200,
307
+ });
308
+ });
309
+ });
310
+
311
+ describe("when Stripe returns StripeInvalidRequestError", () => {
312
+ it("re-throws the error", async () => {
313
+ stripe.billing.meters.listEventSummaries.mockRejectedValue(
314
+ makeStripeError({
315
+ type: "StripeInvalidRequestError",
316
+ rawType: "invalid_request_error",
317
+ message: "No such meter",
318
+ })
319
+ );
320
+
321
+ await expect(
322
+ service.getUsageSummary({
323
+ stripeCustomerId: "cus_abc123",
324
+ startTime: 1708300800,
325
+ endTime: 1708387200,
326
+ })
327
+ ).rejects.toThrow("No such meter");
328
+ });
329
+ });
330
+
331
+ describe("when input validation fails", () => {
332
+ it("throws ZodError for bad customer prefix", async () => {
333
+ await expect(
334
+ service.getUsageSummary({
335
+ stripeCustomerId: "invalid",
336
+ startTime: 1708300800,
337
+ endTime: 1708387200,
338
+ })
339
+ ).rejects.toThrow();
340
+ });
341
+
342
+ it("throws ZodError when endTime <= startTime", async () => {
343
+ await expect(
344
+ service.getUsageSummary({
345
+ stripeCustomerId: "cus_abc123",
346
+ startTime: 1708387200,
347
+ endTime: 1708300800,
348
+ })
349
+ ).rejects.toThrow();
350
+ });
351
+ });
352
+ });
353
+
354
+ describe("reportUsageSet()", () => {
355
+ describe("when delta is positive", () => {
356
+ it("sends value minus previouslyReportedValue to Stripe", async () => {
357
+ stripe.billing.meterEvents.create.mockResolvedValue({});
358
+
359
+ const results = await service.reportUsageSet({
360
+ stripeCustomerId: "cus_abc123",
361
+ organizationId: "org_1",
362
+ events: [
363
+ {
364
+ ...makeEvent({ value: 150 }),
365
+ previouslyReportedValue: 100,
366
+ },
367
+ ],
368
+ });
369
+
370
+ expect(results).toEqual([
371
+ {
372
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
373
+ reported: true,
374
+ valueSent: 50,
375
+ },
376
+ ]);
377
+ expect(stripe.billing.meterEvents.create).toHaveBeenCalledWith(
378
+ expect.objectContaining({
379
+ payload: expect.objectContaining({ value: "50" }),
380
+ })
381
+ );
382
+ });
383
+ });
384
+
385
+ describe("when delta is zero", () => {
386
+ it("skips and returns reported: false", async () => {
387
+ const results = await service.reportUsageSet({
388
+ stripeCustomerId: "cus_abc123",
389
+ organizationId: "org_1",
390
+ events: [
391
+ {
392
+ ...makeEvent({ value: 100 }),
393
+ previouslyReportedValue: 100,
394
+ },
395
+ ],
396
+ });
397
+
398
+ expect(results).toEqual([
399
+ {
400
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
401
+ reported: false,
402
+ valueSent: 0,
403
+ },
404
+ ]);
405
+ expect(stripe.billing.meterEvents.create).not.toHaveBeenCalled();
406
+ });
407
+ });
408
+
409
+ describe("when delta is negative", () => {
410
+ it("skips (count cannot decrease)", async () => {
411
+ const results = await service.reportUsageSet({
412
+ stripeCustomerId: "cus_abc123",
413
+ organizationId: "org_1",
414
+ events: [
415
+ {
416
+ ...makeEvent({ value: 50 }),
417
+ previouslyReportedValue: 100,
418
+ },
419
+ ],
420
+ });
421
+
422
+ expect(results).toEqual([
423
+ {
424
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
425
+ reported: false,
426
+ valueSent: 0,
427
+ },
428
+ ]);
429
+ expect(stripe.billing.meterEvents.create).not.toHaveBeenCalled();
430
+ });
431
+ });
432
+
433
+ describe("when previouslyReportedValue is zero", () => {
434
+ it("sends full value (first report)", async () => {
435
+ stripe.billing.meterEvents.create.mockResolvedValue({});
436
+
437
+ const results = await service.reportUsageSet({
438
+ stripeCustomerId: "cus_abc123",
439
+ organizationId: "org_1",
440
+ events: [
441
+ {
442
+ ...makeEvent({ value: 200 }),
443
+ previouslyReportedValue: 0,
444
+ },
445
+ ],
446
+ });
447
+
448
+ expect(results).toEqual([
449
+ {
450
+ identifier: "org_1:langwatch_billable_events:2026-02-19:d100",
451
+ reported: true,
452
+ valueSent: 200,
453
+ },
454
+ ]);
455
+ expect(stripe.billing.meterEvents.create).toHaveBeenCalledWith(
456
+ expect.objectContaining({
457
+ payload: expect.objectContaining({ value: "200" }),
458
+ })
459
+ );
460
+ });
461
+ });
462
+
463
+ describe("when batch has mixed deltas", () => {
464
+ it("only sends positive deltas to Stripe", async () => {
465
+ stripe.billing.meterEvents.create.mockResolvedValue({});
466
+
467
+ const results = await service.reportUsageSet({
468
+ stripeCustomerId: "cus_abc123",
469
+ organizationId: "org_1",
470
+ events: [
471
+ {
472
+ ...makeEvent({ value: 200, identifier: "id1" }),
473
+ previouslyReportedValue: 100,
474
+ },
475
+ {
476
+ ...makeEvent({ value: 50, identifier: "id2" }),
477
+ previouslyReportedValue: 50,
478
+ },
479
+ {
480
+ ...makeEvent({ value: 30, identifier: "id3" }),
481
+ previouslyReportedValue: 100,
482
+ },
483
+ {
484
+ ...makeEvent({ value: 500, identifier: "id4" }),
485
+ previouslyReportedValue: 0,
486
+ },
487
+ ],
488
+ });
489
+
490
+ expect(results).toHaveLength(4);
491
+ expect(results[0]).toEqual({
492
+ identifier: "id1",
493
+ reported: true,
494
+ valueSent: 100,
495
+ });
496
+ expect(results[1]).toEqual({
497
+ identifier: "id2",
498
+ reported: false,
499
+ valueSent: 0,
500
+ });
501
+ expect(results[2]).toEqual({
502
+ identifier: "id3",
503
+ reported: false,
504
+ valueSent: 0,
505
+ });
506
+ expect(results[3]).toEqual({
507
+ identifier: "id4",
508
+ reported: true,
509
+ valueSent: 500,
510
+ });
511
+ expect(stripe.billing.meterEvents.create).toHaveBeenCalledTimes(2);
512
+ });
513
+ });
514
+ });
515
+ });