fix: 拒绝歧义员工身份与业务字段写入

协助登录按唯一启用员工解析姓名,新增 --staff-id 及完整参数解析覆盖;同名不同员工在换票前拒绝。DataLink 筛选、赋值与 JSON 安全合并共用物理键优先的唯一字段解析,未知原始字段维持透传。
补充无换票/无写入请求的负例、精确目标正例,恢复 CLI 测试环境变量并清理临时目录,同步模块说明。

验证:bun test 902 通过、0 失败;bun run check、git diff --check 通过。全量验证使用隔离配置和数据库,没有调用真实企业接口。
This commit is contained in:
gitea committed 2026-10-29 18:10:21 +08:00
1 parent bb703620f2
commit 9916936eae
12 files changed
+167 -42

No files matched your search

+1 -1
View File
@@ -831,7 +831,7 @@ export const COMMAND_METADATA: Record<string, CliCommandMetadata> = {
"forward": "transfer"
},
flags: {
...flagList("token t user name u hose-token jwt ticket ticket-id auth-code staff alias corp-name count limit n start offset state status corpid id keyword query q message msg reason matter hours hour duration days day to recipient target remarks comment corp", "value"),
...flagList("token t user name u hose-token jwt ticket ticket-id auth-code staff staff-id staffId alias corp-name count limit n start offset state status corpid id keyword query q message msg reason matter hours hour duration days day to recipient target remarks comment corp", "value"),
...flagList("all a logout admin save import persist switch current overwrite candidates list-candidates", "boolean")
}
}
+21 -1
View File
@@ -1,8 +1,9 @@
import { describe, expect, it, beforeEach } from "bun:test";
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,
@@ -28,14 +29,33 @@ function createMockRuntime(enterprisesDir: string): CliRuntime {
}
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 = {
+10 -5
View File
@@ -60,16 +60,18 @@ export async function handleEnter(
if (!corpId) {
return {
stdout: "",
stderr: "用法: hose-use assist-platform enter <企业ID> [--admin] [--staff <姓名>] [--save] [--switch]\n示例: hose-use assist-platform enter Cxa3oOtQi2eS$g --staff 李志财 --save --yes\n",
stderr: "用法: hose-use assist-platform enter <企业ID> [--admin | --staff <姓名> | --staff-id <ID>] [--save] [--switch]\n示例: hose-use assist-platform enter Cxa3oOtQi2eS$g --staff 李志财 --save --yes\n",
exitCode: 1,
};
}
const ticketId = stringOption(parsed.options, ["ticket", "ticket-id", "auth-code"]);
const staffName = stringOption(parsed.options, ["staff", "user", "name"]);
const staffId = stringOption(parsed.options, ["staff-id", "staffId"]);
const forceAdmin = booleanOption(parsed.options, ["admin"]);
// 如果显式指定了 staffName 且未显式指定 --admin,则为用户模式;否则默认为管理员模式
const isAdmin = forceAdmin || !staffName;
if (forceAdmin && (staffName || staffId)) throw new BusinessError("--admin 不能与 --staff / --staff-id 同时使用");
// 如果显式指定了 staffName/staffId 且未显式指定 --admin,则为用户模式;否则默认为管理员模式
const isAdmin = forceAdmin || !(staffName || staffId);
const shouldSave = booleanOption(parsed.options, ["save", "import", "persist"]);
const shouldSwitch = booleanOption(parsed.options, ["switch", "current"]);
@@ -88,6 +90,7 @@ export async function handleEnter(
corpId,
isAdmin,
staffName: staffName || null,
staffId: staffId || null,
ticketId: ticketId || "自动选择最新有效工单",
save: shouldSave || shouldSwitch,
switchCurrent: shouldSwitch,
@@ -105,6 +108,7 @@ export async function handleEnter(
ticketId,
isAdmin,
staffName,
staffId,
});
if (runtime.jsonMode) {
@@ -133,14 +137,14 @@ export async function handleEnter(
`====== 协助平台免密会话生成成功 ======\n${printKeyValue(pairs)}\n` +
`${warningText}\n操作提示:\n` +
` - 在浏览器中打开以上链接可直接进入协助调试工作台\n` +
` - 若要一键落盘到本地企业配置: hose-use assist-platform enter ${corpId} ${sessionRes.isAdmin ? "--admin" : `--staff "${sessionRes.staffName}"`} --save --yes\n`,
` - 若要一键落盘到本地企业配置: hose-use assist-platform enter ${corpId} ${sessionRes.isAdmin ? "--admin" : `--staff-id "${sessionRes.staffId}"`} --save --yes\n`,
stderr: "",
exitCode: 0,
};
}
// 2. 要求持久化落盘 / 切换为工作企业
const nextConfirmCmd = `hose-use assist-platform enter ${corpId} ${isAdmin ? "--admin" : `--staff "${staffName}"`} --save ${shouldSwitch ? "--switch " : ""}--yes`;
const nextConfirmCmd = `hose-use assist-platform enter ${corpId} ${isAdmin ? "--admin" : staffId ? `--staff-id "${staffId}"` : `--staff "${staffName}"`} --save ${shouldSwitch ? "--switch " : ""}--yes`;
requireMutationConfirmation(
parsed,
runtime,
@@ -153,6 +157,7 @@ export async function handleEnter(
ticketId,
isAdmin,
staffName,
staffId,
saveName,
overwrite: overwrite ?? true,
});
@@ -96,6 +96,7 @@ function printHelp(): CliResult {
--token, -t <TOKEN> 显式指定当前操作使用的协助平台 Token
--admin 指定使用企业管理员模式换取免密链接(默认)
--staff <员工姓名> 指定使用特定员工身份换取免密链接
--staff-id <员工ID> 显式指定唯一员工,避免同名歧义
--save 一键将换取的免密会话持久化落盘至本地企业列表(自动提取 OpenAPI 密钥)
--switch 配合 --save,导入后自动切换为当前激活工作企业
--json 输出结构化 JSON 格式结果