mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 01:28:27 +00:00
test(commands): dedupe auth-sync fixture and cover invalid profile handling
This commit is contained in:
@@ -16,67 +16,114 @@ async function pathExists(pathname: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AuthSyncFixture = {
|
||||||
|
root: string;
|
||||||
|
stateDir: string;
|
||||||
|
agentDir: string;
|
||||||
|
configPath: string;
|
||||||
|
authPath: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function withAuthSyncFixture(run: (fixture: AuthSyncFixture) => Promise<void>) {
|
||||||
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-models-list-auth-sync-"));
|
||||||
|
try {
|
||||||
|
const stateDir = path.join(root, "state");
|
||||||
|
const agentDir = path.join(stateDir, "agents", "main", "agent");
|
||||||
|
const configPath = path.join(stateDir, "openclaw.json");
|
||||||
|
const authPath = path.join(agentDir, "auth.json");
|
||||||
|
|
||||||
|
await fs.mkdir(agentDir, { recursive: true });
|
||||||
|
await fs.writeFile(configPath, "{}\n", "utf8");
|
||||||
|
|
||||||
|
await withEnvAsync(
|
||||||
|
{
|
||||||
|
OPENCLAW_STATE_DIR: stateDir,
|
||||||
|
OPENCLAW_AGENT_DIR: agentDir,
|
||||||
|
PI_CODING_AGENT_DIR: agentDir,
|
||||||
|
OPENCLAW_CONFIG_PATH: configPath,
|
||||||
|
OPENROUTER_API_KEY: undefined,
|
||||||
|
},
|
||||||
|
async () => {
|
||||||
|
clearConfigCache();
|
||||||
|
await run({ root, stateDir, agentDir, configPath, authPath });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
clearConfigCache();
|
||||||
|
await fs.rm(root, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRuntime() {
|
||||||
|
return {
|
||||||
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getProviderRow(payloadText: string, providerPrefix: string) {
|
||||||
|
const payload = JSON.parse(payloadText) as {
|
||||||
|
models?: Array<{ key?: string; available?: boolean }>;
|
||||||
|
};
|
||||||
|
return payload.models?.find((model) => String(model.key ?? "").startsWith(providerPrefix));
|
||||||
|
}
|
||||||
|
|
||||||
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 root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-models-list-auth-sync-"));
|
await withAuthSyncFixture(async ({ agentDir, authPath }) => {
|
||||||
|
saveAuthProfileStore(
|
||||||
try {
|
|
||||||
const stateDir = path.join(root, "state");
|
|
||||||
const agentDir = path.join(stateDir, "agents", "main", "agent");
|
|
||||||
const configPath = path.join(stateDir, "openclaw.json");
|
|
||||||
await fs.mkdir(agentDir, { recursive: true });
|
|
||||||
await fs.writeFile(configPath, "{}\n", "utf8");
|
|
||||||
|
|
||||||
await withEnvAsync(
|
|
||||||
{
|
{
|
||||||
OPENCLAW_STATE_DIR: stateDir,
|
version: 1,
|
||||||
OPENCLAW_AGENT_DIR: agentDir,
|
profiles: {
|
||||||
PI_CODING_AGENT_DIR: agentDir,
|
"openrouter:default": {
|
||||||
OPENCLAW_CONFIG_PATH: configPath,
|
type: "api_key",
|
||||||
OPENROUTER_API_KEY: undefined,
|
provider: "openrouter",
|
||||||
},
|
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,
|
||||||
);
|
);
|
||||||
} finally {
|
|
||||||
clearConfigCache();
|
expect(await pathExists(authPath)).toBe(false);
|
||||||
await fs.rm(root, { recursive: true, force: true });
|
|
||||||
}
|
const runtime = createRuntime();
|
||||||
|
await modelsListCommand({ all: true, json: true }, runtime as never);
|
||||||
|
|
||||||
|
expect(runtime.error).not.toHaveBeenCalled();
|
||||||
|
expect(runtime.log).toHaveBeenCalledTimes(1);
|
||||||
|
const openrouter = getProviderRow(String(runtime.log.mock.calls[0]?.[0]), "openrouter/");
|
||||||
|
expect(openrouter).toBeDefined();
|
||||||
|
expect(openrouter?.available).toBe(true);
|
||||||
|
expect(await pathExists(authPath)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps providers unavailable when auth profile credentials are invalid", async () => {
|
||||||
|
await withAuthSyncFixture(async ({ agentDir, authPath }) => {
|
||||||
|
saveAuthProfileStore(
|
||||||
|
{
|
||||||
|
version: 1,
|
||||||
|
profiles: {
|
||||||
|
"openrouter:default": {
|
||||||
|
type: "api_key",
|
||||||
|
provider: "openrouter",
|
||||||
|
key: " ",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
agentDir,
|
||||||
|
);
|
||||||
|
|
||||||
|
const runtime = createRuntime();
|
||||||
|
await modelsListCommand({ all: true, json: true }, runtime as never);
|
||||||
|
|
||||||
|
expect(runtime.error).not.toHaveBeenCalled();
|
||||||
|
expect(runtime.log).toHaveBeenCalledTimes(1);
|
||||||
|
const openrouter = getProviderRow(String(runtime.log.mock.calls[0]?.[0]), "openrouter/");
|
||||||
|
expect(openrouter).toBeDefined();
|
||||||
|
expect(openrouter?.available).not.toBe(true);
|
||||||
|
expect(await pathExists(authPath)).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user