Files
hose-use/src/cli/printer.test.ts
T

118 lines
4.7 KiB
TypeScript

import { describe, expect, test, beforeEach, afterEach } from "bun:test";
import { mkdtempSync, rmSync, existsSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
DEFAULT_OUTPUT_BYTES_LIMIT,
protectLargeOutput,
printJson,
redactSensitiveText,
} from "./printer.js";
describe("cli/printer - 上下文防爆与敏感脱敏", () => {
describe("redactSensitiveText 脱敏处理", () => {
test("脱敏 URL Query 中的 accessToken / token / returnToken", () => {
const input = "https://app.ekuaibao.com/api/test?accessToken=sec_123456&corpId=corpxx&returnToken=ret_789";
const redacted = redactSensitiveText(input);
expect(redacted).not.toContain("sec_123456");
expect(redacted).not.toContain("ret_789");
expect(redacted).toContain("accessToken=<redacted>");
expect(redacted).toContain("returnToken=<redacted>");
expect(redacted).toContain("corpId=corpxx");
});
test("脱敏对象存储签名 URL 中的临时凭证", () => {
const input = "https://files.example/a.png?Expires=1&OSSAccessKeyId=key&Signature=sig&security-token=secret";
const redacted = redactSensitiveText(input);
expect(redacted).toContain("OSSAccessKeyId=<redacted>");
expect(redacted).toContain("Signature=<redacted>");
expect(redacted).toContain("security-token=<redacted>");
expect(redacted).not.toContain("secret");
});
test("脱敏 Authorization Bearer token", () => {
const input = "Request headers: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.xyz";
const redacted = redactSensitiveText(input);
expect(redacted).not.toContain("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.xyz");
expect(redacted).toContain("Bearer <redacted>");
});
test("脱敏 password、secret、appSecurity 等键值", () => {
const input = '{"username": "admin", "password": "super_secret_password_123", "appSecurity": "sec_key"}';
const redacted = redactSensitiveText(input);
expect(redacted).not.toContain("super_secret_password_123");
expect(redacted).not.toContain("sec_key");
expect(redacted).toContain('"password": "<redacted>"');
expect(redacted).toContain('"appSecurity": "<redacted>"');
});
});
describe("protectLargeOutput 与 printJson 体积熔断保护", () => {
let tempDir: string;
beforeEach(() => {
tempDir = mkdtempSync(join(tmpdir(), "hose-printer-test-"));
});
afterEach(() => {
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {
// ignore
}
});
test("正常体积的数据可以直接输出 JSON", () => {
const normalData = { hello: "world", numbers: [1, 2, 3] };
expect(() => protectLargeOutput(normalData)).not.toThrow();
const output = printJson(normalData);
expect(JSON.parse(output)).toEqual(normalData);
});
test("超过 200KB 的巨型数据默认抛出熔断错误", () => {
// 构造超过 200KB 的大数组
const bigArray = Array.from({ length: 1500 }, (_, i) => ({
id: `id-${i}`,
description: "这是一个用于测试大报文熔断拦截的测试长字段内容,模拟企业级海量单据与工作流节点拓扑",
nested: { count: i, payload: "x".repeat(120) },
}));
expect(() => protectLargeOutput(bigArray)).toThrow(
/输出体积过大.*超过安全阈值.*--output-file.*--full/
);
expect(() => printJson(bigArray)).toThrow(/输出体积过大/);
});
test("显式指定 allowFull 时允许全量输出大报文", () => {
const bigArray = Array.from({ length: 1500 }, (_, i) => ({
id: `id-${i}`,
payload: "y".repeat(150),
}));
expect(() => protectLargeOutput(bigArray, { allowFull: true })).not.toThrow();
const output = printJson(bigArray, { allowFull: true });
expect(output.length).toBeGreaterThan(DEFAULT_OUTPUT_BYTES_LIMIT);
});
test("指定 outputFile 时大报文直接落盘并返回精简摘要", () => {
const targetFile = join(tempDir, "exported-bills.json");
const bigArray = Array.from({ length: 1500 }, (_, i) => ({
id: `id-${i}`,
payload: "z".repeat(150),
}));
const resultJson = printJson(bigArray, { outputFile: targetFile });
const summary = JSON.parse(resultJson);
expect(summary.outputFile).toBe(targetFile);
expect(summary.bytes).toBeGreaterThan(DEFAULT_OUTPUT_BYTES_LIMIT);
expect(existsSync(targetFile)).toBe(true);
const savedContent = JSON.parse(readFileSync(targetFile, "utf-8"));
expect(savedContent).toHaveLength(1500);
expect(savedContent[0].id).toBe("id-0");
});
});
});