grok-build-upstream-mirror/crates/common/xai-tool-runtime/tests/error_conversion.rs
grokkybara[bot] 3af4d5d398 Synced from monorepo
Synced from monorepo

Changes:
- Shell: accept target response id on rewind execute
- Shell: stamp response id on chat user message chunks
- Worktree: optional rebuild and stale git registration cleanup in auto-GC
- Worktree: kind-aware auto-GC TTLs and config knobs
- Worktree: macOS process CWD scan and Unix PID liveness for GC guards
- Worktree: automatic throttled GC on startup (Linux age-based; non-Linux dead-only)
- Pager: add `[ui].combine_queued_prompts` to batch queued follow-ups
- Shell: stop overwriting user skills
- Tools: read markdown in `skills/` directories untruncated
- `/usage` shows per-session token and dollar usage in the TUI
- Security: prompt on environment-dumping `ps` variants
- Security: always-safe `kubectl` no longer runs arbitrary kubeconfig credential plugins without permission
- Tools: make scheduler deletion durable
- Shell: add relocation storage primitives
- Shell: give side model calls their own conversation ids
- Fix five workflow-runtime bugs (budget, pause, cancel, reconnect)
- Security: peel `env -S` / `--split-string` operands in the Bash permission gate (managed deny/ask)
- Pager: expose doctor in the TUI
- Security: block unauthorized RCE via abused safe commands
- Pager idle watcher cue: "1 subagent still running" instead of "watching · 1 subagent"
- Security: block `rg --pre` arbitrary code execution in auto-mode
- Voice: diagnose silent-mic failures (macOS permission) and add doctor/terminal-setup Voice section
- App builder deployer: `allow_forking` and `show_built_with_grok`
- Pager: stop stacking duplicate "Worked for" markers on parked turns
- Shell: support `max` as a distinct reasoning effort tier
- Tools: serialize background `/loop` fires on the whole work unit
- Shell: add working-directory relocation state primitives
- Proto: `ClientToolResult` and `ChatConfig` client-side tools
- Shell: model providers
- Chat: select App Builder product on the Build path
- Shell: attach author identity to feedback when the deployment opts in
- Doctor: fix for SSH wrap setup
- Workflow authoring skills: create-workflow and import-claude-workflow docs
- Add read-only grok doctor
- Sandbox: apply Landlock without a controlling TTY
- Pager: recover image paste over grok wrap on headless remotes
- Pager: make actions screen-mode aware
- Shell: resume sessions when the working directory moves
- Pager: centralize terminal diagnostics
- Workspace: gate inline shell file access
- Pager: centralize terminal probes
- Pager: edit minimal prompts in an external editor
- Pager: standardize backgrounding on Ctrl+B
- Shell: recap rides the parent turn's prompt cache
- Tools: add scheduler lifecycle version clock

Source-Revision: 0f4d7c91b8b2b408333f6de1e8a76cb8eaa71899
2026-07-21 18:10:23 +00:00

226 lines
6.9 KiB
Rust

