force 0.2.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
//! Field Usage Scanner.
//!
//! This module provides a utility to scan SObjects and determine field usage statistics.
//! It helps identify "zombie fields" (fields that are rarely or never populated).

use crate::api::rest_operation::RestOperation;
use crate::auth::Authenticator;
use crate::client::ForceClient;
use crate::error::Result;
use crate::types::describe::FieldType;
use serde::Deserialize;
use serde_json::Value;

/// Usage statistics for a specific field.
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct FieldUsage {
    /// API name of the field.
    pub name: String,
    /// Data type of the field.
    #[serde(rename = "type")]
    pub type_: String,
    /// Number of records where this field is populated (non-null).
    pub populated_count: u64,
    /// Total number of records scanned.
    pub total_count: u64,
    /// Percentage of records where this field is populated (0.0 - 100.0).
    pub percentage: f64,
}

/// Scanner for analyzing field usage.
#[derive(Debug)]
pub struct FieldUsageScanner<'a, A: Authenticator> {
    client: &'a ForceClient<A>,
}

impl<'a, A: Authenticator> FieldUsageScanner<'a, A> {
    /// Creates a new scanner using the provided client.
    #[must_use]
    pub fn new(client: &'a ForceClient<A>) -> Self {
        Self { client }
    }

    /// Scans the specified SObject to determine field usage.
    ///
    /// This method fetches the object definition using the Describe API,
    /// then executes SOQL aggregate queries to count non-null values for each field.
    ///
    /// # Arguments
    ///
    /// * `sobject` - The API name of the SObject to scan (e.g., "Account").
    ///
    /// # Returns
    ///
    /// A list of `FieldUsage` stats for all scanable fields.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The Describe API call fails.
    /// - Any SOQL query fails.
    /// - The response cannot be parsed.
    pub async fn scan(&self, sobject: &str) -> Result<Vec<FieldUsage>> {
        // 1. Describe the object to get fields
        let describe = self.client.rest().describe(sobject).await?;

        // 2. Filter scanable fields
        let scanable_fields: Vec<_> = describe
            .fields
            .iter()
            .filter(|f| is_scanable(&f.type_))
            .collect();

        if scanable_fields.is_empty() {
            return Ok(Vec::new());
        }

        #[allow(unused_doc_comments)]
        /// âš¡ Bolt: Pre-allocate capacity to avoid heap reallocations when extending results.
        let mut results = Vec::with_capacity(scanable_fields.len());

        // 3. Batch fields to avoid SOQL character limits (safe chunk size: 20)
        for chunk in scanable_fields.chunks(20) {
            let usage = self.scan_batch(sobject, chunk).await?;
            results.extend(usage);
        }

        Ok(results)
    }

    async fn scan_batch(
        &self,
        sobject: &str,
        fields: &[&crate::types::describe::FieldDescribe],
    ) -> Result<Vec<FieldUsage>> {
        use std::fmt::Write;

        // Build query: SELECT COUNT(Id) total, COUNT(Field1) f0, COUNT(Field2) f1...
        // Performance: Pre-allocate capacity to avoid 20+ intermediate String allocations per batch.
        // Assuming ~20 bytes per field selection + base query size.
        let mut query = String::with_capacity(128 + (fields.len() * 25));

        query.push_str("SELECT COUNT(Id) total");

        for (i, field) in fields.iter().enumerate() {
            write!(query, ", COUNT({}) f{}", field.name, i)
                .unwrap_or_else(|_| unreachable!("writing to String is infallible"));
        }

        write!(query, " FROM {}", sobject)
            .unwrap_or_else(|_| unreachable!("writing to String is infallible"));

        // Execute query
        let response = self.client.rest().query::<Value>(&query).await?;

        if response.records.is_empty() {
            // Should not happen for aggregate queries unless table is empty?
            // Actually aggregate query always returns 1 row (unless grouped, which we aren't).
            // Wait, if table is empty, COUNT returns 0 in one row.
            return Ok(fields
                .iter()
                .map(|f| FieldUsage {
                    name: f.name.clone(),
                    type_: format!("{:?}", f.type_),
                    populated_count: 0,
                    total_count: 0,
                    percentage: 0.0,
                })
                .collect());
        }

        let record = &response.records[0];
        let total = record.get("total").and_then(|v| v.as_u64()).unwrap_or(0);

        let mut batch_results = Vec::with_capacity(fields.len());

        for (i, field) in fields.iter().enumerate() {
            let alias = format!("f{}", i);
            let count = record.get(&alias).and_then(|v| v.as_u64()).unwrap_or(0);

            let percentage = if total > 0 {
                (count as f64 / total as f64) * 100.0
            } else {
                0.0
            };

            batch_results.push(FieldUsage {
                name: field.name.clone(),
                type_: format!("{:?}", field.type_),
                populated_count: count,
                total_count: total,
                percentage,
            });
        }

        Ok(batch_results)
    }
}

