mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 11:07:41 +00:00
test(commands): dedupe command and onboarding test cases
This commit is contained in:
@@ -24,163 +24,117 @@ describe("volcengine/byteplus auth choice", () => {
|
||||
return env.agentDir;
|
||||
}
|
||||
|
||||
function createTestContext(defaultSelect: string, confirmResult = true, textValue = "unused") {
|
||||
return {
|
||||
prompter: createWizardPrompter(
|
||||
{
|
||||
confirm: vi.fn(async () => confirmResult),
|
||||
text: vi.fn(async () => textValue),
|
||||
},
|
||||
{ defaultSelect },
|
||||
),
|
||||
runtime: createExitThrowingRuntime(),
|
||||
};
|
||||
}
|
||||
|
||||
type ProviderAuthCase = {
|
||||
provider: "volcengine" | "byteplus";
|
||||
authChoice: "volcengine-api-key" | "byteplus-api-key";
|
||||
envVar: "VOLCANO_ENGINE_API_KEY" | "BYTEPLUS_API_KEY";
|
||||
envValue: string;
|
||||
profileId: "volcengine:default" | "byteplus:default";
|
||||
applyAuthChoice: typeof applyAuthChoiceVolcengine | typeof applyAuthChoiceBytePlus;
|
||||
};
|
||||
|
||||
async function runProviderAuthChoice(
|
||||
testCase: ProviderAuthCase,
|
||||
options?: {
|
||||
defaultSelect?: string;
|
||||
confirmResult?: boolean;
|
||||
textValue?: string;
|
||||
secretInputMode?: "ref";
|
||||
},
|
||||
) {
|
||||
const agentDir = await setupTempState();
|
||||
process.env[testCase.envVar] = testCase.envValue;
|
||||
|
||||
const { prompter, runtime } = createTestContext(
|
||||
options?.defaultSelect ?? "plaintext",
|
||||
options?.confirmResult ?? true,
|
||||
options?.textValue ?? "unused",
|
||||
);
|
||||
|
||||
const result = await testCase.applyAuthChoice({
|
||||
authChoice: testCase.authChoice,
|
||||
config: {},
|
||||
prompter,
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
...(options?.secretInputMode ? { opts: { secretInputMode: options.secretInputMode } } : {}),
|
||||
});
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(agentDir);
|
||||
|
||||
return { result, parsed };
|
||||
}
|
||||
|
||||
const providerAuthCases: ProviderAuthCase[] = [
|
||||
{
|
||||
provider: "volcengine",
|
||||
authChoice: "volcengine-api-key",
|
||||
envVar: "VOLCANO_ENGINE_API_KEY",
|
||||
envValue: "volc-env-key",
|
||||
profileId: "volcengine:default",
|
||||
applyAuthChoice: applyAuthChoiceVolcengine,
|
||||
},
|
||||
{
|
||||
provider: "byteplus",
|
||||
authChoice: "byteplus-api-key",
|
||||
envVar: "BYTEPLUS_API_KEY",
|
||||
envValue: "byte-env-key",
|
||||
profileId: "byteplus:default",
|
||||
applyAuthChoice: applyAuthChoiceBytePlus,
|
||||
},
|
||||
];
|
||||
|
||||
afterEach(async () => {
|
||||
await lifecycle.cleanup();
|
||||
});
|
||||
|
||||
it("stores volcengine env key as plaintext by default", async () => {
|
||||
const agentDir = await setupTempState();
|
||||
process.env.VOLCANO_ENGINE_API_KEY = "volc-env-key";
|
||||
it.each(providerAuthCases)(
|
||||
"stores $provider env key as plaintext by default",
|
||||
async (testCase) => {
|
||||
const { result, parsed } = await runProviderAuthChoice(testCase);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.config.auth?.profiles?.[testCase.profileId]).toMatchObject({
|
||||
provider: testCase.provider,
|
||||
mode: "api_key",
|
||||
});
|
||||
expect(parsed.profiles?.[testCase.profileId]?.key).toBe(testCase.envValue);
|
||||
expect(parsed.profiles?.[testCase.profileId]?.keyRef).toBeUndefined();
|
||||
},
|
||||
);
|
||||
|
||||
const prompter = createWizardPrompter(
|
||||
{
|
||||
confirm: vi.fn(async () => true),
|
||||
text: vi.fn(async () => "unused"),
|
||||
},
|
||||
{ defaultSelect: "plaintext" },
|
||||
);
|
||||
const runtime = createExitThrowingRuntime();
|
||||
|
||||
const result = await applyAuthChoiceVolcengine({
|
||||
authChoice: "volcengine-api-key",
|
||||
config: {},
|
||||
prompter,
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
it.each(providerAuthCases)("stores $provider env key as keyRef in ref mode", async (testCase) => {
|
||||
const { result, parsed } = await runProviderAuthChoice(testCase, {
|
||||
defaultSelect: "ref",
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.config.auth?.profiles?.["volcengine:default"]).toMatchObject({
|
||||
provider: "volcengine",
|
||||
mode: "api_key",
|
||||
expect(parsed.profiles?.[testCase.profileId]).toMatchObject({
|
||||
keyRef: { source: "env", provider: "default", id: testCase.envVar },
|
||||
});
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(agentDir);
|
||||
expect(parsed.profiles?.["volcengine:default"]?.key).toBe("volc-env-key");
|
||||
expect(parsed.profiles?.["volcengine:default"]?.keyRef).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores volcengine env key as keyRef in ref mode", async () => {
|
||||
const agentDir = await setupTempState();
|
||||
process.env.VOLCANO_ENGINE_API_KEY = "volc-env-key";
|
||||
|
||||
const prompter = createWizardPrompter(
|
||||
{
|
||||
confirm: vi.fn(async () => true),
|
||||
text: vi.fn(async () => "unused"),
|
||||
},
|
||||
{ defaultSelect: "ref" },
|
||||
);
|
||||
const runtime = createExitThrowingRuntime();
|
||||
|
||||
const result = await applyAuthChoiceVolcengine({
|
||||
authChoice: "volcengine-api-key",
|
||||
config: {},
|
||||
prompter,
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(agentDir);
|
||||
expect(parsed.profiles?.["volcengine:default"]).toMatchObject({
|
||||
keyRef: { source: "env", provider: "default", id: "VOLCANO_ENGINE_API_KEY" },
|
||||
});
|
||||
expect(parsed.profiles?.["volcengine:default"]?.key).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores byteplus env key as plaintext by default", async () => {
|
||||
const agentDir = await setupTempState();
|
||||
process.env.BYTEPLUS_API_KEY = "byte-env-key";
|
||||
|
||||
const prompter = createWizardPrompter(
|
||||
{
|
||||
confirm: vi.fn(async () => true),
|
||||
text: vi.fn(async () => "unused"),
|
||||
},
|
||||
{ defaultSelect: "plaintext" },
|
||||
);
|
||||
const runtime = createExitThrowingRuntime();
|
||||
|
||||
const result = await applyAuthChoiceBytePlus({
|
||||
authChoice: "byteplus-api-key",
|
||||
config: {},
|
||||
prompter,
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.config.auth?.profiles?.["byteplus:default"]).toMatchObject({
|
||||
provider: "byteplus",
|
||||
mode: "api_key",
|
||||
});
|
||||
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(agentDir);
|
||||
expect(parsed.profiles?.["byteplus:default"]?.key).toBe("byte-env-key");
|
||||
expect(parsed.profiles?.["byteplus:default"]?.keyRef).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores byteplus env key as keyRef in ref mode", async () => {
|
||||
const agentDir = await setupTempState();
|
||||
process.env.BYTEPLUS_API_KEY = "byte-env-key";
|
||||
|
||||
const prompter = createWizardPrompter(
|
||||
{
|
||||
confirm: vi.fn(async () => true),
|
||||
text: vi.fn(async () => "unused"),
|
||||
},
|
||||
{ defaultSelect: "ref" },
|
||||
);
|
||||
const runtime = createExitThrowingRuntime();
|
||||
|
||||
const result = await applyAuthChoiceBytePlus({
|
||||
authChoice: "byteplus-api-key",
|
||||
config: {},
|
||||
prompter,
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(agentDir);
|
||||
expect(parsed.profiles?.["byteplus:default"]).toMatchObject({
|
||||
keyRef: { source: "env", provider: "default", id: "BYTEPLUS_API_KEY" },
|
||||
});
|
||||
expect(parsed.profiles?.["byteplus:default"]?.key).toBeUndefined();
|
||||
expect(parsed.profiles?.[testCase.profileId]?.key).toBeUndefined();
|
||||
});
|
||||
|
||||
it("stores explicit volcengine key when env is not used", async () => {
|
||||
const agentDir = await setupTempState();
|
||||
const prompter = createWizardPrompter(
|
||||
{
|
||||
confirm: vi.fn(async () => false),
|
||||
text: vi.fn(async () => "volc-manual-key"),
|
||||
},
|
||||
{ defaultSelect: "" },
|
||||
);
|
||||
const runtime = createExitThrowingRuntime();
|
||||
|
||||
const result = await applyAuthChoiceVolcengine({
|
||||
authChoice: "volcengine-api-key",
|
||||
config: {},
|
||||
prompter,
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
const { result, parsed } = await runProviderAuthChoice(providerAuthCases[0], {
|
||||
defaultSelect: "",
|
||||
confirmResult: false,
|
||||
textValue: "volc-manual-key",
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
const parsed = await readAuthProfilesForAgent<{
|
||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||
}>(agentDir);
|
||||
expect(parsed.profiles?.["volcengine:default"]?.key).toBe("volc-manual-key");
|
||||
expect(parsed.profiles?.["volcengine:default"]?.keyRef).toBeUndefined();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user