tuitbot-core 0.1.47

Core library 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
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
use super::*;
use crate::content::angles::EvidenceType;
use crate::content::evidence::NeighborContent;

fn sample_evidence() -> Vec<EvidenceItem> {
    vec![
        EvidenceItem {
            evidence_type: EvidenceType::DataPoint,
            citation_text: "45% growth".to_string(),
            source_node_id: 1,
            source_note_title: "Metrics".to_string(),
            source_heading_path: None,
            confidence: 0.8,
        },
        EvidenceItem {
            evidence_type: EvidenceType::Contradiction,
            citation_text: "But costs rose too".to_string(),
            source_node_id: 2,
            source_note_title: "Costs".to_string(),
            source_heading_path: None,
            confidence: 0.7,
        },
        EvidenceItem {
            evidence_type: EvidenceType::AhaMoment,
            citation_text: "Unexpected correlation".to_string(),
            source_node_id: 3,
            source_note_title: "Insights".to_string(),
            source_heading_path: None,
            confidence: 0.9,
        },
    ]
}

#[test]
fn parse_angles_well_formatted() {
    let response = "\
ANGLE_TYPE: story
SEED_TEXT: We grew 45% but our costs told a different story...
RATIONALE: Tension between growth and cost creates a compelling narrative.
EVIDENCE_IDS: 1, 2
---
ANGLE_TYPE: listicle
SEED_TEXT: 3 things nobody tells you about scaling fast:
RATIONALE: List format works well for multi-faceted insights.
EVIDENCE_IDS: 1, 3
---
ANGLE_TYPE: hot_take
SEED_TEXT: Growth without profit isn't growth\u{2014}it's a slow leak.
RATIONALE: Bold opinion backed by evidence of rising costs.
EVIDENCE_IDS: 2";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);

    assert_eq!(angles.len(), 3);
    assert_eq!(angles[0].angle_type, AngleType::Story);
    assert_eq!(angles[0].evidence.len(), 2);
    assert_eq!(angles[1].angle_type, AngleType::Listicle);
    assert_eq!(angles[1].evidence.len(), 2);
    assert_eq!(angles[2].angle_type, AngleType::HotTake);
    assert_eq!(angles[2].evidence.len(), 1);
}

#[test]
fn parse_angles_partial() {
    let response = "\
ANGLE_TYPE: story
SEED_TEXT: A tale of growth
RATIONALE: Good narrative.
EVIDENCE_IDS: 1
---
ANGLE_TYPE: listicle
SEED_TEXT: Top insights from the data
RATIONALE: Lists perform well.
EVIDENCE_IDS: 1, 3";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);
    assert_eq!(angles.len(), 2);
}

#[test]
fn parse_angles_empty() {
    let angles = parse_angles_response("", &sample_evidence());
    assert!(angles.is_empty());
}

#[test]
fn fallback_no_neighbors() {
    // Tested via the pipeline: empty neighbors -> "no_neighbors_accepted"
    // This is a sync verification of the pipeline's early return logic.
    let neighbors: Vec<NeighborContent> = vec![];
    assert!(neighbors.is_empty());
}

#[test]
fn parse_angle_type_variants() {
    assert_eq!(parse_angle_type("story"), Some(AngleType::Story));
    assert_eq!(parse_angle_type("listicle"), Some(AngleType::Listicle));
    assert_eq!(parse_angle_type("hot_take"), Some(AngleType::HotTake));
    assert_eq!(parse_angle_type("hottake"), Some(AngleType::HotTake));
    assert_eq!(parse_angle_type("hot take"), Some(AngleType::HotTake));
    assert_eq!(parse_angle_type("STORY"), Some(AngleType::Story));
    assert_eq!(parse_angle_type("unknown"), None);
}

#[test]
fn parse_angles_with_evidence_mapping() {
    let response = "\
ANGLE_TYPE: story
SEED_TEXT: The data surprised us all.
RATIONALE: Data-backed narrative.
EVIDENCE_IDS: 1, 3";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);
    assert_eq!(angles.len(), 1);
    assert_eq!(angles[0].evidence.len(), 2);
    assert_eq!(angles[0].evidence[0].evidence_type, EvidenceType::DataPoint);
    assert_eq!(angles[0].evidence[1].evidence_type, EvidenceType::AhaMoment);
    // 2 items with avg conf (0.8+0.9)/2 = 0.85 >= 0.6 -> "high"
    assert_eq!(angles[0].confidence, "high");
}

