agentchrome 1.51.1

A CLI tool for browser automation via the Chrome DevTools Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use std::time::Duration;

use serde::Serialize;

use agentchrome::cdp::{CdpClient, KeepAliveConfig};
use agentchrome::connection::{ManagedSession, ReconnectPolicy, resolve_target};
use agentchrome::error::{AppError, ExitCode};

use crate::cli::{GlobalOpts, OutputFormat};
use crate::emulate::apply_emulate_state;
use crate::snapshot;

// =============================================================================
// Constants
// =============================================================================

/// Default large-response threshold in bytes (16 KB).
pub const DEFAULT_THRESHOLD: usize = 16_384;

// =============================================================================
// Temp file output struct
// =============================================================================

#[derive(Serialize)]
pub struct TempFileOutput {
    pub output_file: String,
    pub size_bytes: u64,
    pub command: String,
    pub summary: serde_json::Value,
}

// =============================================================================
// Helpers
// =============================================================================

/// Write content to a UUID-named temp file and return the file path.
pub fn write_temp_file(content: &str, extension: &str) -> Result<String, AppError> {
    let id = uuid::Uuid::new_v4();
    let filename = format!("agentchrome-{id}.{extension}");
    let path = std::env::temp_dir().join(filename);
    std::fs::write(&path, content).map_err(|e| AppError {
        message: format!("failed to write temp file {}: {e}", path.display()),
        code: ExitCode::GeneralError,
        custom_json: None,
    })?;
    Ok(path.to_string_lossy().into_owned())
}

#[allow(clippy::needless_pass_by_value)]
fn serialization_error(e: serde_json::Error) -> AppError {
    AppError {
        message: format!("serialization error: {e}"),
        code: ExitCode::GeneralError,
        custom_json: None,
    }
}

/// Format a byte count as a human-readable string.
#[cfg(test)]
#[allow(clippy::cast_precision_loss)]
pub fn format_human_size(bytes: u64) -> String {
    if bytes >= 1_048_576 {
        format!("{:.1} MB", bytes as f64 / 1_048_576.0)
    } else if bytes >= 1024 {
        format!("{} KB", bytes / 1024)
    } else {
        format!("{bytes} bytes")
    }
}

// =============================================================================
// Shared output helpers
// =============================================================================

/// Serialize a value to JSON and print to stdout.
///
/// Uses compact or pretty formatting based on `OutputFormat` flags.
pub fn print_output(value: &impl Serialize, output: &OutputFormat) -> Result<(), AppError> {
    let json = if output.pretty {
        serde_json::to_string_pretty(value)
    } else {
        serde_json::to_string(value)
    };
    let json = json.map_err(serialization_error)?;
    println!("{json}");
    Ok(())
}

// =============================================================================
// CDP config helpers
// =============================================================================

/// Resolve the effective keep-alive config from `GlobalOpts`.
///
/// Precedence: `--no-keepalive` > `--keepalive-interval 0` > `--keepalive-interval N`
/// > `AGENTCHROME_KEEPALIVE_INTERVAL` (handled by clap `env`) > built-in default.
pub fn build_keepalive(global: &GlobalOpts) -> KeepAliveConfig {
    if global.no_keepalive {
        return KeepAliveConfig {
            interval: None,
            ..KeepAliveConfig::default()
        };
    }
    match global.keepalive_interval {
        Some(0) => KeepAliveConfig {
            interval: None,
            ..KeepAliveConfig::default()
        },
        Some(ms) => KeepAliveConfig {
            interval: Some(Duration::from_millis(ms)),
            ..KeepAliveConfig::default()
        },
        None => KeepAliveConfig::default(),
    }
}

/// Open a CDP connection for the current command, applying invocation-level
/// auto-reconnect and keep-alive defaults derived from `global`.
///
/// This collapses the `policy + keepalive + connect_for_command` boilerplate
/// that every command would otherwise repeat.
///
/// # Errors
///
/// Propagates `AppError` from the resolve/connect pipeline.
pub async fn connect_from_global(
    global: &GlobalOpts,
) -> Result<agentchrome::connection::CommandConnection, AppError> {
    connect_from_global_with_timeout(global, global.timeout).await
}

