- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名: * 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 全部通过,类型检查零错误
173 lines
6.7 KiB
TypeScript
173 lines
6.7 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
canRefreshCorpAccessToken,
|
|
getCorpProvisionalAuthLink,
|
|
hasOpenApiPersistentCredentials,
|
|
parseExpiresInSeconds,
|
|
resolveLinkExpiresAt,
|
|
selectOpenApiKey,
|
|
} from "../auth/login.js";
|
|
import { createSessionClient } from "./hose-web-session-client.js";
|
|
import type { CorpConfig } from "../config/types.js";
|
|
|
|
function corp(overrides: Partial<CorpConfig> = {}): CorpConfig {
|
|
return {
|
|
id: "corp-1",
|
|
name: "测试企业",
|
|
source: "link",
|
|
credentials: { accessToken: "token", corpId: "corp-1", staffId: "corp-1:staff-1" },
|
|
...overrides,
|
|
baseUrl: overrides.baseUrl ?? "https://app.ekuaibao.com",
|
|
};
|
|
}
|
|
|
|
describe("core/http/hose-web-session-client refresh policy", () => {
|
|
it("allows refresh for account credentials or OpenAPI persistent credentials", () => {
|
|
expect(canRefreshCorpAccessToken(corp())).toBe(false);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "browser", accountId: "86-1" }))).toBe(false);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "account" }))).toBe(false);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "account", accountId: "86-1" }))).toBe(true);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "account", accountId: "86-1", session: { isShortTermToken: true } }))).toBe(false);
|
|
|
|
const openApiCorp = corp({
|
|
source: "link",
|
|
credentials: {
|
|
accessToken: "token",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
appKey: "key-123",
|
|
appSecurity: "sec-456",
|
|
},
|
|
});
|
|
expect(hasOpenApiPersistentCredentials(openApiCorp)).toBe(true);
|
|
expect(canRefreshCorpAccessToken(openApiCorp)).toBe(true);
|
|
});
|
|
|
|
it("fails expired temporary credentials without retrying or refreshing", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
let calls = 0;
|
|
globalThis.fetch = (async () => {
|
|
calls += 1;
|
|
return new Response("expired", { status: 401 });
|
|
}) as unknown as typeof fetch;
|
|
|
|
try {
|
|
const client = await createSessionClient(corp());
|
|
await expect(client.get("api/test")).rejects.toThrow("临时凭证已过期,请使用 hose-use corps add-link 重新导入");
|
|
expect(calls).toBe(1);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
it("automatically refreshes expired token via OpenAPI getProvisionalAuth when appKey/appSecurity are present", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const tempDir = mkdtempSync(join(tmpdir(), "hose-use-test-"));
|
|
const prevEnv = process.env.HOSE_ENTERPRISES_DIR;
|
|
process.env.HOSE_ENTERPRISES_DIR = tempDir;
|
|
|
|
const seenUrls: string[] = [];
|
|
globalThis.fetch = (async (input: string | URL | Request) => {
|
|
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
seenUrls.push(url);
|
|
if (url.includes("/api/openapi/v1/auth/getAccessToken")) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
value: {
|
|
accessToken: "openapi-token-xyz",
|
|
corporationId: "corp-1",
|
|
expireTime: Date.now() + 7200_000,
|
|
},
|
|
}),
|
|
{ status: 200, headers: { "content-type": "application/json" } }
|
|
);
|
|
}
|
|
if (url.includes("/api/openapi/v1.1/provisional/getProvisionalAuth")) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
value: {
|
|
message:
|
|
"https://app.ekuaibao.com/web/thirdparty.html?accessToken=fresh-session-token-999&ekbCorpId=corp-1",
|
|
},
|
|
}),
|
|
{ status: 200, headers: { "content-type": "application/json" } }
|
|
);
|
|
}
|
|
return new Response(JSON.stringify({ value: { ok: true } }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}) as unknown as typeof fetch;
|
|
|
|
try {
|
|
const persistentCorp = corp({
|
|
source: "openapi",
|
|
credentials: {
|
|
accessToken: "",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
appKey: "test-app-key",
|
|
appSecurity: "test-app-security",
|
|
},
|
|
auth: {
|
|
refreshTtl: 300,
|
|
expiresAt: Date.now() - 10_000,
|
|
},
|
|
});
|
|
|
|
const client = await createSessionClient(persistentCorp);
|
|
expect(persistentCorp.credentials.accessToken).toBe("fresh-session-token-999");
|
|
|
|
const linkRes = await getCorpProvisionalAuthLink(persistentCorp, { enterprisesDir: tempDir });
|
|
expect(linkRes.method).toBe("provisional-openapi");
|
|
expect(linkRes.url).toContain("accessToken=fresh-session-token-999");
|
|
expect(client).toBeDefined();
|
|
expect(seenUrls.some((u) => u.includes("getProvisionalAuth"))).toBe(true);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
if (prevEnv === undefined) delete process.env.HOSE_ENTERPRISES_DIR;
|
|
else process.env.HOSE_ENTERPRISES_DIR = prevEnv;
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("core/auth/login > selectOpenApiKey", () => {
|
|
const sampleKeys = [
|
|
{ id: "k1", name: "停用密钥", appKey: "ak-1", appSecurity: "sec-1", active: false },
|
|
{ id: "k2", name: "启用密钥A", appKey: "ak-2", appSecurity: "sec-2", active: true },
|
|
{ id: "k3", name: "启用密钥B", appKey: "ak-3", appSecurity: "sec-3", active: true },
|
|
];
|
|
|
|
it("auto-selects when only one active key exists", () => {
|
|
const picked = selectOpenApiKey([sampleKeys[0]!, sampleKeys[1]!], "corp-1");
|
|
expect(picked.appKey).toBe("ak-2");
|
|
});
|
|
|
|
it("requires explicit selection or --force when multiple active keys exist", () => {
|
|
expect(() => selectOpenApiKey(sampleKeys, "corp-1")).toThrow("存在多套");
|
|
expect(selectOpenApiKey(sampleKeys, "corp-1", { force: true }).appKey).toBe("ak-2");
|
|
expect(selectOpenApiKey(sampleKeys, "corp-1", { appKey: "ak-3" }).appKey).toBe("ak-3");
|
|
});
|
|
});
|
|
|
|
describe("core/auth/login link expiry", () => {
|
|
it("parses explicit ttl and expires durations", () => {
|
|
const base = 1_700_000_000_000;
|
|
expect(resolveLinkExpiresAt({ ttl: "72h" }, base)).toBe(base + 72 * 60 * 60 * 1000);
|
|
expect(resolveLinkExpiresAt({ ttl: "3d" }, base)).toBe(base + 3 * 24 * 60 * 60 * 1000);
|
|
expect(resolveLinkExpiresAt({ expires: "3" }, base)).toBe(base + 3 * 24 * 60 * 60 * 1000);
|
|
expect(resolveLinkExpiresAt({}, base)).toBeUndefined();
|
|
});
|
|
|
|
it("parses expires-in / ttl strings to seconds for provisional auth link", () => {
|
|
expect(parseExpiresInSeconds("24h")).toBe(86400);
|
|
expect(parseExpiresInSeconds("7d")).toBe(604800);
|
|
expect(parseExpiresInSeconds("86400")).toBe(86400);
|
|
expect(parseExpiresInSeconds(3600)).toBe(3600);
|
|
});
|
|
});
|