Files
hose-use/src/cli/commands/app.test.ts
T

184 lines
4.6 KiB
TypeScript

import { describe, it, expect, spyOn } from "bun:test";
import { handleAppCommand } from "./app.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/app > handleAppCommand", () => {
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 handleAppCommand(
{
area: "app",
action: "list",
args: [],
options: {},
raw: ["app", "list"],
},
dummyRuntimeNoCorp
);
expect(res.exitCode).toBe(1);
expect(res.stderr).toContain("未选中企业");
});
it("lists workbench apps in table format", async () => {
const mockApps = [
{
id: "SCGY:corp123",
name: "用车补贴",
code: "dataLinkItem",
href: "/third-party-item/SCGY:corp123",
type: "MENU",
},
{
id: "ID01PIqCA1Di0v",
name: "用车制度",
code: "dataLinkItem",
href: "/third-party-item/ID01PIqCA1Di0v",
type: "REGULATION",
},
];
const spy = spyOn(menuTools, "fetchWorkbenchAppsList").mockResolvedValue(mockApps as any);
const res = await handleAppCommand(
{
area: "app",
action: "list",
args: [],
options: {},
raw: ["app", "list"],
},
dummyRuntimeWithCorp
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("工作台应用管理列表");
expect(res.stdout).toContain("用车补贴");
expect(res.stdout).toContain("用车制度");
expect(res.stdout).toContain("用车/商旅套件");
expect(res.stdout).toContain("制度与规约");
spy.mockRestore();
});
it("filters workbench apps with --keyword", async () => {
const mockApps = [
{
id: "SCGY:corp123",
name: "用车补贴",
code: "dataLinkItem",
href: "/third-party-item/SCGY:corp123",
},
{
id: "ID01PIqCA1Di0v",
name: "差旅申请",
code: "dataLinkItem",
href: "/third-party-item/ID01PIqCA1Di0v",
},
];
const spy = spyOn(menuTools, "fetchWorkbenchAppsList").mockResolvedValue(mockApps as any);
const res = await handleAppCommand(
{
area: "app",
action: "list",
args: [],
options: { keyword: "补贴" },
raw: ["app", "list", "--keyword", "补贴"],
},
dummyRuntimeWithCorp
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("用车补贴");
expect(res.stdout).not.toContain("差旅申请");
spy.mockRestore();
});
it("supports --json output for app list", 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 jsonRuntime = { ...dummyRuntimeWithCorp, jsonMode: true };
const res = await handleAppCommand(
{
area: "app",
action: "list",
args: [],
options: { json: true },
raw: ["app", "list", "--json"],
},
jsonRuntime
);
expect(res.exitCode).toBe(0);
const parsed = JSON.parse(res.stdout);
expect(parsed.total).toBe(1);
expect(parsed.apps[0].id).toBe("SCGY:corp123");
expect(parsed.apps[0].name).toBe("用车补贴");
spy.mockRestore();
});
it("returns error for unknown subcommand", async () => {
const res = await handleAppCommand(
{
area: "app",
action: "unknownAction",
args: [],
options: {},
raw: ["app", "unknownAction"],
},
dummyRuntimeWithCorp
);
expect(res.exitCode).toBe(1);
expect(res.stderr).toContain("未知 app 子命令");
});
});