tuitbot-server 0.1.49

HTTP API server for Tuitbot autonomous X growth assistant
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
//! Tests for draft studio.

use super::*;

// --- content_preview tests ---

#[test]
fn content_preview_short_text() {
    let preview = content_preview("Hello world", "tweet");
    assert_eq!(preview, "Hello world");
}

#[test]
fn content_preview_truncates_long_text() {
    let long = "a".repeat(100);
    let preview = content_preview(&long, "tweet");
    assert_eq!(preview.len(), 60);
    assert!(preview.ends_with("..."));
}

#[test]
fn content_preview_exactly_60_chars() {
    let text = "a".repeat(60);
    let preview = content_preview(&text, "tweet");
    assert_eq!(preview.len(), 60);
    assert!(!preview.ends_with("..."));
}

#[test]
fn content_preview_thread_with_blocks() {
    let json = r#"{"blocks":[{"text":"First tweet here"}]}"#;
    let preview = content_preview(json, "thread");
    assert_eq!(preview, "First tweet here");
}

#[test]
fn content_preview_thread_with_legacy_array() {
    let json = r#"["First tweet","Second tweet"]"#;
    let preview = content_preview(json, "thread");
    assert_eq!(preview, "First tweet");
}

#[test]
fn content_preview_thread_invalid_json_falls_back() {
    let preview = content_preview("not json", "thread");
    assert_eq!(preview, "not json");
}

#[test]
fn content_preview_trims_whitespace() {
    let preview = content_preview("  hello  ", "tweet");
    assert_eq!(preview, "hello");
}

// --- extract_first_block_text tests ---

#[test]
fn extract_first_block_text_valid_blocks() {
    let json = r#"{"blocks":[{"text":"Block one"},{"text":"Block two"}]}"#;
    assert_eq!(extract_first_block_text(json), "Block one");
}

#[test]
fn extract_first_block_text_empty_blocks() {
    let json = r#"{"blocks":[]}"#;
    assert_eq!(extract_first_block_text(json), json);
}

#[test]
fn extract_first_block_text_no_blocks_key() {
    let json = r#"{"other": "value"}"#;
    assert_eq!(extract_first_block_text(json), json);
}

#[test]
fn extract_first_block_text_legacy_array() {
    let json = r#"["Tweet 1","Tweet 2"]"#;
    assert_eq!(extract_first_block_text(json), "Tweet 1");
}

// --- default functions ---

#[test]
fn default_tweet_returns_tweet() {
    assert_eq!(default_tweet(), "tweet");
}

#[test]
fn default_blank_content_returns_space() {
    assert_eq!(default_blank_content(), " ");
}

#[test]
fn default_manual_returns_manual() {
    assert_eq!(default_manual(), "manual");
}

#[test]
fn default_manual_trigger_returns_manual() {
    assert_eq!(default_manual_trigger(), "manual");
}

// --- request deserialization ---

#[test]
fn draft_list_query_all_none() {
    let json = "{}";
    let q: DraftListQuery = serde_json::from_str(json).expect("deser");
    assert!(q.status.is_none());
    assert!(q.tag.is_none());
    assert!(q.search.is_none());
    assert!(q.archived.is_none());
}

#[test]
fn draft_list_query_with_filters() {
    let json = r#"{"status":"draft","tag":1,"search":"hello","archived":true}"#;
    let q: DraftListQuery = serde_json::from_str(json).expect("deser");
    assert_eq!(q.status.as_deref(), Some("draft"));
    assert_eq!(q.tag, Some(1));
    assert_eq!(q.search.as_deref(), Some("hello"));
    assert_eq!(q.archived, Some(true));
}

#[test]
fn create_studio_draft_body_defaults() {
    let json = "{}";
    let body: CreateStudioDraftBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.content_type, "tweet");
    assert_eq!(body.content, " ");
    assert_eq!(body.source, "manual");
    assert!(body.title.is_none());
}

#[test]
fn autosave_patch_body_deserializes() {
    let json =
        r#"{"content":"updated","content_type":"tweet","updated_at":"2026-03-15T10:00:00Z"}"#;
    let body: AutosavePatchBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.content, "updated");
    assert_eq!(body.content_type, "tweet");
}

