#![allow(unused_imports)]
use crate::sdk::{self, types};
use crate::support;
#[tokio::test]
async fn reports_api_errors() {
let body = support::json(r#"{"code":"not_found","message":"No such resource.","details":{"field":"id"}}"#);
let reply = support::Reply::json(404, body).header("x-request-id", "req_test");
let api = support::MockApi::start(vec![reply]).await;
let params: types::BotsListParams = support::from_json(r#"{"limit":50,"cursor":"cursor 1"}"#);
let error = api.client().bots().list("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).page().await.unwrap_err();
assert!(matches!(error, sdk::Error::Api { .. }), "{error:?}");
assert_eq!(error.status(), Some(404));
assert_eq!(error.code(), "not_found");
assert_eq!(error.request_id(), Some("req_test"));
assert_eq!(error.details().and_then(|d| d.get("field")), Some(&"id".into()));
assert_eq!(error.to_string(), "No such resource. (404 not_found, request req_test)");
assert_eq!(api.calls().await.len(), 1);
}
#[tokio::test]
async fn list_streams_every_page() {
let first = support::json(r#"{
"object": "list",
"items": [
{
"object": "bot",
"id": "id 1",
"personaId": "personaId 1",
"name": "name 1",
"description": "description 1",
"category": "category 1",
"default": true,
"prompts": [
"prompts 1"
],
"commands": [
{
"name": "name 1",
"description": "description 1"
}
]
}
],
"nextCursor": "cursor_2"
}"#);
let last = support::json(r#"{
"object": "list",
"items": [
{
"object": "bot",
"id": "id 1",
"personaId": "personaId 1",
"name": "name 1",
"description": "description 1",
"category": "category 1",
"default": true,
"prompts": [
"prompts 1"
],
"commands": [
{
"name": "name 1",
"description": "description 1"
}
]
}
],
"nextCursor": null
}"#);
let api = support::MockApi::start(vec![
support::Reply::json(200, first),
support::Reply::json(200, last),
])
.await;
let params: types::BotsListParams = support::from_json(r#"{"limit":50,"cursor":"cursor 1"}"#);
let items = api.client().bots().list("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).to_vec().await.unwrap();
support::assert_json(&items, &support::json(r#"[
{
"object": "bot",
"id": "id 1",
"personaId": "personaId 1",
"name": "name 1",
"description": "description 1",
"category": "category 1",
"default": true,
"prompts": [
"prompts 1"
],
"commands": [
{
"name": "name 1",
"description": "description 1"
}
]
},
{
"object": "bot",
"id": "id 1",
"personaId": "personaId 1",
"name": "name 1",
"description": "description 1",
"category": "category 1",
"default": true,
"prompts": [
"prompts 1"
],
"commands": [
{
"name": "name 1",
"description": "description 1"
}
]
}
]"#));
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/bots");
assert_eq!(calls[0].query, support::pairs(&[("limit", "50"), ("cursor", "cursor 1")]));
assert_eq!(calls[1].query, support::pairs(&[("limit", "50"), ("cursor", "cursor_2")]));
}
#[tokio::test]
async fn list_fetches_one_page() {
let page = support::json(r#"{
"object": "list",
"items": [
{
"object": "bot",
"id": "id 1",
"personaId": "personaId 1",
"name": "name 1",
"description": "description 1",
"category": "category 1",
"default": true,
"prompts": [
"prompts 1"
],
"commands": [
{
"name": "name 1",
"description": "description 1"
}
]
}
],
"nextCursor": null
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, page.clone())]).await;
let result = api.client().bots().list("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", Default::default()).page().await.unwrap();
support::assert_json(&result, &page);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/bots");
assert_eq!(calls[0].query, support::pairs(&[]));
}