jules-api 0.1.1

API integrations and client implementation for Jules-SDK
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
//! Integration tests for the real `v1alpha` session/source/activity endpoints on
//! `JulesClient`, against a local mock HTTP server. Response bodies are shaped like the real
//! `v1alpha` API (verified against the live API on 2026-08-08) but use obviously-fake
//! placeholder data.
#![cfg(all(feature = "middleware", not(target_arch = "wasm32")))]

use jules_api::auth::AuthType;
use jules_api::client::{CreateSessionParams, JulesClientBuilder};
use jules_core::session::SourceContext;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};

/// A single captured HTTP request received by the fake server.
struct CapturedRequest {
    request_line: String,
    headers: Vec<(String, String)>,
    body: Vec<u8>,
}

fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack.windows(needle.len()).position(|w| w == needle)
}

async fn read_request(socket: &mut TcpStream) -> CapturedRequest {
    let mut buf = Vec::new();
    let mut chunk = [0u8; 1024];

    let header_end = loop {
        let n = socket.read(&mut chunk).await.expect("read failed");
        assert!(n > 0, "connection closed before headers were received");
        buf.extend_from_slice(&chunk[..n]);
        if let Some(pos) = find_subslice(&buf, b"\r\n\r\n") {
            break pos + 4;
        }
    };

    let header_text = String::from_utf8_lossy(&buf[..header_end]).to_string();
    let mut lines = header_text.lines();
    let request_line = lines.next().unwrap_or_default().to_string();
    let mut headers = Vec::new();
    for line in lines {
        if let Some((k, v)) = line.split_once(':') {
            headers.push((k.trim().to_string(), v.trim().to_string()));
        }
    }

    let content_length: usize = headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("content-length"))
        .and_then(|(_, v)| v.parse().ok())
        .unwrap_or(0);

    let mut body = buf[header_end..].to_vec();
    while body.len() < content_length {
        let n = socket.read(&mut chunk).await.expect("read failed");
        assert!(n > 0, "connection closed before body was fully received");
        body.extend_from_slice(&chunk[..n]);
    }
    body.truncate(content_length);

    CapturedRequest {
        request_line,
        headers,
        body,
    }
}

/// Starts a local server that accepts exactly one connection, captures the request, and
/// responds with `status`/`body`. Returns the bound address and a handle yielding the captured
/// request.
async fn serve_once(
    status: &'static str,
    body: &'static str,
) -> (
    std::net::SocketAddr,
    tokio::task::JoinHandle<CapturedRequest>,
) {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let handle = tokio::spawn(async move {
        let (mut socket, _) = listener.accept().await.unwrap();
        let captured = read_request(&mut socket).await;

        let body_bytes = body.as_bytes();
        let response = format!(
            "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
            body_bytes.len()
        );
        socket.write_all(response.as_bytes()).await.unwrap();
        socket.write_all(body_bytes).await.unwrap();
        socket.shutdown().await.unwrap();

        captured
    });

    (addr, handle)
}

fn find_header<'a>(req: &'a CapturedRequest, name: &str) -> Option<&'a str> {
    req.headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case(name))
        .map(|(_, v)| v.as_str())
}

/// Starts a local server that accepts one connection per entry in `responses`, in order,
/// captures each request, and responds accordingly. Returns the bound address and a handle
/// yielding all captured requests in the order they were received.
async fn serve_sequence(
    responses: Vec<(&'static str, &'static str)>,
) -> (
    std::net::SocketAddr,
    tokio::task::JoinHandle<Vec<CapturedRequest>>,
) {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let handle = tokio::spawn(async move {
        let mut captured = Vec::new();
        for (status, body) in responses {
            let (mut socket, _) = listener.accept().await.unwrap();
            captured.push(read_request(&mut socket).await);

            let body_bytes = body.as_bytes();
            let response = format!(
                "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                body_bytes.len()
            );
            socket.write_all(response.as_bytes()).await.unwrap();
            socket.write_all(body_bytes).await.unwrap();
            socket.shutdown().await.unwrap();
        }
        captured
    });

    (addr, handle)
}

