native-devtools-mcp 0.10.1

MCP server for computer use & browser automation — screenshot, OCR, click, type, find_text, Chrome/Electron CDP, template matching. macOS, Windows & Android.
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
//! Tests for dynamic tool gating and identity validation
//!
//! These tests verify:
//! 1. Tool list changes based on connection state
//! 2. Server capability advertisement for tool list changes
//! 3. Identity validation logic for expected_bundle_id and expected_app_name
//!
//! ## Test Pattern Guidelines
//!
//! The `identity_validation` module demonstrates recommended test patterns:
//!
//! - **Granular test functions**: One behavior per test, named descriptively
//! - **Edge case coverage**: Empty strings, whitespace, case sensitivity, missing fields
//! - **MARK comments**: Group related tests (e.g., `// MARK: - validate_bundle_id tests`)
//! - **Integration tests**: `validate_identity` tests combine lower-level validators
//! - **Behavior documentation**: Test names describe what behavior is being verified
//!
//! When adding new tests, follow the patterns in `identity_validation` for consistency.

use native_devtools_mcp::server::MacOSDevToolsServer;
use rmcp::handler::server::ServerHandler;

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

    #[test]
    fn test_base_tools_always_present_when_disconnected() {
        // When disconnected, should have base tools + app_connect
        // app_* tools (except app_connect) should NOT be present

        // This tests the get_tools(false) case
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let tool_names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();

        // Base tools should be present
        assert!(tool_names.contains(&"take_screenshot".to_string()));
        assert!(tool_names.contains(&"list_windows".to_string()));
        assert!(tool_names.contains(&"click".to_string()));
        assert!(tool_names.contains(&"type_text".to_string()));
        assert!(tool_names.contains(&"element_at_point".to_string()));
        assert!(tool_names.contains(&"quit_app".to_string()));
        assert!(tool_names.contains(&"probe_app".to_string()));

        // app_connect should always be present
        assert!(tool_names.contains(&"app_connect".to_string()));

        // app_* tools should NOT be present when disconnected
        assert!(!tool_names.contains(&"app_disconnect".to_string()));
        assert!(!tool_names.contains(&"app_get_info".to_string()));
        assert!(!tool_names.contains(&"app_get_tree".to_string()));
        assert!(!tool_names.contains(&"app_click".to_string()));
    }

    #[test]
    fn test_app_tools_present_when_connected() {
        // When connected, should have base tools + app_connect + all app_* tools

        let connected_tools = MacOSDevToolsServer::get_tools(true, false, false, false, false);
        let tool_names: Vec<String> = connected_tools.iter().map(|t| t.name.to_string()).collect();

        // Base tools should still be present
        assert!(tool_names.contains(&"take_screenshot".to_string()));
        assert!(tool_names.contains(&"list_windows".to_string()));

        // app_connect should always be present
        assert!(tool_names.contains(&"app_connect".to_string()));

        // app_* tools should be present when connected
        assert!(tool_names.contains(&"app_disconnect".to_string()));
        assert!(tool_names.contains(&"app_get_info".to_string()));
        assert!(tool_names.contains(&"app_get_tree".to_string()));
        assert!(tool_names.contains(&"app_query".to_string()));
        assert!(tool_names.contains(&"app_get_element".to_string()));
        assert!(tool_names.contains(&"app_click".to_string()));
        assert!(tool_names.contains(&"app_type".to_string()));
        assert!(tool_names.contains(&"app_press_key".to_string()));
        assert!(tool_names.contains(&"app_focus".to_string()));
        assert!(tool_names.contains(&"app_screenshot".to_string()));
        assert!(tool_names.contains(&"app_list_windows".to_string()));
        assert!(tool_names.contains(&"app_focus_window".to_string()));

        // Connected state should have more tools than disconnected
        let disconnected_tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        assert!(
            connected_tools.len() > disconnected_tools.len(),
            "Connected state should expose more tools than disconnected state"
        );
    }
}

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

    #[test]
    fn test_android_base_tools_always_present() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();

        assert!(names.contains(&"android_list_devices".to_string()));
        assert!(names.contains(&"android_connect".to_string()));
    }

    #[test]
    fn test_android_tools_hidden_when_disconnected() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();

        assert!(!names.contains(&"android_disconnect".to_string()));
        assert!(!names.contains(&"android_screenshot".to_string()));
        assert!(!names.contains(&"android_click".to_string()));
        assert!(!names.contains(&"android_swipe".to_string()));
        assert!(!names.contains(&"android_type_text".to_string()));
        assert!(!names.contains(&"android_press_key".to_string()));
        assert!(!names.contains(&"android_find_text".to_string()));
        assert!(!names.contains(&"android_list_apps".to_string()));
        assert!(!names.contains(&"android_launch_app".to_string()));
        assert!(!names.contains(&"android_get_display_info".to_string()));
        assert!(!names.contains(&"android_get_current_activity".to_string()));
    }

    #[test]
    fn test_android_tools_visible_when_connected() {
        let tools = MacOSDevToolsServer::get_tools(false, true, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();

        // Base tools
        assert!(names.contains(&"android_list_devices".to_string()));
        assert!(names.contains(&"android_connect".to_string()));

        // Connected-only tools
        assert!(names.contains(&"android_disconnect".to_string()));
        assert!(names.contains(&"android_screenshot".to_string()));
        assert!(names.contains(&"android_click".to_string()));
        assert!(names.contains(&"android_swipe".to_string()));
        assert!(names.contains(&"android_type_text".to_string()));
        assert!(names.contains(&"android_press_key".to_string()));
        assert!(names.contains(&"android_find_text".to_string()));
        assert!(names.contains(&"android_list_apps".to_string()));
        assert!(names.contains(&"android_launch_app".to_string()));
        assert!(names.contains(&"android_get_display_info".to_string()));
        assert!(names.contains(&"android_get_current_activity".to_string()));
    }

    #[test]
    fn test_android_connection_adds_tools() {
        let disconnected = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let connected = MacOSDevToolsServer::get_tools(false, true, false, false, false);

        assert!(
            connected.len() > disconnected.len(),
            "Android connected state should expose more tools: {} vs {}",
            connected.len(),
            disconnected.len()
        );

        // Should add exactly 11 tools (disconnect + 10 functional tools)
        assert_eq!(connected.len() - disconnected.len(), 11);
    }
}

