- 支持一行命令自动发起钉钉交互式扫码登录 (hose-use assist-platform login [--qr]) - 集成 qrcode-terminal 在终端直接输出紧凑 ANSI 文本二维码 - 严格对齐 HoseCloud SSO 钉钉时间戳的 60 秒安全防重放倒计时 - 自动从重定向参数或 Token 换取 Unity 运营中心会话凭证并安全落盘 - 在 core/http/assist-platform-client 增加响应体 errorCode: 403 拦截,杜绝凭证过期被吞成空数据的静默失败 - 修复 assist-platform enter 多集群租户 (wx2/shanghai 等) baseUrl 硬编码为 app 的验票失败问题 - 修复 workflow nodes grant 网关回包双层嵌套 saved/skipped 数据解析问题 - 补全 core/http/sso-client 单元测试并确保全部 957 项测试通过
174 lines
6.1 KiB
TypeScript
174 lines
6.1 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import {
|
|
getBrowserFingerprintHeaders,
|
|
DEFAULT_USER_AGENT,
|
|
DEFAULT_REFERER,
|
|
DEFAULT_ORIGIN,
|
|
} from "./fingerprint.js";
|
|
import {
|
|
getSessionHeaders,
|
|
createSessionClient,
|
|
} from "./session-client.js";
|
|
import {
|
|
createOpenApiClient,
|
|
getOpenApiHeaders,
|
|
} from "./openapi-client.js";
|
|
import { resolveCorpCluster, getEasoClient, getUnityClient, clearUnityClientCache } from "./unity-client.js";
|
|
import { createAssistPlatformClient } from "./assist-platform-client.js";
|
|
import { getScriptEngineClient } from "./script-engine-client.js";
|
|
import { setCorpTokens, resetTokenStoreMemory } from "../auth/token-store.js";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import type { CorpConfig } from "../config/types.js";
|
|
|
|
function mockCorp(overrides: Partial<CorpConfig> = {}): CorpConfig {
|
|
return {
|
|
id: "mock-corp",
|
|
name: "测试企业",
|
|
source: "link",
|
|
credentials: {
|
|
accessToken: "mock-access-token",
|
|
corpId: "mock-corp-id",
|
|
staffId: "mock-staff-id",
|
|
},
|
|
baseUrl: "https://app.ekuaibao.com",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("core/http/fingerprint", () => {
|
|
it("generates default modern browser fingerprint headers", () => {
|
|
const headers = getBrowserFingerprintHeaders();
|
|
expect(headers["User-Agent"]).toBe(DEFAULT_USER_AGENT);
|
|
expect(headers["Referer"]).toBe(DEFAULT_REFERER);
|
|
expect(headers["Origin"]).toBe(DEFAULT_ORIGIN);
|
|
expect(headers["sec-ch-ua-platform"]).toBe('"macOS"');
|
|
expect(headers["sec-ch-ua"]).toContain("Chrome");
|
|
expect(headers["Cookie"]).toBeUndefined();
|
|
});
|
|
|
|
it("supports custom cookie and referer", () => {
|
|
const headers = getBrowserFingerprintHeaders({
|
|
cookie: "SESSION=abc123xyz; cloud_waf=token456",
|
|
referer: "https://custom.ekuaibao.com/flow",
|
|
});
|
|
expect(headers["Cookie"]).toBe("SESSION=abc123xyz; cloud_waf=token456");
|
|
expect(headers["Referer"]).toBe("https://custom.ekuaibao.com/flow");
|
|
});
|
|
});
|
|
|
|
describe("core/http/session-client", () => {
|
|
it("merges closed session headers with browser fingerprint", () => {
|
|
const corp = mockCorp();
|
|
const headers = getSessionHeaders(corp);
|
|
|
|
// 核心三 Header 校验
|
|
expect(headers.accesstoken).toBe("mock-access-token");
|
|
expect(headers.corpid).toBe("mock-corp-id");
|
|
expect(headers.staffid).toBe("mock-staff-id");
|
|
|
|
// 统一防爬指纹校验
|
|
expect(headers["User-Agent"]).toBe(DEFAULT_USER_AGENT);
|
|
expect(headers["Origin"]).toBe(DEFAULT_ORIGIN);
|
|
});
|
|
|
|
it("createSessionClient produces valid client", async () => {
|
|
const corp = mockCorp();
|
|
const client = await createSessionClient(corp);
|
|
expect(client).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe("core/http/openapi-client", () => {
|
|
it("builds correct OpenAPI request headers", () => {
|
|
const headers = getOpenApiHeaders("test-app-token-123");
|
|
expect(headers["app-token"]).toBe("test-app-token-123");
|
|
expect(headers["Authorization"]).toBe("Bearer test-app-token-123");
|
|
expect(headers["User-Agent"]).toBe(DEFAULT_USER_AGENT);
|
|
});
|
|
|
|
it("creates OpenAPI client with static appToken", async () => {
|
|
const client = await createOpenApiClient({
|
|
appToken: "static-token",
|
|
baseUrl: "https://app.ekuaibao.com",
|
|
});
|
|
expect(client).toBeDefined();
|
|
});
|
|
|
|
it("fails to create OpenAPI client when neither auth nor appToken is provided", async () => {
|
|
await expect(createOpenApiClient({})).rejects.toThrow(
|
|
"创建 OpenAPI 客户端失败: 必须提供 auth 凭据或 appToken"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("core/http/unity-client & script-engine-client", () => {
|
|
it("resolves corp cluster from cluster property or baseUrl", () => {
|
|
expect(resolveCorpCluster({ cluster: "shanghai" })).toBe("shanghai");
|
|
expect(resolveCorpCluster({ baseUrl: "https://wx2.ekuaibao.com" })).toBe("wx2");
|
|
expect(resolveCorpCluster({ baseUrl: "https://app.ekuaibao.com" })).toBe("app");
|
|
expect(resolveCorpCluster({})).toBe("app");
|
|
});
|
|
|
|
it("getScriptEngineClient is defined and callable", () => {
|
|
expect(getScriptEngineClient).toBeDefined();
|
|
});
|
|
|
|
it("reuses persisted EASO and Unity tokens from token-store across CLI runs without network calls", async () => {
|
|
const tempDir = mkdtempSync(join(tmpdir(), "hose-tokens-test-"));
|
|
const prevTokensFile = process.env.HOSE_TOKENS_FILE;
|
|
process.env.HOSE_TOKENS_FILE = join(tempDir, "tokens.json");
|
|
|
|
try {
|
|
clearUnityClientCache();
|
|
resetTokenStoreMemory();
|
|
|
|
const corp = mockCorp({ id: "corp-persisted" });
|
|
setCorpTokens("corp-persisted", {
|
|
eas: {
|
|
hoseToken: "persisted-hose-token",
|
|
unityJwt: "persisted-unity-jwt",
|
|
easToken: "persisted-easo-token",
|
|
expiresAt: Date.now() + 3600 * 1000,
|
|
sourceAccessToken: "mock-access-token",
|
|
},
|
|
});
|
|
|
|
// Simulate fresh process memory
|
|
resetTokenStoreMemory();
|
|
|
|
const easo = await getEasoClient(corp);
|
|
expect(easo.client).toBeDefined();
|
|
|
|
const unity = await getUnityClient(corp);
|
|
expect(unity.unityJwt).toBe("persisted-unity-jwt");
|
|
} finally {
|
|
if (prevTokensFile === undefined) delete process.env.HOSE_TOKENS_FILE;
|
|
else process.env.HOSE_TOKENS_FILE = prevTokensFile;
|
|
resetTokenStoreMemory();
|
|
clearUnityClientCache();
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("createAssistPlatformClient throws AuthenticationError when response body contains errorCode: 403", async () => {
|
|
const client = createAssistPlatformClient({
|
|
token: "fake-token",
|
|
baseUrl: "https://mock-unity.invalid",
|
|
});
|
|
|
|
const mockFetch = async () =>
|
|
new Response(JSON.stringify({ errorCode: 403, errorMessage: "token过期请重新登录!" }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
|
|
const testClient = client.extend({ fetch: mockFetch as any });
|
|
|
|
expect(async () => {
|
|
await testClient.get("test").json();
|
|
}).toThrow("协助平台凭证已过期或无效,请运行: hose-use assist-platform login 重新扫码登录");
|
|
});
|
|
});
|