|
| 1 | +// Microsoft Foundry tests cover bounded connection-test error reads. |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import * as cli from "./cli.js"; |
| 4 | +import { testFoundryConnection } from "./onboard.js"; |
| 5 | +import { DEFAULT_API } from "./shared.js"; |
| 6 | + |
| 7 | +const hoisted = vi.hoisted(() => ({ |
| 8 | + fetchWithSsrFGuard: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({ |
| 12 | + fetchWithSsrFGuard: hoisted.fetchWithSsrFGuard, |
| 13 | +})); |
| 14 | + |
| 15 | +function cancelTrackedResponse( |
| 16 | + text: string, |
| 17 | + init: ResponseInit, |
| 18 | +): { |
| 19 | + response: Response; |
| 20 | + wasCanceled: () => boolean; |
| 21 | +} { |
| 22 | + let canceled = false; |
| 23 | + const stream = new ReadableStream<Uint8Array>({ |
| 24 | + start(controller) { |
| 25 | + controller.enqueue(new TextEncoder().encode(text)); |
| 26 | + }, |
| 27 | + cancel() { |
| 28 | + canceled = true; |
| 29 | + }, |
| 30 | + }); |
| 31 | + return { |
| 32 | + response: new Response(stream, init), |
| 33 | + wasCanceled: () => canceled, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe("testFoundryConnection", () => { |
| 38 | + beforeEach(() => { |
| 39 | + vi.spyOn(cli, "getAccessTokenResult").mockReturnValue({ accessToken: "token" }); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + vi.restoreAllMocks(); |
| 44 | + hoisted.fetchWithSsrFGuard.mockReset(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("bounds connection-test error bodies without using response.text()", async () => { |
| 48 | + const note = vi.fn(); |
| 49 | + const tracked = cancelTrackedResponse(`${"foundry failure ".repeat(1024)}tail`, { |
| 50 | + status: 503, |
| 51 | + headers: { "content-type": "text/plain" }, |
| 52 | + }); |
| 53 | + const textSpy = vi.spyOn(tracked.response, "text").mockRejectedValue(new Error("unbounded")); |
| 54 | + hoisted.fetchWithSsrFGuard.mockResolvedValue({ |
| 55 | + response: tracked.response, |
| 56 | + release: async () => {}, |
| 57 | + }); |
| 58 | + |
| 59 | + await testFoundryConnection({ |
| 60 | + ctx: { prompter: { note } } as never, |
| 61 | + endpoint: "https://example.openai.azure.com", |
| 62 | + modelId: "gpt-4o", |
| 63 | + api: DEFAULT_API, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(textSpy).not.toHaveBeenCalled(); |
| 67 | + expect(tracked.wasCanceled()).toBe(true); |
| 68 | + expect(note).toHaveBeenCalledWith( |
| 69 | + expect.stringContaining("Warning: test request returned 503"), |
| 70 | + "Connection Test", |
| 71 | + ); |
| 72 | + }); |
| 73 | +}); |
0 commit comments