force 0.4.0

Production-ready Salesforce Platform API client with REST and Bulk API 2.0 support
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
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
//! Composite Graph API.
//!
//! The Composite Graph API provides an enhanced way to perform complex, dependent operations
//! in a single request. Unlike the Batch API, which is a flat list of independent requests,
//! the Graph API allows you to model dependencies between requests using a graph structure.
//!
//! Each graph can contain up to 500 nodes (requests) and a maximum depth of 15.
//! You can execute multiple graphs in a single API call.

use super::CompositeHandler;
use crate::api::soql::SoqlQueryBuilder;
use crate::auth::Authenticator;
use crate::error::{ForceError, Result};
use crate::types::validator;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Validates graph reference IDs using the shared identifier validator.
fn validate_reference_id(id: &str) -> Result<()> {
    validator::validate_identifier(id, "Reference ID")
}

/// Helper to validate graph IDs.
/// IDs can be Salesforce IDs or references (e.g. "@{ref.id}"),
/// but they strictly cannot contain path traversal characters.
fn validate_graph_id(id: &str) -> Result<()> {
    if id.is_empty() {
        return Err(ForceError::InvalidInput("ID cannot be empty".to_string()));
    }
    if id.contains('/') || id.contains("..") || id.contains('\\') || id.contains('?') {
        return Err(ForceError::InvalidInput(format!(
            "ID contains invalid path traversal characters: {}",
            id
        )));
    }
    Ok(())
}

/// Normalizes a Composite Graph sub-request URL to a full absolute API path.
///
/// The Composite Graph API resolves sub-request URLs as absolute paths from the
/// API root, so every URL must start with `/services/data/vNN.N/`. This differs
/// from the Batch API, whose [`normalize_subrequest_url`](super::batch::normalize_subrequest_url)
/// emits version-relative URLs (`vNN.N/...`) that Salesforce resolves against
/// `/services/data/`.
///
/// Behavior:
/// - Absolute URLs (`http://`, `https://`) are left untouched.
/// - URLs already rooted at `/services/` (with or without a leading slash) are
///   normalized to a single leading slash and otherwise left untouched.
/// - Version-prefixed URLs (`vNN.N/...`) get the `/services/data/` prefix.
/// - Bare relative URLs (`query?q=...`, `sobjects/Account`) get the full
///   `/services/data/{api_version}/` prefix.
fn normalize_graph_subrequest_url(url: &str, api_version: &str) -> String {
    let trimmed = url.trim_start_matches('/');

    if trimmed.starts_with("http://") || trimmed.starts_with("https://") {
        return url.to_string();
    }

    if trimmed.starts_with("services/") {
        return format!("/{trimmed}");
    }

    if super::batch::is_api_version_prefixed(trimmed) {
        return format!("/services/data/{trimmed}");
    }

    let api_ver = api_version.trim_matches('/');
    format!("/services/data/{api_ver}/{trimmed}")
}

/// Constructs a Composite Graph request.
///
/// Use this request object to add one or more graphs and execute them atomically.
#[derive(Debug)]
pub struct CompositeGraphRequest<A: Authenticator> {
    handler: CompositeHandler<A>,
    graphs: Vec<Graph>,
}

impl<A: Authenticator> CompositeGraphRequest<A> {
    /// Creates a new CompositeGraphRequest.
    ///
    /// # Performance
    ///
    /// Pre-allocates capacity for 15 graphs (typical small batch)
    /// to avoid heap reallocations during graph accumulation.
    pub(crate) fn new(handler: CompositeHandler<A>) -> Self {
        Self {
            handler,
            graphs: Vec::with_capacity(15),
        }
    }

    /// Adds a graph to the request.
    ///
    /// # Arguments
    ///
    /// * `graph` - The graph to add
    ///
    /// # Errors
    ///
    /// Returns an error if the total number of subrequests across all graphs exceeds 500.
    pub fn add_graph(mut self, graph: Graph) -> Result<Self> {
        let current_subrequests: usize =
            self.graphs.iter().map(|g| g.composite_request.len()).sum();
        if current_subrequests + graph.composite_request.len() > 500 {
            return Err(ForceError::InvalidInput(
                "Composite Graph limit of 500 total subrequests exceeded".to_string(),
            ));
        }
        self.graphs.push(graph);
        Ok(self)
    }