#[cfg(test)]
#[cfg(feature = "cdp")]
mod cdp_tool_gating {
    use super::*;

    /// The full set of CDP tools — used by visibility checks below.
    /// Kept in sync with `get_cdp_connect_tool` + `get_cdp_tools` in
    /// `src/server.rs`.
    const CDP_TOOL_NAMES: &[&str] = &[
        "cdp_connect",
        "cdp_disconnect",
        "cdp_take_dom_snapshot",
        "cdp_find_elements",
        "cdp_evaluate_script",
        "cdp_click",
        "cdp_list_pages",
        "cdp_select_page",
        "cdp_hover",
        "cdp_fill",
        "cdp_press_key",
        "cdp_handle_dialog",
        "cdp_navigate",
        "cdp_new_page",
        "cdp_close_page",
        "cdp_wait_for",
        "cdp_type_text",
        "cdp_element_at_point",
    ];

    #[test]
    fn test_cdp_tools_always_visible_when_disconnected() {
        // CDP tools are now listed unconditionally so the tool surface does
        // not mutate mid-session. Handlers return a clean "No CDP connection"
        // error when called without an active connection.
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        for tool in CDP_TOOL_NAMES {
            assert!(
                names.contains(&tool.to_string()),
                "{} should be listed even without a CDP connection",
                tool
            );
        }
    }

    #[test]
    fn test_cdp_tools_always_visible_when_connected() {
        let tools = MacOSDevToolsServer::get_tools(false, false, true, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        for tool in CDP_TOOL_NAMES {
            assert!(
                names.contains(&tool.to_string()),
                "{} should be listed when connected",
                tool
            );
        }
    }

    #[test]
    fn test_cdp_tool_list_is_stable_across_connection_state() {
        // The visible tool list must be identical regardless of
        // `cdp_connected` — mid-session tool-list changes are the whole
        // reason these tools are now unconditional.
        let disconnected = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let connected = MacOSDevToolsServer::get_tools(false, false, true, false, false);
        assert_eq!(
            disconnected.len(),
            connected.len(),
            "CDP connection state must not change the number of visible tools"
        );
        let dn: Vec<_> = disconnected.iter().map(|t| t.name.to_string()).collect();
        let cn: Vec<_> = connected.iter().map(|t| t.name.to_string()).collect();
        assert_eq!(dn, cn);
    }
}

#[cfg(test)]
#[cfg(feature = "cdp")]
mod cdp_not_connected_errors {
    //! Calling CDP tool handlers without an active connection must return a
    //! structured error, not panic or silently no-op. This guards the
    //! "tools always listed" guarantee.

