- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名: * session-client.ts ➔ hose-web-session-client.ts (合思 Web 主站会话客户端) * script-engine-client.ts ➔ piper-workflow-client.ts (Piper 工作流引擎客户端) * unity-client.ts ➔ unity-easo-client.ts (Unity / EASO 运营中台与动态集群客户端) * sso-client.ts ➔ sso-auth-client.ts (SSO 与扫码鉴权客户端) - 新增 hose-docs-client.ts 纳为第 7 大通信底座客户端,收口 docs.ekuaibao.com 文档站点通信 - 端正 Piper 工作流引擎定位,完整覆盖工作流创建、保存、发布、108 类节点编排与 Job 流转 - 彻底整体替换全仓 import 引用与测试套件,新增 clients-semantics.test.ts 自动化校验 - 全量自动化测试 150 files 1155 tests 全部通过,类型检查零错误
505 lines
16 KiB
TypeScript
505 lines
16 KiB
TypeScript
import { describe, it, expect } from "bun:test";
|
|
import { parseHeadersOrCurl } from "../../core/auth/login.js";
|
|
import { resolveCorpCluster } from "../../core/http/unity-easo-client.js";
|
|
import { handleCorpsCommand } from "./corps/index.js";
|
|
import type { CliRuntime } from "../types.js";
|
|
import type { CorpConfig } from "../../core/config/types.js";
|
|
|
|
describe("core/auth/login > parseHeadersOrCurl & URL link parsing", () => {
|
|
it("successfully parses standard thirdparty.html assistance links", () => {
|
|
const url = "https://wx2.ekuaibao.com/web/thirdparty.html?accessToken=token123&ekbCorpId=corpABC&pageType=home";
|
|
const parsed = parseHeadersOrCurl(url);
|
|
|
|
expect(parsed.baseUrl).toBe("https://wx2.ekuaibao.com");
|
|
expect(parsed.accessToken).toBe("token123");
|
|
expect(parsed.corpId).toBe("corpABC");
|
|
});
|
|
|
|
it("extracts custom staffId and standard corpId query params", () => {
|
|
const url = "https://app.ekuaibao.com/thirdparty.html?token=secret999&corpId=corpXYZ&staffId=corpXYZ:user1";
|
|
const parsed = parseHeadersOrCurl(url);
|
|
|
|
expect(parsed.baseUrl).toBe("https://app.ekuaibao.com");
|
|
expect(parsed.accessToken).toBe("secret999");
|
|
expect(parsed.corpId).toBe("corpXYZ");
|
|
expect(parsed.staffId).toBe("corpXYZ:user1");
|
|
});
|
|
|
|
it("handles curl command lines with assistance URL", () => {
|
|
const curl = `curl "https://wx.ekuaibao.com/web/thirdparty.html?accessToken=testToken&ekbCorpId=testCorp" -H "User-Agent: Mozilla"`;
|
|
const parsed = parseHeadersOrCurl(curl);
|
|
|
|
expect(parsed.baseUrl).toBe("https://wx.ekuaibao.com");
|
|
expect(parsed.accessToken).toBe("testToken");
|
|
expect(parsed.corpId).toBe("testCorp");
|
|
});
|
|
});
|
|
|
|
describe("core/http/unity-easo-client > resolveCorpCluster", () => {
|
|
it("prioritizes explicit cluster config", () => {
|
|
expect(resolveCorpCluster({ cluster: "wx2", baseUrl: "https://app.ekuaibao.com" })).toBe("wx2");
|
|
});
|
|
|
|
it("extracts cluster from hostname subdomain", () => {
|
|
expect(resolveCorpCluster({ baseUrl: "https://wx2.ekuaibao.com" })).toBe("wx2");
|
|
expect(resolveCorpCluster({ baseUrl: "https://wx.ekuaibao.com" })).toBe("wx");
|
|
expect(resolveCorpCluster({ baseUrl: "https://app.ekuaibao.com" })).toBe("app");
|
|
});
|
|
|
|
it("falls back to app when unknown", () => {
|
|
expect(resolveCorpCluster({})).toBe("app");
|
|
expect(resolveCorpCluster({ baseUrl: "https://custom-domain.com" })).toBe("app");
|
|
});
|
|
});
|
|
|
|
describe("cli/commands/corps > handleCorpsCommand add-link", () => {
|
|
const dummyRuntime: CliRuntime = {
|
|
corpContext: {
|
|
listCorps: () => [],
|
|
getCurrent: () => null,
|
|
switchCorp: () => false,
|
|
resolve: () => null,
|
|
} as any,
|
|
selectedCorp: null,
|
|
jsonMode: false,
|
|
};
|
|
|
|
it("returns error exit code when no link URL is provided", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "add-link",
|
|
args: [],
|
|
options: {},
|
|
raw: ["corps", "add-link"],
|
|
},
|
|
dummyRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(1);
|
|
expect(res.stderr).toContain("必须提供协助/免密链接 URL");
|
|
});
|
|
});
|
|
|
|
describe("cli/commands/corps > handleCorpsCommand search & aliases", () => {
|
|
const mockCorps = [
|
|
{
|
|
id: "corpAlpha",
|
|
name: "阿尔法网络科技有限公司",
|
|
corpId: "alphaPhysicalId",
|
|
staffId: "STAFF_001",
|
|
staffName: "张三",
|
|
accountId: "13800000001",
|
|
source: "account",
|
|
baseUrl: "https://app.ekuaibao.com",
|
|
credentials: {
|
|
accessToken: "token-alpha",
|
|
corpId: "alphaPhysicalId",
|
|
staffId: "STAFF_001",
|
|
},
|
|
},
|
|
{
|
|
id: "corpBeta@13900000002",
|
|
name: "贝塔云端信息系统",
|
|
corpId: "corpBeta",
|
|
staffId: "STAFF_002",
|
|
staffName: "李四",
|
|
accountId: "13900000002",
|
|
source: "link",
|
|
baseUrl: "https://wx2.ekuaibao.com",
|
|
credentials: {
|
|
accessToken: "token-beta",
|
|
corpId: "corpBeta",
|
|
staffId: "STAFF_002",
|
|
},
|
|
session: {
|
|
staffName: "李四",
|
|
cellphone: "13900000002",
|
|
},
|
|
},
|
|
];
|
|
|
|
const searchRuntime: CliRuntime = {
|
|
corpContext: {
|
|
getAllCorps: () => mockCorps,
|
|
listCorps: () => mockCorps.map((c) => ({
|
|
id: c.id,
|
|
name: c.name,
|
|
corpId: c.corpId,
|
|
staffId: c.staffId,
|
|
staffName: c.staffName,
|
|
accountId: c.accountId,
|
|
source: c.source,
|
|
current: c.id === "corpAlpha",
|
|
})),
|
|
getCurrent: () => mockCorps[0],
|
|
switchCorp: (id: string) => mockCorps.some((c) => c.id === id),
|
|
resolve: () => mockCorps[0],
|
|
} as any,
|
|
selectedCorp: mockCorps[0] as any,
|
|
jsonMode: false,
|
|
};
|
|
|
|
it("returns error exit code when search keyword is empty", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: [],
|
|
options: {},
|
|
raw: ["corps", "search"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(1);
|
|
expect(res.stderr).toContain("用法: hose-use corps search <关键词>");
|
|
});
|
|
|
|
it("searches by name with case-insensitive substring match", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: ["阿尔法"],
|
|
options: {},
|
|
raw: ["corps", "search", "阿尔法"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("[阿尔法网络科技有限公司]");
|
|
expect(res.stdout).toContain("命中企业名称");
|
|
expect(res.stdout).toContain("* 1"); // 当前激活标记
|
|
});
|
|
|
|
it("searches by staffName and highlights the matched cell", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: ["李四"],
|
|
options: {},
|
|
raw: ["corps", "search", "李四"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("[李四]");
|
|
expect(res.stdout).toContain("命中员工姓名");
|
|
expect(res.stdout).toContain("corpBeta");
|
|
});
|
|
|
|
it("searches by staffId case-insensitively", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: ["staff_001"],
|
|
options: {},
|
|
raw: ["corps", "search", "staff_001"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("[STAFF_001]");
|
|
expect(res.stdout).toContain("命中员工工号");
|
|
});
|
|
|
|
it("searches by physical corpId", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: ["alphaPhysicalId"],
|
|
options: {},
|
|
raw: ["corps", "search", "alphaPhysicalId"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("阿尔法网络科技有限公司");
|
|
expect(res.stdout).toContain("命中企业ID");
|
|
});
|
|
|
|
it("searches by accountId", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: ["13900000002"],
|
|
options: {},
|
|
raw: ["corps", "search", "13900000002"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("[13900000002]");
|
|
expect(res.stdout).toContain("命中所属账号");
|
|
});
|
|
|
|
it("supports find alias for search", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "find",
|
|
args: ["阿尔法"],
|
|
options: {},
|
|
raw: ["corps", "find", "阿尔法"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("[阿尔法网络科技有限公司]");
|
|
});
|
|
|
|
it("outputs friendly message when no corps match", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: ["nonexistent_keyword"],
|
|
options: {},
|
|
raw: ["corps", "search", "nonexistent_keyword"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toBe("未找到匹配 \"nonexistent_keyword\" 的企业配置\n");
|
|
});
|
|
|
|
it("supports --json mode with structured output", async () => {
|
|
const jsonRuntime = { ...searchRuntime, jsonMode: true };
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "search",
|
|
args: ["阿尔法"],
|
|
options: { json: true },
|
|
raw: ["corps", "search", "阿尔法", "--json"],
|
|
},
|
|
jsonRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
const data = JSON.parse(res.stdout);
|
|
expect(data.keyword).toBe("阿尔法");
|
|
expect(data.count).toBe(1);
|
|
expect(data.activeCorpId).toBe("corpAlpha");
|
|
expect(data.corporations).toHaveLength(1);
|
|
expect(data.corporations[0].id).toBe("corpAlpha");
|
|
expect(data.corporations[0].name).toBe("阿尔法网络科技有限公司");
|
|
expect(data.corporations[0].matchedFields).toContain("name");
|
|
});
|
|
|
|
it("outputs help message via corps help", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "help",
|
|
args: [],
|
|
options: {},
|
|
raw: ["corps", "help"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("hose-use corps <子命令>");
|
|
expect(res.stdout).toContain("search <关键词>");
|
|
expect(res.stdout).toContain("find");
|
|
});
|
|
|
|
it("generates login link via corps link [corp] with preserveSensitiveStdout=true", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = (async () =>
|
|
new Response(JSON.stringify({ items: [] }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
})) as unknown as typeof fetch;
|
|
|
|
try {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "link",
|
|
args: ["corpAlpha"],
|
|
options: {},
|
|
raw: ["corps", "link", "corpAlpha"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.preserveSensitiveStdout).toBe(true);
|
|
expect(res.stdout).toContain("thirdparty.html?accessToken=token-alpha&ekbCorpId=alphaPhysicalId");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
it("supports dynamic staff switching via corps link [corp] --staff=<id>", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const corp = structuredClone(mockCorps[0]);
|
|
Object.assign(corp.credentials, { appKey: "staff-switch-key", appSecurity: "staff-switch-security" });
|
|
const dynamicRuntime = { ...searchRuntime, selectedCorp: corp, corpContext: { ...searchRuntime.corpContext, resolve: () => corp } } as CliRuntime;
|
|
globalThis.fetch = (async (input: string | URL | Request) => {
|
|
const url = input instanceof Request ? input.url : String(input);
|
|
const body = url.includes("getAccessToken")
|
|
? { value: { accessToken: "openapi-staff-switch", corporationId: "alphaPhysicalId", expireTime: Date.now() + 7200000 } }
|
|
: url.includes("getProvisionalAuth")
|
|
? { value: { message: "https://app.ekuaibao.com/web/thirdparty.html?accessToken=token-new-staff&ekbCorpId=alphaPhysicalId" } }
|
|
: { items: [] };
|
|
return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
|
|
}) as typeof fetch;
|
|
|
|
try {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "link",
|
|
args: ["corpAlpha"],
|
|
options: { staff: "alphaPhysicalId:NEW_ADMIN_STAFF" },
|
|
raw: ["corps", "link", "corpAlpha", "--staff=alphaPhysicalId:NEW_ADMIN_STAFF"],
|
|
},
|
|
dynamicRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.preserveSensitiveStdout).toBe(true);
|
|
expect(res.stdout).toContain("NEW_ADMIN_STAFF");
|
|
expect(res.stdout).toContain("动态换签");
|
|
|
|
// --json 模式验证
|
|
const jsonRuntime = { ...dynamicRuntime, jsonMode: true };
|
|
const jsonRes = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "link",
|
|
args: ["corpAlpha"],
|
|
options: { staff: "alphaPhysicalId:NEW_ADMIN_STAFF", json: true },
|
|
raw: ["corps", "link", "corpAlpha", "--staff=alphaPhysicalId:NEW_ADMIN_STAFF", "--json"],
|
|
},
|
|
jsonRuntime
|
|
);
|
|
expect(jsonRes.exitCode).toBe(0);
|
|
const parsed = JSON.parse(jsonRes.stdout);
|
|
expect(parsed.dynamicStaff).toBe(true);
|
|
expect(parsed.staffId).toBe("alphaPhysicalId:NEW_ADMIN_STAFF");
|
|
expect(corp.credentials.accessToken).toBe("token-alpha");
|
|
expect(corp.credentials.staffId).toBe("STAFF_001");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
it("supports corps add-persistent --dry-run", async () => {
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "add-persistent",
|
|
args: [],
|
|
options: {
|
|
"app-key": "key-1",
|
|
"app-security": "sec-1",
|
|
"dry-run": true,
|
|
},
|
|
raw: ["corps", "add-persistent", "--app-key=key-1", "--app-security=sec-1", "--dry-run"],
|
|
},
|
|
searchRuntime
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
expect(res.stdout).toContain("通过 OpenAPI 持久化密钥接入企业");
|
|
});
|
|
|
|
it("supports corps hose-staffs with auto-resolved corp credentials", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
try {
|
|
globalThis.fetch = (async (input: unknown) => {
|
|
const urlStr = typeof input === "string" ? input : input instanceof URL ? input.toString() : (input as { url: string }).url;
|
|
if (urlStr.includes("/api/openapi/v1/auth/getAccessToken")) {
|
|
return new Response(JSON.stringify({
|
|
value: {
|
|
accessToken: "mock-open-token",
|
|
corporationId: "corpKeyed",
|
|
corporation: { id: "corpKeyed", name: "已配密钥企业" },
|
|
},
|
|
}), { status: 200 });
|
|
}
|
|
if (urlStr.includes("/api/openapi/v1/roleDefGroups")) {
|
|
return new Response(JSON.stringify({
|
|
items: [
|
|
{
|
|
id: "g1",
|
|
name: "默认组",
|
|
roleDefs: [
|
|
{ id: "corpKeyed:admin", name: "主管理员" },
|
|
],
|
|
},
|
|
],
|
|
}), { status: 200 });
|
|
}
|
|
if (urlStr.includes("/api/openapi/v1/roledefs/")) {
|
|
return new Response(JSON.stringify({
|
|
value: {
|
|
id: "corpKeyed:admin",
|
|
name: "主管理员",
|
|
count: 1,
|
|
contents: [{ staffs: ["corpKeyed:staff1"] }],
|
|
},
|
|
}), { status: 200 });
|
|
}
|
|
if (urlStr.includes("/api/openapi/v1/staffs/getStaffIds")) {
|
|
return new Response(JSON.stringify({
|
|
items: [
|
|
{ id: "corpKeyed:staff1", name: "测试管理员", code: "001", active: true },
|
|
],
|
|
}), { status: 200 });
|
|
}
|
|
return new Response(JSON.stringify({}), { status: 200 });
|
|
}) as unknown as typeof fetch;
|
|
|
|
const corpWithKeys: CorpConfig = {
|
|
id: "corpKeyed",
|
|
name: "已配密钥企业",
|
|
baseUrl: "https://app.ekuaibao.com",
|
|
source: "openapi",
|
|
credentials: {
|
|
accessToken: "token-x",
|
|
corpId: "corpKeyed",
|
|
staffId: "corpKeyed:STAFF",
|
|
appKey: "my-key-123",
|
|
appSecurity: "my-sec-456",
|
|
},
|
|
};
|
|
|
|
const res = await handleCorpsCommand(
|
|
{
|
|
area: "corps",
|
|
action: "hose-staffs",
|
|
args: [],
|
|
options: { role: "admin", json: true },
|
|
raw: ["corps", "hose-staffs", "--role=admin", "--json"],
|
|
},
|
|
{ ...searchRuntime, selectedCorp: corpWithKeys, jsonMode: true }
|
|
);
|
|
|
|
expect(res.exitCode).toBe(0);
|
|
const data = JSON.parse(res.stdout);
|
|
expect(data.roleName).toBe("主管理员");
|
|
expect(data.staffs).toHaveLength(1);
|
|
expect(data.staffs[0].name).toBe("测试管理员");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
});
|