/// Proves `JulesClient`'s internal `send_with_retry` loop (backing every real v1alpha endpoint
/// method) actually retries on a 500 and succeeds once the server recovers, re-sending auth on
/// each attempt.
#[tokio::test]
async fn test_list_sessions_retries_on_500_then_succeeds() {
    let error_body = r#"{"error": {"message": "internal error", "code": 500}}"#;
    let success_body = r#"{"sessions": [{"name": "sessions/000", "id": "000"}]}"#;
    let (addr, handle) = serve_sequence(vec![
        ("500 Internal Server Error", error_body),
        ("200 OK", success_body),
    ])
    .await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .timeout(Duration::from_secs(5))
        .build()
        .unwrap();

    let page = tokio::time::timeout(Duration::from_secs(5), client.list_sessions(None, None))
        .await
        .expect("timed out")
        .expect("list_sessions should succeed after retrying");

    assert_eq!(page.items().len(), 1);

    let requests = handle.await.unwrap();
    assert_eq!(
        requests.len(),
        2,
        "expected exactly one retry (two requests total)"
    );
    for req in &requests {
        assert_eq!(find_header(req, "X-Goog-Api-Key"), Some("test-key"));
    }
}

/// Proves `send_with_retry` gives up and propagates the final error after exhausting
/// `ExponentialBackoff::default().max_retries`, rather than retrying forever or panicking.
#[tokio::test]
async fn test_get_session_gives_up_after_max_retries() {
    let error_body = r#"{"error": {"message": "internal error", "code": 500}}"#;
    // ExponentialBackoff::default() has max_retries: 3, so 1 initial attempt + 3 retries = 4
    // total requests before send_with_retry gives up.
    let responses = vec![("500 Internal Server Error", error_body); 4];
    let (addr, handle) = serve_sequence(responses).await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .timeout(Duration::from_secs(5))
        .build()
        .unwrap();

    let result = tokio::time::timeout(Duration::from_secs(5), client.get_session("sessions/000"))
        .await
        .expect("timed out");

    assert!(
        result.is_err(),
        "expected an error after exhausting retries"
    );

    let requests = handle.await.unwrap();
    assert_eq!(
        requests.len(),
        4,
        "expected exactly 4 requests (1 initial + 3 retries)"
    );
}

#[tokio::test]
async fn test_list_sessions() {
    let body = r#"{
        "sessions": [
            {
                "name": "sessions/000",
                "title": "Example session",
                "state": "AWAITING_USER_FEEDBACK",
                "id": "000"
            }
        ],
        "nextPageToken": "token-2"
    }"#;
    let (addr, handle) = serve_once("200 OK", body).await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .timeout(Duration::from_secs(5))
        .build()
        .unwrap();

    let page = tokio::time::timeout(
        Duration::from_secs(5),
        client.list_sessions(Some(10), Some("page-token")),
    )
    .await
    .expect("timed out")
    .expect("list_sessions failed");

    assert_eq!(page.items().len(), 1);
    assert_eq!(page.items()[0].name(), Some("sessions/000"));
    assert_eq!(page.next_page_token(), Some("token-2"));

    let req = handle.await.unwrap();
    assert!(req.request_line.starts_with("GET /v1alpha/sessions?"));
    assert!(req.request_line.contains("pageSize=10"));
    assert!(req.request_line.contains("pageToken=page-token"));
    assert_eq!(find_header(&req, "X-Goog-Api-Key"), Some("test-key"));
}

#[tokio::test]
async fn test_get_session() {
    let body = r#"{"name": "sessions/000", "state": "COMPLETED", "id": "000"}"#;
    let (addr, handle) = serve_once("200 OK", body).await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .build()
        .unwrap();

    let session = client.get_session("sessions/000").await.unwrap();
    assert_eq!(session.state(), Some("COMPLETED"));

    let req = handle.await.unwrap();
    assert!(req.request_line.starts_with("GET /v1alpha/sessions/000"));
}