    use native_devtools_mcp::cdp::tools;
    use native_devtools_mcp::cdp::CdpClient;
    use std::sync::Arc;
    use tokio::sync::RwLock;

    fn empty_client() -> Arc<RwLock<Option<CdpClient>>> {
        Arc::new(RwLock::new(None))
    }

    fn assert_not_connected(result: &rmcp::model::CallToolResult) {
        assert_eq!(
            result.is_error,
            Some(true),
            "expected error result when CDP is not connected, got: {:?}",
            result
        );
        let text = result
            .content
            .iter()
            .filter_map(|c| c.as_text().map(|t| t.text.clone()))
            .collect::<Vec<_>>()
            .join("");
        assert!(
            text.to_lowercase().contains("no cdp connection"),
            "error should mention 'No CDP connection', got: {}",
            text
        );
    }

    #[tokio::test]
    async fn cdp_click_without_connection_returns_clean_error() {
        let result = tools::cdp_click("d1".to_string(), false, false, empty_client()).await;
        assert_not_connected(&result);
    }

    #[tokio::test]
    async fn cdp_take_dom_snapshot_without_connection_returns_clean_error() {
        let result = tools::cdp_take_dom_snapshot(None, empty_client()).await;
        assert_not_connected(&result);
    }

    #[tokio::test]
    async fn cdp_list_pages_without_connection_returns_clean_error() {
        let result = tools::cdp_list_pages(empty_client()).await;
        assert_not_connected(&result);
    }

    #[tokio::test]
    async fn cdp_element_at_point_without_connection_returns_clean_error() {
        let result = tools::cdp_element_at_point(0.0, 0.0, empty_client()).await;
        assert_not_connected(&result);
    }
}

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

    #[test]
    fn test_server_advertises_tool_list_changed() {
        let server = MacOSDevToolsServer::new();
        let info = server.get_info();

        // Check that tools capability exists
        let tools_cap = info.capabilities.tools.as_ref();
        assert!(
            tools_cap.is_some(),
            "Server should advertise tools capability"
        );

        // Check that list_changed is advertised
        let tools = tools_cap.unwrap();
        assert_eq!(
            tools.list_changed,
            Some(true),
            "Server should advertise tools.listChanged capability"
        );
    }
}

#[cfg(test)]
mod tool_annotations {
    //! Every tool the server exposes must carry MCP safety-hint
    //! annotations (readOnlyHint, destructiveHint, idempotentHint,
    //! openWorldHint) so clients can reason about the tool before calling.

    use super::*;

    /// Build the full set of tools across every connection permutation so
    /// conditional tools (app_*, android_*, cdp_*, hover, recording) are all
    /// visible.
    fn all_tools() -> Vec<rmcp::model::Tool> {
        MacOSDevToolsServer::get_tools(true, true, true, true, true)
    }

    #[test]
    fn test_every_tool_has_annotations() {
        let tools = all_tools();
        let missing: Vec<String> = tools
            .iter()
            .filter(|t| t.annotations.is_none())
            .map(|t| t.name.to_string())
            .collect();
        assert!(
            missing.is_empty(),
            "These tools are missing MCP annotations: {:?}",
            missing
        );
    }