#[test]
fn meta_patch_body_optional_fields() {
    let json = r#"{"title":"My Title"}"#;
    let body: MetaPatchBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.title.as_deref(), Some("My Title"));
    assert!(body.notes.is_none());
}

#[test]
fn schedule_body_deserializes() {
    let json = r#"{"scheduled_for":"2026-03-15T12:00:00Z"}"#;
    let body: ScheduleBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.scheduled_for, "2026-03-15T12:00:00Z");
}

#[test]
fn reschedule_body_deserializes() {
    let json = r#"{"scheduled_for":"2026-03-16T14:00:00Z"}"#;
    let body: RescheduleBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.scheduled_for, "2026-03-16T14:00:00Z");
}

#[test]
fn create_revision_body_default_trigger() {
    let json = "{}";
    let body: CreateRevisionBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.trigger_kind, "manual");
}

// =========================================================================
// Additional edge case tests for coverage push
// =========================================================================

// --- content_preview edge cases ---

#[test]
fn content_preview_empty_string() {
    let preview = content_preview("", "tweet");
    assert!(preview.is_empty());
}

#[test]
fn content_preview_exactly_57_chars() {
    let text = "a".repeat(57);
    let preview = content_preview(&text, "tweet");
    assert_eq!(preview.len(), 57);
    assert!(!preview.ends_with("..."));
}

#[test]
fn content_preview_exactly_61_chars() {
    let text = "a".repeat(61);
    let preview = content_preview(&text, "tweet");
    assert_eq!(preview.len(), 60);
    assert!(preview.ends_with("..."));
}

#[test]
fn content_preview_whitespace_only() {
    let preview = content_preview("   \n\t  ", "tweet");
    assert!(preview.is_empty());
}

#[test]
fn content_preview_thread_empty_blocks() {
    let json = r#"{"blocks":[]}"#;
    let preview = content_preview(json, "thread");
    assert_eq!(preview, json);
}

#[test]
fn content_preview_thread_block_no_text_key() {
    let json = r#"{"blocks":[{"content":"no text key"}]}"#;
    let preview = content_preview(json, "thread");
    assert_eq!(preview, json);
}

#[test]
fn content_preview_thread_nested_blocks_long() {
    let long_text = "x".repeat(100);
    let json = format!(r#"{{"blocks":[{{"text":"{long_text}"}}]}}"#);
    let preview = content_preview(&json, "thread");
    assert_eq!(preview.len(), 60);
    assert!(preview.ends_with("..."));
}

#[test]
fn content_preview_thread_legacy_array_single() {
    let json = r#"["Only one"]"#;
    let preview = content_preview(json, "thread");
    assert_eq!(preview, "Only one");
}

#[test]
fn content_preview_thread_legacy_array_empty() {
    let json = r#"[]"#;
    let preview = content_preview(json, "thread");
    assert_eq!(preview, json);
}

#[test]
fn content_preview_tweet_with_newlines() {
    let preview = content_preview("Line 1\nLine 2\nLine 3", "tweet");
    assert!(preview.contains('\n'));
}

// --- extract_first_block_text edge cases ---

#[test]
fn extract_first_block_text_number_value() {
    let json = r#"{"blocks":[{"text":123}]}"#;
    assert_eq!(extract_first_block_text(json), json);
}

#[test]
fn extract_first_block_text_null_blocks() {
    let json = r#"{"blocks":null}"#;
    assert_eq!(extract_first_block_text(json), json);
}

#[test]
fn extract_first_block_text_nested_array_non_string() {
    let json = r#"[123, "text"]"#;
    // First element is not a string, so falls back to full content
    assert_eq!(extract_first_block_text(json), json);
}

#[test]
fn extract_first_block_text_string_value() {
    let json = r#""just a string""#;
    assert_eq!(extract_first_block_text(json), json);
}

#[test]
fn extract_first_block_text_many_blocks() {
    let json = r#"{"blocks":[{"text":"First"},{"text":"Second"},{"text":"Third"}]}"#;
    assert_eq!(extract_first_block_text(json), "First");
}

// --- DraftSummary serialization ---

#[test]
fn draft_summary_serializes() {
    let summary = DraftSummary {
        id: 42,
        title: Some("My Draft".into()),
        content_type: "tweet".into(),
        content_preview: "Preview text".into(),
        status: "draft".into(),
        scheduled_for: None,
        archived_at: None,
        updated_at: "2026-03-15T10:00:00Z".into(),
        created_at: "2026-03-14T10:00:00Z".into(),
        source: "manual".into(),
    };
    let json = serde_json::to_string(&summary).expect("serialize");
    assert!(json.contains("42"));
    assert!(json.contains("My Draft"));
    assert!(json.contains("Preview text"));
    assert!(json.contains("draft"));
    assert!(json.contains("manual"));
}

#[test]
fn draft_summary_serializes_with_schedule() {
    let summary = DraftSummary {
        id: 1,
        title: None,
        content_type: "tweet".into(),
        content_preview: "Test".into(),
        status: "scheduled".into(),
        scheduled_for: Some("2026-04-01T12:00:00Z".into()),
        archived_at: None,
        updated_at: "2026-03-15T10:00:00Z".into(),
        created_at: "2026-03-14T10:00:00Z".into(),
        source: "assist".into(),
    };
    let json = serde_json::to_string(&summary).expect("serialize");
    assert!(json.contains("2026-04-01"));
    assert!(json.contains("scheduled"));
    assert!(json.contains("assist"));
}

// --- Request body edge cases ---

#[test]
fn create_studio_draft_body_with_all_fields() {
    let json = r#"{"content_type":"thread","content":"thread content","source":"assist","title":"My Title"}"#;
    let body: CreateStudioDraftBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.content_type, "thread");
    assert_eq!(body.content, "thread content");
    assert_eq!(body.source, "assist");
    assert_eq!(body.title.as_deref(), Some("My Title"));
}