/// Like [`connect_from_global`], but lets a subcommand override the command
/// timeout (e.g. `js exec --timeout`).
///
/// # Errors
///
/// Propagates `AppError` from the resolve/connect pipeline.
pub async fn connect_from_global_with_timeout(
    global: &GlobalOpts,
    timeout_ms: Option<u64>,
) -> Result<agentchrome::connection::CommandConnection, AppError> {
    let policy = ReconnectPolicy::default();
    let keepalive = build_keepalive(global);
    agentchrome::connection::connect_for_command(
        &global.host,
        global.port,
        global.ws_url.as_deref(),
        timeout_ms,
        keepalive,
        &policy,
    )
    .await
}

// =============================================================================
// Session setup
// =============================================================================

/// Connect to Chrome, attach to a target, and apply emulation state.
pub async fn setup_session(global: &GlobalOpts) -> Result<(CdpClient, ManagedSession), AppError> {
    let conn = connect_from_global(global).await?;
    let target = resolve_target(
        &conn.resolved.host,
        conn.resolved.port,
        global.tab.as_deref(),
        global.page_id.as_deref(),
    )
    .await?;

    let session = conn.client.create_session(&target.id).await?;
    let mut managed = ManagedSession::new(session);
    apply_emulate_state(&mut managed).await?;

    Ok((conn.client, managed))
}

/// Like `setup_session`, but without applying emulation state.
///
/// Used by emulate commands that set (rather than apply) emulation state.
pub async fn setup_session_bare(
    global: &GlobalOpts,
) -> Result<(CdpClient, ManagedSession), AppError> {
    let conn = connect_from_global(global).await?;
    let target = resolve_target(
        &conn.resolved.host,
        conn.resolved.port,
        global.tab.as_deref(),
        global.page_id.as_deref(),
    )
    .await?;

    let session = conn.client.create_session(&target.id).await?;
    let managed = ManagedSession::new(session);

    Ok((conn.client, managed))
}

/// Like `setup_session`, but also installs dialog interceptors.
pub async fn setup_session_with_interceptors(
    global: &GlobalOpts,
) -> Result<(CdpClient, ManagedSession), AppError> {
    let (client, managed) = setup_session(global).await?;
    managed.install_dialog_interceptors().await;
    Ok((client, managed))
}

// =============================================================================
// Frame resolution
// =============================================================================