    fn find_tool<'a>(tools: &'a [rmcp::model::Tool], name: &str) -> &'a rmcp::model::Tool {
        tools
            .iter()
            .find(|t| t.name.as_ref() == name)
            .unwrap_or_else(|| panic!("tool {name} not found"))
    }

    fn find_annotation(tools: &[rmcp::model::Tool], name: &str) -> rmcp::model::ToolAnnotations {
        find_tool(tools, name)
            .annotations
            .clone()
            .unwrap_or_else(|| panic!("{name} should have annotations"))
    }

    #[test]
    fn test_quit_app_flagged_destructive() {
        let tools = all_tools();
        let ann = find_annotation(&tools, "quit_app");
        assert_eq!(ann.destructive_hint, Some(true));
        assert_eq!(ann.read_only_hint, Some(false));
    }

    #[cfg(feature = "cdp")]
    #[test]
    fn test_cdp_close_page_flagged_destructive() {
        let tools = all_tools();
        let ann = find_annotation(&tools, "cdp_close_page");
        assert_eq!(ann.destructive_hint, Some(true));
        assert_eq!(ann.read_only_hint, Some(false));
    }

    #[test]
    fn test_read_only_queries_flagged_read_only_and_idempotent() {
        let tools = all_tools();
        for name in [
            "take_screenshot",
            "list_windows",
            "list_apps",
            "find_text",
            "element_at_point",
            "probe_app",
        ] {
            let ann = find_annotation(&tools, name);
            assert_eq!(
                ann.read_only_hint,
                Some(true),
                "{name} should be readOnlyHint=true"
            );
            assert_eq!(
                ann.idempotent_hint,
                Some(true),
                "{name} should be idempotentHint=true"
            );
            assert_eq!(ann.destructive_hint, Some(false));
        }
    }

    #[test]
    fn test_state_change_tools_flagged_not_read_only_not_destructive() {
        let tools = all_tools();
        for name in ["click", "type_text", "press_key", "scroll", "drag"] {
            let ann = find_annotation(&tools, name);
            assert_eq!(ann.read_only_hint, Some(false), "{name}");
            assert_eq!(ann.destructive_hint, Some(false), "{name}");
        }
    }

    #[cfg(feature = "cdp")]
    #[test]
    fn test_cdp_navigate_open_world() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name.as_ref() == "cdp_navigate")
            .expect("cdp_navigate not found");
        let ann = tool
            .annotations
            .as_ref()
            .expect("cdp_navigate should have annotations");
        assert_eq!(
            ann.open_world_hint,
            Some(true),
            "cdp_navigate reaches the open web"
        );
    }

    #[cfg(feature = "cdp")]
    #[test]
    fn test_cdp_evaluate_script_flagged_destructive_and_open_world() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name.as_ref() == "cdp_evaluate_script")
            .expect("cdp_evaluate_script not found");
        let ann = tool
            .annotations
            .as_ref()
            .expect("cdp_evaluate_script should have annotations");
        assert_eq!(ann.destructive_hint, Some(true));
        assert_eq!(ann.open_world_hint, Some(true));
    }
}

#[cfg(test)]
mod identity_validation {
    //! Tests for expected_bundle_id and expected_app_name validation logic
    //!
    //! These tests exercise the extracted validation helpers that are used
    //! by app_connect to validate app identity.

    use native_devtools_mcp::tools::app_protocol::{
        validate_app_name, validate_bundle_id, validate_identity, IdentityValidationResult,
    };
    use serde_json::json;

    // MARK: - validate_bundle_id tests

    #[test]
    fn test_bundle_id_exact_match() {
        assert!(validate_bundle_id("com.example.MyApp", "com.example.MyApp"));
    }

    #[test]
    fn test_bundle_id_case_sensitive() {
        // Bundle ID matching should be case-sensitive
        assert!(!validate_bundle_id(
            "com.example.MyApp",
            "com.example.myapp"
        ));
        assert!(!validate_bundle_id(
            "com.example.MyApp",
            "COM.EXAMPLE.MYAPP"
        ));
    }

    #[test]
    fn test_bundle_id_different_ids() {
        assert!(!validate_bundle_id("com.example.MyApp", "com.other.MyApp"));
        assert!(!validate_bundle_id("com.example.MyApp", ""));
    }

    // MARK: - validate_app_name tests

    #[test]
    fn test_app_name_exact_match() {
        assert!(validate_app_name("MyApp", "MyApp"));
    }

    #[test]
    fn test_app_name_case_insensitive() {
        assert!(validate_app_name("MyApp", "myapp"));
        assert!(validate_app_name("MyApp", "MYAPP"));
        assert!(validate_app_name("myapp", "MyApp"));
    }

    #[test]
    fn test_app_name_whitespace_trimmed() {
        assert!(validate_app_name("MyApp", "  MyApp  "));
        assert!(validate_app_name("  MyApp  ", "MyApp"));
        assert!(validate_app_name("  MyApp  ", "  myapp  "));
    }

    #[test]
    fn test_app_name_different_names() {
        assert!(!validate_app_name("MyApp", "OtherApp"));
        assert!(!validate_app_name("MyApp", ""));
    }

    // MARK: - validate_identity tests (integration of both validations)

    #[test]
    fn test_validate_identity_no_expectations() {
        let info = json!({
            "appName": "TestApp",
            "bundleId": "com.test.TestApp"
        });

        assert_eq!(
            validate_identity(None, None, &info),
            IdentityValidationResult::Ok
        );
    }

