Skip to content

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

fix(cli): capture infers image format from the -o extension when --format is unset, matching screenshot (PIN-411)

Luigi Agosti5546a97de15a
diff --git a/cmd/pinchtab/cmd_cli_register.go b/cmd/pinchtab/cmd_cli_register.go
index ce3d25d4..8167d5d4 100644
--- a/cmd/pinchtab/cmd_cli_register.go
+++ b/cmd/pinchtab/cmd_cli_register.go
@@ -136,7 +136,7 @@ func configureBrowserFlags() {
captureCmd.Flags().StringP("output", "o", "", "Save the captured image to this local file path (default: capture-<ts>.jpg)")
captureCmd.Flags().StringP("selector", "s", "", "Scope: clips screenshot and filters snapshot subtree to the same element")
captureCmd.Flags().String("filter", "", "Snapshot filter: 'interactive' or 'all' (default: interactive)")
- captureCmd.Flags().String("format", "", "Image format: 'jpeg' (default) or 'png'")
+ captureCmd.Flags().String("format", "", "Image format: 'jpeg' or 'png' (default: inferred from -o .png, otherwise jpeg)")
captureCmd.Flags().StringP("quality", "q", "", "JPEG quality (0-100)")
captureCmd.Flags().String("depth", "", "Snapshot max depth (-1 for full)")
captureCmd.Flags().String("wait", "", "Lifecycle wait: stable (default) | load | none")
diff --git a/internal/cli/actions/actions_capture.go b/internal/cli/actions/actions_capture.go
index 9550b537..d1711e00 100644
--- a/internal/cli/actions/actions_capture.go
+++ b/internal/cli/actions/actions_capture.go
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
+ "path/filepath"
"strings"
"time"
@@ -27,7 +28,14 @@ func Capture(client *http.Client, base, token string, cmd *cobra.Command) {
format, _ := cmd.Flags().GetString("format")
if format == "" {
- format = "jpeg"
+ // Infer from the -o extension when --format is unset, matching screenshot,
+ // so `capture -o x.png` writes a real PNG rather than JPEG-in-.png. An
+ // explicit --format stays authoritative because it makes format non-empty.
+ if strings.EqualFold(filepath.Ext(outFile), ".png") {
+ format = "png"
+ } else {
+ format = "jpeg"
+ }
}
if format != "jpeg" {
params.Set("format", format)
diff --git a/internal/cli/actions/actions_capture_test.go b/internal/cli/actions/actions_capture_test.go
index 2b2ea842..dae7a39e 100644
--- a/internal/cli/actions/actions_capture_test.go
+++ b/internal/cli/actions/actions_capture_test.go
@@ -1,6 +1,13 @@
package actions
import (
+ "bytes"
+ "encoding/base64"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
"strings"
"testing"
@@ -30,3 +37,88 @@ func TestCaptureThreadsExplicitTabIntoPairedRequest(t *testing.T) {
}
}
}
+
+// capture must infer the image format from the -o extension when --format is
+// unset, matching screenshot, so `capture -o x.png` writes real PNG bytes rather
+// than JPEG-in-.png. The server here reflects the requested format into the image
+// it returns, so the written file's magic bytes prove the whole chain: the CLI
+// inferred the format, sent it, and wrote what came back.
+func TestCaptureInfersFormatFromOutputExtension(t *testing.T) {
+ pngMagic := []byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}
+ jpegMagic := []byte{0xff, 0xd8, 0xff, 0xe0}
+
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ magic, format := jpegMagic, "jpeg"
+ if r.URL.Query().Get("format") == "png" {
+ magic, format = pngMagic, "png"
+ }
+ resp := map[string]any{
+ "status": "ok",
+ "image": map[string]any{"format": format, "base64": base64.StdEncoding.EncodeToString(magic)},
+ "pairing": map[string]any{"navigated": false},
+ }
+ _ = json.NewEncoder(w).Encode(resp)
+ }))
+ defer srv.Close()
+
+ newCaptureCmd := func() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().String("output", "", "")
+ cmd.Flags().String("format", "", "")
+ cmd.Flags().Bool("json", false, "")
+ cmd.Flags().String("tab", "", "")
+ return cmd
+ }
+
+ for _, tc := range []struct {
+ name string
+ outName string
+ setFormat string
+ wantMagic []byte
+ }{
+ {"png extension infers png", "x.png", "", pngMagic},
+ {"jpg extension defaults jpeg", "x.jpg", "", jpegMagic},
+ {"uppercase .PNG infers png", "x.PNG", "", pngMagic},
+ {"explicit format overrides extension", "x.png", "jpeg", jpegMagic},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ out := filepath.Join(t.TempDir(), tc.outName)
+ cmd := newCaptureCmd()
+ _ = cmd.Flags().Set("output", out)
+ if tc.setFormat != "" {
+ _ = cmd.Flags().Set("format", tc.setFormat)
+ }
+ _ = captureStdout(t, func() {
+ Capture(srv.Client(), srv.URL, "", cmd)
+ })
+ got, err := os.ReadFile(out)
+ if err != nil {
+ t.Fatalf("read output: %v", err)
+ }
+ if !bytes.HasPrefix(got, tc.wantMagic) {
+ t.Fatalf("written bytes %x do not carry the magic %x for the format the -o extension implies", got, tc.wantMagic)
+ }
+ })
+ }
+
+ // No -o at all still defaults to JPEG (auto-named in the working dir).
+ t.Run("no output defaults to jpeg", func(t *testing.T) {
+ dir := t.TempDir()
+ t.Chdir(dir)
+ cmd := newCaptureCmd()
+ _ = captureStdout(t, func() {
+ Capture(srv.Client(), srv.URL, "", cmd)
+ })
+ matches, _ := filepath.Glob(filepath.Join(dir, "capture-*.jpg"))
+ if len(matches) != 1 {
+ t.Fatalf("want exactly one auto-named .jpg, got %v", matches)
+ }
+ got, err := os.ReadFile(matches[0])
+ if err != nil {
+ t.Fatalf("read auto-named file: %v", err)
+ }
+ if !bytes.HasPrefix(got, jpegMagic) {
+ t.Fatalf("auto-named default is not JPEG: %x", got)
+ }
+ })
+}