import { describe, it, expect, spyOn } from "bun:test"; import { handleMenuCommand } from "./menu.js"; import * as menuTools from "../../domains/menu/tools.js"; import type { CliRuntime } from "../types.js"; import type { CorpConfig } from "../../core/config/types.js"; describe("cli/commands/menu > handleMenuCommand", () => { const dummyRuntimeNoCorp: CliRuntime = { corpContext: { listCorps: () => [], getCurrent: () => null, switchCorp: () => false, resolve: () => null, } as any, selectedCorp: null, jsonMode: false, }; const mockCorp: CorpConfig = { id: "corp123", name: "转转二手交易网", source: "account", baseUrl: "https://app.ekuaibao.com", credentials: { accessToken: "mock-token", corpId: "corp123", staffId: "mock-staff", }, }; const dummyRuntimeWithCorp: CliRuntime = { corpContext: { listCorps: () => [mockCorp], getCurrent: () => mockCorp, switchCorp: () => true, resolve: () => mockCorp, } as any, selectedCorp: mockCorp, jsonMode: false, }; it("fails with exitCode 1 when no corp is selected", async () => { const res = await handleMenuCommand( { area: "menu", action: "list", args: [], options: {}, raw: ["menu", "list"], }, dummyRuntimeNoCorp ); expect(res.exitCode).toBe(1); expect(res.stderr).toContain("未选中企业"); }); it("lists desktop menus in table format", async () => { const mockMenus = [ { id: "newHomePage", label: "首页", appId: "100-home", href: "/home", children: [], }, { id: "dataLink", label: "应用管理", appId: "160-datalink-platform", href: "/datalink", children: [ { id: "sub1", label: "用车补贴" }, { id: "sub2", label: "用车制度" }, ], }, ]; const spy = spyOn(menuTools, "fetchDesktopMenus").mockResolvedValue(mockMenus as any); const res = await handleMenuCommand( { area: "menu", action: "list", args: [], options: {}, raw: ["menu", "list"], }, dummyRuntimeWithCorp ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("桌面端主框架导航与模块列表"); expect(res.stdout).toContain("首页"); expect(res.stdout).toContain("应用管理"); expect(res.stdout).toContain("160-datalink-platform"); spy.mockRestore(); }); it("supports --json output for menu list", async () => { const mockMenus = [ { id: "newHomePage", label: "首页", appId: "100-home", href: "/home", }, ]; const spy = spyOn(menuTools, "fetchDesktopMenus").mockResolvedValue(mockMenus as any); const jsonRuntime = { ...dummyRuntimeWithCorp, jsonMode: true }; const res = await handleMenuCommand( { area: "menu", action: "list", args: [], options: { json: true }, raw: ["menu", "list", "--json"], }, jsonRuntime ); expect(res.exitCode).toBe(0); const parsed = JSON.parse(res.stdout); expect(parsed.count).toBe(1); expect(parsed.menus[0].id).toBe("newHomePage"); spy.mockRestore(); }); it("delegates menu apps to handleAppCommand", async () => { const mockApps = [ { id: "SCGY:corp123", name: "用车补贴", code: "dataLinkItem", href: "/third-party-item/SCGY:corp123", }, ]; const spy = spyOn(menuTools, "fetchWorkbenchAppsList").mockResolvedValue(mockApps as any); const res = await handleMenuCommand( { area: "menu", action: "apps", args: [], options: {}, raw: ["menu", "apps"], }, dummyRuntimeWithCorp ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("工作台应用管理列表"); expect(res.stdout).toContain("用车补贴"); spy.mockRestore(); }); it("returns error for unknown menu subcommand", async () => { const res = await handleMenuCommand( { area: "menu", action: "badsub", args: [], options: {}, raw: ["menu", "badsub"], }, dummyRuntimeWithCorp ); expect(res.exitCode).toBe(1); expect(res.stderr).toContain("未知 menu 子命令"); }); });