#[tokio::test]
async fn test_list_sources() {
    let body = r#"{
        "sources": [
            {
                "name": "sources/github/example-owner/example-repo",
                "githubRepo": {
                    "owner": "example-owner",
                    "repo": "example-repo",
                    "isPrivate": false
                },
                "id": "github/example-owner/example-repo"
            }
        ]
    }"#;
    let (addr, handle) = serve_once("200 OK", body).await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .build()
        .unwrap();

    let page = client.list_sources(None, None).await.unwrap();
    assert_eq!(page.items().len(), 1);
    assert_eq!(
        page.items()[0].github_repo().and_then(|r| r.owner()),
        Some("example-owner")
    );
    assert_eq!(page.next_page_token(), None);

    let req = handle.await.unwrap();
    assert!(req.request_line.starts_with("GET /v1alpha/sources"));
}

#[tokio::test]
async fn test_list_activities() {
    let body = r#"{
        "activities": [
            {
                "name": "sessions/000/activities/001",
                "originator": "AGENT",
                "planGenerated": {
                    "plan": {
                        "id": "plan-1",
                        "steps": [{"id": "s1", "title": "Step one"}]
                    }
                },
                "id": "001"
            }
        ]
    }"#;
    let (addr, handle) = serve_once("200 OK", body).await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .build()
        .unwrap();

    let page = client
        .list_activities("sessions/000", None, None)
        .await
        .unwrap();
    assert_eq!(page.items().len(), 1);
    assert_eq!(page.items()[0].originator(), Some("AGENT"));

    let req = handle.await.unwrap();
    assert!(req
        .request_line
        .starts_with("GET /v1alpha/sessions/000/activities"));
}

#[tokio::test]
async fn test_create_session() {
    let body = r#"{"name": "sessions/new", "title": "New session", "id": "new"}"#;
    let (addr, handle) = serve_once("200 OK", body).await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .build()
        .unwrap();

    let session = client
        .create_session(CreateSessionParams {
            title: Some("New session".to_string()),
            prompt: Some("Do the thing".to_string()),
            source_context: Some(SourceContext::new(
                "sources/github/example-owner/example-repo",
            )),
        })
        .await
        .unwrap();

    assert_eq!(session.name(), Some("sessions/new"));

    let req = handle.await.unwrap();
    assert!(req.request_line.starts_with("POST /v1alpha/sessions"));
    let sent: serde_json::Value = serde_json::from_slice(&req.body).unwrap();
    assert_eq!(sent["title"], "New session");
    assert_eq!(sent["prompt"], "Do the thing");
    assert_eq!(
        sent["sourceContext"]["source"],
        "sources/github/example-owner/example-repo"
    );
}

#[tokio::test]
async fn test_send_message() {
    let (addr, handle) = serve_once("200 OK", "{}").await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .build()
        .unwrap();

    // Note: In the live API, this endpoint may return HTTP 404 if the session
    // is in the QUEUED state. It must be in a ready state (e.g., AWAITING_USER_FEEDBACK).
    client
        .send_message("sessions/000", "hello agent")
        .await
        .unwrap();

    let req = handle.await.unwrap();
    assert!(req
        .request_line
        .starts_with("POST /v1alpha/sessions/000:sendMessage"));
    let sent: serde_json::Value = serde_json::from_slice(&req.body).unwrap();
    assert_eq!(sent["prompt"], "hello agent");
}

#[tokio::test]
async fn test_approve_plan() {
    let (addr, handle) = serve_once("200 OK", "{}").await;

    let client = JulesClientBuilder::new()
        .base_url(format!("http://{addr}"))
        .auth(AuthType::google_api_key("test-key"))
        .build()
        .unwrap();

    client.approve_plan("sessions/000").await.unwrap();

    let req = handle.await.unwrap();
    assert!(req
        .request_line
        .starts_with("POST /v1alpha/sessions/000:approvePlan"));
    assert_eq!(std::str::from_utf8(&req.body).unwrap(), "{}");
}