@kodelyth/msteams 2026.5.42 → 2026.6.1

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 (177) hide show
  1. package/klaw.plugin.json +726 -2
  2. package/package.json +16 -4
  3. package/api.ts +0 -3
  4. package/channel-config-api.ts +0 -1
  5. package/channel-plugin-api.ts +0 -2
  6. package/config-api.ts +0 -4
  7. package/contract-api.ts +0 -4
  8. package/index.ts +0 -20
  9. package/runtime-api.ts +0 -66
  10. package/secret-contract-api.ts +0 -5
  11. package/setup-entry.ts +0 -13
  12. package/setup-plugin-api.ts +0 -3
  13. package/src/ai-entity.ts +0 -7
  14. package/src/approval-auth.ts +0 -44
  15. package/src/attachments/bot-framework.test.ts +0 -506
  16. package/src/attachments/bot-framework.ts +0 -348
  17. package/src/attachments/download.ts +0 -328
  18. package/src/attachments/graph.test.ts +0 -441
  19. package/src/attachments/graph.ts +0 -489
  20. package/src/attachments/html.ts +0 -122
  21. package/src/attachments/payload.ts +0 -14
  22. package/src/attachments/remote-media.test.ts +0 -187
  23. package/src/attachments/remote-media.ts +0 -86
  24. package/src/attachments/shared.test.ts +0 -547
  25. package/src/attachments/shared.ts +0 -655
  26. package/src/attachments/types.ts +0 -47
  27. package/src/attachments.graph.test.ts +0 -414
  28. package/src/attachments.helpers.test.ts +0 -245
  29. package/src/attachments.test-helpers.ts +0 -17
  30. package/src/attachments.test.ts +0 -754
  31. package/src/attachments.ts +0 -18
  32. package/src/block-streaming-config.test.ts +0 -61
  33. package/src/channel-api.ts +0 -1
  34. package/src/channel.actions.test.ts +0 -797
  35. package/src/channel.directory.test.ts +0 -176
  36. package/src/channel.message-adapter.test.ts +0 -227
  37. package/src/channel.runtime.ts +0 -56
  38. package/src/channel.setup.ts +0 -77
  39. package/src/channel.test.ts +0 -136
  40. package/src/channel.ts +0 -1176
  41. package/src/config-schema.ts +0 -6
  42. package/src/config-ui-hints.ts +0 -40
  43. package/src/conversation-store-fs.test.ts +0 -81
  44. package/src/conversation-store-fs.ts +0 -149
  45. package/src/conversation-store-helpers.test.ts +0 -202
  46. package/src/conversation-store-helpers.ts +0 -105
  47. package/src/conversation-store-memory.ts +0 -51
  48. package/src/conversation-store.shared.test.ts +0 -260
  49. package/src/conversation-store.ts +0 -71
  50. package/src/directory-live.test.ts +0 -156
  51. package/src/directory-live.ts +0 -111
  52. package/src/doctor.ts +0 -27
  53. package/src/errors.test.ts +0 -154
  54. package/src/errors.ts +0 -270
  55. package/src/feedback-reflection-prompt.ts +0 -117
  56. package/src/feedback-reflection-store.ts +0 -113
  57. package/src/feedback-reflection.test.ts +0 -237
  58. package/src/feedback-reflection.ts +0 -268
  59. package/src/file-consent-helpers.test.ts +0 -328
  60. package/src/file-consent-helpers.ts +0 -115
  61. package/src/file-consent-invoke.ts +0 -150
  62. package/src/file-consent.test.ts +0 -378
  63. package/src/file-consent.ts +0 -223
  64. package/src/graph-chat.ts +0 -36
  65. package/src/graph-group-management.test.ts +0 -332
  66. package/src/graph-group-management.ts +0 -168
  67. package/src/graph-members.test.ts +0 -89
  68. package/src/graph-members.ts +0 -48
  69. package/src/graph-messages.actions.test.ts +0 -253
  70. package/src/graph-messages.read.test.ts +0 -391
  71. package/src/graph-messages.search.test.ts +0 -227
  72. package/src/graph-messages.test-helpers.ts +0 -50
  73. package/src/graph-messages.ts +0 -534
  74. package/src/graph-teams.test.ts +0 -222
  75. package/src/graph-teams.ts +0 -114
  76. package/src/graph-thread.test.ts +0 -252
  77. package/src/graph-thread.ts +0 -146
  78. package/src/graph-upload.test.ts +0 -253
  79. package/src/graph-upload.ts +0 -531
  80. package/src/graph-users.ts +0 -29
  81. package/src/graph.test.ts +0 -540
  82. package/src/graph.ts +0 -308
  83. package/src/inbound.test.ts +0 -221
  84. package/src/inbound.ts +0 -148
  85. package/src/index.ts +0 -4
  86. package/src/media-helpers.test.ts +0 -220
  87. package/src/media-helpers.ts +0 -105
  88. package/src/mentions.test.ts +0 -254
  89. package/src/mentions.ts +0 -114
  90. package/src/messenger.test.ts +0 -961
  91. package/src/messenger.ts +0 -608
  92. package/src/monitor-handler/access.ts +0 -136
  93. package/src/monitor-handler/inbound-media.test.ts +0 -314
  94. package/src/monitor-handler/inbound-media.ts +0 -180
  95. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  96. package/src/monitor-handler/message-handler.authz.test.ts +0 -739
  97. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  98. package/src/monitor-handler/message-handler.test-support.ts +0 -99
  99. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
  100. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
  101. package/src/monitor-handler/message-handler.ts +0 -1003
  102. package/src/monitor-handler/reaction-handler.test.ts +0 -325
  103. package/src/monitor-handler/reaction-handler.ts +0 -122
  104. package/src/monitor-handler/thread-session.ts +0 -30
  105. package/src/monitor-handler.adaptive-card.test.ts +0 -158
  106. package/src/monitor-handler.feedback-authz.test.ts +0 -357
  107. package/src/monitor-handler.file-consent.test.ts +0 -443
  108. package/src/monitor-handler.sso.test.ts +0 -576
  109. package/src/monitor-handler.test-helpers.ts +0 -181
  110. package/src/monitor-handler.ts +0 -538
  111. package/src/monitor-handler.types.ts +0 -27
  112. package/src/monitor-types.ts +0 -6
  113. package/src/monitor.lifecycle.test.ts +0 -457
  114. package/src/monitor.test.ts +0 -119
  115. package/src/monitor.ts +0 -476
  116. package/src/oauth.flow.ts +0 -77
  117. package/src/oauth.shared.ts +0 -37
  118. package/src/oauth.test.ts +0 -350
  119. package/src/oauth.token.ts +0 -162
  120. package/src/oauth.ts +0 -130
  121. package/src/outbound.test.ts +0 -400
  122. package/src/outbound.ts +0 -198
  123. package/src/pending-uploads-fs.test.ts +0 -261
  124. package/src/pending-uploads-fs.ts +0 -235
  125. package/src/pending-uploads.test.ts +0 -186
  126. package/src/pending-uploads.ts +0 -121
  127. package/src/policy.test.ts +0 -156
  128. package/src/policy.ts +0 -245
  129. package/src/polls-store-memory.ts +0 -32
  130. package/src/polls.test.ts +0 -169
  131. package/src/polls.ts +0 -312
  132. package/src/presentation.ts +0 -93
  133. package/src/probe.test.ts +0 -79
  134. package/src/probe.ts +0 -132
  135. package/src/reply-dispatcher.test.ts +0 -543
  136. package/src/reply-dispatcher.ts +0 -523
  137. package/src/reply-stream-controller.test.ts +0 -424
  138. package/src/reply-stream-controller.ts +0 -334
  139. package/src/resolve-allowlist.test.ts +0 -253
  140. package/src/resolve-allowlist.ts +0 -309
  141. package/src/revoked-context.ts +0 -17
  142. package/src/runtime.ts +0 -12
  143. package/src/sdk-types.ts +0 -59
  144. package/src/sdk.test.ts +0 -727
  145. package/src/sdk.ts +0 -916
  146. package/src/secret-contract.ts +0 -49
  147. package/src/secret-input.ts +0 -7
  148. package/src/send-context.test.ts +0 -93
  149. package/src/send-context.ts +0 -269
  150. package/src/send.test.ts +0 -588
  151. package/src/send.ts +0 -697
  152. package/src/sent-message-cache.test.ts +0 -106
  153. package/src/sent-message-cache.ts +0 -174
  154. package/src/session-route.ts +0 -40
  155. package/src/setup-core.ts +0 -162
  156. package/src/setup-surface.test.ts +0 -175
  157. package/src/setup-surface.ts +0 -319
  158. package/src/sso-token-store.test.ts +0 -74
  159. package/src/sso-token-store.ts +0 -166
  160. package/src/sso.ts +0 -300
  161. package/src/storage.ts +0 -25
  162. package/src/store-fs.ts +0 -42
  163. package/src/streaming-message.test.ts +0 -323
  164. package/src/streaming-message.ts +0 -327
  165. package/src/test-runtime.ts +0 -16
  166. package/src/thread-parent-context.test.ts +0 -224
  167. package/src/thread-parent-context.ts +0 -159
  168. package/src/token-response.ts +0 -11
  169. package/src/token.test.ts +0 -268
  170. package/src/token.ts +0 -194
  171. package/src/user-agent.test.ts +0 -121
  172. package/src/user-agent.ts +0 -53
  173. package/src/webhook-timeouts.ts +0 -27
  174. package/src/welcome-card.test.ts +0 -104
  175. package/src/welcome-card.ts +0 -57
  176. package/test-api.ts +0 -1
  177. package/tsconfig.json +0 -16
