import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { RunConfig } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { menuGroupItems, registerMenuGroup } from "@/workbench/contributions"; import type { MenuItem } from "@/ui"; import { canDebugProduced, choiceItems, compiledDebugPlan, contextMenuItems, projectContext } from "./contextRun"; import { OPTIMISTIC, produceContext, produceContextChoices, type ToolEnv } from "./producers"; import { gutterTestState, markerIcon, targetMenuItems } from "./runGutter"; import { useRun } from "./store"; import type { RunTarget } from "./targets"; import { TestResults } from "./testing"; import { clearTestStates, recordTestStates } from "./testing/states"; import { setInvokeRoute } from "@/platform/tauriCore"; const mocks = vi.hoisted(() => ({ tauri: { value: false }, read: { value: null as string | null }, toolEnv: { value: null as ((...args: unknown[]) => unknown) | null }, })); vi.mock("@/platform/ipc", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, isTauri: () => mocks.tauri.value }; }); vi.mock("./runtimes", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, toolEnv: (...args: Parameters) => mocks.toolEnv.value ? mocks.toolEnv.value(...args) : actual.toolEnv(...args), }; }); const ROOT = "/work/app"; const SCRATCHES = "/Users/me/scratches"; const npmTest: RunConfig = { id: "npm:test", name: "test", kind: "npm", command: "npm", args: ["run", "test"], cwd: null, env: {}, detected: true, isTest: true, debugAdapter: "js-debug", }; type Item = ReturnType[number]; // Flattens "More Run/Debug" so its entries read in menu order. const labels = (items: Item[]): string[] => items.flatMap((i) => i.kind === "action" ? [`${i.action}|${i.label}|${i.icon}`] : i.kind === undefined || i.kind === "item" ? "submenu" in i && i.submenu ? labels(i.submenu) : [i.label] : [i.kind], ); describe("run context menu group", () => { beforeEach(() => { useProject.setState({ project: { name: "app", root: ROOT, trusted: true, isGit: false, settings: {} } as never }); useRun.setState({ configs: [npmTest], loaded: true }); }); afterEach(() => { useProject.setState({ project: null }); useRun.setState({ configs: [], loaded: false }); }); it("editor: Run / Debug '' and More Run/Debug › Modify Run Configuration… for a runnable scratch", () => { const items = contextMenuItems("editor", { paths: [`${SCRATCHES}/hi.ts`], text: "console.log(123)", line: 0 }); expect(labels(items)).toEqual([ "run.runContext|Run 'hi.ts'|run", "run.debugContext|Debug 'hi.ts'|debug", "run.modifyContext|Modify Run Configuration…|edit", ]); // The actions get the context (path + caret line), so the menu runs what it shows. expect(items[0]).toMatchObject({ arg: { path: `${SCRATCHES}/hi.ts`, line: 0, dir: false } }); }); it("editor: offers Run / Debug '