Peekaboo/tests/unit/utils/colon-parsing.test.ts
Peter Steinberger dd680eb638 feat: Improve window title matching and error messages for URLs with ports
When users search for windows with URLs containing ports (e.g., 'http://example.com:8080'),
the system now provides much better debugging information when the window isn't found.

Key improvements:
- Enhanced window not found errors now list all available window titles
- Added specific guidance for URL-based searches (try without protocol)
- New CaptureError.windowTitleNotFound with detailed debugging info
- Comprehensive test coverage for colon parsing in app targets
- Better error messages help users understand why matching failed

Example improved error:
"Window with title containing 'http://example.com:8080' not found in Google Chrome.
Available windows: 'example.com:8080 - Google Chrome', 'New Tab - Google Chrome'.
Note: For URLs, try without the protocol (e.g., 'example.com:8080' instead of 'http://example.com:8080')."

This addresses the common issue where browsers display simplified URLs in window titles
without the protocol, making it easier for users to find the correct matching pattern.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-08 08:09:47 +01:00

116 lines
No EOL
4.1 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { buildSwiftCliArgs } from "../../../src/utils/image-cli-args";
describe("App Target Colon Parsing", () => {
it("should correctly parse window title with URLs containing ports", () => {
const input = {
app_target: "Google Chrome:WINDOW_TITLE:http://example.com:8080",
format: "png" as const
};
const args = buildSwiftCliArgs(input, "/tmp/test.png");
// Should contain the correct app name
expect(args).toContain("--app");
const appIndex = args.indexOf("--app");
expect(args[appIndex + 1]).toBe("Google Chrome");
// Should be in window mode
expect(args).toContain("--mode");
const modeIndex = args.indexOf("--mode");
expect(args[modeIndex + 1]).toBe("window");
// Should contain the window title argument with the full URL including port
expect(args).toContain("--window-title");
const titleIndex = args.indexOf("--window-title");
expect(args[titleIndex + 1]).toBe("http://example.com:8080");
});
it("should handle URLs with multiple colons correctly", () => {
const input = {
app_target: "Safari:WINDOW_TITLE:https://user:pass@example.com:8443/path?param=value",
format: "png" as const
};
const args = buildSwiftCliArgs(input, "/tmp/test.png");
expect(args).toContain("--window-title");
const titleIndex = args.indexOf("--window-title");
expect(args[titleIndex + 1]).toBe("https://user:pass@example.com:8443/path?param=value");
});
it("should handle window titles with colons in file paths", () => {
const input = {
app_target: "TextEdit:WINDOW_TITLE:C:\\Users\\test\\file.txt",
format: "png" as const
};
const args = buildSwiftCliArgs(input, "/tmp/test.png");
expect(args).toContain("--window-title");
const titleIndex = args.indexOf("--window-title");
expect(args[titleIndex + 1]).toBe("C:\\Users\\test\\file.txt");
});
it("should handle simple window titles without additional colons", () => {
const input = {
app_target: "TextEdit:WINDOW_TITLE:My Document.txt",
format: "png" as const
};
const args = buildSwiftCliArgs(input, "/tmp/test.png");
expect(args).toContain("--window-title");
const titleIndex = args.indexOf("--window-title");
expect(args[titleIndex + 1]).toBe("My Document.txt");
});
it("should handle window index correctly (no colons in value)", () => {
const input = {
app_target: "Google Chrome:WINDOW_INDEX:0",
format: "png" as const
};
const args = buildSwiftCliArgs(input, "/tmp/test.png");
expect(args).toContain("--window-index");
const indexIdx = args.indexOf("--window-index");
expect(args[indexIdx + 1]).toBe("0");
});
it("should handle colons in app names gracefully", () => {
// Edge case: what if app name itself contains colons?
const input = {
app_target: "App:Name:WINDOW_TITLE:Title",
format: "png" as const
};
const args = buildSwiftCliArgs(input, "/tmp/test.png");
// This case is ambiguous - current logic takes first part as app name
expect(args).toContain("--app");
const appIndex = args.indexOf("--app");
expect(args[appIndex + 1]).toBe("App");
// "Name" is not a valid specifier, so no window-specific flags should be added
// It should default to main window (no --window-title or --window-index flags)
expect(args).not.toContain("--window-title");
expect(args).not.toContain("--window-index");
expect(args).toContain("--mode");
const modeIndex = args.indexOf("--mode");
expect(args[modeIndex + 1]).toBe("window");
});
it("should handle timestamp-like patterns in titles", () => {
const input = {
app_target: "Log Viewer:WINDOW_TITLE:2023-01-01 12:30:45",
format: "png" as const
};
const args = buildSwiftCliArgs(input, "/tmp/test.png");
expect(args).toContain("--window-title");
const titleIndex = args.indexOf("--window-title");
expect(args[titleIndex + 1]).toBe("2023-01-01 12:30:45");
});
});