|
1 | 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 |
|
3 | | -const completeMock = vi.fn(); |
4 | | -const minimaxUnderstandImageMock = vi.fn(); |
5 | | -const ensureOpenClawModelsJsonMock = vi.fn(async () => {}); |
6 | | -const getApiKeyForModelMock = vi.fn(async () => ({ |
7 | | - apiKey: "oauth-test", // pragma: allowlist secret |
8 | | - source: "test", |
9 | | - mode: "oauth", |
| 3 | +const hoisted = vi.hoisted(() => ({ |
| 4 | + completeMock: vi.fn(), |
| 5 | + minimaxUnderstandImageMock: vi.fn(), |
| 6 | + ensureOpenClawModelsJsonMock: vi.fn(async () => {}), |
| 7 | + getApiKeyForModelMock: vi.fn(async () => ({ |
| 8 | + apiKey: "oauth-test", // pragma: allowlist secret |
| 9 | + source: "test", |
| 10 | + mode: "oauth", |
| 11 | + })), |
| 12 | + resolveApiKeyForProviderMock: vi.fn(async () => ({ |
| 13 | + apiKey: "oauth-test", // pragma: allowlist secret |
| 14 | + source: "test", |
| 15 | + mode: "oauth", |
| 16 | + })), |
| 17 | + requireApiKeyMock: vi.fn((auth: { apiKey?: string }) => auth.apiKey ?? ""), |
| 18 | + setRuntimeApiKeyMock: vi.fn(), |
| 19 | + discoverModelsMock: vi.fn(), |
10 | 20 | })); |
11 | | -const resolveApiKeyForProviderMock = vi.fn(async () => ({ |
12 | | - apiKey: "oauth-test", // pragma: allowlist secret |
13 | | - source: "test", |
14 | | - mode: "oauth", |
| 21 | +const { |
| 22 | + completeMock, |
| 23 | + minimaxUnderstandImageMock, |
| 24 | + ensureOpenClawModelsJsonMock, |
| 25 | + getApiKeyForModelMock, |
| 26 | + resolveApiKeyForProviderMock, |
| 27 | + requireApiKeyMock, |
| 28 | + setRuntimeApiKeyMock, |
| 29 | + discoverModelsMock, |
| 30 | +} = hoisted; |
| 31 | + |
| 32 | +vi.mock("@mariozechner/pi-ai", async (importOriginal) => { |
| 33 | + const actual = await importOriginal<typeof import("@mariozechner/pi-ai")>(); |
| 34 | + return { |
| 35 | + ...actual, |
| 36 | + complete: completeMock, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +vi.mock("../../agents/minimax-vlm.js", () => ({ |
| 41 | + isMinimaxVlmProvider: (provider: string) => |
| 42 | + provider === "minimax" || provider === "minimax-portal", |
| 43 | + isMinimaxVlmModel: (provider: string, modelId: string) => |
| 44 | + (provider === "minimax" || provider === "minimax-portal") && modelId === "MiniMax-VL-01", |
| 45 | + minimaxUnderstandImage: minimaxUnderstandImageMock, |
15 | 46 | })); |
16 | | -const requireApiKeyMock = vi.fn((auth: { apiKey?: string }) => auth.apiKey ?? ""); |
17 | | -const setRuntimeApiKeyMock = vi.fn(); |
18 | | -const discoverModelsMock = vi.fn(); |
19 | | -type ImageModule = typeof import("./image.js"); |
20 | 47 |
|
21 | | -let describeImageWithModel: ImageModule["describeImageWithModel"]; |
| 48 | +vi.mock("../../agents/models-config.js", () => ({ |
| 49 | + ensureOpenClawModelsJson: ensureOpenClawModelsJsonMock, |
| 50 | +})); |
| 51 | + |
| 52 | +vi.mock("../../agents/model-auth.js", () => ({ |
| 53 | + getApiKeyForModel: getApiKeyForModelMock, |
| 54 | + resolveApiKeyForProvider: resolveApiKeyForProviderMock, |
| 55 | + requireApiKey: requireApiKeyMock, |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock("../../agents/pi-model-discovery-runtime.js", () => ({ |
| 59 | + discoverAuthStorage: () => ({ |
| 60 | + setRuntimeApiKey: setRuntimeApiKeyMock, |
| 61 | + }), |
| 62 | + discoverModels: discoverModelsMock, |
| 63 | +})); |
| 64 | + |
| 65 | +const { describeImageWithModel } = await import("./image.js"); |
22 | 66 |
|
23 | 67 | describe("describeImageWithModel", () => { |
24 | | - beforeEach(async () => { |
25 | | - vi.resetModules(); |
| 68 | + beforeEach(() => { |
26 | 69 | vi.clearAllMocks(); |
27 | | - vi.doMock("@mariozechner/pi-ai", async (importOriginal) => { |
28 | | - const actual = await importOriginal<typeof import("@mariozechner/pi-ai")>(); |
29 | | - return { |
30 | | - ...actual, |
31 | | - complete: completeMock, |
32 | | - }; |
33 | | - }); |
34 | | - vi.doMock("../../agents/minimax-vlm.js", () => ({ |
35 | | - isMinimaxVlmProvider: (provider: string) => |
36 | | - provider === "minimax" || provider === "minimax-portal", |
37 | | - isMinimaxVlmModel: (provider: string, modelId: string) => |
38 | | - (provider === "minimax" || provider === "minimax-portal") && modelId === "MiniMax-VL-01", |
39 | | - minimaxUnderstandImage: minimaxUnderstandImageMock, |
40 | | - })); |
41 | | - vi.doMock("../../agents/models-config.js", () => ({ |
42 | | - ensureOpenClawModelsJson: ensureOpenClawModelsJsonMock, |
43 | | - })); |
44 | | - vi.doMock("../../agents/model-auth.js", () => ({ |
45 | | - getApiKeyForModel: getApiKeyForModelMock, |
46 | | - resolveApiKeyForProvider: resolveApiKeyForProviderMock, |
47 | | - requireApiKey: requireApiKeyMock, |
48 | | - })); |
49 | | - vi.doMock("../../agents/pi-model-discovery-runtime.js", () => ({ |
50 | | - discoverAuthStorage: () => ({ |
51 | | - setRuntimeApiKey: setRuntimeApiKeyMock, |
52 | | - }), |
53 | | - discoverModels: discoverModelsMock, |
54 | | - })); |
55 | | - ({ describeImageWithModel } = await import("./image.js")); |
56 | 70 | minimaxUnderstandImageMock.mockResolvedValue("portal ok"); |
57 | 71 | discoverModelsMock.mockReturnValue({ |
58 | 72 | find: vi.fn(() => ({ |
|
0 commit comments