    #[test]
    fn test_validate_identity_bundle_id_match() {
        let info = json!({
            "appName": "TestApp",
            "bundleId": "com.test.TestApp"
        });

        assert_eq!(
            validate_identity(Some("com.test.TestApp"), None, &info),
            IdentityValidationResult::Ok
        );
    }

    #[test]
    fn test_validate_identity_bundle_id_mismatch() {
        let info = json!({
            "appName": "TestApp",
            "bundleId": "com.test.TestApp"
        });

        assert_eq!(
            validate_identity(Some("com.other.App"), None, &info),
            IdentityValidationResult::BundleIdMismatch {
                expected: "com.other.App".to_string(),
                actual: "com.test.TestApp".to_string(),
                actual_app_name: "TestApp".to_string(),
            }
        );
    }

    #[test]
    fn test_validate_identity_app_name_match() {
        let info = json!({
            "appName": "TestApp",
            "bundleId": "com.test.TestApp"
        });

        assert_eq!(
            validate_identity(None, Some("TestApp"), &info),
            IdentityValidationResult::Ok
        );
        // Case-insensitive
        assert_eq!(
            validate_identity(None, Some("testapp"), &info),
            IdentityValidationResult::Ok
        );
    }

    #[test]
    fn test_validate_identity_app_name_mismatch() {
        let info = json!({
            "appName": "TestApp",
            "bundleId": "com.test.TestApp"
        });

        assert_eq!(
            validate_identity(None, Some("OtherApp"), &info),
            IdentityValidationResult::AppNameMismatch {
                expected: "OtherApp".to_string(),
                actual: "TestApp".to_string(),
                actual_bundle_id: "com.test.TestApp".to_string(),
            }
        );
    }

    #[test]
    fn test_validate_identity_both_expectations_pass() {
        let info = json!({
            "appName": "TestApp",
            "bundleId": "com.test.TestApp"
        });

        assert_eq!(
            validate_identity(Some("com.test.TestApp"), Some("testapp"), &info),
            IdentityValidationResult::Ok
        );
    }

    #[test]
    fn test_validate_identity_bundle_id_checked_first() {
        // When both are provided, bundle ID is checked first
        let info = json!({
            "appName": "TestApp",
            "bundleId": "com.test.TestApp"
        });

        // Both mismatch - should return BundleIdMismatch since it's checked first
        let result = validate_identity(Some("com.wrong.Id"), Some("WrongName"), &info);
        assert!(matches!(
            result,
            IdentityValidationResult::BundleIdMismatch { .. }
        ));
    }

    #[test]
    fn test_validate_identity_missing_fields() {
        // Empty info object
        let info = json!({});

        // Missing bundleId should fail if expected
        assert_eq!(
            validate_identity(Some("com.test.App"), None, &info),
            IdentityValidationResult::BundleIdMismatch {
                expected: "com.test.App".to_string(),
                actual: "".to_string(),
                actual_app_name: "".to_string(),
            }
        );

        // Missing appName should fail if expected
        assert_eq!(
            validate_identity(None, Some("TestApp"), &info),
            IdentityValidationResult::AppNameMismatch {
                expected: "TestApp".to_string(),
                actual: "".to_string(),
                actual_bundle_id: "".to_string(),
            }
        );
    }

    #[test]
    fn test_validate_identity_whitespace_in_app_name() {
        let info = json!({
            "appName": "  TestApp  ",
            "bundleId": "com.test.TestApp"
        });

        // Should still match with trimming
        assert_eq!(
            validate_identity(None, Some("TestApp"), &info),
            IdentityValidationResult::Ok
        );
        assert_eq!(
            validate_identity(None, Some("  testapp  "), &info),
            IdentityValidationResult::Ok
        );
    }
}

#[cfg(test)]
#[cfg(target_os = "macos")]
mod hover_tracking_tool_gating {
    use super::*;

