test(msteams): cover upload and webhook helpers

This commit is contained in:
Vincent Koc
2026-03-22 19:07:04 -07:00
parent 1ea2593362
commit 7d11f6cf69
2 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
clearPendingUploads,
getPendingUpload,
getPendingUploadCount,
removePendingUpload,
storePendingUpload,
} from "./pending-uploads.js";
describe("msteams pending uploads", () => {
beforeEach(() => {
vi.useFakeTimers();
clearPendingUploads();
});
afterEach(() => {
clearPendingUploads();
vi.useRealTimers();
});
it("stores uploads, exposes them by id, and tracks count", () => {
const id = storePendingUpload({
buffer: Buffer.from("hello"),
filename: "hello.txt",
contentType: "text/plain",
conversationId: "conv-1",
});
expect(getPendingUploadCount()).toBe(1);
expect(getPendingUpload(id)).toEqual(
expect.objectContaining({
id,
filename: "hello.txt",
contentType: "text/plain",
conversationId: "conv-1",
}),
);
});
it("removes uploads explicitly and ignores empty ids", () => {
const id = storePendingUpload({
buffer: Buffer.from("hello"),
filename: "hello.txt",
conversationId: "conv-1",
});
removePendingUpload(undefined);
expect(getPendingUploadCount()).toBe(1);
removePendingUpload(id);
expect(getPendingUpload(id)).toBeUndefined();
expect(getPendingUploadCount()).toBe(0);
});
it("expires uploads by ttl even if the timeout callback has not been observed yet", () => {
const id = storePendingUpload({
buffer: Buffer.from("hello"),
filename: "hello.txt",
conversationId: "conv-1",
});
vi.advanceTimersByTime(5 * 60 * 1000 + 1);
expect(getPendingUpload(id)).toBeUndefined();
expect(getPendingUploadCount()).toBe(0);
});
it("clears all uploads for test cleanup", () => {
storePendingUpload({
buffer: Buffer.from("a"),
filename: "a.txt",
conversationId: "conv-1",
});
storePendingUpload({
buffer: Buffer.from("b"),
filename: "b.txt",
conversationId: "conv-2",
});
clearPendingUploads();
expect(getPendingUploadCount()).toBe(0);
});
});

View File

@@ -0,0 +1,36 @@
import { describe, expect, it, vi } from "vitest";
import { applyMSTeamsWebhookTimeouts } from "./webhook-timeouts.js";
describe("applyMSTeamsWebhookTimeouts", () => {
it("applies default timeouts and header clamp", () => {
const httpServer = {
setTimeout: vi.fn(),
requestTimeout: 0,
headersTimeout: 0,
} as never;
applyMSTeamsWebhookTimeouts(httpServer);
expect(httpServer.setTimeout).toHaveBeenCalledWith(30_000);
expect(httpServer.requestTimeout).toBe(30_000);
expect(httpServer.headersTimeout).toBe(15_000);
});
it("uses explicit overrides and clamps headers timeout to request timeout", () => {
const httpServer = {
setTimeout: vi.fn(),
requestTimeout: 0,
headersTimeout: 0,
} as never;
applyMSTeamsWebhookTimeouts(httpServer, {
inactivityTimeoutMs: 12_000,
requestTimeoutMs: 9_000,
headersTimeoutMs: 15_000,
});
expect(httpServer.setTimeout).toHaveBeenCalledWith(12_000);
expect(httpServer.requestTimeout).toBe(9_000);
expect(httpServer.headersTimeout).toBe(9_000);
});
});