import { describe, it, expect } from "bun:test"; import { parseHeadersOrCurl } from "../../core/auth/login.js"; import { resolveCorpCluster } from "../../core/http/unity-client.js"; import { handleCorpsCommand } from "./corps/index.js"; import type { CliRuntime } from "../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-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 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 持久化密钥接入企业"); }); });