use std::sync::OnceLock;
use serde_json::Value;
use super::go_exec::Verdict;
use super::{alias, stream};
fn fixture() -> &'static Value {
static FIXTURE: OnceLock<Value> = OnceLock::new();
FIXTURE.get_or_init(|| serde_json::from_str(include_str!("testdata/go_unit.json")).unwrap())
}
fn fixture_v8_0_27() -> &'static Value {
static FIXTURE: OnceLock<Value> = OnceLock::new();
FIXTURE.get_or_init(|| serde_json::from_str(include_str!("testdata/go_unit_v8.0.27.json")).unwrap())
}
fn fixture_v8_0_35() -> &'static Value {
static FIXTURE: OnceLock<Value> = OnceLock::new();
FIXTURE.get_or_init(|| serde_json::from_str(include_str!("testdata/go_unit_v8.0.35.json")).unwrap())
}
fn fixture_tool_search() -> &'static Value {
static FIXTURE: OnceLock<Value> = OnceLock::new();
FIXTURE.get_or_init(|| serde_json::from_str(include_str!("testdata/go_unit_tool_search.json")).unwrap())
}
const SUPERSEDED: &[&str] = &[
"TestClaudeExecutor_DisabledThinkingStripsDisplayBeta",
"TestClaudeExecutorPayloadOverrideDisabledThinking",
"TestClaudeExecutorPayloadOverrideReenablesThinking",
"TestClaudeExecutor_ChatCompletionsDefaultMaxTokensAndPayloadDefaultRule",
"TestClaudeExecutor_PayloadCustomReportingOutcomesPreservedOnRewrite",
"TestClaudeExecutor_PayloadFilterFallbacksPreservesRemovalOnFable",
"TestClaudeExecutor_PayloadOverrideParentThinkingPreventsDisplayUpdates",
"TestClaudeExecutor_PayloadOverridePreservesExplicitFallbacks",
"TestClaudeExecutor_PayloadOverrideProbeWithExplicitDiagnosticsPreserved",
"TestClaudeExecutor_PayloadOverrideRawPreservesExplicitFallbacks",
"TestClaudeExecutor_PayloadStringSystemWithExactReportingPromptDoesNotDuplicate",
"TestClaudeExecutor_PayloadOverrideCannotSmuggleLegacyMidSystemMessage",
"TestClaudeExecutor_PayloadOverrideDoesNotClaimMatchingCallerTurn",
];
const REMOVED_IN_868E137: &[&str] = &[
"TestClaudeExecutor_ExecuteTransportMatchesResponseFormat",
"TestClaudeExecutor_PayloadFableThinkingAdaptiveToDisabledDropsInjectedDisplay",
"TestClaudeExecutor_PayloadFableToSonnetWithSensitiveWordsRemovesReportingBlock",
"TestClaudeExecutor_PayloadOverrideFableModelReconciled",
"TestClaudeExecutor_PayloadOverrideFableToProbeStripsFableAdditions",
"TestClaudeExecutor_PayloadOverrideMaxTokensTo1ReclassifiesAsProbe",
"TestClaudeExecutor_PayloadOverrideNonFableToFableReconciled",
"TestClaudeExecutor_PayloadOverrideProbeToNormalReinitializes",
"TestClaudeExecutor_PayloadOverrideUnrelatedModelRuleDoesNotPreserveFableFallbacks",
"TestClaudeExecutor_PayloadReplacesSystemOnOriginalFableReaddsReportingBlock",
"TestClaudeExecutor_PayloadSonnetToFableWithSensitiveWordsObfuscatesInjectedReportingBlock",
"TestClaudeExecutor_PayloadSonnetToFableWithSystemTTLEditsAddsReportingBlock",
"TestClaudeExecutor_PayloadStringSystemFableAddsReportingBlock",
"TestClaudeExecutor_PayloadStringSystemMentioningReportingOutcomesStillInjectsFableReportingBlock",
"TestClaudeExecutor_PayloadSystemTTLOverrideStillRemovesInjectedFableReportingBlock",
"TestClaudeExecutor_PayloadOverrideReconcilesRelocatedSystemPrompt",
];
fn bytes(value: &Value) -> Option<Vec<u8>> {
use base64::Engine;
match value {
Value::String(s) => Some(s.as_bytes().to_vec()),
Value::Object(o) => o
.get("b64")
.and_then(Value::as_str)
.map(|b| base64::engine::general_purpose::STANDARD.decode(b).unwrap()),
_ => None,
}
}
fn text(value: &Value) -> String {
String::from_utf8(bytes(value).unwrap_or_default()).unwrap()
}
fn reverse(value: &Value) -> alias::Reverse {
value
.as_object()
.into_iter()
.flatten()
.map(|(k, v)| (k.clone(), v.as_str().unwrap().to_owned()))
.collect()
}
async fn replay(test: &str, record: &Value) -> Result<Verdict, String> {
let field = |name: &str| &record[name];
let checked = match field("fn").as_str().unwrap() {
"execute" | "execute_stream" | "count_tokens" | "count_tokens_upstream" => {
return super::go_exec::replay(test, record).await;
}
"remap" => {
let (out, map) = alias::remap(&text(field("body")), field("secret").as_str().unwrap());
check("body", &out, &text(field("out")))?;
check("reverse map", &map, &reverse(field("reverse")))
}
"remap_batched" => {
let body = text(field("body"));
if !field("ok").as_bool().unwrap() {
return check("valid JSON", &cpa_common::json::valid(body.as_bytes()), &false)
.map(|()| Verdict::Matched);
}
let (out, map) = alias::remap(&body, field("secret").as_str().unwrap());
check("body", &out, &text(field("out")))?;
check("reverse map", &map, &reverse(field("reverse")))
}
"restore" => {
let got = alias::restore_response(&text(field("body")), &reverse(field("reverse")));
match field("error").as_str() {
Some(error) => check("error", &got.err(), &Some(error.to_owned())),
None => check("body", &got?, &text(field("out"))),
}
}
"restore_line" => {
let got = stream::restore_line(&bytes(field("line")).unwrap(), &reverse(field("reverse")));
match field("error").as_str() {
Some(error) => check("error", &got.err(), &Some(error.to_owned())),
None => check("line", &String::from_utf8(got?).unwrap(), &text(field("out"))),
}
}
"parse_alias" => {
let name = field("name").as_str().unwrap();
let want = field("ok").as_bool().unwrap().then(|| {
(
field("server").as_str().unwrap(),
field("tool_id").as_str().unwrap(),
field("semantic").as_str().unwrap(),
)
});
check("parts", &alias::alias_parts(name), &want)
}
other => Err(format!("no Rust replay for recorded function {other:?}")),
};
checked.map(|()| Verdict::Matched)
}
fn check<T: PartialEq + std::fmt::Debug>(what: &str, got: &T, want: &T) -> Result<(), String> {
if got == want {
Ok(())
} else {
Err(format!("{what}:\n rust: {got:?}\n go: {want:?}"))
}
}
async fn replay_file(fixture: &Value, file: &str) {
let tests = fixture["files"][file]
.as_object()
.unwrap_or_else(|| panic!("no records for {file}"));
let mut failures = Vec::new();
let mut count = 0;
for (test, records) in tests {
if std::ptr::eq(fixture, self::fixture()) && SUPERSEDED.contains(&test.as_str()) {
assert!(
fixture_v8_0_35()["files"][file][test]
.as_array()
.is_some_and(|calls| !calls.is_empty()),
"{test}: superseded but no v8.0.35 call recorded"
);
continue;
}
if std::ptr::eq(fixture, self::fixture()) && REMOVED_IN_868E137.contains(&test.as_str()) {
continue;
}
for (i, record) in records.as_array().unwrap().iter().enumerate() {
count += 1;
match replay(test, record).await {
Ok(Verdict::Matched) => {}
Ok(Verdict::Unverified(why)) => {
eprintln!("UNVERIFIED {file}: {test} call {i}: {why}");
assert!(
UNVERIFIED.contains(&test.as_str()),
"{test}: unverified but not listed in UNVERIFIED"
);
}
Err(why) => failures.push(format!("{test} call {i} ({}): {why}", record["fn"])),
}
}
}
assert!(count > 0, "{file}: nothing recorded");
assert!(
failures.is_empty(),
"{file}: {} of {count} Go calls differ\n{}",
failures.len(),
failures.join("\n")
);
}
const UNVERIFIED: &[&str] = &[
"TestClaudeExecutor_ExecuteStreamOAuthCancellationIsRequestScoped",
"TestClaudeExecutor_ExecuteStreamOAuthStartupCancellationIsRequestScoped",
];
#[test]
#[ignore = "unverified: Go cancels the request context mid-call (see UNVERIFIED)"]
fn unverified_go_cancellation_cases() {
for test in UNVERIFIED {
assert!(fixture()["files"]["claude_executor_test.go"][test].is_array(), "{test}");
}
}
#[test]
fn go_assertions_held_while_recording() {
assert_eq!(fixture()["go_failures"], serde_json::json!([]));
assert_eq!(fixture_v8_0_27()["go_failures"], serde_json::json!([]));
assert_eq!(fixture_v8_0_35()["go_failures"], serde_json::json!([]));
assert_eq!(fixture_tool_search()["go_failures"], serde_json::json!([]));
}
macro_rules! go_file {
($(#[$doc:meta])* $name:ident, $file:literal) => {
$(#[$doc])*
#[tokio::test]
async fn $name() {
replay_file(fixture(), $file).await;
}
};
}
go_file!(
m1_0072_cloaked_cache_repro, "claude_cloaked_cache_repro_test.go"
);
go_file!(
m1_0074_executor_auth, "claude_executor_auth_test.go"
);
go_file!(
m1_0076_beta_policy, "claude_executor_beta_policy_test.go"
);
go_file!(
m1_0078_diagnostics, "claude_executor_diagnostics_test.go"
);
go_file!(
m1_0079_fable_ratelimit, "claude_executor_fable_ratelimit_test.go"
);
go_file!(
m1_0080_fast_error, "claude_executor_fast_error_test.go"
);
go_file!(
m1_0081_native_helper, "claude_executor_native_helper_test.go"
);
go_file!(
m1_0082_ratelimit, "claude_executor_ratelimit_test.go"
);
go_file!(
m1_0083_request_remap, "claude_executor_request_remap_test.go"
);
go_file!(
m1_0084_stream_terminal, "claude_executor_stream_terminal_test.go"
);
go_file!(
m1_0085_subagent_ttl, "claude_executor_subagent_ttl_regression_test.go"
);
go_file!(
m1_0086_executor, "claude_executor_test.go"
);
go_file!(
m1_0087_thinking_signature, "claude_executor_thinking_signature_test.go"
);
go_file!(
m1_0089_fingerprint_policy, "claude_fingerprint_policy_test.go"
);
go_file!(
m1_0093_mid_system_model, "claude_mid_system_model_test.go"
);
go_file!(
m1_0095_thinking_replay, "claude_thinking_replay_test.go"
);
go_file!(
apply_patch_integration_claude, "apply_patch_integration_test.go"
);
macro_rules! go_file_v8_0_27 {
($(#[$doc:meta])* $name:ident, $file:literal) => {
$(#[$doc])*
#[tokio::test]
async fn $name() {
replay_file(fixture_v8_0_27(), $file).await;
}
};
}
go_file_v8_0_27!(
v8_0_27_prompt_cache_options,
"claude_prompt_cache_options_test.go"
);
go_file_v8_0_27!(
v8_0_27_cloaked_date_pin,
"claude_cloaked_date_pin_test.go"
);
go_file_v8_0_27!(
v8_0_27_effort_directive,
"claude_effort_directive_test.go"
);
go_file_v8_0_27!(
v8_0_27_executor,
"claude_executor_test.go"
);
macro_rules! go_file_v8_0_35 {
($(#[$doc:meta])* $name:ident, $file:literal) => {
$(#[$doc])*
#[tokio::test]
async fn $name() {
replay_file(fixture_v8_0_35(), $file).await;
}
};
}
go_file_v8_0_35!(
v8_0_35_executor,
"claude_executor_test.go"
);
go_file_v8_0_35!(
v8_0_35_mid_system_model,
"claude_mid_system_model_test.go"
);
go_file_v8_0_35!(
v8_0_35_cloaked_date_pin,
"claude_cloaked_date_pin_test.go"
);
go_file_v8_0_35!(
v8_0_35_payload_barrier,
"payload_barrier_test.go"
);
go_file_v8_0_35!(
v8_0_35_payload_review,
"payload_review_test.go"
);
#[tokio::test]
async fn v8_0_35_tool_search() {
replay_file(fixture_tool_search(), "claude_executor_tool_search_test.go").await;
}