| 1 | use chrono::{DateTime, TimeZone, Utc}; |
| 2 | use store::{AttachmentImage, DraftRecord, OutboxRecord, OutboxState, OutboxTarget, Store}; |
| 3 | |
| 4 | fn at(second: u32) -> DateTime<Utc> { |
| 5 | Utc.with_ymd_and_hms(2026, 10, 6, 9, 0, second).unwrap() |
| 6 | } |
| 7 | |
| 8 | fn image(name: &str) -> AttachmentImage { |
| 9 | AttachmentImage { |
| 10 | name: name.to_owned(), |
| 11 | format: "image/png".to_owned(), |
| 12 | bytes: vec![1, 2, 3, 4], |
| 13 | width: Some(10), |
| 14 | height: None, |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | fn outbox(id: &str, conversation_id: &str, second: u32) -> OutboxRecord { |
| 19 | OutboxRecord { |
| 20 | id: id.to_owned(), |
| 21 | conversation_id: conversation_id.to_owned(), |
| 22 | target: OutboxTarget::Flat, |
| 23 | thread_root_id: None, |
| 24 | payload: format!("{{\"id\":\"{id}\"}}"), |
| 25 | images: vec![image("a.png"), image("b.png")], |
| 26 | state: OutboxState::Sending, |
| 27 | last_error: None, |
| 28 | created_at: at(second), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn draft(conversation_id: &str, preview: &str) -> DraftRecord { |
| 33 | DraftRecord { |
| 34 | conversation_id: conversation_id.to_owned(), |
| 35 | payload: "{}".to_owned(), |
| 36 | preview: preview.to_owned(), |
| 37 | images: vec![image("pasted.png")], |
| 38 | updated_at: at(1), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn outbox_rows_round_trip_with_images_oldest_first() { |
| 44 | let store = Store::open_in_memory().unwrap(); |
| 45 | let mut thread = outbox("late", "chat-1", 30); |
| 46 | thread.target = OutboxTarget::Thread; |
| 47 | thread.thread_root_id = Some("root-1".to_owned()); |
| 48 | store.put_outbox(&thread).unwrap(); |
| 49 | store.put_outbox(&outbox("early", "chat-1", 10)).unwrap(); |
| 50 | store.put_outbox(&outbox("other", "chat-2", 20)).unwrap(); |
| 51 | let rows = store.outbox_for_conversation("chat-1").unwrap(); |
| 52 | assert_eq!(rows, vec![outbox("early", "chat-1", 10), thread]); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn failed_rows_leave_the_sending_list_and_keep_their_error() { |
| 57 | let store = Store::open_in_memory().unwrap(); |
| 58 | store.put_outbox(&outbox("a", "chat-1", 10)).unwrap(); |
| 59 | store.put_outbox(&outbox("b", "chat-2", 20)).unwrap(); |
| 60 | store.mark_outbox_failed("a", "HTTP 403").unwrap(); |
| 61 | let sending = store.sending_outbox().unwrap(); |
| 62 | assert_eq!(sending.len(), 1); |
| 63 | assert_eq!(sending[0].id, "b"); |
| 64 | let failed = store.outbox_for_conversation("chat-1").unwrap(); |
| 65 | assert_eq!(failed[0].state, OutboxState::Failed); |
| 66 | assert_eq!(failed[0].last_error.as_deref(), Some("HTTP 403")); |
| 67 | assert_eq!(store.failed_outbox_conversations().unwrap(), vec!["chat-1"]); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn putting_a_failed_row_again_makes_it_a_fresh_attempt() { |
| 72 | let store = Store::open_in_memory().unwrap(); |
| 73 | store.put_outbox(&outbox("a", "chat-1", 10)).unwrap(); |
| 74 | store.mark_outbox_failed("a", "boom").unwrap(); |
| 75 | store.put_outbox(&outbox("a", "chat-1", 40)).unwrap(); |
| 76 | let rows = store.outbox_for_conversation("chat-1").unwrap(); |
| 77 | assert_eq!(rows, vec![outbox("a", "chat-1", 40)]); |
| 78 | assert!(store.failed_outbox_conversations().unwrap().is_empty()); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn deleting_an_outbox_row_removes_its_images() { |
| 83 | let store = Store::open_in_memory().unwrap(); |
| 84 | store.put_outbox(&outbox("a", "chat-1", 10)).unwrap(); |
| 85 | store.delete_outbox("a").unwrap(); |
| 86 | assert!(store.outbox_for_conversation("chat-1").unwrap().is_empty()); |
| 87 | store.put_outbox(&outbox("a", "chat-1", 10)).unwrap(); |
| 88 | assert_eq!( |
| 89 | store.outbox_for_conversation("chat-1").unwrap()[0] |
| 90 | .images |
| 91 | .len(), |
| 92 | 2 |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn drafts_save_load_replace_and_delete() { |
| 98 | let store = Store::open_in_memory().unwrap(); |
| 99 | assert_eq!(store.draft("chat-1").unwrap(), None); |
| 100 | store.save_draft(&draft("chat-1", "first")).unwrap(); |
| 101 | store.save_draft(&draft("chat-2", "other")).unwrap(); |
| 102 | let mut changed = draft("chat-1", "second"); |
| 103 | changed.images.clear(); |
| 104 | store.save_draft(&changed).unwrap(); |
| 105 | assert_eq!(store.draft("chat-1").unwrap(), Some(changed)); |
| 106 | assert_eq!( |
| 107 | store.draft("chat-2").unwrap(), |
| 108 | Some(draft("chat-2", "other")) |
| 109 | ); |
| 110 | store.delete_draft("chat-1").unwrap(); |
| 111 | assert_eq!(store.draft("chat-1").unwrap(), None); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn draft_previews_map_conversations_to_plain_text() { |
| 116 | let store = Store::open_in_memory().unwrap(); |
| 117 | store.save_draft(&draft("chat-1", "hello")).unwrap(); |
| 118 | store.save_draft(&draft("chat-2", "world")).unwrap(); |
| 119 | let previews = store.draft_previews().unwrap(); |
| 120 | assert_eq!(previews.len(), 2); |
| 121 | assert_eq!(previews["chat-1"], "hello"); |
| 122 | assert_eq!(previews["chat-2"], "world"); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn draft_and_outbox_images_do_not_collide_on_the_same_id() { |
| 127 | let store = Store::open_in_memory().unwrap(); |
| 128 | store.put_outbox(&outbox("same", "chat-1", 10)).unwrap(); |
| 129 | store.save_draft(&draft("same", "text")).unwrap(); |
| 130 | store.delete_draft("same").unwrap(); |
| 131 | assert_eq!( |
| 132 | store.outbox_for_conversation("chat-1").unwrap()[0] |
| 133 | .images |
| 134 | .len(), |
| 135 | 2 |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn saving_only_the_draft_text_keeps_the_stored_images() { |
| 141 | let store = Store::open_in_memory().unwrap(); |
| 142 | store.save_draft(&draft("chat-1", "first")).unwrap(); |
| 143 | let mut edited = draft("chat-1", "second"); |
| 144 | edited.payload = "{\"lines\":[]}".to_owned(); |
| 145 | edited.images.clear(); |
| 146 | edited.updated_at = at(9); |
| 147 | store.save_draft_text(&edited).unwrap(); |
| 148 | let loaded = store.draft("chat-1").unwrap().unwrap(); |
| 149 | assert_eq!(loaded.preview, "second"); |
| 150 | assert_eq!(loaded.payload, "{\"lines\":[]}"); |
| 151 | assert_eq!(loaded.updated_at, at(9)); |
| 152 | assert_eq!(loaded.images, vec![image("pasted.png")]); |
| 153 | } |