145 lines
4.6 KiB
TypeScript
145 lines
4.6 KiB
TypeScript
import { describe, expect, test, spyOn } from "bun:test";
|
|
import { handleKeysCommand } from "./keys.js";
|
|
import { handleOpenApiCommand } from "./openapi.js";
|
|
import type { CliRuntime, CliParsedArgs } from "../types.js";
|
|
import * as storeTools from "../../domains/store/tools.js";
|
|
|
|
describe("CLI - keys 顶级命令与凭证提取", () => {
|
|
const dummyCorp: any = {
|
|
id: "test-corp-id",
|
|
name: "测试企业A",
|
|
corpId: "test-corp-id",
|
|
credentials: { corpId: "test-corp-id", accessToken: "token123" },
|
|
};
|
|
|
|
const createRuntime = (corp: any = dummyCorp, jsonMode = false): CliRuntime => ({
|
|
corpContext: {} as any,
|
|
selectedCorp: corp,
|
|
jsonMode,
|
|
});
|
|
|
|
test("未选中企业时返回提示并报错", async () => {
|
|
const runtime = createRuntime(null);
|
|
const parsed: CliParsedArgs = { area: "keys", options: {}, args: [], raw: ["keys"] };
|
|
const res = await handleKeysCommand(parsed, runtime);
|
|
expect(res.exitCode).toBe(1);
|
|
expect(res.stderr).toContain("未选中企业");
|
|
});
|
|
|
|
test("keys 默认脱敏显示 appSecurity 接入密码", async () => {
|
|
const spy = spyOn(storeTools, "fetchOpenApiKeys").mockImplementation(() =>
|
|
Promise.resolve([
|
|
{
|
|
id: "k-1",
|
|
name: "开发测试密钥",
|
|
appKey: "545f8b6a-75a7-4f99-86e1-bca747c985e7",
|
|
appSecurity: "9bc91727-e96e-4924-abcb-d8ed77557f58",
|
|
active: true,
|
|
createTime: 1751357077503,
|
|
updateTime: 1769063905460,
|
|
},
|
|
])
|
|
);
|
|
|
|
const runtime = createRuntime(dummyCorp);
|
|
const parsed: CliParsedArgs = { area: "keys", options: {}, args: [], raw: ["keys"] };
|
|
const res = await handleKeysCommand(parsed, runtime);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("开发测试密钥");
|
|
expect(res.stdout).toContain("545f8b6a-75a7-4f99-86e1-bca747c985e7");
|
|
// 默认脱敏
|
|
expect(res.stdout).toContain("9bc91727...7f58");
|
|
expect(res.stdout).not.toContain("9bc91727-e96e-4924-abcb-d8ed77557f58");
|
|
spy.mockRestore();
|
|
});
|
|
|
|
test("keys --reveal 显式输出完整接入密码明文", async () => {
|
|
const spy = spyOn(storeTools, "fetchOpenApiKeys").mockImplementation(() =>
|
|
Promise.resolve([
|
|
{
|
|
id: "k-1",
|
|
name: "生产密钥",
|
|
appKey: "key-full-123",
|
|
appSecurity: "secret-super-long-plain-text",
|
|
active: true,
|
|
createTime: 1751357077503,
|
|
updateTime: 1769063905460,
|
|
},
|
|
])
|
|
);
|
|
|
|
const runtime = createRuntime(dummyCorp);
|
|
const parsed: CliParsedArgs = {
|
|
area: "keys",
|
|
options: { reveal: true },
|
|
args: [],
|
|
raw: ["keys", "--reveal"],
|
|
};
|
|
const res = await handleKeysCommand(parsed, runtime);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("secret-super-long-plain-text");
|
|
spy.mockRestore();
|
|
});
|
|
|
|
test("keys --json 模式正常序列化", async () => {
|
|
const spy = spyOn(storeTools, "fetchOpenApiKeys").mockImplementation(() =>
|
|
Promise.resolve([
|
|
{
|
|
id: "k-1",
|
|
name: "JSON密钥",
|
|
appKey: "json-app-key",
|
|
appSecurity: "secret-for-json-test",
|
|
active: true,
|
|
createTime: 1751357077503,
|
|
updateTime: 1769063905460,
|
|
},
|
|
])
|
|
);
|
|
|
|
const runtime = createRuntime(dummyCorp, true);
|
|
const parsed: CliParsedArgs = { area: "keys", options: { json: true }, args: [], raw: ["keys", "--json"] };
|
|
const res = await handleKeysCommand(parsed, runtime);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
const data = JSON.parse(res.stdout);
|
|
expect(Array.isArray(data)).toBe(true);
|
|
expect(data[0].appKey).toBe("json-app-key");
|
|
// 默认 json 脱敏
|
|
expect(data[0].appSecurity).toContain("...");
|
|
spy.mockRestore();
|
|
});
|
|
|
|
test("openapi keys 子命令平滑转发至同一执行器", async () => {
|
|
const spy = spyOn(storeTools, "fetchOpenApiKeys").mockImplementation(() =>
|
|
Promise.resolve([
|
|
{
|
|
id: "k-1",
|
|
name: "OpenAPI子命令测试",
|
|
appKey: "sub-app-key",
|
|
appSecurity: "sub-app-security-123456",
|
|
active: true,
|
|
createTime: 1751357077503,
|
|
updateTime: 1769063905460,
|
|
},
|
|
])
|
|
);
|
|
|
|
const runtime = createRuntime(dummyCorp);
|
|
const parsed: CliParsedArgs = {
|
|
area: "openapi",
|
|
action: "keys",
|
|
options: {},
|
|
args: [],
|
|
raw: ["openapi", "keys"],
|
|
};
|
|
const res = await handleOpenApiCommand(parsed, runtime);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("OpenAPI子命令测试");
|
|
expect(res.stdout).toContain("sub-app-key");
|
|
spy.mockRestore();
|
|
});
|
|
});
|