276 lines
9.0 KiB
TypeScript
276 lines
9.0 KiB
TypeScript
import { describe, expect, it, beforeEach } from "bun:test";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { handleAssistPlatformCommand } from "./assist-platform/index.js";
|
|
import {
|
|
saveAssistPlatformSession,
|
|
clearAssistPlatformSession,
|
|
} from "../../core/auth/assist-platform-store.js";
|
|
import type { CliParsedArgs, CliRuntime } from "../types.js";
|
|
|
|
function createMockRuntime(enterprisesDir: string): CliRuntime {
|
|
return {
|
|
jsonMode: false,
|
|
selectedCorp: null,
|
|
corpContext: {
|
|
enterprisesDir,
|
|
corps: new Map(),
|
|
stateful: true,
|
|
current: undefined,
|
|
defaultCorpId: undefined,
|
|
getCorp: () => undefined,
|
|
switchCorp: () => true,
|
|
listCorps: () => [],
|
|
getAllCorps: () => [],
|
|
} as any,
|
|
};
|
|
}
|
|
|
|
describe("CLI Commands - assist-platform (合思协助平台)", () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = mkdtempSync(join(tmpdir(), "hose-assist-test-"));
|
|
process.env.HOSE_STATE_DIR = tempDir;
|
|
clearAssistPlatformSession();
|
|
});
|
|
|
|
it("assist-platform help 输出完整使用指南与子命令", async () => {
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "help",
|
|
args: [],
|
|
options: {},
|
|
raw: ["assist-platform", "help"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("合思协助平台");
|
|
expect(res.stdout).toContain("login");
|
|
expect(res.stdout).toContain("search");
|
|
expect(res.stdout).toContain("tickets");
|
|
expect(res.stdout).toContain("enter");
|
|
expect(res.stdout).toContain("assist-center");
|
|
});
|
|
|
|
it("assist-platform login 无凭证时给出指引", async () => {
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "login",
|
|
args: [],
|
|
options: {},
|
|
raw: ["assist-platform", "login"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.exitCode).toBe(1);
|
|
expect(res.stderr).toContain("未检测到协助平台登录凭证");
|
|
});
|
|
|
|
it("assist-platform login --logout 能够注销凭证", async () => {
|
|
saveAssistPlatformSession({
|
|
token: "mock-token",
|
|
userName: "测试人员",
|
|
baseUrl: "https://unity.ekuaibao.com/api",
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "login",
|
|
args: [],
|
|
options: { logout: true },
|
|
raw: ["assist-platform", "login", "--logout"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("已成功注销");
|
|
});
|
|
|
|
it("未登录时运行 tickets 提示需要先登录", async () => {
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "tickets",
|
|
args: ["Cxa3oOtQi2eS$g"],
|
|
options: {},
|
|
raw: ["assist-platform", "tickets", "Cxa3oOtQi2eS$g"],
|
|
};
|
|
|
|
try {
|
|
await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(true).toBe(false);
|
|
} catch (err: any) {
|
|
expect(err.message).toContain("未检测到协助平台登录凭证");
|
|
}
|
|
});
|
|
|
|
it("assist-platform enter --save --dry-run 能够输出变更预览", async () => {
|
|
saveAssistPlatformSession({
|
|
token: "mock-token",
|
|
userName: "测试人员",
|
|
baseUrl: "https://unity.ekuaibao.com/api",
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "enter",
|
|
args: ["Cxa3oOtQi2eS$g"],
|
|
options: { save: true, dryRun: true },
|
|
raw: ["assist-platform", "enter", "Cxa3oOtQi2eS$g", "--save", "--dry-run"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.stdout).toContain("[DRY-RUN]");
|
|
expect(res.stdout).toContain("Cxa3oOtQi2eS$g");
|
|
});
|
|
|
|
it("assist-platform apply --dry-run 能够输出申请工单变更预览", async () => {
|
|
saveAssistPlatformSession({
|
|
token: "mock-token",
|
|
userName: "测试人员",
|
|
baseUrl: "https://unity.ekuaibao.com/api",
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "apply",
|
|
args: ["ID01KrLMQJWdMH"],
|
|
options: { dryRun: true, hours: 12, message: "测试联调" },
|
|
raw: ["assist-platform", "apply", "ID01KrLMQJWdMH", "--dry-run", "--hours", "12"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.stdout).toContain("[DRY-RUN]");
|
|
expect(res.stdout).toContain("ID01KrLMQJWdMH");
|
|
expect(res.stdout).toContain("12 小时");
|
|
});
|
|
|
|
it("assist-platform approve --dry-run 能够输出工单审批变更预览", async () => {
|
|
const runtime = createMockRuntime(tempDir);
|
|
runtime.selectedCorp = {
|
|
id: "ID01ofWoOC1wiX",
|
|
name: "沈玥妍测试企业",
|
|
corpId: "ID01ofWoOC1wiX",
|
|
credentials: { accessToken: "mock-token" },
|
|
} as any;
|
|
|
|
const parsed: CliParsedArgs = {
|
|
action: "approve",
|
|
args: ["ID01Xeno3GpoNV"],
|
|
options: { dryRun: true },
|
|
raw: ["assist-platform", "approve", "ID01Xeno3GpoNV", "--dry-run"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.stdout).toContain("[DRY-RUN]");
|
|
expect(res.stdout).toContain("ID01Xeno3GpoNV");
|
|
expect(res.stdout).toContain("同意授权");
|
|
});
|
|
|
|
it("assist-platform apply 传入超过 24 小时或多天时返回明确拒绝提示", async () => {
|
|
saveAssistPlatformSession({
|
|
token: "mock-token",
|
|
userName: "测试人员",
|
|
baseUrl: "https://unity.ekuaibao.com/api",
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "apply",
|
|
args: ["ID01KrLMQJWdMH"],
|
|
options: { days: 2 },
|
|
raw: ["assist-platform", "apply", "ID01KrLMQJWdMH", "--days", "2"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.exitCode).toBe(1);
|
|
expect(res.stderr).toContain("申请协助时长不能超过 24 小时 (1 天)");
|
|
expect(res.stderr).toContain("2 天 (48 小时)");
|
|
expect(res.stderr).toContain("POST /api/assistPlatform/create");
|
|
expect(res.stderr).toContain("单次协助工单最长有效期限为 24 小时");
|
|
});
|
|
|
|
it("assist-platform apply --days 1 --dry-run 成功换算为 24 小时并预览", async () => {
|
|
saveAssistPlatformSession({
|
|
token: "mock-token",
|
|
userName: "测试人员",
|
|
baseUrl: "https://unity.ekuaibao.com/api",
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "apply",
|
|
args: ["ID01KrLMQJWdMH"],
|
|
options: { days: 1, dryRun: true },
|
|
raw: ["assist-platform", "apply", "ID01KrLMQJWdMH", "--days", "1", "--dry-run"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("[DRY-RUN]");
|
|
expect(res.stdout).toContain("24 小时");
|
|
});
|
|
|
|
it("assist-platform transfer 缺少 --to 参数时报错提示", async () => {
|
|
saveAssistPlatformSession({
|
|
token: "mock-token",
|
|
userName: "测试人员",
|
|
baseUrl: "https://unity.ekuaibao.com/api",
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "transfer",
|
|
args: ["ID01ofWoOC1wiX"],
|
|
options: {},
|
|
raw: ["assist-platform", "transfer", "ID01ofWoOC1wiX"],
|
|
};
|
|
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.exitCode).toBe(1);
|
|
expect(res.stderr).toContain("必须通过 --to 指定接收人员姓名、工号或账号");
|
|
});
|
|
|
|
it("assist-platform transfer --dry-run 能够输出工单转交变更预览", async () => {
|
|
saveAssistPlatformSession({
|
|
token: "mock-token",
|
|
userName: "测试人员",
|
|
baseUrl: "https://unity.ekuaibao.com/api",
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const runtime = createMockRuntime(tempDir);
|
|
const parsed: CliParsedArgs = {
|
|
action: "transfer",
|
|
args: ["ID01ofWoOC1wiX", "ID01Xenwiu9w0D"],
|
|
options: { to: "陈龙", dryRun: true },
|
|
raw: ["assist-platform", "transfer", "ID01ofWoOC1wiX", "ID01Xenwiu9w0D", "--to", "陈龙", "--dry-run"],
|
|
};
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = (async () => Response.json({
|
|
items: [{
|
|
id: "role-chenlong",
|
|
userId: { id: "chenlong3016", name: "陈龙", code: "3016" },
|
|
}],
|
|
})) as unknown as typeof globalThis.fetch;
|
|
try {
|
|
const res = await handleAssistPlatformCommand(parsed, runtime);
|
|
expect(res.stdout).toContain("[DRY-RUN]");
|
|
expect(res.stdout).toContain("ID01Xenwiu9w0D");
|
|
expect(res.stdout).toContain("陈龙");
|
|
expect(res.stdout).toContain("chenlong3016");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
});
|