fn is_scanable(field_type: &FieldType) -> bool {
    !matches!(
        field_type,
        FieldType::Address
            | FieldType::Location
            | FieldType::Base64
            | FieldType::Encryptedstring
            | FieldType::Datacategorygroupreference
    )
}

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

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

        setup_mock_describe(&mock_server).await;
        setup_mock_query(&mock_server).await;

        let scanner = FieldUsageScanner::new(&client);
        let usage = scanner.scan("Account").await.must();

        assert_eq!(usage.len(), 2);

        let id_usage = &usage[0];
        assert_eq!(id_usage.name, "Id");
        assert_eq!(id_usage.populated_count, 10);
        assert_eq!(id_usage.total_count, 10);
        assert!((id_usage.percentage - 100.0).abs() < f64::EPSILON);

        let name_usage = &usage[1];
        assert_eq!(name_usage.name, "Name");
        assert_eq!(name_usage.populated_count, 5);
        assert_eq!(name_usage.total_count, 10);
        assert!((name_usage.percentage - 50.0).abs() < f64::EPSILON);
    }

    async fn setup_mock_describe(mock_server: &MockServer) {
        // Construct fields separately to avoid recursion limit
        let id_field = json!({
            "name": "Id",
            "type": "id",
            "label": "Account ID",
            "aggregatable": true, "autoNumber": false, "byteLength": 18, "calculated": false,
            "cascadeDelete": false, "caseSensitive": false, "createable": false, "custom": false,
            "defaultedOnCreate": true, "dependentPicklist": false, "deprecatedAndHidden": false,
            "digits": 0, "displayLocationInDecimal": false, "encrypted": false, "externalId": false,
            "filterable": true, "groupable": true, "highScaleNumber": false, "htmlFormatted": false,
            "idLookup": true, "length": 18, "nameField": false, "namePointing": false, "nillable": false,
            "permissionable": false, "polymorphicForeignKey": false, "precision": 0, "queryByDistance": false,
            "referenceTo": [], "restrictedDelete": false, "restrictedPicklist": false, "scale": 0,
            "soapType": "tns:ID", "sortable": true, "unique": false, "updateable": false,
            "writeRequiresMasterRead": false
        });

        let name_field = json!({
            "name": "Name",
            "type": "string",
            "label": "Account Name",
            "aggregatable": true, "autoNumber": false, "byteLength": 18, "calculated": false,
            "cascadeDelete": false, "caseSensitive": false, "createable": false, "custom": false,
            "defaultedOnCreate": true, "dependentPicklist": false, "deprecatedAndHidden": false,
            "digits": 0, "displayLocationInDecimal": false, "encrypted": false, "externalId": false,
            "filterable": true, "groupable": true, "highScaleNumber": false, "htmlFormatted": false,
            "idLookup": true, "length": 18, "nameField": false, "namePointing": false, "nillable": false,
            "permissionable": false, "polymorphicForeignKey": false, "precision": 0, "queryByDistance": false,
            "referenceTo": [], "restrictedDelete": false, "restrictedPicklist": false, "scale": 0,
            "soapType": "xsd:string", "sortable": true, "unique": false, "updateable": false,
            "writeRequiresMasterRead": false
        });

        // Mock Describe
        let describe_json = json!({
            "name": "Account",
            "label": "Account",
            "custom": false,
            "queryable": true,
            "activateable": false,
            "createable": true,
            "customSetting": false,
            "deletable": true,
            "deprecatedAndHidden": false,
            "feedEnabled": true,
            "hasSubtypes": false,
            "isSubtype": false,
            "keyPrefix": "001",
            "labelPlural": "Accounts",
            "layoutable": true,
            "mergeable": true,
            "mruEnabled": true,
            "replicateable": true,
            "retrieveable": true,
            "searchable": true,
            "triggerable": true,
            "undeletable": true,
            "updateable": true,
            "urls": {},
            "childRelationships": [],
            "recordTypeInfos": [],
            "fields": [id_field, name_field]
        });

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/sobjects/Account/describe"))
            .respond_with(ResponseTemplate::new(200).set_body_json(describe_json))
            .mount(mock_server)
            .await;
    }

    async fn setup_mock_query(mock_server: &MockServer) {
        // Mock Query
        // SELECT COUNT(Id) total, COUNT(Id) f0, COUNT(Name) f1 FROM Account
        let query_json = json!({
            "totalSize": 1,
            "done": true,
            "records": [
                {
                    "attributes": {
                        "type": "AggregateResult",
                        "url": "/services/data/v60.0/sobjects/AggregateResult/row0"
                    },
                    "total": 10,
                    "f0": 10, // Id
                    "f1": 5   // Name
                }
            ]
        });

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/query"))
            .and(query_param(
                "q",
                "SELECT COUNT(Id) total, COUNT(Id) f0, COUNT(Name) f1 FROM Account",
            ))
            .respond_with(ResponseTemplate::new(200).set_body_json(query_json))
            .mount(mock_server)
            .await;
    }

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

        let id_field = json!({
            "name": "Id", "type": "id", "label": "Account ID", "aggregatable": true,
            "autoNumber": false, "byteLength": 18, "calculated": false, "cascadeDelete": false,
            "caseSensitive": false, "createable": false, "custom": false, "defaultedOnCreate": true,
            "dependentPicklist": false, "deprecatedAndHidden": false, "digits": 0,
            "displayLocationInDecimal": false, "encrypted": false, "externalId": false,
            "filterable": true, "groupable": true, "highScaleNumber": false, "htmlFormatted": false,
            "idLookup": true, "length": 18, "nameField": false, "namePointing": false,
            "nillable": false, "permissionable": false, "polymorphicForeignKey": false,
            "precision": 0, "queryByDistance": false, "referenceTo": [], "restrictedDelete": false,
            "restrictedPicklist": false, "scale": 0, "soapType": "tns:ID", "sortable": true,
            "unique": false, "updateable": false, "writeRequiresMasterRead": false
        });

        let address_field = json!({
            "name": "BillingAddress", "type": "address", "label": "Billing Address",
            "aggregatable": true, "autoNumber": false, "byteLength": 0, "calculated": false,
            "cascadeDelete": false, "caseSensitive": false, "createable": false, "custom": false,
            "defaultedOnCreate": false, "dependentPicklist": false, "deprecatedAndHidden": false,
            "digits": 0, "displayLocationInDecimal": false, "encrypted": false, "externalId": false,
            "filterable": false, "groupable": false, "highScaleNumber": false, "htmlFormatted": false,
            "idLookup": false, "length": 0, "nameField": false, "namePointing": false,
            "nillable": true, "permissionable": false, "polymorphicForeignKey": false,
            "precision": 0, "queryByDistance": false, "referenceTo": [], "restrictedDelete": false,
            "restrictedPicklist": false, "scale": 0, "soapType": "urn:address", "sortable": false,
            "unique": false, "updateable": false, "writeRequiresMasterRead": false
        });

        let describe_json = json!({
            "name": "Account", "label": "Account", "custom": false, "queryable": true,
            "activateable": false, "createable": true, "customSetting": false, "deletable": true,
            "deprecatedAndHidden": false, "feedEnabled": true, "hasSubtypes": false,
            "isSubtype": false, "keyPrefix": "001", "labelPlural": "Accounts", "layoutable": true,
            "mergeable": true, "mruEnabled": true, "replicateable": true, "retrieveable": true,
            "searchable": true, "triggerable": true, "undeletable": true, "updateable": true,
            "urls": {}, "childRelationships": [], "recordTypeInfos": [],
            "fields": [id_field, address_field]
        });

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/sobjects/Account/describe"))
            .respond_with(ResponseTemplate::new(200).set_body_json(describe_json))
            .mount(&mock_server)
            .await;

        let query_json = json!({
            "totalSize": 1, "done": true,
            "records": [{
                "attributes": {
                    "type": "AggregateResult",
                    "url": "/services/data/v60.0/sobjects/AggregateResult/row0"
                },
                "total": 10, "f0": 10
            }]
        });

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/query"))
            // Should ONLY query Id (f0), not BillingAddress
            .and(query_param(
                "q",
                "SELECT COUNT(Id) total, COUNT(Id) f0 FROM Account",
            ))
            .respond_with(ResponseTemplate::new(200).set_body_json(query_json))
            .mount(&mock_server)
            .await;

        let scanner = FieldUsageScanner::new(&client);
        let usage = scanner.scan("Account").await.must();

        assert_eq!(usage.len(), 1);
        assert_eq!(usage[0].name, "Id");
    }

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

        // Standard setup for describe (just Id)
        setup_mock_describe_simple(&mock_server).await;

        let query_json = json!({
            "totalSize": 1, "done": true,
            "records": [{
                "attributes": {
                    "type": "AggregateResult",
                    "url": "/services/data/v60.0/sobjects/AggregateResult/row0"
                },
                "total": 0, "f0": 0
            }]
        });

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/query"))
            .respond_with(ResponseTemplate::new(200).set_body_json(query_json))
            .mount(&mock_server)
            .await;

        let scanner = FieldUsageScanner::new(&client);
        let usage = scanner.scan("Account").await.must();

        assert_eq!(usage.len(), 1);
        assert_eq!(usage[0].populated_count, 0);
        assert_eq!(usage[0].total_count, 0);
        // Ensure no NaN
        assert!(!usage[0].percentage.is_nan());
        assert!(usage[0].percentage.abs() < f64::EPSILON);
    }

    async fn setup_mock_describe_simple(mock_server: &MockServer) {
        let id_field = json!({
            "name": "Id", "type": "id", "label": "Account ID", "aggregatable": true,
            "autoNumber": false, "byteLength": 18, "calculated": false, "cascadeDelete": false,
            "caseSensitive": false, "createable": false, "custom": false, "defaultedOnCreate": true,
            "dependentPicklist": false, "deprecatedAndHidden": false, "digits": 0,
            "displayLocationInDecimal": false, "encrypted": false, "externalId": false,
            "filterable": true, "groupable": true, "highScaleNumber": false, "htmlFormatted": false,
            "idLookup": true, "length": 18, "nameField": false, "namePointing": false,
            "nillable": false, "permissionable": false, "polymorphicForeignKey": false,
            "precision": 0, "queryByDistance": false, "referenceTo": [], "restrictedDelete": false,
            "restrictedPicklist": false, "scale": 0, "soapType": "tns:ID", "sortable": true,
            "unique": false, "updateable": false, "writeRequiresMasterRead": false
        });

        let describe_json = json!({
            "name": "Account", "label": "Account", "custom": false, "queryable": true,
            "activateable": false, "createable": true, "customSetting": false, "deletable": true,
            "deprecatedAndHidden": false, "feedEnabled": true, "hasSubtypes": false,
            "isSubtype": false, "keyPrefix": "001", "labelPlural": "Accounts", "layoutable": true,
            "mergeable": true, "mruEnabled": true, "replicateable": true, "retrieveable": true,
            "searchable": true, "triggerable": true, "undeletable": true, "updateable": true,
            "urls": {}, "childRelationships": [], "recordTypeInfos": [],
            "fields": [id_field]
        });

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/sobjects/Account/describe"))
            .respond_with(ResponseTemplate::new(200).set_body_json(describe_json))
            .mount(mock_server)
            .await;
    }

    struct QueryContains(&'static str);
    impl wiremock::Match for QueryContains {
        fn matches(&self, request: &wiremock::Request) -> bool {
            request
                .url
                .query_pairs()
                .any(|(k, v)| k == "q" && v.contains(self.0))
        }
    }

    struct QueryNotContains(&'static str);
    impl wiremock::Match for QueryNotContains {
        fn matches(&self, request: &wiremock::Request) -> bool {
            !request
                .url
                .query_pairs()
                .any(|(k, v)| k == "q" && v.contains(self.0))
        }
    }

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

        // Generate 25 fields
        let mut fields = Vec::with_capacity(25);
        for i in 0..25 {
            fields.push(json!({
                "name": format!("Field{}", i), "type": "string", "label": format!("Field {}", i),
                // (Other required fields omitted for brevity as they aren't checked by logic, but needed for parsing)
                "aggregatable": true, "autoNumber": false, "byteLength": 18, "calculated": false,
                "cascadeDelete": false, "caseSensitive": false, "createable": false, "custom": false,
                "defaultedOnCreate": true, "dependentPicklist": false, "deprecatedAndHidden": false,
                "digits": 0, "displayLocationInDecimal": false, "encrypted": false, "externalId": false,
                "filterable": true, "groupable": true, "highScaleNumber": false, "htmlFormatted": false,
                "idLookup": true, "length": 18, "nameField": false, "namePointing": false,
                "nillable": false, "permissionable": false, "polymorphicForeignKey": false,
                "precision": 0, "queryByDistance": false, "referenceTo": [], "restrictedDelete": false,
                "restrictedPicklist": false, "scale": 0, "soapType": "xsd:string", "sortable": true,
                "unique": false, "updateable": false, "writeRequiresMasterRead": false
            }));
        }

        let describe_json = json!({
            "name": "Account", "label": "Account", "custom": false, "queryable": true,
            "activateable": false, "createable": true, "customSetting": false, "deletable": true,
            "deprecatedAndHidden": false, "feedEnabled": true, "hasSubtypes": false,
            "isSubtype": false, "keyPrefix": "001", "labelPlural": "Accounts", "layoutable": true,
            "mergeable": true, "mruEnabled": true, "replicateable": true, "retrieveable": true,
            "searchable": true, "triggerable": true, "undeletable": true, "updateable": true,
            "urls": {}, "childRelationships": [], "recordTypeInfos": [],
            "fields": fields
        });

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/sobjects/Account/describe"))
            .respond_with(ResponseTemplate::new(200).set_body_json(describe_json))
            .mount(&mock_server)
            .await;

        // Expect 2 queries.
        // Batch 1: 20 fields (0-19)
        // Batch 2: 5 fields (20-24)

        // We match strictly on the query param to verify batching
        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/query"))
            .and(QueryContains("Field0"))
            .and(QueryContains("Field19"))
            .and(QueryNotContains("Field20")) // Should NOT contain Field20
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "totalSize": 1, "done": true,
                "records": [{
                    "attributes": { "type": "AggregateResult", "url": "..." },
                    "total": 10, "f0": 1, "f19": 1
                }]
            })))
            .mount(&mock_server)
            .await;

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/query"))
            .and(QueryContains("Field20"))
            .and(QueryContains("Field24"))
            .and(QueryNotContains("Field0")) // Should NOT contain Field0
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "totalSize": 1, "done": true,
                "records": [{
                    "attributes": { "type": "AggregateResult", "url": "..." },
                    "total": 10, "f0": 1, "f4": 1 // f0-f4 map to Field20-Field24 in this batch
                }]
            })))
            .mount(&mock_server)
            .await;

        let scanner = FieldUsageScanner::new(&client);
        let usage = scanner.scan("Account").await.must();

        assert_eq!(usage.len(), 25);
    }

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

        // 1. Describe failure
        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/sobjects/Account/describe"))
            .respond_with(ResponseTemplate::new(404)) // Not Found
            .mount(&mock_server)
            .await;

        let scanner = FieldUsageScanner::new(&client);
        let result = scanner.scan("Account").await;
        let Err(err) = result else {
            panic!("Expected an error");
        };
        assert!(err.to_string().contains(""));

        mock_server.reset().await;

        // 2. Query failure
        setup_mock_describe_simple(&mock_server).await;

        Mock::given(method("GET"))
            .and(path("/services/data/v60.0/query"))
            .respond_with(ResponseTemplate::new(400).set_body_string("Bad Query"))
            .mount(&mock_server)
            .await;

        let result = scanner.scan("Account").await;
        let Err(err) = result else {
            panic!("Expected an error");
        };
        assert!(err.to_string().contains(""));
    }
}