
























@@ -53,7 +53,33 @@ function registerProvider() {
5353}),
5454);
5555expect(registerProviderMock).toHaveBeenCalledTimes(1);
56-return registerProviderMock.mock.calls[0]?.[0];
56+const firstCall = registerProviderMock.mock.calls[0];
57+expect(firstCall).toBeDefined();
58+if (!firstCall) {
59+throw new Error("expected Microsoft Foundry provider registration");
60+}
61+return firstCall[0];
62+}
63+64+type FoundryProvider = ReturnType<typeof registerProvider>;
65+66+function requirePrepareRuntimeAuth(
67+provider: FoundryProvider,
68+): NonNullable<FoundryProvider["prepareRuntimeAuth"]> {
69+const prepareRuntimeAuth = provider.prepareRuntimeAuth;
70+expect(prepareRuntimeAuth).toBeTypeOf("function");
71+if (!prepareRuntimeAuth) {
72+throw new Error("expected Microsoft Foundry runtime auth hook");
73+}
74+return prepareRuntimeAuth;
75+}
76+77+function requireRuntimeAuthResult(result: { apiKey?: string; baseUrl?: string } | undefined) {
78+expect(result).toBeDefined();
79+if (!result) {
80+throw new Error("expected Microsoft Foundry runtime auth result");
81+}
82+return result;
5783}
58845985const defaultFoundryBaseUrl = "https://example.services.ai.azure.com/openai/v1";
@@ -276,71 +302,75 @@ describe("microsoft-foundry plugin", () => {
276302277303it("preserves the model-derived base URL for Entra runtime auth refresh", async () => {
278304const provider = registerProvider();
305+const prepareRuntimeAuth = requirePrepareRuntimeAuth(provider);
279306mockAzureCliToken({ accessToken: "test-token", expiresInMs: 60_000 });
280307ensureAuthProfileStoreMock.mockReturnValueOnce(buildEntraProfileStore());
281308282-const prepared = await provider.prepareRuntimeAuth?.(buildFoundryRuntimeAuthContext());
309+const prepared = requireRuntimeAuthResult(
310+await prepareRuntimeAuth(buildFoundryRuntimeAuthContext()),
311+);
283312284-expect(prepared?.baseUrl).toBe("https://example.services.ai.azure.com/openai/v1");
313+expect(prepared.baseUrl).toBe("https://example.services.ai.azure.com/openai/v1");
285314});
286315287316it("retries Entra token refresh after a failed attempt", async () => {
288317const provider = registerProvider();
318+const prepareRuntimeAuth = requirePrepareRuntimeAuth(provider);
289319mockAzureCliLoginFailure();
290320mockAzureCliToken({ accessToken: "retry-token", expiresInMs: 10 * 60_000 });
291321ensureAuthProfileStoreMock.mockReturnValue(buildEntraProfileStore());
292322293323const runtimeContext = buildFoundryRuntimeAuthContext();
294324295-await expect(provider.prepareRuntimeAuth?.(runtimeContext)).rejects.toThrow(
296-"Azure CLI is not logged in",
297-);
325+await expect(prepareRuntimeAuth(runtimeContext)).rejects.toThrow("Azure CLI is not logged in");
298326299-await expect(provider.prepareRuntimeAuth?.(runtimeContext)).resolves.toMatchObject({
327+await expect(prepareRuntimeAuth(runtimeContext)).resolves.toMatchObject({
300328apiKey: "retry-token",
301329});
302330expect(execFileMock).toHaveBeenCalledTimes(2);
303331});
304332305333it("dedupes concurrent Entra token refreshes for the same profile", async () => {
306334const provider = registerProvider();
335+const prepareRuntimeAuth = requirePrepareRuntimeAuth(provider);
307336mockAzureCliToken({ accessToken: "deduped-token", expiresInMs: 60_000, delayMs: 10 });
308337ensureAuthProfileStoreMock.mockReturnValue(buildEntraProfileStore());
309338310339const runtimeContext = buildFoundryRuntimeAuthContext();
311340312341const [first, second] = await Promise.all([
313-provider.prepareRuntimeAuth?.(runtimeContext),
314-provider.prepareRuntimeAuth?.(runtimeContext),
342+prepareRuntimeAuth(runtimeContext),
343+prepareRuntimeAuth(runtimeContext),
315344]);
316345317346expect(execFileMock).toHaveBeenCalledTimes(1);
318-expect(first?.apiKey).toBe("deduped-token");
319-expect(second?.apiKey).toBe("deduped-token");
347+expect(requireRuntimeAuthResult(first).apiKey).toBe("deduped-token");
348+expect(requireRuntimeAuthResult(second).apiKey).toBe("deduped-token");
320349});
321350322351it("clears failed refresh state so later concurrent retries succeed", async () => {
323352const provider = registerProvider();
353+const prepareRuntimeAuth = requirePrepareRuntimeAuth(provider);
324354mockAzureCliLoginFailure(10);
325355mockAzureCliToken({ accessToken: "recovered-token", expiresInMs: 10 * 60_000, delayMs: 10 });
326356ensureAuthProfileStoreMock.mockReturnValue(buildEntraProfileStore());
327357328358const runtimeContext = buildFoundryRuntimeAuthContext();
329359330360const failed = await Promise.allSettled([
331-provider.prepareRuntimeAuth?.(runtimeContext),
332-provider.prepareRuntimeAuth?.(runtimeContext),
361+prepareRuntimeAuth(runtimeContext),
362+prepareRuntimeAuth(runtimeContext),
333363]);
334364expect(failed.filter((result) => result.status !== "rejected")).toEqual([]);
335365expect(execFileMock).toHaveBeenCalledTimes(1);
336366337367const [first, second] = await Promise.all([
338-provider.prepareRuntimeAuth?.(runtimeContext),
339-provider.prepareRuntimeAuth?.(runtimeContext),
368+prepareRuntimeAuth(runtimeContext),
369+prepareRuntimeAuth(runtimeContext),
340370]);
341371expect(execFileMock).toHaveBeenCalledTimes(2);
342-expect(first?.apiKey).toBe("recovered-token");
343-expect(second?.apiKey).toBe("recovered-token");
372+expect(requireRuntimeAuthResult(first).apiKey).toBe("recovered-token");
373+expect(requireRuntimeAuthResult(second).apiKey).toBe("recovered-token");
344374});
345375346376it("refreshes again when a cached token is too close to expiry", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。