    #[test]
    fn test_start_hover_tracking_always_visible() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(names.contains(&"start_hover_tracking".to_string()));
    }

    #[test]
    fn test_hover_tools_hidden_when_not_tracking() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(!names.contains(&"get_hover_events".to_string()));
        assert!(!names.contains(&"stop_hover_tracking".to_string()));
    }

    #[test]
    fn test_hover_tools_visible_when_tracking() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, true, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(names.contains(&"start_hover_tracking".to_string()));
        assert!(names.contains(&"get_hover_events".to_string()));
        assert!(names.contains(&"stop_hover_tracking".to_string()));
    }

    #[test]
    fn test_hover_tracking_adds_two_tools() {
        let not_tracking = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let tracking = MacOSDevToolsServer::get_tools(false, false, false, true, false);
        assert_eq!(tracking.len() - not_tracking.len(), 2);
    }

    #[test]
    fn test_all_states_independent() {
        // Hover tracking state should be independent of app/android connection
        let base = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let hover_only = MacOSDevToolsServer::get_tools(false, false, false, true, false);
        let app_and_hover = MacOSDevToolsServer::get_tools(true, false, false, true, false);
        // Hover adds exactly 2 tools regardless of other state
        assert_eq!(hover_only.len() - base.len(), 2);

        // App connection adds the same number of tools regardless of hover state
        let app_only = MacOSDevToolsServer::get_tools(true, false, false, false, false);
        assert_eq!(
            app_and_hover.len() - hover_only.len(),
            app_only.len() - base.len()
        );

        // All five states are additive
        let android_only = MacOSDevToolsServer::get_tools(false, true, false, false, false);
        let cdp_only = MacOSDevToolsServer::get_tools(false, false, true, false, false);
        let recording_only = MacOSDevToolsServer::get_tools(false, false, false, false, true);
        let all = MacOSDevToolsServer::get_tools(true, true, true, true, true);
        let expected_total = base.len()
            + (app_only.len() - base.len())
            + (android_only.len() - base.len())
            + (cdp_only.len() - base.len())
            + (hover_only.len() - base.len())
            + (recording_only.len() - base.len());
        assert_eq!(all.len(), expected_total);
    }
}

#[cfg(test)]
#[cfg(target_os = "macos")]
mod recording_tool_gating {
    use super::*;

    #[test]
    fn test_start_recording_always_visible() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(names.contains(&"start_recording".to_string()));
    }

    #[test]
    fn test_stop_recording_hidden_when_not_recording() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(!names.contains(&"stop_recording".to_string()));
    }

    #[test]
    fn test_stop_recording_visible_when_recording() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, true);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(names.contains(&"start_recording".to_string()));
        assert!(names.contains(&"stop_recording".to_string()));
    }

    #[test]
    fn test_recording_adds_one_tool() {
        let not_recording = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let recording = MacOSDevToolsServer::get_tools(false, false, false, false, true);
        assert_eq!(recording.len() - not_recording.len(), 1);
    }
}

#[cfg(test)]
#[cfg(target_os = "macos")]
mod ax_dispatch_tool_gating {
    use super::*;

