import { afterEach, describe, expect, test } from "bun:test"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { CorpConfig, HoseSealSource } from "../config/types.js"; import { HOSE_STANDARD_DOMAINS, assertHoseAssistTenantMatches, defaultSealUrl, exchangeHoseAssistLink, fetchHoseStaffDetails, getHoseEnterpriseUrl, normalizeBaseUrl, parseHoseAssistLink } from "./hose.js"; const ORIGINAL_ENV = process.env.SEAL_HOME_TOKEN_STORE; const ORIGINAL_FETCH = globalThis.fetch; const tempDirs: string[] = []; function setupTokenStore(data: Record): string { const dir = mkdtempSync(join(tmpdir(), "hose-auth-test-")); tempDirs.push(dir); const storePath = join(dir, "tokens.json"); process.env.SEAL_HOME_TOKEN_STORE = storePath; writeFileSync(storePath, JSON.stringify(data, null, 2)); return storePath; } afterEach(() => { if (ORIGINAL_ENV === undefined) { delete process.env.SEAL_HOME_TOKEN_STORE; } else { process.env.SEAL_HOME_TOKEN_STORE = ORIGINAL_ENV; } globalThis.fetch = ORIGINAL_FETCH; for (const dir of tempDirs.splice(0)) { rmSync(dir, { recursive: true, force: true }); } }); describe("hose auth helpers", () => { test("normalizeBaseUrl strips trailing slashes", () => { expect(normalizeBaseUrl("https://app.ekuaibao.com///")).toBe("https://app.ekuaibao.com"); expect(normalizeBaseUrl("https://app.ekuaibao.com")).toBe("https://app.ekuaibao.com"); }); test("defaultSealUrl resolves corp.seal.url, source.sealUrl, or fallback subdomain", () => { const source: HoseSealSource = { type: "hose", domain: "https://app.ekuaibao.com", appKey: "key", appSecurity: "sec", staffId: "user1", corpId: "CORP123" }; const corpWithSealUrl: CorpConfig = { id: "corp-1", name: "Corp 1", environment: "production", seal: { url: "https://custom.sealai.cc", endpoints: { approvalStylePreferences: "" } }, source, auth: { refreshTtl: 300 } }; expect(defaultSealUrl(corpWithSealUrl, source)).toBe("https://custom.sealai.cc"); const corpWithoutSealUrl: CorpConfig = { id: "corp-1", name: "Corp 1", environment: "production", seal: { endpoints: { approvalStylePreferences: "" } }, source, auth: { refreshTtl: 300 } }; expect(defaultSealUrl(corpWithoutSealUrl, source)).toBe("https://corp123.sealai.cc"); }); test("getHoseEnterpriseUrl retrieves cached provisional auth URL", async () => { setupTokenStore({ "corp-cached": { hose: { closeapi: { token: "close-token", url: "https://app.ekuaibao.com/web/home.html?accessToken=close-token", expiresAt: Date.now() + 3600_000 } } } }); const corp: CorpConfig = { id: "corp-cached", name: "Corp Cached", environment: "production", seal: { endpoints: { approvalStylePreferences: "" } }, source: { type: "hose", domain: "https://app.ekuaibao.com", appKey: "key", appSecurity: "sec", staffId: "staff", corpId: "corp" }, auth: { refreshTtl: 300 } }; const url = await getHoseEnterpriseUrl(corp); expect(url).toBe("https://app.ekuaibao.com/web/home.html?accessToken=close-token"); }); test("HOSE_STANDARD_DOMAINS contains 6 expected public platform domains", () => { expect(HOSE_STANDARD_DOMAINS.length).toBe(6); expect(HOSE_STANDARD_DOMAINS).toContain("https://app.ekuaibao.com"); expect(HOSE_STANDARD_DOMAINS).toContain("https://dd2.hosecloud.com"); expect(HOSE_STANDARD_DOMAINS).toContain("https://wx2.ekuaibao.com"); expect(HOSE_STANDARD_DOMAINS).toContain("https://kdcloud2.ekuaibao.com"); expect(HOSE_STANDARD_DOMAINS).toContain("https://ddenterprise.ekuaibao.com"); expect(HOSE_STANDARD_DOMAINS).toContain("https://feishu.ekuaibao.com"); }); test("fetchHoseStaffDetails returns empty array for empty staffIds", async () => { const details = await fetchHoseStaffDetails("https://app.ekuaibao.com", "fake-token", []); expect(details).toEqual([]); }); test("parses a Hose third-party assistance link without exposing its token", () => { const result = parseHoseAssistLink( "https://wx2.ekuaibao.com/web/thirdparty.html?accessToken=temporary-token&ekbCorpId=Acme-01&pageType=home" ); expect(result).toEqual({ corpId: "acme-01", accessToken: "temporary-token" }); }); test("rejects unsupported Hose assistance link shapes", () => { expect(() => parseHoseAssistLink("http://wx2.ekuaibao.com/web/thirdparty.html?accessToken=t&ekbCorpId=acme")).toThrow( "expected an HTTPS" ); expect(() => parseHoseAssistLink("https://example.com/web/thirdparty.html?accessToken=t&ekbCorpId=acme")).toThrow( "expected an HTTPS" ); expect(() => parseHoseAssistLink("https://wx2.ekuaibao.com/web/home.html?accessToken=t&ekbCorpId=acme")).toThrow( "expected an HTTPS" ); expect(() => parseHoseAssistLink("https://wx2.ekuaibao.com/web/thirdparty.html?ekbCorpId=acme")).toThrow( "required exactly once" ); }); test("exchanges a Hose assistance link for a Seal session", async () => { let requestedUrl = ""; globalThis.fetch = (async (input: RequestInfo | URL) => { requestedUrl = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; return new Response(JSON.stringify({ data: { accessToken: "seal-bearer-token", expiresIn: 1800 } }), { status: 200, headers: { "content-type": "application/json" } }); }) as typeof fetch; const result = await exchangeHoseAssistLink( "https://wx2.ekuaibao.com/web/thirdparty.html?accessToken=hose-token&ekbCorpId=Acme-01" ); const url = new URL(requestedUrl); expect(url.origin).toBe("https://acme-01.sealai.cc"); expect(url.pathname).toBe("/api/auth/oauth2/session/oem-hosecloud"); expect(url.searchParams.get("token")).toBe("hose-token"); expect(url.searchParams.get("returnToken")).toBe("1"); expect(result).toEqual({ corpId: "acme-01", sealUrl: "https://acme-01.sealai.cc", token: "seal-bearer-token", expiresIn: 1800 }); }); test("requires complete and consistent tenant identity metadata", () => { expect(() => assertHoseAssistTenantMatches("acme-01", {})).toThrow("tenant mismatch"); expect(() => assertHoseAssistTenantMatches("acme-01", { tenantSlug: "acme-01", hoseCorpConfig: { corporationId: "other-corp" } })).toThrow("tenant mismatch"); expect(() => assertHoseAssistTenantMatches("Acme-01", { hoseCorpConfig: { corporationId: " ACME-01 " } })).not.toThrow(); }); });