#![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::ChannelsListParams = support::from_json(r#"{"limit":50,"cursor":"cursor 1"}"#);
let error = api.client().channels().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": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": "cursor_2"
}"#);
let last = support::json(r#"{
"object": "list",
"items": [
{
"object": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": null
}"#);
let api = support::MockApi::start(vec![
support::Reply::json(200, first),
support::Reply::json(200, last),
])
.await;
let params: types::ChannelsListParams = support::from_json(r#"{"limit":50,"cursor":"cursor 1"}"#);
let items = api.client().channels().list("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).to_vec().await.unwrap();
support::assert_json(&items, &support::json(r#"[
{
"object": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
},
{
"object": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}
]"#));
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels");
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": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": null
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, page.clone())]).await;
let result = api.client().channels().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/channels");
assert_eq!(calls[0].query, support::pairs(&[]));
}
#[tokio::test]
async fn create() {
let response = support::json(r#"{
"object": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(201, response.clone())]).await;
let params: types::ChannelsCreateParams = support::from_json(r#"{"name":"name 1","description":"description 1","pictureBase64":"pictureBase64 1"}"#);
let result = api.client().channels().create("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "POST");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"name":"name 1","description":"description 1","pictureBase64":"pictureBase64 1"}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn create_retries_with_one_key() {
let response = support::json(r#"{
"object": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(201, response.clone())]).await;
let params: types::ChannelsCreateParams = support::from_json(r#"{"name":"name 1","description":"description 1","pictureBase64":"pictureBase64 1"}"#);
let result = api.client().channels().create("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", params).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
assert!(!key.is_empty());
assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
assert_eq!(calls[0].body, calls[1].body);
}
#[tokio::test]
async fn get_invite() {
let response = support::json(r#"{
"object": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().channels().get_invite("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "AbCdEf123456").await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/invites/AbCdEf123456");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn get() {
let response = support::json(r#"{
"object": "channel",
"id": "id 1",
"accountId": "accountId 1",
"name": "name 1",
"description": "description 1",
"inviteCode": "inviteCode 1",
"subscriberCount": 1,
"verified": true,
"status": "status 1",
"role": "role 1",
"muted": true,
"pictureUrl": "pictureUrl 1",
"previewUrl": "previewUrl 1",
"createdAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().channels().get("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn follow() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().channels().follow("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "POST");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter/follow");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn follow_retries_with_one_key() {
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::status(204)]).await;
api.client().channels().follow("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
assert!(!key.is_empty());
assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
assert_eq!(calls[0].body, calls[1].body);
}
#[tokio::test]
async fn unfollow() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().channels().unfollow("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "POST");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter/unfollow");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn unfollow_retries_with_one_key() {
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::status(204)]).await;
api.client().channels().unfollow("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
assert!(!key.is_empty());
assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
assert_eq!(calls[0].body, calls[1].body);
}
#[tokio::test]
async fn mute() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().channels().mute("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "POST");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter/mute");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn mute_retries_with_one_key() {
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::status(204)]).await;
api.client().channels().mute("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
assert!(!key.is_empty());
assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
assert_eq!(calls[0].body, calls[1].body);
}
#[tokio::test]
async fn unmute() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().channels().unmute("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "POST");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter/unmute");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn unmute_retries_with_one_key() {
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::status(204)]).await;
api.client().channels().unmute("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
assert!(!key.is_empty());
assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
assert_eq!(calls[0].body, calls[1].body);
}
#[tokio::test]
async fn list_messages_streams_every_page() {
let first = support::json(r#"{
"object": "list",
"items": [
{
"object": "channel_message",
"id": "id 1",
"accountId": "accountId 1",
"channelId": "channelId 1",
"type": "type 1",
"text": "text 1",
"viewCount": 1,
"reactions": {
"key": 1
},
"sentAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": "cursor_2"
}"#);
let last = support::json(r#"{
"object": "list",
"items": [
{
"object": "channel_message",
"id": "id 1",
"accountId": "accountId 1",
"channelId": "channelId 1",
"type": "type 1",
"text": "text 1",
"viewCount": 1,
"reactions": {
"key": 1
},
"sentAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": null
}"#);
let api = support::MockApi::start(vec![
support::Reply::json(200, first),
support::Reply::json(200, last),
])
.await;
let params: types::ChannelsListMessagesParams = support::from_json(r#"{"limit":50,"cursor":"cursor 1"}"#);
let items = api.client().channels().list_messages("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter", params).to_vec().await.unwrap();
support::assert_json(&items, &support::json(r#"[
{
"object": "channel_message",
"id": "id 1",
"accountId": "accountId 1",
"channelId": "channelId 1",
"type": "type 1",
"text": "text 1",
"viewCount": 1,
"reactions": {
"key": 1
},
"sentAt": "2026-09-24T09:00:00.000Z"
},
{
"object": "channel_message",
"id": "id 1",
"accountId": "accountId 1",
"channelId": "channelId 1",
"type": "type 1",
"text": "text 1",
"viewCount": 1,
"reactions": {
"key": 1
},
"sentAt": "2026-09-24T09:00:00.000Z"
}
]"#));
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter/messages");
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_messages_fetches_one_page() {
let page = support::json(r#"{
"object": "list",
"items": [
{
"object": "channel_message",
"id": "id 1",
"accountId": "accountId 1",
"channelId": "channelId 1",
"type": "type 1",
"text": "text 1",
"viewCount": 1,
"reactions": {
"key": 1
},
"sentAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": null
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, page.clone())]).await;
let result = api.client().channels().list_messages("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter", 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/channels/120363198765432101%40newsletter/messages");
assert_eq!(calls[0].query, support::pairs(&[]));
}
#[tokio::test]
async fn react() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
let params: types::ChannelsReactParams = support::from_json(r#"{"emoji":"emoji 1"}"#);
api.client().channels().react("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter", "142", params).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "POST");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter/messages/142/react");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"emoji":"emoji 1"}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn react_retries_with_one_key() {
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::status(204)]).await;
let params: types::ChannelsReactParams = support::from_json(r#"{"emoji":"emoji 1"}"#);
api.client().channels().react("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter", "142", params).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
assert!(!key.is_empty());
assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
assert_eq!(calls[0].body, calls[1].body);
}
#[tokio::test]
async fn mark_viewed() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
let params: types::ChannelsMarkViewedParams = support::from_json(r#"{"channelMessageIds":["channelMessageIds 1"]}"#);
api.client().channels().mark_viewed("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter", params).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "POST");
assert_eq!(calls[0].path, "/v1/accounts/k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr/channels/120363198765432101%40newsletter/mark-viewed");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"channelMessageIds":["channelMessageIds 1"]}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn mark_viewed_retries_with_one_key() {
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::status(204)]).await;
let params: types::ChannelsMarkViewedParams = support::from_json(r#"{"channelMessageIds":["channelMessageIds 1"]}"#);
api.client().channels().mark_viewed("k57a8m2x9d3f0q1wjh6ypc4n2d7s0vbr", "120363198765432101@newsletter", params).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 2);
let key = calls[0].header(sdk::IDEMPOTENCY_HEADER).unwrap();
assert!(!key.is_empty());
assert_eq!(calls[1].header(sdk::IDEMPOTENCY_HEADER), Some(key));
assert_eq!(calls[0].body, calls[1].body);
}