lark-channel 0.5.0

Lark/Feishu Channel SDK for Rust
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
use std::time::Duration;

use crate::card::{CardElementContent, CardSettings};
use crate::lark_openapi::{CardUpdateOptions, OpenApiTransport};
use crate::{CardId, Error, MessageId, Result};

use super::super::sender::{MessageSender, generate_idempotency_key};
use super::throttle::ThrottledMarkdownStream;
use super::{
    MarkdownStreamCardProfile, STREAM_ELEMENT_ID, derive_summary, validate_stream_content_states,
};

#[derive(Debug, Clone)]
enum PendingMarkdownStreamOperation {
    Content {
        content: CardElementContent,
        options: CardUpdateOptions,
    },
    Finish {
        settings: CardSettings,
        options: CardUpdateOptions,
    },
}

/// Active high-level Markdown stream backed by one CardKit entity.
///
/// Call `append` with token deltas or `set_content` with a complete snapshot,
/// then call `finish` to disable CardKit streaming mode. Dropping an unfinished
/// stream cannot perform asynchronous cleanup; Lark/Feishu closes the card
/// automatically after its platform timeout.
#[derive(Debug)]
pub struct MarkdownStream<'a, T> {
    sender: &'a MessageSender<T>,
    card_id: CardId,
    message_id: MessageId,
    profile: MarkdownStreamCardProfile,
    content: Option<CardElementContent>,
    next_sequence: u32,
    max_attempts: usize,
    finished: bool,
    pending: Option<PendingMarkdownStreamOperation>,
}

impl<'a, T> MarkdownStream<'a, T> {
    pub(super) fn new(
        sender: &'a MessageSender<T>,
        card_id: CardId,
        message_id: MessageId,
        profile: MarkdownStreamCardProfile,
        max_attempts: usize,
    ) -> Self {
        Self {
            sender,
            card_id,
            message_id,
            profile,
            content: None,
            next_sequence: 1,
            max_attempts,
            finished: false,
            pending: None,
        }
    }
}