/// Resolve the optional `--frame` argument and return a `FrameContext`.
///
/// `uid` is the target element identifier; it is required when `frame` is
/// `"auto"` so that `resolve_frame_auto` can locate the correct frame.
pub async fn resolve_optional_frame(
    client: &CdpClient,
    managed: &mut ManagedSession,
    frame: Option<&str>,
    uid: Option<&str>,
) -> Result<Option<agentchrome::frame::FrameContext>, AppError> {
    // Aggregate-snapshot UID auto-routing (T029). When a UID-based command is
    // invoked against a snapshot produced by `page snapshot --include-iframes`,
    // UIDs can point into any frame. Consult the aggregate UID ranges to pick
    // the right frame automatically, and verify the recorded frame_id is still
    // attached to the live frame tree.
    if let Some(uid_str) = uid {
        if snapshot::is_uid(uid_str) {
            if let Some(state) = snapshot::read_snapshot_state().ok().flatten() {
                if let Some(uid_frame_idx) = snapshot::aggregate_frame_for_uid(&state, uid_str) {
                    let recorded_frame_id =
                        snapshot::aggregate_frame_id(&state, uid_frame_idx).map(str::to_string);

                    // Explicit --frame that names a specific index: warn on mismatch.
                    if let Some(frame_str) = frame {
                        if let Ok(agentchrome::frame::FrameArg::Index(n)) =
                            agentchrome::frame::parse_frame_arg(frame_str)
                        {
                            if n != uid_frame_idx {
                                eprintln!(
                                    "warning: UID {uid_str} was recorded in frame {uid_frame_idx} but --frame {n} was supplied; proceeding with explicit frame"
                                );
                            }
                        }
                        // Fall through to the standard resolution path below.
                    } else {
                        // No --frame given: route to the UID's originating frame.
                        verify_frame_still_attached(
                            managed,
                            uid_frame_idx,
                            recorded_frame_id.as_deref(),
                        )
                        .await?;
                        if uid_frame_idx == 0 {
                            return Ok(None);
                        }
                        let arg = agentchrome::frame::FrameArg::Index(uid_frame_idx);
                        let ctx = agentchrome::frame::resolve_frame(client, managed, &arg).await?;
                        return Ok(Some(ctx));
                    }
                }
            }
        }
    }

    if let Some(frame_str) = frame {
        let arg = agentchrome::frame::parse_frame_arg(frame_str)?;
        if matches!(arg, agentchrome::frame::FrameArg::Auto) {
            let target_uid = uid.unwrap_or_default();
            let state = snapshot::read_snapshot_state().ok().flatten();
            let hint = state
                .as_ref()
                .and_then(|s| s.frame_index.map(|idx| (idx, &s.uid_map)));
            let (ctx, _frame_idx) =
                agentchrome::frame::resolve_frame_auto(client, managed, target_uid, hint).await?;
            Ok(Some(ctx))
        } else {
            let ctx = agentchrome::frame::resolve_frame(client, managed, &arg).await?;
            Ok(Some(ctx))
        }
    } else {
        Ok(None)
    }
}

/// Confirm that a frame previously recorded in an aggregate snapshot is still
/// present at the same index with the same CDP frame id.
async fn verify_frame_still_attached(
    managed: &mut ManagedSession,
    frame_index: u32,
    recorded_frame_id: Option<&str>,
) -> Result<(), AppError> {
    let Some(expected_id) = recorded_frame_id else {
        return Ok(());
    };
    let frames = agentchrome::frame::list_frames(managed).await?;
    let Some(current) = frames.iter().find(|f| f.index == frame_index) else {
        return Err(AppError::frame_detached());
    };
    if current.id != expected_id {
        return Err(AppError::frame_detached());
    }
    Ok(())
}

// =============================================================================
// Emit functions
// =============================================================================

/// Emit plain text through the large-response gate.
///
/// If the text exceeds the threshold, it is written to a temp file and
/// the file path is printed on stdout instead.
pub fn emit_plain(text: &str, output: &OutputFormat) -> Result<(), AppError> {
    let threshold = output.large_response_threshold.unwrap_or(DEFAULT_THRESHOLD);

    if text.len() <= threshold {
        print!("{text}");
        return Ok(());
    }

    let path = write_temp_file(text, "txt")?;
    println!("{path}");
    Ok(())
}

/// Emit a serializable value through the large-response gate.
///
/// If the serialized JSON exceeds the threshold, the full output is written
/// to a UUID-named temp file and a `TempFileOutput` object is printed instead.
pub fn emit<T, F>(
    value: &T,
    output: &OutputFormat,
    command_name: &str,
    summary_fn: F,
) -> Result<(), AppError>
where
    T: Serialize,
    F: FnOnce(&T) -> serde_json::Value,
{
    // 1. Serialize to JSON string (once)
    let json_string = if output.pretty {
        serde_json::to_string_pretty(value)
    } else {
        serde_json::to_string(value)
    }
    .map_err(serialization_error)?;

    // 2. Determine effective threshold
    let threshold = output.large_response_threshold.unwrap_or(DEFAULT_THRESHOLD);

    // 3. If under threshold, print and return
    if json_string.len() <= threshold {
        println!("{json_string}");
        return Ok(());
    }

    // 4. Write to temp file
    let path = write_temp_file(&json_string, "json")?;

    // 5. Build and print TempFileOutput
    let summary = summary_fn(value);
    #[allow(clippy::cast_possible_truncation)]
    let size_bytes = json_string.len() as u64;

    let temp_output = TempFileOutput {
        output_file: path,
        size_bytes,
        command: command_name.to_string(),
        summary,
    };

    let output_json = serde_json::to_string(&temp_output).map_err(serialization_error)?;
    println!("{output_json}");
    Ok(())
}

