|
1 | | -import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 1 | +import { expectDefined } from "@openclaw/normalization-core"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 3 | import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
3 | 4 | import { clearPluginMetadataLifecycleCaches } from "../../plugins/plugin-metadata-lifecycle.js"; |
| 5 | +import { AuthStorage, ModelRegistry } from "../sessions/index.js"; |
4 | 6 | import { |
5 | 7 | createModelGenerationFixture, |
6 | 8 | publishCurrentModelGeneration, |
@@ -32,9 +34,50 @@ describe("model runtime generation scope", () => { |
32 | 34 | }); |
33 | 35 |
|
34 | 36 | afterEach(() => { |
| 37 | + vi.restoreAllMocks(); |
35 | 38 | resetModelGenerationFixtureState(); |
36 | 39 | }); |
37 | 40 |
|
| 41 | + it.each([ |
| 42 | + { auth: true, registry: true }, |
| 43 | + { auth: true, registry: false }, |
| 44 | + { auth: false, registry: true }, |
| 45 | + { auth: false, registry: false }, |
| 46 | + ])("fills only missing discovery stores (auth=$auth, registry=$registry)", async (supplied) => { |
| 47 | + const generation = createModelGenerationFixture({ config: {}, label: "stores" }); |
| 48 | + const { preparedModelRuntime } = generation; |
| 49 | + const stores = preparedModelRuntime.createStores(); |
| 50 | + stores.authStorage.setRuntimeApiKey(generation.provider, "fixture-runtime-key"); |
| 51 | + const preparedStores = vi.spyOn(preparedModelRuntime, "createStores"); |
| 52 | + const emptyAuth = vi.spyOn(AuthStorage, "inMemory"); |
| 53 | + const emptyRegistry = vi.spyOn(ModelRegistry, "inMemory"); |
| 54 | + |
| 55 | + const result = await resolveModelAsync( |
| 56 | + generation.provider, |
| 57 | + generation.modelId, |
| 58 | + preparedModelRuntime.agentDir, |
| 59 | + preparedModelRuntime.config, |
| 60 | + { |
| 61 | + ...(supplied.auth ? { authStorage: stores.authStorage } : {}), |
| 62 | + ...(supplied.registry ? { modelRegistry: stores.modelRegistry } : {}), |
| 63 | + preparedModelRuntime, |
| 64 | + skipAgentDiscovery: true, |
| 65 | + workspaceDir: preparedModelRuntime.workspaceDir, |
| 66 | + }, |
| 67 | + ); |
| 68 | + |
| 69 | + expect(preparedStores).not.toHaveBeenCalled(); |
| 70 | + const allocations = supplied.auth && supplied.registry ? 0 : 1; |
| 71 | + expect(emptyAuth).toHaveBeenCalledTimes(allocations); |
| 72 | + expect(emptyRegistry).toHaveBeenCalledTimes(allocations); |
| 73 | + expect(result.authStorage === stores.authStorage).toBe(supplied.auth); |
| 74 | + expect(result.modelRegistry === stores.modelRegistry).toBe(supplied.registry); |
| 75 | + const model = expectDefined(result.model, "resolved fixture model"); |
| 76 | + expect(await result.modelRegistry.getApiKeyAndHeaders(model)).toMatchObject({ |
| 77 | + apiKey: supplied.auth || supplied.registry ? "fixture-runtime-key" : undefined, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
38 | 81 | it("keeps alias, suppression, static metadata, and runtime hooks on the prepared generation", async () => { |
39 | 82 | const config = {} satisfies OpenClawConfig; |
40 | 83 | const generationA = createModelGenerationFixture({ config, label: "a" }); |
|
0 commit comments