- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名: * 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 全部通过,类型检查零错误
194 lines
6.6 KiB
TypeScript
194 lines
6.6 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import {
|
|
getBrowserFingerprintHeaders,
|
|
DEFAULT_USER_AGENT,
|
|
DEFAULT_REFERER,
|
|
DEFAULT_ORIGIN,
|
|
} from "./fingerprint.js";
|
|
import {
|
|
getSessionHeaders,
|
|
createSessionClient,
|
|
getHoseWebSessionHeaders,
|
|
createHoseWebSessionClient,
|
|
} from "./hose-web-session-client.js";
|
|
import {
|
|
createOpenApiClient,
|
|
getOpenApiHeaders,
|
|
} from "./openapi-client.js";
|
|
import {
|
|
resolveCorpCluster,
|
|
getEasoClient,
|
|
getUnityClient,
|
|
getUnityEasoClient,
|
|
clearUnityClientCache,
|
|
} from "./unity-easo-client.js";
|
|
import { createAssistPlatformClient } from "./assist-platform-client.js";
|
|
import {
|
|
getScriptEngineClient,
|
|
getPiperWorkflowClient,
|
|
} from "./piper-workflow-client.js";
|
|
import {
|
|
createHoseDocsClient,
|
|
DEFAULT_HOSE_DOCS_BASE_URL,
|
|
} from "./hose-docs-client.js";
|
|
import {
|
|
createSsoAuthClient,
|
|
createSsoClient,
|
|
} from "./sso-auth-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();
|
|
});
|
|
});
|
|
|
|
describe("core/http/hose-web-session-client", () => {
|
|
it("injects valid session headers and preserves caller custom options", () => {
|
|
const corp = mockCorp();
|
|
const headers = getHoseWebSessionHeaders(corp, {
|
|
referer: "https://custom.ekuaibao.com",
|
|
});
|
|
|
|
expect(headers["accesstoken"]).toBe("mock-access-token");
|
|
expect(headers["corpid"]).toBe("mock-corp-id");
|
|
expect(headers["staffid"]).toBe("mock-staff-id");
|
|
expect(headers["Referer"]).toBe("https://custom.ekuaibao.com");
|
|
});
|
|
|
|
it("createHoseWebSessionClient is callable and aliased to createSessionClient", async () => {
|
|
const corp = mockCorp();
|
|
const client = await createHoseWebSessionClient(corp);
|
|
expect(client).toBeDefined();
|
|
expect(createSessionClient).toBe(createHoseWebSessionClient);
|
|
});
|
|
});
|
|
|
|
describe("core/http/openapi-client", () => {
|
|
it("formats standard Bearer authorization header", () => {
|
|
const headers = getOpenApiHeaders("valid-mock-token");
|
|
expect(headers["Authorization"]).toBe("Bearer valid-mock-token");
|
|
expect(headers["app-token"]).toBe("valid-mock-token");
|
|
});
|
|
|
|
it("throws ValidationError when both auth and appToken are missing", async () => {
|
|
await expect(createOpenApiClient({})).rejects.toThrow(
|
|
"创建 OpenAPI 客户端失败: 必须提供 auth 凭据或 appToken"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("core/http/unity-easo-client & piper-workflow-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("getPiperWorkflowClient is defined and callable (aliased to getScriptEngineClient)", () => {
|
|
expect(getPiperWorkflowClient).toBeDefined();
|
|
expect(getScriptEngineClient).toBe(getPiperWorkflowClient);
|
|
});
|
|
|
|
it("getUnityEasoClient is defined and aliased to getUnityClient", () => {
|
|
expect(getUnityEasoClient).toBeDefined();
|
|
expect(getUnityEasoClient).toBe(getUnityClient);
|
|
});
|
|
|
|
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 重新扫码登录");
|
|
});
|
|
});
|
|
|
|
describe("core/http/hose-docs-client & sso-auth-client", () => {
|
|
it("createHoseDocsClient constructs with default base URL and docs user-agent", () => {
|
|
const client = createHoseDocsClient();
|
|
expect(client).toBeDefined();
|
|
expect(DEFAULT_HOSE_DOCS_BASE_URL).toBe("https://docs.ekuaibao.com");
|
|
});
|
|
|
|
it("createSsoAuthClient is aliased to createSsoClient and instantiates correctly", () => {
|
|
expect(createSsoAuthClient).toBe(createSsoClient);
|
|
const ssoClient = createSsoAuthClient();
|
|
expect(ssoClient).toBeDefined();
|
|
});
|
|
});
|