#![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::MessagesSendParams = support::from_json(r#"{
"accountId": "accountId 1",
"to": "to 1",
"type": "text",
"text": "text 1",
"mentions": [
"mentions 1"
],
"mentionAll": true,
"forwarded": true,
"disappearingSeconds": 0,
"linkPreview": {
"url": "https://example.com/url",
"title": "title 1",
"description": "description 1",
"thumbnailBase64": "thumbnailBase64 1"
},
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"metadata": {
"key": "value 1"
}
}"#);
let error = api.client().messages().send(params).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 send() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(202, response.clone())]).await;
let params: types::MessagesSendParams = support::from_json(r#"{
"accountId": "accountId 1",
"to": "to 1",
"type": "text",
"text": "text 1",
"mentions": [
"mentions 1"
],
"mentionAll": true,
"forwarded": true,
"disappearingSeconds": 0,
"linkPreview": {
"url": "https://example.com/url",
"title": "title 1",
"description": "description 1",
"thumbnailBase64": "thumbnailBase64 1"
},
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"metadata": {
"key": "value 1"
}
}"#);
let result = api.client().messages().send(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/messages");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{
"accountId": "accountId 1",
"to": "to 1",
"type": "text",
"text": "text 1",
"mentions": [
"mentions 1"
],
"mentionAll": true,
"forwarded": true,
"disappearingSeconds": 0,
"linkPreview": {
"url": "https://example.com/url",
"title": "title 1",
"description": "description 1",
"thumbnailBase64": "thumbnailBase64 1"
},
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"metadata": {
"key": "value 1"
}
}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn send_retries_with_one_key() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(202, response.clone())]).await;
let params: types::MessagesSendParams = support::from_json(r#"{
"accountId": "accountId 1",
"to": "to 1",
"type": "text",
"text": "text 1",
"mentions": [
"mentions 1"
],
"mentionAll": true,
"forwarded": true,
"disappearingSeconds": 0,
"linkPreview": {
"url": "https://example.com/url",
"title": "title 1",
"description": "description 1",
"thumbnailBase64": "thumbnailBase64 1"
},
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"metadata": {
"key": "value 1"
}
}"#);
let result = api.client().messages().send(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 list_streams_every_page() {
let first = support::json(r#"{
"object": "list",
"items": [
{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": "cursor_2"
}"#);
let last = support::json(r#"{
"object": "list",
"items": [
{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "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::MessagesListParams = support::from_json(r#"{
"accountId": "accountId 1",
"chatId": "chatId 1",
"direction": "inbound",
"limit": 50,
"cursor": "cursor 1",
"projectId": "ext:customer_8812"
}"#);
let items = api.client().messages().list(params).to_vec().await.unwrap();
support::assert_json(&items, &support::json(r#"[
{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
},
{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "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/messages");
assert_eq!(calls[0].query, support::pairs(&[("accountId", "accountId 1"), ("chatId", "chatId 1"), ("direction", "inbound"), ("limit", "50"), ("projectId", "ext:customer_8812"), ("cursor", "cursor 1")]));
assert_eq!(calls[1].query, support::pairs(&[("accountId", "accountId 1"), ("chatId", "chatId 1"), ("direction", "inbound"), ("limit", "50"), ("projectId", "ext:customer_8812"), ("cursor", "cursor_2")]));
}
#[tokio::test]
async fn list_fetches_one_page() {
let page = support::json(r#"{
"object": "list",
"items": [
{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "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().messages().list(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/messages");
assert_eq!(calls[0].query, support::pairs(&[]));
}
#[tokio::test]
async fn get() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().messages().get("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e").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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e");
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_media() {
let response = support::json(r#"{
"object": "media",
"messageId": "messageId 1",
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let params: types::MessagesGetMediaParams = support::from_json(r#"{"redirect":true}"#);
let result = api.client().messages().get_media("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", params).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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/media");
assert_eq!(calls[0].query, support::pairs(&[("redirect", "true")]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn get_media_without_params() {
let response = support::json(r#"{
"object": "media",
"messageId": "messageId 1",
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().messages().get_media("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", Default::default()).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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/media");
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 edit() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let params: types::MessagesEditParams = support::from_json(r#"{"text":"text 1"}"#);
let result = api.client().messages().edit("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", params).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "PATCH");
assert_eq!(calls[0].path, "/v1/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"text":"text 1"}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn edit_is_not_repeated() {
let api = support::MockApi::start(vec![
support::Reply::status(503),
support::Reply::status(503),
])
.await;
let params: types::MessagesEditParams = support::from_json(r#"{"text":"text 1"}"#);
let error = api.client().messages().edit("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", params).await.unwrap_err();
assert_eq!(error.status(), Some(503));
assert_eq!(api.calls().await.len(), 1);
}
#[tokio::test]
async fn delete() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
let params: types::MessagesDeleteParams = support::from_json(r#"{"forEveryone":true}"#);
api.client().messages().delete("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", params).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "DELETE");
assert_eq!(calls[0].path, "/v1/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e");
assert_eq!(calls[0].query, support::pairs(&[("forEveryone", "true")]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn delete_without_params() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().messages().delete("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", Default::default()).await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "DELETE");
assert_eq!(calls[0].path, "/v1/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e");
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 react() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
let params: types::MessagesReactParams = support::from_json(r#"{"emoji":"emoji 1"}"#);
api.client().messages().react("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/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::MessagesReactParams = support::from_json(r#"{"emoji":"emoji 1"}"#);
api.client().messages().react("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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 vote() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let params: types::MessagesVoteParams = support::from_json(r#"{"options":["options 1"]}"#);
let result = api.client().messages().vote("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/vote");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"options":["options 1"]}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn vote_retries_with_one_key() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(200, response.clone())]).await;
let params: types::MessagesVoteParams = support::from_json(r#"{"options":["options 1"]}"#);
let result = api.client().messages().vote("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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 forward() {
let response = support::json(r#"{
"object": "list",
"items": [
{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": "nextCursor 1"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(202, response.clone())]).await;
let params: types::MessagesForwardParams = support::from_json(r#"{"to":["to 1"]}"#);
let result = api.client().messages().forward("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/forward");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"to":["to 1"]}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn forward_retries_with_one_key() {
let response = support::json(r#"{
"object": "list",
"items": [
{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}
],
"nextCursor": "nextCursor 1"
}"#);
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(202, response.clone())]).await;
let params: types::MessagesForwardParams = support::from_json(r#"{"to":["to 1"]}"#);
let result = api.client().messages().forward("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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 star() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().messages().star("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e").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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/star");
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 star_retries_with_one_key() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(200, response.clone())]).await;
let result = api.client().messages().star("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e").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 unstar() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().messages().unstar("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e").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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/unstar");
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 unstar_retries_with_one_key() {
let response = support::json(r#"{
"object": "message",
"id": "id 1",
"projectId": "projectId 1",
"accountId": "accountId 1",
"chatId": "chatId 1",
"chatType": "direct",
"direction": "inbound",
"source": "api",
"from": "from 1",
"to": "to 1",
"profileName": "profileName 1",
"username": "username 1",
"type": "text",
"text": "text 1",
"media": {
"url": "url 1",
"mimeType": "mimeType 1",
"filename": "filename 1",
"size": 1,
"width": 1,
"height": 1,
"durationSeconds": 1,
"gifPlayback": true,
"downloaded": true
},
"location": {
"latitude": 1.5,
"longitude": 1.5,
"name": "name 1",
"address": "address 1"
},
"contact": {
"name": "name 1",
"phone": "phone 1"
},
"contacts": [
{
"name": "name 1",
"phone": "phone 1"
}
],
"poll": {
"name": "name 1",
"options": [
{
"name": "name 1",
"voteCount": 1
}
],
"selectableCount": 1,
"voterCount": 1
},
"calendarEvent": {
"name": "name 1",
"description": "description 1",
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z",
"location": {
"name": "name 1",
"address": "address 1",
"latitude": 1.5,
"longitude": 1.5
},
"callType": "audio",
"joinUrl": "joinUrl 1",
"allowExtraGuests": true,
"cancelled": true
},
"mentions": [
"mentions 1"
],
"forwarded": true,
"forwardedManyTimes": true,
"viewOnce": true,
"starred": true,
"replyToMessageId": "replyToMessageId 1",
"replyToStoryId": "replyToStoryId 1",
"status": "queued",
"error": {
"code": "not_on_whatsapp",
"message": "message 1"
},
"metadata": {
"key": "value 1"
},
"sentAt": "2026-09-24T09:00:00.000Z",
"editedAt": "2026-09-24T09:00:00.000Z",
"deletedAt": "2026-09-24T09:00:00.000Z",
"createdAt": "2026-09-24T09:00:00.000Z",
"updatedAt": "2026-09-24T09:00:00.000Z"
}"#);
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::json(200, response.clone())]).await;
let result = api.client().messages().unstar("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e").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 add_label() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
let params: types::MessagesAddLabelParams = support::from_json(r#"{"labelId":"labelId 1"}"#);
api.client().messages().add_label("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/labels");
assert_eq!(calls[0].query, support::pairs(&[]));
support::assert_json(&calls[0].body, &support::json(r#"{"labelId":"labelId 1"}"#));
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_some_and(|key| !key.is_empty()));
}
#[tokio::test]
async fn add_label_retries_with_one_key() {
let api = support::MockApi::start(vec![support::Reply::status(503), support::Reply::status(204)]).await;
let params: types::MessagesAddLabelParams = support::from_json(r#"{"labelId":"labelId 1"}"#);
api.client().messages().add_label("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", 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 remove_label() {
let api = support::MockApi::start(vec![support::Reply::status(204)]).await;
api.client().messages().remove_label("m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e", "5").await.unwrap();
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "DELETE");
assert_eq!(calls[0].path, "/v1/messages/m17d0a9w2sqc7k3v8x1n5ybr6t4hjp2e/labels/5");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}