Skip to content

Commit d8add73

Browse files
committed
test(msteams): prove attachmentInfo stream cancellation
1 parent 3849d4c commit d8add73

1 file changed

Lines changed: 32 additions & 31 deletions

File tree

extensions/msteams/src/attachments/bot-framework.test.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -534,39 +534,40 @@ describe("downloadMSTeamsBotFrameworkAttachment", () => {
534534
const jsonSpy = vi.spyOn(Response.prototype, "json").mockImplementation(async () => {
535535
throw new Error("raw response.json() should not be used");
536536
});
537-
const fetchFn = createMockFetch([
538-
{
539-
match: /\/v3\/attachments\/att-1$/,
540-
response: new Response(stream, {
541-
status: 200,
542-
headers: { "content-type": "application/json" },
543-
}),
544-
},
545-
]);
546-
547-
const warn = vi.fn();
548-
const media = await downloadMSTeamsBotFrameworkAttachment({
549-
serviceUrl: "https://smba.trafficmanager.net/amer",
550-
attachmentId: "att-1",
551-
tokenProvider: buildTokenProvider(),
552-
maxBytes: 10_000_000,
553-
fetchFn,
554-
fetchFnSupportsDispatcher: true,
555-
resolveFn: resolvePublicHost,
556-
logger: { warn },
537+
const fetchFn: typeof fetch = vi.fn(async () => {
538+
return new Response(stream, {
539+
status: 200,
540+
headers: { "content-type": "application/json" },
541+
});
557542
});
558543

559-
expect(media).toBeUndefined();
560-
expect(jsonSpy).not.toHaveBeenCalled();
561-
// Enforced well before the 64 MiB test ceiling; an unbounded reader would keep pulling.
562-
expect(state.enqueued).toBeLessThan(32);
563-
expect(warn).toHaveBeenCalledWith(
564-
"msteams botFramework attachmentInfo parse failed",
565-
expect.objectContaining({
566-
error: expect.stringMatching(/JSON response exceeds 16777216 bytes/),
567-
}),
568-
);
569-
jsonSpy.mockRestore();
544+
try {
545+
const warn = vi.fn();
546+
const media = await downloadMSTeamsBotFrameworkAttachment({
547+
serviceUrl: "https://smba.trafficmanager.net/amer",
548+
attachmentId: "att-1",
549+
tokenProvider: buildTokenProvider(),
550+
maxBytes: 10_000_000,
551+
fetchFn,
552+
fetchFnSupportsDispatcher: true,
553+
resolveFn: resolvePublicHost,
554+
logger: { warn },
555+
});
556+
557+
expect(media).toBeUndefined();
558+
expect(jsonSpy).not.toHaveBeenCalled();
559+
// Enforced well before the 64 MiB test ceiling; an unbounded reader would keep pulling.
560+
expect(state.enqueued).toBeLessThan(32);
561+
expect(state.canceled).toBe(true);
562+
expect(warn).toHaveBeenCalledWith(
563+
"msteams botFramework attachmentInfo parse failed",
564+
expect.objectContaining({
565+
error: expect.stringMatching(/JSON response exceeds 16777216 bytes/),
566+
}),
567+
);
568+
} finally {
569+
jsonSpy.mockRestore();
570+
}
570571
});
571572
});
572573
});

0 commit comments

Comments
 (0)