cpa-exec 0.4.3

Part of cliproxy: upstream provider executors
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
use super::*;
use axum::extract::{Request, State};
use axum::response::IntoResponse;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[derive(Default)]
struct Mock {
    calls: Mutex<Vec<(String, http::HeaderMap, Vec<u8>)>>,
    profile_status: u16,
    token_status: u16,
    /// Statuses for the next token calls, before `token_status` applies. 1 answers 200
    /// with a body that is not JSON.
    token_statuses: Mutex<std::collections::VecDeque<u16>>,
    omit_refresh: bool,
    started: tokio::sync::Notify,
    release: tokio::sync::Notify,
    hold: bool,
    token_calls: AtomicUsize,
}

async fn handler(State(mock): State<Arc<Mock>>, request: Request) -> axum::response::Response {
    let (parts, body) = request.into_parts();
    let body = axum::body::to_bytes(body, 8192).await.unwrap();
    mock.calls
        .lock()
        .unwrap()
        .push((parts.uri.path().into(), parts.headers, body.to_vec()));
    if parts.uri.path() == "/token" {
        mock.token_calls.fetch_add(1, Ordering::SeqCst);
        mock.started.notify_one();
        if mock.hold {
            mock.release.notified().await;
        }
        let status = mock
            .token_statuses
            .lock()
            .unwrap()
            .pop_front()
            .unwrap_or(mock.token_status);
        if status == 1 {
            return "not json".into_response();
        }
        if status != 0 {
            return (
                http::StatusCode::from_u16(status).unwrap(),
                [("retry-after", "2")],
                "fake-refresh-echo",
            )
                .into_response();
        }
        let mut response = json!({"access_token":"sk-ant-oat-new-fake", "expires_in":36000,
            "account":{"uuid":"token-account", "email_address":"token@example.test"},
            "organization":{"uuid":"token-org", "name":"Token Organization"}});
        if !mock.omit_refresh {
            response["refresh_token"] = "fake-rotated".into();
        }
        axum::Json(response).into_response()
    } else if parts.uri.path() == "/profile" {
        if mock.profile_status != 0 {
            return (
                http::StatusCode::from_u16(mock.profile_status).unwrap(),
                "fake-access-echo",
            )
                .into_response();
        }
        axum::Json(
            json!({"account":{"uuid":"profile-account", "email":"profile@example.test"},
            "organization":{"uuid":"profile-org", "name":"Profile Organization"}}),
        )
        .into_response()
    } else {
        axum::Json(json!({})).into_response()
    }
}

async fn service(mock: Arc<Mock>) -> OAuth {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let base = format!("http://{}", listener.local_addr().unwrap());
    let router = axum::Router::new().fallback(handler).with_state(mock);
    tokio::spawn(async move { axum::serve(listener, router).await.unwrap() });
    OAuth::with_endpoints(
        wreq::Client::builder()
            .http1_only()
            .redirect(wreq::redirect::Policy::none())
            .build()
            .unwrap(),
        &format!("{base}/token"),
        &format!("{base}/profile"),
        &format!("{base}/roles"),
    )
}

fn credential() -> Credential {
    Credential::from_file(Path::new("/fake"), Path::new("/fake/claude.json"), json!({
        "type":"claude", "access_token":"sk-ant-oat-old-fake", "refresh_token":"fake-refresh",
        "email":"old@example.test", "account_uuid":"old-account", "organization_uuid":"old-org",
        "expired":"2000-01-01T00:00:00Z", "claude_device_ids":["invalid", format!(" {} ", "A".repeat(64)), "b".repeat(64)],
        "unknown":{"preserve":true}, "id_token":"fake-id"
    }).as_object().unwrap().clone()).unwrap()
}

