mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
test: clean model cache state env
This commit is contained in:
@@ -3,7 +3,8 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";
|
import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { withEnvAsync } from "../../test-utils/env.js";
|
||||||
import { discoverAuthStorage, discoverModels } from "../agent-model-discovery.js";
|
import { discoverAuthStorage, discoverModels } from "../agent-model-discovery.js";
|
||||||
import {
|
import {
|
||||||
clearRuntimeAuthProfileStoreSnapshots,
|
clearRuntimeAuthProfileStoreSnapshots,
|
||||||
@@ -188,10 +189,6 @@ beforeEach(() => {
|
|||||||
resolveBundledStaticCatalogModelMock.mockReset();
|
resolveBundledStaticCatalogModelMock.mockReset();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
vi.unstubAllEnvs();
|
|
||||||
});
|
|
||||||
|
|
||||||
function createRuntimeHooks() {
|
function createRuntimeHooks() {
|
||||||
// Runtime hooks emulate provider plugin model discovery, transport
|
// Runtime hooks emulate provider plugin model discovery, transport
|
||||||
// normalization, and OpenRouter capability loading without plugin imports.
|
// normalization, and OpenRouter capability loading without plugin imports.
|
||||||
@@ -412,39 +409,44 @@ describe("resolveModel", () => {
|
|||||||
|
|
||||||
it("invalidates agent discovery stores when implicit main auth changes without config", async () => {
|
it("invalidates agent discovery stores when implicit main auth changes without config", async () => {
|
||||||
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-model-cache-state-"));
|
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-model-cache-state-"));
|
||||||
vi.stubEnv("OPENCLAW_STATE_DIR", rootDir);
|
|
||||||
const agentDir = path.join(rootDir, "agents", "worker", "agent");
|
const agentDir = path.join(rootDir, "agents", "worker", "agent");
|
||||||
const mainAgentDir = path.join(rootDir, "agents", "main", "agent");
|
const mainAgentDir = path.join(rootDir, "agents", "main", "agent");
|
||||||
fs.mkdirSync(agentDir, { recursive: true });
|
fs.mkdirSync(agentDir, { recursive: true });
|
||||||
fs.mkdirSync(mainAgentDir, { recursive: true });
|
fs.mkdirSync(mainAgentDir, { recursive: true });
|
||||||
mockDiscoveredModel(discoverModels, {
|
try {
|
||||||
provider: "openai",
|
await withEnvAsync({ OPENCLAW_STATE_DIR: rootDir }, async () => {
|
||||||
modelId: "gpt-5.5",
|
mockDiscoveredModel(discoverModels, {
|
||||||
templateModel: {
|
provider: "openai",
|
||||||
provider: "openai",
|
modelId: "gpt-5.5",
|
||||||
...makeModel("gpt-5.5"),
|
templateModel: {
|
||||||
},
|
provider: "openai",
|
||||||
});
|
...makeModel("gpt-5.5"),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const first = await resolveModelAsync("openai", "gpt-5.5", agentDir, undefined, {
|
const first = await resolveModelAsync("openai", "gpt-5.5", agentDir, undefined, {
|
||||||
runtimeHooks: createRuntimeHooks(),
|
runtimeHooks: createRuntimeHooks(),
|
||||||
});
|
});
|
||||||
saveAuthProfileStore(
|
saveAuthProfileStore(
|
||||||
{
|
{
|
||||||
version: 1,
|
version: 1,
|
||||||
profiles: { "openai:default": { type: "api_key", provider: "openai", key: "one" } },
|
profiles: { "openai:default": { type: "api_key", provider: "openai", key: "one" } },
|
||||||
},
|
},
|
||||||
mainAgentDir,
|
mainAgentDir,
|
||||||
{ filterExternalAuthProfiles: false, syncExternalCli: false },
|
{ filterExternalAuthProfiles: false, syncExternalCli: false },
|
||||||
);
|
);
|
||||||
const second = await resolveModelAsync("openai", "gpt-5.5", agentDir, undefined, {
|
const second = await resolveModelAsync("openai", "gpt-5.5", agentDir, undefined, {
|
||||||
runtimeHooks: createRuntimeHooks(),
|
runtimeHooks: createRuntimeHooks(),
|
||||||
});
|
});
|
||||||
|
|
||||||
expectResolvedModel(first);
|
expectResolvedModel(first);
|
||||||
expectResolvedModel(second);
|
expectResolvedModel(second);
|
||||||
expect(discoverAuthStorage).toHaveBeenCalledTimes(2);
|
expect(discoverAuthStorage).toHaveBeenCalledTimes(2);
|
||||||
expect(discoverModels).toHaveBeenCalledTimes(2);
|
expect(discoverModels).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
fs.rmSync(rootDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not cache agent discovery stores while runtime auth snapshots are active", async () => {
|
it("does not cache agent discovery stores while runtime auth snapshots are active", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user