diff --git a/extensions/microsoft-foundry/onboard.connection.test.ts b/extensions/microsoft-foundry/onboard.connection.test.ts index 836747d74d8..3b9787183c4 100644 --- a/extensions/microsoft-foundry/onboard.connection.test.ts +++ b/extensions/microsoft-foundry/onboard.connection.test.ts @@ -70,4 +70,40 @@ describe("testFoundryConnection", () => { "Connection Test", ); }); + + it.each([ + { + status: 400, + expectedPrefix: + "Endpoint is reachable but returned 400 Bad Request - check your deployment name and API version.\n", + expectedSuffix: "", + }, + { + status: 503, + expectedPrefix: "Warning: test request returned 503. ", + expectedSuffix: "\nProceeding anyway - you can fix the endpoint later.", + }, + ])( + "keeps $status error-body previews UTF-16 safe", + async ({ status, expectedPrefix, expectedSuffix }) => { + const note = vi.fn(); + const prefix = "x".repeat(199); + hoisted.fetchWithSsrFGuard.mockResolvedValue({ + response: new Response(`${prefix}😀tail`, { status }), + release: async () => {}, + }); + + await testFoundryConnection({ + ctx: { prompter: { note } } as never, + endpoint: "https://example.openai.azure.com", + modelId: "gpt-4o", + api: DEFAULT_API, + }); + + expect(note).toHaveBeenCalledExactlyOnceWith( + `${expectedPrefix}${prefix}${expectedSuffix}`, + "Connection Test", + ); + }, + ); }); diff --git a/extensions/microsoft-foundry/onboard.ts b/extensions/microsoft-foundry/onboard.ts index 6f6e8fa1334..d11d4103061 100644 --- a/extensions/microsoft-foundry/onboard.ts +++ b/extensions/microsoft-foundry/onboard.ts @@ -7,6 +7,7 @@ import { normalizeOptionalString, normalizeStringifiedOptionalString, } from "openclaw/plugin-sdk/string-coerce-runtime"; +import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime"; import { azLoginDeviceCode, azLoginDeviceCodeWithOptions, @@ -613,7 +614,7 @@ export async function testFoundryConnection(params: { FOUNDRY_CONNECTION_TEST_ERROR_BODY_LIMIT_BYTES, ).catch(() => ""); await params.ctx.prompter.note( - `Endpoint is reachable but returned 400 Bad Request - check your deployment name and API version.\n${body.slice(0, 200)}`, + `Endpoint is reachable but returned 400 Bad Request - check your deployment name and API version.\n${truncateUtf16Safe(body, 200)}`, "Connection Test", ); } else if (!res.ok) { @@ -622,7 +623,7 @@ export async function testFoundryConnection(params: { FOUNDRY_CONNECTION_TEST_ERROR_BODY_LIMIT_BYTES, ).catch(() => ""); await params.ctx.prompter.note( - `Warning: test request returned ${res.status}. ${body.slice(0, 200)}\nProceeding anyway - you can fix the endpoint later.`, + `Warning: test request returned ${res.status}. ${truncateUtf16Safe(body, 200)}\nProceeding anyway - you can fix the endpoint later.`, "Connection Test", ); } else {