|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
1 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
2 | 5 | import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
3 | 6 | import { |
4 | 7 | listModels, |
5 | 8 | providerCatalogEntry, |
6 | 9 | } from "./models-list-result.openai-routes.test-support.js"; |
7 | 10 |
|
8 | | -const mocks = vi.hoisted(() => ({ |
9 | | - readClaudeCliCredentialsCached: vi.fn<() => unknown>(() => null), |
10 | | - readCodexCliCredentialsCached: vi.fn<() => unknown>(() => null), |
11 | | - readMiniMaxCliCredentialsCached: vi.fn<() => unknown>(() => null), |
12 | | -})); |
13 | | - |
14 | | -vi.mock("../../agents/cli-credentials.js", () => mocks); |
| 11 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
15 | 12 |
|
16 | 13 | const config = { |
17 | 14 | agents: { |
@@ -40,26 +37,28 @@ async function listClaudeCliModel() { |
40 | 37 | describe("models.list CLI runtime availability", () => { |
41 | 38 | beforeEach(() => { |
42 | 39 | vi.stubEnv("ANTHROPIC_API_KEY", ""); |
43 | | - mocks.readClaudeCliCredentialsCached.mockReset(); |
44 | | - mocks.readClaudeCliCredentialsCached.mockReturnValue(null); |
45 | | - mocks.readCodexCliCredentialsCached.mockReset(); |
46 | | - mocks.readCodexCliCredentialsCached.mockReturnValue(null); |
47 | | - mocks.readMiniMaxCliCredentialsCached.mockReset(); |
48 | | - mocks.readMiniMaxCliCredentialsCached.mockReturnValue(null); |
49 | 40 | }); |
50 | 41 |
|
51 | 42 | afterEach(() => { |
52 | 43 | vi.unstubAllEnvs(); |
53 | 44 | }); |
54 | 45 |
|
55 | 46 | it("marks a Claude CLI runtime model available with ambient CLI OAuth", async () => { |
56 | | - mocks.readClaudeCliCredentialsCached.mockReturnValue({ |
57 | | - type: "oauth", |
58 | | - provider: "anthropic", |
59 | | - access: "test-access", |
60 | | - refresh: "test-refresh", |
61 | | - expires: Date.now() + 3_600_000, |
62 | | - }); |
| 47 | + const homeDir = tempDirs.make("models-list-claude-cli-"); |
| 48 | + const credentialDir = path.join(homeDir, ".claude"); |
| 49 | + fs.mkdirSync(credentialDir, { recursive: true, mode: 0o700 }); |
| 50 | + fs.writeFileSync( |
| 51 | + path.join(credentialDir, ".credentials.json"), |
| 52 | + JSON.stringify({ |
| 53 | + claudeAiOauth: { |
| 54 | + accessToken: "test-access", |
| 55 | + refreshToken: "test-refresh", |
| 56 | + expiresAt: Date.now() + 3_600_000, |
| 57 | + }, |
| 58 | + }), |
| 59 | + { mode: 0o600 }, |
| 60 | + ); |
| 61 | + vi.stubEnv("HOME", homeDir); |
63 | 62 |
|
64 | 63 | await expect(listClaudeCliModel()).resolves.toEqual({ |
65 | 64 | models: [expect.objectContaining({ id: "claude-opus-5", available: true })], |
|
0 commit comments