impl<'a, T> MarkdownStream<'a, T>
where
    T: OpenApiTransport,
{
    /// Returns the message that displays this stream.
    pub fn message_id(&self) -> &MessageId {
        &self.message_id
    }

    /// Returns the CardKit entity updated by this stream.
    pub fn card_id(&self) -> &CardId {
        &self.card_id
    }

    /// Returns the accumulated generated content, excluding the placeholder.
    pub fn content(&self) -> Option<&str> {
        self.content.as_ref().map(CardElementContent::as_str)
    }

    /// Returns the sequence that will be used by the next CardKit operation.
    pub fn next_sequence(&self) -> u32 {
        self.next_sequence
    }

    /// Returns whether `finish` completed successfully.
    pub fn is_finished(&self) -> bool {
        self.finished
    }

    /// Returns whether an ambiguous remote failure left one operation awaiting
    /// an idempotent replay.
    pub fn has_pending_operation(&self) -> bool {
        self.pending.is_some()
    }

    /// Wraps this stream in a runtime-independent content update throttler.
    ///
    /// The first content update is sent immediately. Later updates that arrive
    /// before `min_update_interval` elapses are coalesced until another update
    /// reaches the interval or the caller invokes `flush` or `finish`.
    pub fn throttle(self, min_update_interval: Duration) -> ThrottledMarkdownStream<'a, T> {
        ThrottledMarkdownStream::new(self, min_update_interval)
    }

    /// Appends one delta verbatim and immediately pushes the accumulated text.
    ///
    /// Empty chunks are a no-op. This method does not merge overlapping text;
    /// producers that already hold a complete snapshot should use
    /// `set_content` instead.
    pub async fn append(&mut self, chunk: impl AsRef<str>) -> Result<()> {
        self.ensure_active()?;
        let chunk = chunk.as_ref();
        if chunk.is_empty() {
            return Ok(());
        }

        let mut content = self.content().unwrap_or_default().to_owned();
        content.push_str(chunk);
        self.update_content(content).await
    }

    /// Replaces the accumulated text with a complete snapshot and pushes it.
    pub async fn set_content(&mut self, content: impl Into<String>) -> Result<()> {
        self.ensure_active()?;
        self.update_content(content.into()).await
    }

    /// Retries the operation retained after an ambiguous remote failure.
    ///
    /// The original payload, sequence, and UUID are reused. Validation and API
    /// errors are definitive and clear the retained operation. Transport, HTTP
    /// status, and response-decoding failures preserve the operation because
    /// the remote update may already have been applied.
    pub async fn retry_pending(&mut self) -> Result<()> {
        let Some(operation) = self.pending.clone() else {
            return Ok(());
        };

        let result = match &operation {
            PendingMarkdownStreamOperation::Content { content, options } => {
                self.sender
                    .retry_transport_errors(self.max_attempts, || {
                        self.sender.client().update_card_element_content(
                            &self.card_id,
                            STREAM_ELEMENT_ID,
                            content.as_str(),
                            options.clone(),
                        )
                    })
                    .await
            }
            PendingMarkdownStreamOperation::Finish { settings, options } => {
                self.sender
                    .retry_transport_errors(self.max_attempts, || {
                        self.sender.client().update_card_settings(
                            &self.card_id,
                            settings,
                            options.clone(),
                        )
                    })
                    .await
            }
        };

        if let Err(error) = result {
            if !operation_outcome_may_be_ambiguous(&error) {
                self.pending = None;
            }
            return Err(error);
        }

        match operation {
            PendingMarkdownStreamOperation::Content { content, options } => {
                self.content = Some(content);
                self.next_sequence = options.sequence.saturating_add(1);
            }
            PendingMarkdownStreamOperation::Finish { options, .. } => {
                self.next_sequence = options.sequence.saturating_add(1);
                self.finished = true;
            }
        }
        self.pending = None;
        Ok(())
    }

    /// Disables CardKit streaming mode and updates the final preview summary.
    ///
    /// Finishing an already completed local stream is an idempotent no-op. If
    /// no content was produced, the configured empty text is pushed first.
    pub async fn finish(&mut self) -> Result<()> {
        if self.finished {
            return Ok(());
        }
        if self.pending.is_some() {
            self.retry_pending().await?;
            if self.finished {
                return Ok(());
            }
        }
        if self.content.is_none() {
            self.set_content(self.profile.empty_text.clone()).await?;
        }

        let sequence = self.next_sequence;
        let uuid = generate_idempotency_key();
        let summary = self.final_summary();
        let settings = CardSettings::new().streaming_mode(false).summary(summary);
        let options = CardUpdateOptions::new(sequence).uuid(uuid);
        self.pending = Some(PendingMarkdownStreamOperation::Finish { settings, options });
        self.retry_pending().await
    }

    async fn update_content(&mut self, content: String) -> Result<()> {
        if let Some(pending) = &self.pending {
            return match pending {
                PendingMarkdownStreamOperation::Content {
                    content: pending_content,
                    ..
                } if pending_content.as_str() == content => self.retry_pending().await,
                _ => Err(Error::Validation(
                    "retry the pending markdown stream operation before changing content"
                        .to_owned(),
                )),
            };
        }
        if self.content() == Some(content.as_str()) {
            return Ok(());
        }

        let content = validate_stream_content_states(&content, &self.profile)?;
        let sequence = self.next_sequence;
        let uuid = generate_idempotency_key();
        let options = CardUpdateOptions::new(sequence).uuid(uuid);
        self.pending = Some(PendingMarkdownStreamOperation::Content { content, options });
        self.retry_pending().await
    }

    fn ensure_active(&self) -> Result<()> {
        if self.finished {
            return Err(Error::Validation(
                "markdown stream is already finished".to_owned(),
            ));
        }
        Ok(())
    }

    fn final_summary(&self) -> String {
        self.profile.final_summary.clone().unwrap_or_else(|| {
            derive_summary(self.content().unwrap_or(self.profile.empty_text.as_str()))
        })
    }

    pub(super) fn validate_content(&self, content: &str) -> Result<()> {
        validate_stream_content_states(content, &self.profile).map(|_| ())
    }

    pub(super) fn has_pending_finish(&self) -> bool {
        matches!(
            self.pending,
            Some(PendingMarkdownStreamOperation::Finish { .. })
        )
    }

    pub(super) fn pending_content(&self) -> Option<&str> {
        match &self.pending {
            Some(PendingMarkdownStreamOperation::Content { content, .. }) => Some(content.as_str()),
            _ => None,
        }
    }
}

fn operation_outcome_may_be_ambiguous(error: &Error) -> bool {
    matches!(
        error,
        Error::Transport(_) | Error::HttpStatus { .. } | Error::Serde(_)
    )
}

#[cfg(test)]
mod tests {
    use serde_json::{Value, json};

