@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.
- package/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
- package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
- package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
- package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
- package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
- package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
- package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
- package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
- package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
- package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
- package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
- package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
- package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
- package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
- package/langwatch/ee/admin/impersonation.service.ts +170 -0
- package/langwatch/ee/admin/isAdmin.ts +9 -0
- package/langwatch/ee/admin/routes/admin.ts +531 -0
- package/langwatch/ee/admin/safeSelects.ts +64 -0
- package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
- package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
- package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
- package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
- package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
- package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
- package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
- package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
- package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
- package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
- package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
- package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
- package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
- package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
- package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
- package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
- package/langwatch/ee/billing/currencyRouter.ts +18 -0
- package/langwatch/ee/billing/errors.ts +129 -0
- package/langwatch/ee/billing/index.ts +67 -0
- package/langwatch/ee/billing/middleware.ts +17 -0
- package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
- package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
- package/langwatch/ee/billing/notifications/types.ts +9 -0
- package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
- package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
- package/langwatch/ee/billing/nurturing/types.ts +125 -0
- package/langwatch/ee/billing/planLimits.ts +190 -0
- package/langwatch/ee/billing/planProvider.ts +119 -0
- package/langwatch/ee/billing/planTypes.ts +53 -0
- package/langwatch/ee/billing/pricing.ts +57 -0
- package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
- package/langwatch/ee/billing/services/customerService.ts +90 -0
- package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
- package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
- package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
- package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
- package/langwatch/ee/billing/services/subscription.service.ts +549 -0
- package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
- package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
- package/langwatch/ee/billing/services/webhookService.ts +858 -0
- package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
- package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
- package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
- package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
- package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
- package/langwatch/ee/billing/types.ts +102 -0
- package/langwatch/ee/billing/utils/currency.ts +99 -0
- package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
- package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
- package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
- package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
- package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
- package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
- package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
- package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
- package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
- package/langwatch/ee/licensing/client.ts +29 -0
- package/langwatch/ee/licensing/constants.ts +195 -0
- package/langwatch/ee/licensing/defaults.ts +55 -0
- package/langwatch/ee/licensing/errors.ts +11 -0
- package/langwatch/ee/licensing/index.ts +52 -0
- package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
- package/langwatch/ee/licensing/licenseHandler.ts +308 -0
- package/langwatch/ee/licensing/planInfo.ts +46 -0
- package/langwatch/ee/licensing/planMapping.ts +37 -0
- package/langwatch/ee/licensing/planTemplates.ts +109 -0
- package/langwatch/ee/licensing/server.ts +43 -0
- package/langwatch/ee/licensing/signing.ts +43 -0
- package/langwatch/ee/licensing/types.ts +156 -0
- package/langwatch/ee/licensing/validation.ts +107 -0
- package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
- package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
- package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
- package/langwatch/package.json +1 -1
- package/langwatch_nlp/uv.lock +179 -5
- package/package.json +1 -1
- package/packages/server/dist/cli.cjs +192 -142
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import { afterAll, describe, expect, it } from "vitest";
|
|
3
|
+
import { StripeUsageReportingService } from "../services/usageReportingService";
|
|
4
|
+
import { meters } from "../stripe/stripePriceCatalog";
|
|
5
|
+
|
|
6
|
+
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
|
7
|
+
|
|
8
|
+
if (!STRIPE_SECRET_KEY) {
|
|
9
|
+
console.warn(
|
|
10
|
+
"Skipping usage reporting integration tests because STRIPE_SECRET_KEY is not set"
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (STRIPE_SECRET_KEY && !STRIPE_SECRET_KEY.startsWith("sk_test_")) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
"SAFETY: Stripe integration tests require a TEST key (sk_test_*). " +
|
|
17
|
+
"A live key was detected — aborting to prevent charges on a real account."
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const describeIfStripeKey = STRIPE_SECRET_KEY ? describe : describe.skip;
|
|
22
|
+
|
|
23
|
+
describeIfStripeKey("Usage reporting integration", () => {
|
|
24
|
+
const stripe = new Stripe(STRIPE_SECRET_KEY!, { apiVersion: "2024-04-10" });
|
|
25
|
+
const service = new StripeUsageReportingService({
|
|
26
|
+
stripe,
|
|
27
|
+
meterId: meters.BILLABLE_EVENTS,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const createdCustomerIds: string[] = [];
|
|
31
|
+
|
|
32
|
+
afterAll(async () => {
|
|
33
|
+
for (const custId of createdCustomerIds) {
|
|
34
|
+
try {
|
|
35
|
+
await stripe.customers.del(custId);
|
|
36
|
+
} catch {
|
|
37
|
+
// already deleted or cleaned up
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const createTrackedCustomer = async (suffix: string) => {
|
|
43
|
+
const customer = await stripe.customers.create({
|
|
44
|
+
email: `integration-test-usage-${suffix}@langwatch.test`,
|
|
45
|
+
name: `Integration Test Usage ${suffix}`,
|
|
46
|
+
});
|
|
47
|
+
createdCustomerIds.push(customer.id);
|
|
48
|
+
return customer;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const uniqueId = () =>
|
|
52
|
+
`test-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
53
|
+
|
|
54
|
+
describe("reportUsageDelta()", () => {
|
|
55
|
+
it("sends a meter event to Stripe and returns reported: true", async () => {
|
|
56
|
+
const customer = await createTrackedCustomer("delta-basic");
|
|
57
|
+
const identifier = uniqueId();
|
|
58
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
59
|
+
|
|
60
|
+
const results = await service.reportUsageDelta({
|
|
61
|
+
stripeCustomerId: customer.id,
|
|
62
|
+
organizationId: "org_integ_test",
|
|
63
|
+
events: [
|
|
64
|
+
{
|
|
65
|
+
eventName: "langwatch_billable_events",
|
|
66
|
+
value: 42,
|
|
67
|
+
timestamp,
|
|
68
|
+
identifier,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
expect(results).toHaveLength(1);
|
|
74
|
+
expect(results[0]).toEqual({
|
|
75
|
+
identifier,
|
|
76
|
+
reported: true,
|
|
77
|
+
valueSent: 42,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("handles duplicate identifier gracefully (does not throw)", async () => {
|
|
82
|
+
const customer = await createTrackedCustomer("delta-dup");
|
|
83
|
+
const identifier = uniqueId();
|
|
84
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
85
|
+
|
|
86
|
+
const input = {
|
|
87
|
+
stripeCustomerId: customer.id,
|
|
88
|
+
organizationId: "org_integ_test",
|
|
89
|
+
events: [
|
|
90
|
+
{
|
|
91
|
+
eventName: "langwatch_billable_events",
|
|
92
|
+
value: 10,
|
|
93
|
+
timestamp,
|
|
94
|
+
identifier,
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// First call succeeds
|
|
100
|
+
const first = await service.reportUsageDelta(input);
|
|
101
|
+
expect(first[0]!.reported).toBe(true);
|
|
102
|
+
|
|
103
|
+
// Second call with same identifier — Stripe may return
|
|
104
|
+
// resource_already_exists (treated as success) or another
|
|
105
|
+
// InvalidRequestError code. Either way, handled without throwing.
|
|
106
|
+
const second = await service.reportUsageDelta(input);
|
|
107
|
+
expect(second).toHaveLength(1);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("skips zero-value events without calling Stripe", async () => {
|
|
111
|
+
const customer = await createTrackedCustomer("delta-zero");
|
|
112
|
+
const identifier = uniqueId();
|
|
113
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
114
|
+
|
|
115
|
+
const results = await service.reportUsageDelta({
|
|
116
|
+
stripeCustomerId: customer.id,
|
|
117
|
+
organizationId: "org_integ_test",
|
|
118
|
+
events: [
|
|
119
|
+
{
|
|
120
|
+
eventName: "langwatch_billable_events",
|
|
121
|
+
value: 0,
|
|
122
|
+
timestamp,
|
|
123
|
+
identifier,
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
expect(results).toEqual([
|
|
129
|
+
{ identifier, reported: false, valueSent: 0 },
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("processes a batch of multiple events sequentially", async () => {
|
|
134
|
+
const customer = await createTrackedCustomer("delta-batch");
|
|
135
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
136
|
+
|
|
137
|
+
const results = await service.reportUsageDelta({
|
|
138
|
+
stripeCustomerId: customer.id,
|
|
139
|
+
organizationId: "org_integ_test",
|
|
140
|
+
events: [
|
|
141
|
+
{
|
|
142
|
+
eventName: "langwatch_billable_events",
|
|
143
|
+
value: 5,
|
|
144
|
+
timestamp,
|
|
145
|
+
identifier: uniqueId(),
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
eventName: "langwatch_billable_events",
|
|
149
|
+
value: 15,
|
|
150
|
+
timestamp,
|
|
151
|
+
identifier: uniqueId(),
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
eventName: "langwatch_billable_events",
|
|
155
|
+
value: 25,
|
|
156
|
+
timestamp,
|
|
157
|
+
identifier: uniqueId(),
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
expect(results).toHaveLength(3);
|
|
163
|
+
expect(results[0]!.reported).toBe(true);
|
|
164
|
+
expect(results[0]!.valueSent).toBe(5);
|
|
165
|
+
expect(results[1]!.reported).toBe(true);
|
|
166
|
+
expect(results[1]!.valueSent).toBe(15);
|
|
167
|
+
expect(results[2]!.reported).toBe(true);
|
|
168
|
+
expect(results[2]!.valueSent).toBe(25);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("reportUsageSet()", () => {
|
|
173
|
+
it("sends the delta between value and previouslyReportedValue", async () => {
|
|
174
|
+
const customer = await createTrackedCustomer("set-basic");
|
|
175
|
+
const identifier = uniqueId();
|
|
176
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
177
|
+
|
|
178
|
+
const results = await service.reportUsageSet({
|
|
179
|
+
stripeCustomerId: customer.id,
|
|
180
|
+
organizationId: "org_integ_test",
|
|
181
|
+
events: [
|
|
182
|
+
{
|
|
183
|
+
eventName: "langwatch_billable_events",
|
|
184
|
+
value: 200,
|
|
185
|
+
previouslyReportedValue: 150,
|
|
186
|
+
timestamp,
|
|
187
|
+
identifier,
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
expect(results).toHaveLength(1);
|
|
193
|
+
expect(results[0]).toEqual({
|
|
194
|
+
identifier,
|
|
195
|
+
reported: true,
|
|
196
|
+
valueSent: 50,
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("skips when delta is zero (no change)", async () => {
|
|
201
|
+
const customer = await createTrackedCustomer("set-zero");
|
|
202
|
+
const identifier = uniqueId();
|
|
203
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
204
|
+
|
|
205
|
+
const results = await service.reportUsageSet({
|
|
206
|
+
stripeCustomerId: customer.id,
|
|
207
|
+
organizationId: "org_integ_test",
|
|
208
|
+
events: [
|
|
209
|
+
{
|
|
210
|
+
eventName: "langwatch_billable_events",
|
|
211
|
+
value: 100,
|
|
212
|
+
previouslyReportedValue: 100,
|
|
213
|
+
timestamp,
|
|
214
|
+
identifier,
|
|
215
|
+
},
|
|
216
|
+
],
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
expect(results).toEqual([
|
|
220
|
+
{ identifier, reported: false, valueSent: 0 },
|
|
221
|
+
]);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("skips when delta is negative (count decreased)", async () => {
|
|
225
|
+
const customer = await createTrackedCustomer("set-neg");
|
|
226
|
+
const identifier = uniqueId();
|
|
227
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
228
|
+
|
|
229
|
+
const results = await service.reportUsageSet({
|
|
230
|
+
stripeCustomerId: customer.id,
|
|
231
|
+
organizationId: "org_integ_test",
|
|
232
|
+
events: [
|
|
233
|
+
{
|
|
234
|
+
eventName: "langwatch_billable_events",
|
|
235
|
+
value: 50,
|
|
236
|
+
previouslyReportedValue: 100,
|
|
237
|
+
timestamp,
|
|
238
|
+
identifier,
|
|
239
|
+
},
|
|
240
|
+
],
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
expect(results).toEqual([
|
|
244
|
+
{ identifier, reported: false, valueSent: 0 },
|
|
245
|
+
]);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("sends full value when previouslyReportedValue is zero", async () => {
|
|
249
|
+
const customer = await createTrackedCustomer("set-first");
|
|
250
|
+
const identifier = uniqueId();
|
|
251
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
252
|
+
|
|
253
|
+
const results = await service.reportUsageSet({
|
|
254
|
+
stripeCustomerId: customer.id,
|
|
255
|
+
organizationId: "org_integ_test",
|
|
256
|
+
events: [
|
|
257
|
+
{
|
|
258
|
+
eventName: "langwatch_billable_events",
|
|
259
|
+
value: 300,
|
|
260
|
+
previouslyReportedValue: 0,
|
|
261
|
+
timestamp,
|
|
262
|
+
identifier,
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
expect(results).toHaveLength(1);
|
|
268
|
+
expect(results[0]).toEqual({
|
|
269
|
+
identifier,
|
|
270
|
+
reported: true,
|
|
271
|
+
valueSent: 300,
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("handles batch with mixed deltas — only positive deltas hit Stripe", async () => {
|
|
276
|
+
const customer = await createTrackedCustomer("set-mixed");
|
|
277
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
278
|
+
|
|
279
|
+
const results = await service.reportUsageSet({
|
|
280
|
+
stripeCustomerId: customer.id,
|
|
281
|
+
organizationId: "org_integ_test",
|
|
282
|
+
events: [
|
|
283
|
+
{
|
|
284
|
+
eventName: "langwatch_billable_events",
|
|
285
|
+
value: 200,
|
|
286
|
+
previouslyReportedValue: 100,
|
|
287
|
+
timestamp,
|
|
288
|
+
identifier: uniqueId(),
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
eventName: "langwatch_billable_events",
|
|
292
|
+
value: 50,
|
|
293
|
+
previouslyReportedValue: 50,
|
|
294
|
+
timestamp,
|
|
295
|
+
identifier: uniqueId(),
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
eventName: "langwatch_billable_events",
|
|
299
|
+
value: 500,
|
|
300
|
+
previouslyReportedValue: 0,
|
|
301
|
+
timestamp,
|
|
302
|
+
identifier: uniqueId(),
|
|
303
|
+
},
|
|
304
|
+
],
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
expect(results).toHaveLength(3);
|
|
308
|
+
expect(results[0]!.reported).toBe(true);
|
|
309
|
+
expect(results[0]!.valueSent).toBe(100);
|
|
310
|
+
expect(results[1]!.reported).toBe(false);
|
|
311
|
+
expect(results[1]!.valueSent).toBe(0);
|
|
312
|
+
expect(results[2]!.reported).toBe(true);
|
|
313
|
+
expect(results[2]!.valueSent).toBe(500);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe("getUsageSummary()", () => {
|
|
318
|
+
/**
|
|
319
|
+
* Prerequisite: This test uses a persistent Stripe test customer that
|
|
320
|
+
* accumulates meter events across runs. Stripe's Billing Meter Event
|
|
321
|
+
* Summaries API has aggregation lag — events reported in the current
|
|
322
|
+
* run may not appear in summaries until minutes later.
|
|
323
|
+
*
|
|
324
|
+
* On the FIRST run against a fresh Stripe test account, the
|
|
325
|
+
* "returns aggregated usage" test WILL FAIL because no previously
|
|
326
|
+
* aggregated data exists yet. Run the suite a second time and it
|
|
327
|
+
* will pass once Stripe has processed the events from the first run.
|
|
328
|
+
*
|
|
329
|
+
* The persistent customer is identified by email and is intentionally
|
|
330
|
+
* NOT cleaned up in afterAll so it retains history across runs.
|
|
331
|
+
*/
|
|
332
|
+
const findOrCreatePersistentCustomer = async () => {
|
|
333
|
+
const email = "integration-test-usage-summary-persistent@langwatch.test";
|
|
334
|
+
const existing = await stripe.customers.list({ email, limit: 1 });
|
|
335
|
+
if (existing.data[0]) return existing.data[0];
|
|
336
|
+
|
|
337
|
+
const customer = await stripe.customers.create({
|
|
338
|
+
email,
|
|
339
|
+
name: "Integration Test Usage Summary (persistent)",
|
|
340
|
+
});
|
|
341
|
+
// Do NOT track for cleanup — this customer persists across runs
|
|
342
|
+
return customer;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
it("returns aggregated usage for a customer with historical events", async () => {
|
|
346
|
+
const customer = await findOrCreatePersistentCustomer();
|
|
347
|
+
const now = Math.floor(Date.now() / 1000);
|
|
348
|
+
|
|
349
|
+
// Report an event so future runs will have aggregated data
|
|
350
|
+
await service.reportUsageDelta({
|
|
351
|
+
stripeCustomerId: customer.id,
|
|
352
|
+
organizationId: "org_integ_test",
|
|
353
|
+
events: [
|
|
354
|
+
{
|
|
355
|
+
eventName: "langwatch_billable_events",
|
|
356
|
+
value: 10,
|
|
357
|
+
timestamp: now,
|
|
358
|
+
identifier: uniqueId(),
|
|
359
|
+
},
|
|
360
|
+
],
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
// Query broad range — events from previous runs should be aggregated
|
|
364
|
+
const result = await service.getUsageSummary({
|
|
365
|
+
stripeCustomerId: customer.id,
|
|
366
|
+
startTime: now - 86400 * 30,
|
|
367
|
+
endTime: now + 86400,
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
expect(result).toEqual({
|
|
371
|
+
aggregatedValue: expect.any(Number),
|
|
372
|
+
startTime: now - 86400 * 30,
|
|
373
|
+
endTime: now + 86400,
|
|
374
|
+
});
|
|
375
|
+
// Will be 0 on first-ever run due to aggregation lag, >0 on subsequent runs
|
|
376
|
+
expect(result.aggregatedValue).toBeGreaterThan(0);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("returns zero for a future time range with no events", async () => {
|
|
380
|
+
const customer = await createTrackedCustomer("summary-empty");
|
|
381
|
+
const futureStart = Math.floor(Date.now() / 1000) + 86400 * 365;
|
|
382
|
+
const futureEnd = futureStart + 3600;
|
|
383
|
+
|
|
384
|
+
const result = await service.getUsageSummary({
|
|
385
|
+
stripeCustomerId: customer.id,
|
|
386
|
+
startTime: futureStart,
|
|
387
|
+
endTime: futureEnd,
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
expect(result).toEqual({
|
|
391
|
+
aggregatedValue: 0,
|
|
392
|
+
startTime: futureStart,
|
|
393
|
+
endTime: futureEnd,
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
describe("error handling with real Stripe API", () => {
|
|
399
|
+
it("returns reported: false for invalid customer ID", async () => {
|
|
400
|
+
const identifier = uniqueId();
|
|
401
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
402
|
+
|
|
403
|
+
const results = await service.reportUsageDelta({
|
|
404
|
+
stripeCustomerId: "cus_nonexistent000000",
|
|
405
|
+
organizationId: "org_integ_test",
|
|
406
|
+
events: [
|
|
407
|
+
{
|
|
408
|
+
eventName: "langwatch_billable_events",
|
|
409
|
+
value: 10,
|
|
410
|
+
timestamp,
|
|
411
|
+
identifier,
|
|
412
|
+
},
|
|
413
|
+
],
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
expect(results).toHaveLength(1);
|
|
417
|
+
expect(results[0]!.reported).toBe(false);
|
|
418
|
+
expect(results[0]!.error).toBeDefined();
|
|
419
|
+
});
|
|
420
|
+
});
|
|
421
|
+
});
|