协助登录按唯一启用员工解析姓名,新增 --staff-id 及完整参数解析覆盖;同名不同员工在换票前拒绝。DataLink 筛选、赋值与 JSON 安全合并共用物理键优先的唯一字段解析,未知原始字段维持透传。 补充无换票/无写入请求的负例、精确目标正例,恢复 CLI 测试环境变量并清理临时目录,同步模块说明。 验证:bun test 902 通过、0 失败;bun run check、git diff --check 通过。全量验证使用隔离配置和数据库,没有调用真实企业接口。
296 lines
10 KiB
TypeScript
296 lines
10 KiB
TypeScript
import { describe, expect, it, beforeEach, afterEach } 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 { parseArgs } from "../parser.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 (合思协助平台)", () => {
|
|
it("enter 的 staff-id 通过完整解析并选择员工模式预览", async () => {
|
|
const parsed = parseArgs(["assist-platform", "enter", "corp", "--staff-id", "staff-two", "--dry-run", "--json"]);
|
|
const result = await handleAssistPlatformCommand(parsed, { jsonMode: true } as CliRuntime);
|
|
expect(result.exitCode).toBe(0);
|
|
expect(JSON.parse(result.stdout).changes).toMatchObject({ staffId: "staff-two", isAdmin: false });
|
|
});
|
|
|
|
it("enter 不允许管理员模式覆盖显式员工 ID", async () => {
|
|
const parsed = parseArgs(["assist-platform", "enter", "corp", "--staff-id", "staff-two", "--admin", "--dry-run"]);
|
|
await expect(handleAssistPlatformCommand(parsed, {} as CliRuntime)).rejects.toThrow("不能与");
|
|
});
|
|
let tempDir: string;
|
|
let previousStateDir: string | undefined;
|
|
|
|
beforeEach(() => {
|
|
previousStateDir = process.env.HOSE_STATE_DIR;
|
|
tempDir = mkdtempSync(join(tmpdir(), "hose-assist-test-"));
|
|
process.env.HOSE_STATE_DIR = tempDir;
|
|
clearAssistPlatformSession();
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (previousStateDir === undefined) delete process.env.HOSE_STATE_DIR;
|
|
else process.env.HOSE_STATE_DIR = previousStateDir;
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
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;
|
|
}
|
|
});
|
|
});
|