/// Write already-serialized JSON larger than the threshold to a temp file and
/// print a `TempFileOutput` with an empty summary. Used by
/// `emit_with_snapshot` when the compound shape invariants (object outer,
/// snapshot field present) do not hold — preserves the bounded-output promise.
fn emit_oversized_fallback(full_json: &str, command_name: &str) -> Result<(), AppError> {
    let path = write_temp_file(full_json, "json")?;
    #[allow(clippy::cast_possible_truncation)]
    let size_bytes = full_json.len() as u64;
    let temp_output = TempFileOutput {
        output_file: path,
        size_bytes,
        command: command_name.to_string(),
        summary: serde_json::json!({}),
    };
    let output_json = serde_json::to_string(&temp_output).map_err(serialization_error)?;
    println!("{output_json}");
    Ok(())
}

/// Emit a compound result that carries a small interaction-confirmation payload
/// plus a potentially large `snapshot` field.
///
/// If the serialized form of the whole value fits under the threshold, the full
/// inline JSON is printed (identical to `emit`).
///
/// Otherwise, the `snapshot_field` key is extracted from the serialized value,
/// written to a UUID-named temp file, and replaced with a `TempFileOutput`
/// whose `summary` is produced by `snapshot_summary_fn`. The
/// interaction-confirmation fields (everything other than `snapshot_field`)
/// remain inline.
///
/// If the `snapshot_field` key is absent or the outer value is not a JSON
/// object, the whole payload is offloaded to a temp file and a `TempFileOutput`
/// with an empty summary is printed — stdout stays bounded either way.
///
/// # Errors
///
/// Propagates `AppError` from serialization and temp-file writes.
pub fn emit_with_snapshot<T, F>(
    value: &T,
    output: &OutputFormat,
    command_name: &str,
    snapshot_field: &'static str,
    snapshot_summary_fn: F,
) -> Result<(), AppError>
where
    T: Serialize,
    F: FnOnce(&serde_json::Value) -> serde_json::Value,
{
    // 1. Serialize `value` to a serde_json::Value (no string yet).
    let mut outer: serde_json::Value = serde_json::to_value(value).map_err(serialization_error)?;

    // 2. Compute total size by serializing to a string.
    let full_json = if output.pretty {
        serde_json::to_string_pretty(&outer)
    } else {
        serde_json::to_string(&outer)
    }
    .map_err(serialization_error)?;

    let threshold = output.large_response_threshold.unwrap_or(DEFAULT_THRESHOLD);

    // 3. Under threshold → print inline (identical to emit's small-path).
    if full_json.len() <= threshold {
        println!("{full_json}");
        return Ok(());
    }

    // 4. Extract the snapshot field. If the outer value is not an object, or the
    //    snapshot field is absent, offload the whole payload to a temp file so
    //    stdout stays bounded. Summary is empty because the caller's
    //    `snapshot_summary_fn` targets the snapshot field specifically.
    let Some(map) = outer.as_object_mut() else {
        return emit_oversized_fallback(&full_json, command_name);
    };

    let Some(snapshot_value) = map.remove(snapshot_field) else {
        return emit_oversized_fallback(&full_json, command_name);
    };

    // 5. Serialize the snapshot alone and write it to a temp file.
    let snapshot_json = serde_json::to_string(&snapshot_value).map_err(serialization_error)?;
    let path = write_temp_file(&snapshot_json, "json")?;

    // 6. Build a TempFileOutput for the snapshot.
    let summary = snapshot_summary_fn(&snapshot_value);
    #[allow(clippy::cast_possible_truncation)]
    let size_bytes = snapshot_json.len() as u64;

    let temp_output = TempFileOutput {
        output_file: path,
        size_bytes,
        command: command_name.to_string(),
        summary,
    };

    // 7. Replace the snapshot field in the outer object with the TempFileOutput.
    let temp_value = serde_json::to_value(&temp_output).map_err(serialization_error)?;
    map.insert(snapshot_field.to_string(), temp_value);

    // 8. Print the modified outer object.
    let result_json = if output.pretty {
        serde_json::to_string_pretty(&outer)
    } else {
        serde_json::to_string(&outer)
    }
    .map_err(serialization_error)?;
    println!("{result_json}");
    Ok(())
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_human_size_bytes() {
        assert_eq!(format_human_size(500), "500 bytes");
        assert_eq!(format_human_size(0), "0 bytes");
        assert_eq!(format_human_size(1023), "1023 bytes");
    }

    #[test]
    fn format_human_size_kb() {
        assert_eq!(format_human_size(1024), "1 KB");
        assert_eq!(format_human_size(16_384), "16 KB");
        assert_eq!(format_human_size(1_048_575), "1023 KB");
    }

    #[test]
    fn format_human_size_mb() {
        assert_eq!(format_human_size(1_048_576), "1.0 MB");
        assert_eq!(format_human_size(5_242_880), "5.0 MB");
    }

    #[test]
    fn temp_file_output_serialization() {
        let output = TempFileOutput {
            output_file: "/tmp/agentchrome-abc.json".to_string(),
            size_bytes: 32_768,
            command: "page snapshot".to_string(),
            summary: serde_json::json!({"total_nodes": 5000}),
        };
        let json: serde_json::Value = serde_json::to_value(&output).unwrap();
        assert_eq!(json["output_file"], "/tmp/agentchrome-abc.json");
        assert_eq!(json["size_bytes"], 32_768);
        assert_eq!(json["command"], "page snapshot");
        assert_eq!(json["summary"]["total_nodes"], 5000);
    }

    #[test]
    fn temp_file_output_has_exactly_four_keys() {
        let output = TempFileOutput {
            output_file: "/tmp/test.json".to_string(),
            size_bytes: 100,
            command: "test".to_string(),
            summary: serde_json::json!({}),
        };
        let json: serde_json::Value = serde_json::to_value(&output).unwrap();
        let keys = json.as_object().unwrap();
        assert_eq!(keys.len(), 4);
        assert!(keys.contains_key("output_file"));
        assert!(keys.contains_key("size_bytes"));
        assert!(keys.contains_key("command"));
        assert!(keys.contains_key("summary"));
    }

    #[test]
    fn write_temp_file_creates_readable_file() {
        let content = "hello temp file";
        let path = write_temp_file(content, "txt").unwrap();
        let read_back = std::fs::read_to_string(&path).unwrap();
        assert_eq!(read_back, content);
        assert!(path.contains("agentchrome-"));
        assert!(
            std::path::Path::new(&path)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("txt"))
        );
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn write_temp_file_json_extension() {
        let content = r#"{"key":"value"}"#;
        let path = write_temp_file(content, "json").unwrap();
        assert!(
            std::path::Path::new(&path)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
        );
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn write_temp_file_uuid_uniqueness() {
        let path1 = write_temp_file("a", "txt").unwrap();
        let path2 = write_temp_file("b", "txt").unwrap();
        assert_ne!(path1, path2);
        let _ = std::fs::remove_file(&path1);
        let _ = std::fs::remove_file(&path2);
    }

    #[test]
    fn emit_below_threshold_prints_json() {
        let value = serde_json::json!({"key": "value"});
        let output = OutputFormat {
            json: true,
            pretty: false,
            plain: false,
            large_response_threshold: Some(1_000_000),
        };
        let result = emit(&value, &output, "test", |_| serde_json::json!({}));
        assert!(result.is_ok());
    }

    #[test]
    fn emit_above_threshold_creates_temp_file() {
        // Create a large value that exceeds the threshold
        let large_string: String = "x".repeat(1000);
        let value = serde_json::json!({"data": large_string});
        let output = OutputFormat {
            json: true,
            pretty: false,
            plain: false,
            large_response_threshold: Some(10), // Very low threshold
        };
        let result = emit(
            &value,
            &output,
            "test cmd",
            |_| serde_json::json!({"test": true}),
        );
        assert!(result.is_ok());
        // The function prints to stdout; we verify no error occurred.
        // Full integration behavior is tested via BDD.
    }

    #[test]
    fn emit_plain_below_threshold() {
        let output = OutputFormat {
            json: false,
            pretty: false,
            plain: true,
            large_response_threshold: Some(1_000_000),
        };
        let result = emit_plain("short text", &output);
        assert!(result.is_ok());
    }

    #[test]
    fn emit_plain_above_threshold() {
        let output = OutputFormat {
            json: false,
            pretty: false,
            plain: true,
            large_response_threshold: Some(5), // Very low
        };
        let result = emit_plain("this text is longer than 5 bytes", &output);
        assert!(result.is_ok());
    }

    // -------------------------------------------------------------------------
    // emit_with_snapshot tests
    // -------------------------------------------------------------------------

    /// Helper: `OutputFormat` with a very large threshold so nothing is offloaded.
    fn unlimited_output() -> OutputFormat {
        OutputFormat {
            json: true,
            pretty: false,
            plain: false,
            large_response_threshold: Some(10_000_000),
        }
    }

    /// Helper: `OutputFormat` with threshold = 1 so everything is offloaded.
    fn tiny_threshold_output() -> OutputFormat {
        OutputFormat {
            json: true,
            pretty: false,
            plain: false,
            large_response_threshold: Some(1),
        }
    }

    #[test]
    fn emit_with_snapshot_below_threshold_is_inline() {
        let value = serde_json::json!({
            "success": true,
            "uid": "s1",
            "snapshot": {"total_nodes": 10, "nodes": []}
        });
        let result = emit_with_snapshot(
            &value,
            &unlimited_output(),
            "interact click",
            "snapshot",
            |_| serde_json::json!({"total_nodes": 10}),
        );
        // Should succeed (prints inline JSON to stdout — no temp file).
        assert!(result.is_ok());
    }

    #[test]
    fn emit_with_snapshot_above_threshold_offloads_snapshot_only() {
        let large_snapshot: Vec<serde_json::Value> = (0..500)
            .map(|i| serde_json::json!({"id": i, "role": "generic", "name": format!("node-{i}")}))
            .collect();
        let value = serde_json::json!({
            "success": true,
            "uid": "s12",
            "navigation": {"url": "https://example.com", "committed": true},
            "snapshot": {"total_nodes": 500, "nodes": large_snapshot}
        });
        let result = emit_with_snapshot(
            &value,
            &tiny_threshold_output(),
            "interact click",
            "snapshot",
            |v| {
                let count = v["total_nodes"].as_u64().unwrap_or(0);
                serde_json::json!({"total_nodes": count, "top_roles": ["generic"]})
            },
        );
        assert!(result.is_ok());
    }

    #[test]
    fn emit_with_snapshot_missing_field_falls_back_to_emit() {
        // Value has no "snapshot" key — should fall back without panicking.
        let value = serde_json::json!({"success": true, "uid": "s5"});
        let result = emit_with_snapshot(
            &value,
            &tiny_threshold_output(),
            "interact click",
            "snapshot",
            |_| serde_json::json!({}),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn emit_with_snapshot_non_object_falls_back() {
        // A plain array — not an object.
        let value = serde_json::json!([1, 2, 3]);
        let result =
            emit_with_snapshot(&value, &tiny_threshold_output(), "test", "snapshot", |_| {
                serde_json::json!({})
            });
        assert!(result.is_ok());
    }

    #[test]
    fn emit_oversized_fallback_writes_temp_file_and_returns_temp_file_output() {
        // Build a JSON string that we want to offload wholesale.
        let full_json = "x".repeat(100);
        let result = emit_oversized_fallback(&full_json, "test cmd");
        assert!(result.is_ok());
        // We cannot easily capture stdout in unit tests, but we verify no panic.
        // Integration coverage comes from BDD large-response-detection.feature.
    }
}