mirror of
https://github.com/samsonjs/Peekaboo.git
synced 2026-03-25 09:25:47 +00:00
Adds support for capturing the frontmost window of the frontmost application instead of falling back to screen capture mode. Changes: - Added 'frontmost' case to CaptureMode enum in Swift CLI - Implemented captureFrontmostWindow() method using NSWorkspace.shared.frontmostApplication - Updated TypeScript to use --mode frontmost instead of defaulting to screen mode - Added comprehensive test coverage for frontmost functionality - Updated existing tests to reflect new behavior The frontmost mode now: 1. Detects the currently active application 2. Captures only its frontmost window (index 0) 3. Returns a single image file with proper metadata 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
173 lines
4.2 KiB
TypeScript
173 lines
4.2 KiB
TypeScript
import {
|
|
SwiftCliResponse,
|
|
ApplicationListData,
|
|
WindowListData,
|
|
ImageCaptureData,
|
|
} from "../../src/types/index";
|
|
import { vi } from "vitest";
|
|
|
|
// Mock Swift CLI responses for testing
|
|
export const mockSwiftCli = {
|
|
// Mock successful application list response
|
|
listApplications(): SwiftCliResponse {
|
|
return {
|
|
success: true,
|
|
data: {
|
|
applications: [
|
|
{
|
|
app_name: "Safari",
|
|
bundle_id: "com.apple.Safari",
|
|
pid: 1234,
|
|
is_active: true,
|
|
window_count: 2,
|
|
},
|
|
{
|
|
app_name: "Cursor",
|
|
bundle_id: "com.todesktop.230313mzl4w4u92",
|
|
pid: 5678,
|
|
is_active: false,
|
|
window_count: 1,
|
|
},
|
|
],
|
|
} as ApplicationListData,
|
|
messages: [],
|
|
};
|
|
},
|
|
|
|
// Mock successful window list response
|
|
listWindows(appName: string): SwiftCliResponse {
|
|
return {
|
|
success: true,
|
|
data: {
|
|
target_application_info: {
|
|
app_name: appName,
|
|
bundle_id: `com.apple.${appName}`,
|
|
pid: 1234,
|
|
},
|
|
windows: [
|
|
{
|
|
window_title: `${appName} - Main Window`,
|
|
window_id: 1,
|
|
window_index: 0,
|
|
bounds: { x: 100, y: 100, width: 800, height: 600 },
|
|
is_on_screen: true,
|
|
},
|
|
{
|
|
window_title: `${appName} - Secondary Window`,
|
|
window_id: 2,
|
|
window_index: 1,
|
|
bounds: { x: 200, y: 200, width: 600, height: 400 },
|
|
is_on_screen: true,
|
|
},
|
|
],
|
|
} as WindowListData,
|
|
messages: [],
|
|
};
|
|
},
|
|
|
|
// Mock successful image capture response
|
|
captureImage(
|
|
mode: string,
|
|
options?: { app?: string; path?: string; format?: string },
|
|
): SwiftCliResponse {
|
|
const appName = options?.app;
|
|
const format = options?.format || "png";
|
|
const defaultFileName = appName
|
|
? `${appName.toLowerCase()}_window.${format}`
|
|
: `screen_capture.${format}`;
|
|
const actualPath = options?.path || `/tmp/${defaultFileName}`;
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
saved_files: [
|
|
{
|
|
path: actualPath,
|
|
item_label: appName ? `${appName} Window` : "Screen Capture",
|
|
window_title: appName ? `${appName} - Main Window` : undefined,
|
|
window_id: appName ? 1 : undefined,
|
|
mime_type: `image/${format === "jpg" ? "jpeg" : format}`,
|
|
},
|
|
],
|
|
} as ImageCaptureData,
|
|
messages: [],
|
|
};
|
|
},
|
|
|
|
// Mock frontmost window capture response
|
|
captureFrontmostWindow(): SwiftCliResponse {
|
|
return {
|
|
success: true,
|
|
data: {
|
|
saved_files: [
|
|
{
|
|
path: "/tmp/frontmost_Safari_20250608_083000.png",
|
|
item_label: "Safari",
|
|
window_title: "Example Website - Safari",
|
|
window_id: 12345,
|
|
window_index: 0,
|
|
mime_type: "image/png",
|
|
},
|
|
],
|
|
} as ImageCaptureData,
|
|
messages: [],
|
|
};
|
|
},
|
|
|
|
// Mock error responses
|
|
permissionDenied(): SwiftCliResponse {
|
|
return {
|
|
success: false,
|
|
error: {
|
|
message: "Permission denied. Screen recording permission required.",
|
|
code: "PERMISSION_DENIED",
|
|
},
|
|
};
|
|
},
|
|
|
|
appNotFound(appName: string): SwiftCliResponse {
|
|
return {
|
|
success: false,
|
|
error: {
|
|
message: `Application '${appName}' not found or not running.`,
|
|
code: "APP_NOT_FOUND",
|
|
},
|
|
};
|
|
},
|
|
|
|
// Mock server status response
|
|
serverStatus(): SwiftCliResponse {
|
|
return {
|
|
success: true,
|
|
data: {
|
|
server_version: "1.1.1",
|
|
swift_cli_version: "1.0.0",
|
|
status: "running",
|
|
},
|
|
messages: [],
|
|
};
|
|
},
|
|
};
|
|
|
|
// Mock child_process.spawn for Swift CLI execution
|
|
export const mockChildProcess = {
|
|
spawn: vi.fn().mockImplementation(() => ({
|
|
stdout: {
|
|
on: vi.fn((event, callback) => {
|
|
if (event === "data") {
|
|
callback(
|
|
Buffer.from(JSON.stringify(mockSwiftCli.listApplications())),
|
|
);
|
|
}
|
|
}),
|
|
},
|
|
stderr: {
|
|
on: vi.fn(),
|
|
},
|
|
on: vi.fn((event, callback) => {
|
|
if (event === "close") {
|
|
callback(0);
|
|
}
|
|
}),
|
|
})),
|
|
};
|