    /// Executes the graph request.
    ///
    /// Sends all accumulated graphs to the Salesforce Composite Graph API.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Authentication fails
    /// - The HTTP request fails
    /// - The response cannot be deserialized
    pub async fn execute(self) -> Result<GraphResponse> {
        if self.graphs.is_empty() {
            return Err(ForceError::Serialization(
                crate::error::SerializationError::InvalidFormat(
                    "Graph request cannot be empty".to_string(),
                ),
            ));
        }

        // Construct the composite graph URL
        let url = self.handler.inner.resolve_url("composite/graph").await?;

        // Normalize each sub-request URL to a FULL absolute API path. Unlike the
        // Batch API — which resolves version-relative URLs (`v62.0/query?...`)
        // against `/services/data/` — the Composite Graph API requires each
        // sub-request URL to be the absolute path from the API root
        // (`/services/data/v62.0/query?...`). A bare `query?q=...` is otherwise
        // rejected with `PROCESSING_HALTED: "... is not a valid url"`.
        let api_version = self.handler.inner.config.api_version.as_str();
        let mut graphs = self.graphs;
        for graph in &mut graphs {
            for request in &mut graph.composite_request {
                request.url = normalize_graph_subrequest_url(&request.url, api_version);
            }
        }

        let request_body = GraphRequestBody { graphs };

        let request = self
            .handler
            .inner
            .post(&url)
            .json(&request_body)
            .build()
            .map_err(crate::error::HttpError::from)?;

        self.handler
            .inner
            .send_request_and_decode(request, "Composite Graph failed")
            .await
    }
}

/// A single graph within a Composite Graph request.
///
/// A graph is a collection of dependent subrequests.
///
/// # Supported nodes
///
/// The Composite Graph API only accepts sObject **record** operations as nodes:
/// `sobjects/{type}` (POST), `sobjects/{type}/{id}` (GET/PATCH/DELETE), and
/// `sobjects/{type}/{externalIdField}/{externalId}` (GET/PATCH/POST/DELETE). The
/// SOQL `/query` resource is **not** a supported node (see [`Graph::query`]);
/// prefer [`Graph::get`]/[`Graph::post`]/[`Graph::patch`]/[`Graph::delete`].
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Graph {
    /// Unique identifier for this graph.
    pub graph_id: String,
    /// List of subrequests in this graph.
    pub composite_request: Vec<GraphRequest>,
}

impl Graph {
    /// Creates a new Graph.
    ///
    /// # Arguments
    ///
    /// * `graph_id` - A unique identifier for this graph (e.g., "graph1")
    ///
    /// # Performance
    ///
    /// Pre-allocates capacity for 15 subrequests (typical small graph)
    /// to avoid heap reallocations during request accumulation.
    #[must_use]
    pub fn new(graph_id: impl Into<String>) -> Self {
        Self {
            graph_id: graph_id.into(),
            composite_request: Vec::with_capacity(15),
        }
    }

    /// Adds a subrequest to the graph.
    ///
    /// # Arguments
    ///
    /// * `request` - The subrequest to add
    pub fn add_request(mut self, request: GraphRequest) -> Result<Self> {
        if self.composite_request.len() >= 500 {
            return Err(ForceError::InvalidInput(
                "Graph size limit of 500 requests exceeded".to_string(),
            ));
        }
        self.composite_request.push(request);
        Ok(self)
    }

    /// Adds a GET request to the graph.
    ///
    /// # Arguments
    ///
    /// * `sobject` - The SObject type (e.g., "Account")
    /// * `id` - The record ID or reference (e.g., "@{ref.id}")
    /// * `reference_id` - Unique reference ID for this request
    pub fn get(self, sobject: &str, id: &str, reference_id: &str) -> Result<Self> {
        validator::validate_sobject_name(sobject)?;
        validate_graph_id(id)?;
        validate_reference_id(reference_id)?;
        self.add_request(GraphRequest::new(
            "GET",
            crate::api::path_utils::format_sobject_path(sobject, Some(id)),
            reference_id,
        )?)
    }

