mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-14 18:51:04 +00:00
test: exercise models json file mode without provider discovery
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { resolveOpenClawAgentDir } from "./agent-paths.js";
|
||||
import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js";
|
||||
import {
|
||||
CUSTOM_PROXY_MODELS_CONFIG,
|
||||
installModelsConfigTestHooks,
|
||||
withModelsTempHome as withTempHome,
|
||||
} from "./models-config.e2e-harness.js";
|
||||
import { ensureOpenClawModelsJson, resetModelsJsonReadyCacheForTest } from "./models-config.js";
|
||||
ensureModelsFileModeForModelsJson,
|
||||
writeModelsFileAtomicForModelsJson,
|
||||
} from "./models-config.js";
|
||||
|
||||
installModelsConfigTestHooks();
|
||||
const tempDirs = new Set<string>();
|
||||
|
||||
afterEach(() => {
|
||||
resetModelsJsonReadyCacheForTest();
|
||||
cleanupTempDirs(tempDirs);
|
||||
});
|
||||
|
||||
describe("models-config file mode", () => {
|
||||
@@ -20,28 +18,25 @@ describe("models-config file mode", () => {
|
||||
if (process.platform === "win32") {
|
||||
return;
|
||||
}
|
||||
await withTempHome(async () => {
|
||||
await ensureOpenClawModelsJson(CUSTOM_PROXY_MODELS_CONFIG);
|
||||
const modelsPath = path.join(resolveOpenClawAgentDir(), "models.json");
|
||||
const stat = await fs.stat(modelsPath);
|
||||
expect(stat.mode & 0o777).toBe(0o600);
|
||||
});
|
||||
const dir = makeTempDir(tempDirs, "models-json-mode-");
|
||||
const modelsPath = path.join(dir, "models.json");
|
||||
await writeModelsFileAtomicForModelsJson(modelsPath, '{"providers":{}}\n');
|
||||
const stat = await fs.stat(modelsPath);
|
||||
expect(stat.mode & 0o777).toBe(0o600);
|
||||
});
|
||||
|
||||
it("repairs models.json mode to 0600 on no-content-change paths", async () => {
|
||||
if (process.platform === "win32") {
|
||||
return;
|
||||
}
|
||||
await withTempHome(async () => {
|
||||
await ensureOpenClawModelsJson(CUSTOM_PROXY_MODELS_CONFIG);
|
||||
const modelsPath = path.join(resolveOpenClawAgentDir(), "models.json");
|
||||
await fs.chmod(modelsPath, 0o644);
|
||||
const dir = makeTempDir(tempDirs, "models-json-mode-");
|
||||
const modelsPath = path.join(dir, "models.json");
|
||||
await writeModelsFileAtomicForModelsJson(modelsPath, '{"providers":{}}\n');
|
||||
await fs.chmod(modelsPath, 0o644);
|
||||
|
||||
const result = await ensureOpenClawModelsJson(CUSTOM_PROXY_MODELS_CONFIG);
|
||||
expect(result.wrote).toBe(false);
|
||||
await ensureModelsFileModeForModelsJson(modelsPath);
|
||||
|
||||
const stat = await fs.stat(modelsPath);
|
||||
expect(stat.mode & 0o777).toBe(0o600);
|
||||
});
|
||||
const stat = await fs.stat(modelsPath);
|
||||
expect(stat.mode & 0o777).toBe(0o600);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -74,13 +74,16 @@ async function readExistingModelsFile(pathname: string): Promise<{
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureModelsFileMode(pathname: string): Promise<void> {
|
||||
export async function ensureModelsFileModeForModelsJson(pathname: string): Promise<void> {
|
||||
await fs.chmod(pathname, 0o600).catch(() => {
|
||||
// best-effort
|
||||
});
|
||||
}
|
||||
|
||||
async function writeModelsFileAtomic(targetPath: string, contents: string): Promise<void> {
|
||||
export async function writeModelsFileAtomicForModelsJson(
|
||||
targetPath: string,
|
||||
contents: string,
|
||||
): Promise<void> {
|
||||
const tempPath = `${targetPath}.${process.pid}.${Date.now()}.tmp`;
|
||||
await fs.writeFile(tempPath, contents, { mode: 0o600 });
|
||||
await fs.rename(tempPath, targetPath);
|
||||
@@ -149,7 +152,7 @@ export async function ensureOpenClawModelsJson(
|
||||
if (cached) {
|
||||
const settled = await cached;
|
||||
if (settled.fingerprint === fingerprint) {
|
||||
await ensureModelsFileMode(targetPath);
|
||||
await ensureModelsFileModeForModelsJson(targetPath);
|
||||
return settled.result;
|
||||
}
|
||||
}
|
||||
@@ -173,13 +176,13 @@ export async function ensureOpenClawModelsJson(
|
||||
}
|
||||
|
||||
if (plan.action === "noop") {
|
||||
await ensureModelsFileMode(targetPath);
|
||||
await ensureModelsFileModeForModelsJson(targetPath);
|
||||
return { fingerprint, result: { agentDir, wrote: false } };
|
||||
}
|
||||
|
||||
await fs.mkdir(agentDir, { recursive: true, mode: 0o700 });
|
||||
await writeModelsFileAtomic(targetPath, plan.contents);
|
||||
await ensureModelsFileMode(targetPath);
|
||||
await writeModelsFileAtomicForModelsJson(targetPath, plan.contents);
|
||||
await ensureModelsFileModeForModelsJson(targetPath);
|
||||
return { fingerprint, result: { agentDir, wrote: true } };
|
||||
});
|
||||
MODELS_JSON_STATE.readyCache.set(targetPath, pending);
|
||||
|
||||
Reference in New Issue
Block a user