@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,768 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const mockSlackSend = vi.fn().mockResolvedValue(undefined);
|
|
4
|
+
|
|
5
|
+
// Mock external dependencies
|
|
6
|
+
vi.mock("@slack/webhook", () => ({
|
|
7
|
+
IncomingWebhook: class MockIncomingWebhook {
|
|
8
|
+
constructor() {
|
|
9
|
+
// no-op
|
|
10
|
+
}
|
|
11
|
+
send = mockSlackSend;
|
|
12
|
+
},
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
vi.mock("../../../src/env.mjs", () => ({
|
|
16
|
+
env: {
|
|
17
|
+
BASE_HOST: "https://app.langwatch.ai",
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock("../../../src/server/mailer/usageLimitEmail", () => ({
|
|
22
|
+
sendUsageLimitEmail: vi.fn().mockResolvedValue(undefined),
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
vi.mock("../../../src/utils/logger/server", () => ({
|
|
26
|
+
createLogger: () => ({
|
|
27
|
+
error: vi.fn(),
|
|
28
|
+
warn: vi.fn(),
|
|
29
|
+
info: vi.fn(),
|
|
30
|
+
debug: vi.fn(),
|
|
31
|
+
}),
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
vi.mock("../../../src/utils/posthogErrorCapture", () => ({
|
|
35
|
+
captureException: vi.fn(),
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
import { sendUsageLimitEmail } from "../../../src/server/mailer/usageLimitEmail";
|
|
39
|
+
import { captureException } from "../../../src/utils/posthogErrorCapture";
|
|
40
|
+
import { NotificationService } from "../notifications/notification.service";
|
|
41
|
+
|
|
42
|
+
const mockSendUsageLimitEmail = sendUsageLimitEmail as ReturnType<
|
|
43
|
+
typeof vi.fn
|
|
44
|
+
>;
|
|
45
|
+
|
|
46
|
+
describe("NotificationService", () => {
|
|
47
|
+
let service: NotificationService;
|
|
48
|
+
let config: {
|
|
49
|
+
baseHost: string;
|
|
50
|
+
slackPlanLimitChannel?: string;
|
|
51
|
+
slackSignupsChannel?: string;
|
|
52
|
+
slackSubscriptionsChannel?: string;
|
|
53
|
+
hubspotPortalId?: string;
|
|
54
|
+
hubspotReachedLimitFormId?: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
vi.clearAllMocks();
|
|
59
|
+
config = {
|
|
60
|
+
baseHost: "https://app.langwatch.ai",
|
|
61
|
+
};
|
|
62
|
+
service = NotificationService.create({ config });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("create()", () => {
|
|
66
|
+
it("returns a NotificationService instance", () => {
|
|
67
|
+
expect(service).toBeInstanceOf(NotificationService);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("sendUsageLimitEmail()", () => {
|
|
72
|
+
const emailParams = {
|
|
73
|
+
to: "admin@acme.com",
|
|
74
|
+
orgName: "Acme",
|
|
75
|
+
usageData: {
|
|
76
|
+
organizationName: "Acme",
|
|
77
|
+
usagePercentage: 90,
|
|
78
|
+
usagePercentageFormatted: "90",
|
|
79
|
+
currentMonthMessagesCount: 9000,
|
|
80
|
+
maxMonthlyUsageLimit: 10000,
|
|
81
|
+
crossedThreshold: 90,
|
|
82
|
+
projectUsageData: [{ id: "p1", name: "Project A", messageCount: 9000 }],
|
|
83
|
+
actionUrl: "https://app.langwatch.ai/settings/usage",
|
|
84
|
+
logoUrl: "https://example.com/logo.png",
|
|
85
|
+
severity: "High",
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
describe("when email sends successfully", () => {
|
|
90
|
+
it("delegates to sendUsageLimitEmail mailer", async () => {
|
|
91
|
+
await service.sendUsageLimitEmail(emailParams);
|
|
92
|
+
|
|
93
|
+
expect(mockSendUsageLimitEmail).toHaveBeenCalledWith({
|
|
94
|
+
to: "admin@acme.com",
|
|
95
|
+
organizationName: "Acme",
|
|
96
|
+
usagePercentage: 90,
|
|
97
|
+
usagePercentageFormatted: "90",
|
|
98
|
+
currentMonthMessagesCount: 9000,
|
|
99
|
+
maxMonthlyUsageLimit: 10000,
|
|
100
|
+
crossedThreshold: 90,
|
|
101
|
+
projectUsageData: [
|
|
102
|
+
{ id: "p1", name: "Project A", messageCount: 9000 },
|
|
103
|
+
],
|
|
104
|
+
actionUrl: "https://app.langwatch.ai/settings/usage",
|
|
105
|
+
logoUrl: "https://example.com/logo.png",
|
|
106
|
+
severity: "High",
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("when email fails", () => {
|
|
112
|
+
it("rethrows the error", async () => {
|
|
113
|
+
mockSendUsageLimitEmail.mockRejectedValueOnce(
|
|
114
|
+
new Error("SMTP failure"),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
await expect(
|
|
118
|
+
service.sendUsageLimitEmail(emailParams),
|
|
119
|
+
).rejects.toThrow("SMTP failure");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe("sendSlackPlanLimitAlert()", () => {
|
|
125
|
+
const context = {
|
|
126
|
+
organizationId: "org_1",
|
|
127
|
+
organizationName: "Acme",
|
|
128
|
+
adminName: "Jane",
|
|
129
|
+
adminEmail: "jane@acme.com",
|
|
130
|
+
planName: "free",
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
describe("when SLACK_PLAN_LIMIT_CHANNEL is not set", () => {
|
|
134
|
+
it("returns without sending", async () => {
|
|
135
|
+
config.slackPlanLimitChannel = undefined;
|
|
136
|
+
|
|
137
|
+
await service.sendSlackPlanLimitAlert(context);
|
|
138
|
+
|
|
139
|
+
expect(mockSlackSend).not.toHaveBeenCalled();
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("when SLACK_PLAN_LIMIT_CHANNEL is set", () => {
|
|
144
|
+
it("sends a Slack message with plan limit info", async () => {
|
|
145
|
+
config.slackPlanLimitChannel = "https://hooks.slack.com/test";
|
|
146
|
+
|
|
147
|
+
await service.sendSlackPlanLimitAlert(context);
|
|
148
|
+
|
|
149
|
+
expect(mockSlackSend).toHaveBeenCalledWith({
|
|
150
|
+
text: expect.stringContaining("Plan limit reached: Acme"),
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe("when Slack webhook fails", () => {
|
|
156
|
+
it("catches the error and captures exception", async () => {
|
|
157
|
+
config.slackPlanLimitChannel = "https://hooks.slack.com/test";
|
|
158
|
+
|
|
159
|
+
const error = new Error("webhook error");
|
|
160
|
+
mockSlackSend.mockRejectedValueOnce(error);
|
|
161
|
+
|
|
162
|
+
await service.sendSlackPlanLimitAlert(context);
|
|
163
|
+
|
|
164
|
+
expect(captureException).toHaveBeenCalledWith(error);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("sendSlackResourceLimitAlert()", () => {
|
|
170
|
+
const context = {
|
|
171
|
+
organizationId: "org_1",
|
|
172
|
+
organizationName: "Acme",
|
|
173
|
+
adminName: "Jane",
|
|
174
|
+
adminEmail: "jane@acme.com",
|
|
175
|
+
planName: "Launch",
|
|
176
|
+
limitType: "Workflows",
|
|
177
|
+
current: 5,
|
|
178
|
+
max: 5,
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
describe("when SLACK_PLAN_LIMIT_CHANNEL is set", () => {
|
|
182
|
+
it("sends with correct text including resource type and counts", async () => {
|
|
183
|
+
config.slackPlanLimitChannel = "https://hooks.slack.com/test";
|
|
184
|
+
|
|
185
|
+
await service.sendSlackResourceLimitAlert(context);
|
|
186
|
+
|
|
187
|
+
expect(mockSlackSend).toHaveBeenCalledWith({
|
|
188
|
+
text: "Resource limit reached: Acme, jane@acme.com, Plan: Launch, Workflows: 5/5",
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("when SLACK_PLAN_LIMIT_CHANNEL is not set", () => {
|
|
194
|
+
it("returns without sending", async () => {
|
|
195
|
+
config.slackPlanLimitChannel = undefined;
|
|
196
|
+
|
|
197
|
+
await service.sendSlackResourceLimitAlert(context);
|
|
198
|
+
|
|
199
|
+
expect(mockSlackSend).not.toHaveBeenCalled();
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("when Slack webhook fails", () => {
|
|
204
|
+
it("catches the error and captures exception", async () => {
|
|
205
|
+
config.slackPlanLimitChannel = "https://hooks.slack.com/test";
|
|
206
|
+
|
|
207
|
+
const error = new Error("webhook error");
|
|
208
|
+
mockSlackSend.mockRejectedValueOnce(error);
|
|
209
|
+
|
|
210
|
+
await service.sendSlackResourceLimitAlert(context);
|
|
211
|
+
|
|
212
|
+
expect(captureException).toHaveBeenCalledWith(error);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("sendSlackSubscriptionEvent()", () => {
|
|
218
|
+
describe("when SLACK_CHANNEL_SUBSCRIPTIONS is not set", () => {
|
|
219
|
+
it("returns without sending", async () => {
|
|
220
|
+
config.slackSubscriptionsChannel = undefined;
|
|
221
|
+
|
|
222
|
+
await service.sendSlackSubscriptionEvent({
|
|
223
|
+
type: "confirmed",
|
|
224
|
+
organizationId: "org_1",
|
|
225
|
+
organizationName: "Acme",
|
|
226
|
+
plan: "LAUNCH",
|
|
227
|
+
subscriptionId: "sub_1",
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
expect(mockSlackSend).not.toHaveBeenCalled();
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("when sending a confirmed subscription event", () => {
|
|
235
|
+
it("sends Slack blocks for confirmed subscription", async () => {
|
|
236
|
+
config.slackSubscriptionsChannel = "https://hooks.slack.com/subs";
|
|
237
|
+
|
|
238
|
+
await service.sendSlackSubscriptionEvent({
|
|
239
|
+
type: "confirmed",
|
|
240
|
+
organizationId: "org_1",
|
|
241
|
+
organizationName: "Acme",
|
|
242
|
+
plan: "LAUNCH",
|
|
243
|
+
subscriptionId: "sub_1",
|
|
244
|
+
startDate: new Date("2025-01-01"),
|
|
245
|
+
maxMembers: 5,
|
|
246
|
+
maxMessagesPerMonth: 10000,
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
expect(mockSlackSend).toHaveBeenCalledWith({
|
|
250
|
+
blocks: expect.arrayContaining([
|
|
251
|
+
expect.objectContaining({
|
|
252
|
+
type: "header",
|
|
253
|
+
text: expect.objectContaining({
|
|
254
|
+
text: "Subscription activated",
|
|
255
|
+
}),
|
|
256
|
+
}),
|
|
257
|
+
]),
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe("when sending a prospective subscription event", () => {
|
|
263
|
+
it("sends Slack blocks for prospective interest", async () => {
|
|
264
|
+
config.slackSubscriptionsChannel = "https://hooks.slack.com/subs";
|
|
265
|
+
|
|
266
|
+
await service.sendSlackSubscriptionEvent({
|
|
267
|
+
type: "prospective",
|
|
268
|
+
organizationId: "org_1",
|
|
269
|
+
organizationName: "Acme",
|
|
270
|
+
plan: "ACCELERATE",
|
|
271
|
+
customerName: "John",
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
expect(mockSlackSend).toHaveBeenCalledWith({
|
|
275
|
+
blocks: expect.arrayContaining([
|
|
276
|
+
expect.objectContaining({
|
|
277
|
+
type: "header",
|
|
278
|
+
text: expect.objectContaining({
|
|
279
|
+
text: "Prospective subscription interest",
|
|
280
|
+
}),
|
|
281
|
+
}),
|
|
282
|
+
]),
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe("when sending a cancelled subscription event", () => {
|
|
288
|
+
it("sends Slack blocks with 'Subscription cancelled' header", async () => {
|
|
289
|
+
config.slackSubscriptionsChannel = "https://hooks.slack.com/subs";
|
|
290
|
+
|
|
291
|
+
await service.sendSlackSubscriptionEvent({
|
|
292
|
+
type: "cancelled",
|
|
293
|
+
organizationId: "org_1",
|
|
294
|
+
organizationName: "Acme",
|
|
295
|
+
plan: "GROWTH_SEAT_EUR_MONTHLY",
|
|
296
|
+
subscriptionId: "sub_1",
|
|
297
|
+
cancellationDate: new Date("2026-03-15T10:00:00Z"),
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
expect(mockSlackSend).toHaveBeenCalledWith({
|
|
301
|
+
blocks: expect.arrayContaining([
|
|
302
|
+
expect.objectContaining({
|
|
303
|
+
type: "header",
|
|
304
|
+
text: expect.objectContaining({
|
|
305
|
+
text: "Subscription cancelled",
|
|
306
|
+
}),
|
|
307
|
+
}),
|
|
308
|
+
]),
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("includes organization name and plan in the body", async () => {
|
|
313
|
+
config.slackSubscriptionsChannel = "https://hooks.slack.com/subs";
|
|
314
|
+
|
|
315
|
+
await service.sendSlackSubscriptionEvent({
|
|
316
|
+
type: "cancelled",
|
|
317
|
+
organizationId: "org_1",
|
|
318
|
+
organizationName: "Acme",
|
|
319
|
+
plan: "GROWTH_SEAT_EUR_MONTHLY",
|
|
320
|
+
subscriptionId: "sub_1",
|
|
321
|
+
cancellationDate: new Date("2026-03-15T10:00:00Z"),
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
const sentBlocks = mockSlackSend.mock.calls[0]![0].blocks;
|
|
325
|
+
const bodyBlock = sentBlocks.find(
|
|
326
|
+
(b: any) => b.type === "section" && b.text?.type === "mrkdwn",
|
|
327
|
+
);
|
|
328
|
+
expect(bodyBlock.text.text).toContain("Acme");
|
|
329
|
+
expect(bodyBlock.text.text).toContain("cancelled");
|
|
330
|
+
expect(bodyBlock.text.text).toContain("GROWTH_SEAT_EUR_MONTHLY");
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("includes an admin link button", async () => {
|
|
334
|
+
config.slackSubscriptionsChannel = "https://hooks.slack.com/subs";
|
|
335
|
+
|
|
336
|
+
await service.sendSlackSubscriptionEvent({
|
|
337
|
+
type: "cancelled",
|
|
338
|
+
organizationId: "org_1",
|
|
339
|
+
organizationName: "Acme",
|
|
340
|
+
plan: "GROWTH_SEAT_EUR_MONTHLY",
|
|
341
|
+
subscriptionId: "sub_1",
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
const sentBlocks = mockSlackSend.mock.calls[0]![0].blocks;
|
|
345
|
+
const actionsBlock = sentBlocks.find((b: any) => b.type === "actions");
|
|
346
|
+
expect(actionsBlock).toBeDefined();
|
|
347
|
+
expect(actionsBlock.elements[0].url).toContain("org_1");
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
describe("sendSlackSignupEvent()", () => {
|
|
353
|
+
const payload = {
|
|
354
|
+
userName: "Jane Doe",
|
|
355
|
+
userEmail: "jane@example.com",
|
|
356
|
+
organizationName: "Acme Corp",
|
|
357
|
+
phoneNumber: "+31 20 123 4567",
|
|
358
|
+
utmCampaign: "launch-week",
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
describe("when SLACK_CHANNEL_SIGNUPS is not set", () => {
|
|
362
|
+
it("returns without sending", async () => {
|
|
363
|
+
config.slackSignupsChannel = undefined;
|
|
364
|
+
|
|
365
|
+
await service.sendSlackSignupEvent(payload);
|
|
366
|
+
|
|
367
|
+
expect(mockSlackSend).not.toHaveBeenCalled();
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
describe("when optional fields are present", () => {
|
|
372
|
+
it("includes phone number and campaign in the Slack text", async () => {
|
|
373
|
+
config.slackSignupsChannel = "https://hooks.slack.com/signups";
|
|
374
|
+
|
|
375
|
+
await service.sendSlackSignupEvent(payload);
|
|
376
|
+
|
|
377
|
+
expect(mockSlackSend).toHaveBeenCalledWith({
|
|
378
|
+
text: "🔔 New user registered: Jane Doe, jane@example.com. Organization: Acme Corp, +31 20 123 4567, Campaign: launch-week",
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
describe("when optional fields are missing", () => {
|
|
384
|
+
it("sends the baseline signup notification text", async () => {
|
|
385
|
+
config.slackSignupsChannel = "https://hooks.slack.com/signups";
|
|
386
|
+
|
|
387
|
+
await service.sendSlackSignupEvent({
|
|
388
|
+
userName: "Jane Doe",
|
|
389
|
+
userEmail: "jane@example.com",
|
|
390
|
+
organizationName: "Acme Corp",
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
expect(mockSlackSend).toHaveBeenCalledWith({
|
|
394
|
+
text: "🔔 New user registered: Jane Doe, jane@example.com. Organization: Acme Corp",
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
describe("when Slack webhook fails", () => {
|
|
400
|
+
it("captures the exception and does not throw", async () => {
|
|
401
|
+
config.slackSignupsChannel = "https://hooks.slack.com/signups";
|
|
402
|
+
|
|
403
|
+
const error = new Error("signup webhook error");
|
|
404
|
+
mockSlackSend.mockRejectedValueOnce(error);
|
|
405
|
+
|
|
406
|
+
await service.sendSlackSignupEvent(payload);
|
|
407
|
+
|
|
408
|
+
expect(captureException).toHaveBeenCalledWith(error);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
describe("sendSlackLicensePurchase()", () => {
|
|
414
|
+
const payload = {
|
|
415
|
+
buyerEmail: "buyer@acme.com",
|
|
416
|
+
planType: "GROWTH",
|
|
417
|
+
seats: 5,
|
|
418
|
+
amountPaid: 14900,
|
|
419
|
+
currency: "usd",
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
describe("when SLACK_CHANNEL_SUBSCRIPTIONS is not set", () => {
|
|
423
|
+
it("returns without sending", async () => {
|
|
424
|
+
config.slackSubscriptionsChannel = undefined;
|
|
425
|
+
|
|
426
|
+
await service.sendSlackLicensePurchase(payload);
|
|
427
|
+
|
|
428
|
+
expect(mockSlackSend).not.toHaveBeenCalled();
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
describe("when SLACK_CHANNEL_SUBSCRIPTIONS is set", () => {
|
|
433
|
+
it("sends a license purchase notification via IncomingWebhook", async () => {
|
|
434
|
+
config.slackSubscriptionsChannel = "https://hooks.slack.com/subs";
|
|
435
|
+
|
|
436
|
+
await service.sendSlackLicensePurchase(payload);
|
|
437
|
+
|
|
438
|
+
expect(mockSlackSend).toHaveBeenCalledWith(
|
|
439
|
+
expect.objectContaining({
|
|
440
|
+
text: "New License Purchase",
|
|
441
|
+
blocks: expect.arrayContaining([
|
|
442
|
+
expect.objectContaining({
|
|
443
|
+
type: "header",
|
|
444
|
+
text: expect.objectContaining({
|
|
445
|
+
text: "New License Purchase",
|
|
446
|
+
}),
|
|
447
|
+
}),
|
|
448
|
+
]),
|
|
449
|
+
}),
|
|
450
|
+
);
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
describe("sendHubspotPlanLimitForm()", () => {
|
|
456
|
+
const context = {
|
|
457
|
+
organizationId: "org_1",
|
|
458
|
+
organizationName: "Acme",
|
|
459
|
+
adminName: "Jane",
|
|
460
|
+
adminEmail: "jane@acme.com",
|
|
461
|
+
planName: "free",
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
describe("when HubSpot env vars are not set", () => {
|
|
465
|
+
it("returns without sending", async () => {
|
|
466
|
+
const mockFetch = vi.fn();
|
|
467
|
+
const localService = NotificationService.create({
|
|
468
|
+
config: { ...config, hubspotPortalId: undefined, hubspotReachedLimitFormId: undefined },
|
|
469
|
+
fetchFn: mockFetch,
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
await localService.sendHubspotPlanLimitForm(context);
|
|
473
|
+
|
|
474
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
describe("when HubSpot env vars are set", () => {
|
|
479
|
+
it("submits form data to HubSpot", async () => {
|
|
480
|
+
const mockFetch = vi
|
|
481
|
+
.fn()
|
|
482
|
+
.mockResolvedValue(new Response("ok", { status: 200 }));
|
|
483
|
+
const localService = NotificationService.create({
|
|
484
|
+
config: {
|
|
485
|
+
...config,
|
|
486
|
+
hubspotPortalId: "12345",
|
|
487
|
+
hubspotReachedLimitFormId: "form_abc",
|
|
488
|
+
},
|
|
489
|
+
fetchFn: mockFetch,
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
await localService.sendHubspotPlanLimitForm(context);
|
|
493
|
+
|
|
494
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
495
|
+
"https://api.hsforms.com/submissions/v3/integration/submit/12345/form_abc",
|
|
496
|
+
expect.objectContaining({
|
|
497
|
+
method: "POST",
|
|
498
|
+
body: expect.stringContaining("jane@acme.com"),
|
|
499
|
+
}),
|
|
500
|
+
);
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
describe("when HubSpot request fails", () => {
|
|
505
|
+
it("catches the error and captures exception", async () => {
|
|
506
|
+
const mockFetch = vi
|
|
507
|
+
.fn()
|
|
508
|
+
.mockResolvedValue(new Response("fail", { status: 500 }));
|
|
509
|
+
const localService = NotificationService.create({
|
|
510
|
+
config: {
|
|
511
|
+
...config,
|
|
512
|
+
hubspotPortalId: "12345",
|
|
513
|
+
hubspotReachedLimitFormId: "form_abc",
|
|
514
|
+
},
|
|
515
|
+
fetchFn: mockFetch,
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
await localService.sendHubspotPlanLimitForm(context);
|
|
519
|
+
|
|
520
|
+
expect(captureException).toHaveBeenCalled();
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
describe("sendHubspotSignupForm()", () => {
|
|
526
|
+
const payload = {
|
|
527
|
+
userName: "Jane Doe",
|
|
528
|
+
userEmail: "jane@example.com",
|
|
529
|
+
organizationName: "Acme Corp",
|
|
530
|
+
phoneNumber: "+31 20 123 4567",
|
|
531
|
+
signUpData: {
|
|
532
|
+
featureUsage: "Evaluations",
|
|
533
|
+
yourRole: "Engineer",
|
|
534
|
+
usage: "Production",
|
|
535
|
+
solution: "LLM monitoring",
|
|
536
|
+
companySize: "10",
|
|
537
|
+
utmCampaign: "launch-week",
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
describe("when hubspotFormId is not configured", () => {
|
|
542
|
+
it("returns without sending", async () => {
|
|
543
|
+
const mockFetch = vi.fn();
|
|
544
|
+
const localService = NotificationService.create({
|
|
545
|
+
config: {
|
|
546
|
+
...config,
|
|
547
|
+
hubspotPortalId: "12345",
|
|
548
|
+
hubspotFormId: undefined,
|
|
549
|
+
},
|
|
550
|
+
fetchFn: mockFetch,
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
await localService.sendHubspotSignupForm(payload);
|
|
554
|
+
|
|
555
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
556
|
+
});
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
describe("when hubspotPortalId is not configured", () => {
|
|
560
|
+
it("returns without sending", async () => {
|
|
561
|
+
const mockFetch = vi.fn();
|
|
562
|
+
const localService = NotificationService.create({
|
|
563
|
+
config: {
|
|
564
|
+
...config,
|
|
565
|
+
hubspotPortalId: undefined,
|
|
566
|
+
hubspotFormId: "form_signup",
|
|
567
|
+
},
|
|
568
|
+
fetchFn: mockFetch,
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
await localService.sendHubspotSignupForm(payload);
|
|
572
|
+
|
|
573
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
describe("when both hubspotPortalId and hubspotFormId are configured", () => {
|
|
578
|
+
it("submits form data to the correct HubSpot URL with correct fields", async () => {
|
|
579
|
+
const mockFetch = vi
|
|
580
|
+
.fn()
|
|
581
|
+
.mockResolvedValue(new Response("ok", { status: 200 }));
|
|
582
|
+
const localService = NotificationService.create({
|
|
583
|
+
config: {
|
|
584
|
+
...config,
|
|
585
|
+
hubspotPortalId: "12345",
|
|
586
|
+
hubspotFormId: "form_signup",
|
|
587
|
+
},
|
|
588
|
+
fetchFn: mockFetch,
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
await localService.sendHubspotSignupForm(payload);
|
|
592
|
+
|
|
593
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
594
|
+
"https://api.hsforms.com/submissions/v3/integration/submit/12345/form_signup",
|
|
595
|
+
expect.objectContaining({
|
|
596
|
+
method: "POST",
|
|
597
|
+
headers: { "Content-Type": "application/json" },
|
|
598
|
+
signal: expect.any(AbortSignal),
|
|
599
|
+
}),
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
const body = JSON.parse(mockFetch.mock.calls[0]![1]!.body as string);
|
|
603
|
+
expect(body.fields).toEqual(
|
|
604
|
+
expect.arrayContaining([
|
|
605
|
+
{ objectTypeId: "0-1", name: "company", value: "Acme Corp" },
|
|
606
|
+
{ objectTypeId: "0-1", name: "firstname", value: "Jane" },
|
|
607
|
+
{ objectTypeId: "0-1", name: "lastname", value: "Doe" },
|
|
608
|
+
{ objectTypeId: "0-1", name: "email", value: "jane@example.com" },
|
|
609
|
+
{
|
|
610
|
+
objectTypeId: "0-1",
|
|
611
|
+
name: "mobilephone",
|
|
612
|
+
value: "+31 20 123 4567",
|
|
613
|
+
},
|
|
614
|
+
{
|
|
615
|
+
objectTypeId: "0-1",
|
|
616
|
+
name: "Features_usage_multiple",
|
|
617
|
+
value: "Evaluations",
|
|
618
|
+
},
|
|
619
|
+
{ objectTypeId: "0-1", name: "user_role", value: "Engineer" },
|
|
620
|
+
{ objectTypeId: "0-1", name: "product_usage", value: "Production" },
|
|
621
|
+
{
|
|
622
|
+
objectTypeId: "0-1",
|
|
623
|
+
name: "product_solution",
|
|
624
|
+
value: "LLM monitoring",
|
|
625
|
+
},
|
|
626
|
+
{ objectTypeId: "0-1", name: "organization_size", value: "10" },
|
|
627
|
+
{
|
|
628
|
+
objectTypeId: "0-1",
|
|
629
|
+
name: "utm_campaign",
|
|
630
|
+
value: "launch-week",
|
|
631
|
+
},
|
|
632
|
+
]),
|
|
633
|
+
);
|
|
634
|
+
expect(body.context).toEqual({
|
|
635
|
+
pageUri: "app.langwatch.ai",
|
|
636
|
+
pageName: "Sign Up",
|
|
637
|
+
});
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
it("splits a multi-word userName into firstname and lastname", async () => {
|
|
641
|
+
const mockFetch = vi
|
|
642
|
+
.fn()
|
|
643
|
+
.mockResolvedValue(new Response("ok", { status: 200 }));
|
|
644
|
+
const localService = NotificationService.create({
|
|
645
|
+
config: {
|
|
646
|
+
...config,
|
|
647
|
+
hubspotPortalId: "12345",
|
|
648
|
+
hubspotFormId: "form_signup",
|
|
649
|
+
},
|
|
650
|
+
fetchFn: mockFetch,
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
await localService.sendHubspotSignupForm({
|
|
654
|
+
...payload,
|
|
655
|
+
userName: "Mary Jane Watson",
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
const body = JSON.parse(mockFetch.mock.calls[0]![1]!.body as string);
|
|
659
|
+
const firstNameField = body.fields.find(
|
|
660
|
+
(f: any) => f.name === "firstname",
|
|
661
|
+
);
|
|
662
|
+
const lastNameField = body.fields.find(
|
|
663
|
+
(f: any) => f.name === "lastname",
|
|
664
|
+
);
|
|
665
|
+
expect(firstNameField.value).toBe("Mary");
|
|
666
|
+
expect(lastNameField.value).toBe("Watson");
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it("applies per-field defaults when signUpData is missing", async () => {
|
|
670
|
+
const mockFetch = vi
|
|
671
|
+
.fn()
|
|
672
|
+
.mockResolvedValue(new Response("ok", { status: 200 }));
|
|
673
|
+
const localService = NotificationService.create({
|
|
674
|
+
config: {
|
|
675
|
+
...config,
|
|
676
|
+
hubspotPortalId: "12345",
|
|
677
|
+
hubspotFormId: "form_signup",
|
|
678
|
+
},
|
|
679
|
+
fetchFn: mockFetch,
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
await localService.sendHubspotSignupForm({
|
|
683
|
+
userName: "Jane",
|
|
684
|
+
userEmail: "jane@example.com",
|
|
685
|
+
organizationName: "Acme",
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
const body = JSON.parse(mockFetch.mock.calls[0]![1]!.body as string);
|
|
689
|
+
const field = (name: string) =>
|
|
690
|
+
body.fields.find((f: any) => f.name === name)?.value;
|
|
691
|
+
|
|
692
|
+
expect(field("Features_usage_multiple")).toBe("Other");
|
|
693
|
+
expect(field("user_role")).toBe("Other");
|
|
694
|
+
expect(field("product_usage")).toBe("");
|
|
695
|
+
expect(field("product_solution")).toBe("");
|
|
696
|
+
expect(field("organization_size")).toBe("1");
|
|
697
|
+
expect(field("utm_campaign")).toBe("");
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
it("falls back to top-level utmCampaign when signUpData has none", async () => {
|
|
701
|
+
const mockFetch = vi
|
|
702
|
+
.fn()
|
|
703
|
+
.mockResolvedValue(new Response("ok", { status: 200 }));
|
|
704
|
+
const localService = NotificationService.create({
|
|
705
|
+
config: {
|
|
706
|
+
...config,
|
|
707
|
+
hubspotPortalId: "12345",
|
|
708
|
+
hubspotFormId: "form_signup",
|
|
709
|
+
},
|
|
710
|
+
fetchFn: mockFetch,
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
await localService.sendHubspotSignupForm({
|
|
714
|
+
userName: "Jane",
|
|
715
|
+
userEmail: "jane@example.com",
|
|
716
|
+
organizationName: "Acme",
|
|
717
|
+
utmCampaign: "top-level-campaign",
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
const body = JSON.parse(mockFetch.mock.calls[0]![1]!.body as string);
|
|
721
|
+
const campaignField = body.fields.find(
|
|
722
|
+
(f: any) => f.name === "utm_campaign",
|
|
723
|
+
);
|
|
724
|
+
expect(campaignField.value).toBe("top-level-campaign");
|
|
725
|
+
});
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
describe("when HubSpot request fails", () => {
|
|
729
|
+
it("catches the error and captures exception", async () => {
|
|
730
|
+
const mockFetch = vi
|
|
731
|
+
.fn()
|
|
732
|
+
.mockRejectedValue(new Error("Network error"));
|
|
733
|
+
const localService = NotificationService.create({
|
|
734
|
+
config: {
|
|
735
|
+
...config,
|
|
736
|
+
hubspotPortalId: "12345",
|
|
737
|
+
hubspotFormId: "form_signup",
|
|
738
|
+
},
|
|
739
|
+
fetchFn: mockFetch,
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
await localService.sendHubspotSignupForm(payload);
|
|
743
|
+
|
|
744
|
+
expect(captureException).toHaveBeenCalled();
|
|
745
|
+
});
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
describe("when HubSpot returns a non-OK status", () => {
|
|
749
|
+
it("captures a descriptive error", async () => {
|
|
750
|
+
const mockFetch = vi
|
|
751
|
+
.fn()
|
|
752
|
+
.mockResolvedValue(new Response("fail", { status: 500 }));
|
|
753
|
+
const localService = NotificationService.create({
|
|
754
|
+
config: {
|
|
755
|
+
...config,
|
|
756
|
+
hubspotPortalId: "12345",
|
|
757
|
+
hubspotFormId: "form_signup",
|
|
758
|
+
},
|
|
759
|
+
fetchFn: mockFetch,
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
await localService.sendHubspotSignupForm(payload);
|
|
763
|
+
|
|
764
|
+
expect(captureException).toHaveBeenCalled();
|
|
765
|
+
});
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
});
|