|
| 1 | +// Msteams tests cover probe token request deadlines. |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { MSTeamsConfig } from "../runtime-api.js"; |
| 4 | + |
| 5 | +const sdkState = vi.hoisted(() => ({ |
| 6 | + stall: null as "bot" | "graph" | null, |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("@microsoft/teams.apps", () => ({ |
| 10 | + App: class { |
| 11 | + tokenManager = { |
| 12 | + async getBotToken() { |
| 13 | + if (sdkState.stall === "bot") { |
| 14 | + return await new Promise<never>(() => {}); |
| 15 | + } |
| 16 | + return { toString: () => "test-token" }; |
| 17 | + }, |
| 18 | + async getGraphToken() { |
| 19 | + if (sdkState.stall === "graph") { |
| 20 | + return await new Promise<never>(() => {}); |
| 21 | + } |
| 22 | + return { toString: () => "test-token" }; |
| 23 | + }, |
| 24 | + }; |
| 25 | + }, |
| 26 | + ExpressAdapter: vi.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock("@microsoft/teams.api", () => ({ |
| 30 | + Client: function Client() {}, |
| 31 | + cloudFromName: () => ({ |
| 32 | + botScope: "https://api.botframework.com/.default", |
| 33 | + graphScope: "https://graph.microsoft.com/.default", |
| 34 | + }), |
| 35 | +})); |
| 36 | + |
| 37 | +import { probeMSTeams } from "./probe.js"; |
| 38 | +import { MSTEAMS_REQUEST_TIMEOUT_MS } from "./request-timeout.js"; |
| 39 | + |
| 40 | +const cfg = { |
| 41 | + enabled: true, |
| 42 | + appId: "app-id", |
| 43 | + appPassword: "test-app-password", |
| 44 | + tenantId: "tenant-id", |
| 45 | +} as unknown as MSTeamsConfig; |
| 46 | + |
| 47 | +describe("probeMSTeams request deadline", () => { |
| 48 | + beforeEach(() => { |
| 49 | + sdkState.stall = null; |
| 50 | + vi.stubEnv("MSTEAMS_APP_ID", ""); |
| 51 | + vi.stubEnv("MSTEAMS_APP_PASSWORD", ""); |
| 52 | + vi.stubEnv("MSTEAMS_TENANT_ID", ""); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + vi.unstubAllEnvs(); |
| 57 | + vi.useRealTimers(); |
| 58 | + }); |
| 59 | + |
| 60 | + it.each([ |
| 61 | + { |
| 62 | + stalled: "bot" as const, |
| 63 | + expected: { |
| 64 | + ok: false, |
| 65 | + appId: "app-id", |
| 66 | + error: `MS Teams Bot Framework probe token timed out after ${MSTEAMS_REQUEST_TIMEOUT_MS}ms`, |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + stalled: "graph" as const, |
| 71 | + expected: { |
| 72 | + ok: true, |
| 73 | + appId: "app-id", |
| 74 | + graph: { |
| 75 | + ok: false, |
| 76 | + error: `MS Teams Graph probe token timed out after ${MSTEAMS_REQUEST_TIMEOUT_MS}ms`, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + ])("bounds stalled $stalled token acquisition", async ({ stalled, expected }) => { |
| 81 | + sdkState.stall = stalled; |
| 82 | + vi.useFakeTimers(); |
| 83 | + |
| 84 | + const result = expect(probeMSTeams(cfg)).resolves.toEqual(expected); |
| 85 | + await vi.advanceTimersByTimeAsync(0); |
| 86 | + await vi.advanceTimersByTimeAsync(MSTEAMS_REQUEST_TIMEOUT_MS); |
| 87 | + |
| 88 | + await result; |
| 89 | + }); |
| 90 | +}); |
0 commit comments