    #[test]
    fn ax_dispatch_triad_is_visible_on_macos() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(names.contains(&"ax_click".to_string()));
        assert!(names.contains(&"ax_set_value".to_string()));
        assert!(names.contains(&"ax_select".to_string()));
    }

    #[test]
    fn ax_click_flagged_state_change() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let tool = tools
            .iter()
            .find(|t| t.name.as_ref() == "ax_click")
            .expect("ax_click not found");
        let ann = tool
            .annotations
            .as_ref()
            .expect("ax_click should have annotations");
        assert_eq!(ann.read_only_hint, Some(false));
        assert_eq!(ann.destructive_hint, Some(false));
        assert_eq!(ann.idempotent_hint, Some(false));
    }

    #[test]
    fn ax_set_value_flagged_state_change() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let tool = tools
            .iter()
            .find(|t| t.name.as_ref() == "ax_set_value")
            .expect("ax_set_value not found");
        let ann = tool
            .annotations
            .as_ref()
            .expect("ax_set_value should have annotations");
        assert_eq!(ann.read_only_hint, Some(false));
        assert_eq!(ann.destructive_hint, Some(false));
    }

    #[test]
    fn ax_select_flagged_state_change() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let tool = tools
            .iter()
            .find(|t| t.name.as_ref() == "ax_select")
            .expect("ax_select not found");
        let ann = tool
            .annotations
            .as_ref()
            .expect("ax_select should have annotations");
        assert_eq!(ann.read_only_hint, Some(false));
        assert_eq!(ann.destructive_hint, Some(false));
        assert_eq!(ann.idempotent_hint, Some(false));
    }

    #[test]
    fn instructions_mention_ax_dispatch_triad() {
        let server = MacOSDevToolsServer::new();
        let info = server.get_info();
        let instr = info.instructions.expect("instructions should be set");
        assert!(
            instr.contains("ax_click"),
            "instructions should mention ax_click"
        );
        assert!(
            instr.contains("ax_set_value"),
            "instructions should mention ax_set_value"
        );
        assert!(
            instr.contains("ax_select"),
            "instructions should mention ax_select"
        );
        assert!(
            instr.contains("take_ax_snapshot"),
            "instructions should mention take_ax_snapshot as the snapshot source for ax_* tools"
        );
    }

    /// Static guardrail — the name strings in the `server.rs::call_tool`
    /// match arms must exactly equal the registered tool names, or clients
    /// will call a tool and get "method not found" instead of a handler.
    ///
    /// We probe this indirectly: the test constructs tool request params
    /// with the expected names and asserts they deserialize into the
    /// handler's param structs — cheap, cfg-gated, and catches both
    /// misspellings and schema drift.
    #[test]
    fn ax_dispatch_param_structs_accept_schema_fields() {
        use native_devtools_mcp::tools::ax_click::AxClickParams;
        use native_devtools_mcp::tools::ax_select::AxSelectParams;
        use native_devtools_mcp::tools::ax_set_value::AxSetValueParams;

        let p: AxClickParams = serde_json::from_value(serde_json::json!({ "uid": "a1g1" }))
            .expect("ax_click params must accept { uid }");
        assert_eq!(p.uid, "a1g1");

        let q: AxSetValueParams =
            serde_json::from_value(serde_json::json!({ "uid": "a2g1", "text": "hello" }))
                .expect("ax_set_value params must accept { uid, text }");
        assert_eq!(q.uid, "a2g1");
        assert_eq!(q.text, "hello");

        let s: AxSelectParams = serde_json::from_value(serde_json::json!({ "uid": "a3g1" }))
            .expect("ax_select params must accept { uid }");
        assert_eq!(s.uid, "a3g1");
    }
}

#[cfg(test)]
#[cfg(not(target_os = "macos"))]
mod ax_dispatch_tool_gating_non_macos {
    use super::*;

    #[test]
    fn ax_dispatch_triad_is_absent_off_macos() {
        let tools = MacOSDevToolsServer::get_tools(true, true, true, true, true);
        let names: Vec<String> = tools.iter().map(|t| t.name.to_string()).collect();
        assert!(!names.contains(&"ax_click".to_string()));
        assert!(!names.contains(&"ax_set_value".to_string()));
        assert!(!names.contains(&"ax_select".to_string()));
    }

    #[test]
    fn instructions_do_not_advertise_macos_tools_off_macos() {
        let server = MacOSDevToolsServer::new();
        let info = server.get_info();
        let instr = info.instructions.expect("instructions should be set");
        assert!(
            !instr.contains("ax_click"),
            "Windows/Linux build must not advertise macOS-only tools in instructions"
        );
        assert!(
            !instr.contains("ax_set_value"),
            "Windows/Linux build must not advertise macOS-only tools in instructions"
        );
        assert!(
            !instr.contains("ax_select"),
            "Windows/Linux build must not advertise macOS-only tools in instructions"
        );
    }
}

// Cross-platform: `take_ax_snapshot` is now state-changing on BOTH platforms.
// Windows takes a minor accuracy hit (the tool really is still read-only
// there) in exchange for a uniform client-safety posture — see the design
// doc's §Tool surface > Annotation change.
#[cfg(test)]
mod take_ax_snapshot_annotation_migration {
    use super::*;

    #[test]
    fn take_ax_snapshot_flagged_state_change_cross_platform() {
        let tools = MacOSDevToolsServer::get_tools(false, false, false, false, false);
        let tool = tools
            .iter()
            .find(|t| t.name.as_ref() == "take_ax_snapshot")
            .expect("take_ax_snapshot not found");
        let ann = tool
            .annotations
            .as_ref()
            .expect("take_ax_snapshot should have annotations");
        assert_eq!(
            ann.read_only_hint,
            Some(false),
            "take_ax_snapshot must not be readOnlyHint=true after this branch"
        );
        assert_eq!(
            ann.idempotent_hint,
            Some(false),
            "take_ax_snapshot must not be idempotentHint=true after this branch"
        );
        assert_eq!(ann.destructive_hint, Some(false));
    }
}