    /// Adds a POST (Create) request to the graph.
    ///
    /// # Arguments
    ///
    /// * `sobject` - The SObject type (e.g., "Account")
    /// * `body` - The JSON body of the record
    /// * `reference_id` - Unique reference ID for this request
    pub fn post(self, sobject: &str, body: Value, reference_id: &str) -> Result<Self> {
        validator::validate_sobject_name(sobject)?;
        validate_reference_id(reference_id)?;
        self.add_request(
            GraphRequest::new(
                "POST",
                crate::api::path_utils::format_sobject_path(sobject, None),
                reference_id,
            )?
            .body(body),
        )
    }

    /// Adds a PATCH (Update) request to the graph.
    ///
    /// # Arguments
    ///
    /// * `sobject` - The SObject type (e.g., "Account")
    /// * `id` - The record ID or reference
    /// * `body` - The JSON body with fields to update
    /// * `reference_id` - Unique reference ID for this request
    pub fn patch(self, sobject: &str, id: &str, body: Value, reference_id: &str) -> Result<Self> {
        validator::validate_sobject_name(sobject)?;
        validate_graph_id(id)?;
        validate_reference_id(reference_id)?;
        self.add_request(
            GraphRequest::new(
                "PATCH",
                crate::api::path_utils::format_sobject_path(sobject, Some(id)),
                reference_id,
            )?
            .body(body),
        )
    }

    /// Adds a DELETE request to the graph.
    ///
    /// # Arguments
    ///
    /// * `sobject` - The SObject type (e.g., "Account")
    /// * `id` - The record ID or reference
    /// * `reference_id` - Unique reference ID for this request
    pub fn delete(self, sobject: &str, id: &str, reference_id: &str) -> Result<Self> {
        validator::validate_sobject_name(sobject)?;
        validate_graph_id(id)?;
        validate_reference_id(reference_id)?;
        self.add_request(GraphRequest::new(
            "DELETE",
            crate::api::path_utils::format_sobject_path(sobject, Some(id)),
            reference_id,
        )?)
    }

    /// Adds a SOQL query request to the graph.
    ///
    /// The query will be properly URL-encoded according to form-urlencoded rules
    /// (e.g., spaces become `+`, `%` becomes `%25`).
    ///
    /// # ⚠️ Not supported by the Composite Graph API
    ///
    /// The Salesforce Composite Graph API does **not** support the SOQL `/query`
    /// (or `/queryAll`) resource as a graph node. Only sObject record operations
    /// are valid nodes — `sobjects/{type}` (POST), `sobjects/{type}/{id}`
    /// (GET/PATCH/DELETE), and `sobjects/{type}/{externalIdField}/{externalId}`.
    /// A query node is rejected by the org with
    /// `404 OPERATION_NOT_ALLOWED: "Endpoint not supported in Composite Graph API"`.
    /// See the [supported subrequest URLs](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_graph_composite_subrequest.htm).
    ///
    /// This method is retained for API completeness and to build sub-request
    /// bodies for other tooling, but a graph containing a `query` node will fail
    /// when executed against a live org. Use [`Graph::get`], [`Graph::post`],
    /// [`Graph::patch`], or [`Graph::delete`] for real graph nodes.
    ///
    /// # Arguments
    ///
    /// * `query_builder` - The SOQL query builder
    /// * `reference_id` - Unique reference ID for this request
    ///
    /// # Errors
    ///
    /// Returns an error if the query builder contains validation errors
    /// or if formatting fails.
    #[allow(clippy::needless_pass_by_value)] // Ownership consumed to enforce builder pattern
    pub fn query(self, query_builder: SoqlQueryBuilder, reference_id: &str) -> Result<Self> {
        validate_reference_id(reference_id)?;
        let url = crate::api::soql::encode_soql_query_url(&query_builder)?;
        self.add_request(GraphRequest::new("GET", url, reference_id)?)
    }
}

/// A single subrequest within a graph.
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GraphRequest {
    /// HTTP method (GET, POST, PATCH, DELETE, etc.)
    pub method: String,
    /// Relative URL (e.g., "sobjects/Account")
    pub url: String,
    /// Unique reference ID for dependent requests
    pub reference_id: String,
    /// Request body (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<Value>,
}

