131 lines
5.4 KiB
TypeScript
131 lines
5.4 KiB
TypeScript
// MUST be first: sets DATABASE_URL before db_client is evaluated (node_health imports it).
|
|
import "./setup_env";
|
|
|
|
import { test, expect, describe, beforeAll, afterAll, beforeEach } from "bun:test";
|
|
import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
/**
|
|
* ALog 解密(控制面)。bytedcli 用一个假的可执行文件顶替:按参数写明文 / 打印
|
|
* auth status,并把每次调用的参数记到日志里 —— 测试进程不出网、不碰真凭据。
|
|
*/
|
|
const root = mkdtempSync(path.join(tmpdir(), "alog-decode-test-"));
|
|
const CALLS = path.join(root, "calls.log");
|
|
const STUB = path.join(root, "bytedcli");
|
|
writeFileSync(STUB, `#!/bin/sh
|
|
echo "$@" >> "${CALLS}"
|
|
out=""
|
|
prev=""
|
|
for a in "$@"; do
|
|
[ "$prev" = "--output" ] && out="$a"
|
|
prev="$a"
|
|
done
|
|
case "$*" in
|
|
*"log decrypt"*)
|
|
if [ "$STUB_MODE" = "fail" ]; then
|
|
echo '{"status":"error","error":{"code":"AUTH_REQUIRED","message":"Not authenticated"}}'
|
|
exit 1
|
|
fi
|
|
printf '2026-10-26 15:36:50 [EditLivePhoto] cropRatio=1:1\\n' > "$out"
|
|
echo '{"status":"success","data":{"size":48}}'
|
|
;;
|
|
*"auth status"*)
|
|
echo "{\\"status\\":\\"success\\",\\"data\\":{\\"bytecloud_auth\\":{\\"status\\":\\"ready\\",\\"identity\\":{\\"email\\":\\"svc@bytedance.com\\"},\\"refreshTokenExpiresAt\\":\\"$STUB_EXP\\"}}}"
|
|
;;
|
|
esac
|
|
`);
|
|
chmodSync(STUB, 0o755);
|
|
process.env.BYTEDCLI_BIN = STUB;
|
|
process.env.STREAMLINE_ALOG_CACHE_DIR = path.join(root, "cache");
|
|
process.env.HEALTH_ALERT_EMAILS = ""; // 不发卡片
|
|
|
|
const mod = await import("../src/alog_decode");
|
|
const { sniffAlogTarget, submitAlogDecode, alogDecodeStatus, checkAlogAuth } = mod;
|
|
|
|
const zip = (tag: string) => new Uint8Array(Buffer.concat([
|
|
Buffer.from("504b0304", "hex"), Buffer.from(`..BDALog_20261026_153648.alog.iOS.1233..${tag}`),
|
|
]));
|
|
const calls = () => (existsSync(CALLS) ? readFileSync(CALLS, "utf8").trim().split("\n") : []);
|
|
|
|
async function settle(sha: string): Promise<ReturnType<typeof alogDecodeStatus>> {
|
|
for (let i = 0; i < 100; i++) {
|
|
const s = alogDecodeStatus(sha);
|
|
if (s.state !== "pending") return s;
|
|
await Bun.sleep(20);
|
|
}
|
|
throw new Error("decode never settled");
|
|
}
|
|
|
|
beforeEach(() => { rmSync(CALLS, { force: true }); delete process.env.STUB_MODE; });
|
|
afterAll(() => { rmSync(root, { recursive: true, force: true }); });
|
|
|
|
describe("sniffAlogTarget", () => {
|
|
test("从 zip 成员名认出 os / aid", () => {
|
|
expect(sniffAlogTarget(zip("a"))).toEqual({ os: "iOS", aid: "1233" });
|
|
const android = new Uint8Array(Buffer.from("PK\u0003\u0004..x.alog.Android.1180.."));
|
|
expect(sniffAlogTarget(android, "ios")).toEqual({ os: "Android", aid: "1180" });
|
|
});
|
|
test("认不出时按任务平台 + 默认 aid", () => {
|
|
const bare = new Uint8Array(Buffer.from("PK\u0003\u0004..no-member-names.."));
|
|
expect(sniffAlogTarget(bare, "android")).toEqual({ os: "Android", aid: "1233" });
|
|
expect(sniffAlogTarget(bare)).toEqual({ os: "iOS", aid: "1233" });
|
|
});
|
|
});
|
|
|
|
describe("submitAlogDecode → alogDecodeStatus", () => {
|
|
test("解开后按 sha 缓存;同一份 zip 再提交直接 ready,不再调 bytedcli", async () => {
|
|
const bytes = zip("cache");
|
|
const first = submitAlogDecode(bytes, { platform: "ios", name: "alog-2h.zip" });
|
|
expect(first.state).toBe("pending");
|
|
const s = await settle(first.sha);
|
|
expect(s.state).toBe("ready");
|
|
expect(readFileSync((s as { file: string }).file, "utf8")).toContain("cropRatio=1:1");
|
|
|
|
const argv = calls()[0];
|
|
expect(argv).toContain("--profile streamline-alog");
|
|
expect(argv).toContain("slardar app log decrypt --aid 1233 --os iOS");
|
|
expect(argv).toContain("alog-2h.zip");
|
|
|
|
expect(submitAlogDecode(bytes, { platform: "ios" })).toEqual({ sha: first.sha, state: "ready" });
|
|
expect(calls()).toHaveLength(1);
|
|
});
|
|
|
|
test("失败带上原因并标成授权问题;下一次提交会重试", async () => {
|
|
process.env.STUB_MODE = "fail";
|
|
const bytes = zip("fail");
|
|
const { sha } = submitAlogDecode(bytes);
|
|
const s = await settle(sha);
|
|
expect(s.state).toBe("failed");
|
|
expect((s as { reason: string }).reason).toContain("bytedcli 未授权或授权已过期");
|
|
|
|
delete process.env.STUB_MODE;
|
|
expect(submitAlogDecode(bytes).state).toBe("pending");
|
|
expect((await settle(sha)).state).toBe("ready");
|
|
});
|
|
|
|
test("不认识的 sha → unknown", () => {
|
|
expect(alogDecodeStatus("nope").state).toBe("unknown");
|
|
expect(alogDecodeStatus("0".repeat(64)).state).toBe("unknown");
|
|
});
|
|
});
|
|
|
|
describe("checkAlogAuth · 续期方式的观察", () => {
|
|
// 真实输出是 Go 的纳秒精度 + 时区偏移(控制面实测),照原样喂。
|
|
beforeAll(() => { process.env.STUB_EXP = "2026-11-29T08:29:02.952945048+08:00"; });
|
|
|
|
test("第一次只记下到期时间;刷新后到期时间往后推 → slides=true", async () => {
|
|
const a = await checkAlogAuth();
|
|
expect(a.state).toBe("ready");
|
|
expect(a.account).toBe("svc@bytedance.com");
|
|
expect(a.slides).toBeNull();
|
|
expect(calls()[0]).toContain("auth status --force-refresh");
|
|
|
|
process.env.STUB_EXP = "2026-11-29T08:29:06.92984126+08:00";
|
|
const b = await checkAlogAuth();
|
|
expect(b.firstRefreshExpiresAt).toBe("2026-11-29T08:29:02.952945048+08:00");
|
|
expect(b.refreshExpiresAt).toBe("2026-11-29T08:29:06.92984126+08:00");
|
|
expect(b.slides).toBe(true);
|
|
});
|
|
});
|