// -- strip_formatting tests --

#[test]
fn strip_formatting_removes_bold() {
    assert_eq!(
        strip_formatting("**ANGLE_TYPE**: story"),
        "ANGLE_TYPE: story"
    );
}

#[test]
fn strip_formatting_removes_numbered_prefix_dot() {
    assert_eq!(
        strip_formatting("1. ANGLE_TYPE: story"),
        "ANGLE_TYPE: story"
    );
}

#[test]
fn strip_formatting_removes_numbered_prefix_paren() {
    assert_eq!(strip_formatting("2) SEED_TEXT: hello"), "SEED_TEXT: hello");
}

#[test]
fn strip_formatting_removes_numbered_prefix_colon() {
    assert_eq!(strip_formatting("3: RATIONALE: why"), "RATIONALE: why");
}

#[test]
fn strip_formatting_removes_bullet_dash() {
    assert_eq!(
        strip_formatting("- ANGLE_TYPE: listicle"),
        "ANGLE_TYPE: listicle"
    );
}

#[test]
fn strip_formatting_removes_bullet_dash_at_start() {
    // Verify dash bullet is stripped (common in LLM output)
    assert_eq!(strip_formatting("- hello world"), "hello world");
}

#[test]
fn strip_formatting_removes_unicode_bullet() {
    let input = "\u{2022} something";
    assert_eq!(strip_formatting(input), "something");
}

#[test]
fn strip_formatting_plain_text_unchanged() {
    assert_eq!(strip_formatting("ANGLE_TYPE: story"), "ANGLE_TYPE: story");
}

#[test]
fn strip_formatting_empty_string() {
    assert_eq!(strip_formatting(""), "");
}

// -- strip_quotes tests --

#[test]
fn strip_quotes_double_quotes() {
    assert_eq!(strip_quotes("\"hello world\""), "hello world");
}

#[test]
fn strip_quotes_single_quotes() {
    assert_eq!(strip_quotes("'hello world'"), "hello world");
}

#[test]
fn strip_quotes_no_quotes() {
    assert_eq!(strip_quotes("hello world"), "hello world");
}

#[test]
fn strip_quotes_mismatched_not_stripped() {
    assert_eq!(strip_quotes("\"hello world'"), "\"hello world'");
}

#[test]
fn strip_quotes_trims_whitespace() {
    assert_eq!(strip_quotes("  \"padded\"  "), "padded");
}

#[test]
fn strip_quotes_empty_quoted() {
    // Single char each side: "\"\"" -> ""
    assert_eq!(strip_quotes("\"\""), "");
}

// -- strip_prefix_ci tests --

#[test]
fn strip_prefix_ci_match() {
    assert_eq!(
        strip_prefix_ci("ANGLE_TYPE: story", "angle_type:"),
        Some(" story")
    );
}

#[test]
fn strip_prefix_ci_no_match() {
    assert!(strip_prefix_ci("SEED_TEXT: foo", "angle_type:").is_none());
}

#[test]
fn strip_prefix_ci_mixed_case() {
    assert_eq!(
        strip_prefix_ci("Angle_Type: hot_take", "angle_type:"),
        Some(" hot_take")
    );
}

// -- parse_angles_response edge cases --

#[test]
fn parse_angles_invalid_evidence_ids_filtered() {
    let response = "\
ANGLE_TYPE: story
SEED_TEXT: A tale
RATIONALE: Good.
EVIDENCE_IDS: 0, 99";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);
    // IDs 0 and 99 are out of range (1-indexed, 3 items)
    assert_eq!(angles.len(), 1);
    assert!(angles[0].evidence.is_empty());
}

#[test]
fn parse_angles_empty_seed_text_filtered() {
    let response = "\
ANGLE_TYPE: story
SEED_TEXT:
RATIONALE: Good.
EVIDENCE_IDS: 1";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);
    // Empty seed text should produce no angle
    assert!(angles.is_empty());
}

