mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 18:54:58 +00:00
test(commands): dedupe command and onboarding test cases
This commit is contained in:
@@ -28,67 +28,109 @@ describe("onboard auth credentials secret refs", () => {
|
||||
await lifecycle.cleanup();
|
||||
});
|
||||
|
||||
it("keeps env-backed moonshot key as plaintext by default", async () => {
|
||||
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-");
|
||||
type AuthProfileEntry = { key?: string; keyRef?: unknown; metadata?: unknown };
|
||||
|
||||
async function withAuthEnv(
|
||||
prefix: string,
|
||||
run: (env: Awaited<ReturnType<typeof setupAuthTestEnv>>) => Promise<void>,
|
||||
) {
|
||||
const env = await setupAuthTestEnv(prefix);
|
||||
lifecycle.setStateDir(env.stateDir);
|
||||
process.env.MOONSHOT_API_KEY = "sk-moonshot-env";
|
||||
|
||||
await setMoonshotApiKey("sk-moonshot-env");
|
||||
await run(env);
|
||||
}
|
||||
|
||||
async function readProfile(
|
||||
agentDir: string,
|
||||
profileId: string,
|
||||
): Promise<AuthProfileEntry | undefined> {
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(env.agentDir);
|
||||
expect(parsed.profiles?.["moonshot:default"]).toMatchObject({
|
||||
key: "sk-moonshot-env",
|
||||
profiles?: Record<string, AuthProfileEntry>;
|
||||
}>(agentDir);
|
||||
return parsed.profiles?.[profileId];
|
||||
}
|
||||
|
||||
async function expectStoredAuthKey(params: {
|
||||
prefix: string;
|
||||
envVar?: string;
|
||||
envValue?: string;
|
||||
profileId: string;
|
||||
apply: (agentDir: string) => Promise<void>;
|
||||
expected: AuthProfileEntry;
|
||||
absent?: Array<keyof AuthProfileEntry>;
|
||||
}) {
|
||||
await withAuthEnv(params.prefix, async (env) => {
|
||||
if (params.envVar && params.envValue !== undefined) {
|
||||
process.env[params.envVar] = params.envValue;
|
||||
}
|
||||
await params.apply(env.agentDir);
|
||||
const profile = await readProfile(env.agentDir, params.profileId);
|
||||
expect(profile).toMatchObject(params.expected);
|
||||
for (const key of params.absent ?? []) {
|
||||
expect(profile?.[key]).toBeUndefined();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
it("keeps env-backed moonshot key as plaintext by default", async () => {
|
||||
await expectStoredAuthKey({
|
||||
prefix: "openclaw-onboard-auth-credentials-",
|
||||
envVar: "MOONSHOT_API_KEY",
|
||||
envValue: "sk-moonshot-env",
|
||||
profileId: "moonshot:default",
|
||||
apply: async () => {
|
||||
await setMoonshotApiKey("sk-moonshot-env");
|
||||
},
|
||||
expected: {
|
||||
key: "sk-moonshot-env",
|
||||
},
|
||||
absent: ["keyRef"],
|
||||
});
|
||||
expect(parsed.profiles?.["moonshot:default"]?.keyRef).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores env-backed moonshot key as keyRef when secret-input-mode=ref", async () => {
|
||||
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-ref-");
|
||||
lifecycle.setStateDir(env.stateDir);
|
||||
process.env.MOONSHOT_API_KEY = "sk-moonshot-env";
|
||||
|
||||
await setMoonshotApiKey("sk-moonshot-env", env.agentDir, { secretInputMode: "ref" });
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(env.agentDir);
|
||||
expect(parsed.profiles?.["moonshot:default"]).toMatchObject({
|
||||
keyRef: { source: "env", provider: "default", id: "MOONSHOT_API_KEY" },
|
||||
await expectStoredAuthKey({
|
||||
prefix: "openclaw-onboard-auth-credentials-ref-",
|
||||
envVar: "MOONSHOT_API_KEY",
|
||||
envValue: "sk-moonshot-env",
|
||||
profileId: "moonshot:default",
|
||||
apply: async (agentDir) => {
|
||||
await setMoonshotApiKey("sk-moonshot-env", agentDir, { secretInputMode: "ref" });
|
||||
},
|
||||
expected: {
|
||||
keyRef: { source: "env", provider: "default", id: "MOONSHOT_API_KEY" },
|
||||
},
|
||||
absent: ["key"],
|
||||
});
|
||||
expect(parsed.profiles?.["moonshot:default"]?.key).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores ${ENV} moonshot input as keyRef even when env value is unset", async () => {
|
||||
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-inline-ref-");
|
||||
lifecycle.setStateDir(env.stateDir);
|
||||
|
||||
await setMoonshotApiKey("${MOONSHOT_API_KEY}");
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(env.agentDir);
|
||||
expect(parsed.profiles?.["moonshot:default"]).toMatchObject({
|
||||
keyRef: { source: "env", provider: "default", id: "MOONSHOT_API_KEY" },
|
||||
await expectStoredAuthKey({
|
||||
prefix: "openclaw-onboard-auth-credentials-inline-ref-",
|
||||
profileId: "moonshot:default",
|
||||
apply: async () => {
|
||||
await setMoonshotApiKey("${MOONSHOT_API_KEY}");
|
||||
},
|
||||
expected: {
|
||||
keyRef: { source: "env", provider: "default", id: "MOONSHOT_API_KEY" },
|
||||
},
|
||||
absent: ["key"],
|
||||
});
|
||||
expect(parsed.profiles?.["moonshot:default"]?.key).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps plaintext moonshot key when no env ref applies", async () => {
|
||||
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-plaintext-");
|
||||
lifecycle.setStateDir(env.stateDir);
|
||||
process.env.MOONSHOT_API_KEY = "sk-moonshot-other";
|
||||
|
||||
await setMoonshotApiKey("sk-moonshot-plaintext");
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(env.agentDir);
|
||||
expect(parsed.profiles?.["moonshot:default"]).toMatchObject({
|
||||
key: "sk-moonshot-plaintext",
|
||||
await expectStoredAuthKey({
|
||||
prefix: "openclaw-onboard-auth-credentials-plaintext-",
|
||||
envVar: "MOONSHOT_API_KEY",
|
||||
envValue: "sk-moonshot-other",
|
||||
profileId: "moonshot:default",
|
||||
apply: async () => {
|
||||
await setMoonshotApiKey("sk-moonshot-plaintext");
|
||||
},
|
||||
expected: {
|
||||
key: "sk-moonshot-plaintext",
|
||||
},
|
||||
absent: ["keyRef"],
|
||||
});
|
||||
expect(parsed.profiles?.["moonshot:default"]?.keyRef).toBeUndefined();
|
||||
});
|
||||
|
||||
it("preserves cloudflare metadata when storing keyRef", async () => {
|
||||
@@ -111,35 +153,35 @@ describe("onboard auth credentials secret refs", () => {
|
||||
});
|
||||
|
||||
it("keeps env-backed openai key as plaintext by default", async () => {
|
||||
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-openai-");
|
||||
lifecycle.setStateDir(env.stateDir);
|
||||
process.env.OPENAI_API_KEY = "sk-openai-env";
|
||||
|
||||
await setOpenaiApiKey("sk-openai-env");
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(env.agentDir);
|
||||
expect(parsed.profiles?.["openai:default"]).toMatchObject({
|
||||
key: "sk-openai-env",
|
||||
await expectStoredAuthKey({
|
||||
prefix: "openclaw-onboard-auth-credentials-openai-",
|
||||
envVar: "OPENAI_API_KEY",
|
||||
envValue: "sk-openai-env",
|
||||
profileId: "openai:default",
|
||||
apply: async () => {
|
||||
await setOpenaiApiKey("sk-openai-env");
|
||||
},
|
||||
expected: {
|
||||
key: "sk-openai-env",
|
||||
},
|
||||
absent: ["keyRef"],
|
||||
});
|
||||
expect(parsed.profiles?.["openai:default"]?.keyRef).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores env-backed openai key as keyRef in ref mode", async () => {
|
||||
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-openai-ref-");
|
||||
lifecycle.setStateDir(env.stateDir);
|
||||
process.env.OPENAI_API_KEY = "sk-openai-env";
|
||||
|
||||
await setOpenaiApiKey("sk-openai-env", env.agentDir, { secretInputMode: "ref" });
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(env.agentDir);
|
||||
expect(parsed.profiles?.["openai:default"]).toMatchObject({
|
||||
keyRef: { source: "env", provider: "default", id: "OPENAI_API_KEY" },
|
||||
await expectStoredAuthKey({
|
||||
prefix: "openclaw-onboard-auth-credentials-openai-ref-",
|
||||
envVar: "OPENAI_API_KEY",
|
||||
envValue: "sk-openai-env",
|
||||
profileId: "openai:default",
|
||||
apply: async (agentDir) => {
|
||||
await setOpenaiApiKey("sk-openai-env", agentDir, { secretInputMode: "ref" });
|
||||
},
|
||||
expected: {
|
||||
keyRef: { source: "env", provider: "default", id: "OPENAI_API_KEY" },
|
||||
},
|
||||
absent: ["key"],
|
||||
});
|
||||
expect(parsed.profiles?.["openai:default"]?.key).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores env-backed volcengine and byteplus keys as keyRef in ref mode", async () => {
|
||||
|
||||
Reference in New Issue
Block a user