Computer use for the Pi coding agent — Windows apps, browsers and desktops through native UI trees, screenshots and real input.
TypeScript accessibilityagent-toolsai-agentbrowser-automationcomputer-usecuadesktop-automationelectronpipi-coding-agentpi-extensionpi-packageplaywrighttypescriptui-automationvisionwebview2windowswindows-automationwsl
Tests: a press on a covered point refuses and names the covering window (LPT-955)
alexshpunt-clanker[bot]5216161f2a0c
diff --git a/test/live/occluded-point.live.ts b/test/live/occluded-point.live.tsnew file mode 100644index 0000000..57cdfb5--- /dev/null+++ b/test/live/occluded-point.live.ts@@ -0,0 +1,83 @@+import { afterAll, expect, test } from "vitest";+import { requireComputerAddress } from "../../src/computer-resources.ts";+import { clickPlan, dragPlan, timelinePlan } from "../../src/computer-tools.ts";+import { presentComputerResult } from "../../src/provider.ts";+import { connectLive, openWindow, WINDOWS_POWERSHELL_PATH } from "./remote.ts";++// LPT-955: a real press goes to whatever window is on top at its point. A WinForms pad logs every press it gets. Its+// own dialog covers the pad's top-left part, and a topmost window of another process covers its bottom-right part.+// A press on either part is refused before anything is pressed and names the covering window; a press on the free+// part reaches the pad as before.+const live = await connectLive();+afterAll(async () => { await live.close(); });++// Both processes are DPI-unaware Windows PowerShell, so they place their windows in the same logical coordinates.+const place = `$a = [Windows.Forms.Screen]::PrimaryScreen.WorkingArea; $x = $a.Left + [int]($a.Width / 2) - 210; $y = $a.Top + [int]($a.Height / 2) - 150`;+function script(body: string): string[] {+ return ["-NoProfile", "-WindowStyle", "Hidden", "-EncodedCommand", Buffer.from(`Add-Type -AssemblyName System.Windows.Forms\n${place}\n${body}`, "utf16le").toString("base64")];+}+function padForm(title: string): string[] {+ return script(`$f = New-Object Windows.Forms.Form; $f.Text = '${title}'; $f.StartPosition = 'Manual'; $f.Left = $x; $f.Top = $y; $f.Width = 420; $f.Height = 300+$p = New-Object Windows.Forms.Panel; $p.Left = 10; $p.Top = 10; $p.Width = 380; $p.Height = 160; $p.BackColor = 'LightSteelBlue'; $p.AccessibleName = 'Pad'+$l = New-Object Windows.Forms.Label; $l.Left = 10; $l.Top = 190; $l.Width = 380; $l.Text = 'log:'+$p.Add_MouseDown({ param($s, $e) $l.Text += ' ' + $e.Button.ToString().Substring(0, 1) + $e.Clicks })+$d = New-Object Windows.Forms.Form; $d.Text = '${title} dialog'; $d.StartPosition = 'Manual'; $d.Left = $x + 20; $d.Top = $y + 40; $d.Width = 150; $d.Height = 110+$d.ShowInTaskbar = $false; $d.MinimizeBox = $false; $d.MaximizeBox = $false+$f.Add_Shown({ $d.Show($f) })+$f.Controls.AddRange(@($p, $l)); [void]$f.ShowDialog()`);+}+function coverForm(title: string): string[] {+ return script(`$c = New-Object Windows.Forms.Form; $c.Text = '${title}'; $c.StartPosition = 'Manual'; $c.Left = $x + 260; $c.Top = $y + 110; $c.Width = 150; $c.Height = 100+$c.TopMost = $true; $c.ShowInTaskbar = $false; [void]$c.ShowDialog()`);+}++test("a press on a point that another window covers is refused and names that window", async () => {+ const title = `pi-occluded-${Date.now()}`;+ const window = await openWindow(live, live.machine, WINDOWS_POWERSHELL_PATH, padForm(title), `regex:^${title}$`);+ const cover = await openWindow(live, live.machine, WINDOWS_POWERSHELL_PATH, coverForm(`${title} cover`), `regex:^${title} cover$`);+ try {+ const full = await live.read(window, ["full"]);+ type Element = { role: string; name?: string; bounds: { window_image?: { x: number; y: number; width: number; height: number } } };+ const box = (full.details.ui as { elements: Element[] }).elements.find(item => item.name === "Pad")!.bounds.window_image!;+ // Points of the pad by its own layout: the dialog covers pad x 2..152, y 0..109; the cover pad x 242..392, y 69..169.+ const at = (x: number, y: number) => ({ x: Math.round(box.x + box.width * x / 380), y: Math.round(box.y + box.height * y / 160) });+ const free = at(200, 30), underDialog = at(60, 50), underCover = at(320, 130);+ const log = async () => /log:[^\n|]*/.exec(presentComputerResult(await live.read(window, ["ui"])))![0].trim();+ const act = async (plan: ReturnType<typeof clickPlan>) => {+ const result = await live.resources.input(requireComputerAddress(window), live.cwd, plan);+ const body = presentComputerResult(result);+ console.log(`${result.details.status}\n${body.split("\n").slice(0, 3).join("\n")}`);+ return { status: result.details.status, body };+ };++ expect((await act(clickPlan({ window, ...free }))).status).toBe("delivered");+ await new Promise(resolve => setTimeout(resolve, 300));+ expect(await log()).toBe("log: L1");++ const dialog = await act(clickPlan({ window, ...underDialog }));+ expect(dialog.status).toBe("refused");+ expect(dialog.body).toContain(`Nothing was pressed: "${title} dialog" (computer:`);+ expect(dialog.body).toContain("a window this window owns, covers the point. It stays in front of this window even when this window is active.");++ const other = await act(clickPlan({ window, ...underCover }));+ expect(other.status).toBe("refused");+ expect(other.body).toContain(`Nothing was pressed: a window of another app, "${title} cover" (computer:`);++ const drag = await act(dragPlan({ window, from: underDialog, to: free }));+ expect(drag.status).toBe("refused");+ expect(drag.body).toContain(`"${title} dialog"`);++ const timeline = await act(timelinePlan({ window, events: [{ at_ms: 0, type: "move", ...underCover }, { at_ms: 50, type: "button_down" }, { at_ms: 100, type: "button_up" }] }));+ expect(timeline.status).toBe("refused");+ expect(timeline.body).toContain(`"${title} cover"`);++ // Only the first click reached the pad.+ expect(await log()).toBe("log: L1");+ } finally {+ // Force-close refuses windows the session found by title rather than opened; both processes are this check's own.+ for (const opened of [cover, window]) {+ const { pid } = (await live.read(opened, ["state"])).details.target as { pid: number };+ await live.kill(pid);+ }+ }+});diff --git a/test/live/pen.live.ts b/test/live/pen.live.tsindex 2f57b4c..8217942 100644--- a/test/live/pen.live.ts+++ b/test/live/pen.live.ts@@ -11,7 +11,11 @@ const gimp = process.env.GIMP_PATH ?? "C:\\Users\\user\\AppData\\Local\\Programslet pid = 0;afterAll(async () => { if (pid) await live.kill(pid); await live.close(); });-test("a pen stroke with rising pressure gets thicker in GIMP", async () => {+test("a pen stroke with rising pressure gets thicker in GIMP", async context => {+ // GIMP runs one instance: a running GIMP would take the canvas, and the cleanup below would kill that instance with+ // whatever the person has open in it.+ const running = (await live.powershell("(Get-Process gimp* -ErrorAction SilentlyContinue | Measure-Object).Count")).trim();+ if (running !== "0") context.skip("GIMP is already running on the machine; close it to run this check.");await sharp({ create: { width: 1200, height: 800, channels: 3, background: "#ffffff" } }).png().toFile(".tmp/pen-canvas.png");const put = await live.insert(live.machine, { action: "put", from: ".tmp/pen-canvas.png" });await live.insert(live.machine, { action: "open", path: gimp, args: [put.details.state!.path as string] });diff --git a/test/occluded-point.test.ts b/test/occluded-point.test.tsnew file mode 100644index 0000000..6460862--- /dev/null+++ b/test/occluded-point.test.ts@@ -0,0 +1,74 @@+import { expect, test, vi } from "vitest";+import { ComputerSession, type DriverResult, type Driver } from "../src/session.ts";++// LPT-955: the driver checks which window a real press at a point reaches and refuses before pressing when another+// window covers it. The receipt names that window, its address and how it relates to the target.+const target = { pid: 42, window_id: 100 };+const covering = { pid: 42, window_id: 200 };+function fixture(refusal: DriverResult, mode: "yolo" | "safe" = "yolo") {+ let windows: object[] = [];+ const driver: Driver = {+ identity: vi.fn(async window => `${window.pid}:${window.window_id}`),+ call: vi.fn<Driver["call"]>(async name => {+ if (name === "list_windows") return { content: [], structuredContent: { windows } };+ if (name === "list_apps") return { content: [], structuredContent: { apps: [{ name: "Pad", launch_path: "C:\\Pad\\pad.exe" }] } };+ if (name === "launch_app") windows = [{ ...target, title: "Pad" }];+ if (name === "launch_app") return { content: [{ type: "text", text: JSON.stringify({ pid: 42, windows: [{ ...target, title: "Pad" }] }) }] };+ if (name === "get_window_state") return { content: [{ type: "image", mimeType: "image/png", data: "cG5n" }],+ structuredContent: { snapshot_id: "s1", screenshot_width: 400, screenshot_height: 300, elements: [] } };+ return refusal;+ }),+ close: vi.fn(async () => {}),+ };+ return new ComputerSession(driver, () => mode);+}+const covered = (relation: string, title: string) => `pointer_target_occluded: screen point (310,220) is covered by x: window_id=${covering.window_id} pid=${covering.pid} relation=${relation} title=${JSON.stringify(title)}; nothing was pressed there`;+async function click(session: ComputerSession) {+ const opened = await session.open("Pad");+ return session.act({ window_ref: opened.details.window_ref!, observation_id: opened.details.observation_id!, delivery_mode: "foreground", action: "click", x: 10, y: 10 });+}+const message = (result: Awaited<ReturnType<typeof click>>) => result.content.filter(block => block.type === "text").map(block => block.text).join("\n");++test("a click under the target's own dialog refuses and names the dialog with its address", async () => {+ const session = fixture({ isError: true, content: [{ type: "text", text: covered("owned_window", "Change \"Foreground\" Color") }],+ structuredContent: { status: "refused", code: "pointer_target_occluded", effect: "refused" } });+ const result = await click(session);+ expect(result.details.status).toBe("refused");+ const text = message(result);+ expect(text).toMatch(/^Nothing was pressed: "Change "Foreground" Color" \(computer:window\/w-[0-9a-f-]+\), a window this window owns, covers the point\./);+ expect(text).toContain("It stays in front of this window even when this window is active.");+ // The named address is a window this session can read and act on.+ const occluder = /computer:window\/(w-[0-9a-f-]+)/.exec(text)![1];+ expect(session.windowTarget(occluder)).toEqual(covering);+});++test("a drag or timeline under another app's window refuses the same way", async () => {+ const session = fixture({ isError: true, content: [{ type: "text", text: `Windows held path: 0/3 points, pressed=false, released=false. ${covered("other_process", "Overlay")}` }],+ structuredContent: { code: "pointer_target_occluded", effect: "refused" } });+ const result = await click(session);+ expect(result.details.status).toBe("refused");+ expect(message(result)).toMatch(/^Nothing was pressed: a window of another app, "Overlay" \(computer:window\/w-[0-9a-f-]+\), covers the point\./);+});++test("another window of the same app is named as such", async () => {+ const session = fixture({ isError: true, content: [{ type: "text", text: covered("same_process", "Layers") }],+ structuredContent: { status: "refused", code: "pointer_target_occluded", effect: "refused" } });+ expect(message(await click(session))).toMatch(/^Nothing was pressed: another window of this app, "Layers" \(computer:window\//);+});++test("Safe mode names the covering window without granting it an address", async () => {+ const session = fixture({ isError: true, content: [{ type: "text", text: covered("other_process", "Overlay") }],+ structuredContent: { status: "refused", code: "pointer_target_occluded", effect: "refused" } }, "safe");+ const result = await click(session);+ expect(result.details.status).toBe("refused");+ expect(message(result)).toMatch(/^Nothing was pressed: a window of another app, "Overlay", covers the point\./);+});++test("a timeline that stops at a covered press after earlier events stays unknown and says where it stopped", async () => {+ const session = fixture({ content: [{ type: "text", text: `Input timeline: 2/5 events sent in 80 ms; everything held released: true. ${covered("other_process", "Overlay")}` }],+ structuredContent: { timeline: { events_sent: 2 } } });+ // A partial delivery carries no refusal; the receipt names the covering window above the usual guidance.+ const result = await click(session);+ expect(result.details.status).toBe("unknown");+ expect(message(result)).toMatch(/^A press stopped: a window of another app, "Overlay" \(computer:window\/w-[0-9a-f-]+\), covers the point\..*\nDriver returned unknown/s);+});