From 30eb467ec84daeb114369d687d2e9a479e0717c2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 21:52:01 +0100 Subject: [PATCH] test: share msteams attachment fixtures --- .../msteams/src/attachments.graph.test.ts | 18 +----------------- .../msteams/src/attachments.test-helpers.ts | 17 +++++++++++++++++ extensions/msteams/src/attachments.test.ts | 19 +------------------ 3 files changed, 19 insertions(+), 35 deletions(-) create mode 100644 extensions/msteams/src/attachments.test-helpers.ts diff --git a/extensions/msteams/src/attachments.graph.test.ts b/extensions/msteams/src/attachments.graph.test.ts index 20ffee21497..977ba3ad921 100644 --- a/extensions/msteams/src/attachments.graph.test.ts +++ b/extensions/msteams/src/attachments.graph.test.ts @@ -1,6 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { mockPinnedHostnameResolution } from "../../../src/test-helpers/ssrf.js"; import type { PluginRuntime } from "../runtime-api.js"; +import { readRemoteMediaResponse } from "./attachments.test-helpers.js"; import { downloadMSTeamsGraphMedia } from "./attachments/graph.js"; import { resolveRequestUrl } from "./attachments/shared.js"; import { setMSTeamsRuntime } from "./runtime.js"; @@ -23,23 +24,6 @@ const saveMediaBufferMock = vi.fn(async () => ({ size: Buffer.byteLength(PNG_BUFFER), contentType: CONTENT_TYPE_IMAGE_PNG, })); -const readRemoteMediaResponse = async ( - res: Response, - params: { maxBytes?: number; filePathHint?: string }, -) => { - if (!res.ok) { - throw new Error(`HTTP ${res.status}`); - } - const buffer = Buffer.from(await res.arrayBuffer()); - if (typeof params.maxBytes === "number" && buffer.byteLength > params.maxBytes) { - throw new Error(`payload exceeds maxBytes ${params.maxBytes}`); - } - return { - buffer, - contentType: res.headers.get("content-type") ?? undefined, - fileName: params.filePathHint, - }; -}; const fetchRemoteMediaMock = vi.fn( async (params: { url: string; diff --git a/extensions/msteams/src/attachments.test-helpers.ts b/extensions/msteams/src/attachments.test-helpers.ts new file mode 100644 index 00000000000..6db8e053cfd --- /dev/null +++ b/extensions/msteams/src/attachments.test-helpers.ts @@ -0,0 +1,17 @@ +export async function readRemoteMediaResponse( + res: Response, + params: { maxBytes?: number; filePathHint?: string }, +) { + if (!res.ok) { + throw new Error(`HTTP ${res.status}`); + } + const buffer = Buffer.from(await res.arrayBuffer()); + if (typeof params.maxBytes === "number" && buffer.byteLength > params.maxBytes) { + throw new Error(`payload exceeds maxBytes ${params.maxBytes}`); + } + return { + buffer, + contentType: res.headers.get("content-type") ?? undefined, + fileName: params.filePathHint, + }; +} diff --git a/extensions/msteams/src/attachments.test.ts b/extensions/msteams/src/attachments.test.ts index 1a34842a7f7..428501f9bba 100644 --- a/extensions/msteams/src/attachments.test.ts +++ b/extensions/msteams/src/attachments.test.ts @@ -1,5 +1,6 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import type { PluginRuntime, SsrFPolicy } from "../runtime-api.js"; +import { readRemoteMediaResponse } from "./attachments.test-helpers.js"; import { downloadMSTeamsAttachments } from "./attachments/download.js"; import { resolveRequestUrl } from "./attachments/shared.js"; import { setMSTeamsRuntime } from "./runtime.js"; @@ -46,24 +47,6 @@ const saveMediaBufferMock = vi.fn(async () => ({ size: Buffer.byteLength(PNG_BUFFER), contentType: CONTENT_TYPE_IMAGE_PNG, })); -const readRemoteMediaResponse = async ( - res: Response, - params: Pick, -) => { - if (!res.ok) { - throw new Error(`HTTP ${res.status}`); - } - const buffer = Buffer.from(await res.arrayBuffer()); - if (typeof params.maxBytes === "number" && buffer.byteLength > params.maxBytes) { - throw new Error(`payload exceeds maxBytes ${params.maxBytes}`); - } - return { - buffer, - contentType: res.headers.get("content-type") ?? undefined, - fileName: params.filePathHint, - }; -}; - function isHostnameAllowedByPattern(hostname: string, pattern: string): boolean { if (pattern.startsWith("*.")) { const suffix = pattern.slice(2);