heartbit-core 2026.613.1

The Rust agentic framework — agents, tools, LLM providers, memory, evaluation.
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
use std::fmt::Write as _;
use std::future::Future;
use std::pin::Pin;
use std::time::SystemTime;

use base64::Engine;
use hmac::{Hmac, Mac};
use serde_json::json;
use sha1::Sha1;

use crate::error::Error;
use crate::llm::types::ToolDefinition;
use crate::tool::{Tool, ToolOutput};

const X_API_URL: &str = "https://api.twitter.com/2/tweets";
const MAX_TWEET_LENGTH: usize = 280;

type HmacSha1 = Hmac<Sha1>;

/// Per-tenant X/Twitter credentials for OAuth 1.0a signing.
#[derive(Clone)]
pub struct TwitterCredentials {
    /// OAuth 1.0a consumer (app) key.
    pub consumer_key: String,
    /// OAuth 1.0a consumer (app) secret.
    pub consumer_secret: String,
    /// Per-user access token.
    pub access_token: String,
    /// Per-user access token secret.
    pub access_token_secret: String,
}

impl std::fmt::Debug for TwitterCredentials {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TwitterCredentials")
            .field("consumer_key", &"[REDACTED]")
            .field("consumer_secret", &"[REDACTED]")
            .field("access_token", &"[REDACTED]")
            .field("access_token_secret", &"[REDACTED]")
            .finish()
    }
}

/// Builtin tool for posting tweets to X/Twitter via API v2.
///
/// Uses OAuth 1.0a signing with per-tenant credentials injected at runtime.
/// Only instantiated when `TwitterCredentials` are provided (multi-tenant).
pub struct TwitterPostTool {
    credentials: TwitterCredentials,
    client: reqwest::Client,
    tweet_url: String,
    media_upload_url: String,
    media_meta_url: String,
}

impl TwitterPostTool {
    /// Create a `TwitterPostTool` with the given credentials.
    ///
    /// Panics if the HTTP client cannot be built. Use [`TwitterPostTool::try_new`]
    /// if you need to handle the error.
    pub fn new(credentials: TwitterCredentials) -> Self {
        Self::try_new(credentials).expect("failed to build reqwest client")
    }

    /// Create a `TwitterPostTool` with the given credentials, returning `Err` on failure.
    ///
    /// Returns `Err` if the underlying HTTP client cannot be constructed
    /// (e.g., TLS initialisation failure).
    pub fn try_new(credentials: TwitterCredentials) -> Result<Self, crate::error::Error> {
        let client = crate::http::vendor_client_builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .map_err(|e| {
                crate::error::Error::Agent(format!("failed to build reqwest client: {e}"))
            })?;
        Ok(Self {
            credentials,
            client,
            tweet_url: X_API_URL.to_string(),
            media_upload_url: "https://upload.twitter.com/1.1/media/upload.json".to_string(),
            media_meta_url: "https://upload.twitter.com/1.1/media/metadata/create.json".to_string(),
        })
    }
}

#[cfg(test)]
impl TwitterPostTool {
    /// Test-only constructor that allows injecting endpoint URLs for wiremock.
    pub(crate) fn new_with_base_urls(
        credentials: TwitterCredentials,
        tweet_url: String,
        media_upload_url: String,
        media_meta_url: String,
    ) -> Self {
        let client = crate::http::vendor_client_builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .expect("test client builds");
        Self {
            credentials,
            client,
            tweet_url,
            media_upload_url,
            media_meta_url,
        }
    }
}

/// Percent-encode a string per RFC 5849 (OAuth 1.0a).
fn percent_encode(s: &str) -> String {
    let mut encoded = String::with_capacity(s.len() * 2);
    for byte in s.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
                encoded.push(byte as char);
            }
            _ => {
                // write! to String is infallible — avoids temporary allocation from format!
                let _ = write!(encoded, "%{byte:02X}");
            }
        }
    }
    encoded
}

