diff --git a/src/browser/PlaywrightElectronActiveProvider.test.ts b/src/browser/PlaywrightElectronActiveProvider.test.ts index 3b9134941..2ddece3a6 100644 --- a/src/browser/PlaywrightElectronActiveProvider.test.ts +++ b/src/browser/PlaywrightElectronActiveProvider.test.ts @@ -1,9 +1,14 @@ import { afterEach, expect, it, vi } from "vitest"; +import * as processOwnership from "../process/ProcessOwnership.js"; import { electronActiveObservationInputSchema } from "../domain/javascript/electronActiveObservation.js"; +import * as electronActions from "./PlaywrightElectronActiveActions.js"; import { PlaywrightElectronActiveProvider } from "./PlaywrightElectronActiveProvider.js"; -afterEach(() => vi.unstubAllEnvs()); +afterEach(() => { + vi.restoreAllMocks(); + vi.unstubAllEnvs(); +}); it("passes the selected environment and ownership token to Electron launch", async () => { vi.stubEnv("LANG", "ambient-language"); @@ -33,3 +38,78 @@ it("passes the selected environment and ownership token to Electron launch", asy REA_PROCESS_RUN_ID: expect.any(String), }); }); + +it("retains the partial capture without taskkill when Windows lineage is unavailable", async () => { + // Exercise the Windows decision branch; this is not native Windows coverage. + const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); + Object.defineProperty(process, "platform", { value: "win32" }); + const terminateTree = vi + .spyOn(processOwnership, "cleanupWindowsProcessTree") + .mockResolvedValue({ cleaned: true, signaled: true }); + const hookSnapshot = { + events: [], + retention_budget_bytes: 0, + estimated_retained_bytes: 0, + event_serialized_byte_upper_bound: 0, + retained: 0, + dropped: 0, + dropped_ipc: 0, + dropped_runtime: 0, + dropped_event_families: [], + dropped_event_roles: [], + observed: 0, + observed_ipc: 0, + observed_runtime: 0, + hook_error: false, + }; + vi.spyOn(electronActions, "readApplicationState").mockResolvedValue({ + windows: [], + metrics: [], + electronVersion: "44.0.0", + hookSnapshot, + }); + const application = { + process: () => ({ pid: 424242 }), + close: vi.fn(async () => undefined), + windows: () => [], + }; + const provider = new PlaywrightElectronActiveProvider( + {}, + async () => + application as unknown as Awaited< + ReturnType + >, + ); + + try { + const result = await provider.capture( + electronActiveObservationInputSchema.parse({ + executable_path: process.execPath, + application_path: process.execPath, + }), + ); + + expect(result.ok).toBe(false); + if (result.ok) throw new Error("Expected unverified cleanup to fail"); + expect(terminateTree).not.toHaveBeenCalled(); + expect(result.error).toMatchObject({ + _tag: "BrowserObservationError", + reason: "cleanup_failed", + cleanup: { + reason: + "owned Electron lineage was unavailable; helper cleanup was not proven", + }, + partialObservation: { + kind: "electron-active-observation", + capture: { + application: { + cleanup: "unverified", + }, + }, + }, + }); + } finally { + if (originalPlatform !== undefined) + Object.defineProperty(process, "platform", originalPlatform); + } +}); diff --git a/src/browser/PlaywrightElectronActiveProvider.ts b/src/browser/PlaywrightElectronActiveProvider.ts index 524375429..8faf1249f 100644 --- a/src/browser/PlaywrightElectronActiveProvider.ts +++ b/src/browser/PlaywrightElectronActiveProvider.ts @@ -264,14 +264,14 @@ const cleanupElectronProcesses = async ( lineage: ProcessLineageObservation | undefined, ): Promise => { if (process.platform === "win32") { - const root = await cleanupWindowsProcessTree(ownership.leaderPid); - if (!root.cleaned) return root; if (lineage?.status !== "verified") return { cleaned: false, reason: "owned Electron lineage was unavailable; helper cleanup was not proven", }; + const root = await cleanupWindowsProcessTree(ownership.leaderPid); + if (!root.cleaned) return root; for (const descendant of lineage.lineage.descendants) { const result = await cleanupWindowsProcessTree(descendant.pid); if (!result.cleaned) return result;