#[test]
fn create_studio_draft_body_partial_defaults() {
    let json = r#"{"content":"Hello"}"#;
    let body: CreateStudioDraftBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.content_type, "tweet");
    assert_eq!(body.content, "Hello");
    assert_eq!(body.source, "manual");
    assert!(body.title.is_none());
}

#[test]
fn autosave_patch_body_full_deserialize() {
    let json =
        r#"{"content":"new content","content_type":"thread","updated_at":"2026-03-15T12:00:00Z"}"#;
    let body: AutosavePatchBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.content, "new content");
    assert_eq!(body.content_type, "thread");
    assert_eq!(body.updated_at, "2026-03-15T12:00:00Z");
}

#[test]
fn meta_patch_body_all_none() {
    let json = r#"{}"#;
    let body: MetaPatchBody = serde_json::from_str(json).expect("deser");
    assert!(body.title.is_none());
    assert!(body.notes.is_none());
}

#[test]
fn meta_patch_body_both_fields() {
    let json = r#"{"title":"My Title","notes":"Some notes here"}"#;
    let body: MetaPatchBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.title.as_deref(), Some("My Title"));
    assert_eq!(body.notes.as_deref(), Some("Some notes here"));
}

#[test]
fn draft_list_query_partial_filters() {
    let json = r#"{"status":"scheduled","archived":false}"#;
    let q: DraftListQuery = serde_json::from_str(json).expect("deser");
    assert_eq!(q.status.as_deref(), Some("scheduled"));
    assert!(q.tag.is_none());
    assert!(q.search.is_none());
    assert_eq!(q.archived, Some(false));
}

#[test]
fn create_revision_body_custom_trigger() {
    let json = r#"{"trigger_kind":"autosave"}"#;
    let body: CreateRevisionBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.trigger_kind, "autosave");
}

#[test]
fn reschedule_body_iso_format() {
    let json = r#"{"scheduled_for":"2026-12-31T23:59:59Z"}"#;
    let body: RescheduleBody = serde_json::from_str(json).expect("deser");
    assert_eq!(body.scheduled_for, "2026-12-31T23:59:59Z");
}

#[test]
fn schedule_body_with_offset() {
    let json = r#"{"scheduled_for":"2026-03-15T12:00:00-05:00"}"#;
    let body: ScheduleBody = serde_json::from_str(json).expect("deser");
    assert!(body.scheduled_for.contains("-05:00"));
}