File size: 4,767 Bytes
afa0cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::time::Duration;

use codex_code_mode_protocol::CellId;
use codex_code_mode_protocol::FunctionCallOutputContentItem;
use codex_code_mode_protocol::ImageDetail;
use codex_code_mode_protocol::MissingCodeModeHostDuration;
use codex_code_mode_protocol::RuntimeResponse;
use codex_code_mode_protocol::WaitOutcome;
use codex_code_mode_protocol::grpc as proto;
use pretty_assertions::assert_eq;
use tonic::Code;

use super::execute_request;
use super::execution_outcome;
use super::wait_response;

#[test]
fn rejects_missing_names_unknown_tool_kinds_and_invalid_json_schemas() {
    let definition = proto::ToolDefinition {
        name: "echo".to_string(),
        tool_name: None,
        description: String::new(),
        kind: proto::ToolKind::Function as i32,
        input_schema_json: None,
        output_schema_json: None,
    };
    let request = |definition| proto::ExecuteRequest {
        session_id: "session".to_string(),
        execution_id: "execution".to_string(),
        tool_call_id: "call".to_string(),
        source: String::new(),
        enabled_tools: vec![definition],
        yield_time_ms: None,
        max_output_tokens: None,
    };

    assert_eq!(
        execute_request(request(definition.clone()))
            .unwrap_err()
            .code(),
        Code::InvalidArgument
    );
    let definition = proto::ToolDefinition {
        tool_name: Some(proto::ToolName {
            name: "echo".to_string(),
            namespace: Some("tools".to_string()),
        }),
        ..definition
    };
    assert_eq!(
        execute_request(request(proto::ToolDefinition {
            kind: proto::ToolKind::Unspecified as i32,
            ..definition.clone()
        }))
        .unwrap_err()
        .code(),
        Code::InvalidArgument
    );
    assert_eq!(
        execute_request(request(proto::ToolDefinition {
            input_schema_json: Some(b"not-json".to_vec()),
            ..definition
        }))
        .unwrap_err()
        .code(),
        Code::InvalidArgument
    );
}

#[test]
fn maps_text_image_audio_and_terminal_error_without_losing_details() {
    let outcome = execution_outcome(RuntimeResponse::Result {
        code_mode_host_duration: Some(Duration::from_nanos(/*nanos*/ 123_456_789)),
        cell_id: CellId::new("cell".to_string()),
        content_items: vec![
            FunctionCallOutputContentItem::InputText {
                text: "hello".to_string(),
            },
            FunctionCallOutputContentItem::InputImage {
                image_url: "data:image/png;base64,YQ==".to_string(),
                detail: Some(ImageDetail::Original),
            },
            FunctionCallOutputContentItem::InputAudio {
                audio_url: "data:audio/wav;base64,YQ==".to_string(),
            },
        ],
        error_text: Some("failed".to_string()),
    });

    assert_eq!(
        outcome,
        Ok(proto::ExecutionOutcome {
            code_mode_host_duration_ns: 123_456_789,
            cell_id: "cell".to_string(),
            content_items: vec![
                proto::ContentItem {
                    item: Some(proto::content_item::Item::Text(proto::TextContent {
                        text: "hello".to_string(),
                    })),
                },
                proto::ContentItem {
                    item: Some(proto::content_item::Item::Image(proto::ImageContent {
                        image_url: "data:image/png;base64,YQ==".to_string(),
                        detail: Some(proto::ImageDetail::Original as i32),
                    })),
                },
                proto::ContentItem {
                    item: Some(proto::content_item::Item::Audio(proto::AudioContent {
                        audio_url: "data:audio/wav;base64,YQ==".to_string(),
                    })),
                },
            ],
            outcome: Some(proto::execution_outcome::Outcome::Completed(
                proto::ExecutionCompleted {
                    error_text: Some("failed".to_string()),
                },
            )),
        })
    );
}

/// Encoding must not turn a missing request measurement into measured zero,
/// including when no live cell remains to supply output.
#[test]
fn grpc_encoding_rejects_untimed_runtime_output() {
    let response = RuntimeResponse::Terminated {
        cell_id: CellId::new("cell".to_string()),
        content_items: Vec::new(),
        code_mode_host_duration: None,
    };
    assert_eq!(
        execution_outcome(response.clone()),
        Err(MissingCodeModeHostDuration)
    );
    for outcome in [
        WaitOutcome::LiveCell(response.clone()),
        WaitOutcome::MissingCell(response),
    ] {
        assert_eq!(wait_response(outcome), Err(MissingCodeModeHostDuration));
    }
}