grok-build-upstream-mirror/crates/common/xai-tool-protocol/tests/identifier_validation.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

192 lines
5.7 KiB
Rust

//! Validation rules for every identifier newtype, plus the synthetic
//! `ServerId` helper invariants.
use std::str::FromStr;
use xai_tool_protocol::{
ConnectionId, IdError, RequestId, ServerId, SessionId, ToolCallId, ToolId, UserId,
};
#[test]
fn tool_id_accepts_bare_and_namespaced_names() {
let bare = ToolId::new("read_file").unwrap();
assert_eq!(bare.as_str(), "read_file");
let namespaced = ToolId::new("GrokBuild:read_file").unwrap();
assert_eq!(namespaced.as_str(), "GrokBuild:read_file");
assert_eq!(
ToolId::from_str("github:list_repos").unwrap().as_str(),
"github:list_repos"
);
}
#[test]
fn tool_id_rejects_empty() {
assert_eq!(ToolId::new("").unwrap_err(), IdError::Empty);
}
#[test]
fn tool_id_rejects_more_than_one_separator() {
let err = ToolId::new("foo:bar:baz").unwrap_err();
assert_eq!(
err,
IdError::InvalidFormat {
value: "foo:bar:baz".to_owned()
}
);
}
#[test]
fn tool_id_rejects_disallowed_characters() {
for bad in [
"foo bar",
"foo!bar",
"foo/bar",
"foo.bar",
"foo:bar baz",
" foo",
"foo ",
"foo\tbar",
"foo\u{00e9}bar",
"f\u{00f6}\u{00f6}bar",
] {
let err = ToolId::new(bad).unwrap_err();
assert!(
matches!(err, IdError::InvalidFormat { ref value } if value == bad),
"expected InvalidFormat for {bad:?}, got {err:?}"
);
}
}
#[test]
fn tool_id_accepts_digits_only_and_other_boundary_inputs() {
assert_eq!(ToolId::new("123").unwrap().as_str(), "123");
assert_eq!(ToolId::new("v2:42").unwrap().as_str(), "v2:42");
assert_eq!(ToolId::new("a").unwrap().as_str(), "a");
assert_eq!(ToolId::new("a:b").unwrap().as_str(), "a:b");
assert_eq!(ToolId::new("-_-").unwrap().as_str(), "-_-");
}
#[test]
fn tool_id_rejects_empty_segments_around_separator() {
for bad in [":foo", "foo:", ":"] {
let err = ToolId::new(bad).unwrap_err();
assert!(
matches!(err, IdError::InvalidFormat { ref value } if value == bad),
"expected InvalidFormat for {bad:?}, got {err:?}"
);
}
}
#[test]
fn tool_id_try_from_string_works() {
let id: ToolId = "GrokBuild:read_file".to_owned().try_into().unwrap();
assert_eq!(id.as_str(), "GrokBuild:read_file");
}
#[test]
fn server_id_accepts_arbitrary_non_empty_strings() {
for ok in ["my-uuid-v7", "srv_42", "x", "abc.def"] {
let s = ServerId::new(ok).unwrap();
assert_eq!(s.as_str(), ok);
}
}
#[test]
fn server_id_rejects_empty() {
assert_eq!(ServerId::new("").unwrap_err(), IdError::Empty);
}
#[test]
fn server_id_rejects_reserved_auto_prefix() {
for bad in ["auto:my-server", "auto:", "auto:tool:read_file"] {
let err = ServerId::new(bad).unwrap_err();
assert!(
matches!(err, IdError::ReservedPrefix { ref value } if value == bad),
"expected ReservedPrefix for {bad:?}, got {err:?}"
);
}
}
#[test]
fn server_id_synthesis_starts_with_auto_prefix() {
let conn = ConnectionId::new("conn-abc").unwrap();
let bare = ToolId::new("read_file").unwrap();
let synth = ServerId::synthesize_for_tool(&conn, &bare);
assert_eq!(synth.as_str(), "auto:tool:read_file");
let namespaced = ToolId::new("GrokBuild:read_file").unwrap();
let synth_ns = ServerId::synthesize_for_tool(&conn, &namespaced);
assert_eq!(synth_ns.as_str(), "auto:tool:GrokBuild:read_file");
}
#[test]
fn server_id_synthesis_is_deterministic() {
let conn = ConnectionId::new("conn-abc").unwrap();
let tool = ToolId::new("GrokBuild:read_file").unwrap();
let a = ServerId::synthesize_for_tool(&conn, &tool);
let b = ServerId::synthesize_for_tool(&conn, &tool);
assert_eq!(
a, b,
"synthesis must be a pure function of (connection, tool)"
);
}
#[test]
fn server_id_synthesis_bypasses_reserved_prefix_check() {
// The synthesised id starts with `auto:`; the reserved-prefix rule
// only applies to client-supplied values via `ServerId::new`.
let conn = ConnectionId::new("conn-abc").unwrap();
let tool = ToolId::new("read_file").unwrap();
let synth = ServerId::synthesize_for_tool(&conn, &tool);
assert!(synth.as_str().starts_with("auto:"));
let err = ServerId::new(synth.as_str()).unwrap_err();
assert!(matches!(err, IdError::ReservedPrefix { .. }));
}
#[test]
fn opaque_ids_reject_empty() {
assert_eq!(SessionId::new("").unwrap_err(), IdError::Empty);
assert_eq!(UserId::new("").unwrap_err(), IdError::Empty);
assert_eq!(ConnectionId::new("").unwrap_err(), IdError::Empty);
assert_eq!(RequestId::new("").unwrap_err(), IdError::Empty);
assert_eq!(ToolCallId::new("").unwrap_err(), IdError::Empty);
}
#[test]
fn opaque_ids_accept_arbitrary_non_empty_strings() {
assert_eq!(
SessionId::new("anything goes").unwrap().as_str(),
"anything goes"
);
assert_eq!(
UserId::new("alice@example.com").unwrap().as_str(),
"alice@example.com"
);
assert_eq!(RequestId::new("req-9c4f").unwrap().as_str(), "req-9c4f");
}
#[test]
fn tool_call_id_uuid_v7_helper_is_unique_and_valid_uuid() {
let a = ToolCallId::new_v7();
let b = ToolCallId::new_v7();
assert_ne!(a, b, "two consecutive v7 ids must differ");
for id in [&a, &b] {
let parsed = uuid::Uuid::parse_str(id.as_str()).expect("parse uuid");
assert_eq!(
parsed.get_version_num(),
7,
"expected UUID v7, got {parsed}"
);
}
}
#[test]
fn opaque_id_display_matches_inner_string() {
let s = SessionId::new("sess_abc").unwrap();
assert_eq!(format!("{s}"), "sess_abc");
let t = ToolId::new("github:list_repos").unwrap();
assert_eq!(format!("{t}"), "github:list_repos");
}