#[test]
fn parse_angles_with_formatting_artifacts() {
    let response = "\
1. **ANGLE_TYPE**: story
- **SEED_TEXT**: \"We grew 45%\"
**RATIONALE**: Strong narrative.
**EVIDENCE_IDS**: 1, 2
---
2. **ANGLE_TYPE**: hot_take
- **SEED_TEXT**: Growth without profit is a leak
**RATIONALE**: Bold claim.
**EVIDENCE_IDS**: 2";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);
    assert_eq!(angles.len(), 2);
    assert_eq!(angles[0].angle_type, AngleType::Story);
    // seed_text should have quotes stripped
    assert_eq!(angles[0].seed_text, "We grew 45%");
    assert_eq!(angles[1].angle_type, AngleType::HotTake);
}

#[test]
fn parse_angles_no_trailing_separator() {
    // Last block has no --- separator
    let response = "\
ANGLE_TYPE: listicle
SEED_TEXT: Top 3 insights
RATIONALE: Lists work.
EVIDENCE_IDS: 1, 2, 3";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);
    assert_eq!(angles.len(), 1);
    assert_eq!(angles[0].angle_type, AngleType::Listicle);
    assert_eq!(angles[0].evidence.len(), 3);
}

#[test]
fn parse_angles_alt_separator() {
    let response = "\
ANGLE_TYPE: story
SEED_TEXT: Tale one
RATIONALE: r1.
EVIDENCE_IDS: 1
- - -
ANGLE_TYPE: hot_take
SEED_TEXT: Bold claim
RATIONALE: r2.
EVIDENCE_IDS: 2";

    let evidence = sample_evidence();
    let angles = parse_angles_response(response, &evidence);
    assert_eq!(angles.len(), 2);
}

// -- build_angle_generation_prompt tests --

#[test]
fn prompt_includes_product_and_topic() {
    let biz = BusinessProfile {
        product_name: "TestApp".to_string(),
        product_description: "A test product".to_string(),
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Growth", &evidence, None);
    assert!(prompt.contains("TestApp"));
    assert!(prompt.contains("A test product"));
    assert!(prompt.contains("Topic: Growth"));
}

#[test]
fn prompt_includes_audience_when_set() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        target_audience: "indie hackers".to_string(),
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Topic", &evidence, None);
    assert!(prompt.contains("indie hackers"));
}

#[test]
fn prompt_excludes_audience_when_empty() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        target_audience: String::new(),
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Topic", &evidence, None);
    assert!(!prompt.contains("Your audience"));
}

#[test]
fn prompt_includes_brand_voice() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        brand_voice: Some("witty and irreverent".to_string()),
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Topic", &evidence, None);
    assert!(prompt.contains("witty and irreverent"));
}

#[test]
fn prompt_includes_selection_context() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt =
        build_angle_generation_prompt(&biz, "Topic", &evidence, Some("Selected vault text here"));
    assert!(prompt.contains("Selected vault context"));
    assert!(prompt.contains("Selected vault text here"));
}

#[test]
fn prompt_excludes_selection_context_when_none() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Topic", &evidence, None);
    assert!(!prompt.contains("Selected vault context"));
}

#[test]
fn prompt_includes_persona_opinions() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        persona_opinions: vec!["AI is overhyped".to_string()],
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Topic", &evidence, None);
    assert!(prompt.contains("Opinions you hold"));
    assert!(prompt.contains("AI is overhyped"));
}

#[test]
fn prompt_includes_persona_experiences() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        persona_experiences: vec!["Built 3 startups".to_string()],
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Topic", &evidence, None);
    assert!(prompt.contains("Experiences you can reference"));
    assert!(prompt.contains("Built 3 startups"));
}

#[test]
fn prompt_lists_evidence_with_confidence() {
    let biz = BusinessProfile {
        product_name: "App".to_string(),
        product_description: "desc".to_string(),
        ..Default::default()
    };
    let evidence = sample_evidence();
    let prompt = build_angle_generation_prompt(&biz, "Topic", &evidence, None);
    assert!(prompt.contains("[1] (data_point)"));
    assert!(prompt.contains("45% growth"));
    assert!(prompt.contains("[confidence: 0.8]"));
}