diff --git a/packages/adapters/grok-local/src/server/execute.test.ts b/packages/adapters/grok-local/src/server/execute.test.ts index 3d7f5d6c9b..2b03c7dcbd 100644 --- a/packages/adapters/grok-local/src/server/execute.test.ts +++ b/packages/adapters/grok-local/src/server/execute.test.ts @@ -139,8 +139,6 @@ describe("grok_local execute", () => { }); it("reports real per-run token usage, marks it as per_run, and only surfaces cost for API billing", async () => { - const root = await makeTempRoot(); - runProcessMock.mockImplementation(async () => ({ exitCode: 0, signal: null, @@ -159,8 +157,8 @@ describe("grok_local execute", () => { stderr: "", })); - const baseCtx: AdapterExecutionContext = { - runId: "run-1", + const makeCtx = async (runId: string): Promise => ({ + runId, agent: { id: "agent-1", companyId: "company-1", @@ -169,27 +167,30 @@ describe("grok_local execute", () => { adapterConfig: {}, }, runtime: { sessionId: null, sessionParams: null, sessionDisplayId: null, taskKey: null }, - config: { cwd: root }, + config: { cwd: await makeTempRoot() }, context: {}, authToken: "run-token", onLog: async () => {}, - }; - - // Subscription billing (no XAI_API_KEY): token usage is populated, but - // there is no marginal dollar cost so costUsd stays null. - const subscriptionResult = await execute(baseCtx); - expect(subscriptionResult).toMatchObject({ - usage: { inputTokens: 2384, outputTokens: 261, cachedInputTokens: 23040 }, - usageBasis: "per_run", - billingType: "subscription", - costUsd: null, }); - // API-key billing: same token usage, plus the real dollar cost. const previousApiKey = process.env.XAI_API_KEY; - process.env.XAI_API_KEY = "test-key"; try { - const apiResult = await execute(baseCtx); + // Subscription billing (no XAI_API_KEY): token usage is populated, but + // there is no marginal dollar cost so costUsd stays null. Clear the key + // explicitly so the ambient environment (dev machine or CI with provider + // secrets) cannot flip this branch to API billing. + delete process.env.XAI_API_KEY; + const subscriptionResult = await execute(await makeCtx("run-subscription")); + expect(subscriptionResult).toMatchObject({ + usage: { inputTokens: 2384, outputTokens: 261, cachedInputTokens: 23040 }, + usageBasis: "per_run", + billingType: "subscription", + costUsd: null, + }); + + // API-key billing: same token usage, plus the real dollar cost. + process.env.XAI_API_KEY = "test-key"; + const apiResult = await execute(await makeCtx("run-api")); expect(apiResult).toMatchObject({ usage: { inputTokens: 2384, outputTokens: 261, cachedInputTokens: 23040 }, usageBasis: "per_run",