import { describe, expect, test } from "bun:test"; import { readFileSync } from "node:fs"; import path from "node:path"; import type { Security } from "@bbbug/shared/types"; import { buildSecurityFirstPrompt, parseOnlineAdbDeviceIds } from "../src/security_runner"; const RUNNER_SOURCE = path.resolve(import.meta.dir, "../src/security_runner.ts"); const security = { id: "security-1", title: "clipboard access", platform: "android", attempts: 0, targetRef: null, lang: "zh", reviewHistory: [], reviewFeedback: null, } as unknown as Security; describe("security Android device selection", () => { test("keeps runner preparation and device handoff out of the Agent prompt", () => { const prompt = buildSecurityFirstPrompt(security, "/tmp/security-workspace"); expect(prompt).not.toContain("adb devices -l"); expect(prompt).not.toContain("selected-adb-device.txt"); expect(prompt).not.toContain("tiktok-run-pre"); expect(prompt).not.toContain("security-tiktok-patch"); expect(prompt).not.toContain("git checkout"); expect(prompt).not.toContain("唯一目标 ADB 设备 ID"); expect(prompt).toContain("ADB 设备由你根据任务自行选择"); expect(prompt).toContain("如果你的复现需要多个设备"); }); test("parses every online ADB device and ignores unavailable rows", () => { const output = [ "List of devices attached", "device-a device product:a model:A transport_id:1", "device-b\tdevice usb:1-2 product:b model:B transport_id:2", "device-off offline transport_id:3", "device-locked unauthorized transport_id:4", "device-a device product:a model:A transport_id:1", "", ].join("\n"); expect(parseOnlineAdbDeviceIds(output)).toEqual(["device-a", "device-b"]); }); test("hardcodes the published run hooks without URL environment overrides", () => { const source = readFileSync(RUNNER_SOURCE, "utf8"); const preUrlLine = source.split("\n").find((line) => line.includes("security-tiktok-run-pre_2610131148.sh")); const postUrlLine = source.split("\n").find((line) => line.includes("security-tiktok-run-post_2609221459.sh")); expect(preUrlLine).toBeDefined(); expect(preUrlLine).toContain("https://voffline.byted.org/download/tos/schedule/"); expect(preUrlLine).not.toContain("process.env"); expect(postUrlLine).toBeDefined(); expect(postUrlLine).toContain("https://voffline.byted.org/download/tos/schedule/"); expect(postUrlLine).not.toContain("process.env"); }); test("places the single pre hook before all automatic retry loops", () => { const source = readFileSync(RUNNER_SOURCE, "utf8"); const runStart = source.indexOf("export async function runSecurity("); const runBody = source.slice(runStart); const preHook = runBody.indexOf('phase: "pre"'); const retryLoop = runBody.indexOf("while (!terminalFinalized)"); expect(runStart).toBeGreaterThanOrEqual(0); expect(preHook).toBeGreaterThanOrEqual(0); expect(preHook).toBeLessThan(retryLoop); expect(runBody.match(/phase: "pre"/g)?.length).toBe(1); expect(runBody.slice(preHook, retryLoop)).toContain("retryable: false"); }); test("validates Agent artifacts before post-run and keeps retry targets separate", () => { const source = readFileSync(RUNNER_SOURCE, "utf8"); const runStart = source.indexOf("export async function runSecurity("); const runBody = source.slice(runStart); const agentTurn = runBody.indexOf("streamJson = await runSecurityAgentTurn"); const validation = runBody.indexOf("const validation = validateSecurityArtifacts"); const postHook = runBody.indexOf("while (!postHookCompleted)"); const quotaMonitor = runBody.indexOf("await monitorSecurityAccountAfterSuccess"); expect(agentTurn).toBeGreaterThanOrEqual(0); expect(validation).toBeGreaterThan(agentTurn); expect(postHook).toBeGreaterThan(validation); expect(quotaMonitor).toBeGreaterThan(postHook); const validationBlock = runBody.slice(validation, postHook); expect(validationBlock).toContain("if (!validation.ok)"); expect(validationBlock).toContain("agentSession = prepareAgentSession"); expect(validationBlock).toContain("buildSecurityRetryPrompt"); expect(validationBlock).toContain("continue;"); const postBlock = runBody.slice(postHook, quotaMonitor); expect(postBlock).toContain("POST_HOOK_FAILED"); expect(postBlock).toContain("Retry only the failed hook"); expect(postBlock).not.toContain("runSecurityAgentTurn"); expect(postBlock).not.toContain("validateSecurityArtifacts"); }); });