





























@@ -1,25 +1,30 @@
11import { afterEach, describe, expect, it, vi } from "vitest";
22import { TeamsHttpStream } from "./streaming-message.js";
334+async function flushStreamTimer(): Promise<void> {
5+await vi.advanceTimersByTimeAsync(1);
6+}
7+48describe("TeamsHttpStream", () => {
59afterEach(() => {
610vi.useRealTimers();
711});
812913it("sends first chunk as typing activity with streaminfo", async () => {
14+vi.useFakeTimers();
15+1016const sent: unknown[] = [];
1117const stream = new TeamsHttpStream({
1218sendActivity: vi.fn(async (activity) => {
1319sent.push(activity);
1420return { id: "stream-1" };
1521}),
22+throttleMs: 1,
1623});
17241825// Enough text to pass MIN_INITIAL_CHARS threshold
1926stream.update("Hello, this is a test response that is long enough.");
20-21-// Wait for throttle to flush
22-await new Promise((r) => setTimeout(r, 700));
27+await flushStreamTimer();
23282429expect(sent.length).toBeGreaterThanOrEqual(1);
2530const firstActivity = sent[0] as Record<string, unknown>;
@@ -36,16 +41,19 @@ describe("TeamsHttpStream", () => {
3641});
37423843it("sends final message activity on finalize", async () => {
44+vi.useFakeTimers();
45+3946const sent: unknown[] = [];
4047const stream = new TeamsHttpStream({
4148sendActivity: vi.fn(async (activity) => {
4249sent.push(activity);
4350return { id: "stream-1" };
4451}),
52+throttleMs: 1,
4553});
46544755stream.update("Hello, this is a complete response for finalization testing.");
48-await new Promise((r) => setTimeout(r, 700));
56+await flushStreamTimer();
49575058await stream.finalize();
5159@@ -76,11 +84,13 @@ describe("TeamsHttpStream", () => {
7684});
77857886it("does not send below MIN_INITIAL_CHARS", async () => {
87+vi.useFakeTimers();
88+7989const sendActivity = vi.fn(async () => ({ id: "x" }));
80-const stream = new TeamsHttpStream({ sendActivity });
90+const stream = new TeamsHttpStream({ sendActivity, throttleMs: 1 });
81918292stream.update("Hi");
83-await new Promise((r) => setTimeout(r, 700));
93+await flushStreamTimer();
84948595expect(sendActivity).not.toHaveBeenCalled();
8696});
@@ -114,17 +124,20 @@ describe("TeamsHttpStream", () => {
114124});
115125116126it("sets feedbackLoopEnabled on final message", async () => {
127+vi.useFakeTimers();
128+117129const sent: unknown[] = [];
118130const stream = new TeamsHttpStream({
119131sendActivity: vi.fn(async (activity) => {
120132sent.push(activity);
121133return { id: "stream-1" };
122134}),
123135feedbackLoopEnabled: true,
136+throttleMs: 1,
124137});
125138126139stream.update("A response long enough to pass the minimum character threshold for streaming.");
127-await new Promise((r) => setTimeout(r, 700));
140+await flushStreamTimer();
128141await stream.finalize();
129142130143const finalActivity = sent.find(
@@ -163,17 +176,20 @@ describe("TeamsHttpStream", () => {
163176});
164177165178it("informative update establishes streamId for subsequent chunks", async () => {
179+vi.useFakeTimers();
180+166181const sent: unknown[] = [];
167182const stream = new TeamsHttpStream({
168183sendActivity: vi.fn(async (activity) => {
169184sent.push(activity);
170185return { id: "stream-1" };
171186}),
187+throttleMs: 1,
172188});
173189174190await stream.sendInformativeUpdate("Working...");
175191stream.update("Hello, this is a long enough response for streaming to begin.");
176-await new Promise((r) => setTimeout(r, 1600));
192+await flushStreamTimer();
177193178194// Second activity (streaming chunk) should have the streamId from the informative update
179195expect(sent.length).toBeGreaterThanOrEqual(2);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。