Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
test(transport): stop four files replacing the credential authority for the whole run
Jack Rudenkoc035b183a271
diff --git a/packages/cli/src/providers/transport/anthropic-compat-kimi-oauth.test.ts b/packages/cli/src/providers/transport/anthropic-compat-kimi-oauth.test.tsindex 7c0f9331..f41c04ac 100644--- a/packages/cli/src/providers/transport/anthropic-compat-kimi-oauth.test.ts+++ b/packages/cli/src/providers/transport/anthropic-compat-kimi-oauth.test.ts@@ -16,12 +16,19 @@* It also pins that NON-OAuth providers (MiniMax / Z.AI) are unaffected — they keep* their plain x-api-key / Bearer + anthropic-version + provider.headers path.*- * Hermetic strategy: we mock the credential authority's getRequestAuth (the single- * dispatch point the transport now calls) so no real OAuth file / SDK is touched.+ * Hermetic strategy: fake credentials registered on the REAL authority (the single+ * dispatch point the transport now calls) delegate to `getRequestAuthMock`, so no+ * real OAuth file / SDK is touched. The real credentials are re-registered in+ * afterAll. Never `mock.module()` the authority: Bun keeps that replacement for+ * the rest of the process, and every later file that imports `credentials` got a+ * fake with no `register`.*/-import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, mock, test } from "bun:test";+import { credentials } from "../../auth/credentials/authority.js";+import type { CredentialProvider } from "../../auth/credentials/types.js";import type { RemoteProvider } from "../../handlers/shared/remote-provider-types.js";+import { AnthropicProviderTransport } from "./anthropic-compat.js";const FAKE_TOKEN = "kimi-oauth-token-xyz";@@ -46,16 +53,47 @@ const KIMI_OAUTH_AUTH = {},};-// ── Mock the authority's getRequestAuth (the new delegation target) ───────────+// ── Fake the authority's credentials (the new delegation target) ──────────────let getRequestAuthMock = mock(async (_name: string, _ctx: any) => KIMI_OAUTH_AUTH as any);-mock.module("../../auth/credentials/authority.js", () => ({- credentials: {- getRequestAuth: (name: string, ctx: any) => getRequestAuthMock(name, ctx),- },-}));+// Every name this file's transports could sign under. "kimi-coding" is the+// delegation under test. "minimax" and "z-ai" are faked too so the "no authority+// call" assertions can still fail: without a fake, a call under either name+// would reach the real api-key provider and never touch the mock.+const FAKED_NAMES = ["kimi-coding", "minimax", "z-ai"] as const;++/** Answers under `name` only, and passes that name on (the authority consumes it). */+function fakeCredential(name: string): CredentialProvider {+ return {+ catalogName: name,+ isAvailable: async () => true,+ describeReadiness: async () => ({ readiness: "present" }),+ getRequestAuth: (ctx) => getRequestAuthMock(name, ctx),+ };+}++const realCredentials = new Map<string, CredentialProvider>();++beforeAll(() => {+ for (const name of FAKED_NAMES) {+ const real = credentials.get(name);+ if (!real) {+ throw new Error(`the authority has no ${name} credential to restore after this file`);+ }+ realCredentials.set(name, real);+ credentials.register(fakeCredential(name), [name]);+ credentials.invalidate(name);+ }+});-const { AnthropicProviderTransport } = await import("./anthropic-compat.js");+afterAll(() => {+ // Put the real credentials back so no later file in the Bun run signs with a fake.+ for (const [name, real] of realCredentials) {+ credentials.register(real, [name]);+ credentials.invalidate(name);+ }+ realCredentials.clear();+});beforeEach(() => {getRequestAuthMock = mock(async (_name: string, _ctx: any) => KIMI_OAUTH_AUTH as any);diff --git a/packages/cli/src/providers/transport/openai-codex-cache-key.test.ts b/packages/cli/src/providers/transport/openai-codex-cache-key.test.tsindex 053075a7..24bd66fe 100644--- a/packages/cli/src/providers/transport/openai-codex-cache-key.test.ts+++ b/packages/cli/src/providers/transport/openai-codex-cache-key.test.ts@@ -6,8 +6,12 @@* Every request also needs a fallback key, while explicit caller keys and the* existing model/auth payload transforms must continue to win where applicable.*- * Hermetic: mock only credentials.getRequestAuth, matching the sibling OAuth- * transport test, so no real credentials, filesystem, or network are touched.+ * Hermetic: a fake `openai-codex` credential is registered on the REAL authority+ * and delegates to `getRequestAuthMock`, matching the sibling OAuth transport+ * test, so no real credentials, filesystem, or network are touched. The real+ * credential is re-registered in afterAll. Never `mock.module()` the authority:+ * Bun keeps that replacement for the rest of the process, and every later file+ * that imports `credentials` got a fake with no `register`.* Both arms are modelled as the artifacts the real halves return (`arm` included);* the api-key half never throws, so nothing here pretends it does. See the sibling* file's header for why that distinction is load-bearing.@@ -16,9 +20,12 @@* the afterEach clears it. See the sibling file's afterEach.*/-import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, mock, test } from "bun:test";+import { credentials } from "../../auth/credentials/authority.js";import { clearSignedArm } from "../../auth/credentials/billing-probe.js";+import type { CredentialProvider } from "../../auth/credentials/types.js";import type { RemoteProvider } from "../../handlers/shared/remote-provider-types.js";+import { OpenAICodexTransport } from "./openai-codex.js";const FAKE_TOKEN = "codex-oauth-token-abc";const FAKE_ACCOUNT = "acct-123";@@ -53,13 +60,31 @@ const API_KEY_AUTH = {let getRequestAuthMock = mock(async (_name: string, _ctx: any) => CODEX_OAUTH_AUTH as any);-mock.module("../../auth/credentials/authority.js", () => ({- credentials: {- getRequestAuth: (name: string, ctx: any) => getRequestAuthMock(name, ctx),- },-}));+// Registered under "openai-codex" ONLY; the authority consumes the name in its+// registry lookup, so the fake passes it on. See the sibling file.+const fakeCodexCredential: CredentialProvider = {+ catalogName: "openai-codex",+ isAvailable: async () => true,+ describeReadiness: async () => ({ readiness: "present" }),+ getRequestAuth: (ctx) => getRequestAuthMock("openai-codex", ctx),+};++let realCodexCredential: CredentialProvider | undefined;-const { OpenAICodexTransport } = await import("./openai-codex.js");+beforeAll(() => {+ realCodexCredential = credentials.get("openai-codex");+ if (!realCodexCredential) {+ throw new Error("the authority has no openai-codex credential to restore after this file");+ }+ credentials.register(fakeCodexCredential, ["openai-codex"]);+ credentials.invalidate("openai-codex");+});++afterAll(() => {+ // Put the real credential back so no later file in the Bun run signs with the fake.+ if (realCodexCredential) credentials.register(realCodexCredential, ["openai-codex"]);+ credentials.invalidate("openai-codex");+});const provider: RemoteProvider = {name: "openai-codex",diff --git a/packages/cli/src/providers/transport/openai-codex-oauth.test.ts b/packages/cli/src/providers/transport/openai-codex-oauth.test.tsindex d8fa117b..6316e854 100644--- a/packages/cli/src/providers/transport/openai-codex-oauth.test.ts+++ b/packages/cli/src/providers/transport/openai-codex-oauth.test.ts@@ -16,8 +16,13 @@* OAuth present but its refresh REJECTED → the same api-key-shaped result, by a* different mechanism (cachedAuth null → super.getHeaders()).*- * Hermetic: mock credentials.getRequestAuth (the delegation target), with each- * fixture shaped like what the REAL half returns, `arm` included.+ * Hermetic: a fake `openai-codex` credential is registered on the REAL authority+ * and delegates to `getRequestAuthMock`, with each fixture shaped like what the+ * REAL half returns, `arm` included. The real credential is saved with+ * `credentials.get` and re-registered in afterAll. Never `mock.module()` the+ * authority: Bun keeps that replacement for the rest of the process, so every+ * later file that imports `credentials` got a fake with no `register` (measured:+ * 9 failures in openai-codex-refresh-outage.test.ts).** "No OAuth" is NOT a throw. `CompositeCredentialProvider.getRequestAuth` falls* through to `this.fallback.getRequestAuth(ctx)`, and `ApiKeyCredentialProvider`@@ -35,9 +40,12 @@* fixture — see the afterEach.*/-import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, mock, test } from "bun:test";+import { credentials } from "../../auth/credentials/authority.js";import { clearSignedArm } from "../../auth/credentials/billing-probe.js";+import type { CredentialProvider } from "../../auth/credentials/types.js";import type { RemoteProvider } from "../../handlers/shared/remote-provider-types.js";+import { OpenAICodexTransport } from "./openai-codex.js";const FAKE_TOKEN = "codex-oauth-token-abc";const FAKE_ACCOUNT = "acct-123";@@ -77,13 +85,33 @@ const API_KEY_AUTH = {let getRequestAuthMock = mock(async (_name: string, _ctx: any) => CODEX_OAUTH_AUTH as any);-mock.module("../../auth/credentials/authority.js", () => ({- credentials: {- getRequestAuth: (name: string, ctx: any) => getRequestAuthMock(name, ctx),- },-}));+// Registered under "openai-codex" ONLY. The authority consumes the name in its+// registry lookup, so the fake passes that name on itself; the mock is reached+// only when the transport asked for "openai-codex", which is what the+// "delegates with the openai-codex catalog name" assertions pin together.+const fakeCodexCredential: CredentialProvider = {+ catalogName: "openai-codex",+ isAvailable: async () => true,+ describeReadiness: async () => ({ readiness: "present" }),+ getRequestAuth: (ctx) => getRequestAuthMock("openai-codex", ctx),+};++let realCodexCredential: CredentialProvider | undefined;-const { OpenAICodexTransport } = await import("./openai-codex.js");+beforeAll(() => {+ realCodexCredential = credentials.get("openai-codex");+ if (!realCodexCredential) {+ throw new Error("the authority has no openai-codex credential to restore after this file");+ }+ credentials.register(fakeCodexCredential, ["openai-codex"]);+ credentials.invalidate("openai-codex");+});++afterAll(() => {+ // Put the real credential back so no later file in the Bun run signs with the fake.+ if (realCodexCredential) credentials.register(realCodexCredential, ["openai-codex"]);+ credentials.invalidate("openai-codex");+});const provider: RemoteProvider = {name: "openai-codex",diff --git a/packages/cli/src/providers/transport/vertex-oauth.test.ts b/packages/cli/src/providers/transport/vertex-oauth.test.tsindex 1f8faf83..451f708f 100644--- a/packages/cli/src/providers/transport/vertex-oauth.test.ts+++ b/packages/cli/src/providers/transport/vertex-oauth.test.ts@@ -16,11 +16,17 @@* re-delegates → getHeaders() returns the refreshed token* - getEndpoint() / transformPayload() / getRequestInit() are unchanged*- * Hermetic: mock credentials.getRequestAuth (delegation target) and the vertex-auth- * manager (so refreshToken is observable and no gcloud/ADC is touched).+ * Hermetic: a fake `vertex` credential registered on the REAL authority delegates+ * to `getRequestAuthMock` (the real one is re-registered in afterAll), and the+ * vertex-auth manager is mocked (so refreshToken is observable and no gcloud/ADC+ * is touched). Never `mock.module()` the authority: Bun keeps that replacement+ * for the rest of the process, and every later file that imports `credentials`+ * got a fake with no `register`.*/-import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, mock, test } from "bun:test";+import { credentials } from "../../auth/credentials/authority.js";+import type { CredentialProvider } from "../../auth/credentials/types.js";import type { VertexConfig } from "../../auth/vertex-auth.js";import { classifyConnectionError } from "../../handlers/shared/connection-error.js";@@ -39,11 +45,32 @@ let getRequestAuthMock = mock(async (_name: string, _ctx: any) => ({headers: { Authorization: `Bearer ${currentToken}` },}));-mock.module("../../auth/credentials/authority.js", () => ({- credentials: {- getRequestAuth: (name: string, ctx: any) => getRequestAuthMock(name, ctx),- },-}));+// Registered under "vertex" ONLY. The authority consumes the name in its registry+// lookup, so the fake passes that name on itself; the mock is reached only when+// the transport asked for "vertex", which the delegation assertions pin together.+const fakeVertexCredential: CredentialProvider = {+ catalogName: "vertex",+ isAvailable: async () => true,+ describeReadiness: async () => ({ readiness: "present" }),+ getRequestAuth: (ctx) => getRequestAuthMock("vertex", ctx),+};++let realVertexCredential: CredentialProvider | undefined;++beforeAll(() => {+ realVertexCredential = credentials.get("vertex");+ if (!realVertexCredential) {+ throw new Error("the authority has no vertex credential to restore after this file");+ }+ credentials.register(fakeVertexCredential, ["vertex"]);+ credentials.invalidate("vertex");+});++afterAll(() => {+ // Put the real credential back so no later file in the Bun run signs with the fake.+ if (realVertexCredential) credentials.register(realVertexCredential, ["vertex"]);+ credentials.invalidate("vertex");+});mock.module("../../auth/vertex-auth.js", () => ({getVertexAuthManager: () => ({