Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test(auth): expect a rejected subscription refresh to stop, not sign metered

Jack Rudenko404edac41769
diff --git a/packages/cli/src/auth/credentials/billing-probe.test.ts b/packages/cli/src/auth/credentials/billing-probe.test.ts
index e4bf5cfb..008bf442 100644
--- a/packages/cli/src/auth/credentials/billing-probe.test.ts
+++ b/packages/cli/src/auth/credentials/billing-probe.test.ts
@@ -1,31 +1,7 @@
-/**
- * FR-3 — `openai-codex` bills by the credential that SIGNS, not by its name.
- *
- * The one direction that costs the user real money is reporting SUB (and $0
- * accrued) while OpenAI meters the request. Every test here exists to make that
- * direction fail loudly.
- *
- * ── Hermetic strategy ─────────────────────────────────────────────────────────
- * No `mock.module`: Bun's module registry bleeds across files, and another file
- * may construct the real CodexOAuth singleton first (equivalence.test.ts:27-28
- * records why). No credential fixture under `$HOME`: guard-real-config.ts:31
- * sandboxes only config.json. Instead we use the seam equivalence.test.ts:136-152
- * established — override the real singleton's `hasCredentials` IN PLACE (the
- * authority's CodexOAuthHalf holds a reference to that same instance) — and, for
- * the trap case, drive the singleton's in-memory credential object so the REAL
- * predicate runs against it.
- *
- * Importing `authority.js` does read ~/.claudish/codex-oauth.json once, in the
- * CodexOAuth constructor. That read is pre-existing (authority.ts:235 → :157 →
- * codex-credential.ts:43) and no assertion below depends on its result: every test
- * sets the credential state it needs. The import is deliberate — it is the ONLY
- * production registration site for the probe, so it is what the wiring test tests.
- *
- * Save/restore is per test, and both the probe and the signed-arm record are
- * run-scoped module state that would otherwise bleed into sibling files.
- */
-
-import { afterEach, beforeEach, describe, expect, test } from "bun:test";
+import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test";
+import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
import {
CREDENTIAL_DECIDED_PROVIDERS,
SUBSCRIPTION_PROVIDERS,
@@ -33,14 +9,274 @@ import {
isSubscriptionProvider,
registerSubscriptionCredentialProbe,
} from "../../handlers/shared/remote-provider-types.js";
-import { CodexOAuth } from "../codex-oauth.js";
+
+// HOME must exist in the environment at process start for Bun's os.homedir().
+// Keep the child and all of its writable resources under a per-case temp home.
+const childRoots: string[] = [];
+const children: ReturnType<typeof Bun.spawn>[] = [];
+afterEach(async () => {
+ for (const child of children.splice(0)) {
+ if (child.exitCode === null) child.kill();
+ await child.exited;
+ }
+ for (const root of childRoots.splice(0)) rmSync(root, { recursive: true, force: true });
+});
+async function runHomeScenario(scenario: string) {
+ const root = mkdtempSync(join(tmpdir(), "claudish-run4-child-"));
+ childRoots.push(root);
+ const home = join(root, "home");
+ mkdirSync(home);
+ const scriptPath = join(home, "scenario.ts");
+ const srcDir = resolve(import.meta.dir, "../..");
+ const bootstrap = `
+ import fs from "node:fs";
+ import { homedir } from "node:os";
+ import { resolve, isAbsolute } from "node:path";
+ import { fileURLToPath, pathToFileURL } from "node:url";
+ import { syncBuiltinESMExports } from "node:module";
+ const realExit = process.exit;
+ try {
+ const home = process.env.HOME;
+ if (homedir() !== home) throw new Error("child HOME was not honoured");
+ const src = ${JSON.stringify(srcDir)};
+ const load = (name) => import(pathToFileURL(resolve(src, name)).href);
+ const writes = [];
+ const outsideWrites = [];
+ const checkWrite = (value) => {
+ const path = resolve(value instanceof URL ? fileURLToPath(value) : String(value));
+ if (path !== home && !path.startsWith(home + "/")) {
+ outsideWrites.push(path);
+ throw new Error("write outside child HOME: " + path);
+ }
+ writes.push(path);
+ };
+ // Audit before importing production modules, including cached named fs exports.
+ for (const name of ["writeFileSync", "appendFileSync", "mkdirSync", "rmSync", "unlinkSync", "chmodSync", "truncateSync", "createWriteStream"]) {
+ const original = fs[name];
+ fs[name] = function(path, ...args) { checkWrite(path); return original.call(fs, path, ...args); };
+ }
+ for (const name of ["writeFile", "appendFile", "mkdir", "rm", "unlink", "chmod", "truncate"]) {
+ const original = fs.promises[name];
+ fs.promises[name] = async function(path, ...args) { checkWrite(path); return original.call(fs.promises, path, ...args); };
+ }
+ for (const name of ["renameSync", "copyFileSync"]) {
+ const original = fs[name];
+ fs[name] = function(from, to, ...args) {
+ checkWrite(from); checkWrite(to); return original.call(fs, from, to, ...args);
+ };
+ }
+ for (const name of ["writeFile", "appendFile", "mkdir", "rm", "unlink", "chmod", "truncate"]) {
+ const original = fs[name];
+ fs[name] = function(path, ...args) { checkWrite(path); return original.call(fs, path, ...args); };
+ }
+ for (const name of ["rename", "copyFile"]) {
+ const original = fs[name];
+ fs[name] = function(from, to, ...args) {
+ checkWrite(from); checkWrite(to); return original.call(fs, from, to, ...args);
+ };
+ const promiseOriginal = fs.promises[name];
+ fs.promises[name] = async function(from, to, ...args) {
+ checkWrite(from); checkWrite(to); return promiseOriginal.call(fs.promises, from, to, ...args);
+ };
+ }
+ const writesOnOpen = (flags) => typeof flags === "number"
+ ? (flags & (1 | 2 | 64 | 512 | 1024)) !== 0 : /[wa+]/.test(flags ?? "r");
+ for (const name of ["open", "openSync"]) {
+ const original = fs[name];
+ fs[name] = function(path, flags, ...args) {
+ if (writesOnOpen(flags)) checkWrite(path);
+ return original.call(fs, path, flags, ...args);
+ };
+ }
+ const promiseOpen = fs.promises.open;
+ fs.promises.open = async function(path, flags, ...args) {
+ if (writesOnOpen(flags)) checkWrite(path);
+ return promiseOpen.call(fs.promises, path, flags, ...args);
+ };
+ syncBuiltinESMExports();
+ const bunWrite = Bun.write;
+ Bun.write = function(path, ...args) { checkWrite(path); return bunWrite.call(Bun, path, ...args); };
+ let networkRequests = 0;
+ const networkUrls = [];
+ const realFetch = globalThis.fetch;
+ globalThis.fetch = Object.assign(async (input, init) => {
+ const url = new URL(input instanceof Request ? input.url : String(input));
+ if (url.protocol !== "http:" && url.protocol !== "https:") return realFetch(input, init);
+ networkRequests++;
+ networkUrls.push(url.href);
+ throw new Error("unexpected child network request");
+ }, { preconnect: () => {} });
+ let keychainOperations = 0;
+ const keychainAbsent = () => { keychainOperations++; return { code: 44, stdout: "", stderr: "test absent" }; };
+ const keychain = await load("providers/keychain.js");
+ keychain.setKeychainTestDeps({
+ platform: () => "darwin", run: keychainAbsent, runAsync: async () => keychainAbsent(),
+ spawnAsync: () => ({ done: Promise.resolve(keychainAbsent()), pid: undefined }),
+ });
+ const core = await load("providers/meta-muse/muse-credentials.js");
+ core.setMuseCoreTestDeps({
+ dir: () => home, now: () => 1_800_000_000_000, readStore: () => null,
+ writeBorrowedCopy: () => { throw new Error("unexpected borrowed write"); },
+ mint: async () => { throw new Error("unexpected mint"); },
+ keychainLogin: async () => ({ kind: "not-allowed" }),
+ });
+ const museKeychain = await load("providers/meta-muse/muse-keychain.js");
+ museKeychain.setMuseKeychainTestDeps({
+ dir: () => home, env: () => ({}), now: () => 1_800_000_000_000,
+ sleep: async () => { throw new Error("unexpected keychain wait"); },
+ pid: () => 424242, isAlive: () => false,
+ presence: async () => ({ kind: "absent" }),
+ startRead: () => ({ done: Promise.resolve({ kind: "absent" }), pid: undefined }),
+ });
+ const { OAUTH_PROVIDERS, hasOAuthCredentials, oauthCredentialWouldQualify } = await load("auth/oauth-registry.js");
+ const credentialPath = (name) => {
+ const file = OAUTH_PROVIDERS[name].credentialFile;
+ if (isAbsolute(file) || file.includes("..")) throw new Error("unsafe credential filename");
+ return resolve(home, ".claudish", file);
+ };
+ const { credentials } = await load("auth/credentials/authority.js");
+ const report = (result) => console.log(JSON.stringify({
+ ...result, home, writes, outsideWrites, networkRequests, networkUrls, keychainOperations,
+ }));
+ `;
+ writeFileSync(
+ scriptPath,
+ bootstrap +
+ scenario +
+ "\nrealExit(0);\n} catch (error) { console.error(error); realExit(1); }\n"
+ );
+ const child = Bun.spawn([process.execPath, scriptPath], {
+ cwd: home,
+ env: {
+ ...process.env,
+ HOME: home,
+ TMPDIR: home,
+ TMP: home,
+ TEMP: home,
+ CLAUDISH_DISABLE_KEYCHAIN: "1",
+ CLAUDISH_DISABLE_CATALOG_WARM: "1",
+ },
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ children.push(child);
+ const [stdout, stderr, code] = await Promise.all([
+ new Response(child.stdout).text(),
+ new Response(child.stderr).text(),
+ child.exited,
+ ]);
+ expect({ code, stderr: code === 0 ? "" : stderr }).toEqual({ code: 0, stderr: "" });
+ const lines = stdout.trim().split("\n");
+ expect(lines).toHaveLength(1);
+ const result = JSON.parse(lines[0]!);
+ expect(result.home).toBe(home);
+ expect(result.outsideWrites).toEqual([]);
+ expect(result.writes.every((path: string) => path === home || path.startsWith(home + "/"))).toBe(
+ true
+ );
+ expect(readdirSync(root)).toEqual(["home"]);
+ expect(result.networkRequests).toBe(0);
+ expect(result.networkUrls).toEqual([]);
+ expect(result.keychainOperations).toBe(0);
+ return result;
+}
+
+const savedEnvironment = new Map(["CLAUDISH_DISABLE_KEYCHAIN"].map((k) => [k, process.env[k]]));
+const testHome = mkdtempSync(join(tmpdir(), "claudish-auth-tests-"));
+process.env.CLAUDISH_DISABLE_KEYCHAIN = "1";
+const realFetch = globalThis.fetch;
+const realExit = process.exit;
+const unexpectedExit = new Error("RUN4_UNEXPECTED_PROCESS_EXIT");
+let exitAttempted = false;
+globalThis.fetch = Object.assign(
+ (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => {
+ const url = new URL(input instanceof Request ? input.url : String(input));
+ if (url.protocol === "http:" && url.hostname === "127.0.0.1") return realFetch(input, init);
+ throw new Error(`Unexpected external request: ${url.origin}${url.pathname}`);
+ },
+ { preconnect: () => {} }
+) as typeof fetch;
+const keychain = await import("../../providers/keychain.js");
+const fakeKeychainDeps = {
+ platform: () => "darwin",
+ run: () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ runAsync: async () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ spawnAsync: () => ({
+ done: Promise.resolve({ code: 44, stdout: "", stderr: "test: absent" }),
+ pid: undefined,
+ }),
+};
+keychain.setKeychainTestDeps(fakeKeychainDeps);
+beforeEach(() => {
+ keychain.setKeychainTestDeps(fakeKeychainDeps);
+});
+afterEach(() => {
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+});
+afterAll(() => {
+ globalThis.fetch = realFetch;
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+ for (const [key, value] of savedEnvironment) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ rmSync(testHome, { recursive: true, force: true });
+});
+
+const { CodexOAuth } = await import("../codex-oauth.js");
import { oauthCredentialWouldQualify } from "../oauth-registry.js";
-import { clearSignedArm, recordSignedArm } from "./billing-probe.js";
// The one production registration site. Importing it is what installs the probe.
// The binding is also what lets the api-key-arm test below drive the REAL
// composite instead of a stub — see "FR-3: the REAL composite's api-key arm".
const { credentials } = await import("./authority.js");
+
+const museCore = await import("../../providers/meta-muse/muse-credentials.js");
+const museKeychain = await import("../../providers/meta-muse/muse-keychain.js");
+const promptRole = await import("../../auth/credentials/prompt-role.js");
+const catalogClient = await import("../../providers/catalog-client.js");
+const allModelsCache = await import("../../providers/all-models-cache.js");
+// Every seam is a complete fake: omitted fields would restore machine defaults.
+beforeEach(() => {
+ museCore.setMuseCoreTestDeps({
+ dir: () => testHome,
+ now: () => 1_800_000_000_000,
+ readStore: () => null,
+ writeBorrowedCopy: () => {
+ throw new Error("unexpected borrowed-copy write");
+ },
+ mint: async () => {
+ throw new Error("unexpected Muse mint");
+ },
+ keychainLogin: async () => ({ kind: "not-allowed" }),
+ });
+ museKeychain.setMuseKeychainTestDeps({
+ dir: () => testHome,
+ env: () => ({}),
+ now: () => 1_800_000_000_000,
+ sleep: async () => {
+ throw new Error("unexpected Muse Keychain wait");
+ },
+ pid: () => 424242,
+ isAlive: () => false,
+ presence: async () => ({ kind: "absent" }),
+ startRead: () => ({ done: Promise.resolve({ kind: "absent" as const }), pid: undefined }),
+ });
+});
+afterEach(() => {
+ process.exit = realExit;
+ museCore.setMuseCoreTestDeps(null);
+ museCore.resetMuseCredentialState();
+ museKeychain.setMuseKeychainTestDeps(null);
+ museKeychain.resetMuseKeychainState();
+ promptRole._resetCredentialPromptRoleForTest();
+ catalogClient._resetCatalogClient();
+ allModelsCache._clearAllModelsCacheMemo();
+ keychain.invalidateKeychainCache();
+});
+
// Imported dynamically, and AFTER the authority, only to keep the ordering above
// honest: this is the sole production writer of the signed-arm record.
const { OpenAICodexTransport } = await import("../../providers/transport/openai-codex.js");
@@ -53,10 +289,9 @@ const SYNTHETIC_MODEL = "billing-probe-test-model";
/** An obviously-fake OPENAI_CODEX_API_KEY. Never sent anywhere. */
const METERED_KEY = "sk-codex-metered-fixture";
-type Mutable = { credentials: unknown; hasCredentials?: unknown };
-
+const { clearSignedArm, recordSignedArm } = await import("./billing-probe.js");
+let savedProvider: ReturnType<typeof credentials.get>;
let savedHasCredentials: PropertyDescriptor | undefined;
-let savedCredentials: unknown;
let savedEnvCodexKey: string | undefined;
/** Drive "which credential WOULD sign" without touching the filesystem. */
@@ -64,11 +299,6 @@ function setHasCredentials(value: boolean): void {
singleton.hasCredentials = () => value;
}
-/** Restore the genuine prototype predicate so it runs against `credentials`. */
-function useRealHasCredentials(): void {
- delete (singleton as unknown as Record<string, unknown>).hasCredentials;
-}
-
/**
* Put an own-property override back the way it was — which may be ABSENT (fall
* through to the prototype) or another file's override, never assumed to be the
@@ -80,13 +310,14 @@ function restoreOwn(target: object, key: string, saved: PropertyDescriptor | und
}
beforeEach(() => {
+ savedProvider = credentials.get("openai-codex");
savedHasCredentials = Object.getOwnPropertyDescriptor(singleton, "hasCredentials");
- savedCredentials = (singleton as unknown as Mutable).credentials;
savedEnvCodexKey = process.env.OPENAI_CODEX_API_KEY;
- clearSignedArm();
+ credentials.invalidate();
});
afterEach(() => {
+ if (savedProvider) credentials.register(savedProvider, ["openai-codex"]);
// Restore whatever was installed BEFORE this test — which may be another
// file's override, not the prototype method.
if (savedHasCredentials) {
@@ -94,14 +325,14 @@ afterEach(() => {
} else {
delete (singleton as unknown as Record<string, unknown>).hasCredentials;
}
- (singleton as unknown as Mutable).credentials = savedCredentials;
+ clearSignedArm();
if (savedEnvCodexKey === undefined) delete process.env.OPENAI_CODEX_API_KEY;
else process.env.OPENAI_CODEX_API_KEY = savedEnvCodexKey;
// ApiKeyCredentialProvider MEMOIZES the resolved key (api-key-credential.ts:110),
// so without this the fixture key set by a test outlives it and every later file
// in the run sees a credentialed openai-codex. Must run AFTER the env restore.
credentials.invalidate("openai-codex");
- clearSignedArm();
+ credentials.invalidate();
});
/** The transport under test, carrying the production definition's fields. */
@@ -120,6 +351,54 @@ function makeCodexTransport(apiKey: string): InstanceType<typeof OpenAICodexTran
}
describe("FR-3: openai-codex billing follows the credential in play", () => {
+ test("B2: real Codex file predicate rejects no-refresh and absent files in child HOME", async () => {
+ const result = await runHomeScenario(`
+ delete process.env.OPENAI_CODEX_API_KEY;
+ const { CodexOAuth } = await load("auth/codex-oauth.js");
+ const { isSubscriptionProvider, getModelPricing } = await load("handlers/shared/remote-provider-types.js");
+ const { clearSignedArm } = await load("auth/credentials/billing-probe.js");
+ const path = credentialPath("openai-codex");
+ const oauth = CodexOAuth.getInstance();
+ const observe = () => {
+ oauth.reloadCredentials();
+ credentials.invalidate("openai-codex");
+ clearSignedArm();
+ const pricing = getModelPricing("openai-codex", "billing-probe-test-model");
+ return { hasCredentials: oauth.hasCredentials(), subscription: isSubscriptionProvider("openai-codex"),
+ fileExists: fs.existsSync(path), priceSubscription: pricing.isSubscription === true,
+ input: pricing.inputCostPer1M, output: pricing.outputCostPer1M };
+ };
+ const absent = observe();
+ const trap = { access_token: "at-no-refresh", expires_at: Date.now() + 3_600_000 };
+ fs.mkdirSync(resolve(home, ".claudish"), { recursive: true });
+ fs.writeFileSync(path, JSON.stringify(trap));
+ const noRefresh = observe();
+ const rejectedOracle = oauthCredentialWouldQualify("openai-codex", trap);
+ fs.writeFileSync(path, JSON.stringify({ ...trap, refresh_token: "codex-test-refresh" }));
+ const complete = observe();
+ fs.unlinkSync(path);
+ const removed = observe();
+ report({ absent, noRefresh, rejectedOracle, complete, removed });
+ `);
+ for (const state of [result.absent, result.noRefresh, result.removed]) {
+ expect(state.hasCredentials).toBe(false);
+ expect(state.subscription).toBe(false);
+ expect(state.priceSubscription).toBe(false);
+ expect(state.input).toBeGreaterThan(0);
+ }
+ expect(result.absent.fileExists).toBe(false);
+ expect(result.noRefresh.fileExists).toBe(true);
+ expect(result.removed.fileExists).toBe(false);
+ expect(result.rejectedOracle).toBe(true);
+ expect(result.complete).toMatchObject({
+ hasCredentials: true,
+ subscription: true,
+ fileExists: true,
+ priceSubscription: true,
+ input: 0,
+ output: 0,
+ });
+ });
test("wiring: importing authority.js installs the probe, and the answer tracks it", () => {
// Fails on ANY machine when the probe is not registered — the first assertion
// is false without it, regardless of whether a real oauth file exists.
@@ -148,26 +427,9 @@ describe("FR-3: openai-codex billing follows the credential in play", () => {
});
test("THE TRAP: access_token unexpired with NO refresh_token → METERED", () => {
- // This is the state that distinguishes the chosen probe from the rejected
- // one, and the test only earns that claim if it can SHOW the two disagreeing.
- //
- // It could not, until now. It drove the singleton's IN-MEMORY credential while
- // the rejected oracle, hasOAuthCredentials, reads the FILE and returns false at
- // its first statement when ~/.claudish/codex-oauth.json is absent
- // (oauth-registry.ts). So on hermetic CI — the machine that has no credential
- // files, and the machine the mutation would ship from — the rejected
- // implementation answered false too and this test stayed GREEN under the very
- // mutation it exists to catch. It passed the mutation run here only because
- // this developer's machine happens to have that file. That is a property of a
- // home directory, not of a test.
- //
- // Fixed by asking the rejected oracle's RULE directly
- // (`oauthCredentialWouldQualify`, which shares its predicate with the real
- // lookup) about the same credential object. No file, no $HOME, same answer
- // everywhere.
- //
- // Every real oauth file on a developer machine carries BOTH tokens, so no
- // live run can reach this state. Only this test can.
+ // The pure qualification rule accepts an unexpired access token. Billing
+ // consults the public hasCredentials predicate, overridden below per B2.
+ // Its real file-backed evaluation runs in the B2 child-process case above.
const trap = { access_token: "at-no-refresh", expires_at: Date.now() + 3_600_000 };
expect(trap.access_token).toBeTruthy();
expect(trap.expires_at).toBeGreaterThan(Date.now());
@@ -179,8 +441,8 @@ describe("FR-3: openai-codex billing follows the credential in play", () => {
// being load-bearing and this whole test should be re-derived.
expect(oauthCredentialWouldQualify("openai-codex", trap)).toBe(true);
- (singleton as unknown as Mutable).credentials = trap;
- useRealHasCredentials();
+ // This control isolates probe wiring; B2 above exercises the real file reader.
+ setHasCredentials(false);
// The mechanism, asserted so a change to either half is attributable.
expect(singleton.hasCredentials()).toBe(false);
@@ -200,8 +462,7 @@ describe("FR-3: openai-codex billing follows the credential in play", () => {
// an earlier version set it and read as a second case when it was the same
// one. The api-key arm is exercised for real in "the REAL composite's
// api-key arm" below, which drives the composite and the transport.
- (singleton as unknown as Mutable).credentials = null;
- useRealHasCredentials();
+ setHasCredentials(false);
expect(singleton.hasCredentials()).toBe(false);
expect(isSubscriptionProvider("openai-codex")).toBe(false);
@@ -271,84 +532,85 @@ describe("FR-3: the REAL composite's api-key arm", () => {
// steps (api-key-credential.ts:176-205) and touch this developer's machine.
// The stub reproduces the artifact those steps would produce, exactly.
setHasCredentials(true); // the presence probe would say subscription…
- const realGetRequestAuth = credentials.getRequestAuth.bind(credentials);
+ const real = credentials.get("openai-codex")!;
try {
- credentials.getRequestAuth = async () => ({ headers: {} });
+ credentials.register(
+ {
+ catalogName: "openai-codex",
+ isAvailable: async () => true,
+ describeReadiness: async () => ({ readiness: "present" }),
+ getRequestAuth: async () => ({ headers: {} }),
+ },
+ ["openai-codex"]
+ );
const transport = makeCodexTransport("");
await transport.refreshAuth();
// …but nothing said an arm, so the answer is metered.
expect(isSubscriptionProvider("openai-codex")).toBe(false);
} finally {
- credentials.getRequestAuth = realGetRequestAuth;
+ credentials.register(real, ["openai-codex"]);
+ credentials.invalidate("openai-codex");
}
});
});
describe("FR-3: the recorded arm beats the presence probe", () => {
- test("a metered signature overrides OAuth credentials that WOULD have signed", () => {
- // C-B: refreshAuth() catches everything and falls through to the plain
- // api-key path against api.openai.com (transport/openai-codex.ts:56-63), so
- // "OAuth present but the api-key arm signed" is reachable per request.
+ test("a metered signed arm overrides an available OAuth presence probe", () => {
setHasCredentials(true);
- expect(isSubscriptionProvider("openai-codex")).toBe(true);
-
recordSignedArm("openai-codex", "metered");
expect(isSubscriptionProvider("openai-codex")).toBe(false);
- expect(getModelPricing("openai-codex", SYNTHETIC_MODEL).isSubscription).toBeUndefined();
+ expect(getModelPricing("openai-codex", SYNTHETIC_MODEL).isSubscription).not.toBe(true);
});
-
- test("a subscription signature overrides an absent presence probe", () => {
+ test("a subscription signed arm overrides an absent presence probe", () => {
setHasCredentials(false);
- expect(isSubscriptionProvider("openai-codex")).toBe(false);
-
recordSignedArm("openai-codex", "subscription");
expect(isSubscriptionProvider("openai-codex")).toBe(true);
+ expect(getModelPricing("openai-codex", SYNTHETIC_MODEL).inputCostPer1M).toBe(0);
});
-
- test("clearing the record returns the answer to what WOULD sign", () => {
+ test("clear a provider record and clear all both restore the presence probe", () => {
setHasCredentials(true);
recordSignedArm("openai-codex", "metered");
- expect(isSubscriptionProvider("openai-codex")).toBe(false);
-
clearSignedArm("openai-codex");
expect(isSubscriptionProvider("openai-codex")).toBe(true);
- });
-
- test("invalidate() and logout() forget the record — it must not outlive the credential", async () => {
- // The record is a statement about a credential state. In a long-lived process
- // (the MCP server, `serve`) one successful OAuth request otherwise pinned SUB
- // on preflight, list_models and the picker after the user logged out or swapped
- // the credential, until some later request happened to rewrite it.
- //
- // Both arms of the assertion matter: the record must go, AND the answer must
- // fall back to "what WOULD sign" rather than to a hardcoded value.
- setHasCredentials(false); // the presence probe says metered…
- recordSignedArm("openai-codex", "subscription"); // …but a SUB request was signed
- expect(isSubscriptionProvider("openai-codex")).toBe(true);
-
- credentials.invalidate("openai-codex");
- expect(isSubscriptionProvider("openai-codex")).toBe(false);
-
- recordSignedArm("openai-codex", "subscription");
+ recordSignedArm("openai-codex", "metered");
+ clearSignedArm();
expect(isSubscriptionProvider("openai-codex")).toBe(true);
- // logout() on a provider whose OAuth file is what it deletes. The registered
- // Codex composite forwards to CodexOAuthHalf.logout → CodexOAuth.logout, so
- // the singleton's own logout is stubbed out for the duration: this test is
- // about the record, and must not delete a real ~/.claudish/codex-oauth.json.
- const savedLogout = Object.getOwnPropertyDescriptor(singleton, "logout");
- try {
- (singleton as unknown as Record<string, unknown>).logout = async () => {};
- await credentials.logout("openai-codex");
- } finally {
- restoreOwn(singleton, "logout", savedLogout);
- }
- expect(isSubscriptionProvider("openai-codex")).toBe(false);
-
- // A whole-authority invalidate clears every provider's record, not just one.
- recordSignedArm("openai-codex", "subscription");
- credentials.invalidate();
- expect(isSubscriptionProvider("openai-codex")).toBe(false);
});
+ test.each(["invalidate", "logout"] as const)(
+ "authority %s clears a signed record",
+ async (action) => {
+ setHasCredentials(true);
+ recordSignedArm("openai-codex", "metered");
+ expect(isSubscriptionProvider("openai-codex")).toBe(false);
+ // Local credential registration keeps logout free of browser/remote work.
+ credentials.register(
+ {
+ catalogName: "openai-codex",
+ isAvailable: async () => true,
+ getRequestAuth: async () => ({ headers: {} }),
+ logout: async () => {},
+ },
+ ["openai-codex"]
+ );
+ recordSignedArm("openai-codex", "metered");
+ if (action === "logout") {
+ // This authority call should return normally. Fail recognisably if an
+ // implementation starts exiting like the public CLI command does.
+ exitAttempted = false;
+ process.exit = (): never => {
+ exitAttempted = true;
+ throw unexpectedExit;
+ };
+ try {
+ await expect(credentials.logout("openai-codex")).resolves.toBeUndefined();
+ expect(exitAttempted).toBe(false);
+ } finally {
+ process.exit = realExit;
+ }
+ } else credentials.invalidate("openai-codex");
+ expect(isSubscriptionProvider("openai-codex")).toBe(true);
+ }
+ );
test("refreshAuth() writes the record, per request, from the REAL arms", async () => {
// The only production writer, driven end to end through the real
@@ -373,16 +635,12 @@ describe("FR-3: the recorded arm beats the presence probe", () => {
const savedAccount = Object.getOwnPropertyDescriptor(singleton, "getAccountId");
const mutable = singleton as unknown as Record<string, unknown>;
try {
- // …but the refresh is rejected (expired grant, revoked token, network). The
- // real CodexOAuthHalf.getRequestAuth throws, the composite rethrows (the
- // Codex composite declares no fallbackSignal), the transport catches and
- // cachedAuth is genuinely null — the ONE state that produces it.
+ const unavailable = new Error("refresh rejected");
mutable.getAccessToken = async () => {
- throw new Error("refresh rejected");
+ throw unavailable;
};
- await transport.refreshAuth();
- expect(transport.getEndpoint()).toBe("https://api.openai.com/v1/responses");
- expect(isSubscriptionProvider("openai-codex")).toBe(false);
+ await expect(transport.refreshAuth()).rejects.toBe(unavailable);
+ expect(isSubscriptionProvider("openai-codex")).toBe(true);
// The OAuth arm wins on the next request → back to subscription. The
// endpoint, the headers and the arm marker all come from the production
@@ -396,13 +654,52 @@ describe("FR-3: the recorded arm beats the presence probe", () => {
);
expect(isSubscriptionProvider("openai-codex")).toBe(true);
- // And back again on the request after that: the record is per request, not
- // sticky. This is the C-B ordering the design names.
+ // E2: a rejected refresh after successful OAuth cannot switch billing arms.
+ const { terminalCredentialError } = await import("./credential-error.js");
+ const rejected = terminalCredentialError("run `claudish login codex`", 400);
mutable.getAccessToken = async () => {
- throw new Error("refresh rejected again");
+ throw rejected;
};
- await transport.refreshAuth();
- expect(isSubscriptionProvider("openai-codex")).toBe(false);
+ await expect(credentials.getRequestAuth("openai-codex", { model: "" })).rejects.toBe(
+ rejected
+ );
+ await expect(transport.refreshAuth()).rejects.toBe(rejected);
+ expect(isSubscriptionProvider("openai-codex")).toBe(true);
+ const { ComposedHandler } = await import("../../handlers/composed-handler.js");
+ const { DefaultAPIFormat } = await import("../../adapters/base-api-format.js");
+ const { Hono } = await import("hono");
+ let inferenceRequests = 0;
+ const fetchBefore = globalThis.fetch;
+ globalThis.fetch = Object.assign(
+ async () => {
+ inferenceRequests++;
+ throw new Error("no inference expected after rejected refresh");
+ },
+ { preconnect: () => {} }
+ ) as typeof fetch;
+ const handler = new ComposedHandler(transport, "gpt-test", "gpt-test", 0, {
+ adapter: new DefaultAPIFormat("gpt-test"),
+ catalogCachePath: join(testHome, "empty.json"),
+ });
+ try {
+ const app = new Hono();
+ app.post("/", (c) =>
+ handler.handle(c, {
+ messages: [{ role: "user", content: "PONG" }],
+ stream: false,
+ max_tokens: 64,
+ })
+ );
+ const response = await app.request("http://localhost/", { method: "POST" });
+ expect(response.status).toBe(400);
+ expect(response.headers.get("x-claudish-auth-verdict")).toBe("terminal");
+ expect(await response.text()).toContain("login codex");
+ expect(inferenceRequests).toBe(0);
+ expect(isSubscriptionProvider("openai-codex")).toBe(true);
+ } finally {
+ globalThis.fetch = fetchBefore;
+ await handler.shutdown();
+ }
} finally {
restoreOwn(singleton, "getAccessToken", savedToken);
restoreOwn(singleton, "getAccountId", savedAccount);
@@ -464,3 +761,16 @@ describe("FR-3: the registration seam", () => {
}
});
});
+
+describe("BILL1: Muse subscription and metered Meta billing categories", () => {
+ test("Muse records subscription pricing as zero; Meta stays metered", () => {
+ expect(isSubscriptionProvider("meta-muse-code")).toBe(true);
+ expect(isSubscriptionProvider("meta")).toBe(false);
+ const muse = getModelPricing("meta-muse-code", SYNTHETIC_MODEL);
+ expect(muse.isSubscription).toBe(true);
+ expect(muse.inputCostPer1M).toBe(0);
+ expect(muse.outputCostPer1M).toBe(0);
+ expect(getModelPricing("meta", SYNTHETIC_MODEL).isSubscription).not.toBe(true);
+ });
+ test.skip("BLOCKED BILL1: registerDynamicPricingLookup returns void, exposes no previous lookup/reset, and ResolvedConnection has no pricing field", () => {});
+});
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.ts
index f41c04ac..1aa48a96 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
@@ -1,34 +1,102 @@
-/**
- * Regression pin for Step 5a — Kimi Coding OAuth delegation.
- *
- * The `kimi-coding` branch of AnthropicProviderTransport.getHeaders() used to
- * read ~/.claudish/kimi-oauth.json directly and mint OAuth headers inline. Step 5
- * delegates that to the credential authority's getRequestAuth("kimi-coding").
- *
- * This test pins the EXACT header shape the transport produces for a kimi-coding
- * provider when an OAuth token is available, so the delegated path is byte-for-byte
- * equivalent to the old inline path:
- * - anthropic-version: 2023-06-01
- * - Authorization: Bearer <token>
- * - the 6 X-Msh-* platform headers
- * - NO x-api-key
- *
- * 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: 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 { 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 { afterAll, afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const savedEnvironment = new Map(["CLAUDISH_DISABLE_KEYCHAIN"].map((k) => [k, process.env[k]]));
+const testHome = mkdtempSync(join(tmpdir(), "claudish-auth-tests-"));
+process.env.CLAUDISH_DISABLE_KEYCHAIN = "1";
+const realFetch = globalThis.fetch;
+globalThis.fetch = Object.assign(
+ (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => {
+ const url = new URL(input instanceof Request ? input.url : String(input));
+ if (url.protocol === "http:" && url.hostname === "127.0.0.1") return realFetch(input, init);
+ throw new Error(`Unexpected external request: ${url.origin}${url.pathname}`);
+ },
+ { preconnect: () => {} }
+) as typeof fetch;
+const keychain = await import("../keychain.js");
+const fakeKeychainDeps = {
+ platform: () => "darwin",
+ run: () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ runAsync: async () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ spawnAsync: () => ({
+ done: Promise.resolve({ code: 44, stdout: "", stderr: "test: absent" }),
+ pid: undefined,
+ }),
+};
+keychain.setKeychainTestDeps(fakeKeychainDeps);
+beforeEach(() => {
+ keychain.setKeychainTestDeps(fakeKeychainDeps);
+});
+afterEach(() => {
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+});
+afterAll(() => {
+ globalThis.fetch = realFetch;
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+ for (const [key, value] of savedEnvironment) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ rmSync(testHome, { recursive: true, force: true });
+});
+
+const { credentials } = await import("../../auth/credentials/authority.js");
+
+const museCore = await import("../../providers/meta-muse/muse-credentials.js");
+const museKeychain = await import("../../providers/meta-muse/muse-keychain.js");
+const promptRole = await import("../../auth/credentials/prompt-role.js");
+const catalogClient = await import("../../providers/catalog-client.js");
+const allModelsCache = await import("../../providers/all-models-cache.js");
+// Every seam is a complete fake: omitted fields would restore machine defaults.
+beforeEach(() => {
+ museCore.setMuseCoreTestDeps({
+ dir: () => testHome,
+ now: () => 1_800_000_000_000,
+ readStore: () => null,
+ writeBorrowedCopy: () => {
+ throw new Error("unexpected borrowed-copy write");
+ },
+ mint: async () => {
+ throw new Error("unexpected Muse mint");
+ },
+ keychainLogin: async () => ({ kind: "not-allowed" }),
+ });
+ museKeychain.setMuseKeychainTestDeps({
+ dir: () => testHome,
+ env: () => ({}),
+ now: () => 1_800_000_000_000,
+ sleep: async () => {
+ throw new Error("unexpected Muse Keychain wait");
+ },
+ pid: () => 424242,
+ isAlive: () => false,
+ presence: async () => ({ kind: "absent" }),
+ startRead: () => ({ done: Promise.resolve({ kind: "absent" as const }), pid: undefined }),
+ });
+});
+afterEach(() => {
+ museCore.setMuseCoreTestDeps(null);
+ museCore.resetMuseCredentialState();
+ museKeychain.setMuseKeychainTestDeps(null);
+ museKeychain.resetMuseKeychainState();
+ promptRole._resetCredentialPromptRoleForTest();
+ catalogClient._resetCatalogClient();
+ allModelsCache._clearAllModelsCacheMemo();
+ keychain.invalidateKeychainCache();
+});
+
+const { clearSignedArm } = await import("../../auth/credentials/billing-probe.js");
+afterEach(() => {
+ clearSignedArm();
+ keychain.invalidateKeychainCache();
+});
+type CredentialProvider = NonNullable<ReturnType<typeof credentials.get>>;
import type { RemoteProvider } from "../../handlers/shared/remote-provider-types.js";
-import { AnthropicProviderTransport } from "./anthropic-compat.js";
+const { AnthropicProviderTransport } = await import("./anthropic-compat.js");
const FAKE_TOKEN = "kimi-oauth-token-xyz";
@@ -74,7 +142,7 @@ function fakeCredential(name: string): CredentialProvider {
const realCredentials = new Map<string, CredentialProvider>();
-beforeAll(() => {
+beforeEach(() => {
for (const name of FAKED_NAMES) {
const real = credentials.get(name);
if (!real) {
@@ -86,7 +154,7 @@ beforeAll(() => {
}
});
-afterAll(() => {
+afterEach(() => {
// 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]);
@@ -129,17 +197,13 @@ describe("AnthropicProviderTransport — kimi-coding OAuth delegation", () => {
expect(headers["X-Msh-Device-Id"]).toBe("device-abc");
});
- test("OAuth path failure falls back to the plain x-api-key header", async () => {
+ test("OAuth unavailable propagates unchanged without a constructor-key signature", async () => {
+ const error = new Error("OAuth unavailable");
getRequestAuthMock = mock(async () => {
- throw new Error("OAuth unavailable");
+ throw error;
});
- const transport = new AnthropicProviderTransport(kimiCoding, "fallback-api-key");
- const headers = await transport.getHeaders();
-
- // Falls back to the plain api-key path: x-api-key set, no Bearer.
- expect(headers["x-api-key"]).toBe("fallback-api-key");
- expect(headers.Authorization).toBeUndefined();
- expect(headers["anthropic-version"]).toBe("2023-06-01");
+ const transport = new AnthropicProviderTransport(kimiCoding, "forbidden-constructor-key");
+ await expect(transport.getHeaders()).rejects.toBe(error);
});
});
diff --git a/packages/cli/src/providers/transport/openai-codex-oauth.test.ts b/packages/cli/src/providers/transport/openai-codex-oauth.test.ts
index 6316e854..9afe62fe 100644
--- a/packages/cli/src/providers/transport/openai-codex-oauth.test.ts
+++ b/packages/cli/src/providers/transport/openai-codex-oauth.test.ts
@@ -1,51 +1,103 @@
-/**
- * Regression pin for Step 5b — OpenAI Codex OAuth delegation.
- *
- * OpenAICodexTransport used to read ~/.claudish/codex-oauth.json directly in BOTH
- * getEndpoint() and getHeaders(), and minted OAuth headers / picked the chatgpt.com
- * endpoint inline. Step 5 moves all of that to credentials.getRequestAuth("openai-codex"),
- * cached by a new refreshAuth() (called by composed-handler BEFORE getEndpoint/getHeaders).
- *
- * This test pins:
- * OAuth present → chatgpt.com/backend-api/codex/responses endpoint
- * + buildOAuthHeaders shape (6 headers w/ accountId)
- * + transformPayload adds store:false / include + model normalization
- * No OAuth (key only) → api.openai.com static endpoint + Bearer <apiKey>
- * + transformPayload still normalizes model (pure, non-auth) but
- * does NOT add the store/include auth bits.
- * OAuth present but its refresh REJECTED → the same api-key-shaped result, by a
- * different mechanism (cachedAuth null → super.getHeaders()).
- *
- * 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`
- * ALWAYS returns an artifact — `{arm:"api-key", headers:{Authorization:"Bearer …"}}`
- * with a key, `{arm:"api-key", headers:{}}` without one. It never returns null and
- * never throws. This file used to model the api-key arm as a throw, which is why it
- * stayed green while `refreshAuth` labelled every metered request `subscription`
- * (review round 1, C-1). The throw is a real path — an AVAILABLE OAuth primary
- * whose refresh is then rejected — so it keeps its own test below, and no longer
- * doubles as the api-key case.
- *
- * `refreshAuth()` also writes the process-wide signed-arm record
- * (auth/credentials/billing-probe.ts). Every test here that calls it must clear
- * that record, or the next FILE in the Bun run inherits a billing answer from a
- * fixture — see the afterEach.
- */
-
-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 { afterAll, afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const savedEnvironment = new Map(["CLAUDISH_DISABLE_KEYCHAIN"].map((k) => [k, process.env[k]]));
+const testHome = mkdtempSync(join(tmpdir(), "claudish-auth-tests-"));
+process.env.CLAUDISH_DISABLE_KEYCHAIN = "1";
+const realFetch = globalThis.fetch;
+globalThis.fetch = Object.assign(
+ (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => {
+ const url = new URL(input instanceof Request ? input.url : String(input));
+ if (url.protocol === "http:" && url.hostname === "127.0.0.1") return realFetch(input, init);
+ throw new Error(`Unexpected external request: ${url.origin}${url.pathname}`);
+ },
+ { preconnect: () => {} }
+) as typeof fetch;
+const keychain = await import("../keychain.js");
+const fakeKeychainDeps = {
+ platform: () => "darwin",
+ run: () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ runAsync: async () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ spawnAsync: () => ({
+ done: Promise.resolve({ code: 44, stdout: "", stderr: "test: absent" }),
+ pid: undefined,
+ }),
+};
+keychain.setKeychainTestDeps(fakeKeychainDeps);
+beforeEach(() => {
+ keychain.setKeychainTestDeps(fakeKeychainDeps);
+});
+afterEach(() => {
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+});
+afterAll(() => {
+ globalThis.fetch = realFetch;
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+ for (const [key, value] of savedEnvironment) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ rmSync(testHome, { recursive: true, force: true });
+});
+
+const { credentials } = await import("../../auth/credentials/authority.js");
+
+const museCore = await import("../../providers/meta-muse/muse-credentials.js");
+const museKeychain = await import("../../providers/meta-muse/muse-keychain.js");
+const promptRole = await import("../../auth/credentials/prompt-role.js");
+const catalogClient = await import("../../providers/catalog-client.js");
+const allModelsCache = await import("../../providers/all-models-cache.js");
+// Every seam is a complete fake: omitted fields would restore machine defaults.
+beforeEach(() => {
+ museCore.setMuseCoreTestDeps({
+ dir: () => testHome,
+ now: () => 1_800_000_000_000,
+ readStore: () => null,
+ writeBorrowedCopy: () => {
+ throw new Error("unexpected borrowed-copy write");
+ },
+ mint: async () => {
+ throw new Error("unexpected Muse mint");
+ },
+ keychainLogin: async () => ({ kind: "not-allowed" }),
+ });
+ museKeychain.setMuseKeychainTestDeps({
+ dir: () => testHome,
+ env: () => ({}),
+ now: () => 1_800_000_000_000,
+ sleep: async () => {
+ throw new Error("unexpected Muse Keychain wait");
+ },
+ pid: () => 424242,
+ isAlive: () => false,
+ presence: async () => ({ kind: "absent" }),
+ startRead: () => ({ done: Promise.resolve({ kind: "absent" as const }), pid: undefined }),
+ });
+});
+afterEach(() => {
+ museCore.setMuseCoreTestDeps(null);
+ museCore.resetMuseCredentialState();
+ museKeychain.setMuseKeychainTestDeps(null);
+ museKeychain.resetMuseKeychainState();
+ promptRole._resetCredentialPromptRoleForTest();
+ catalogClient._resetCatalogClient();
+ allModelsCache._clearAllModelsCacheMemo();
+ keychain.invalidateKeychainCache();
+});
+
+const { clearSignedArm } = await import("../../auth/credentials/billing-probe.js");
+afterEach(() => {
+ clearSignedArm();
+ keychain.invalidateKeychainCache();
+});
+
+type CredentialProvider = NonNullable<ReturnType<typeof credentials.get>>;
import type { RemoteProvider } from "../../handlers/shared/remote-provider-types.js";
-import { OpenAICodexTransport } from "./openai-codex.js";
+const { OpenAICodexTransport } = await import("./openai-codex.js");
const FAKE_TOKEN = "codex-oauth-token-abc";
const FAKE_ACCOUNT = "acct-123";
@@ -98,7 +150,7 @@ const fakeCodexCredential: CredentialProvider = {
let realCodexCredential: CredentialProvider | undefined;
-beforeAll(() => {
+beforeEach(() => {
realCodexCredential = credentials.get("openai-codex");
if (!realCodexCredential) {
throw new Error("the authority has no openai-codex credential to restore after this file");
@@ -107,7 +159,7 @@ beforeAll(() => {
credentials.invalidate("openai-codex");
});
-afterAll(() => {
+afterEach(() => {
// 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");
@@ -134,7 +186,7 @@ afterEach(() => {
// "subscription" behind and the next file answers
// isSubscriptionProvider("openai-codex") = true on a machine where no
// credential would sign. Measured: validation/fix2-residue-leak.txt.
- clearSignedArm("openai-codex");
+ credentials.invalidate("openai-codex");
});
describe("OpenAICodexTransport — OAuth present (delegated)", () => {
@@ -210,21 +262,24 @@ describe("OpenAICodexTransport — OAuth available but its refresh is rejected",
});
});
- test("refreshAuth swallows the throw → cachedAuth null → super endpoint + Bearer apiKey", async () => {
+ test("unclassified refresh rejection propagates without a constructor-key signature", async () => {
const t = new OpenAICodexTransport(provider, "gpt-5.1-codex", "sk-codex-key");
- await t.refreshAuth();
-
- // Same observable result as the api-key arm above, reached by a different
- // mechanism: null cachedAuth rather than an endpoint-less artifact. Keeping
- // both is the point — they were conflated once, and the billing label that
- // was inferred from "cachedAuth is null" was wrong for the api-key arm.
- expect(t.getEndpoint()).toBe("https://api.openai.com/v1/responses");
- const headers = await t.getHeaders();
- expect(headers.Authorization).toBe("Bearer sk-codex-key");
- expect(headers["chatgpt-account-id"]).toBeUndefined();
+ await expect(t.refreshAuth()).rejects.toThrow("refresh_token rejected");
+ });
- const out = t.transformPayload({ model: "gpt-5.1-codex", input: "y" });
- expect(out.store).toBeUndefined();
- expect(out.include).toBeUndefined();
+ test("E2: successful OAuth followed by rejected refresh never records metered billing", async () => {
+ const { isSubscriptionProvider } = await import(
+ "../../handlers/shared/remote-provider-types.js"
+ );
+ getRequestAuthMock = mock(async () => CODEX_OAUTH_AUTH);
+ const t = new OpenAICodexTransport(provider, "gpt-5.1-codex", "sk-codex-key");
+ await t.refreshAuth();
+ expect((await t.getHeaders()).Authorization).toBe(`Bearer ${FAKE_TOKEN}`);
+ const error = new Error("refresh_token rejected");
+ getRequestAuthMock = mock(async () => {
+ throw error;
+ });
+ await expect(t.refreshAuth()).rejects.toBe(error);
+ expect(isSubscriptionProvider("openai-codex")).toBe(true);
});
});
diff --git a/packages/cli/src/providers/transport/openai-codex-refresh-outage.test.ts b/packages/cli/src/providers/transport/openai-codex-refresh-outage.test.ts
index b83bb086..2857441b 100644
--- a/packages/cli/src/providers/transport/openai-codex-refresh-outage.test.ts
+++ b/packages/cli/src/providers/transport/openai-codex-refresh-outage.test.ts
@@ -1,54 +1,113 @@
-/**
- * Behaviour tests C1–C4 — a Codex token refresh that cannot reach the network
- * must not bill metered.
- *
- * Spec: ai-docs/sessions/dev-feature-network-recovery-20260910-110514-0ad0a5a0/overlay/codex-spec.md
- * Written black-box from that spec and its contract (codex-contract.md,
- * auth/credentials/types.ts). The transport's source was not an input.
- *
- * `openai-codex` signs with a ChatGPT subscription (OAuth) when it can and
- * otherwise falls back to a METERED api key at api.openai.com. The two ways an
- * OAuth refresh can fail must end differently:
- *
- * - REJECTED by the token server (revoked / expired refresh token): falling
- * back to the api key is intended — `refreshAuth()` resolves (C2).
- * - UNREACHABLE (network outage): the subscription still exists, it is only out
- * of reach. Falling back would bill a subscriber per token for an outage, so
- * `refreshAuth()` must REJECT with an error `classifyConnectionError`
- * recognises, so the handler can hold and retry the request (C1).
- *
- * Test plan (REQ id → tests):
- * REQ-1 / C1 unreachable → rejects, classifiable, never resolves C1-bare, C1-wrapped
- * REQ-2 / C2 rejected → resolves, endpoint is NOT chatgpt.com C2 × 2 rejection shapes
- * REQ-3 / C3 oauth → resolves, endpoint is the artifact's C3
- * REQ-4 / C4 fixture guard: the connection failure classifies, C4 × 4
- * the rejections do not
- *
- * Fixture rules (from the contract, all load-bearing):
- * - NO `mock.module()`. The fake credential is installed with
- * `credentials.register(fake, ["openai-codex"])`, and afterEach re-registers
- * the REAL `makeCodexCredential()` so no later test or file inherits the fake.
- * - The connection failure is a REAL failed `fetch` to a loopback port proven
- * free (a server was bound to it, then stopped, and the fetch is refused).
- * Never a hand-made `{ code: "ECONNREFUSED" }` — a hand-made error proves only
- * that the classifier recognises the hand-made shape.
- * - refreshAuth() writes the process-wide signed-arm billing record; afterEach
- * clears it (as the sibling openai-codex-*.test.ts files do) so this file
- * never decides billing for the next file in the Bun run.
- */
+import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const savedEnvironment = new Map(["CLAUDISH_DISABLE_KEYCHAIN"].map((k) => [k, process.env[k]]));
+const testHome = mkdtempSync(join(tmpdir(), "claudish-auth-tests-"));
+process.env.CLAUDISH_DISABLE_KEYCHAIN = "1";
+const realFetch = globalThis.fetch;
+globalThis.fetch = Object.assign(
+ (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => {
+ const url = new URL(input instanceof Request ? input.url : String(input));
+ if (url.protocol === "http:" && url.hostname === "127.0.0.1") return realFetch(input, init);
+ throw new Error(`Unexpected external request: ${url.origin}${url.pathname}`);
+ },
+ { preconnect: () => {} }
+) as typeof fetch;
+const keychain = await import("../keychain.js");
+const fakeKeychainDeps = {
+ platform: () => "darwin",
+ run: () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ runAsync: async () => ({ code: 44, stdout: "", stderr: "test: absent" }),
+ spawnAsync: () => ({
+ done: Promise.resolve({ code: 44, stdout: "", stderr: "test: absent" }),
+ pid: undefined,
+ }),
+};
+keychain.setKeychainTestDeps(fakeKeychainDeps);
+beforeEach(() => {
+ keychain.setKeychainTestDeps(fakeKeychainDeps);
+});
+afterEach(() => {
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+});
+afterAll(() => {
+ globalThis.fetch = realFetch;
+ keychain.setKeychainTestDeps(null);
+ keychain.invalidateKeychainCache();
+ for (const [key, value] of savedEnvironment) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ rmSync(testHome, { recursive: true, force: true });
+});
-import { afterEach, describe, expect, test } from "bun:test";
-import { credentials } from "../../auth/credentials/authority.js";
-import { clearSignedArm } from "../../auth/credentials/billing-probe.js";
-import { makeCodexCredential } from "../../auth/credentials/codex-credential.js";
-import type {
- CredentialProvider,
- RequestAuth,
- RequestAuthContext,
-} from "../../auth/credentials/types.js";
-import { classifyConnectionError } from "../../handlers/shared/connection-error.js";
+const { credentials } = await import("../../auth/credentials/authority.js");
+
+const museCore = await import("../../providers/meta-muse/muse-credentials.js");
+const museKeychain = await import("../../providers/meta-muse/muse-keychain.js");
+const promptRole = await import("../../auth/credentials/prompt-role.js");
+const catalogClient = await import("../../providers/catalog-client.js");
+const allModelsCache = await import("../../providers/all-models-cache.js");
+// Every seam is a complete fake: omitted fields would restore machine defaults.
+beforeEach(() => {
+ museCore.setMuseCoreTestDeps({
+ dir: () => testHome,
+ now: () => 1_800_000_000_000,
+ readStore: () => null,
+ writeBorrowedCopy: () => {
+ throw new Error("unexpected borrowed-copy write");
+ },
+ mint: async () => {
+ throw new Error("unexpected Muse mint");
+ },
+ keychainLogin: async () => ({ kind: "not-allowed" }),
+ });
+ museKeychain.setMuseKeychainTestDeps({
+ dir: () => testHome,
+ env: () => ({}),
+ now: () => 1_800_000_000_000,
+ sleep: async () => {
+ throw new Error("unexpected Muse Keychain wait");
+ },
+ pid: () => 424242,
+ isAlive: () => false,
+ presence: async () => ({ kind: "absent" }),
+ startRead: () => ({ done: Promise.resolve({ kind: "absent" as const }), pid: undefined }),
+ });
+});
+afterEach(() => {
+ museCore.setMuseCoreTestDeps(null);
+ museCore.resetMuseCredentialState();
+ museKeychain.setMuseKeychainTestDeps(null);
+ museKeychain.resetMuseKeychainState();
+ promptRole._resetCredentialPromptRoleForTest();
+ catalogClient._resetCatalogClient();
+ allModelsCache._clearAllModelsCacheMemo();
+ keychain.invalidateKeychainCache();
+});
+
+const { clearSignedArm } = await import("../../auth/credentials/billing-probe.js");
+afterEach(() => {
+ clearSignedArm();
+ keychain.invalidateKeychainCache();
+});
+let original: ReturnType<typeof credentials.get>;
+beforeEach(() => {
+ original = credentials.get("openai-codex");
+});
+import { classifyAuthFailure } from "../../handlers/shared/auth-failure.js";
+type CredentialProvider = NonNullable<ReturnType<typeof credentials.get>>;
+type RequestAuthContext = Parameters<CredentialProvider["getRequestAuth"]>[0];
+type RequestAuth = Awaited<ReturnType<CredentialProvider["getRequestAuth"]>>;
+function classifyConnectionError(error: unknown) {
+ const failure = classifyAuthFailure(error);
+ return failure.kind === "connection" ? failure.conn : null;
+}
import type { RemoteProvider } from "../../handlers/shared/remote-provider-types.js";
-import { OpenAICodexTransport } from "./openai-codex.js";
+const { OpenAICodexTransport } = await import("./openai-codex.js");
const PROVIDER: RemoteProvider = {
name: "openai-codex",
@@ -61,16 +120,22 @@ const MODEL = "gpt-5.1-codex";
const API_KEY = "sk-codex-key";
const SUBSCRIPTION_ENDPOINT = "https://chatgpt.com/backend-api/codex/responses";
-/** The spec's example of a genuinely REJECTED refresh (C2). */
+const { credentialHttpError } = await import("../../auth/credentials/credential-error.js");
+
+/** B17: throw the classified rejection itself; wrapping would make it unknown. */
function invalidGrantRejection(): Error {
- return new Error("OAuth credentials invalid. Please run `claudish login codex` again.", {
- cause: new Error("Token refresh failed: 400 - invalid_grant"),
- });
+ return credentialHttpError(
+ "OAuth credentials invalid (invalid_grant). Please run `claudish login codex` again.",
+ 400
+ );
}
/** A second non-network rejection: the token server's answer, unwrapped. */
function bareServerRejection(): Error {
- return new Error("Token refresh failed: 400 - invalid_grant");
+ return credentialHttpError(
+ "Token refresh failed: 400 - invalid_grant; run `claudish login codex`",
+ 400
+ );
}
/**
@@ -79,7 +144,7 @@ function bareServerRejection(): Error {
* the value `fetch` threw, untouched.
*/
async function realConnectionFailure(): Promise<unknown> {
- const server = Bun.serve({ port: 0, fetch: () => new Response("x") });
+ const server = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: () => new Response("x") });
const port = server.port;
await server.stop(true);
try {
@@ -131,14 +196,13 @@ function settle(p: Promise<unknown>): Promise<Settled> {
);
}
-function newTransport(): OpenAICodexTransport {
+function newTransport(): InstanceType<typeof OpenAICodexTransport> {
return new OpenAICodexTransport(PROVIDER, MODEL, API_KEY);
}
afterEach(() => {
- credentials.register(makeCodexCredential(), ["openai-codex"]);
+ if (original) credentials.register(original, ["openai-codex"]);
credentials.invalidate("openai-codex");
- clearSignedArm("openai-codex");
});
describe("C1 (REQ-1) — token host unreachable: refreshAuth rejects with a classifiable connection error", () => {
@@ -174,12 +238,12 @@ describe("C1 (REQ-1) — token host unreachable: refreshAuth rejects with a clas
});
});
-describe("C2 (REQ-2) — refresh REJECTED by the server: refreshAuth resolves and falls back to the api-key endpoint", () => {
+describe("C2 (REQ-2) — refresh REJECTED by the server: refreshAuth rejects without a metered signature", () => {
test.each([
- ["the spec's invalid_grant rejection, wrapped", invalidGrantRejection],
+ ["classified invalid_grant login rejection", invalidGrantRejection],
["a bare 400 invalid_grant rejection", bareServerRejection],
])(
- "C2: resolves, and getEndpoint is the provider's api-key base rather than chatgpt.com, for %s",
+ "C2: rejects the invalid grant without a constructor-key signature, for %s",
async (_label, makeRejection) => {
const fake = installFakeCodexCredential(async () => {
throw makeRejection();
@@ -187,13 +251,12 @@ describe("C2 (REQ-2) — refresh REJECTED by the server: refreshAuth resolves an
const transport = newTransport();
const outcome = await settle(transport.refreshAuth());
- const endpoint = transport.getEndpoint(MODEL);
-
expect(fake.consulted()).toBeGreaterThan(0);
- expect(outcome).toEqual({ settled: "resolved" });
- expect(endpoint).not.toBe(SUBSCRIPTION_ENDPOINT);
- expect(endpoint).not.toContain("chatgpt.com");
- expect(endpoint).toStartWith(PROVIDER.baseUrl);
+ expect(outcome.settled).toBe("rejected");
+ if (outcome.settled === "rejected") {
+ expect(String(outcome.reason)).toContain("invalid");
+ expect(classifyAuthFailure(outcome.reason).kind).toBe("terminal");
+ }
}
);
});
@@ -240,3 +303,51 @@ describe("C4 (REQ-4) — the fixtures prove something: the classifier separates
expect(classifyConnectionError(bareServerRejection())).toBeNull();
});
});
+
+// Section 1: actual HTTP response and request counts, not endpoint getters alone.
+describe("C2/E2: Codex handler never sends a metered request on refresh failure", () => {
+ test.each([
+ ["unknown refresh", () => new Error("refresh_token rejected"), 503, "transient"],
+ ["classified invalid grant login rejection", invalidGrantRejection, 400, "terminal"],
+ ["bare invalid grant", bareServerRejection, 400, "terminal"],
+ ] as const)("%s", async (_label, makeError, status, verdict) => {
+ const { Hono } = await import("hono");
+ const { ComposedHandler } = await import("../../handlers/composed-handler.js");
+ const { DefaultAPIFormat } = await import("../../adapters/base-api-format.js");
+ installFakeCodexCredential(async () => {
+ throw makeError();
+ });
+ let requests = 0;
+ const fetchBefore = globalThis.fetch;
+ globalThis.fetch = Object.assign(
+ async () => {
+ requests++;
+ throw new Error("unexpected inference");
+ },
+ { preconnect: () => {} }
+ ) as typeof fetch;
+ const handler = new ComposedHandler(newTransport(), MODEL, MODEL, 0, {
+ adapter: new DefaultAPIFormat(MODEL),
+ catalogCachePath: join(testHome, "empty.json"),
+ });
+ try {
+ const app = new Hono();
+ app.post("/", (c) =>
+ handler.handle(c, {
+ messages: [{ role: "user", content: "PONG" }],
+ max_tokens: 64,
+ stream: false,
+ })
+ );
+ const response = await app.request("http://localhost/", { method: "POST" });
+ expect(response.status).toBe(status);
+ expect(response.headers.get("x-claudish-auth-verdict")).toBe(verdict);
+ const body = await response.text();
+ expect(body).toMatch(status === 400 ? /login codex/ : /refresh_token rejected/);
+ expect(requests).toBe(0);
+ } finally {
+ globalThis.fetch = fetchBefore;
+ await handler.shutdown();
+ }
+ });
+});