/// Build the OAuth 1.0a Authorization header for a POST request.
fn build_oauth_header(
    url: &str,
    consumer_key: &str,
    consumer_secret: &str,
    access_token: &str,
    access_token_secret: &str,
    nonce: &str,
    timestamp: u64,
) -> Result<String, Error> {
    let oauth_params = [
        ("oauth_consumer_key", consumer_key),
        ("oauth_nonce", nonce),
        ("oauth_signature_method", "HMAC-SHA1"),
        ("oauth_timestamp", &timestamp.to_string()),
        ("oauth_token", access_token),
        ("oauth_version", "1.0"),
    ];

    // Build parameter string (sorted by key)
    let param_string: String = oauth_params
        .iter()
        .map(|(k, v)| format!("{}={}", percent_encode(k), percent_encode(v)))
        .collect::<Vec<_>>()
        .join("&");

    // Build signature base string: METHOD&url&params
    let base_string = format!(
        "POST&{}&{}",
        percent_encode(url),
        percent_encode(&param_string),
    );

    // Sign with HMAC-SHA1
    let signing_key = format!(
        "{}&{}",
        percent_encode(consumer_secret),
        percent_encode(access_token_secret),
    );

    let mut mac = HmacSha1::new_from_slice(signing_key.as_bytes())
        .map_err(|e| Error::Agent(format!("HMAC key error: {e}")))?;
    mac.update(base_string.as_bytes());
    let signature = base64::engine::general_purpose::STANDARD.encode(mac.finalize().into_bytes());

    // Build Authorization header
    Ok(format!(
        "OAuth oauth_consumer_key=\"{}\", \
         oauth_nonce=\"{}\", \
         oauth_signature=\"{}\", \
         oauth_signature_method=\"HMAC-SHA1\", \
         oauth_timestamp=\"{}\", \
         oauth_token=\"{}\", \
         oauth_version=\"1.0\"",
        percent_encode(consumer_key),
        percent_encode(nonce),
        percent_encode(&signature),
        timestamp,
        percent_encode(access_token),
    ))
}

impl TwitterPostTool {
    /// Fetch image bytes from `media_url`, upload to X v1.1 media endpoint, and
    /// (optionally) attach alt text. Returns the `media_id_string` to be referenced
    /// in the tweet POST.
    async fn upload_media(
        &self,
        media_url: &str,
        media_alt_text: Option<&str>,
    ) -> Result<String, Error> {
        // Step A: Fetch the image bytes (HTTP GET, ≤5 MB)
        let bytes_resp = self
            .client
            .get(media_url)
            .send()
            .await
            .map_err(|e| Error::Agent(format!("media fetch failed: {e}")))?;
        let status = bytes_resp.status();
        if !status.is_success() {
            return Err(Error::Agent(format!(
                "media fetch returned status {}",
                status.as_u16()
            )));
        }
        // SECURITY (F-NET-1): cap the media body. The previous `.bytes().await`
        // buffered the ENTIRE response before checking the 5 MB limit, so a
        // hostile (or compromised) media_url serving a multi-GB body would OOM
        // the process. `read_body_capped` streams and stops at the cap. We read
        // one byte past the limit so an over-size body is detected (truncated).
        const MEDIA_LIMIT: usize = 5 * 1024 * 1024;
        let (body, truncated) = crate::http::read_body_capped(bytes_resp, MEDIA_LIMIT + 1)
            .await
            .map_err(|e| Error::Agent(format!("media body read failed: {e}")))?;
        if truncated || body.len() > MEDIA_LIMIT {
            return Err(Error::Agent(format!(
                "media exceeds 5 MB limit (read at least {} bytes)",
                body.len()
            )));
        }

        // Step B: Upload to X v1.1 media endpoint via multipart/form-data
        let timestamp = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map_err(|e| Error::Agent(format!("system time error: {e}")))?
            .as_secs();
        let nonce = format!("{}{}", timestamp, &timestamp.to_string()[..6]);

        let auth_header = build_oauth_header(
            &self.media_upload_url,
            &self.credentials.consumer_key,
            &self.credentials.consumer_secret,
            &self.credentials.access_token,
            &self.credentials.access_token_secret,
            &nonce,
            timestamp,
        )?;

        let form = reqwest::multipart::Form::new().part(
            "media",
            reqwest::multipart::Part::bytes(body.to_vec()).file_name("media"),
        );
        let response = self
            .client
            .post(&self.media_upload_url)
            .header("Authorization", auth_header)
            .multipart(form)
            .send()
            .await
            .map_err(|e| Error::Agent(format!("media upload failed: {e}")))?;
        let status = response.status();
        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(Error::Agent(format!(
                "media upload returned status {}: {body}",
                status.as_u16()
            )));
        }
        let parsed: serde_json::Value = response
            .json()
            .await
            .map_err(|e| Error::Agent(format!("media upload parse failed: {e}")))?;
        let media_id_string = parsed
            .get("media_id_string")
            .and_then(|v| v.as_str())
            .ok_or_else(|| Error::Agent("media upload returned no media_id_string".into()))?
            .to_string();