#[test]
fn refresh_lead_and_legacy_refresh_spelling() {
    let now = DateTime::parse_from_rfc3339("2026-10-02T12:00:00Z")
        .unwrap()
        .with_timezone(&Utc);
    let mut credential = credential();
    credential
        .metadata
        .insert("expired".into(), "2026-10-02T16:00:01Z".into());
    assert!(!refresh_due(&credential, now));
    credential
        .metadata
        .insert("expired".into(), "2026-10-02T16:00:00Z".into());
    assert!(refresh_due(&credential, now));
    credential.metadata.insert("refresh_token".into(), "".into());
    assert!(!refresh_due(&credential, now));
    credential.metadata.insert("refreshToken".into(), "fake-legacy".into());
    assert!(refresh_due(&credential, now));
}

#[tokio::test]
async fn refresh_rotation_preserves_identity_on_optional_profile_failure() {
    let mock = Arc::new(Mock {
        profile_status: 503,
        ..Default::default()
    });
    let oauth = service(mock.clone()).await;
    let mut credential = credential();
    // Identity preparation never refreshes, even inside the refresh lead.
    assert!(refresh_due(&credential, Utc::now()));
    let prepared = oauth.prepare(&credential, &crate::proxy::Proxy::Inherit).await.unwrap();
    assert_eq!(prepared.set.keys().collect::<Vec<_>>(), ["claude_device_ids"]);
    prepared.apply(&mut credential.metadata);
    let patch = oauth
        .refresh_credential(&credential, &crate::proxy::Proxy::Inherit)
        .await
        .unwrap();
    patch.apply(&mut credential.metadata);
    assert!(!refresh_due(&credential, Utc::now()));
    assert_eq!(credential.str("access_token"), Some("sk-ant-oat-new-fake"));
    assert_eq!(credential.str("refresh_token"), Some("fake-rotated"));
    assert_eq!(credential.str("account_uuid"), Some("old-account"));
    assert_eq!(credential.str("email"), Some("old@example.test"));
    assert_eq!(credential.str("id_token"), Some("fake-id"));
    assert_eq!(credential.metadata["unknown"], json!({"preserve":true}));
    assert_eq!(credential.metadata["claude_device_ids"], json!(["a".repeat(64)]));
    assert!(!needs_prepare(&credential, chrono::Utc::now()));
    let calls = mock.calls.lock().unwrap();
    assert_eq!(calls.len(), 2);
    assert_eq!(calls[0].2, format!(r#"{{"client_id":"{CLIENT_ID}","grant_type":"refresh_token","refresh_token":"fake-refresh","scope":"{SCOPE}"}}"#).as_bytes());
    assert_eq!(calls[1].1["authorization"], "Bearer sk-ant-oat-new-fake");
}

#[tokio::test]
async fn missing_rotated_refresh_and_profile_fields_keep_saved_values() {
    let mock = Arc::new(Mock {
        omit_refresh: true,
        ..Default::default()
    });
    let oauth = service(mock).await;
    let patch = oauth.refresh("fake-refresh").await.unwrap();
    assert_eq!(patch.set["refresh_token"], "fake-refresh");
    assert_eq!(patch.set["account_uuid"], "profile-account");
    assert_eq!(patch.set["email"], "profile@example.test");
}

#[tokio::test]
async fn token_key_singleflight_survives_canceled_waiter() {
    let mock = Arc::new(Mock {
        hold: true,
        ..Default::default()
    });
    let oauth = service(mock.clone()).await;
    let first = oauth.clone();
    let waiter = tokio::spawn(async move { first.refresh("fake-refresh").await });
    mock.started.notified().await;
    waiter.abort();
    assert!(waiter.await.unwrap_err().is_cancelled());
    let second = oauth.clone();
    let waiter = tokio::spawn(async move { second.refresh("fake-refresh").await });
    mock.release.notify_one();
    let patch = waiter.await.unwrap().unwrap();
    assert_eq!(patch.set["refresh_token"], "fake-rotated");
    assert_eq!(mock.token_calls.load(Ordering::SeqCst), 1);
    // A finished exchange is not reused (Go singleflight): a forced refresh after a 401
    // exchanges again.
    mock.release.notify_one();
    oauth.refresh("fake-refresh").await.unwrap();
    assert_eq!(mock.token_calls.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn a_late_joiner_keeps_its_own_retry_budget() {
    // Go shares one exchange per token, not a caller's retry loop: B joins A's third
    // (failing) attempt and still retries on its own budget.
    let mock = Arc::new(Mock {
        hold: true,
        token_statuses: Mutex::new([503, 503, 503].into()),
        ..Default::default()
    });
    let oauth = service(mock.clone()).await;
    let refresh = |oauth: &OAuth| {
        let oauth = oauth.clone();
        tokio::spawn(async move { oauth.refresh("fake-refresh-late").await })
    };
    let a = refresh(&oauth);
    for _ in 0..2 {
        mock.started.notified().await;
        mock.release.notify_one();
    }
    mock.started.notified().await;
    let b = refresh(&oauth);
    tokio::time::sleep(Duration::from_millis(100)).await;
    mock.release.notify_one();
    assert_eq!(a.await.unwrap().unwrap_err().status, 503);
    tokio::time::timeout(Duration::from_secs(5), mock.started.notified())
        .await
        .expect("the late joiner retries on its own budget");
    mock.release.notify_one();
    let patch = b.await.unwrap().unwrap();
    assert_eq!(patch.set["refresh_token"], "fake-rotated");
    assert_eq!(mock.token_calls.load(Ordering::SeqCst), 4);
}

/// Go c163bae4 (isClaudeRefreshRetryable): a 5xx answer is retried, but a response
/// that cannot be read is not. Anthropic may already have spent the single-use refresh
/// token, so a second exchange would turn the fault into invalid_grant.
#[tokio::test]
async fn an_unreadable_refresh_answer_is_not_retried() {
    let mock = Arc::new(Mock {
        token_statuses: Mutex::new([503, 1].into()),
        ..Default::default()
    });
    let oauth = service(mock.clone()).await;
    let error = oauth.refresh("fake-refresh-unreadable").await.unwrap_err();
    assert_eq!(error.status, 502);
    assert_eq!(
        mock.token_calls.load(Ordering::SeqCst),
        2,
        "the 503 is retried once, then no more"
    );
    // A connection dropped before any answer (DoesNotReplayAfterTransportError).
    let error = refresh_against_raw(b"").await;
    assert_eq!(error.scope, FailureScope::Transport);
    // A 200 whose body ends before its Content-Length, then the connection closes.
    refresh_against_raw(
        b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 100\r\n\r\n{\"access_token\"",
    )
    .await;
    // A 200 whose gzip body is not gzip.
    refresh_against_raw(
        b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Encoding: gzip\r\nContent-Length: 8\r\nConnection: close\r\n\r\nnot gzip",
    )
    .await;
}

/// Refreshes against a server that answers every connection with `raw` and closes it,
/// and asserts that exactly one token request was made.
async fn refresh_against_raw(raw: &'static [u8]) -> ExecError {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let url = format!("http://{}/token", listener.local_addr().unwrap());
    let accepted = Arc::new(AtomicUsize::new(0));
    tokio::spawn({
        let accepted = accepted.clone();
        async move {
            while let Ok((mut socket, _)) = listener.accept().await {
                accepted.fetch_add(1, Ordering::SeqCst);
                if !raw.is_empty() {
                    let mut request = [0u8; 4096];
                    let _ = socket.read(&mut request).await;
                    let _ = socket.write_all(raw).await;
                }
                drop(socket);
            }
        }
    });
    let oauth = OAuth::with_endpoints(wreq::Client::new(), &url, &url, &url);
    let error = oauth.refresh("fake-refresh-raw").await.unwrap_err();
    assert_eq!(accepted.load(Ordering::SeqCst), 1, "retried after {error:?}");
    error
}

/// A refused refresh keeps the token endpoint's OAuth error code when it is a known
/// one (RFC 6749 or an Anthropic error type) and nothing else of the body.
#[tokio::test]
async fn a_refused_refresh_keeps_only_a_known_error_code() {
    async fn refused(body: &'static str) -> String {
        let raw = format!(
            "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
            body.len()
        );
        let error = refresh_against_raw(Box::leak(raw.into_bytes().into_boxed_slice())).await;
        assert_eq!(error.status, 400);
        String::from_utf8(error.body.to_vec()).unwrap()
    }
    assert_eq!(
        refused(r#"{"error":"invalid_grant","error_description":"token fake-refresh-raw revoked"}"#).await,
        "Claude OAuth acquisition failed: invalid_grant"
    );
    assert_eq!(
        refused(r#"{"type":"error","error":{"type":"authentication_error","message":"fake-refresh-raw"}}"#).await,
        "Claude OAuth acquisition failed: authentication_error"
    );
    // A lower-case value that is not a known code could be a secret.
    assert_eq!(
        refused(r#"{"error":"opaquelowercasesecret"}"#).await,
        "Claude OAuth acquisition failed"
    );
}

#[tokio::test]
async fn a_canceled_caller_stops_retrying() {
    let mock = Arc::new(Mock {
        token_statuses: Mutex::new([503].into()),
        ..Default::default()
    });
    let oauth = service(mock.clone()).await;
    let caller = {
        let oauth = oauth.clone();
        tokio::spawn(async move { oauth.refresh("fake-refresh-cancel").await })
    };
    mock.started.notified().await;
    // The first exchange has failed; the caller is in its one-second backoff.
    tokio::time::sleep(Duration::from_millis(300)).await;
    caller.abort();
    tokio::time::sleep(Duration::from_millis(1500)).await;
    assert_eq!(mock.token_calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn refresh_429_backoff_and_error_redaction() {
    let mock = Arc::new(Mock {
        token_status: 429,
        ..Default::default()
    });
    let oauth = service(mock.clone()).await;
    let error = oauth.refresh("fake-refresh-429").await.unwrap_err();
    assert_eq!(error.status, 429);
    assert_eq!(error.retry_after, Some(Duration::from_secs(5)));
    assert!(!error.to_string().contains("fake-refresh"));
    assert!(error.headers.is_empty());
    // Blocked until Retry-After: no second exchange.
    let error = oauth.refresh("fake-refresh-429").await.unwrap_err();
    assert_eq!(error.status, 429);
    assert!(
        error
            .retry_after
            .is_some_and(|d| d <= Duration::from_secs(5) && !d.is_zero())
    );
    assert_eq!(mock.token_calls.load(Ordering::SeqCst), 1);
    let mut headers = http::HeaderMap::new();
    headers.insert("retry-after-ms", "9000".parse().unwrap());
    assert_eq!(refresh_backoff(&headers), Duration::from_secs(9));
    headers.insert("retry-after", "9999".parse().unwrap());
    assert_eq!(refresh_backoff(&headers), Duration::from_secs(300));
}

#[tokio::test]
async fn code_exchange_login_layout_and_atomic_permissions() {
    let mock: Arc<Mock> = Arc::default();
    let oauth = service(mock.clone()).await;
    let patch = oauth
        .exchange("fake-code#state-fragment", "state-argument", "fake-verifier")
        .await
        .unwrap();
    let calls = mock.calls.lock().unwrap();
    assert_eq!(
        calls.iter().map(|c| c.0.as_str()).collect::<Vec<_>>(),
        ["/token", "/profile", "/roles"]
    );
    assert_eq!(calls[0].2, format!(r#"{{"grant_type":"authorization_code","code":"fake-code","redirect_uri":"{REDIRECT_URI}","client_id":"{CLIENT_ID}","code_verifier":"fake-verifier","state":"state-fragment"}}"#).as_bytes());
    drop(calls);
    for field in [
        "id_token",
        "access_token",
        "refresh_token",
        "last_refresh",
        "email",
        "type",
        "expired",
    ] {
        assert!(patch.set[field].is_string(), "{field} must always serialize");
    }
    assert!(canonical_pool(patch.set.get("claude_device_ids")));
}

#[test]
fn pkce_and_authorize_url() {
    let (verifier, challenge) = pkce().unwrap();
    assert_eq!(verifier.len(), 128);
    assert_eq!(challenge.len(), 43);
    let url = Url::parse(&authorize_url("good-state", &challenge)).unwrap();
    assert!(url.query_pairs().any(|(key, value)| key == "scope" && value == SCOPE));
}

#[tokio::test]
async fn raw_oauth_token_and_inspection_header_order_and_case() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let base = format!("http://{}", listener.local_addr().unwrap());
    let capture = tokio::spawn(async move {
        let mut captures = Vec::new();
        for index in 0..3 {
            let (mut socket, _) = listener.accept().await.unwrap();
            let mut bytes = Vec::new();
            while !bytes.ends_with(b"\r\n\r\n") {
                let mut byte = [0];
                socket.read_exact(&mut byte).await.unwrap();
                bytes.push(byte[0]);
            }
            let headers = String::from_utf8(bytes).unwrap();
            let length = headers
                .lines()
                .find_map(|line| line.strip_prefix("Content-Length: "))
                .map(|s| s.parse::<usize>().unwrap())
                .unwrap_or(0);
            let mut body = vec![0; length];
            socket.read_exact(&mut body).await.unwrap();
            captures.push((headers, body));
            let body = if index == 0 {
                r#"{"access_token":"fake-access","refresh_token":"fake-refresh","expires_in":36000}"#
            } else {
                "{}"
            };
            socket.write_all(format!("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).as_bytes()).await.unwrap();
        }
        captures
    });
    let oauth = OAuth::with_endpoints(
        wreq::Client::builder().http1_only().build().unwrap(),
        &format!("{base}/token"),
        &format!("{base}/profile"),
        &format!("{base}/roles"),
    );
    oauth
        .exchange("fake-code", "fake-state", "fake-verifier")
        .await
        .unwrap();
    let captures = capture.await.unwrap();
    let names = |raw: &str| {
        raw.lines()
            .skip(1)
            .filter_map(|line| line.split_once(':').map(|(name, _)| name.to_owned()))
            .collect::<Vec<_>>()
    };
    assert_eq!(
        names(&captures[0].0),
        [
            "Accept",
            "Content-Type",
            "User-Agent",
            "Content-Length",
            "Accept-Encoding",
            "Host",
            "Connection"
        ]
    );
    for (headers, body) in &captures[1..] {
        assert_eq!(
            names(headers),
            [
                "Accept",
                "Content-Type",
                "Authorization",
                "Cache-Control",
                "User-Agent",
                "Accept-Encoding",
                "Host",
                "Connection"
            ]
        );
        assert!(headers.contains("Authorization: Bearer fake-access\r\n"));
        assert!(body.is_empty());
    }
}

#[tokio::test]
async fn many_credentials_refresh_without_an_admission_limit() {
    let mock = Arc::new(Mock::default());
    let oauth = service(mock.clone()).await;
    for i in 0..65 {
        oauth.refresh(&format!("fake-refresh-many-{i}")).await.unwrap();
    }
    assert_eq!(mock.token_calls.load(Ordering::SeqCst), 65);
}

#[tokio::test]
async fn prepare_forces_a_refresh_outside_the_lead_window() {
    // The runtime's refresh-and-retry after a 401 (Go tryRefreshAfterUnauthorized) calls
    // prepare on an identified credential whose token has not expired yet.
    let mock = Arc::new(Mock::default());
    let oauth = service(mock.clone()).await;
    let mut credential = credential();
    credential
        .metadata
        .insert("expired".into(), "2099-01-01T00:00:00Z".into());
    credential
        .metadata
        .insert("refresh_token".into(), "fake-refresh-forced".into());
    credential
        .metadata
        .insert("claude_device_ids".into(), json!(["a".repeat(64)]));
    assert!(
        !needs_prepare(&credential, chrono::Utc::now()),
        "not due and identified"
    );
    let patch = oauth.prepare(&credential, &crate::proxy::Proxy::Inherit).await.unwrap();
    assert_eq!(patch.set["access_token"], "sk-ant-oat-new-fake");
    assert_eq!(mock.token_calls.load(Ordering::SeqCst), 1);
}

/// Generated device IDs are random: like the Go golden, any 64-hex run that is not
/// a fixture ID (one repeated character) reads as `<device>`.
fn normalize_devices(text: &str) -> String {
    let bytes = text.as_bytes();
    let mut out = String::new();
    let mut i = 0;
    while i < bytes.len() {
        let run = bytes[i..]
            .iter()
            .take_while(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(b))
            .count();
        if run >= 64 {
            let device = &text[i..i + 64];
            if device.bytes().all(|b| b == bytes[i]) {
                out.push_str(device);
            } else {
                out.push_str("<device>");
            }
            i += 64;
        } else if run > 0 {
            out.push_str(&text[i..i + run]);
            i += run;
        } else {
            let c = text[i..].chars().next().unwrap();
            out.push(c);
            i += c.len_utf8();
        }
    }
    out
}

/// Go `EnsureClaudeCredentialDevicePoolRequired` in Home mode, recorded by the
/// reference's zz_rustgolden_test.go: the KV calls (key from Home's auth index or the
/// credential's own), NX/XX writes, the pool, and the errors.
#[tokio::test]
async fn home_device_pools_match_go() {
    let golden: Value = serde_json::from_str(include_str!("claude/testdata/go_claude_device_pool_home.json")).unwrap();
    for case in golden["cases"].as_array().unwrap() {
        let scenario = &case["scenario"];
        let name = scenario["name"].as_str().unwrap();
        let mut attributes = std::collections::BTreeMap::new();
        let index = scenario["index"].as_str().unwrap();
        if !index.is_empty() {
            attributes.insert(
                cpa_core::config::credentials::HOME_AUTH_INDEX.to_owned(),
                index.to_owned(),
            );
        }
        let credential = Credential {
            id: scenario["id"].as_str().unwrap().into(),
            provider: "claude".into(),
            source: cpa_core::credential::Source::Config {
                section: "home".into(),
                index: 0,
            },
            disabled: false,
            label: String::new(),
            attributes,
            metadata: scenario["metadata"].as_object().cloned().unwrap_or_default(),
            revision: 0,
            next_refresh_after: None,
        };
        let raw_pool = credential.metadata.get("claude_device_ids");
        if canonical_pool(raw_pool) {
            // Go returns the canonical pool before consulting Home.
            assert_eq!(case["calls"], json!([]), "{name}");
            assert_eq!(raw_pool.unwrap(), &case["pool"], "{name}");
            continue;
        }
        let values = Arc::new(Mutex::new(std::collections::HashMap::new()));
        if let Some(preset) = scenario["preset"].as_object() {
            for (k, v) in preset {
                values.lock().unwrap().insert(k.clone(), v.as_str().unwrap().to_owned());
            }
        }
        let home = cpa_home::fake::FakeHome::start(crate::claude::kv_test::kv_home(values.clone())).await;
        let client = home.client();
        let candidate = normalize_pool(
            raw_pool
                .and_then(Value::as_array)
                .into_iter()
                .flatten()
                .filter_map(Value::as_str),
        );
        let got = home_device_pool(&client, &credential, candidate).await;
        let calls: Vec<Value> = home
            .commands()
            .iter()
            .filter_map(|c| crate::claude::kv_test::as_go_call(c))
            .map(|mut call| {
                if call[0] == "set" {
                    call[2] = normalize_devices(call[2].as_str().unwrap()).into();
                }
                call
            })
            .collect();
        assert_eq!(&calls, case["calls"].as_array().unwrap(), "{name}: calls");
        match got {
            Ok(device) => assert_eq!(json!([normalize_devices(&device)]), case["pool"], "{name}: pool"),
            Err(error) => {
                let text = String::from_utf8_lossy(&error.body).into_owned();
                let want = case["error"].as_str().unwrap();
                // Go's JSON decoder words its errors differently from serde.
                match want.split_once("decode Home KV value: ") {
                    Some((prefix, _)) => assert!(
                        text.starts_with(&format!("{prefix}decode Home KV value: ")),
                        "{name}: {text}"
                    ),
                    None => assert_eq!(text, want, "{name}: error"),
                }
            }
        }
    }
}