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
//! Capability tier model for progressive activation.
//!
//! Defines four tiers that represent what the user can do based on their
//! current configuration state. Tiers are computed — never stored — so
//! they always reflect the live config.

use serde::{Deserialize, Serialize};

use super::Config;

/// Progressive activation tiers, ordered from least to most capable.
///
/// Each tier is a strict superset of the previous one.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CapabilityTier {
    /// No configuration exists yet.
    Unconfigured = 0,
    /// Business profile is filled — dashboard access, view settings.
    ProfileReady = 1,
    /// Profile + X credentials — discovery, search, scoring.
    ExplorationReady = 2,
    /// Exploration + valid LLM config — draft generation, reply composition.
    GenerationReady = 3,
    /// Generation + valid posting tokens — scheduled posting, autopilot.
    PostingReady = 4,
}

impl CapabilityTier {
    /// Human-readable label for this tier.
    pub fn label(self) -> &'static str {
        match self {
            Self::Unconfigured => "Unconfigured",
            Self::ProfileReady => "Profile Ready",
            Self::ExplorationReady => "Exploration Ready",
            Self::GenerationReady => "Generation Ready",
            Self::PostingReady => "Posting Ready",
        }
    }

    /// Short description of what this tier unlocks.
    pub fn description(self) -> &'static str {
        match self {
            Self::Unconfigured => "Complete onboarding to get started",
            Self::ProfileReady => "Dashboard access and settings",
            Self::ExplorationReady => "Content discovery and scoring",
            Self::GenerationReady => "AI draft generation and composition",
            Self::PostingReady => "Scheduled posting and autopilot",
        }
    }

    /// Returns a list of what's missing to reach the next tier.
    pub fn missing_for_next(self, config: &Config, can_post: bool) -> Vec<String> {
        match self {
            Self::Unconfigured => {
                let mut missing = Vec::new();
                if config.business.product_name.is_empty() {
                    missing.push("Product/profile name".to_string());
                }
                if config.business.product_description.trim().is_empty() {
                    missing.push("Product/profile description".to_string());
                }
                if config.business.product_keywords.is_empty() {
                    missing.push("Product keywords".to_string());
                }
                if config.business.industry_topics.is_empty()
                    && config.business.product_keywords.is_empty()
                {
                    missing.push("Industry topics".to_string());
                }
                missing
            }
            Self::ProfileReady => {
                let mut missing = Vec::new();
                let backend = config.x_api.provider_backend.as_str();
                let is_x_api = backend.is_empty() || backend == "x_api";
                if is_x_api && config.x_api.client_id.trim().is_empty() {
                    missing.push("X API client ID".to_string());
                }
                missing
            }
            Self::ExplorationReady => {
                let mut missing = Vec::new();
                if config.llm.provider.is_empty() {
                    missing.push("LLM provider".to_string());
                } else if matches!(
                    config.llm.provider.as_str(),
                    "openai" | "anthropic" | "groq"
                ) && config.llm.api_key.as_ref().map_or(true, |k| k.is_empty())
                {
                    missing.push("LLM API key".to_string());
                }
                missing
            }
            Self::GenerationReady => {
                if !can_post {
                    vec!["Valid posting credentials (OAuth tokens or scraper session)".to_string()]
                } else {
                    vec![]
                }
            }
            Self::PostingReady => vec![],
        }
    }
}