package/src/graph.test.ts DELETED
@@ -1,540 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- const {
4
- loadMSTeamsSdkWithAuthMock,
5
- createMSTeamsTokenProviderMock,
6
- readAccessTokenMock,
7
- resolveMSTeamsCredentialsMock,
8
- } = vi.hoisted(() => {
9
- return {
10
- loadMSTeamsSdkWithAuthMock: vi.fn(),
11
- createMSTeamsTokenProviderMock: vi.fn(),
12
- readAccessTokenMock: vi.fn(),
13
- resolveMSTeamsCredentialsMock: vi.fn(),
14
- };
15
- });
16
-
17
- vi.mock("./sdk.js", () => ({
18
- loadMSTeamsSdkWithAuth: loadMSTeamsSdkWithAuthMock,
19
- createMSTeamsTokenProvider: createMSTeamsTokenProviderMock,
20
- }));
21
-
22
- vi.mock("./token-response.js", () => ({
23
- readAccessToken: readAccessTokenMock,
24
- }));
25
-
26
- vi.mock("./token.js", () => ({
27
- resolveMSTeamsCredentials: resolveMSTeamsCredentialsMock,
28
- }));
29
-
30
- vi.mock("../runtime-api.js", async (importOriginal) => {
31
- const original = await importOriginal<typeof import("../runtime-api.js")>();
32
- return {
33
- ...original,
34
- fetchWithSsrFGuard: async (params: { url: string; init?: RequestInit }) => ({
35
- response: await globalThis.fetch(params.url, params.init),
36
- finalUrl: params.url,
37
- release: async () => undefined,
38
- }),
39
- };
40
- });
41
-
42
- import { searchGraphUsers } from "./graph-users.js";
43
- import {
44
- deleteGraphRequest,
45
- escapeOData,
46
- fetchAllGraphPages,
47
- fetchGraphJson,
48
- listChannelsForTeam,
49
- listTeamsByName,
50
- normalizeQuery,
51
- postGraphBetaJson,
52
- postGraphJson,
53
- resolveGraphToken,
54
- } from "./graph.js";
55
-
56
- const originalFetch = globalThis.fetch;
57
- const graphToken = "graph-token";
58
- const mockCredentials = {
59
- appId: "app-id",
60
- appPassword: "app-password",
61
- tenantId: "tenant-id",
62
- };
63
- const mockApp = { id: "mock-app" };
64
- const groupOne = { id: "group-1" };
65
- const opsTeam = { id: "team-1", displayName: "Ops" };
66
- const deploymentsChannel = { id: "chan-1", displayName: "Deployments" };
67
- const userOne = { id: "user-1", displayName: "User One" };
68
- const bobUser = { id: "user-2", displayName: "Bob" };
69
-
70
- function jsonResponse(body: unknown, init?: ResponseInit): Response {
71
- return new Response(JSON.stringify(body), {
72
- status: 200,
73
- headers: { "content-type": "application/json" },
74
- ...init,
75
- });
76
- }
77
-
78
- function textResponse(body: string, init?: ResponseInit): Response {
79
- return new Response(body, init);
80
- }
81
-
82
- function mockFetch(handler: Parameters<typeof vi.fn>[0]) {
83
- globalThis.fetch = vi.fn(handler) as unknown as typeof fetch;
84
- }
85
-
86
- function mockJsonFetchResponse(body: unknown, init?: ResponseInit) {
87
- mockFetch(async () => jsonResponse(body, init));
88
- }
89
-
90
- function mockTextFetchResponse(body: string, init?: ResponseInit) {
91
- mockFetch(async () => textResponse(body, init));
92
- }
93
-
94
- function graphCollection<T>(...items: T[]) {
95
- return { value: items };
96
- }
97
-
98
- function mockGraphCollection(...items: unknown[]) {
99
- mockJsonFetchResponse(graphCollection(...items));
100
- }
101
-
102
- function requestUrl(input: string | URL | Request) {
103
- if (typeof input === "string") {
104
- return input;
105
- }
106
- if (input instanceof URL) {
107
- return input.toString();
108
- }
109
- return input.url;
110
- }
111
-
112
- function fetchCallUrl(index: number) {
113
- const input = vi.mocked(globalThis.fetch).mock.calls[index]?.[0];
114
- if (!input) {
115
- return "";
116
- }
117
- return requestUrl(input);
118
- }
119
-
120
- function fetchCallInit(index: number) {
121
- return vi.mocked(globalThis.fetch).mock.calls[index]?.[1];
122
- }
123
-
124
- function fetchCallHeader(index: number, name: string) {
125
- const headers = fetchCallInit(index)?.headers;
126
- if (!headers) {
127
- throw new Error(`Expected fetch headers at index ${index}`);
128
- }
129
- return (headers as Record<string, string>)[name];
130
- }
131
-
132
- function expectFetchPathContains(index: number, expectedPath: string) {
133
- expect(fetchCallUrl(index)).toContain(expectedPath);
134
- }
135
-
136
- function fetchCallSearchParam(index: number, name: string): string | null {
137
- const url = fetchCallUrl(index);
138
- if (!url) {
139
- throw new Error(`Expected fetch call ${index}`);
140
- }
141
- return new URL(url).searchParams.get(name);
142
- }
143
-
144
- async function expectSearchGraphUsers(
145
- query: string,
146
- expected: Array<Record<string, unknown>>,
147
- options?: { token?: string; top?: number },
148
- ) {
149
- await expect(
150
- searchGraphUsers({
151
- token: options?.token ?? graphToken,
152
- query,
153
- top: options?.top,
154
- }),
155
- ).resolves.toEqual(expected);
156
- }
157
-
158
- async function expectRejectsToThrow(promise: Promise<unknown>, message: string) {
159
- await expect(promise).rejects.toThrow(message);
160
- }
161
-
162
- function mockGraphTokenResolution(options?: {
163
- rawToken?: string | null;
164
- resolvedToken?: string | null;
165
- }) {
166
- const rawToken = options && "rawToken" in options ? options.rawToken : "raw-graph-token";
167
- const resolvedToken =
168
- options && "resolvedToken" in options ? options.resolvedToken : "resolved-token";
169
- const getAccessToken = vi.fn(async () => rawToken);
170
- loadMSTeamsSdkWithAuthMock.mockResolvedValue({ app: mockApp });
171
- createMSTeamsTokenProviderMock.mockReturnValue({ getAccessToken });
172
- resolveMSTeamsCredentialsMock.mockReturnValue(mockCredentials);
173
- readAccessTokenMock.mockReturnValue(resolvedToken);
174
- return { getAccessToken };
175
- }
176
-
177
- describe("msteams graph helpers", () => {
178
- beforeEach(() => {
179
- vi.clearAllMocks();
180
- });
181
-
182
- afterEach(() => {
183
- globalThis.fetch = originalFetch;
184
- });
185
-
186
- it("normalizes queries and escapes OData apostrophes", () => {
187
- expect(normalizeQuery(" Team Alpha ")).toBe("Team Alpha");
188
- expect(normalizeQuery(" ")).toBe("");
189
- expect(escapeOData("alice.o'hara")).toBe("alice.o''hara");
190
- });
191
-
192
- it("fetches Graph JSON and surfaces Graph errors with response text", async () => {
193
- mockGraphCollection(groupOne);
194
-
195
- await expect(
196
- fetchGraphJson<{ value: Array<{ id: string }> }>({
197
- token: graphToken,
198
- path: "/groups?$select=id",
199
- headers: { ConsistencyLevel: "eventual" },
200
- }),
201
- ).resolves.toEqual(graphCollection(groupOne));
202
-
203
- expect(fetchCallUrl(0)).toBe("https://graph.microsoft.com/v1.0/groups?$select=id");
204
- expect(fetchCallHeader(0, "Authorization")).toBe(`Bearer ${graphToken}`);
205
- expect(fetchCallHeader(0, "ConsistencyLevel")).toBe("eventual");
206
-
207
- mockTextFetchResponse("forbidden", { status: 403 });
208
-
209
- await expectRejectsToThrow(
210
- fetchGraphJson({
211
- token: graphToken,
212
- path: "/teams/team-1/channels",
213
- }),
214
- "Graph /teams/team-1/channels failed (403): forbidden",
215
- );
216
-
217
- mockTextFetchResponse("{ nope", {
218
- status: 200,
219
- headers: { "content-type": "application/json" },
220
- });
221
-
222
- await expectRejectsToThrow(
223
- fetchGraphJson({
224
- token: graphToken,
225
- path: "/teams/team-1/channels",
226
- }),
227
- "Graph /teams/team-1/channels failed: malformed JSON response",
228
- );
229
- });
230
-
231
- it("posts Graph JSON to v1 and beta roots and treats empty mutation responses as undefined", async () => {
232
- mockFetch(async (input) => {
233
- if (requestUrl(input).startsWith("https://graph.microsoft.com/beta")) {
234
- return new Response(null, { status: 204 });
235
- }
236
- return jsonResponse({ id: "created-1" });
237
- });
238
-
239
- await expect(
240
- postGraphJson<{ id: string }>({
241
- token: graphToken,
242
- path: "/chats/chat-1/pinnedMessages",
243
- body: { messageId: "msg-1" },
244
- }),
245
- ).resolves.toEqual({ id: "created-1" });
246
-
247
- await expect(
248
- postGraphBetaJson<undefined>({
249
- token: graphToken,
250
- path: "/chats/chat-1/messages/msg-1/setReaction",
251
- body: { reactionType: "like" },
252
- }),
253
- ).resolves.toBeUndefined();
254
-
255
- expect(fetchCallUrl(0)).toBe("https://graph.microsoft.com/v1.0/chats/chat-1/pinnedMessages");
256
- expect(fetchCallInit(0)?.method).toBe("POST");
257
- expect(fetchCallInit(0)?.body).toBe(JSON.stringify({ messageId: "msg-1" }));
258
- expect(fetchCallHeader(0, "Authorization")).toBe(`Bearer ${graphToken}`);
259
- expect(fetchCallHeader(0, "Content-Type")).toBe("application/json");
260
- expect(fetchCallUrl(1)).toBe(
261
- "https://graph.microsoft.com/beta/chats/chat-1/messages/msg-1/setReaction",
262
- );
263
- expect(fetchCallInit(1)?.method).toBe("POST");
264
- expect(fetchCallInit(1)?.body).toBe(JSON.stringify({ reactionType: "like" }));
265
- });
266
-
267
- it("surfaces POST and DELETE graph failures with method-specific labels", async () => {
268
- mockFetch(async (_input, init) => {
269
- const method = init?.method ?? "GET";
270
- if (method === "DELETE") {
271
- return textResponse("not found", { status: 404 });
272
- }
273
- return textResponse("denied", { status: 403 });
274
- });
275
-
276
- await expectRejectsToThrow(
277
- postGraphJson({
278
- token: graphToken,
279
- path: "/teams/team-1/channels",
280
- body: { displayName: "Deployments" },
281
- }),
282
- "Graph POST /teams/team-1/channels failed (403): denied",
283
- );
284
-
285
- await expectRejectsToThrow(
286
- deleteGraphRequest({
287
- token: graphToken,
288
- path: "/teams/team-1/channels/channel-1",
289
- }),
290
- "Graph DELETE /teams/team-1/channels/channel-1 failed (404): not found",
291
- );
292
- });
293
-
294
- it("resolves Graph tokens through the SDK auth provider", async () => {
295
- const { getAccessToken } = mockGraphTokenResolution();
296
-
297
- await expect(resolveGraphToken({ channels: { msteams: {} } })).resolves.toBe("resolved-token");
298
-
299
- expect(createMSTeamsTokenProviderMock).toHaveBeenCalledWith(mockApp);
300
- expect(getAccessToken).toHaveBeenCalledWith("https://graph.microsoft.com");
301
- });
302
-
303
- it("fails when credentials or access tokens are unavailable", async () => {
304
- resolveMSTeamsCredentialsMock.mockReturnValue(undefined);
305
- await expectRejectsToThrow(resolveGraphToken({ channels: {} }), "MS Teams credentials missing");
306
-
307
- mockGraphTokenResolution({ rawToken: null, resolvedToken: null });
308
-
309
- await expectRejectsToThrow(
310
- resolveGraphToken({ channels: { msteams: {} } }),
311
- "MS Teams graph token unavailable",
312
- );
313
- });
314
-
315
- it("builds encoded Graph paths for teams and channels", async () => {
316
- mockFetch(async (input) => {
317
- if (requestUrl(input).includes("/groups?")) {
318
- return jsonResponse(graphCollection(opsTeam));
319
- }
320
- return jsonResponse(graphCollection(deploymentsChannel));
321
- });
322
-
323
- await expect(listTeamsByName(graphToken, "Bob's Team")).resolves.toEqual([opsTeam]);
324
- await expect(listChannelsForTeam(graphToken, "team/ops")).resolves.toEqual([
325
- deploymentsChannel,
326
- ]);
327
-
328
- expect(fetchCallSearchParam(0, "$filter")).toBe(
329
- "resourceProvisioningOptions/Any(x:x eq 'Team') and startsWith(displayName,'Bob''s Team')",
330
- );
331
- expect(fetchCallSearchParam(0, "$select")).toBe("id,displayName");
332
- expectFetchPathContains(1, "/teams/team%2Fops/channels?$select=id,displayName");
333
- });
334
-
335
- it("returns no graph users for blank queries", async () => {
336
- mockJsonFetchResponse({});
337
- await expectSearchGraphUsers(" ", [], { token: "token-1" });
338
- expect(globalThis.fetch).not.toHaveBeenCalled();
339
- });
340
-
341
- it("uses exact mail or UPN lookup for email-like graph user queries", async () => {
342
- mockGraphCollection(userOne);
343
-
344
- await expectSearchGraphUsers("alice.o'hara@example.com", [userOne], {
345
- token: "token-2",
346
- });
347
- expect(fetchCallSearchParam(0, "$filter")).toBe(
348
- "(mail eq 'alice.o''hara@example.com' or userPrincipalName eq 'alice.o''hara@example.com')",
349
- );
350
- expect(fetchCallSearchParam(0, "$select")).toBe("id,displayName,mail,userPrincipalName");
351
- });
352
-
353
- it("uses displayName search with eventual consistency and default top handling", async () => {
354
- mockFetch(async (input) => {
355
- if (requestUrl(input).includes("displayName%3Abob")) {
356
- return jsonResponse(graphCollection(bobUser));
357
- }
358
- return jsonResponse({});
359
- });
360
-
361
- await expectSearchGraphUsers("bob", [bobUser], {
362
- token: "token-3",
363
- top: 25,
364
- });
365
- await expectSearchGraphUsers("carol", [], { token: "token-4" });
366
-
367
- expectFetchPathContains(
368
- 0,
369
- "/users?$search=%22displayName%3Abob%22&$select=id,displayName,mail,userPrincipalName&$top=25",
370
- );
371
- expect(fetchCallHeader(0, "ConsistencyLevel")).toBe("eventual");
372
- expectFetchPathContains(
373
- 1,
374
- "/users?$search=%22displayName%3Acarol%22&$select=id,displayName,mail,userPrincipalName&$top=10",
375
- );
376
- });
377
-
378
- describe("fetchAllGraphPages", () => {
379
- type Item = { id: string; name: string };
380
-
381
- /** Build a paged Graph response with optional nextLink. */
382
- function pagedResponse(items: Item[], nextLink?: string) {
383
- const body: Record<string, unknown> = { value: items };
384
- if (nextLink) {
385
- body["@odata.nextLink"] = nextLink;
386
- }
387
- return body;
388
- }
389
-
390
- it("single page, no nextLink", async () => {
391
- const items = [{ id: "1", name: "a" }];
392
- mockJsonFetchResponse(pagedResponse(items));
393
-
394
- const result = await fetchAllGraphPages<Item>({
395
- token: graphToken,
396
- path: "/items",
397
- });
398
-
399
- expect(result).toEqual({ items, truncated: false });
400
- expect(globalThis.fetch).toHaveBeenCalledTimes(1);
401
- });
402
-
403
- it("multiple pages with nextLink chain", async () => {
404
- const page1Items = [{ id: "1", name: "a" }];
405
- const page2Items = [{ id: "2", name: "b" }];
406
- const page3Items = [{ id: "3", name: "c" }];
407
- let callCount = 0;
408
-
409
- mockFetch(async () => {
410
- callCount++;
411
- if (callCount === 1) {
412
- return jsonResponse(
413
- pagedResponse(page1Items, "https://graph.microsoft.com/v1.0/items?$skiptoken=page2"),
414
- );
415
- }
416
- if (callCount === 2) {
417
- return jsonResponse(
418
- pagedResponse(page2Items, "https://graph.microsoft.com/v1.0/items?$skiptoken=page3"),
419
- );
420
- }
421
- return jsonResponse(pagedResponse(page3Items));
422
- });
423
-
424
- const result = await fetchAllGraphPages<Item>({
425
- token: graphToken,
426
- path: "/items",
427
- });
428
-
429
- expect(result.items).toEqual([...page1Items, ...page2Items, ...page3Items]);
430
- expect(result.truncated).toBe(false);
431
- expect(globalThis.fetch).toHaveBeenCalledTimes(3);
432
- });
433
-
434
- it("truncation at maxPages", async () => {
435
- mockFetch(async () =>
436
- jsonResponse(
437
- pagedResponse(
438
- [{ id: "x", name: "x" }],
439
- "https://graph.microsoft.com/v1.0/items?$skiptoken=more",
440
- ),
441
- ),
442
- );
443
-
444
- const result = await fetchAllGraphPages<Item>({
445
- token: graphToken,
446
- path: "/items",
447
- maxPages: 2,
448
- });
449
-
450
- expect(result.items).toHaveLength(2);
451
- expect(result.truncated).toBe(true);
452
- expect(globalThis.fetch).toHaveBeenCalledTimes(2);
453
- });
454
-
455
- it("findOne early exit", async () => {
456
- const target = { id: "target", name: "found-it" };
457
- let callCount = 0;
458
-
459
- mockFetch(async () => {
460
- callCount++;
461
- if (callCount === 1) {
462
- return jsonResponse(
463
- pagedResponse(
464
- [{ id: "1", name: "a" }],
465
- "https://graph.microsoft.com/v1.0/items?$skiptoken=p2",
466
- ),
467
- );
468
- }
469
- // Page 2 contains the target; page 3 should never be fetched
470
- return jsonResponse(
471
- pagedResponse(
472
- [{ id: "2", name: "b" }, target],
473
- "https://graph.microsoft.com/v1.0/items?$skiptoken=p3",
474
- ),
475
- );
476
- });
477
-
478
- const result = await fetchAllGraphPages<Item>({
479
- token: graphToken,
480
- path: "/items",
481
- findOne: (item) => item.id === "target",
482
- });
483
-
484
- expect(result.found).toEqual(target);
485
- expect(result.truncated).toBe(false);
486
- // Page 1 items + page 2 items (where match was found)
487
- expect(result.items).toEqual([{ id: "1", name: "a" }, { id: "2", name: "b" }, target]);
488
- // Only 2 fetches; page 3 was never requested
489
- expect(globalThis.fetch).toHaveBeenCalledTimes(2);
490
- });
491
-
492
- it("findOne with no match (exhausted)", async () => {
493
- mockJsonFetchResponse(pagedResponse([{ id: "1", name: "a" }]));
494
-
495
- const result = await fetchAllGraphPages<Item>({
496
- token: graphToken,
497
- path: "/items",
498
- findOne: (item) => item.id === "missing",
499
- });
500
-
501
- expect(result.found).toBeUndefined();
502
- expect(result.truncated).toBe(false);
503
- expect(result.items).toEqual([{ id: "1", name: "a" }]);
504
- });
505
-
506
- it("findOne with no match (truncated)", async () => {
507
- mockFetch(async () =>
508
- jsonResponse(
509
- pagedResponse(
510
- [{ id: "x", name: "x" }],
511
- "https://graph.microsoft.com/v1.0/items?$skiptoken=more",
512
- ),
513
- ),
514
- );
515
-
516
- const result = await fetchAllGraphPages<Item>({
517
- token: graphToken,
518
- path: "/items",
519
- maxPages: 2,
520
- findOne: (item) => item.id === "missing",
521
- });
522
-
523
- expect(result.found).toBeUndefined();
524
- expect(result.truncated).toBe(true);
525
- expect(result.items).toHaveLength(2);
526
- });
527
-
528
- it("empty first page", async () => {
529
- mockJsonFetchResponse(pagedResponse([]));
530
-
531
- const result = await fetchAllGraphPages<Item>({
532
- token: graphToken,
533
- path: "/items",
534
- });
535
-
536
- expect(result).toEqual({ items: [], truncated: false });
537
- expect(globalThis.fetch).toHaveBeenCalledTimes(1);
538
- });
539
- });
540
- });