impl GraphRequest {
    /// Creates a new GraphRequest.
    ///
    /// # Arguments
    ///
    /// * `method` - HTTP method
    /// * `url` - Relative URL
    /// * `reference_id` - Unique reference ID
    pub fn new(
        method: impl Into<String>,
        url: impl Into<String>,
        reference_id: impl Into<String>,
    ) -> Result<Self> {
        let url_str = url.into();
        validator::validate_url_path(&url_str)?;
        Ok(Self {
            method: method.into(),
            url: url_str,
            reference_id: reference_id.into(),
            body: None,
        })
    }

    /// Sets the request body.
    #[must_use]
    pub fn body(mut self, body: Value) -> Self {
        self.body = Some(body);
        self
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct GraphRequestBody {
    graphs: Vec<Graph>,
}

/// The response from a Composite Graph API call.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphResponse {
    /// List of results for each graph executed.
    pub graphs: Vec<GraphResult>,
}

/// The result of a single graph execution.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphResult {
    /// The ID of the graph.
    pub graph_id: String,
    /// True if the graph execution was successful.
    pub is_successful: bool,
    /// List of results for each subrequest in the graph.
    #[serde(default)]
    pub composite_response: Vec<GraphSubResponse>,
    /// Error details if the graph failed (e.g., transaction rolled back).
    #[serde(default)]
    pub graph_response: Option<GraphErrorResponse>,
}

/// Detailed error response for a failed graph.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphErrorResponse {
    /// List of subrequests with their statuses.
    pub composite_response: Vec<GraphSubResponse>,
}

