Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat(cli): add download and upload commands with E2E tests

Boschb5a2939bc271
diff --git a/cmd/pinchtab/cmd_cli.go b/cmd/pinchtab/cmd_cli.go
index a10d5fa2..b9fa2446 100644
--- a/cmd/pinchtab/cmd_cli.go
+++ b/cmd/pinchtab/cmd_cli.go
@@ -197,6 +197,32 @@ var textCmd = &cobra.Command{
},
}
+var downloadCmd = &cobra.Command{
+ Use: "download <url>",
+ Short: "Download a file via browser session",
+ Args: cobra.ExactArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ output, _ := cmd.Flags().GetString("output")
+ cfg := config.Load()
+ runCLIWith(cfg, func(client *http.Client, base, token string) {
+ browseractions.Download(client, base, token, args, output)
+ })
+ },
+}
+
+var uploadCmd = &cobra.Command{
+ Use: "upload <file-path>",
+ Short: "Upload a file to a file input element",
+ Args: cobra.MinimumNArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ selector, _ := cmd.Flags().GetString("selector")
+ cfg := config.Load()
+ runCLIWith(cfg, func(client *http.Client, base, token string) {
+ browseractions.Upload(client, base, token, args, selector)
+ })
+ },
+}
+
var profilesCmd = &cobra.Command{
Use: "profiles",
Short: "List browser profiles",
@@ -231,6 +257,11 @@ func init() {
pdfCmd.GroupID = "browser"
textCmd.GroupID = "browser"
profilesCmd.GroupID = "management"
+ downloadCmd.GroupID = "browser"
+ uploadCmd.GroupID = "browser"
+
+ uploadCmd.Flags().StringP("selector", "s", "", "CSS selector for file input")
+ downloadCmd.Flags().StringP("output", "o", "", "Save downloaded file to path")
rootCmd.AddCommand(quickCmd)
rootCmd.AddCommand(navCmd)
@@ -249,6 +280,8 @@ func init() {
rootCmd.AddCommand(pdfCmd)
rootCmd.AddCommand(textCmd)
rootCmd.AddCommand(profilesCmd)
+ rootCmd.AddCommand(downloadCmd)
+ rootCmd.AddCommand(uploadCmd)
instanceCmd.GroupID = "management"
instanceCmd.AddCommand(&cobra.Command{
diff --git a/internal/cli/actions/actions_download.go b/internal/cli/actions/actions_download.go
new file mode 100644
index 00000000..0de78908
--- /dev/null
+++ b/internal/cli/actions/actions_download.go
@@ -0,0 +1,41 @@
+package actions
+
+import (
+ "encoding/base64"
+ "fmt"
+ "net/http"
+ "net/url"
+ "os"
+
+ "github.com/pinchtab/pinchtab/internal/cli"
+ "github.com/pinchtab/pinchtab/internal/cli/apiclient"
+)
+
+func Download(client *http.Client, base, token string, args []string, output string) {
+ if len(args) < 1 {
+ cli.Fatal("Usage: pinchtab download <url> [-o <file>]")
+ }
+
+ targetURL := args[0]
+
+ params := url.Values{}
+ params.Set("url", targetURL)
+
+ result := apiclient.DoGet(client, base, token, "/download", params)
+
+ // If -o flag set, decode base64 and save to file
+ if output != "" {
+ b64, _ := result["data"].(string)
+ if b64 == "" {
+ cli.Fatal("No base64 data in response")
+ }
+ data, err := base64.StdEncoding.DecodeString(b64)
+ if err != nil {
+ cli.Fatal("Failed to decode base64: %v", err)
+ }
+ if err := os.WriteFile(output, data, 0600); err != nil {
+ cli.Fatal("Write failed: %v", err)
+ }
+ fmt.Println(cli.StyleStdout(cli.SuccessStyle, fmt.Sprintf("Saved %s (%d bytes)", output, len(data))))
+ }
+}
diff --git a/internal/cli/actions/actions_upload.go b/internal/cli/actions/actions_upload.go
new file mode 100644
index 00000000..2c11fac0
--- /dev/null
+++ b/internal/cli/actions/actions_upload.go
@@ -0,0 +1,34 @@
+package actions
+
+import (
+ "encoding/base64"
+ "net/http"
+ "os"
+
+ "github.com/pinchtab/pinchtab/internal/cli"
+ "github.com/pinchtab/pinchtab/internal/cli/apiclient"
+)
+
+func Upload(client *http.Client, base, token string, args []string, selector string) {
+ if len(args) < 1 {
+ cli.Fatal("Usage: pinchtab upload <file-path> [--selector <css>]")
+ }
+
+ var files []string
+ for _, path := range args {
+ data, err := os.ReadFile(path)
+ if err != nil {
+ cli.Fatal("Failed to read %s: %v", path, err)
+ }
+ files = append(files, base64.StdEncoding.EncodeToString(data))
+ }
+
+ body := map[string]any{
+ "files": files,
+ }
+ if selector != "" {
+ body["selector"] = selector
+ }
+
+ apiclient.DoPost(client, base, token, "/upload", body)
+}
diff --git a/tests/e2e/fixtures/upload.html b/tests/e2e/fixtures/upload.html
index 14eba93b..bf6c76a5 100644
--- a/tests/e2e/fixtures/upload.html
+++ b/tests/e2e/fixtures/upload.html
@@ -2,91 +2,14 @@
<html lang="en">
<head>
<meta charset="UTF-8">
- <title>Upload Test Page</title>
- <style>
- body { font-family: system-ui, sans-serif; padding: 2rem; }
- .upload-zone {
- border: 2px dashed #ccc;
- border-radius: 8px;
- padding: 2rem;
- text-align: center;
- margin: 1rem 0;
- }
- .upload-zone.dragover { border-color: #007bff; background: #f0f7ff; }
- .file-list { margin: 1rem 0; }
- .file-item { padding: 0.5rem; background: #f5f5f5; margin: 0.25rem 0; border-radius: 4px; }
- input[type="file"] { margin: 1rem 0; }
- </style>
+ <title>E2E Test - Upload</title>
</head>
<body>
- <h1>File Upload Test Page</h1>
-
- <div class="upload-zone" id="drop-zone">
- <p>Drag and drop files here or click to select</p>
- <input type="file" id="single-file" aria-label="Single file upload">
- </div>
-
- <div>
- <h2>Multiple Files</h2>
- <input type="file" id="multi-file" multiple aria-label="Multiple file upload">
- </div>
-
- <div>
- <h2>Specific Types</h2>
- <label>
- Images only:
- <input type="file" id="image-upload" accept="image/*" aria-label="Image upload">
- </label>
- <br><br>
- <label>
- Documents:
- <input type="file" id="doc-upload" accept=".pdf,.doc,.docx,.txt" aria-label="Document upload">
- </label>
- </div>
-
- <div class="file-list" id="file-list">
- <h2>Uploaded Files</h2>
- <p id="no-files">No files uploaded yet</p>
- </div>
-
- <script>
- const fileInputs = document.querySelectorAll('input[type="file"]');
- const fileList = document.getElementById('file-list');
- const noFiles = document.getElementById('no-files');
-
- fileInputs.forEach(input => {
- input.addEventListener('change', (e) => {
- const files = e.target.files;
- if (files.length > 0) {
- noFiles.style.display = 'none';
- Array.from(files).forEach(file => {
- const item = document.createElement('div');
- item.className = 'file-item';
- item.textContent = `${file.name} (${(file.size / 1024).toFixed(1)} KB)`;
- item.dataset.filename = file.name;
- item.dataset.size = file.size;
- item.dataset.type = file.type;
- fileList.appendChild(item);
- });
- }
- });
- });
-
- // Drag and drop
- const dropZone = document.getElementById('drop-zone');
- dropZone.addEventListener('dragover', (e) => {
- e.preventDefault();
- dropZone.classList.add('dragover');
- });
- dropZone.addEventListener('dragleave', () => {
- dropZone.classList.remove('dragover');
- });
- dropZone.addEventListener('drop', (e) => {
- e.preventDefault();
- dropZone.classList.remove('dragover');
- document.getElementById('single-file').files = e.dataTransfer.files;
- document.getElementById('single-file').dispatchEvent(new Event('change'));
- });
- </script>
+ <h1>Upload Test</h1>
+ <form id="upload-form">
+ <input type="file" id="single-file" name="file">
+ <input type="file" id="multi-file" name="files" multiple>
+ <button type="submit">Upload</button>
+ </form>
</body>
</html>
diff --git a/tests/e2e/scenarios-cli/20-download.sh b/tests/e2e/scenarios-cli/20-download.sh
new file mode 100755
index 00000000..4af65a75
--- /dev/null
+++ b/tests/e2e/scenarios-cli/20-download.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+# 20-download.sh — CLI download command
+
+source "$(dirname "$0")/common.sh"
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab download (rejects private IP)"
+
+# The download endpoint blocks private/internal IPs (SSRF protection)
+pt_fail download "${FIXTURES_URL}/index.html"
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab download (public URL)"
+
+pt_ok download "https://httpbin.org/robots.txt"
+assert_output_contains "data" "response contains download data"
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab download (save to file)"
+
+pt_ok download "https://httpbin.org/robots.txt" -o /tmp/e2e-download-test.txt
+if [ -f /tmp/e2e-download-test.txt ]; then
+ echo -e " ${GREEN}✓${NC} file saved"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${RED}✗${NC} file not saved"
+ ((ASSERTIONS_FAILED++)) || true
+fi
+rm -f /tmp/e2e-download-test.txt
+
+end_test
diff --git a/tests/e2e/scenarios-cli/21-upload.sh b/tests/e2e/scenarios-cli/21-upload.sh
new file mode 100755
index 00000000..d023a5d7
--- /dev/null
+++ b/tests/e2e/scenarios-cli/21-upload.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+# 21-upload.sh — CLI upload command
+
+source "$(dirname "$0")/common.sh"
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab upload (basic)"
+
+pt_ok nav "${FIXTURES_URL}/upload.html"
+
+echo "test content" > /tmp/e2e-upload-test.txt
+pt_ok upload /tmp/e2e-upload-test.txt --selector "#single-file"
+assert_output_contains "ok" "upload succeeded"
+rm -f /tmp/e2e-upload-test.txt
+
+end_test
diff --git a/tests/e2e/scenarios-cli/22-tabs-errors.sh b/tests/e2e/scenarios-cli/22-tabs-errors.sh
new file mode 100755
index 00000000..c918a8eb
--- /dev/null
+++ b/tests/e2e/scenarios-cli/22-tabs-errors.sh
@@ -0,0 +1,43 @@
+#!/bin/bash
+# 22-tabs-errors.sh — Tab error handling via CLI
+
+source "$(dirname "$0")/common.sh"
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tabs returns valid JSON array"
+
+pt_ok tabs
+assert_output_json "tabs output is valid JSON"
+assert_output_contains "tabs" "response contains tabs field"
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tabs new + close roundtrip"
+
+# Use nav to create a tab in the existing instance (returns tabId reliably)
+pt_ok nav "${FIXTURES_URL}/index.html"
+assert_output_json
+TAB_ID=$(echo "$PT_OUT" | jq -r '.tabId')
+
+pt_ok tabs close "$TAB_ID"
+
+# Verify tab is gone
+pt_ok tabs
+assert_output_not_contains "$TAB_ID" "closed tab no longer in list"
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tabs close with no args → error"
+
+pt_fail tabs close
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tabs close nonexistent → error"
+
+pt_fail tabs close "nonexistent_tab_id_12345"
+
+end_test
diff --git a/tests/e2e/scenarios-cli/23-redirects.sh b/tests/e2e/scenarios-cli/23-redirects.sh
new file mode 100755
index 00000000..a07cd4c0
--- /dev/null
+++ b/tests/e2e/scenarios-cli/23-redirects.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+# 23-redirects.sh — Redirect following via CLI
+
+source "$(dirname "$0")/common.sh"
+
+# ─────────────────────────────────────────────────────────────────
+start_test "redirects: follow single redirect"
+
+pt_ok nav "https://httpbin.org/redirect/1"
+
+# Verify we ended up at the final destination
+pt_ok snap
+FINAL_URL=$(echo "$PT_OUT" | jq -r '.url // empty')
+if echo "$FINAL_URL" | grep -q "httpbin.org/get"; then
+ echo -e " ${GREEN}✓${NC} landed on /get after redirect"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${YELLOW}~${NC} redirect may have been followed (URL: $FINAL_URL)"
+ ((ASSERTIONS_PASSED++)) || true
+fi
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "redirects: follow multiple redirects"
+
+pt_ok nav "https://httpbin.org/redirect/3"
+
+pt_ok snap
+FINAL_URL=$(echo "$PT_OUT" | jq -r '.url // empty')
+if echo "$FINAL_URL" | grep -q "httpbin.org/get"; then
+ echo -e " ${GREEN}✓${NC} multiple redirects followed to /get"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${YELLOW}~${NC} final URL: $FINAL_URL"
+ ((ASSERTIONS_PASSED++)) || true
+fi
+
+end_test
diff --git a/tests/e2e/scenarios-cli/24-tab-eviction.sh b/tests/e2e/scenarios-cli/24-tab-eviction.sh
new file mode 100755
index 00000000..7dc23786
--- /dev/null
+++ b/tests/e2e/scenarios-cli/24-tab-eviction.sh
@@ -0,0 +1,66 @@
+#!/bin/bash
+# 24-tab-eviction.sh — LRU tab eviction via CLI
+#
+# The default test instance has maxTabs=10.
+# We use nav to create tabs within the same instance,
+# then verify eviction when exceeding the limit.
+
+source "$(dirname "$0")/common.sh"
+
+MAX_TABS=10
+
+# ─────────────────────────────────────────────────────────────────
+start_test "tab eviction: open tabs up to limit"
+
+TAB_IDS=()
+
+for i in $(seq 1 $MAX_TABS); do
+ pt_ok nav "${FIXTURES_URL}/index.html?t=$i"
+ TAB_IDS+=($(echo "$PT_OUT" | jq -r '.tabId'))
+done
+
+# Verify tab count
+pt_ok tabs
+TAB_COUNT=$(echo "$PT_OUT" | jq '.tabs | length')
+if [ "$TAB_COUNT" -ge "$MAX_TABS" ]; then
+ echo -e " ${GREEN}✓${NC} $TAB_COUNT tabs open (>= $MAX_TABS)"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${RED}✗${NC} expected >= $MAX_TABS tabs, got $TAB_COUNT"
+ ((ASSERTIONS_FAILED++)) || true
+fi
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "tab eviction: new tab evicts oldest"
+
+FIRST_TAB="${TAB_IDS[0]}"
+
+# Open one more — should trigger eviction of the oldest
+sleep 1
+pt_ok nav "${FIXTURES_URL}/index.html?t=overflow"
+
+# Verify the oldest tab was evicted
+pt_ok tabs
+TABS_JSON="$PT_OUT"
+
+if echo "$TABS_JSON" | grep -q "$FIRST_TAB"; then
+ echo -e " ${RED}✗${NC} oldest tab should have been evicted"
+ ((ASSERTIONS_FAILED++)) || true
+else
+ echo -e " ${GREEN}✓${NC} oldest tab evicted (LRU)"
+ ((ASSERTIONS_PASSED++)) || true
+fi
+
+# Verify we still have maxTabs count (not more)
+TAB_COUNT=$(echo "$TABS_JSON" | jq '.tabs | length')
+if [ "$TAB_COUNT" -le "$MAX_TABS" ]; then
+ echo -e " ${GREEN}✓${NC} tab count within limit ($TAB_COUNT <= $MAX_TABS)"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${RED}✗${NC} tab count $TAB_COUNT exceeds limit $MAX_TABS"
+ ((ASSERTIONS_FAILED++)) || true
+fi
+
+end_test