fix: preserve explicit assist ticket and reject ambiguous recipients
This commit is contained in:
1 parent
3597ec6694
commit
6ce83dd8a7
7 files changed
+339
-56
No files matched your search
@@ -255,9 +255,21 @@ describe("CLI Commands - assist-platform (合思协助平台)", () => {
|
||||
raw: ["assist-platform", "transfer", "ID01ofWoOC1wiX", "ID01Xenwiu9w0D", "--to", "陈龙", "--dry-run"],
|
||||
};
|
||||
|
||||
const res = await handleAssistPlatformCommand(parsed, runtime);
|
||||
expect(res.stdout).toContain("[DRY-RUN]");
|
||||
expect(res.stdout).toContain("ID01Xenwiu9w0D");
|
||||
expect(res.stdout).toContain("陈龙");
|
||||
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;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -13,10 +13,8 @@ export async function handleApprove(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const explicitCorpId =
|
||||
stringOption(parsed.options, ["corp", "corpid", "id"]) ||
|
||||
parsed.args[0] ||
|
||||
runtime.selectedCorp?.id;
|
||||
const corpOption = stringOption(parsed.options, ["corp", "corpid", "id"]);
|
||||
const explicitCorpId = corpOption || parsed.args[0] || runtime.selectedCorp?.id;
|
||||
|
||||
if (!explicitCorpId) {
|
||||
return {
|
||||
@@ -31,22 +29,38 @@ export async function handleApprove(
|
||||
|
||||
// 1. 识别 corpId 与 ticketId
|
||||
const allCorps = runtime.corpContext?.getAllCorps?.() || [];
|
||||
let targetCorp =
|
||||
allCorps.find((c) => c.id === explicitCorpId || c.corpId === explicitCorpId) ||
|
||||
(runtime.selectedCorp?.id === explicitCorpId || runtime.selectedCorp?.corpId === explicitCorpId
|
||||
const findCorp = (id: string) =>
|
||||
allCorps.find((c) => c.id === id || c.corpId === id) ||
|
||||
(runtime.selectedCorp?.id === id || runtime.selectedCorp?.corpId === id
|
||||
? runtime.selectedCorp
|
||||
: undefined);
|
||||
|
||||
let ticketId = stringOption(parsed.options, ["ticket", "ticket-id"]);
|
||||
let targetCorp = findCorp(explicitCorpId);
|
||||
const ticketOption = stringOption(parsed.options, ["ticket", "ticket-id"]);
|
||||
let positionalTicketId: string | undefined;
|
||||
|
||||
// 如果第 0 个参数不是企业,可能是工单编号
|
||||
if (!targetCorp && parsed.args[0]) {
|
||||
ticketId = parsed.args[0];
|
||||
if (corpOption) {
|
||||
// 显式企业选项下,唯一位置参数始终是工单,不能被本地企业命中结果覆盖。
|
||||
positionalTicketId = parsed.args[0];
|
||||
} else if (!targetCorp && parsed.args[0]) {
|
||||
positionalTicketId = parsed.args[0];
|
||||
targetCorp = runtime.selectedCorp ?? undefined;
|
||||
} else if (targetCorp && parsed.args[1]) {
|
||||
ticketId = parsed.args[1];
|
||||
positionalTicketId = parsed.args[1];
|
||||
}
|
||||
|
||||
const maxArgs = !corpOption && parsed.args[0] && findCorp(parsed.args[0]) ? 2 : 1;
|
||||
if (parsed.args.length > maxArgs ||
|
||||
(ticketOption && positionalTicketId && ticketOption !== positionalTicketId)) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "[ERROR] 企业或工单参数存在歧义或冲突。请使用 approve <工单ID> --corp <企业ID>,且 --ticket 必须与位置工单一致。\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
let ticketId = ticketOption || positionalTicketId;
|
||||
|
||||
if (!targetCorp) {
|
||||
return {
|
||||
stdout: "",
|
||||
|
||||
@@ -86,20 +86,11 @@ export async function handleTransfer(
|
||||
try {
|
||||
resolvedRecipient = await resolveTransferRecipient(client, targetRecipient);
|
||||
} catch (err) {
|
||||
if (isDryRun(parsed, runtime)) {
|
||||
resolvedRecipient = {
|
||||
id: targetRecipient,
|
||||
name: targetRecipient,
|
||||
userRoleId: "",
|
||||
roles: [],
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] ${err instanceof Error ? err.message : String(err)}\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] ${err instanceof Error ? err.message : String(err)}\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (!ticketId) {
|
||||
@@ -136,7 +127,7 @@ export async function handleTransfer(
|
||||
parsed,
|
||||
runtime,
|
||||
operation,
|
||||
`hose-use assist-platform transfer ${targetCorpId} ${ticketId} --to "${resolvedRecipient.name}" --yes`
|
||||
`hose-use assist-platform transfer ${targetCorpId} ${ticketId} --to "${resolvedRecipient.id}" --yes`
|
||||
);
|
||||
|
||||
const result = await transferAssistTicket(client, {
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
import { parseArgs } from "../parser.js";
|
||||
import type { CliRuntime } from "../types.js";
|
||||
import { handleApprove } from "./assist-platform/approve.js";
|
||||
import { handleTransfer } from "./assist-platform/transfer.js";
|
||||
|
||||
const corp: CorpConfig = {
|
||||
id: "CORP", corpId: "CORP", name: "测试企业", source: "manual",
|
||||
baseUrl: "https://corp.invalid",
|
||||
credentials: { accessToken: "mock-token", corpId: "CORP", staffId: "ADMIN" },
|
||||
};
|
||||
const otherCorp: CorpConfig = { ...corp, id: "OTHER-CORP", corpId: "OTHER-CORP" };
|
||||
const sameNameUsers = [
|
||||
{ id: "ROLE1", userId: { id: "user3016", name: "张三", code: "3016" } },
|
||||
{ id: "ROLE2", userId: { id: "user3020", name: "张三", code: "3020" } },
|
||||
];
|
||||
|
||||
function runtime(selectedCorp: CorpConfig | null = corp): CliRuntime {
|
||||
return {
|
||||
jsonMode: true, selectedCorp,
|
||||
corpContext: { getAllCorps: () => [corp, otherCorp] } as CliRuntime["corpContext"],
|
||||
};
|
||||
}
|
||||
|
||||
let originalFetch: typeof globalThis.fetch;
|
||||
let originalStore: string | undefined;
|
||||
let stateDir: string;
|
||||
let requests: Request[];
|
||||
let responses: (request: Request) => Response | Promise<Response>;
|
||||
|
||||
beforeEach(() => {
|
||||
originalFetch = globalThis.fetch;
|
||||
originalStore = process.env.HOSE_ASSIST_PLATFORM_FILE;
|
||||
stateDir = mkdtempSync(join(tmpdir(), "hose-assist-targets-"));
|
||||
process.env.HOSE_ASSIST_PLATFORM_FILE = join(stateDir, "session.json");
|
||||
requests = [];
|
||||
responses = () => Response.json({ items: [{ ticketId: "OTHER-TICKET", state: "0" }] });
|
||||
globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
|
||||
const request = input instanceof Request ? input : new Request(input.toString(), init);
|
||||
// Bun 的 Request.clone 声明采用 undici Headers;运行态仍是同一个 Request 类型。
|
||||
requests.push(request.clone() as unknown as Request);
|
||||
return responses(request);
|
||||
}) as typeof globalThis.fetch;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
if (originalStore === undefined) delete process.env.HOSE_ASSIST_PLATFORM_FILE;
|
||||
else process.env.HOSE_ASSIST_PLATFORM_FILE = originalStore;
|
||||
rmSync(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe("assist-platform approve 的明确工单目标", () => {
|
||||
it.each([
|
||||
["REQUESTED", "--corp", "CORP"],
|
||||
["CORP", "REQUESTED"],
|
||||
["CORP", "--ticket", "REQUESTED"],
|
||||
["--corp", "CORP", "--ticket", "REQUESTED"],
|
||||
["REQUESTED"],
|
||||
["--ticket", "REQUESTED"],
|
||||
["REQUESTED", "--corp", "CORP", "--ticket", "REQUESTED"],
|
||||
].map((args) => ({ args })))("预览保留工单参数 %j,不自动查询其他工单", async ({ args }) => {
|
||||
const result = await handleApprove(parseArgs(["assist-platform", "approve", ...args, "--dry-run"]), runtime());
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(JSON.parse(result.stdout).changes["工单编号"]).toBe("REQUESTED");
|
||||
expect(requests).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("显式 --corp 下首参即使也是已知企业 ID,仍作为工单", async () => {
|
||||
const parsed = parseArgs(["assist-platform", "approve", "OTHER-CORP", "--corp", "CORP", "--dry-run"]);
|
||||
const result = await handleApprove(parsed, runtime());
|
||||
expect(JSON.parse(result.stdout).changes["工单编号"]).toBe("OTHER-CORP");
|
||||
expect(requests).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("--yes 真实提交路径只审批显式工单,不能自动替换为列表首项", async () => {
|
||||
responses = (request) => request.method === "PUT"
|
||||
? Response.json({ value: { id: "REQUESTED", corporationId: "CORP" } })
|
||||
: Response.json({ items: [{ ticketId: "OTHER-TICKET", state: "0" }] });
|
||||
const result = await handleApprove(
|
||||
parseArgs(["assist-platform", "approve", "REQUESTED", "--corp", "CORP", "--yes"]), runtime()
|
||||
);
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(requests).toHaveLength(1);
|
||||
expect(requests[0].method).toBe("PUT");
|
||||
expect(requests[0].url).toContain("/$REQUESTED/updateStatus?corpId=CORP");
|
||||
expect(await requests[0].json()).toEqual({ id: "REQUESTED", status: 1 });
|
||||
});
|
||||
|
||||
it.each([
|
||||
["REQUESTED", "--corp", "CORP", "--ticket", "OTHER-TICKET"],
|
||||
["CORP", "REQUESTED", "--ticket", "OTHER-TICKET"],
|
||||
["UNKNOWN-CORP", "REQUESTED"],
|
||||
["CORP", "REQUESTED", "--corp", "OTHER-CORP"],
|
||||
["CORP", "REQUESTED", "EXTRA"],
|
||||
].map((args) => ({ args })))("拒绝歧义或冲突参数 %j,且不查询或提交", async ({ args }) => {
|
||||
const result = await handleApprove(parseArgs(["assist-platform", "approve", ...args, "--yes"]), runtime());
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain("歧义或冲突");
|
||||
expect(requests).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("显式企业未知时不能回退当前企业", async () => {
|
||||
const result = await handleApprove(
|
||||
parseArgs(["assist-platform", "approve", "REQUESTED", "--corp", "UNKNOWN", "--yes"]), runtime()
|
||||
);
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain("UNKNOWN");
|
||||
expect(requests).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("只有未指定工单时才自动查询待授权工单", async () => {
|
||||
const result = await handleApprove(parseArgs([
|
||||
"assist-platform", "approve", "--corp", "CORP", "--token", "mock-token", "--dry-run",
|
||||
]), runtime());
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(JSON.parse(result.stdout).changes["工单编号"]).toBe("OTHER-TICKET");
|
||||
expect(requests).toHaveLength(1);
|
||||
expect(requests[0].url).toContain("assistPlatform/$CORP/search");
|
||||
});
|
||||
});
|
||||
|
||||
describe("assist-platform transfer 预览与执行的收件人解析", () => {
|
||||
it.each(["--dry-run", "--yes"])("%s 遇到同名用户都拒绝且不提交", async (mode) => {
|
||||
responses = () => Response.json({ items: sameNameUsers });
|
||||
const result = await handleTransfer(parseArgs([
|
||||
"assist-platform", "transfer", "CORP", "TICKET", "--to", "张三", "--token", "mock-token", mode,
|
||||
]), runtime());
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain("唯一工号或账号");
|
||||
expect(requests).toHaveLength(1);
|
||||
expect(requests[0].url).toContain("userRole/search");
|
||||
});
|
||||
|
||||
it("查询失败的 dry-run 也不能显示成功解析的预览", async () => {
|
||||
responses = () => new Response("unauthorized", { status: 401 });
|
||||
const result = await handleTransfer(parseArgs([
|
||||
"assist-platform", "transfer", "CORP", "TICKET", "--to", "user3016", "--token", "mock-token", "--dry-run",
|
||||
]), runtime());
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stdout).toBe("");
|
||||
});
|
||||
|
||||
it("唯一工号的 dry-run 显示实际账号,与 --yes 的请求一致", async () => {
|
||||
responses = (request) => request.url.includes("userRole/search")
|
||||
? Response.json({ items: sameNameUsers })
|
||||
: Response.json({ value: true });
|
||||
const args = ["assist-platform", "transfer", "CORP", "TICKET", "--to", "3016", "--token", "mock-token"];
|
||||
const preview = await handleTransfer(parseArgs([...args, "--dry-run"]), runtime());
|
||||
expect(JSON.parse(preview.stdout).changes.recipientId).toBe("user3016");
|
||||
const result = await handleTransfer(parseArgs([...args, "--yes"]), runtime());
|
||||
expect(result.exitCode).toBe(0);
|
||||
const writes = requests.filter((request) => request.url.includes("assistPlatform/transfer"));
|
||||
expect(writes).toHaveLength(1);
|
||||
expect(await writes[0].json()).toEqual({ assistId: "TICKET", recipient: "user3016" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user