mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 00:41:36 +00:00
refactor(test): reuse env helper in models auth sync
This commit is contained in:
@@ -4,31 +4,9 @@ import path from "node:path";
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { saveAuthProfileStore } from "../agents/auth-profiles.js";
|
import { saveAuthProfileStore } from "../agents/auth-profiles.js";
|
||||||
import { clearConfigCache } from "../config/config.js";
|
import { clearConfigCache } from "../config/config.js";
|
||||||
|
import { withEnvAsync } from "../test-utils/env.js";
|
||||||
import { modelsListCommand } from "./models/list.list-command.js";
|
import { modelsListCommand } from "./models/list.list-command.js";
|
||||||
|
|
||||||
const ENV_KEYS = [
|
|
||||||
"OPENCLAW_STATE_DIR",
|
|
||||||
"OPENCLAW_AGENT_DIR",
|
|
||||||
"PI_CODING_AGENT_DIR",
|
|
||||||
"OPENCLAW_CONFIG_PATH",
|
|
||||||
"OPENROUTER_API_KEY",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
function captureEnv() {
|
|
||||||
return Object.fromEntries(ENV_KEYS.map((key) => [key, process.env[key]]));
|
|
||||||
}
|
|
||||||
|
|
||||||
function restoreEnv(snapshot: Record<string, string | undefined>) {
|
|
||||||
for (const key of ENV_KEYS) {
|
|
||||||
const value = snapshot[key];
|
|
||||||
if (value === undefined) {
|
|
||||||
delete process.env[key];
|
|
||||||
} else {
|
|
||||||
process.env[key] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function pathExists(pathname: string): Promise<boolean> {
|
async function pathExists(pathname: string): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
await fs.stat(pathname);
|
await fs.stat(pathname);
|
||||||
@@ -40,7 +18,6 @@ async function pathExists(pathname: string): Promise<boolean> {
|
|||||||
|
|
||||||
describe("models list auth-profile sync", () => {
|
describe("models list auth-profile sync", () => {
|
||||||
it("marks models available when auth exists only in auth-profiles.json", async () => {
|
it("marks models available when auth exists only in auth-profiles.json", async () => {
|
||||||
const env = captureEnv();
|
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-models-list-auth-sync-"));
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-models-list-auth-sync-"));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -50,51 +27,55 @@ describe("models list auth-profile sync", () => {
|
|||||||
await fs.mkdir(agentDir, { recursive: true });
|
await fs.mkdir(agentDir, { recursive: true });
|
||||||
await fs.writeFile(configPath, "{}\n", "utf8");
|
await fs.writeFile(configPath, "{}\n", "utf8");
|
||||||
|
|
||||||
process.env.OPENCLAW_STATE_DIR = stateDir;
|
await withEnvAsync(
|
||||||
process.env.OPENCLAW_AGENT_DIR = agentDir;
|
|
||||||
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
||||||
process.env.OPENCLAW_CONFIG_PATH = configPath;
|
|
||||||
delete process.env.OPENROUTER_API_KEY;
|
|
||||||
|
|
||||||
saveAuthProfileStore(
|
|
||||||
{
|
{
|
||||||
version: 1,
|
OPENCLAW_STATE_DIR: stateDir,
|
||||||
profiles: {
|
OPENCLAW_AGENT_DIR: agentDir,
|
||||||
"openrouter:default": {
|
PI_CODING_AGENT_DIR: agentDir,
|
||||||
type: "api_key",
|
OPENCLAW_CONFIG_PATH: configPath,
|
||||||
provider: "openrouter",
|
OPENROUTER_API_KEY: undefined,
|
||||||
key: "sk-or-v1-regression-test",
|
},
|
||||||
},
|
async () => {
|
||||||
},
|
saveAuthProfileStore(
|
||||||
|
{
|
||||||
|
version: 1,
|
||||||
|
profiles: {
|
||||||
|
"openrouter:default": {
|
||||||
|
type: "api_key",
|
||||||
|
provider: "openrouter",
|
||||||
|
key: "sk-or-v1-regression-test",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
agentDir,
|
||||||
|
);
|
||||||
|
|
||||||
|
const authPath = path.join(agentDir, "auth.json");
|
||||||
|
expect(await pathExists(authPath)).toBe(false);
|
||||||
|
|
||||||
|
clearConfigCache();
|
||||||
|
const runtime = {
|
||||||
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
await modelsListCommand({ all: true, json: true }, runtime as never);
|
||||||
|
|
||||||
|
expect(runtime.error).not.toHaveBeenCalled();
|
||||||
|
expect(runtime.log).toHaveBeenCalledTimes(1);
|
||||||
|
const payload = JSON.parse(String(runtime.log.mock.calls[0]?.[0])) as {
|
||||||
|
models?: Array<{ key?: string; available?: boolean }>;
|
||||||
|
};
|
||||||
|
const openrouter = payload.models?.find((model) =>
|
||||||
|
String(model.key ?? "").startsWith("openrouter/"),
|
||||||
|
);
|
||||||
|
expect(openrouter).toBeDefined();
|
||||||
|
expect(openrouter?.available).toBe(true);
|
||||||
|
expect(await pathExists(authPath)).toBe(true);
|
||||||
},
|
},
|
||||||
agentDir,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const authPath = path.join(agentDir, "auth.json");
|
|
||||||
expect(await pathExists(authPath)).toBe(false);
|
|
||||||
|
|
||||||
clearConfigCache();
|
|
||||||
const runtime = {
|
|
||||||
log: vi.fn(),
|
|
||||||
error: vi.fn(),
|
|
||||||
};
|
|
||||||
|
|
||||||
await modelsListCommand({ all: true, json: true }, runtime as never);
|
|
||||||
|
|
||||||
expect(runtime.error).not.toHaveBeenCalled();
|
|
||||||
expect(runtime.log).toHaveBeenCalledTimes(1);
|
|
||||||
const payload = JSON.parse(String(runtime.log.mock.calls[0]?.[0])) as {
|
|
||||||
models?: Array<{ key?: string; available?: boolean }>;
|
|
||||||
};
|
|
||||||
const openrouter = payload.models?.find((model) =>
|
|
||||||
String(model.key ?? "").startsWith("openrouter/"),
|
|
||||||
);
|
|
||||||
expect(openrouter).toBeDefined();
|
|
||||||
expect(openrouter?.available).toBe(true);
|
|
||||||
expect(await pathExists(authPath)).toBe(true);
|
|
||||||
} finally {
|
} finally {
|
||||||
clearConfigCache();
|
clearConfigCache();
|
||||||
restoreEnv(env);
|
|
||||||
await fs.rm(root, { recursive: true, force: true });
|
await fs.rm(root, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user