/// Compute the capability tier from config state and posting ability.
///
/// This is a pure function with no side effects — call it freely.
pub fn compute_tier(config: &Config, can_post: bool) -> CapabilityTier {
    // Tier 1: business profile
    if config.business.product_name.is_empty()
        || config.business.product_description.trim().is_empty()
        || (config.business.product_keywords.is_empty()
            && config.business.competitor_keywords.is_empty())
    {
        return CapabilityTier::Unconfigured;
    }

    // Tier 2: X credentials
    let backend = config.x_api.provider_backend.as_str();
    let has_x = if backend == "scraper" {
        true // scraper mode doesn't need client_id
    } else {
        // x_api mode (default)
        !config.x_api.client_id.trim().is_empty()
    };

    if !has_x {
        return CapabilityTier::ProfileReady;
    }

    // Tier 3: LLM config
    let has_llm = if config.llm.provider.is_empty() {
        false
    } else if config.llm.provider == "ollama" {
        true // ollama doesn't need an API key
    } else {
        config.llm.api_key.as_ref().is_some_and(|k| !k.is_empty())
    };

    if !has_llm {
        return CapabilityTier::ExplorationReady;
    }

    // Tier 4: can post
    if !can_post {
        return CapabilityTier::GenerationReady;
    }

    CapabilityTier::PostingReady
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;

    fn minimal_profile_config() -> Config {
        let mut config = Config::default();
        config.business.product_name = "TestProduct".to_string();
        config.business.product_description = "A test product".to_string();
        config.business.product_keywords = vec!["test".to_string()];
        config.business.industry_topics = vec!["testing".to_string()];
        config
    }

    #[test]
    fn test_unconfigured_tier() {
        let config = Config::default();
        assert_eq!(compute_tier(&config, false), CapabilityTier::Unconfigured);
    }

    #[test]
    fn test_profile_ready_tier() {
        let config = minimal_profile_config();
        assert_eq!(compute_tier(&config, false), CapabilityTier::ProfileReady);
    }

    #[test]
    fn test_exploration_ready_x_api() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc123".to_string();
        assert_eq!(
            compute_tier(&config, false),
            CapabilityTier::ExplorationReady
        );
    }

    #[test]
    fn test_exploration_ready_scraper() {
        let mut config = minimal_profile_config();
        config.x_api.provider_backend = "scraper".to_string();
        assert_eq!(
            compute_tier(&config, false),
            CapabilityTier::ExplorationReady
        );
    }

    #[test]
    fn test_generation_ready_cloud_provider() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc123".to_string();
        config.llm.provider = "openai".to_string();
        config.llm.api_key = Some("sk-test".to_string());
        assert_eq!(
            compute_tier(&config, false),
            CapabilityTier::GenerationReady
        );
    }

    #[test]
    fn test_generation_ready_ollama() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc123".to_string();
        config.llm.provider = "ollama".to_string();
        assert_eq!(
            compute_tier(&config, false),
            CapabilityTier::GenerationReady
        );
    }

    #[test]
    fn test_posting_ready() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc123".to_string();
        config.llm.provider = "anthropic".to_string();
        config.llm.api_key = Some("sk-ant-test".to_string());
        assert_eq!(compute_tier(&config, true), CapabilityTier::PostingReady);
    }

    #[test]
    fn test_tier_ordering() {
        assert!(CapabilityTier::Unconfigured < CapabilityTier::ProfileReady);
        assert!(CapabilityTier::ProfileReady < CapabilityTier::ExplorationReady);
        assert!(CapabilityTier::ExplorationReady < CapabilityTier::GenerationReady);
        assert!(CapabilityTier::GenerationReady < CapabilityTier::PostingReady);
    }

    #[test]
    fn test_missing_for_next_unconfigured() {
        let config = Config::default();
        let missing = CapabilityTier::Unconfigured.missing_for_next(&config, false);
        assert!(!missing.is_empty());
        assert!(missing.iter().any(|m| m.contains("name")));
    }

    #[test]
    fn test_missing_for_next_posting_ready() {
        let config = Config::default();
        let missing = CapabilityTier::PostingReady.missing_for_next(&config, true);
        assert!(missing.is_empty());
    }

    #[test]
    fn test_cloud_provider_without_key_stays_exploration() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc123".to_string();
        config.llm.provider = "openai".to_string();
        // No API key
        assert_eq!(
            compute_tier(&config, false),
            CapabilityTier::ExplorationReady
        );
    }

    // -----------------------------------------------------------------------
    // Additional capability coverage tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_tier_labels_non_empty() {
        let tiers = [
            CapabilityTier::Unconfigured,
            CapabilityTier::ProfileReady,
            CapabilityTier::ExplorationReady,
            CapabilityTier::GenerationReady,
            CapabilityTier::PostingReady,
        ];
        for tier in tiers {
            assert!(!tier.label().is_empty());
            assert!(!tier.description().is_empty());
        }
    }

    #[test]
    fn test_tier_debug_and_clone() {
        let tier = CapabilityTier::GenerationReady;
        let cloned = tier;
        assert_eq!(tier, cloned);
        let debug = format!("{:?}", tier);
        assert!(debug.contains("GenerationReady"));
    }

    #[test]
    fn test_tier_serde_roundtrip() {
        let tier = CapabilityTier::PostingReady;
        let json = serde_json::to_string(&tier).expect("serialize");
        assert_eq!(json, "\"posting_ready\"");
        let deserialized: CapabilityTier = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(deserialized, tier);
    }

    #[test]
    fn test_tier_serde_all_variants() {
        let expected = [
            (CapabilityTier::Unconfigured, "\"unconfigured\""),
            (CapabilityTier::ProfileReady, "\"profile_ready\""),
            (CapabilityTier::ExplorationReady, "\"exploration_ready\""),
            (CapabilityTier::GenerationReady, "\"generation_ready\""),
            (CapabilityTier::PostingReady, "\"posting_ready\""),
        ];
        for (tier, expected_json) in expected {
            let json = serde_json::to_string(&tier).expect("serialize");
            assert_eq!(json, expected_json, "mismatch for {:?}", tier);
        }
    }

    #[test]
    fn test_missing_for_next_profile_ready() {
        let config = minimal_profile_config();
        let missing = CapabilityTier::ProfileReady.missing_for_next(&config, false);
        assert!(!missing.is_empty());
        assert!(missing.iter().any(|m| m.contains("X API")));
    }

    #[test]
    fn test_missing_for_next_exploration_ready() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc".to_string();
        let missing = CapabilityTier::ExplorationReady.missing_for_next(&config, false);
        assert!(!missing.is_empty());
        assert!(missing.iter().any(|m| m.contains("LLM")));
    }

    #[test]
    fn test_missing_for_next_exploration_ready_with_provider_no_key() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc".to_string();
        config.llm.provider = "anthropic".to_string();
        // No API key
        let missing = CapabilityTier::ExplorationReady.missing_for_next(&config, false);
        assert!(!missing.is_empty());
        assert!(missing.iter().any(|m| m.contains("API key")));
    }

    #[test]
    fn test_missing_for_next_generation_ready_no_post() {
        let config = minimal_profile_config();
        let missing = CapabilityTier::GenerationReady.missing_for_next(&config, false);
        assert!(!missing.is_empty());
        assert!(missing.iter().any(|m| m.contains("posting")));
    }

    #[test]
    fn test_missing_for_next_generation_ready_can_post() {
        let config = minimal_profile_config();
        let missing = CapabilityTier::GenerationReady.missing_for_next(&config, true);
        assert!(missing.is_empty());
    }

    #[test]
    fn test_unconfigured_empty_description() {
        let mut config = Config::default();
        config.business.product_name = "Test".to_string();
        config.business.product_description = "   ".to_string(); // whitespace only
        config.business.product_keywords = vec!["kw".to_string()];
        assert_eq!(compute_tier(&config, false), CapabilityTier::Unconfigured);
    }

    #[test]
    fn test_competitor_keywords_count_for_profile() {
        let mut config = Config::default();
        config.business.product_name = "Test".to_string();
        config.business.product_description = "A product".to_string();
        // No product_keywords, but has competitor_keywords
        config.business.competitor_keywords = vec!["rival".to_string()];
        assert_eq!(compute_tier(&config, false), CapabilityTier::ProfileReady);
    }

    #[test]
    fn test_unconfigured_missing_with_some_fields() {
        let mut config = Config::default();
        config.business.product_name = "Test".to_string();
        // Missing description and keywords
        let missing = CapabilityTier::Unconfigured.missing_for_next(&config, false);
        assert!(missing.iter().any(|m| m.contains("description")));
        assert!(missing.iter().any(|m| m.contains("keywords")));
    }

    #[test]
    fn test_profile_ready_scraper_backend_no_client_id() {
        let mut config = minimal_profile_config();
        config.x_api.provider_backend = "scraper".to_string();
        // No client_id needed for scraper
        let missing = CapabilityTier::ProfileReady.missing_for_next(&config, false);
        assert!(missing.is_empty());
    }

    #[test]
    fn test_cloud_provider_with_empty_key_stays_exploration() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc123".to_string();
        config.llm.provider = "anthropic".to_string();
        config.llm.api_key = Some("".to_string());
        assert_eq!(
            compute_tier(&config, false),
            CapabilityTier::ExplorationReady
        );
    }

    #[test]
    fn test_ollama_no_key_reaches_generation() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "abc123".to_string();
        config.llm.provider = "ollama".to_string();
        // No API key needed for ollama
        assert_eq!(
            compute_tier(&config, false),
            CapabilityTier::GenerationReady
        );
    }

    #[test]
    fn test_whitespace_client_id_stays_profile() {
        let mut config = minimal_profile_config();
        config.x_api.client_id = "   ".to_string();
        assert_eq!(compute_tier(&config, false), CapabilityTier::ProfileReady);
    }
}