    use super::*;
    use crate::lark_openapi::{HttpMethod, HttpResponse};
    use crate::message::streaming::STREAM_ELEMENT_ID;
    use crate::message::streaming::test_support::{
        FakeTransport, assert_card_content_update, block_on, card_response, message_response,
        ok_response, token_response,
    };
    use crate::{CardStreamingPlatformValues, ChannelConfig, Recipient};

    #[test]
    fn streams_markdown_message_with_accumulated_content_and_sequences() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157313"),
            message_response("om_stream"),
            ok_response(),
            ok_response(),
            ok_response(),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);
        let config =
            crate::CardStreamingConfig::new().print_step(CardStreamingPlatformValues::new(2));

        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .initial_text("H")
                .streaming_summary("Working")
                .streaming_config(config)
                .uuid("stream-message-1")
                .start(),
        )
        .expect("started stream");

        assert_eq!(
            stream.message_id(),
            &crate::MessageId("om_stream".to_owned())
        );
        assert_eq!(stream.card_id().as_str(), "7355372766134157313");
        assert_eq!(stream.content(), None);
        assert_eq!(stream.next_sequence(), 1);

        block_on(stream.append("共 3")).expect("first update");
        block_on(stream.append("3 条")).expect("second update");
        assert_eq!(stream.content(), Some("共 33 条"));
        assert_eq!(stream.next_sequence(), 3);

        let calls_before_duplicate = transport.calls().len();
        block_on(stream.set_content("共 33 条")).expect("duplicate snapshot is a no-op");
        assert_eq!(transport.calls().len(), calls_before_duplicate);

        block_on(stream.finish()).expect("finished stream");
        assert!(stream.is_finished());
        assert_eq!(stream.next_sequence(), 4);

        let calls_before_second_finish = transport.calls().len();
        block_on(stream.finish()).expect("second finish is a no-op");
        assert_eq!(transport.calls().len(), calls_before_second_finish);

        let error = block_on(stream.append("late")).expect_err("finished stream rejects updates");
        assert!(matches!(error, Error::Validation(_)));

        let calls = transport.calls();
        assert_eq!(calls.len(), 6);

        let initial_card: Value = serde_json::from_str(
            calls[1].body["data"]
                .as_str()
                .expect("serialized initial card"),
        )
        .expect("initial card JSON");
        assert_eq!(initial_card["config"]["streaming_mode"], true);
        assert_eq!(initial_card["config"]["summary"]["content"], "Working");
        assert_eq!(
            initial_card["config"]["streaming_config"]["print_step"]["default"],
            2
        );
        assert_eq!(
            initial_card["body"]["elements"][0]["element_id"],
            STREAM_ELEMENT_ID
        );
        assert_eq!(initial_card["body"]["elements"][0]["content"], "H");

        assert_eq!(calls[2].body["uuid"], "stream-message-1");
        let reference: Value = serde_json::from_str(
            calls[2].body["content"]
                .as_str()
                .expect("card reference content"),
        )
        .expect("card reference JSON");
        assert_eq!(reference["data"]["card_id"], "7355372766134157313");

        assert_card_content_update(&calls[3], 1, "共 3");
        assert_card_content_update(&calls[4], 2, "共 33 条");
        assert_eq!(calls[5].method, HttpMethod::Patch);
        assert_eq!(calls[5].body["sequence"], 3);
        let settings: Value = serde_json::from_str(
            calls[5].body["settings"]
                .as_str()
                .expect("serialized settings"),
        )
        .expect("settings JSON");
        assert_eq!(settings["config"]["streaming_mode"], false);
        assert_eq!(settings["config"]["summary"]["content"], "共 33 条");
    }

    #[test]
    fn empty_reply_pushes_fallback_before_finishing_in_thread() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157314"),
            message_response("om_reply"),
            ok_response(),
            ok_response(),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);

        let mut stream = block_on(
            sender
                .markdown_stream_reply(crate::MessageId("om_parent".to_owned()))
                .reply_in_thread(true)
                .empty_text("No response")
                .final_summary("Done")
                .start(),
        )
        .expect("started reply stream");
        block_on(stream.finish()).expect("finished empty stream");

        let calls = transport.calls();
        assert_eq!(
            calls[2].url.as_str(),
            "https://open.feishu.cn/open-apis/im/v1/messages/om_parent/reply"
        );
        assert_eq!(calls[2].body["reply_in_thread"], true);
        assert_card_content_update(&calls[3], 1, "No response");
        assert_eq!(calls[4].body["sequence"], 2);
        let settings: Value = serde_json::from_str(
            calls[4].body["settings"]
                .as_str()
                .expect("serialized settings"),
        )
        .expect("settings JSON");
        assert_eq!(settings["config"]["summary"]["content"], "Done");
    }

    #[test]
    fn retries_transport_failures_with_the_same_sequence_and_uuid() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157315")),
            Ok(message_response("om_retry")),
            Err(Error::Transport("temporary network error".to_owned())),
            Ok(ok_response()),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);

        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .max_attempts(2)
                .start(),
        )
        .expect("started stream");
        block_on(stream.append("hello")).expect("retried update");

        let calls = transport.calls();
        assert_eq!(calls.len(), 5);
        assert_eq!(calls[3].body["sequence"], 1);
        assert_eq!(calls[4].body["sequence"], 1);
        assert_eq!(calls[3].body["uuid"], calls[4].body["uuid"]);
        assert!(
            calls[3].body["uuid"]
                .as_str()
                .is_some_and(|uuid| { uuid.starts_with("lc-") && uuid.chars().count() <= 64 })
        );
        assert_eq!(stream.content(), Some("hello"));
        assert_eq!(stream.next_sequence(), 2);
    }

    #[test]
    fn pending_content_update_reuses_operation_and_blocks_different_content() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157319")),
            Ok(message_response("om_pending_content")),
            Err(Error::Transport("content response lost".to_owned())),
            Ok(ok_response()),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);
        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .max_attempts(1)
                .start(),
        )
        .expect("started stream");

        let error = block_on(stream.append("hello")).expect_err("ambiguous update failure");
        assert!(matches!(error, Error::Transport(_)));
        assert!(stream.has_pending_operation());
        assert_eq!(stream.content(), None);
        assert_eq!(stream.next_sequence(), 1);

        let calls_before_different_content = transport.calls().len();
        let error = block_on(stream.append("world")).expect_err("pending update must be resolved");
        assert!(matches!(error, Error::Validation(_)));
        assert_eq!(transport.calls().len(), calls_before_different_content);
        assert!(stream.has_pending_operation());

        block_on(stream.append("hello")).expect("same logical update is replayed");
        assert!(!stream.has_pending_operation());
        assert_eq!(stream.content(), Some("hello"));
        assert_eq!(stream.next_sequence(), 2);

        let calls = transport.calls();
        assert_eq!(calls[3].body["sequence"], 1);
        assert_eq!(calls[4].body["sequence"], 1);
        assert_eq!(calls[3].body["uuid"], calls[4].body["uuid"]);
        assert_eq!(calls[3].body["content"], calls[4].body["content"]);
    }

    #[test]
    fn pending_finish_reuses_operation_until_acknowledged() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157320")),
            Ok(message_response("om_pending_finish")),
            Ok(ok_response()),
            Err(Error::Transport("finish response lost".to_owned())),
            Ok(ok_response()),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);
        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .max_attempts(1)
                .start(),
        )
        .expect("started stream");
        block_on(stream.set_content("done")).expect("content update");

        let error = block_on(stream.finish()).expect_err("ambiguous finish failure");
        assert!(matches!(error, Error::Transport(_)));
        assert!(stream.has_pending_operation());
        assert!(!stream.is_finished());
        assert_eq!(stream.next_sequence(), 2);

        block_on(stream.finish()).expect("replayed finish");
        assert!(!stream.has_pending_operation());
        assert!(stream.is_finished());
        assert_eq!(stream.next_sequence(), 3);

        let calls = transport.calls();
        assert_eq!(calls[4].body["sequence"], 2);
        assert_eq!(calls[5].body["sequence"], 2);
        assert_eq!(calls[4].body["uuid"], calls[5].body["uuid"]);
        assert_eq!(calls[4].body["settings"], calls[5].body["settings"]);
    }

    #[test]
    fn http_status_during_finish_retains_the_original_operation() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157323"),
            message_response("om_http_finish"),
            ok_response(),
            HttpResponse::json(503, Value::Null),
            ok_response(),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);
        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .start(),
        )
        .expect("started stream");
        block_on(stream.set_content("done")).expect("content update");

        let error = block_on(stream.finish()).expect_err("ambiguous HTTP finish failure");
        assert!(matches!(error, Error::HttpStatus { status: 503 }));
        assert!(stream.has_pending_operation());

        block_on(stream.finish()).expect("replayed finish");
        assert!(stream.is_finished());
        let calls = transport.calls();
        assert_eq!(calls[4].body["sequence"], calls[5].body["sequence"]);
        assert_eq!(calls[4].body["uuid"], calls[5].body["uuid"]);
        assert_eq!(calls[4].body["settings"], calls[5].body["settings"]);
    }

    #[test]
    fn api_errors_do_not_retry_or_advance_local_state() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157316"),
            message_response("om_api_error"),
            HttpResponse::json(200, json!({ "code": 230099, "msg": "stale sequence" })),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);

        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .start(),
        )
        .expect("started stream");
        let error = block_on(stream.append("hello")).expect_err("API error");

        assert!(matches!(error, Error::Api { code: 230099, .. }));
        assert_eq!(transport.calls().len(), 4);
        assert_eq!(stream.content(), None);
        assert_eq!(stream.next_sequence(), 1);
        assert!(!stream.has_pending_operation());
    }

    #[test]
    fn http_status_errors_retain_pending_operations_for_explicit_replay() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157322"),
            message_response("om_http_error"),
            HttpResponse::json(503, Value::Null),
            ok_response(),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);

        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .start(),
        )
        .expect("started stream");
        let error = block_on(stream.append("hello")).expect_err("HTTP status error");

        assert!(matches!(error, Error::HttpStatus { status: 503 }));
        assert_eq!(transport.calls().len(), 4);
        assert_eq!(stream.content(), None);
        assert_eq!(stream.next_sequence(), 1);
        assert!(stream.has_pending_operation());

        block_on(stream.retry_pending()).expect("replayed content update");
        assert_eq!(stream.content(), Some("hello"));
        assert_eq!(stream.next_sequence(), 2);
        assert!(!stream.has_pending_operation());

        let calls = transport.calls();
        assert_eq!(calls[3].body["sequence"], calls[4].body["sequence"]);
        assert_eq!(calls[3].body["uuid"], calls[4].body["uuid"]);
        assert_eq!(calls[3].body["content"], calls[4].body["content"]);
    }

    #[test]
    fn response_decoding_errors_retain_content_and_finish_operations() {
        let transport = FakeTransport::new(vec![
            Ok(token_response()),
            Ok(card_response("7355372766134157324")),
            Ok(message_response("om_serde_error")),
            Err(response_decoding_error()),
            Ok(ok_response()),
            Err(response_decoding_error()),
            Ok(ok_response()),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);
        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .max_attempts(1)
                .start(),
        )
        .expect("started stream");

        let error = block_on(stream.set_content("done")).expect_err("ambiguous content response");
        assert!(matches!(error, Error::Serde(_)));
        assert!(stream.has_pending_operation());
        block_on(stream.retry_pending()).expect("replayed content update");

        let error = block_on(stream.finish()).expect_err("ambiguous finish response");
        assert!(matches!(error, Error::Serde(_)));
        assert!(stream.has_pending_operation());
        block_on(stream.retry_pending()).expect("replayed finish");
        assert!(stream.is_finished());

        let calls = transport.calls();
        assert_eq!(calls[3].body["sequence"], calls[4].body["sequence"]);
        assert_eq!(calls[3].body["uuid"], calls[4].body["uuid"]);
        assert_eq!(calls[3].body["content"], calls[4].body["content"]);
        assert_eq!(calls[5].body["sequence"], calls[6].body["sequence"]);
        assert_eq!(calls[5].body["uuid"], calls[6].body["uuid"]);
        assert_eq!(calls[5].body["settings"], calls[6].body["settings"]);
    }

    #[test]
    fn oversized_updates_fail_without_advancing_sequence_or_content() {
        let transport = FakeTransport::http(vec![
            token_response(),
            card_response("7355372766134157317"),
            message_response("om_oversized"),
        ]);
        let client = crate::lark_openapi::OpenApiClient::new(
            ChannelConfig::new("cli_a", "secret"),
            transport.clone(),
        );
        let sender = MessageSender::new(client);

        let mut stream = block_on(
            sender
                .markdown_stream_message(Recipient::Chat("oc_123".to_owned()))
                .start(),
        )
        .expect("started stream");
        let error = block_on(stream.set_content("x".repeat(31_000)))
            .expect_err("whole card exceeds 30 KiB");

        assert!(matches!(error, Error::Validation(_)));
        assert_eq!(transport.calls().len(), 3);
        assert_eq!(stream.content(), None);
        assert_eq!(stream.next_sequence(), 1);
    }

    fn response_decoding_error() -> Error {
        serde_json::from_str::<Value>("{")
            .expect_err("invalid JSON")
            .into()
    }
}