import { describe, expect, test, spyOn } from "bun:test"; import { handleStoreCommand } from "./store.js"; import type { CliRuntime, CliParsedArgs } from "../types.js"; import * as storeTools from "../../domains/store/tools.js"; describe("CLI - store 应用中心与应用商店探查", () => { 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: "store", options: {}, args: [], raw: ["store"] }; const res = await handleStoreCommand(parsed, runtime); expect(res.exitCode).toBe(1); expect(res.stderr).toContain("未选中企业"); }); test("store list 展示企业已购应用列表", async () => { const spy = spyOn(storeTools, "fetchClientGoods").mockImplementation(() => Promise.resolve([ { id: "g-1", name: "开放接口(新)", status: "OPENED", expireTime: 1914422399000, hasConfig: true, powers: [{ powerCode: "219904", powerName: "开放接口(新)" }], }, ] as any) ); const runtime = createRuntime(dummyCorp); const parsed: CliParsedArgs = { area: "store", action: "list", options: {}, args: [], raw: ["store", "list"], }; const res = await handleStoreCommand(parsed, runtime); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("开放接口(新)"); expect(res.stdout).toContain("已开通"); expect(res.stdout).toContain("支持配置"); spy.mockRestore(); }); test("store search 检索应用市场商品", async () => { const spy = spyOn(storeTools, "searchGoods").mockImplementation(() => Promise.resolve({ total: 1, items: [ { id: "g-2", name: "纷享销客基础互联应用", status: "OPENED", summary: "连接纷享销客", }, ], } as any) ); const runtime = createRuntime(dummyCorp); const parsed: CliParsedArgs = { area: "store", action: "search", options: {}, args: ["纷享"], raw: ["store", "search", "纷享"], }; const res = await handleStoreCommand(parsed, runtime); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("纷享销客基础互联应用"); expect(res.stdout).toContain("连接纷享销客"); spy.mockRestore(); }); test("store get 查看应用详情", async () => { const spy = spyOn(storeTools, "fetchGoodsDetail").mockImplementation(() => Promise.resolve({ id: "g-1", name: "开放接口(新)", status: "OPENED", hasConfig: true, configStyle: "SIDESLIP", powers: [{ powerCode: "219904", powerName: "开放接口(新)" }], summary: "OpenAPI提供标准化接口", } as any) ); const runtime = createRuntime(dummyCorp); const parsed: CliParsedArgs = { area: "store", action: "get", options: {}, args: ["g-1"], raw: ["store", "get", "g-1"], }; const res = await handleStoreCommand(parsed, runtime); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("开放接口(新)"); expect(res.stdout).toContain("219904"); expect(res.stdout).toContain("SIDESLIP"); spy.mockRestore(); }); test("store keys 转发至 keys 执行器", async () => { const spy = spyOn(storeTools, "fetchOpenApiKeys").mockImplementation(() => Promise.resolve([ { id: "k-1", name: "Store转发密钥", appKey: "store-key-123", appSecurity: "store-secret-456", active: true, createTime: 1751357077503, updateTime: 1769063905460, }, ]) ); const runtime = createRuntime(dummyCorp); const parsed: CliParsedArgs = { area: "store", action: "keys", options: {}, args: [], raw: ["store", "keys"], }; const res = await handleStoreCommand(parsed, runtime); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("Store转发密钥"); expect(res.stdout).toContain("store-key-123"); spy.mockRestore(); }); });