import { execFileSync, spawnSync } from "node:child_process"; import { existsSync } from "node:fs"; import { extname, resolve } from "node:path"; import { supportedBunVersion } from "./prepare-dev-environment"; import { readScriptArguments } from "./script-arguments"; export type VerificationSurface = "desktop" | "renderer" | "mobile" | "api" | "remote" | "contracts" | "docs"; export interface DevVerificationReport { ready: boolean; reasons: string[]; setup: { bun: { ready: boolean; current: string; expected: string }; dependencies: boolean; developmentEnv: boolean; }; changes: { files: string[]; surfaces: VerificationSurface[]; suggestedTests: string[] }; runtime: { stackRunning: boolean; appRunning: boolean; isolatedApp: boolean; orphanedStack: boolean; ambiguousApp: boolean; }; qa: { required: boolean; ready: boolean; reasons: string[]; loop: string[]; }; commands: string[]; runnableCommands: string[]; qaCommands: string[]; } const rendererQaLoop = [ "bun run dev:automation snapshot", "bun run dev:automation --role= --name= --allow-mutations --wait-for=,", "bun run dev:automation snapshot --wait-for=,", "bun run dev:automation screenshot --wait-for=,", ]; export function surfacesForFiles(files: string[]): VerificationSurface[] { const surfaces = new Set(); for (const file of files) { if (file.startsWith("src/renderer/")) surfaces.add("renderer"); if (file.startsWith("src/main/") || file.startsWith("src/backend/") || file.startsWith("scripts/")) { surfaces.add("desktop"); } if (file.startsWith("apps/mobile/")) surfaces.add("mobile"); if (file.startsWith("apps/auth-api/") || file.startsWith("apps/site-router/")) surfaces.add("api"); if (file.startsWith("remote/")) surfaces.add("remote"); if (file.startsWith("packages/contracts/")) surfaces.add("contracts"); if (file.startsWith("docs/") || file === "README.md") surfaces.add("docs"); } return [...surfaces].sort(); } function isTestFile(file: string): boolean { return /\.(test|dom\.test)\.tsx?$/u.test(file); } function isDesktopTest(file: string): boolean { if (!isTestFile(file)) return false; return [ "src/backend/", "src/main/", "src/preload/", "src/renderer/", "scripts/", "packages/logging/", "apps/mobile/src/", ].some((prefix) => file.startsWith(prefix)); } function isDelegatedTest(file: string): boolean { return file.startsWith("apps/auth-api/test/") && isTestFile(file); } function isSupportedTest(file: string): boolean { return isDesktopTest(file) || isDelegatedTest(file); } export function suggestedTestsForFiles(files: string[], projectRoot = process.cwd()): string[] { const tests = new Set(files.filter((file) => isSupportedTest(file) && existsSync(resolve(projectRoot, file)))); for (const file of files) { if (isTestFile(file)) continue; const extension = extname(file); if (extension !== ".ts" && extension !== ".tsx") continue; const stem = file.slice(0, -extension.length); for (const suffix of [`.test${extension}`, `.dom.test${extension}`]) { const candidate = `${stem}${suffix}`; if (isSupportedTest(candidate) && existsSync(resolve(projectRoot, candidate))) tests.add(candidate); } } return [...tests].sort(); } interface VerificationCommandPlan { commands: string[]; runnableCommands: string[]; runnableCommandArgs: string[][]; qaCommands: string[]; suggestedTests: string[]; } function formatBunCommand(args: string[]): string { return `bun ${args.join(" ")}`; } function testCommandArgs(tests: string[]): string[][] { const desktop = tests.filter(isDesktopTest); const auth = tests.filter((file) => file.startsWith("apps/auth-api/test/")); const commands: string[][] = []; if (desktop.length > 0) commands.push(["run", "test:desktop", "--", ...desktop]); if (auth.length > 0) { commands.push([ "run", "--cwd", "apps/auth-api", "test", "--", ...auth.map((file) => file.slice("apps/auth-api/".length)), ]); } return commands; } function workspaceTestCommandArgs(files: string[], suggestedTests: string[]): string[][] { const commands: string[][] = []; if ( files.some((file) => file.startsWith("apps/auth-api/")) && !suggestedTests.some((file) => file.startsWith("apps/auth-api/")) ) { commands.push(["run", "test:api"]); } return commands; } export function verificationCommands( files: string[], surfaces: VerificationSurface[], setupReady: boolean, isolatedApp: boolean, projectRoot = process.cwd(), ): Omit { const plan = createVerificationCommandPlan(files, surfaces, setupReady, isolatedApp, projectRoot); return { commands: plan.commands, runnableCommands: plan.runnableCommands, qaCommands: plan.qaCommands, suggestedTests: plan.suggestedTests, }; } function createVerificationCommandPlan( files: string[], surfaces: VerificationSurface[], setupReady: boolean, isolatedApp: boolean, projectRoot = process.cwd(), ): VerificationCommandPlan { const commands: string[] = []; const runnableCommandArgs: string[][] = []; const qaCommands: string[] = []; if (!setupReady) commands.push("bun run dev:prepare"); const suggestedTests = suggestedTestsForFiles(files, projectRoot); for (const args of testCommandArgs(suggestedTests)) { commands.push(formatBunCommand(args)); runnableCommandArgs.push(args); } for (const args of [["run", "lint"], ["run", "typecheck"], ...workspaceTestCommandArgs(files, suggestedTests)]) { const command = formatBunCommand(args); if (!commands.includes(command)) commands.push(command); if (!runnableCommandArgs.some((candidate) => formatBunCommand(candidate) === command)) runnableCommandArgs.push(args); } if (surfaces.includes("api")) commands.push("bun run check:api"); if (surfaces.includes("renderer")) { commands.push("bun run check:ui"); runnableCommandArgs.push(["run", "check:ui"]); } if (surfaces.includes("renderer") && !isolatedApp) commands.push("bun run dev"); if (surfaces.includes("renderer")) { qaCommands.push("bun run dev:automation snapshot", "bun run dev:automation screenshot"); commands.push(...qaCommands); } return { commands, runnableCommands: runnableCommandArgs.map(formatBunCommand), runnableCommandArgs, qaCommands, suggestedTests, }; } function changedFiles(projectRoot: string): string[] { const tracked = execFileSync("git", ["diff", "--name-only", "HEAD"], { cwd: projectRoot, encoding: "utf8" }); const untracked = execFileSync("git", ["ls-files", "--others", "--exclude-standard"], { cwd: projectRoot, encoding: "utf8", }); return [ ...new Set( `${tracked}\n${untracked}` .split("\n") .map((file) => file.trim()) .filter(Boolean), ), ].sort(); } function readinessReasons(setup: DevVerificationReport["setup"], runtime: DevVerificationReport["runtime"]): string[] { const reasons: string[] = []; if (!setup.bun.ready) reasons.push(`Bun ${setup.bun.expected} is required; current version is ${setup.bun.current}.`); if (!setup.dependencies) reasons.push("node_modules is missing. Run bun run dev:prepare."); if (!setup.developmentEnv) reasons.push(".openbot/dev-state.json is missing. Run bun run dev:prepare."); if (runtime.orphanedStack) reasons.push("This worktree has an orphaned dev stack. Inspect bun run dev:status."); if (runtime.ambiguousApp) reasons.push("More than one app instance matches this worktree."); return reasons; } export function qaReadinessReasons( surfaces: VerificationSurface[], setupReady: boolean, runtime: DevVerificationReport["runtime"], ): string[] { if (!surfaces.includes("renderer")) return []; const reasons: string[] = []; if (!setupReady) reasons.push("Development setup is incomplete. Run bun run dev:prepare before renderer QA."); if (runtime.orphanedStack) reasons.push("This worktree has an orphaned dev stack. Inspect bun run dev:status."); if (runtime.ambiguousApp) reasons.push("More than one app instance matches this worktree."); if (!runtime.appRunning && !runtime.ambiguousApp) reasons.push("No running app matches this worktree."); if (runtime.appRunning && !runtime.isolatedApp) { reasons.push( "The running app uses the shared profile. Start bun run dev without --shared for isolated renderer QA.", ); } return reasons; } async function createDevVerificationState( projectRoot = process.cwd(), ): Promise<{ report: DevVerificationReport; runnableCommandArgs: string[][]; setupReady: boolean }> { const files = changedFiles(projectRoot); const surfaces = surfacesForFiles(files); const bunCurrent = process.versions.bun ?? "unknown"; const setup = { bun: { ready: bunCurrent === supportedBunVersion, current: bunCurrent, expected: supportedBunVersion }, dependencies: existsSync(resolve(projectRoot, "node_modules")), developmentEnv: existsSync(resolve(projectRoot, ".openbot/dev-state.json")), }; const setupReady = setup.bun.ready && setup.dependencies && setup.developmentEnv; let runtime: DevVerificationReport["runtime"] = { stackRunning: false, appRunning: false, isolatedApp: false, orphanedStack: false, ambiguousApp: false, }; if (setup.dependencies) { const [{ readDevInstanceRecords }, { isOrphanedDevStack, isSameWorktree, readDevStackRecords }] = await Promise.all( [import("./dev-automation/instance-registry"), import("./dev-automation/stack-registry")], ); const localStacks = readDevStackRecords().filter((stack) => isSameWorktree(stack, projectRoot)); const appInstances = readDevInstanceRecords().filter( (instance) => resolve(instance.projectRoot) === resolve(projectRoot) && instance.service === "app", ); runtime = { stackRunning: localStacks.length > 0, appRunning: appInstances.length === 1, isolatedApp: appInstances.length === 1 && appInstances[0]?.instanceId !== "default", orphanedStack: localStacks.some((stack) => isOrphanedDevStack(stack)), ambiguousApp: appInstances.length > 1, }; } const isolatedApp = runtime.isolatedApp; const reasons = readinessReasons(setup, runtime); const plan = createVerificationCommandPlan(files, surfaces, setupReady, isolatedApp, projectRoot); const qaReasons = qaReadinessReasons(surfaces, setupReady, runtime); const qaRequired = surfaces.includes("renderer"); const report = { ready: reasons.length === 0, reasons, setup, changes: { files, surfaces, suggestedTests: plan.suggestedTests }, runtime, qa: { required: qaRequired, ready: !qaRequired || qaReasons.length === 0, reasons: qaReasons, loop: qaRequired ? rendererQaLoop : [], }, commands: plan.commands, runnableCommands: plan.runnableCommands, qaCommands: plan.qaCommands, }; return { report, runnableCommandArgs: plan.runnableCommandArgs, setupReady }; } export async function createDevVerificationReport(projectRoot = process.cwd()): Promise { return (await createDevVerificationState(projectRoot)).report; } function runBun(projectRoot: string, args: string[]): void { process.stderr.write(`dev:verify running: bun ${args.join(" ")}\n`); const result = spawnSync(process.execPath, args, { cwd: projectRoot, stdio: "inherit" }); if (result.status !== 0) process.exit(result.status ?? 1); } function runRecommendedChecks(commandArgs: string[][], projectRoot: string): void { for (const args of commandArgs) runBun(projectRoot, args); } const USAGE = [ "Usage: bun run dev:verify [--run]", "", "Prints a JSON verification plan for the uncommitted changes in this worktree.", " --run Also run the safe checks in the plan that change nothing.", ].join("\n"); if (import.meta.main) { const parsed = readScriptArguments({ usage: USAGE, options: { run: { type: "boolean" } } }); if (parsed !== null) { const projectRoot = process.cwd(); const { report, runnableCommandArgs, setupReady } = await createDevVerificationState(projectRoot); process.stdout.write(`${JSON.stringify(report, null, 2)}\n`); if (parsed.values.run && setupReady) runRecommendedChecks(runnableCommandArgs, projectRoot); } }