//! `From<ToolError> for ToolErrorWire` coverage for the struct-based ToolError.
use serde_json::json;
use xai_tool_protocol::{ToolErrorWire, ToolId};
use xai_tool_runtime::error::{ToolError, ToolErrorKind};
fn tid(name: &str) -> ToolId {
ToolId::new(name).unwrap()
}
#[test]
fn not_implemented_maps_to_custom_with_snake_case_code() {
let err = ToolError::not_implemented("nope");
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Custom {
subcode, message, ..
} => {
assert_eq!(subcode, "not_implemented");
assert_eq!(message, "nope");
}
other => panic!("expected Custom, got {other:?}"),
}
}
#[test]
fn invalid_arguments_round_trips_message_and_details() {
let details = json!({"field": "name", "expected": "non-empty"});
let err = ToolError::invalid_arguments("bad name").with_details(details.clone());
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::InvalidArguments {
message,
details: d,
} => {
assert_eq!(message, "bad name");
assert_eq!(d, Some(details));
}
other => panic!("expected InvalidArguments, got {other:?}"),
}
}
#[test]
fn not_found_maps_to_tool_not_found() {
let err = ToolError::not_found(tid("missing"), "tool 'missing' not registered");
let wire: ToolErrorWire = err.into();
assert!(matches!(wire, ToolErrorWire::ToolNotFound { tool_id } if tool_id == tid("missing")));
}
#[test]
fn permission_denied_round_trips_reason() {
let err = ToolError::permission_denied("not authorised for write");
let wire: ToolErrorWire = err.into();
assert!(
matches!(wire, ToolErrorWire::PermissionDenied { reason } if reason == "not authorised for write")
);
}
#[test]
fn unauthorized_maps_to_custom_with_unauthorized_subcode() {
let err = ToolError::unauthorized("session expired");
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Custom {
subcode, message, ..
} => {
assert_eq!(subcode, "unauthorized");
assert_eq!(message, "session expired");
}
other => panic!("expected Custom(unauthorized), got {other:?}"),
}
}
#[test]
fn timeout_with_details() {
let err = ToolError::timeout(tid("slow"), "image generation timed out")
.with_details(json!({"tool_id": "slow", "elapsed_ms": 2500}));
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Timeout {
tool_id,
elapsed_ms,
} => {
assert_eq!(tool_id, tid("slow"));
assert_eq!(elapsed_ms, 2_500);
}
other => panic!("expected Timeout, got {other:?}"),
}
}
#[test]
fn cancelled_round_trips_tool_id() {
let err = ToolError::cancelled(tid("paused"), "user cancelled");
let wire: ToolErrorWire = err.into();
assert!(matches!(wire, ToolErrorWire::Cancelled { tool_id } if tool_id == tid("paused")));
}
#[test]
fn rate_limited_carries_detail_message() {
let err = ToolError::rate_limited("You've reached your image generation limit.");
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Custom {
subcode, message, ..
} => {
assert_eq!(subcode, "rate_limited");
assert_eq!(message, "You've reached your image generation limit.");
}
other => panic!("expected Custom, got {other:?}"),
}
}
#[test]
fn service_unavailable_carries_detail() {
let err = ToolError::service_unavailable("Media service temporarily down.");
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Custom {
subcode, message, ..
} => {
assert_eq!(subcode, "service_unavailable");
assert_eq!(message, "Media service temporarily down.");
}
other => panic!("expected Custom, got {other:?}"),
}
}
#[test]
fn network_error_maps_to_custom_with_message() {
let err = ToolError::network_error("dns failure");
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Custom {
subcode, message, ..
} => {
assert_eq!(subcode, "network_error");
assert_eq!(message, "dns failure");
}
other => panic!("expected Custom, got {other:?}"),
}
}
#[test]
fn execution_uses_detail_as_message() {
let err = ToolError::execution(
tid("worker"),
"image generation failed: model returned empty response",
)
.with_source(anyhow::anyhow!("root cause"));
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Execution { tool_id, message } => {
assert_eq!(tool_id, tid("worker"));
assert_eq!(
message,
"image generation failed: model returned empty response"
);
}
other => panic!("expected Execution, got {other:?}"),
}
}
#[test]
fn custom_round_trips_code_message_details() {
let err = ToolError::custom("billing_overflow", "quota exhausted")
.with_details(json!({"code": "billing_overflow", "limit": 1000}));
let wire: ToolErrorWire = err.into();
match wire {
ToolErrorWire::Custom {
subcode,
message,
details,
} => {
assert_eq!(subcode, "billing_overflow");
assert_eq!(message, "quota exhausted");
assert!(details.is_some());
}
other => panic!("expected Custom, got {other:?}"),
}
}
#[test]
fn variant_name_covers_all_kinds() {
let kinds = [
ToolErrorKind::NotImplemented,
ToolErrorKind::InvalidArguments,
ToolErrorKind::NotFound,
ToolErrorKind::PermissionDenied,
ToolErrorKind::Unauthorized,
ToolErrorKind::Timeout,
ToolErrorKind::Cancelled,
ToolErrorKind::RateLimited,
ToolErrorKind::ServiceUnavailable,
ToolErrorKind::NetworkError,
ToolErrorKind::Execution,
ToolErrorKind::BehaviorVersionUnsupported,
ToolErrorKind::RenderLimited,
ToolErrorKind::TerminalError,
ToolErrorKind::Custom,
];
let names: std::collections::HashSet<_> = kinds.iter().map(|k| k.as_str()).collect();
assert_eq!(names.len(), 15);
}
#[test]
fn display_shows_detail_not_kind() {
let err = ToolError::rate_limited("You've exceeded your quota.");
assert_eq!(err.to_string(), "You've exceeded your quota.");
}
#[test]
fn serde_json_error_converts_to_invalid_arguments() {
let err: ToolError = serde_json::from_str::<u32>("\"not a number\"")
.unwrap_err()
.into();
assert_eq!(err.kind, ToolErrorKind::InvalidArguments);
}
#[test]
fn with_source_preserves_detail() {
let err = ToolError::execution(tid("test"), "something broke")
.with_source(anyhow::anyhow!("inner cause"));
assert_eq!(err.detail, "something broke");
assert!(std::error::Error::source(&err).is_some());
}