1use serde::{Deserialize, Serialize};
19
20use crate::artifact::Artifact;
21
22#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
31pub struct TaskId(pub String);
32
33impl TaskId {
34 #[must_use]
39 pub fn new(s: impl Into<String>) -> Self {
40 Self(s.into())
41 }
42
43 pub fn try_new(s: impl Into<String>) -> Result<Self, &'static str> {
49 let s = s.into();
50 if s.trim().is_empty() {
51 Err("TaskId must not be empty or whitespace-only")
52 } else {
53 Ok(Self(s))
54 }
55 }
56}
57
58impl std::fmt::Display for TaskId {
59 #[inline]
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 f.write_str(&self.0)
62 }
63}
64
65impl From<String> for TaskId {
68 fn from(s: String) -> Self {
69 Self(s)
70 }
71}
72
73impl From<&str> for TaskId {
76 fn from(s: &str) -> Self {
77 Self(s.to_owned())
78 }
79}
80
81impl AsRef<str> for TaskId {
82 #[inline]
83 fn as_ref(&self) -> &str {
84 &self.0
85 }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
97pub struct ContextId(pub String);
98
99impl ContextId {
100 #[must_use]
105 pub fn new(s: impl Into<String>) -> Self {
106 Self(s.into())
107 }
108
109 pub fn try_new(s: impl Into<String>) -> Result<Self, &'static str> {
115 let s = s.into();
116 if s.trim().is_empty() {
117 Err("ContextId must not be empty or whitespace-only")
118 } else {
119 Ok(Self(s))
120 }
121 }
122}
123
124impl std::fmt::Display for ContextId {
125 #[inline]
126 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127 f.write_str(&self.0)
128 }
129}
130
131impl From<String> for ContextId {
134 fn from(s: String) -> Self {
135 Self(s)
136 }
137}
138
139impl From<&str> for ContextId {
142 fn from(s: &str) -> Self {
143 Self(s.to_owned())
144 }
145}
146
147impl AsRef<str> for ContextId {
148 #[inline]
149 fn as_ref(&self) -> &str {
150 &self.0
151 }
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
160pub struct TaskVersion(pub u64);
161
162impl TaskVersion {
163 #[must_use]
165 pub const fn new(v: u64) -> Self {
166 Self(v)
167 }
168
169 #[must_use]
171 pub const fn get(self) -> u64 {
172 self.0
173 }
174}
175
176impl std::fmt::Display for TaskVersion {
177 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
178 write!(f, "{}", self.0)
179 }
180}
181
182impl From<u64> for TaskVersion {
183 fn from(v: u64) -> Self {
184 Self(v)
185 }
186}
187
188include!("generated/task.rs");
192
193impl TaskState {
194 pub const ALL: [Self; 9] = [
207 Self::Unspecified,
208 Self::Submitted,
209 Self::Working,
210 Self::InputRequired,
211 Self::AuthRequired,
212 Self::Completed,
213 Self::Failed,
214 Self::Canceled,
215 Self::Rejected,
216 ];
217
218 #[inline]
222 #[must_use]
223 pub const fn is_terminal(self) -> bool {
224 matches!(
225 self,
226 Self::Completed | Self::Failed | Self::Canceled | Self::Rejected
227 )
228 }
229
230 #[inline]
236 #[must_use]
237 pub const fn is_interrupted(self) -> bool {
238 matches!(self, Self::InputRequired | Self::AuthRequired)
239 }
240
241 #[inline]
264 #[must_use]
265 pub const fn can_transition_to(self, next: Self) -> bool {
266 if self.is_terminal() {
268 return false;
269 }
270 if matches!(self, Self::Unspecified) {
274 return true;
275 }
276 if matches!(next, Self::Submitted | Self::Unspecified) {
278 return false;
279 }
280 true
283 }
284}
285
286impl std::fmt::Display for TaskState {
287 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
288 let s = match self {
289 Self::Unspecified => "TASK_STATE_UNSPECIFIED",
290 Self::Submitted => "TASK_STATE_SUBMITTED",
291 Self::Working => "TASK_STATE_WORKING",
292 Self::InputRequired => "TASK_STATE_INPUT_REQUIRED",
293 Self::AuthRequired => "TASK_STATE_AUTH_REQUIRED",
294 Self::Completed => "TASK_STATE_COMPLETED",
295 Self::Failed => "TASK_STATE_FAILED",
296 Self::Canceled => "TASK_STATE_CANCELED",
297 Self::Rejected => "TASK_STATE_REJECTED",
298 };
299 f.write_str(s)
300 }
301}
302
303impl TaskStatus {
308 #[must_use]
313 pub const fn new(state: TaskState) -> Self {
314 Self {
315 state,
316 message: None,
317 timestamp: None,
318 }
319 }
320
321 #[must_use]
323 pub fn with_timestamp(state: TaskState) -> Self {
324 Self {
325 state,
326 message: None,
327 timestamp: Some(crate::utc_now_iso8601()),
328 }
329 }
330
331 #[must_use]
337 pub fn has_valid_timestamp(&self) -> bool {
338 self.timestamp
341 .as_ref()
342 .is_none_or(|ts| ts.len() >= 19 && ts.contains('T'))
343 }
344}
345
346pub(crate) const fn empty_context_id() -> ContextId {
350 ContextId(String::new())
351}
352
353impl Task {
356 #[must_use]
370 pub fn new(
371 id: impl Into<TaskId>,
372 context_id: impl Into<ContextId>,
373 status: TaskStatus,
374 ) -> Self {
375 Self {
376 id: id.into(),
377 context_id: context_id.into(),
378 status,
379 history: None,
380 artifacts: None,
381 metadata: None,
382 }
383 }
384
385 #[must_use]
416 pub fn text(&self) -> Option<&str> {
417 self.artifacts.iter().flatten().find_map(Artifact::text)
418 }
419
420 #[must_use]
446 pub fn failure_class(&self) -> Option<crate::failure::FailureClass> {
447 crate::failure::class_of(self.status.message.as_ref()?)
448 }
449
450 pub fn texts(&self) -> impl Iterator<Item = &str> {
456 self.artifacts.iter().flatten().flat_map(Artifact::texts)
457 }
458}
459
460#[cfg(test)]
463mod tests {
464 use super::*;
465
466 fn make_task() -> Task {
467 Task {
468 id: TaskId::new("task-1"),
469 context_id: ContextId::new("ctx-1"),
470 status: TaskStatus::new(TaskState::Working),
471 history: None,
472 artifacts: None,
473 metadata: None,
474 }
475 }
476
477 #[test]
484 fn failure_class_reads_the_class_off_the_status_message() {
485 use crate::failure::{FailureClass, set_class};
486 use crate::message::Message;
487
488 let mut task = make_task();
489 assert_eq!(
490 task.failure_class(),
491 None,
492 "a task with no status message has no class to read"
493 );
494
495 let mut msg = Message::agent_text("m-1", "upstream timed out");
496 assert_eq!(
497 Task {
498 status: TaskStatus {
499 state: TaskState::Failed,
500 message: Some(msg.clone()),
501 timestamp: None,
502 },
503 ..make_task()
504 }
505 .failure_class(),
506 None,
507 "a message carrying no class is unclassified, not a default"
508 );
509
510 set_class(&mut msg, FailureClass::Transient);
511 task.status = TaskStatus {
512 state: TaskState::Failed,
513 message: Some(msg),
514 timestamp: None,
515 };
516 assert_eq!(task.failure_class(), Some(FailureClass::Transient));
517 assert!(
518 task.failure_class().expect("classified").is_retryable(),
519 "Transient is the class a caller retries on"
520 );
521 }
522
523 #[test]
524 fn task_state_screaming_snake_serde() {
525 assert_eq!(
526 serde_json::to_string(&TaskState::InputRequired).expect("ser"),
527 "\"TASK_STATE_INPUT_REQUIRED\""
528 );
529 assert_eq!(
530 serde_json::to_string(&TaskState::AuthRequired).expect("ser"),
531 "\"TASK_STATE_AUTH_REQUIRED\""
532 );
533 assert_eq!(
534 serde_json::to_string(&TaskState::Submitted).expect("ser"),
535 "\"TASK_STATE_SUBMITTED\""
536 );
537 assert_eq!(
538 serde_json::to_string(&TaskState::Unspecified).expect("ser"),
539 "\"TASK_STATE_UNSPECIFIED\""
540 );
541 let back: TaskState = serde_json::from_str("\"completed\"").unwrap();
543 assert_eq!(back, TaskState::Completed);
544 let back: TaskState = serde_json::from_str("\"input-required\"").unwrap();
545 assert_eq!(back, TaskState::InputRequired);
546 }
547
548 #[test]
549 fn task_state_is_terminal() {
550 assert!(TaskState::Completed.is_terminal());
551 assert!(TaskState::Failed.is_terminal());
552 assert!(TaskState::Canceled.is_terminal());
553 assert!(TaskState::Rejected.is_terminal());
554 assert!(!TaskState::Working.is_terminal());
555 assert!(!TaskState::Submitted.is_terminal());
556 }
557
558 #[test]
559 fn task_roundtrip() {
560 let task = make_task();
561 let json = serde_json::to_string(&task).expect("serialize");
562 assert!(json.contains("\"id\":\"task-1\""));
563
564 let back: Task = serde_json::from_str(&json).expect("deserialize");
565 assert_eq!(back.id, TaskId::new("task-1"));
566 assert_eq!(back.context_id, ContextId::new("ctx-1"));
567 assert_eq!(back.status.state, TaskState::Working);
568 }
569
570 #[test]
571 fn optional_fields_omitted() {
572 let task = make_task();
573 let json = serde_json::to_string(&task).expect("serialize");
574 assert!(!json.contains("\"history\""), "history should be omitted");
575 assert!(
576 !json.contains("\"artifacts\""),
577 "artifacts should be omitted"
578 );
579 assert!(!json.contains("\"metadata\""), "metadata should be omitted");
580 }
581
582 #[test]
583 fn task_version_ordering() {
584 assert!(TaskVersion::new(2) > TaskVersion::new(1));
585 assert_eq!(TaskVersion::new(5).get(), 5);
586 }
587
588 #[test]
589 fn wire_format_submitted_state() {
590 let json = serde_json::to_string(&TaskState::Submitted).unwrap();
591 assert_eq!(json, "\"TASK_STATE_SUBMITTED\"");
592
593 let back: TaskState = serde_json::from_str("\"submitted\"").unwrap();
595 assert_eq!(back, TaskState::Submitted);
596 let back: TaskState = serde_json::from_str("\"TASK_STATE_SUBMITTED\"").unwrap();
597 assert_eq!(back, TaskState::Submitted);
598 }
599
600 #[test]
601 fn task_version_serde_roundtrip() {
602 let v = TaskVersion::new(42);
603 let json = serde_json::to_string(&v).expect("serialize");
604 assert_eq!(json, "42");
605
606 let back: TaskVersion = serde_json::from_str(&json).expect("deserialize");
607 assert_eq!(back, TaskVersion::new(42));
608
609 let v0 = TaskVersion::new(0);
611 let json0 = serde_json::to_string(&v0).expect("serialize zero");
612 assert_eq!(json0, "0");
613 let back0: TaskVersion = serde_json::from_str(&json0).expect("deserialize zero");
614 assert_eq!(back0, TaskVersion::new(0));
615
616 let vmax = TaskVersion::new(u64::MAX);
618 let json_max = serde_json::to_string(&vmax).expect("serialize max");
619 let back_max: TaskVersion = serde_json::from_str(&json_max).expect("deserialize max");
620 assert_eq!(back_max, vmax);
621 }
622
623 #[test]
624 fn empty_string_ids_work() {
625 let tid = TaskId::new("");
626 let json = serde_json::to_string(&tid).expect("serialize empty TaskId");
627 assert_eq!(json, "\"\"");
628 let back: TaskId = serde_json::from_str(&json).expect("deserialize empty TaskId");
629 assert_eq!(back, TaskId::new(""));
630
631 let cid = ContextId::new("");
632 let json = serde_json::to_string(&cid).expect("serialize empty ContextId");
633 assert_eq!(json, "\"\"");
634 let back: ContextId = serde_json::from_str(&json).expect("deserialize empty ContextId");
635 assert_eq!(back, ContextId::new(""));
636
637 let task = Task {
639 id: TaskId::new(""),
640 context_id: ContextId::new(""),
641 status: TaskStatus::new(TaskState::Submitted),
642 history: None,
643 artifacts: None,
644 metadata: None,
645 };
646 let json = serde_json::to_string(&task).expect("serialize task with empty ids");
647 let back: Task = serde_json::from_str(&json).expect("deserialize task with empty ids");
648 assert_eq!(back.id, TaskId::new(""));
649 assert_eq!(back.context_id, ContextId::new(""));
650 }
651
652 #[test]
653 fn task_state_display_trait() {
654 assert_eq!(TaskState::Working.to_string(), "TASK_STATE_WORKING");
655 assert_eq!(TaskState::Completed.to_string(), "TASK_STATE_COMPLETED");
656 assert_eq!(TaskState::Failed.to_string(), "TASK_STATE_FAILED");
657 assert_eq!(TaskState::Canceled.to_string(), "TASK_STATE_CANCELED");
658 assert_eq!(TaskState::Rejected.to_string(), "TASK_STATE_REJECTED");
659 assert_eq!(TaskState::Submitted.to_string(), "TASK_STATE_SUBMITTED");
660 assert_eq!(
661 TaskState::InputRequired.to_string(),
662 "TASK_STATE_INPUT_REQUIRED"
663 );
664 assert_eq!(
665 TaskState::AuthRequired.to_string(),
666 "TASK_STATE_AUTH_REQUIRED"
667 );
668 assert_eq!(TaskState::Unspecified.to_string(), "TASK_STATE_UNSPECIFIED");
669 }
670
671 #[test]
674 fn all_lists_every_variant() {
675 const fn index(state: TaskState) -> usize {
680 match state {
681 TaskState::Unspecified => 0,
682 TaskState::Submitted => 1,
683 TaskState::Working => 2,
684 TaskState::InputRequired => 3,
685 TaskState::AuthRequired => 4,
686 TaskState::Completed => 5,
687 TaskState::Failed => 6,
688 TaskState::Canceled => 7,
689 TaskState::Rejected => 8,
690 }
691 }
692 let mut seen = [false; TaskState::ALL.len()];
693 for state in TaskState::ALL {
694 let i = index(state);
695 assert!(!seen[i], "{state} appears in ALL twice");
696 seen[i] = true;
697 }
698 assert!(
699 seen.iter().all(|&b| b),
700 "TaskState::ALL is missing a variant; retention would treat it as \
701 non-terminal forever"
702 );
703 }
704
705 #[test]
706 fn is_terminal_all_variants() {
707 assert!(!TaskState::Unspecified.is_terminal());
708 assert!(!TaskState::Submitted.is_terminal());
709 assert!(!TaskState::Working.is_terminal());
710 assert!(!TaskState::InputRequired.is_terminal());
711 assert!(!TaskState::AuthRequired.is_terminal());
712 assert!(TaskState::Completed.is_terminal());
713 assert!(TaskState::Failed.is_terminal());
714 assert!(TaskState::Canceled.is_terminal());
715 assert!(TaskState::Rejected.is_terminal());
716 }
717
718 #[test]
726 fn is_interrupted_all_variants() {
727 assert!(!TaskState::Unspecified.is_interrupted());
728 assert!(!TaskState::Submitted.is_interrupted());
729 assert!(!TaskState::Working.is_interrupted());
730 assert!(TaskState::InputRequired.is_interrupted());
731 assert!(TaskState::AuthRequired.is_interrupted());
732 assert!(!TaskState::Completed.is_interrupted());
733 assert!(!TaskState::Failed.is_interrupted());
734 assert!(!TaskState::Canceled.is_interrupted());
735 assert!(!TaskState::Rejected.is_interrupted());
736 }
737
738 #[test]
742 fn can_transition_to_valid_transitions() {
743 use TaskState::*;
744
745 for &target in &[
747 Unspecified,
748 Submitted,
749 Working,
750 InputRequired,
751 AuthRequired,
752 Completed,
753 Failed,
754 Canceled,
755 Rejected,
756 ] {
757 assert!(
758 Unspecified.can_transition_to(target),
759 "Unspecified → {target:?} should be valid"
760 );
761 }
762
763 assert!(Submitted.can_transition_to(Working));
767 assert!(Submitted.can_transition_to(Failed));
768 assert!(Submitted.can_transition_to(Canceled));
769 assert!(Submitted.can_transition_to(Rejected));
770 assert!(Submitted.can_transition_to(Completed));
771 assert!(Submitted.can_transition_to(InputRequired));
772 assert!(Submitted.can_transition_to(AuthRequired));
773
774 assert!(Working.can_transition_to(Completed));
778 assert!(Working.can_transition_to(Failed));
779 assert!(Working.can_transition_to(Canceled));
780 assert!(Working.can_transition_to(InputRequired));
781 assert!(Working.can_transition_to(AuthRequired));
782 assert!(Working.can_transition_to(Rejected));
783
784 for &from in &[InputRequired, AuthRequired] {
786 for &to in &[Working, Failed, Canceled, Completed, Rejected] {
787 assert!(
788 from.can_transition_to(to),
789 "{from:?} → {to:?} should be valid"
790 );
791 }
792 }
793 }
794
795 #[test]
797 fn can_transition_to_invalid_transitions() {
798 use TaskState::*;
799
800 for &terminal in &[Completed, Failed, Canceled, Rejected] {
802 for &target in &[
803 Unspecified,
804 Submitted,
805 Working,
806 InputRequired,
807 AuthRequired,
808 Completed,
809 Failed,
810 Canceled,
811 Rejected,
812 ] {
813 assert!(
814 !terminal.can_transition_to(target),
815 "{terminal:?} → {target:?} should be invalid (terminal state)"
816 );
817 }
818 }
819
820 for &from in &[Submitted, Working, InputRequired, AuthRequired] {
823 assert!(
824 !from.can_transition_to(Submitted),
825 "{from:?} → Submitted should be invalid (entry state)"
826 );
827 assert!(
828 !from.can_transition_to(Unspecified),
829 "{from:?} → Unspecified should be invalid (proto default)"
830 );
831 }
832
833 assert!(Working.can_transition_to(Working));
836 }
837
838 #[test]
841 fn task_id_display_and_as_ref() {
842 let id = TaskId::new("abc");
843 assert_eq!(id.to_string(), "abc");
844 assert_eq!(id.as_ref(), "abc");
845 }
846
847 #[test]
848 fn task_id_from_impls() {
849 let from_str: TaskId = "hello".into();
850 assert_eq!(from_str, TaskId::new("hello"));
851
852 let from_string: TaskId = String::from("world").into();
853 assert_eq!(from_string, TaskId::new("world"));
854 }
855
856 #[test]
857 fn context_id_display_and_as_ref() {
858 let id = ContextId::new("ctx");
859 assert_eq!(id.to_string(), "ctx");
860 assert_eq!(id.as_ref(), "ctx");
861 }
862
863 #[test]
864 fn context_id_from_impls() {
865 let from_str: ContextId = "c1".into();
866 assert_eq!(from_str, ContextId::new("c1"));
867
868 let from_string: ContextId = String::from("c2").into();
869 assert_eq!(from_string, ContextId::new("c2"));
870 }
871
872 #[test]
873 fn task_version_display() {
874 assert_eq!(TaskVersion::new(42).to_string(), "42");
875 assert_eq!(TaskVersion::new(0).to_string(), "0");
876 }
877
878 #[test]
879 fn task_version_from_u64() {
880 let v: TaskVersion = 99u64.into();
881 assert_eq!(v.get(), 99);
882 }
883
884 #[test]
885 fn task_status_with_timestamp_has_timestamp() {
886 let status = TaskStatus::with_timestamp(TaskState::Working);
887 assert!(
888 status.timestamp.is_some(),
889 "with_timestamp should set timestamp"
890 );
891 assert!(status.message.is_none());
892 assert_eq!(status.state, TaskState::Working);
893 }
894
895 #[test]
896 fn task_status_new_has_no_timestamp() {
897 let status = TaskStatus::new(TaskState::Submitted);
898 assert!(status.timestamp.is_none());
899 assert!(status.message.is_none());
900 assert_eq!(status.state, TaskState::Submitted);
901 }
902
903 #[test]
906 fn task_id_try_new_valid() {
907 let id = TaskId::try_new("task-1");
908 assert!(id.is_ok());
909 assert_eq!(id.unwrap(), TaskId::new("task-1"));
910 }
911
912 #[test]
913 fn task_id_try_new_valid_string() {
914 let id = TaskId::try_new("task-1".to_string());
915 assert!(id.is_ok());
916 assert_eq!(id.unwrap(), TaskId::new("task-1"));
917 }
918
919 #[test]
920 fn task_id_try_new_empty_rejected() {
921 let id = TaskId::try_new("");
922 assert!(id.is_err());
923 assert_eq!(
924 id.unwrap_err(),
925 "TaskId must not be empty or whitespace-only"
926 );
927 }
928
929 #[test]
930 fn task_id_try_new_whitespace_only_rejected() {
931 let id = TaskId::try_new(" ");
932 assert!(id.is_err());
933 }
934
935 #[test]
936 fn context_id_try_new_valid() {
937 let id = ContextId::try_new("ctx-1");
938 assert!(id.is_ok());
939 assert_eq!(id.unwrap(), ContextId::new("ctx-1"));
940 }
941
942 #[test]
943 fn context_id_try_new_valid_string() {
944 let id = ContextId::try_new("ctx-1".to_string());
945 assert!(id.is_ok());
946 assert_eq!(id.unwrap(), ContextId::new("ctx-1"));
947 }
948
949 #[test]
950 fn context_id_try_new_empty_rejected() {
951 let id = ContextId::try_new("");
952 assert!(id.is_err());
953 assert_eq!(
954 id.unwrap_err(),
955 "ContextId must not be empty or whitespace-only"
956 );
957 }
958
959 #[test]
960 fn context_id_try_new_whitespace_only_rejected() {
961 let id = ContextId::try_new(" \t ");
962 assert!(id.is_err());
963 }
964
965 #[test]
968 fn has_valid_timestamp_none_is_valid() {
969 let status = TaskStatus::new(TaskState::Working);
970 assert!(status.has_valid_timestamp());
971 }
972
973 #[test]
974 fn has_valid_timestamp_valid_iso8601() {
975 let status = TaskStatus {
976 state: TaskState::Working,
977 message: None,
978 timestamp: Some("2026-03-19T12:00:00Z".into()),
979 };
980 assert!(status.has_valid_timestamp());
981 }
982
983 #[test]
984 fn has_valid_timestamp_valid_with_offset() {
985 let status = TaskStatus {
986 state: TaskState::Working,
987 message: None,
988 timestamp: Some("2026-03-19T12:00:00+05:30".into()),
989 };
990 assert!(status.has_valid_timestamp());
991 }
992
993 #[test]
994 fn has_valid_timestamp_too_short() {
995 let status = TaskStatus {
996 state: TaskState::Working,
997 message: None,
998 timestamp: Some("2026-03-19".into()),
999 };
1000 assert!(!status.has_valid_timestamp());
1001 }
1002
1003 #[test]
1004 fn has_valid_timestamp_missing_t_separator() {
1005 let status = TaskStatus {
1006 state: TaskState::Working,
1007 message: None,
1008 timestamp: Some("2026-03-19 12:00:00Z".into()),
1009 };
1010 assert!(!status.has_valid_timestamp());
1011 }
1012
1013 #[test]
1014 fn has_valid_timestamp_empty_string() {
1015 let status = TaskStatus {
1016 state: TaskState::Working,
1017 message: None,
1018 timestamp: Some(String::new()),
1019 };
1020 assert!(!status.has_valid_timestamp());
1021 }
1022
1023 #[test]
1024 fn has_valid_timestamp_with_timestamp_constructor() {
1025 let status = TaskStatus::with_timestamp(TaskState::Completed);
1026 assert!(status.has_valid_timestamp());
1027 }
1028
1029 #[test]
1033 fn text_finds_the_first_textual_artifact_and_texts_returns_all_of_them() {
1034 use crate::message::Part;
1035
1036 let mut task = make_task();
1037 assert_eq!(task.text(), None, "no artifacts at all");
1038 assert_eq!(task.texts().count(), 0);
1039
1040 task.artifacts = Some(vec![
1041 Artifact::new("chart", vec![Part::url("https://example.com/c.png")]),
1042 Artifact::new("summary", vec![Part::text("revenue rose 4%")]),
1043 Artifact::new("notes", vec![Part::text("caveat"), Part::text("second")]),
1044 ]);
1045
1046 assert_eq!(task.text(), Some("revenue rose 4%"));
1047 assert_eq!(
1048 task.texts().collect::<Vec<_>>(),
1049 vec!["revenue rose 4%", "caveat", "second"]
1050 );
1051
1052 let first = task.artifacts.as_ref().and_then(|a| a.first());
1053 assert_eq!(
1054 first.and_then(Artifact::text),
1055 None,
1056 "the chain text() deliberately differs from"
1057 );
1058 }
1059
1060 #[test]
1061 fn text_is_none_when_every_artifact_is_non_textual() {
1062 use crate::message::Part;
1063
1064 let mut task = make_task();
1065 task.artifacts = Some(vec![Artifact::new(
1066 "chart",
1067 vec![Part::url("https://example.com/c.png")],
1068 )]);
1069 assert_eq!(task.text(), None);
1070 assert_eq!(task.texts().count(), 0);
1071 }
1072}