Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
@@ -0,0 +1,484 @@
import { describe, expect, test, beforeAll } from "bun:test";
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, chmodSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { parseManifest, type ManifestEntry } from "@bbbug/shared/evidence_capture";
/**
* 后端选择 + 预注册留痕(iOS / cat-supervisor 实例这条线)。
*
* 背景:iOS 节点上被测 App 是 cat-supervisor 实例(Catalyst 构建、跑在宿主机上),不是设备。
* `mobilecli devices` 因此枚举不到它,`record-evidence start` 直接报「未发现在线设备」,
* 于是所有 iOS 任务都退回裸 `catuse record` / `catuse savescreenshot` —— 文件有了,
* manifest 没有,intent / expect-moment / 校验结论全部缺席。2026-09 审计:39 个 iOS 任务、
* 602 个媒体文件,只有 29 个进过 manifest,真实视频声明 0 个(安卓 49/49)。
*
* 所以这里盯的不是"录得清不清楚",是**这条路到底有没有留下可对照的声明**:
* 后端选对了吗、实例认对了吗、manifest 写了吗、intent / expectMoment 在不在。
*
* 用假 `catuse` / `cat-supervisor` / `adb` 跑,因为要断言的是 wrapper 的选路与留痕,不是这几个二进制。
* 真二进制的行为(SIGINT 后 mp4 收尾完整、savescreenshot 全分辨率、失败时 JSON 落 stderr
* 且 exit 1)是在 fei-ios-02 上实测过的,不在这里复验。
*/
const REPO = path.resolve(import.meta.dir, "../..");
const CLI = path.join(REPO, "bin/record-evidence.ts");
let hasFfmpeg = false;
let SANDBOX = "";
let PNG = ""; // 真 PNG,让 ffprobe 能读出分辨率
let MP4 = ""; // 真 3s mp4(短于 staticProbeMinS,避免静止判定干扰)
function sh(file: string, body: string): string {
writeFileSync(file, `#!/bin/sh\n${body}\n`);
chmodSync(file, 0o755);
return file;
}
/**
* 假 catuse。`windows` 是 `catuse apps` 要吐的窗口列表。
* `record` 先把文件写完再睡,等 SIGINT —— 真 catuse 是录制期间持续写、断连时由守护进程收尾,
* 对 wrapper 而言两者可观察行为一致:stop 时文件已完整。
*/
function fakeCatuse(dir: string, windows: unknown[], broken = false): string {
const appsJson = path.join(dir, "catuse-apps.json");
writeFileSync(appsJson, JSON.stringify({ count: windows.length, windows }));
// broken:产出一个几乎为空的文件,判定必然 fail —— 用来构造「允许重录」的局面。
const png = broken ? "/dev/null" : PNG;
const mp4 = broken ? "/dev/null" : MP4;
return sh(path.join(dir, "catuse"), `
out=""; inst=""; prev=""
for a in "$@"; do
if [ "$prev" = "-o" ]; then out="$a"; fi
if [ "$prev" = "--instance" ]; then inst="$a"; fi
prev="$a"
done
case "$1" in
apps) cat ${JSON.stringify(appsJson)}; exit 0 ;;
savescreenshot)
if [ -z "$inst" ]; then echo '{"ok":false,"error":"--instance required"}' >&2; exit 1; fi
if [ -z "$out" ]; then echo '{"ok":false,"error":"-o required"}' >&2; exit 1; fi
cp ${JSON.stringify(png)} "$out"
echo '{"ok":true,"via":"daemon-sck","mode":"save"}'
exit 0 ;;
record)
if [ -z "$inst" ]; then echo '{"ok":false,"error":"--instance required"}' >&2; exit 1; fi
if [ -z "$out" ]; then echo '{"ok":false,"error":"-o required"}' >&2; exit 1; fi
cp ${JSON.stringify(mp4)} "$out"
trap 'echo "{\\"ok\\":true,\\"via\\":\\"daemon\\"}"; exit 0' INT TERM
i=0; while [ $i -lt 600 ]; do sleep 0.1; i=$((i+1)); done
exit 0 ;;
esac
echo '{"ok":false,"error":"unknown subcommand"}' >&2
exit 1`);
}
/**
* 假 adb。`serials` 是 `adb devices` 里就绪的设备;每次调用的参数追加进 adb.log,
* 用来断言 stop 收尾时信号发给了哪一条录制、文件是不是拉回来了。
* `shell screenrecord` 像真的一样挂着,直到被信号打断。
*/
function fakeAdb(dir: string, serials: string[] = ["emulator-5554"]): string {
const log = path.join(dir, "adb.log");
const devices = ["List of devices attached", ...serials.map((d) => `${d}\tdevice`), ""].join("\n");
writeFileSync(path.join(dir, "adb-devices.txt"), devices);
return sh(path.join(dir, "adb"), `
echo "$*" >> ${JSON.stringify(log)}
if [ "$1" = "-s" ]; then shift 2; fi
case "$1" in
devices) cat ${JSON.stringify(path.join(dir, "adb-devices.txt"))}; exit 0 ;;
exec-out) cat ${JSON.stringify(PNG)}; exit 0 ;;
pull) cp ${JSON.stringify(MP4)} "$3"; exit 0 ;;
shell)
case "$2" in
screenrecord)
trap 'exit 0' INT TERM
i=0; while [ $i -lt 600 ]; do sleep 0.1; i=$((i+1)); done
exit 0 ;;
*) exit 0 ;;
esac ;;
esac
exit 1`);
}
/** 假 cat-supervisor —— 只需要 `list`。 */
function fakeSupervisor(dir: string, instances: unknown[]): string {
const listJson = path.join(dir, "supervisor-list.json");
writeFileSync(listJson, JSON.stringify({ ok: true, count: instances.length, instances }));
return sh(path.join(dir, "cat-supervisor"), `
case "$1" in
list) cat ${JSON.stringify(listJson)}; exit 0 ;;
esac
echo '{"ok":false,"code":"unknown-command","message":"unknown"}'
exit 1`);
}
interface RunResult { code: number; stdout: string; stderr: string }
async function runCli(evidenceDir: string, argv: string[], env: Record<string, string>): Promise<RunResult> {
const proc = Bun.spawn(["bun", CLI, ...argv], {
cwd: REPO,
stdout: "pipe",
stderr: "pipe",
env: {
...process.env,
// 判据必须来自编译进来的 baseline:连得上控制面的话,生产的 active criteria
// 会让同一段素材在不同日子得出不同结论。
CONTROL_PLANE_URL: "http://127.0.0.1:1",
CLUSTER_SECRET: "",
// 不要触发采集即镜像(会往控制面 POST 字节)。
EVIDENCE_OWNER_KIND: "",
EVIDENCE_OWNER_ID: "",
AGENT_PLATFORM: "",
ANDROID_SERIAL: "",
...env,
},
});
const [stdout, stderr, code] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
return { code, stdout, stderr };
}
function manifest(dir: string): ManifestEntry[] {
const f = path.join(dir, "manifest.jsonl");
return existsSync(f) ? parseManifest(readFileSync(f, "utf8")) : [];
}
let caseNo = 0;
/** 每个用例一套独立的 evidence/ 与假二进制;默认是 iOS 节点。 */
function newCase(
opts: { instances?: unknown[]; windows?: unknown[]; broken?: boolean; platform?: string; serials?: string[] } = {},
): { dir: string; bin: string; env: Record<string, string> } {
const root = path.join(SANDBOX, `case-${++caseNo}`);
const bin = path.join(root, "bin");
const dir = path.join(root, "evidence");
mkdirSync(bin, { recursive: true });
mkdirSync(dir, { recursive: true });
return {
dir,
bin,
env: {
AGENT_PLATFORM: opts.platform ?? "ios",
CATUSE_PATH: fakeCatuse(bin, opts.windows ?? [TERMINAL_WINDOW, CATALYST_WINDOW], opts.broken),
CAT_SUPERVISOR_PATH: fakeSupervisor(bin, opts.instances ?? [INSTANCE]),
ADB_PATH: fakeAdb(bin, opts.serials),
},
};
}
const INSTANCE = { id: "tiktok-1", state: "running", pid: 70708, owner: "streamline:bug:t1" };
const OTHER_INSTANCE = { id: "tiktok-2", state: "running", pid: 81234, owner: "streamline:feature:f9" };
const CATALYST_WINDOW = { owner: "tiktok-1", instance: "tiktok-1", pid: 70708, windowID: 8385, title: "TikTok M-Staff" };
const OTHER_WINDOW = { owner: "tiktok-2", instance: "tiktok-2", pid: 81234, windowID: 8390, title: "TikTok M-Staff" };
const TERMINAL_WINDOW = { owner: "Terminal", pid: 793, windowID: 74, title: "" };
const TWO_RUNNING = { instances: [INSTANCE, OTHER_INSTANCE], windows: [TERMINAL_WINDOW, CATALYST_WINDOW, OTHER_WINDOW] };
beforeAll(async () => {
SANDBOX = mkdtempSync(path.join(tmpdir(), "record-evidence-backend-"));
PNG = path.join(SANDBOX, "still.png");
MP4 = path.join(SANDBOX, "clip.mp4");
hasFfmpeg = Bun.which("ffmpeg") != null && Bun.which("ffprobe") != null;
if (hasFfmpeg) {
await Bun.spawn(["ffmpeg", "-v", "error", "-f", "lavfi", "-i", "testsrc=size=320x240:rate=1",
"-frames:v", "1", "-y", PNG]).exited;
await Bun.spawn(["ffmpeg", "-v", "error", "-f", "lavfi", "-i", "testsrc=size=320x240:rate=25",
"-t", "3", "-pix_fmt", "yuv420p", "-y", MP4]).exited;
} else {
// 没有 ffmpeg 时素材判不了级,但选路与留痕照样要成立。
writeFileSync(PNG, Buffer.alloc(4096, 7));
writeFileSync(MP4, Buffer.alloc(4096, 7));
}
});
describe("record-evidence · 后端选择", () => {
const shot = (dir: string, env: Record<string, string>, extra: string[] = []) =>
runCli(dir, ["shot", "--dir", dir, "--out", "before-1-entry.png", "--intent", "入口页基线", ...extra], env);
test("iOS 节点走 catuse 实例 —— 哪怕同一台 Mac 上还插着安卓机", async () => {
// 插在 iOS 节点上的无关安卓机若赢了选路,录出来的是另一块屏幕,判定还会显示通过。
const { dir, env } = newCase();
const r = await shot(dir, env);
const entries = manifest(dir);
expect(entries).toHaveLength(1);
expect(entries[0].device).toBe("catalyst:tiktok-1#70708");
expect(r.stderr).not.toContain("未发现在线设备");
});
test("iOS 节点没有运行中的实例 → 报错指向 cat-supervisor run,不回落 adb", async () => {
const { dir, env } = newCase({ instances: [{ id: "tiktok-1", state: "stopped" }], windows: [TERMINAL_WINDOW] });
const r = await shot(dir, env);
expect(r.code).toBe(4);
expect(r.stderr).toContain("cat-supervisor run");
expect(r.stderr).toContain("tiktok-1");
expect(manifest(dir)).toHaveLength(0);
});
test("多个运行中的实例又没给 --instance → 报错列出候选,不猜", async () => {
const { dir, env } = newCase(TWO_RUNNING);
const r = await shot(dir, env);
expect(r.code).toBe(4);
expect(r.stderr).toContain("tiktok-1");
expect(r.stderr).toContain("tiktok-2");
expect(r.stderr).toContain("--instance");
expect(manifest(dir)).toHaveLength(0);
});
test("--instance 指定实例(--app 是它的别名)", async () => {
const a = newCase(TWO_RUNNING);
await shot(a.dir, a.env, ["--instance", "tiktok-2"]);
expect(manifest(a.dir)[0]?.device).toBe("catalyst:tiktok-2#81234");
const b = newCase(TWO_RUNNING);
await shot(b.dir, b.env, ["--app", "tiktok-1"]);
expect(manifest(b.dir)[0]?.device).toBe("catalyst:tiktok-1#70708");
});
test("--instance 指向没在跑的实例 → 报错说清状态", async () => {
const { dir, env } = newCase({ instances: [INSTANCE, { id: "tiktok-3", state: "stopped" }] });
const r = await shot(dir, env, ["--instance", "tiktok-3"]);
expect(r.code).toBe(4);
expect(r.stderr).toContain("stopped");
expect(manifest(dir)).toHaveLength(0);
});
test("实例在跑但 catuse 看不到它的窗口 → 报错,不采集", async () => {
const { dir, env } = newCase({ windows: [TERMINAL_WINDOW] });
const r = await shot(dir, env);
expect(r.code).toBe(4);
expect(r.stderr).toContain("catuse apps");
expect(manifest(dir)).toHaveLength(0);
});
test("iOS 节点上 --via adb / --device 直接拒绝", async () => {
const a = newCase();
expect((await shot(a.dir, a.env, ["--via", "adb"])).code).toBe(2);
const b = newCase();
expect((await shot(b.dir, b.env, ["--device", "emulator-5554"])).code).toBe(2);
expect(manifest(a.dir)).toHaveLength(0);
expect(manifest(b.dir)).toHaveLength(0);
});
test("安卓节点走 adb screencap,不碰 catuse", async () => {
const { dir, bin, env } = newCase({ platform: "android" });
await shot(dir, env);
expect(manifest(dir)[0]?.device).toBe("android:emulator-5554");
expect(readFileSync(path.join(bin, "adb.log"), "utf8")).toContain("-s emulator-5554 exec-out screencap -p");
});
test("安卓选设备:--device 优先,其次 ANDROID_SERIAL;多台又没指定就报错", async () => {
const two = ["emulator-5554", "R5CT1234"];
const a = newCase({ platform: "android", serials: two });
const r = await shot(a.dir, a.env);
expect(r.code).toBe(4);
expect(r.stderr).toContain("R5CT1234");
expect(r.stderr).toContain("--device");
const b = newCase({ platform: "android", serials: two });
await shot(b.dir, b.env, ["--device", "R5CT1234"]);
expect(manifest(b.dir)[0]?.device).toBe("android:R5CT1234");
const c = newCase({ platform: "android", serials: two });
await shot(c.dir, { ...c.env, ANDROID_SERIAL: "R5CT1234" });
expect(manifest(c.dir)[0]?.device).toBe("android:R5CT1234");
});
test("安卓 start/stop:只给本次录制发信号,录像拉回本地再删掉设备上的", async () => {
const { dir, bin, env } = newCase({ platform: "android" });
const start = await runCli(dir, ["start", "--dir", dir, "--out", "demo-1-feed.mp4",
"--intent", "演示", "--expect-moment", "刷到下一条"], env);
expect(start.code).toBe(0);
const captureId = /captureId=(\S+)/.exec(start.stdout)?.[1]!;
await runCli(dir, ["stop", "--dir", dir, "--capture", captureId], env);
const log = readFileSync(path.join(bin, "adb.log"), "utf8");
const devicePath = `/sdcard/record-evidence-${captureId}.mp4`;
expect(log).toContain(`shell screenrecord --time-limit 180 ${devicePath}`);
expect(log).toContain(`shell pkill -INT -f ${devicePath}`);
expect(log).toContain(`pull ${devicePath}`);
expect(log).toContain(`shell rm -f ${devicePath}`);
expect(existsSync(path.join(dir, "demo-1-feed.mp4"))).toBe(true);
expect(manifest(dir).find((e) => e.event === "stop")?.device).toBe("android:emulator-5554");
}, 30_000);
test("--via 只认 adb / catuse(mobilecli 已经不是选项)", async () => {
const { dir, env } = newCase();
const r = await runCli(dir, ["shot", "--dir", dir, "--out", "x-1-y.png",
"--intent", "基线", "--via", "mobilecli"], env);
expect(r.code).toBe(2);
expect(r.stderr).toContain("--via");
});
});
describe("record-evidence · catuse 路径的预注册留痕", () => {
test("shot 把 intent 写进 manifest(以前裸 savescreenshot 只有字节)", async () => {
const { dir, env } = newCase();
const r = await runCli(dir, ["shot", "--dir", dir, "--out", "before-1-story-tray.png",
"--intent", "发布前 Story 托盘里没有新条目"], env);
const [e] = manifest(dir);
expect(e.event).toBe("shot");
expect(e.kind).toBe("image");
expect(e.out).toBe("before-1-story-tray.png");
expect(e.phase).toBe("before");
expect(e.intent).toBe("发布前 Story 托盘里没有新条目");
expect(e.attempt).toBe(1);
expect(existsSync(path.join(dir, "before-1-story-tray.png"))).toBe(true);
if (hasFfmpeg) {
expect(e.verdict).toBe("pass");
expect(e.probe?.width).toBe(320);
expect(r.code).toBe(0);
}
});
test("start/stop 落下 intent + expect-moment,stop 给出判定", async () => {
const { dir, env } = newCase();
const start = await runCli(dir, ["start", "--dir", dir, "--out", "demo-1-flip-story.mp4",
"--intent", "端到端演示 Flip Story 发布",
"--expect-moment", "发布后新条目出现在托盘首位",
"--steps", "进入相机 → 拍摄 → 发布 → 回到托盘",
"--max-seconds", "60"], env);
expect(start.code).toBe(0);
const captureId = /captureId=(\S+)/.exec(start.stdout)?.[1];
expect(captureId).toBeTruthy();
const started = manifest(dir);
expect(started).toHaveLength(1);
expect(started[0].event).toBe("start");
expect(started[0].device).toBe("catalyst:tiktok-1#70708");
expect(started[0].kind).toBe("video");
expect(started[0].intent).toBe("端到端演示 Flip Story 发布");
expect(started[0].expectMoment).toBe("发布后新条目出现在托盘首位");
expect(started[0].plannedSteps).toContain("进入相机");
const stop = await runCli(dir, ["stop", "--dir", dir, "--capture", captureId!], env);
const entries = manifest(dir);
expect(entries).toHaveLength(2);
const done = entries[1];
expect(done.event).toBe("stop");
expect(done.captureId).toBe(captureId);
expect(done.out).toBe("demo-1-flip-story.mp4");
// start 的声明在 stop 这条上重复一份 —— 审计时只看终态行也能对照。
expect(done.intent).toBe("端到端演示 Flip Story 发布");
expect(done.expectMoment).toBe("发布后新条目出现在托盘首位");
expect(done.verdict).toBeDefined();
if (hasFfmpeg) {
expect(done.verdict).toBe("pass");
expect(done.probe?.durationS).toBeCloseTo(3, 0);
expect(stop.code).toBe(0);
}
}, 30_000);
test("同名覆盖仍被拦住(catuse 路径不绕过这条保护)", async () => {
const { dir, env } = newCase();
if (!hasFfmpeg) return; // 没判到 pass/warn 就谈不上「已有通过的证据」
await runCli(dir, ["shot", "--dir", dir, "--out", "after-1-x.png", "--intent", "第一次"], env);
const r = await runCli(dir, ["shot", "--dir", dir, "--out", "after-1-x.png", "--intent", "第二次"], env);
expect(r.code).toBe(2);
expect(r.stderr).toContain("已有通过的证据");
expect(r.stderr).toContain("--replace");
expect(manifest(dir)).toHaveLength(1);
});
test("--replace 显式作废后可同名重拍:旧文件改名保留,作废原因进 manifest", async () => {
const { dir, env } = newCase();
if (!hasFfmpeg) return;
await runCli(dir, ["shot", "--dir", dir, "--out", "flow-r1-de-s2-dm-36-activity.png", "--intent", "第一次(拍成了 Inbox)"], env);
const r = await runCli(dir, ["shot", "--dir", dir, "--out", "flow-r1-de-s2-dm-36-activity.png", "--intent", "重拍 Activity",
"--replace", "上一张拍到的是 Inbox 不是 Activity"], env);
expect(r.code).toBe(0);
const m = manifest(dir);
const v = m.find((e) => e.event === "void")!;
expect(v.reason).toBe("上一张拍到的是 Inbox 不是 Activity");
expect(v.replacedAs).toBe("flow-r1-de-s2-dm-36-activity.replaced-1.png");
expect(existsSync(path.join(dir, "flow-r1-de-s2-dm-36-activity.replaced-1.png"))).toBe(true);
expect(existsSync(path.join(dir, "flow-r1-de-s2-dm-36-activity.png"))).toBe(true);
expect(m.filter((e) => e.event === "shot")).toHaveLength(2);
});
});
describe("record-evidence · 采集前的声明", () => {
test("重录时换了要证明什么 → 记一条 redeclare,两段文本都留着", async () => {
// 第一次判 fail(坏文件)才谈得上重录,这也是审计里声明被改写的真实时机。
const { dir, env } = newCase({ broken: true });
const first = await runCli(dir, ["shot", "--dir", dir, "--out", "mech-1-read.png",
"--intent", "从特效面板选图触发生成后被踢到编辑页"], env);
expect(first.code).toBe(3);
const second = await runCli(dir, ["shot", "--dir", dir, "--out", "mech-1-read.png",
"--intent", "按返回键后拍摄页只闪现一瞬"], env);
const redeclare = manifest(dir).find((e) => e.event === "redeclare");
expect(redeclare).toBeDefined();
expect(redeclare!.out).toBe("mech-1-read.png");
expect(redeclare!.prevIntent).toBe("从特效面板选图触发生成后被踢到编辑页");
expect(redeclare!.intent).toBe("按返回键后拍摄页只闪现一瞬");
// 覆盖不是撤回:旧说法在 reason 里也留了一份,Portal 不改 schema 就能显示。
expect(redeclare!.reason).toContain("从特效面板选图");
// 这一次的 shot 自己也被标了出来,读终态行就知道声明动过。
expect(manifest(dir).find((e) => e.event === "shot" && e.attempt === 2)?.redeclared).toBe(true);
expect(second.stdout + second.stderr).toContain("声明改写");
});
test("声明没变就不记 —— 单纯重录一次不是改声明", async () => {
const { dir, env } = newCase({ broken: true });
const args = ["shot", "--dir", "", "--out", "mech-1-read.png", "--intent", "同一句话"];
args[2] = dir;
await runCli(dir, args, env);
await runCli(dir, args, env);
expect(manifest(dir).some((e) => e.event === "redeclare")).toBe(false);
});
test("--static-scene 必须带理由:它要被复核,不是一个开关", async () => {
const { dir, env } = newCase();
const r = await runCli(dir, ["start", "--dir", dir, "--out", "demo-1-x.mp4",
"--intent", "i", "--expect-moment", "m", "--static-scene"], env);
expect(r.code).toBe(2);
expect(r.stderr).toContain("理由");
expect(manifest(dir)).toHaveLength(0);
});
test("start 时的声明被 stop 沿用 —— 看到判定之后已经改不了了", async () => {
const { dir, env } = newCase();
const start = await runCli(dir, ["start", "--dir", dir, "--out", "demo-1-press.mp4",
"--intent", "长按态", "--expect-moment", "按压态出现",
"--static-scene", "长按 @全部 的按压态,画面本就不动",
"--scene", "zh/评论页"], env);
const captureId = /captureId=(\S+)/.exec(start.stdout)?.[1]!;
// stop 不重新解析这些参数,所以这里故意不再传一遍。
await runCli(dir, ["stop", "--dir", dir, "--capture", captureId], env);
const stop = manifest(dir).find((e) => e.event === "stop")!;
expect(stop.staticScene).toBe("长按 @全部 的按压态,画面本就不动");
expect(stop.scene).toBe("zh/评论页");
}, 30_000);
test("after 与 before 的场景标识不一致 → scene-mismatch(两张都通过了文件属性校验)", async () => {
const { dir, env } = newCase();
if (!hasFfmpeg) return; // 没判到 pass/warn 就不会被当成可配对的那一张
await runCli(dir, ["shot", "--dir", dir, "--out", "before-1-panel.png",
"--intent", "基线", "--scene", "zh/弹窗"], env);
const r = await runCli(dir, ["shot", "--dir", dir, "--out", "after-1-panel.png",
"--intent", "修复后", "--scene", "en/弹窗"], env);
const after = manifest(dir).find((e) => e.out === "after-1-panel.png")!;
expect(after.issues?.map((i) => i.code)).toContain("scene-mismatch");
expect(after.verdict).toBe("warn");
expect(r.code).toBe(0); // 告警而非拦截:不一致也可能是有意的,但必须被看见
});
});