@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
@@ -1,441 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- // Mock shared.js to avoid transitive runtime-api imports that pull in uninstalled packages.
4
- vi.mock("./shared.js", async (importOriginal) => {
5
- const actual = await importOriginal<typeof import("./shared.js")>();
6
- return {
7
- ...actual,
8
- applyAuthorizationHeaderForUrl: vi.fn(),
9
- GRAPH_ROOT: "https://graph.microsoft.com/v1.0",
10
- inferPlaceholder: vi.fn(({ contentType }: { contentType?: string }) =>
11
- contentType?.startsWith("image/") ? "[image]" : "[file]",
12
- ),
13
- isRecord: (v: unknown) => typeof v === "object" && v !== null && !Array.isArray(v),
14
- isUrlAllowed: vi.fn(() => true),
15
- normalizeContentType: vi.fn((ct: string | null | undefined) => ct ?? undefined),
16
- resolveMediaSsrfPolicy: vi.fn(() => undefined),
17
- resolveAttachmentFetchPolicy: vi.fn(() => ({ allowHosts: ["*"], authAllowHosts: ["*"] })),
18
- resolveRequestUrl: vi.fn((input: string) => input),
19
- safeFetchWithPolicy: vi.fn(),
20
- };
21
- });
22
-
23
- vi.mock("klaw/plugin-sdk/ssrf-runtime", () => ({
24
- fetchWithSsrFGuard: vi.fn(),
25
- }));
26
-
27
- vi.mock("../runtime.js", () => ({
28
- getMSTeamsRuntime: vi.fn(() => ({
29
- media: {
30
- detectMime: vi.fn(async () => "image/png"),
31
- },
32
- channel: {
33
- media: {
34
- saveResponseMedia: vi.fn(
35
- async (
36
- response: Response,
37
- options?: { fallbackContentType?: string; maxBytes?: number },
38
- ) => {
39
- const length = Number(response.headers.get("content-length"));
40
- if (
41
- Number.isFinite(length) &&
42
- options?.maxBytes !== undefined &&
43
- length > options.maxBytes
44
- ) {
45
- throw new Error("content length exceeds maxBytes");
46
- }
47
- return {
48
- path: "/tmp/saved.png",
49
- contentType: options?.fallbackContentType ?? "image/png",
50
- };
51
- },
52
- ),
53
- saveMediaBuffer: vi.fn(async (_buf: Buffer, ct: string) => ({
54
- path: "/tmp/saved.png",
55
- contentType: ct ?? "image/png",
56
- })),
57
- },
58
- },
59
- })),
60
- }));
61
-
62
- vi.mock("./download.js", () => ({
63
- downloadMSTeamsAttachments: vi.fn(async () => []),
64
- }));
65
-
66
- vi.mock("./remote-media.js", () => ({
67
- downloadAndStoreMSTeamsRemoteMedia: vi.fn(),
68
- }));
69
-
70
- import { fetchWithSsrFGuard } from "klaw/plugin-sdk/ssrf-runtime";
71
- import { downloadMSTeamsGraphMedia } from "./graph.js";
72
- import { downloadAndStoreMSTeamsRemoteMedia } from "./remote-media.js";
73
- import { safeFetchWithPolicy } from "./shared.js";
74
-
75
- function mockFetchResponse(body: unknown, status = 200) {
76
- const bodyStr = typeof body === "string" ? body : JSON.stringify(body);
77
- return new Response(bodyStr, { status, headers: { "content-type": "application/json" } });
78
- }
79
-
80
- function mockBinaryResponse(data: Uint8Array, status = 200) {
81
- return new Response(Buffer.from(data) as BodyInit, { status });
82
- }
83
-
84
- type GuardedFetchParams = { url: string; init?: RequestInit };
85
-
86
- function guardedFetchResult(params: GuardedFetchParams, response: Response) {
87
- return {
88
- response,
89
- release: async () => {},
90
- finalUrl: params.url,
91
- };
92
- }
93
-
94
- function requireFirstMockCall<TArgs extends unknown[]>(
95
- mock: { mock: { calls: TArgs[] } },
96
- label: string,
97
- ): TArgs {
98
- const [call] = mock.mock.calls;
99
- if (!call) {
100
- throw new Error(`expected ${label}`);
101
- }
102
- return call;
103
- }
104
-
105
- function mockGraphMediaFetch(options: {
106
- messageId: string;
107
- messageResponse?: unknown;
108
- hostedContents?: unknown[];
109
- valueResponses?: Record<string, Response>;
110
- fetchCalls?: string[];
111
- }) {
112
- vi.mocked(fetchWithSsrFGuard).mockImplementation(async (params: GuardedFetchParams) => {
113
- options.fetchCalls?.push(params.url);
114
- const url = params.url;
115
- if (url.endsWith(`/messages/${options.messageId}`) && !url.includes("hostedContents")) {
116
- return guardedFetchResult(
117
- params,
118
- mockFetchResponse(options.messageResponse ?? { body: {}, attachments: [] }),
119
- );
120
- }
121
- if (url.endsWith("/hostedContents")) {
122
- return guardedFetchResult(params, mockFetchResponse({ value: options.hostedContents ?? [] }));
123
- }
124
- for (const [fragment, response] of Object.entries(options.valueResponses ?? {})) {
125
- if (url.includes(fragment)) {
126
- return guardedFetchResult(params, response);
127
- }
128
- }
129
- return guardedFetchResult(params, mockFetchResponse({}, 404));
130
- });
131
- }
132
-
133
- describe("downloadMSTeamsGraphMedia hosted content $value fallback", () => {
134
- beforeEach(() => {
135
- vi.clearAllMocks();
136
- });
137
-
138
- it("fetches $value endpoint when contentBytes is null but item.id exists", async () => {
139
- const imageBytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]); // PNG magic bytes
140
-
141
- const fetchCalls: string[] = [];
142
-
143
- mockGraphMediaFetch({
144
- messageId: "msg-1",
145
- hostedContents: [{ id: "hosted-123", contentType: "image/png", contentBytes: null }],
146
- valueResponses: {
147
- "/hostedContents/hosted-123/$value": mockBinaryResponse(imageBytes),
148
- },
149
- fetchCalls,
150
- });
151
-
152
- const result = await downloadMSTeamsGraphMedia({
153
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-1",
154
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
155
- maxBytes: 10 * 1024 * 1024,
156
- });
157
-
158
- // Verify the $value endpoint was fetched
159
- expect(fetchCalls).toContain(
160
- "https://graph.microsoft.com/v1.0/chats/c/messages/msg-1/hostedContents/hosted-123/$value",
161
- );
162
- expect(result.media.length).toBeGreaterThan(0);
163
- expect(result.hostedCount).toBe(1);
164
- });
165
-
166
- it("skips hosted content when contentBytes is null and id is missing", async () => {
167
- mockGraphMediaFetch({
168
- messageId: "msg-2",
169
- hostedContents: [{ contentType: "image/png", contentBytes: null }],
170
- });
171
-
172
- const result = await downloadMSTeamsGraphMedia({
173
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-2",
174
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
175
- maxBytes: 10 * 1024 * 1024,
176
- });
177
-
178
- // No media because there's no id to fetch $value from and no contentBytes
179
- expect(result.media).toHaveLength(0);
180
- });
181
-
182
- it("skips $value content when Content-Length exceeds maxBytes", async () => {
183
- const fetchCalls: string[] = [];
184
-
185
- mockGraphMediaFetch({
186
- messageId: "msg-cl",
187
- hostedContents: [{ id: "hosted-big", contentType: "image/png", contentBytes: null }],
188
- valueResponses: {
189
- "/hostedContents/hosted-big/$value": new Response(
190
- Buffer.from(new Uint8Array([0x89, 0x50, 0x4e, 0x47])) as BodyInit,
191
- {
192
- status: 200,
193
- headers: { "content-length": "999999999" },
194
- },
195
- ),
196
- },
197
- fetchCalls,
198
- });
199
-
200
- const result = await downloadMSTeamsGraphMedia({
201
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-cl",
202
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
203
- maxBytes: 1024, // 1 KB limit
204
- });
205
-
206
- // $value was fetched but skipped due to Content-Length exceeding maxBytes
207
- expect(fetchCalls).toContain(
208
- "https://graph.microsoft.com/v1.0/chats/c/messages/msg-cl/hostedContents/hosted-big/$value",
209
- );
210
- expect(result.media).toHaveLength(0);
211
- });
212
-
213
- it("uses inline contentBytes when available instead of $value", async () => {
214
- const fetchCalls: string[] = [];
215
- const base64Png = Buffer.from([0x89, 0x50, 0x4e, 0x47]).toString("base64");
216
-
217
- mockGraphMediaFetch({
218
- messageId: "msg-3",
219
- hostedContents: [{ id: "hosted-456", contentType: "image/png", contentBytes: base64Png }],
220
- fetchCalls,
221
- });
222
-
223
- const result = await downloadMSTeamsGraphMedia({
224
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-3",
225
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
226
- maxBytes: 10 * 1024 * 1024,
227
- });
228
-
229
- // Should NOT have fetched $value since contentBytes was available
230
- const valueCall = fetchCalls.find((u) => u.includes("/$value"));
231
- expect(valueCall).toBeUndefined();
232
- expect(result.media.length).toBeGreaterThan(0);
233
- });
234
-
235
- it("adds the Klaw User-Agent to guarded Graph attachment fetches", async () => {
236
- mockGraphMediaFetch({ messageId: "msg-ua" });
237
-
238
- await downloadMSTeamsGraphMedia({
239
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-ua",
240
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
241
- maxBytes: 10 * 1024 * 1024,
242
- });
243
-
244
- const guardCalls = vi.mocked(fetchWithSsrFGuard).mock.calls;
245
- for (const [call] of guardCalls) {
246
- const headers = call.init?.headers;
247
- expect(headers).toBeInstanceOf(Headers);
248
- expect((headers as Headers).get("Authorization")).toBe("Bearer test-token");
249
- expect((headers as Headers).get("User-Agent")).toMatch(/^teams\.ts\[apps\]\/.+ Klaw\/.+$/);
250
- }
251
- });
252
-
253
- it("adds the Klaw User-Agent to Graph shares downloads for reference attachments", async () => {
254
- mockGraphMediaFetch({
255
- messageId: "msg-share",
256
- messageResponse: {
257
- body: {},
258
- attachments: [
259
- {
260
- contentType: "reference",
261
- contentUrl: "https://tenant.sharepoint.com/file.docx",
262
- name: "file.docx",
263
- },
264
- ],
265
- },
266
- });
267
- vi.mocked(safeFetchWithPolicy).mockResolvedValue(new Response(null, { status: 200 }));
268
- vi.mocked(downloadAndStoreMSTeamsRemoteMedia).mockImplementation(async (params) => {
269
- if (params.fetchImpl) {
270
- await params.fetchImpl(params.url, {});
271
- }
272
- return {
273
- path: "/tmp/file.docx",
274
- contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
275
- placeholder: "[file]",
276
- };
277
- });
278
-
279
- await downloadMSTeamsGraphMedia({
280
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-share",
281
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
282
- maxBytes: 10 * 1024 * 1024,
283
- });
284
-
285
- const [fetchParams] = requireFirstMockCall(
286
- vi.mocked(safeFetchWithPolicy),
287
- "safeFetchWithPolicy call",
288
- );
289
- expect(fetchParams.requestInit?.headers).toBeInstanceOf(Headers);
290
- const requestInit = fetchParams.requestInit;
291
- const headers = requestInit?.headers as Headers;
292
- expect(headers.get("User-Agent")).toMatch(/^teams\.ts\[apps\]\/.+ Klaw\/.+$/);
293
- });
294
- });
295
-
296
- describe("downloadMSTeamsGraphMedia attachment sourcing and error logging", () => {
297
- beforeEach(() => {
298
- vi.clearAllMocks();
299
- });
300
-
301
- it("does NOT call the nonexistent ${messageUrl}/attachments sub-resource", async () => {
302
- // The Graph v1.0 API does not expose a `/attachments` sub-resource on
303
- // channel or chat messages. Issue #58617 documented that the old code
304
- // path called this endpoint and recorded a 404 in diagnostics. After
305
- // this fix, the helper must source attachments from the main message
306
- // resource's inline `attachments` array instead.
307
- const fetchCalls: string[] = [];
308
-
309
- mockGraphMediaFetch({
310
- messageId: "msg-no-sub",
311
- messageResponse: {
312
- body: { content: "hi" },
313
- attachments: [],
314
- },
315
- fetchCalls,
316
- });
317
-
318
- await downloadMSTeamsGraphMedia({
319
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-no-sub",
320
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
321
- maxBytes: 10 * 1024 * 1024,
322
- });
323
-
324
- const calledSubResource = fetchCalls.some((u) =>
325
- u.endsWith("/messages/msg-no-sub/attachments"),
326
- );
327
- expect(calledSubResource).toBe(false);
328
- });
329
-
330
- it("sources reference attachments from the message body's attachments array", async () => {
331
- // Before the fix, the helper fetched `/attachments` and used that list.
332
- // After the fix, it must use `msgData.attachments` from the main fetch.
333
- mockGraphMediaFetch({
334
- messageId: "msg-inline",
335
- messageResponse: {
336
- body: {},
337
- attachments: [
338
- {
339
- contentType: "reference",
340
- contentUrl: "https://tenant.sharepoint.com/inline.pdf",
341
- name: "inline.pdf",
342
- },
343
- ],
344
- },
345
- });
346
- vi.mocked(safeFetchWithPolicy).mockResolvedValue(new Response(null, { status: 200 }));
347
- vi.mocked(downloadAndStoreMSTeamsRemoteMedia).mockResolvedValue({
348
- path: "/tmp/inline.pdf",
349
- contentType: "application/pdf",
350
- placeholder: "[file]",
351
- });
352
-
353
- const result = await downloadMSTeamsGraphMedia({
354
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-inline",
355
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
356
- maxBytes: 10 * 1024 * 1024,
357
- });
358
-
359
- expect(result.media).toHaveLength(1);
360
- expect(result.media[0]?.path).toBe("/tmp/inline.pdf");
361
- // Regression guard: attachmentCount now reflects real inline attachments,
362
- // not the imaginary `/attachments` sub-resource count.
363
- expect(result.attachmentCount).toBe(1);
364
- });
365
-
366
- it("logs a debug event when the message fetch throws instead of swallowing it", async () => {
367
- // Regression test for #51749: empty `catch {}` blocks used to hide the
368
- // real error, producing misleading `graph media fetch empty` diagnostics
369
- // without surfacing the underlying cause.
370
- vi.mocked(fetchWithSsrFGuard).mockImplementation(async (params: GuardedFetchParams) => {
371
- if (params.url.endsWith("/messages/msg-err")) {
372
- throw new Error("network boom");
373
- }
374
- // hostedContents and any other paths succeed so the error branch under
375
- // test is the only one that fires.
376
- return guardedFetchResult(params, mockFetchResponse({ value: [] }));
377
- });
378
- const logger = { warn: vi.fn() };
379
-
380
- const result = await downloadMSTeamsGraphMedia({
381
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-err",
382
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
383
- maxBytes: 10 * 1024 * 1024,
384
- logger,
385
- });
386
-
387
- expect(result.media).toHaveLength(0);
388
- const [message, context] = requireFirstMockCall(logger.warn, "message fetch warning");
389
- expect(message).toBe("msteams graph message fetch failed");
390
- expect((context as { error?: unknown }).error).toBe("network boom");
391
- });
392
-
393
- it("logs a debug event when the message fetch returns non-ok", async () => {
394
- // If the message endpoint returns 403/404, we want that recorded so
395
- // operators can distinguish auth issues from empty result sets.
396
- vi.mocked(fetchWithSsrFGuard).mockImplementation(async (params: GuardedFetchParams) => {
397
- const url = params.url;
398
- if (url.endsWith("/hostedContents")) {
399
- return guardedFetchResult(params, mockFetchResponse({ value: [] }));
400
- }
401
- return guardedFetchResult(params, mockFetchResponse({ error: "forbidden" }, 403));
402
- });
403
- const log = { debug: vi.fn() };
404
-
405
- const result = await downloadMSTeamsGraphMedia({
406
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-403",
407
- tokenProvider: { getAccessToken: vi.fn(async () => "test-token") },
408
- maxBytes: 10 * 1024 * 1024,
409
- log,
410
- });
411
-
412
- expect(result.media).toHaveLength(0);
413
- expect(result.attachmentStatus).toBe(403);
414
- const [message, context] = requireFirstMockCall(log.debug, "message fetch debug event");
415
- expect(message).toBe("graph media message fetch not ok");
416
- expect((context as { status?: unknown }).status).toBe(403);
417
- });
418
-
419
- it("logs a debug event when token acquisition fails", async () => {
420
- vi.mocked(fetchWithSsrFGuard).mockImplementation(async (params: GuardedFetchParams) =>
421
- guardedFetchResult(params, mockFetchResponse({})),
422
- );
423
- const logger = { warn: vi.fn() };
424
-
425
- const result = await downloadMSTeamsGraphMedia({
426
- messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-token",
427
- tokenProvider: {
428
- getAccessToken: vi.fn(async () => {
429
- throw new Error("token expired");
430
- }),
431
- },
432
- maxBytes: 10 * 1024 * 1024,
433
- logger,
434
- });
435
-
436
- expect(result.tokenError).toBe(true);
437
- const [message, context] = requireFirstMockCall(logger.warn, "token acquisition warning");
438
- expect(message).toBe("msteams graph token acquisition failed");
439
- expect((context as { error?: unknown }).error).toBe("token expired");
440
- });
441
- });