        // Step C (optional): attach alt text via metadata/create
        if let Some(alt) = media_alt_text {
            let meta_timestamp = SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .map_err(|e| Error::Agent(format!("system time error: {e}")))?
                .as_secs();
            let meta_nonce = format!("{}{}", meta_timestamp, &meta_timestamp.to_string()[..6]);
            let meta_auth = build_oauth_header(
                &self.media_meta_url,
                &self.credentials.consumer_key,
                &self.credentials.consumer_secret,
                &self.credentials.access_token,
                &self.credentials.access_token_secret,
                &meta_nonce,
                meta_timestamp,
            )?;
            let body = json!({
                "media_id": media_id_string,
                "alt_text": {"text": alt}
            });
            let _ = self
                .client
                .post(&self.media_meta_url)
                .header("Authorization", meta_auth)
                .json(&body)
                .send()
                .await
                .map_err(|e| Error::Agent(format!("alt-text attach failed: {e}")))?;
            // Don't fail if alt-text attach fails — the media itself is already up
            // and the tweet can still post.
        }

        Ok(media_id_string)
    }
}

impl Tool for TwitterPostTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "twitter_post".into(),
            description: "Post a tweet to X/Twitter. Maximum 280 characters. Optionally attaches one image via media_url with an accessibility description via media_alt_text.".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "text": {
                        "type": "string",
                        "description": "The tweet text to post (max 280 characters)"
                    },
                    "media_url": {
                        "type": "string",
                        "description": "Optional. Public URL of one image to attach (≤5 MB, JPEG/PNG/WebP/GIF). HTTPS recommended."
                    },
                    "media_alt_text": {
                        "type": "string",
                        "description": "Optional. Accessibility description for the image (≤1000 chars). Ignored if media_url is absent.",
                        "maxLength": 1000
                    }
                },
                "required": ["text"]
            }),
        }
    }

    fn execute(
        &self,
        _ctx: &crate::ExecutionContext,
        input: serde_json::Value,
    ) -> Pin<Box<dyn Future<Output = Result<ToolOutput, Error>> + Send + '_>> {
        Box::pin(async move {
            let text = input
                .get("text")
                .and_then(|v| v.as_str())
                .ok_or_else(|| Error::Agent("text is required".into()))?;

            if text.is_empty() {
                return Ok(ToolOutput::error("text must not be empty"));
            }

            let char_count = text.chars().count();
            if char_count > MAX_TWEET_LENGTH {
                return Ok(ToolOutput::error(format!(
                    "Tweet exceeds {MAX_TWEET_LENGTH} characters (got {char_count}). \
                     Please shorten your tweet."
                )));
            }

            // Optional media handling: upload first, then attach to the tweet body.
            let media_url = input.get("media_url").and_then(|v| v.as_str());
            let media_alt_text = input.get("media_alt_text").and_then(|v| v.as_str());

            let media_id_string: Option<String> = if let Some(url) = media_url {
                match self.upload_media(url, media_alt_text).await {
                    Ok(id) => Some(id),
                    Err(e) => return Ok(ToolOutput::error(format!("media upload failed: {e}"))),
                }
            } else {
                None
            };

            // Generate OAuth nonce and timestamp
            let timestamp = SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .map_err(|e| Error::Agent(format!("system time error: {e}")))?
                .as_secs();

            // UUID v4 provides cryptographically random nonce (required by RFC 5849)
            let nonce = uuid::Uuid::new_v4().to_string().replace('-', "");

            let auth_header = build_oauth_header(
                &self.tweet_url,
                &self.credentials.consumer_key,
                &self.credentials.consumer_secret,
                &self.credentials.access_token,
                &self.credentials.access_token_secret,
                &nonce,
                timestamp,
            )?;

            let body = if let Some(ref id) = media_id_string {
                json!({
                    "text": text,
                    "media": {"media_ids": [id]}
                })
            } else {
                json!({"text": text})
            };

            let response = self
                .client
                .post(&self.tweet_url)
                .header("Authorization", &auth_header)
                .header("Content-Type", "application/json")
                .json(&body)
                .send()
                .await
                .map_err(|e| Error::Agent(format!("X API request failed: {e}")))?;

            let status = response.status();
            // SECURITY (F-NET-1): cap response body. Tweet responses are tiny
            // (well under 1 MiB) — 256 KiB is generous and bounds memory.
            let (body_bytes, _truncated) = crate::http::read_body_capped(response, 256 * 1024)
                .await
                .map_err(|e| Error::Agent(format!("Failed to read X API response: {e}")))?;
            let response_body: serde_json::Value = serde_json::from_slice(&body_bytes)
                .map_err(|e| Error::Agent(format!("Failed to parse X API response: {e}")))?;

            if !status.is_success() {
                let detail = response_body
                    .get("detail")
                    .and_then(|v| v.as_str())
                    .or_else(|| response_body.get("title").and_then(|v| v.as_str()))
                    .unwrap_or("Unknown error");
                return Ok(ToolOutput::error(format!(
                    "X API error (HTTP {}): {detail}",
                    status.as_u16()
                )));
            }

            // Extract tweet ID from response
            let tweet_id = response_body
                .get("data")
                .and_then(|d| d.get("id"))
                .and_then(|v| v.as_str())
                .unwrap_or("unknown");

            Ok(ToolOutput::success(format!(
                "Tweet posted successfully!\n\
                 Tweet ID: {tweet_id}\n\
                 URL: https://x.com/i/status/{tweet_id}\n\
                 Text: {text}"
            )))
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_credentials() -> TwitterCredentials {
        TwitterCredentials {
            consumer_key: "test_consumer_key".into(),
            consumer_secret: "test_consumer_secret".into(),
            access_token: "test_access_token".into(),
            access_token_secret: "test_access_token_secret".into(),
        }
    }

    #[test]
    fn definition_has_correct_name() {
        let tool = TwitterPostTool::new(test_credentials());
        assert_eq!(tool.definition().name, "twitter_post");
    }

    #[test]
    fn definition_requires_text() {
        let tool = TwitterPostTool::new(test_credentials());
        let schema = &tool.definition().input_schema;
        let required = schema["required"].as_array().unwrap();
        assert_eq!(required.len(), 1);
        assert_eq!(required[0], "text");
    }

    #[test]
    fn percent_encode_unreserved() {
        assert_eq!(percent_encode("abc123"), "abc123");
        assert_eq!(
            percent_encode("hello-world_test.v2~"),
            "hello-world_test.v2~"
        );
    }

    #[test]
    fn percent_encode_reserved() {
        assert_eq!(percent_encode("hello world"), "hello%20world");
        assert_eq!(percent_encode("a&b=c"), "a%26b%3Dc");
        assert_eq!(percent_encode("100%"), "100%25");
    }

    #[test]
    fn percent_encode_special_chars() {
        assert_eq!(percent_encode("/"), "%2F");
        assert_eq!(percent_encode(":"), "%3A");
        assert_eq!(percent_encode("@"), "%40");
    }

    #[test]
    fn build_oauth_header_produces_valid_format() {
        let header = build_oauth_header(
            "https://api.twitter.com/2/tweets",
            "consumer_key",
            "consumer_secret",
            "access_token",
            "access_token_secret",
            "testnonce123",
            1234567890,
        )
        .unwrap();

        assert!(header.starts_with("OAuth "));
        assert!(header.contains("oauth_consumer_key=\"consumer_key\""));
        assert!(header.contains("oauth_nonce=\"testnonce123\""));
        assert!(header.contains("oauth_signature_method=\"HMAC-SHA1\""));
        assert!(header.contains("oauth_timestamp=\"1234567890\""));
        assert!(header.contains("oauth_token=\"access_token\""));
        assert!(header.contains("oauth_version=\"1.0\""));
        assert!(header.contains("oauth_signature=\""));
    }

    #[test]
    fn build_oauth_header_signature_is_deterministic() {
        let h1 = build_oauth_header(X_API_URL, "ck", "cs", "at", "ats", "nonce", 1000).unwrap();
        let h2 = build_oauth_header(X_API_URL, "ck", "cs", "at", "ats", "nonce", 1000).unwrap();
        assert_eq!(h1, h2);
    }

    #[test]
    fn build_oauth_header_different_nonce_produces_different_signature() {
        let h1 = build_oauth_header(X_API_URL, "ck", "cs", "at", "ats", "nonce1", 1000).unwrap();
        let h2 = build_oauth_header(X_API_URL, "ck", "cs", "at", "ats", "nonce2", 1000).unwrap();
        assert_ne!(h1, h2);
    }

    #[tokio::test]
    async fn rejects_empty_text() {
        let tool = TwitterPostTool::new(test_credentials());
        let result = tool
            .execute(&crate::ExecutionContext::default(), json!({"text": ""}))
            .await
            .unwrap();
        assert!(result.is_error);
        assert!(result.content.contains("must not be empty"));
    }

    #[tokio::test]
    async fn rejects_text_too_long() {
        let tool = TwitterPostTool::new(test_credentials());
        let long = "a".repeat(281);
        let result = tool
            .execute(&crate::ExecutionContext::default(), json!({"text": long}))
            .await
            .unwrap();
        assert!(result.is_error);
        assert!(result.content.contains("exceeds 280 characters"));
    }

    #[tokio::test]
    async fn rejects_missing_text() {
        let tool = TwitterPostTool::new(test_credentials());
        let result = tool
            .execute(&crate::ExecutionContext::default(), json!({}))
            .await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("text is required"), "got: {err}");
    }

    #[test]
    fn credentials_debug_redacts_secrets() {
        let creds = test_credentials();
        let debug = format!("{creds:?}");
        assert!(debug.contains("[REDACTED]"));
        assert!(!debug.contains("test_consumer_key"));
        assert!(!debug.contains("test_consumer_secret"));
    }

    #[tokio::test]
    async fn accepts_280_chars() {
        // 280 chars should pass validation (will fail at HTTP level, but that's expected)
        let tool = TwitterPostTool::new(test_credentials());
        let text = "a".repeat(280);
        let result = tool
            .execute(&crate::ExecutionContext::default(), json!({"text": text}))
            .await;
        // Should not be a validation error — will fail at HTTP level
        match result {
            Ok(output) => {
                // Network error is fine, but should NOT be a validation error
                if output.is_error {
                    assert!(
                        !output.content.contains("exceeds"),
                        "280 chars should not be rejected: {}",
                        output.content
                    );
                }
            }
            Err(_) => {
                // Network error is expected with fake credentials
            }
        }
    }

    #[tokio::test]
    async fn post_with_media_url_and_alt_text() {
        use wiremock::matchers::{method, path as wm_path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;

        // Stub the media bytes URL (the image)
        Mock::given(method("GET"))
            .and(wm_path("/test-image.png"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_bytes(vec![0u8; 1024])
                    .insert_header("Content-Type", "image/png"),
            )
            .mount(&server)
            .await;

        // Stub the media upload endpoint
        Mock::given(method("POST"))
            .and(wm_path("/1.1/media/upload.json"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "media_id_string": "999888777",
                "media_id": 999888777u64
            })))
            .mount(&server)
            .await;

        // Stub the metadata/create endpoint (alt text)
        Mock::given(method("POST"))
            .and(wm_path("/1.1/media/metadata/create.json"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&server)
            .await;

        // Stub the tweet POST endpoint
        Mock::given(method("POST"))
            .and(wm_path("/2/tweets"))
            .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({
                "data": {"id": "5555"}
            })))
            .mount(&server)
            .await;

        let media_url = format!("{}/test-image.png", server.uri());
        let tool = TwitterPostTool::new_with_base_urls(
            test_credentials(),
            format!("{}/2/tweets", server.uri()),
            format!("{}/1.1/media/upload.json", server.uri()),
            format!("{}/1.1/media/metadata/create.json", server.uri()),
        );
        let ctx = crate::ExecutionContext::default();
        let input = json!({
            "text": "look at this",
            "media_url": media_url,
            "media_alt_text": "a square of zeros"
        });
        let result = tool.execute(&ctx, input).await.expect("ok");
        assert!(!result.is_error);
        assert!(result.content.contains("5555"));
    }

    #[tokio::test]
    async fn post_text_only_still_works_without_media_fields() {
        use wiremock::matchers::{method, path as wm_path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(wm_path("/2/tweets"))
            .respond_with(
                ResponseTemplate::new(201)
                    .set_body_json(serde_json::json!({"data": {"id": "111"}})),
            )
            .mount(&server)
            .await;

        let tool = TwitterPostTool::new_with_base_urls(
            test_credentials(),
            format!("{}/2/tweets", server.uri()),
            format!("{}/1.1/media/upload.json", server.uri()),
            format!("{}/1.1/media/metadata/create.json", server.uri()),
        );
        let ctx = crate::ExecutionContext::default();
        let input = json!({"text": "no media"});
        let result = tool.execute(&ctx, input).await.expect("ok");
        assert!(!result.is_error);
    }

    #[tokio::test]
    async fn post_rejects_oversized_media() {
        use wiremock::matchers::{method, path as wm_path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(wm_path("/big.png"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_bytes(vec![0u8; 6 * 1024 * 1024])
                    .insert_header("Content-Type", "image/png"),
            )
            .mount(&server)
            .await;

        let tool = TwitterPostTool::new_with_base_urls(
            test_credentials(),
            format!("{}/2/tweets", server.uri()),
            format!("{}/1.1/media/upload.json", server.uri()),
            format!("{}/1.1/media/metadata/create.json", server.uri()),
        );
        let ctx = crate::ExecutionContext::default();
        let media_url = format!("{}/big.png", server.uri());
        let input = json!({
            "text": "won't fit",
            "media_url": media_url
        });
        let result = tool
            .execute(&ctx, input)
            .await
            .expect("Tool::execute returns Ok");
        assert!(result.is_error);
        assert!(result.content.contains("5 MB") || result.content.contains("exceeds"));
    }

    #[tokio::test]
    async fn post_handles_404_on_media_url() {
        use wiremock::matchers::{method, path as wm_path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(wm_path("/missing.png"))
            .respond_with(ResponseTemplate::new(404))
            .mount(&server)
            .await;

        let tool = TwitterPostTool::new_with_base_urls(
            test_credentials(),
            format!("{}/2/tweets", server.uri()),
            format!("{}/1.1/media/upload.json", server.uri()),
            format!("{}/1.1/media/metadata/create.json", server.uri()),
        );
        let ctx = crate::ExecutionContext::default();
        let media_url = format!("{}/missing.png", server.uri());
        let input = json!({
            "text": "broken link",
            "media_url": media_url
        });
        let result = tool
            .execute(&ctx, input)
            .await
            .expect("Tool::execute returns Ok");
        assert!(result.is_error);
        assert!(result.content.contains("404") || result.content.contains("media fetch"));
    }
}