/// The result of a single subrequest within a graph.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphSubResponse {
    /// The HTTP status code.
    pub http_status_code: u16,
    /// The reference ID of the request.
    pub reference_id: String,
    /// The body of the response (can be success or error).
    pub body: Option<Value>,
    /// HTTP headers (optional).
    pub http_headers: Option<Value>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::builder as client_builder;
    use crate::test_utils::mock_auth::MockAuthenticator;
    use crate::test_utils::must::Must;
    use serde_json::json;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    async fn create_builder() -> CompositeGraphRequest<MockAuthenticator> {
        let auth = MockAuthenticator::new("token", "https://test.salesforce.com");
        let client = client_builder().authenticate(auth).build().await.must();

        client.composite().graph()
    }

    #[tokio::test]
    async fn test_composite_graph_total_limit() {
        let mut builder = create_builder().await;

        let mut graph1 = Graph::new("graph1");
        for i in 0..250 {
            graph1 = graph1
                .get("Account", "001000000000000AAA", &format!("ref{}", i))
                .must();
        }
        builder = builder.add_graph(graph1).must();

        let mut graph2 = Graph::new("graph2");
        for i in 0..251 {
            graph2 = graph2
                .get("Account", "001000000000000AAA", &format!("ref2_{}", i))
                .must();
        }

        // Total would be 501, which exceeds the limit
        let result = builder.add_graph(graph2);
        assert!(matches!(result, Err(ForceError::InvalidInput(_))));
    }

    #[test]
    fn test_graph_serialization() {
        let mut graph = Graph::new("graph1");
        graph = graph
            .post("Account", json!({"Name": "RefAccount"}), "refAccount")
            .must();
        graph = graph
            .post(
                "Contact",
                json!({
                    "LastName": "Doe",
                    "AccountId": "@{refAccount.id}"
                }),
                "refContact",
            )
            .must();

        let req = GraphRequestBody {
            graphs: vec![graph],
        };

        let json = serde_json::to_string(&req).must();
        assert!(json.contains("\"graphId\":\"graph1\""));
        assert!(json.contains("\"referenceId\":\"refAccount\""));
        assert!(json.contains("\"referenceId\":\"refContact\""));
        assert!(json.contains("\"AccountId\":\"@{refAccount.id}\""));
    }

    #[tokio::test]
    async fn test_graph_execute_success() {
        let mock_server = MockServer::start().await;
        let auth = MockAuthenticator::new("token", &mock_server.uri());
        let client = client_builder().authenticate(auth).build().await.must();

        let mut graph = Graph::new("graph1");
        graph = graph
            .post("Account", json!({"Name": "Test"}), "acc1")
            .must();

        let builder = client.composite().graph().add_graph(graph).must();

        let response_json = json!({
            "graphs": [
                {
                    "graphId": "graph1",
                    "isSuccessful": true,
                    "compositeResponse": [
                        {
                            "body": {
                                "id": "001...",
                                "success": true,
                                "errors": []
                            },
                            "httpHeaders": {},
                            "httpStatusCode": 201,
                            "referenceId": "acc1"
                        }
                    ]
                }
            ]
        });

        Mock::given(method("POST"))
            .and(path("/services/data/v67.0/composite/graph"))
            .respond_with(ResponseTemplate::new(200).set_body_json(response_json))
            .mount(&mock_server)
            .await;

        let response = builder.execute().await.must();
        assert_eq!(response.graphs.len(), 1);
        assert!(response.graphs[0].is_successful);
        assert_eq!(response.graphs[0].graph_id, "graph1");
        assert_eq!(
            response.graphs[0].composite_response[0].reference_id,
            "acc1"
        );
    }

    #[tokio::test]
    async fn test_graph_execute_uses_full_services_data_subrequest_urls() {
        use wiremock::matchers::body_json;

        let mock_server = MockServer::start().await;
        let auth = MockAuthenticator::new("token", &mock_server.uri());
        let client = client_builder().authenticate(auth).build().await.must();

        // This drives the exact real code path `tests/live_core.rs` uses:
        // `Graph::new(..).query(SoqlQueryBuilder::new().select(&["Id"]).from("Account").limit(1), ..)`.
        // `encode_soql_query_url` yields a bare `query?q=...` sub-request URL. The
        // Composite Graph API rejects that unless it is the full absolute
        // `/services/data/vNN.N/` path — a version-relative `v62.0/query?...`
        // (the pre-fix batch-style normalization) was echoed and rejected by the
        // live org as `/v62.0/query?...` (OPERATION_NOT_ALLOWED, missing
        // /services/data). Reverting `normalize_graph_subrequest_url` to that
        // behavior makes the `body_json` matcher below miss and this test fail.
        let account_query = crate::api::soql::SoqlQueryBuilder::new()
            .select(&["Id"])
            .from("Account")
            .limit(1);
        let graph = Graph::new("graph1")
            .query(account_query, "acctQuery")
            .must();

        let builder = client.composite().graph().add_graph(graph).must();

        Mock::given(method("POST"))
            .and(path("/services/data/v67.0/composite/graph"))
            .and(body_json(json!({
                "graphs": [
                    {
                        "graphId": "graph1",
                        "compositeRequest": [
                            {
                                "method": "GET",
                                "url": "/services/data/v67.0/query?q=SELECT+Id+FROM+Account+LIMIT+1",
                                "referenceId": "acctQuery"
                            }
                        ]
                    }
                ]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "graphs": [
                    {
                        "graphId": "graph1",
                        "isSuccessful": true,
                        "compositeResponse": [
                            {
                                "body": {"records": []},
                                "httpHeaders": {},
                                "httpStatusCode": 200,
                                "referenceId": "acctQuery"
                            }
                        ]
                    }
                ]
            })))
            .expect(1)
            .mount(&mock_server)
            .await;

        let response = builder.execute().await.must();
        assert!(response.graphs[0].is_successful);
    }

    /// Reproduces the EXACT chain `tests/live_core.rs::live_core_composite_graph`
    /// drives — `client.composite().graph().add_graph(Graph::new(..).query(..))?.execute()`
    /// — pinned to the live org's default `v62.0` api_version, and asserts the
    /// captured on-the-wire sub-request URL is the full absolute
    /// `/services/data/v62.0/query?q=...` path (never the version-relative
    /// `/v62.0/query?...` that the live org rejects with OPERATION_NOT_ALLOWED).
    /// The `body_json` matcher + `.expect(1)` fail the test if the collection
    /// `execute()` emits any other URL shape.
    #[tokio::test]
    async fn test_graph_collection_execute_emits_full_services_data_url_v62() {
        use crate::config::{ClientConfig, Environment};
        use wiremock::matchers::body_json;

        let mock_server = MockServer::start().await;
        let config = ClientConfig {
            api_version: "v62.0".to_string(),
            environment: Environment::Production,
            timeout: std::time::Duration::from_secs(30),
            max_retries: 3,
        };
        let client = crate::client::ForceClientBuilder::new()
            .config(config)
            .authenticate(MockAuthenticator::new("token", &mock_server.uri()))
            .build()
            .await
            .must();

        let account_query = crate::api::soql::SoqlQueryBuilder::new()
            .select(&["Id"])
            .from("Account")
            .limit(1);
        let graph = Graph::new("live-graph")
            .query(account_query, "acctQuery")
            .must();

        Mock::given(method("POST"))
            .and(path("/services/data/v62.0/composite/graph"))
            .and(body_json(json!({
                "graphs": [
                    {
                        "graphId": "live-graph",
                        "compositeRequest": [
                            {
                                "method": "GET",
                                "url": "/services/data/v62.0/query?q=SELECT+Id+FROM+Account+LIMIT+1",
                                "referenceId": "acctQuery"
                            }
                        ]
                    }
                ]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "graphs": [
                    {
                        "graphId": "live-graph",
                        "isSuccessful": true,
                        "compositeResponse": [
                            {
                                "body": {"records": []},
                                "httpHeaders": {},
                                "httpStatusCode": 200,
                                "referenceId": "acctQuery"
                            }
                        ]
                    }
                ]
            })))
            .expect(1)
            .mount(&mock_server)
            .await;

        let response = client
            .composite()
            .graph()
            .add_graph(graph)
            .must()
            .execute()
            .await
            .must();
        assert!(response.graphs[0].is_successful);
    }

    /// Mirrors the redesigned `tests/live_core.rs::live_core_composite_graph`,
    /// which drives a **supported** sObject-GET graph node (the SOQL `/query`
    /// resource is not a valid Composite Graph node). Asserts the emitted
    /// sub-request URL is the full absolute
    /// `/services/data/{version}/sobjects/Account/{id}` path.
    #[tokio::test]
    async fn test_graph_collection_execute_emits_full_services_data_sobject_get_url() {
        use wiremock::matchers::body_json;

        let mock_server = MockServer::start().await;
        let auth = MockAuthenticator::new("token", &mock_server.uri());
        let client = client_builder().authenticate(auth).build().await.must();

        let graph = Graph::new("live-graph")
            .get("Account", "001000000000001AAA", "acctGet")
            .must();

        let builder = client.composite().graph().add_graph(graph).must();

        Mock::given(method("POST"))
            .and(path("/services/data/v67.0/composite/graph"))
            .and(body_json(json!({
                "graphs": [
                    {
                        "graphId": "live-graph",
                        "compositeRequest": [
                            {
                                "method": "GET",
                                "url": "/services/data/v67.0/sobjects/Account/001000000000001AAA",
                                "referenceId": "acctGet"
                            }
                        ]
                    }
                ]
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "graphs": [
                    {
                        "graphId": "live-graph",
                        "isSuccessful": true,
                        "compositeResponse": [
                            {
                                "body": {"Id": "001000000000001AAA", "Name": "Acme"},
                                "httpHeaders": {},
                                "httpStatusCode": 200,
                                "referenceId": "acctGet"
                            }
                        ]
                    }
                ]
            })))
            .expect(1)
            .mount(&mock_server)
            .await;

        let response = builder.execute().await.must();
        assert!(response.graphs[0].is_successful);
    }

    #[tokio::test]
    async fn test_graph_execute_empty() {
        let builder = create_builder().await;
        let result = builder.execute().await;

        let Err(ForceError::Serialization(e)) = result else {
            panic!("Expected Serialization error, got {:?}", result);
        };
        assert!(e.to_string().contains("Graph request cannot be empty"));
    }

    #[test]
    fn test_graph_query_encoding() {
        let query = SoqlQueryBuilder::new()
            .select(&["Id", "Name"])
            .from("Account")
            .where_eq("Name", "Acme & Co.");

        let graph = Graph::new("graph1").query(query, "refQuery").must();

        assert_eq!(graph.composite_request.len(), 1);

        let req = &graph.composite_request[0];
        assert_eq!(req.method, "GET");
        assert_eq!(req.reference_id, "refQuery");
        assert!(req.body.is_none());

        // Expected SOQL: SELECT Id, Name FROM Account WHERE Name = 'Acme & Co.'
        // Encoded: query?q=SELECT+Id%2C+Name+FROM+Account+WHERE+Name+%3D+%27Acme+%26+Co.%27
        let expected_url =
            "query?q=SELECT+Id%2C+Name+FROM+Account+WHERE+Name+%3D+%27Acme+%26+Co.%27";
        assert_eq!(req.url, expected_url);
    }

    #[test]
    fn test_normalize_graph_subrequest_url() {
        // Bare relative URLs get the full /services/data/{version}/ prefix.
        assert_eq!(
            normalize_graph_subrequest_url("query?q=SELECT+Id+FROM+Account", "v62.0"),
            "/services/data/v62.0/query?q=SELECT+Id+FROM+Account"
        );
        assert_eq!(
            normalize_graph_subrequest_url("sobjects/Account", "v62.0"),
            "/services/data/v62.0/sobjects/Account"
        );

        // Version-prefixed URLs only need /services/data/.
        assert_eq!(
            normalize_graph_subrequest_url("v62.0/query?q=SELECT+Id", "v67.0"),
            "/services/data/v62.0/query?q=SELECT+Id"
        );

        // Leading-slash + version (`/v62.0/query?...`) — the shape the live org
        // rejected as `/v62.0/query?...` (missing /services/data). The leading
        // slash is trimmed before the version check, so it still gets the full
        // /services/data/ prefix.
        assert_eq!(
            normalize_graph_subrequest_url(
                "/v62.0/query?q=SELECT+Id+FROM+Account+LIMIT+1",
                "v67.0"
            ),
            "/services/data/v62.0/query?q=SELECT+Id+FROM+Account+LIMIT+1"
        );

        // Already-rooted /services/ paths are left untouched (single leading slash).
        assert_eq!(
            normalize_graph_subrequest_url("/services/data/v62.0/sobjects/Account", "v67.0"),
            "/services/data/v62.0/sobjects/Account"
        );
        // Idempotent: a fully-qualified query path is returned verbatim.
        assert_eq!(
            normalize_graph_subrequest_url(
                "/services/data/v62.0/query?q=SELECT+Id+FROM+Account+LIMIT+1",
                "v67.0"
            ),
            "/services/data/v62.0/query?q=SELECT+Id+FROM+Account+LIMIT+1"
        );
        assert_eq!(
            normalize_graph_subrequest_url("services/data/v62.0/sobjects/Account", "v67.0"),
            "/services/data/v62.0/sobjects/Account"
        );

        // Absolute URLs are left untouched.
        assert_eq!(
            normalize_graph_subrequest_url("https://example.com/foo", "v62.0"),
            "https://example.com/foo"
        );

        // API version separators are trimmed so we never emit a double slash.
        assert_eq!(
            normalize_graph_subrequest_url("query?q=SELECT+Id", "/v62.0/"),
            "/services/data/v62.0/query?q=SELECT+Id"
        );
    }

    #[test]
    fn test_validate_reference_id() {
        assert!(validate_reference_id("valid_id_123").is_ok());
        assert!(validate_reference_id("validId").is_ok());
        assert!(validate_reference_id("").is_err());
        assert!(validate_reference_id("invalid ref id! @#$").is_err());
        assert!(validate_reference_id("invalid-ref").is_err());
    }

    #[test]
    fn test_validate_graph_id() {
        assert!(validate_graph_id("001000000000000").is_ok());
        assert!(validate_graph_id("@{ref.id}").is_ok());
        assert!(validate_graph_id("validId").is_ok());
        assert!(validate_graph_id("").is_err());
        assert!(validate_graph_id("some/path").is_err());
        assert!(validate_graph_id("..").is_err());
        assert!(validate_graph_id("path\\test").is_err());
        assert!(validate_graph_id("path?query").is_err());
    }

    #[test]
    fn test_graph_post_patch_delete() {
        let mut graph = Graph::new("graph1");

        // Valid POST
        graph = graph
            .post("Account", json!({"Name": "Test"}), "refPost")
            .must();

        // Valid PATCH
        graph = graph
            .patch(
                "Account",
                "001000000000000AAA",
                json!({"Name": "Updated"}),
                "refPatch",
            )
            .must();

        // Valid DELETE
        graph = graph
            .delete("Account", "001000000000000AAA", "refDelete")
            .must();

        assert_eq!(graph.composite_request.len(), 3);

        let post_req = &graph.composite_request[0];
        assert_eq!(post_req.method, "POST");
        assert_eq!(post_req.reference_id, "refPost");

        let patch_req = &graph.composite_request[1];
        assert_eq!(patch_req.method, "PATCH");
        assert_eq!(patch_req.reference_id, "refPatch");

        let delete_req = &graph.composite_request[2];
        assert_eq!(delete_req.method, "DELETE");
        assert_eq!(delete_req.reference_id, "refDelete");
    }

    #[test]
    fn test_graph_size_limit() {
        let mut graph = Graph::new("graph1");

        // Add 500 requests
        for i in 0..500 {
            graph = graph
                .get("Account", "001000000000000AAA", &format!("ref{}", i))
                .must();
        }

        // 501st request should fail
        let result = graph.get("Account", "001000000000000AAA", "ref501");

        assert!(
            matches!(result, Err(ForceError::InvalidInput(ref msg)) if msg.contains("limit of 500"))
        );
    }

    #[test]
    fn test_havoc_path_traversal() {
        let graph = Graph::new("graph1");

        let result = graph.get("Account", "../../../../../etc/passwd", "ref1");

        let Err(crate::error::ForceError::InvalidInput(msg)) = result else {
            panic!(
                "Expected InvalidInput error for path traversal, got: {:?}",
                result
            );
        };
        assert!(msg.contains("invalid path traversal characters"));
    }

    #[test]
    fn test_havoc_invalid_reference_id() {
        let graph = Graph::new("graph1");

        let result = graph.get("Account", "001xx000003DHP0AAO", "invalid ref id! @#$");

        let Err(crate::error::ForceError::InvalidInput(msg)) = result else {
            panic!(
                "Expected InvalidInput error for invalid reference id, got: {:?}",
                result
            );
        };
        assert!(msg.contains("Reference ID contains invalid characters"));
    }

    #[test]
    fn test_validate_reference_id_table() {
        let valid_ids = vec!["ref1", "Ref_2", "A", "1", "valid_ref_id_123"];
        let invalid_ids = vec![
            "", "ref-1", "ref 1", "ref!1", "ref@1", "ref#1", "ref$1", "ref%1", "ref^1", "ref&1",
            "ref*1", "ref(1", "ref)1", "ref+1", "ref=1", "ref{1", "ref}1", "ref[1", "ref]1",
            "ref|1", "ref\\1", "ref:1", "ref;1", "ref\"1", "ref'1", "ref<1", "ref>1", "ref,1",
            "ref.1", "ref?1", "ref/1",
        ];

        for id in valid_ids {
            assert!(
                validate_reference_id(id).is_ok(),
                "Expected {} to be valid",
                id
            );
        }

        for id in invalid_ids {
            let result = validate_reference_id(id);
            assert!(result.is_err(), "Expected {} to be invalid", id);
            let Err(ForceError::InvalidInput(msg)) = result else {
                panic!("Expected InvalidInput error for {}", id);
            };
            if id.is_empty() {
                assert_eq!(msg, "Reference ID cannot be empty");
            } else {
                assert!(msg.contains("Reference ID contains invalid characters:"));
            }
        }
    }

    #[test]
    fn test_graph_request_body() {
        let req = GraphRequest::new("POST", "sobjects/Account", "ref1")
            .must()
            .body(serde_json::json!({"Name": "Test"}));
        assert_eq!(req.body.must()["Name"], "Test");
    }

    #[test]
    fn test_validate_graph_id_table() {
        let valid_ids = vec!["001000000000000AAA", "@{ref1.id}"];
        let invalid_ids = vec![
            "",
            "../../../etc/passwd",
            "../something",
            "path\\to",
            "path/to",
            "id?param=1",
        ];

        for id in valid_ids {
            // validate_graph_id is a private helper, but we can test it indirectly
            // by calling graph.get() which uses it.
            let graph = Graph::new("graph1");
            let result = graph.get("Account", id, "ref1");
            assert!(result.is_ok(), "Expected {} to be valid graph id", id);
        }

        for id in invalid_ids {
            let graph = Graph::new("graph1");
            let result = graph.get("Account", id, "ref1");
            assert!(result.is_err(), "Expected {} to be invalid graph id", id);
            let Err(ForceError::InvalidInput(msg)) = result else {
                panic!("Expected InvalidInput error for {}", id);
            };
            if id.is_empty() {
                assert_eq!(msg, "ID cannot be empty");
            } else {
                assert!(msg.contains("ID contains invalid path traversal characters:"));
            }
        }
    }
}