-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions.test.ts
More file actions
290 lines (248 loc) · 8.07 KB
/
subscriptions.test.ts
File metadata and controls
290 lines (248 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { afterAll, beforeAll, describe, expect, it } from "@jest/globals";
import { IterableClient } from "../../src/client";
import type { SubscriptionGroup } from "../../src/types/subscriptions.js";
import {
cleanupTestUser,
createTestIdentifiers,
retryWithBackoff,
uniqueId,
withTimeout,
} from "../utils/test-helpers";
describe("Subscription Management Integration Tests", () => {
let client: IterableClient;
let testListId: number;
let createdTestList = false;
const { testUserEmail, testUserId } = createTestIdentifiers();
const ensureTestUser = async () => {
await withTimeout(
client.updateUser({
email: testUserEmail,
userId: testUserId,
dataFields: { subscriptionSource: "mcp-integration-test" },
})
);
};
const expectSuccessResponse = (result: any) => {
expect(result).toHaveProperty("code");
expect(["Success", "Accepted"].includes(result.code)).toBe(true);
};
beforeAll(async () => {
client = new IterableClient();
// Try to get existing lists first
const listsResponse = await withTimeout(client.getLists());
const [existingList] = listsResponse.lists;
if (existingList) {
testListId = existingList.id;
} else {
// Create a test list if none exist
const createResponse = await withTimeout(
client.createList({
name: `MCP Integration Test List ${uniqueId()}`,
description: "Temporary list for integration testing",
})
);
testListId = createResponse.listId;
createdTestList = true;
}
});
afterAll(async () => {
await cleanupTestUser(client, testUserEmail);
// Clean up test list only if we created it
if (createdTestList) {
try {
await withTimeout(client.deleteList({ listId: testListId }));
} catch (error) {
console.warn(`Failed to delete test list ${testListId}:`, error);
}
}
client.destroy();
});
describe("Bulk Subscription Actions", () => {
it("should bulk subscribe users to an email list by email", async () => {
await ensureTestUser();
const result = await retryWithBackoff(() =>
withTimeout(
client.bulkUpdateSubscriptions({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
action: "subscribe",
users: [testUserEmail],
})
)
);
expectSuccessResponse(result);
});
it("should bulk subscribe users by userId", async () => {
await ensureTestUser();
const result = await retryWithBackoff(() =>
withTimeout(
client.bulkUpdateSubscriptions({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
action: "subscribe",
usersByUserId: [testUserId],
})
)
);
expectSuccessResponse(result);
});
it("should bulk unsubscribe users from an email list", async () => {
await ensureTestUser();
await retryWithBackoff(() =>
withTimeout(
client.bulkUpdateSubscriptions({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
action: "subscribe",
users: [testUserEmail],
})
)
);
const result = await retryWithBackoff(() =>
withTimeout(
client.bulkUpdateSubscriptions({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
action: "unsubscribe",
users: [testUserEmail],
})
)
);
expectSuccessResponse(result);
});
});
describe("Single User Subscription Actions", () => {
it("should subscribe a single user by email", async () => {
await ensureTestUser();
const result = await retryWithBackoff(() =>
withTimeout(
client.subscribeUserByEmail({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
userEmail: testUserEmail,
})
)
);
expectSuccessResponse(result);
});
it("should subscribe a single user by userId", async () => {
await ensureTestUser();
const result = await retryWithBackoff(() =>
withTimeout(
client.subscribeUserByUserId({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
userId: testUserId,
})
)
);
expectSuccessResponse(result);
});
it("should unsubscribe a single user by email", async () => {
await ensureTestUser();
await retryWithBackoff(() =>
withTimeout(
client.subscribeUserByEmail({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
userEmail: testUserEmail,
})
)
);
const result = await retryWithBackoff(() =>
withTimeout(
client.unsubscribeUserByEmail({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
userEmail: testUserEmail,
})
)
);
expectSuccessResponse(result);
});
it("should unsubscribe a single user by userId", async () => {
await ensureTestUser();
await retryWithBackoff(() =>
withTimeout(
client.subscribeUserByUserId({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
userId: testUserId,
})
)
);
const result = await retryWithBackoff(() =>
withTimeout(
client.unsubscribeUserByUserId({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
userId: testUserId,
})
)
);
expectSuccessResponse(result);
});
});
describe("Type Safety Validation", () => {
it("should accept all valid subscription group types", async () => {
await ensureTestUser();
const validGroups: SubscriptionGroup[] = [
"emailList",
"messageType",
"messageChannel",
];
for (const subscriptionGroup of validGroups) {
if (subscriptionGroup === "emailList") {
const result = await retryWithBackoff(() =>
withTimeout(
client.bulkUpdateSubscriptions({
subscriptionGroup,
subscriptionGroupId: testListId,
action: "subscribe",
users: [testUserEmail],
})
)
);
expectSuccessResponse(result);
}
}
});
});
describe("Error Handling", () => {
it("should handle invalid subscription group ID gracefully", async () => {
const invalidListId = 999999999; // Very unlikely to exist
try {
await withTimeout(
client.bulkUpdateSubscriptions({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: invalidListId,
action: "subscribe",
users: [testUserEmail],
})
);
// If no error is thrown, that's also acceptable (some APIs return success even for invalid IDs)
} catch (error: any) {
// Should be a meaningful error
expect(error).toHaveProperty("message");
expect(typeof error.message).toBe("string");
}
});
it("should handle invalid user email gracefully", async () => {
try {
await withTimeout(
client.bulkUpdateSubscriptions({
subscriptionGroup: "emailList" as SubscriptionGroup,
subscriptionGroupId: testListId,
action: "subscribe",
users: ["invalid-email-format"],
})
);
// If no error is thrown, that's also acceptable
} catch (error: any) {
// Should be a meaningful error
expect(error).toHaveProperty("message");
expect(typeof error.message).toBe("string");
}
});
});
});