nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! # Cosmic Theme Bridge Layer
//!
//! Bridges the new `TokenResolver` design system with the `Theme` API.
//!
//! This module provides `CosmicTheme`, an adapter that wraps `TokenResolver`
//! while exposing a `Theme`-compatible API.
//!
//! ## Architecture
//!
//! ```text
//! ┌──────────────────┐     ┌──────────────────┐     ┌──────────────────┐
//! │   CosmicTheme    │ ──► │   TokenResolver  │ ──► │  SemanticColors  │
//! │   (Adapter)      │     │      │     │  ColorPalette    │
//! └──────────────────┘     └──────────────────┘     └──────────────────┘
//!//!//! ┌──────────────────┐
//! │      Theme       │ (Convenience API)
//! │                  │
//! └──────────────────┘
//! ```
//!
//! ## Usage
//!
//! ```rust,ignore
//! use nika::tui::cosmic_theme::CosmicTheme;
//! use nika::tui::tokens::CosmicVariant;
//!
//! // Create with default variant
//! let mut cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);
//!
//! // Get Theme
//! let theme = cosmic.as_theme();
//!
//! // Get resolver for new code
//! let resolver = cosmic.resolver();
//!
//! // Cycle through variants
//! cosmic.cycle(); // Dark → Light → Violet → Dark
//! ```

use std::cell::OnceCell;

use super::theme::{Theme, ThemeMode};
use super::tokens::{CosmicVariant, TokenResolver};

// ═══════════════════════════════════════════════════════════════════════════════
// COSMIC THEME ADAPTER
// ═══════════════════════════════════════════════════════════════════════════════

/// Bridge between TokenResolver and Theme API.
///
/// `CosmicTheme` wraps a `TokenResolver` and provides both:
/// - `Theme` access via `as_theme()` for existing widgets
/// - Direct `TokenResolver` access via `resolver()` for new code
///
/// This enables incremental adoption without breaking existing usages.
///
/// # Performance
///
/// The `as_theme()` method caches its result using `OnceCell`. The cache is
/// automatically invalidated when `cycle()` or `set_variant()` is called.
#[derive(Debug)]
pub struct CosmicTheme {
    resolver: TokenResolver,
    variant: CosmicVariant,
    /// Cached Theme for hot-path access (invalidated on variant change)
    cached_theme: OnceCell<Theme>,
}

impl Clone for CosmicTheme {
    fn clone(&self) -> Self {
        Self {
            resolver: self.resolver.clone(),
            variant: self.variant,
            cached_theme: OnceCell::new(), // Fresh cache for clone
        }
    }
}

impl Default for CosmicTheme {
    fn default() -> Self {
        Self::new(CosmicVariant::default())
    }
}

impl CosmicTheme {
    /// Create a new CosmicTheme with the given variant.
    ///
    /// # Arguments
    ///
    /// * `variant` - The cosmic variant (Dark, Light, or Violet)
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);
    /// ```
    pub fn new(variant: CosmicVariant) -> Self {
        Self {
            resolver: variant.resolver(),
            variant,
            cached_theme: OnceCell::new(),
        }
    }

    /// Get the current variant.
    pub fn variant(&self) -> CosmicVariant {
        self.variant
    }

    /// Get Theme for existing widgets.
    ///
    /// This creates a `Theme` struct populated from the TokenResolver's
    /// semantic colors. Use this for existing widgets that expect `&Theme`.
    ///
    /// # Performance
    ///
    /// This method caches the Theme using `OnceCell`. Subsequent calls
    /// return a clone of the cached value. The cache is invalidated
    /// when `cycle()` or `set_variant()` is called.
    pub fn as_theme(&self) -> Theme {
        self.cached_theme
            .get_or_init(|| Theme::from_resolver(&self.resolver))
            .clone()
    }

    /// Get resolver for new code.
    ///
    /// New widgets should use the resolver directly for:
    /// - Semantic color access (bg_primary, text_primary, etc.)
    /// - Verb colors
    /// - Status colors
    pub fn resolver(&self) -> &TokenResolver {
        &self.resolver
    }

    /// Cycle to the next theme variant.
    ///
    /// Order: CosmicDark → CosmicLight → CosmicViolet → CosmicDark
    ///
    /// Invalidates the cached Theme.
    pub fn cycle(&mut self) {
        self.variant = self.variant.cycle();
        self.resolver = self.variant.resolver();
        self.cached_theme.take(); // Invalidate cache
    }

    /// Set a specific variant.
    ///
    /// Invalidates the cached Theme.
    pub fn set_variant(&mut self, variant: CosmicVariant) {
        self.variant = variant;
        self.resolver = variant.resolver();
        self.cached_theme.take(); // Invalidate cache
    }

    /// Get display label for current variant.
    pub fn label(&self) -> &'static str {
        self.variant.label()
    }

    /// Check if current variant is dark.
    pub fn is_dark(&self) -> bool {
        self.resolver.semantic().is_dark()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// THEME MODE CONVERSION
// ═══════════════════════════════════════════════════════════════════════════════

impl From<ThemeMode> for CosmicVariant {
    /// Convert ThemeMode to CosmicVariant.
    ///
    /// Mapping:
    /// - Dark → CosmicDark
    /// - Light → CosmicLight
    /// - Solarized → CosmicDark (closest match)
    fn from(mode: ThemeMode) -> Self {
        match mode {
            ThemeMode::Dark => CosmicVariant::CosmicDark,
            ThemeMode::Light => CosmicVariant::CosmicLight,
            ThemeMode::Solarized => CosmicVariant::CosmicDark, // Closest match
        }
    }
}

impl From<CosmicVariant> for ThemeMode {
    /// Convert CosmicVariant to ThemeMode.
    ///
    /// Mapping:
    /// - CosmicDark → Dark
    /// - CosmicLight → Light
    /// - CosmicViolet → Dark (closest match)
    fn from(variant: CosmicVariant) -> Self {
        match variant {
            CosmicVariant::CosmicDark => ThemeMode::Dark,
            CosmicVariant::CosmicLight => ThemeMode::Light,
            CosmicVariant::CosmicViolet => ThemeMode::Dark, // Closest match
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// THEME FROM RESOLVER (Bridge Method)
// ═══════════════════════════════════════════════════════════════════════════════

impl Theme {
    /// Create Theme from TokenResolver (bridge method).
    ///
    /// Maps all 35+ Theme fields from the resolver's semantic colors.
    pub fn from_resolver(resolver: &TokenResolver) -> Self {
        let semantic = resolver.semantic();

        Self {
            // ═══ REALMS ═══
            // Using accent colors for realms (no direct mapping)
            realm_shared: semantic.accent_secondary, // Cyan for shared
            realm_org: semantic.status_success,      // Emerald for org

            // ═══ TRAITS (Data Origin) ═══
            trait_defined: semantic.text_muted, // Gray for defined
            trait_authored: semantic.accent_primary, // Violet for authored
            trait_imported: semantic.verb_exec, // Amber for imported
            trait_generated: semantic.status_success, // Emerald for generated
            trait_retrieved: semantic.accent_secondary, // Cyan for retrieved

            // ═══ TASK STATUS ═══
            status_pending: semantic.text_muted,
            status_running: semantic.status_warning,
            status_success: semantic.status_success,
            status_failed: semantic.status_error,
            status_paused: semantic.accent_secondary,

            // ═══ MISSION PHASES ═══
            phase_launch: semantic.accent_tertiary,    // Pink
            phase_orbital: semantic.status_info,       // Blue
            phase_rendezvous: semantic.accent_primary, // Violet

            // ═══ MCP TOOLS ═══
            mcp_describe: semantic.status_info,    // Blue
            mcp_context: semantic.accent_tertiary, // Pink
            mcp_search: semantic.verb_exec,        // Amber
            mcp_audit: semantic.accent_primary,    // Violet
            mcp_write: semantic.status_success,    // Emerald

            // ═══ UI ELEMENTS ═══
            border_normal: semantic.border_default,
            border_focused: semantic.border_focused,
            text: semantic.text_primary, // alias for selection
            text_primary: semantic.text_primary,
            text_secondary: semantic.text_secondary,
            text_muted: semantic.text_muted,
            background: semantic.bg_primary,
            highlight: semantic.accent_primary,
            selection: semantic.accent_secondary, // cyan for selection

            // ═══ GIT GUTTER ═══
            git_added: semantic.status_success, // Green for added lines
            git_modified: semantic.status_warning, // Yellow/Orange for modified
            git_deleted: semantic.status_error, // Red for deleted markers

            // ═══ SCROLLBAR ═══
            scrollbar_thumb: semantic.scrollbar_thumb,
            scrollbar_track: semantic.scrollbar_track,
            scrollbar_arrows: semantic.scrollbar_arrows,
        }
    }

    /// Create Cosmic Dark theme (new default).
    pub fn cosmic_dark() -> Self {
        Self::from_resolver(&TokenResolver::cosmic_dark())
    }

    /// Create Cosmic Light theme.
    pub fn cosmic_light() -> Self {
        Self::from_resolver(&TokenResolver::cosmic_light())
    }

    /// Create Cosmic Violet theme.
    pub fn cosmic_violet() -> Self {
        Self::from_resolver(&TokenResolver::cosmic_violet())
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::style::Color;

    #[test]
    fn test_cosmic_theme_new_default() {
        let cosmic = CosmicTheme::default();
        assert_eq!(cosmic.variant(), CosmicVariant::CosmicDark);
    }

    #[test]
    fn test_cosmic_theme_new_with_variant() {
        let cosmic = CosmicTheme::new(CosmicVariant::CosmicLight);
        assert_eq!(cosmic.variant(), CosmicVariant::CosmicLight);
    }

    #[test]
    fn test_cosmic_theme_cycle() {
        let mut cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);

        cosmic.cycle();
        assert_eq!(cosmic.variant(), CosmicVariant::CosmicLight);

        cosmic.cycle();
        assert_eq!(cosmic.variant(), CosmicVariant::CosmicViolet);

        cosmic.cycle();
        assert_eq!(cosmic.variant(), CosmicVariant::CosmicDark);
    }

    #[test]
    fn test_cosmic_theme_set_variant() {
        let mut cosmic = CosmicTheme::default();
        cosmic.set_variant(CosmicVariant::CosmicViolet);
        assert_eq!(cosmic.variant(), CosmicVariant::CosmicViolet);
    }

    #[test]
    fn test_cosmic_theme_label() {
        let cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);
        assert!(cosmic.label().contains("Dark"));

        let cosmic = CosmicTheme::new(CosmicVariant::CosmicLight);
        assert!(cosmic.label().contains("Light"));

        let cosmic = CosmicTheme::new(CosmicVariant::CosmicViolet);
        assert!(cosmic.label().contains("Violet"));
    }

    #[test]
    fn test_cosmic_theme_is_dark() {
        let dark = CosmicTheme::new(CosmicVariant::CosmicDark);
        assert!(dark.is_dark());

        let light = CosmicTheme::new(CosmicVariant::CosmicLight);
        assert!(!light.is_dark());

        let violet = CosmicTheme::new(CosmicVariant::CosmicViolet);
        assert!(violet.is_dark());
    }

    #[test]
    fn test_cosmic_theme_as_theme() {
        let cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);
        let theme = cosmic.as_theme();

        // Verify theme has correct background from resolver
        let resolver = cosmic.resolver();
        assert_eq!(theme.background, resolver.bg_primary());
        assert_eq!(theme.text_primary, resolver.text_primary());
    }

    #[test]
    fn test_cosmic_theme_resolver_access() {
        let cosmic = CosmicTheme::new(CosmicVariant::CosmicLight);
        let resolver = cosmic.resolver();

        // Light theme should have light background
        assert!(!resolver.semantic().is_dark());
    }

    #[test]
    fn test_theme_mode_to_cosmic_variant() {
        assert_eq!(
            CosmicVariant::from(ThemeMode::Dark),
            CosmicVariant::CosmicDark
        );
        assert_eq!(
            CosmicVariant::from(ThemeMode::Light),
            CosmicVariant::CosmicLight
        );
        assert_eq!(
            CosmicVariant::from(ThemeMode::Solarized),
            CosmicVariant::CosmicDark
        );
    }

    #[test]
    fn test_cosmic_variant_to_theme_mode() {
        assert_eq!(ThemeMode::from(CosmicVariant::CosmicDark), ThemeMode::Dark);
        assert_eq!(
            ThemeMode::from(CosmicVariant::CosmicLight),
            ThemeMode::Light
        );
        assert_eq!(
            ThemeMode::from(CosmicVariant::CosmicViolet),
            ThemeMode::Dark
        );
    }

    #[test]
    fn test_theme_from_resolver_cosmic_dark() {
        let theme = Theme::cosmic_dark();

        // Should have Slate-900 background
        assert_eq!(theme.background, Color::Rgb(15, 23, 42)); // #0f172a
    }

    #[test]
    fn test_theme_from_resolver_cosmic_light() {
        let theme = Theme::cosmic_light();

        // Should have Slate-50 background
        assert_eq!(theme.background, Color::Rgb(248, 250, 252)); // #f8fafc
    }

    #[test]
    fn test_theme_from_resolver_cosmic_violet() {
        let theme = Theme::cosmic_violet();

        // Should have Violet-950 background
        assert_eq!(theme.background, Color::Rgb(46, 16, 101)); // #2e1065
    }

    #[test]
    fn test_theme_from_resolver_has_all_fields() {
        let resolver = TokenResolver::cosmic_dark();
        let theme = Theme::from_resolver(&resolver);

        // Check key fields are populated (not default/Reset)
        assert_ne!(theme.realm_shared, Color::Reset);
        assert_ne!(theme.realm_org, Color::Reset);
        assert_ne!(theme.trait_defined, Color::Reset);
        assert_ne!(theme.status_pending, Color::Reset);
        assert_ne!(theme.phase_launch, Color::Reset);
        assert_ne!(theme.mcp_describe, Color::Reset);
        assert_ne!(theme.border_normal, Color::Reset);
        assert_ne!(theme.scrollbar_thumb, Color::Reset);
    }

    #[test]
    fn test_theme_cosmic_variants_differ() {
        let dark = Theme::cosmic_dark();
        let light = Theme::cosmic_light();
        let violet = Theme::cosmic_violet();

        // Backgrounds should all be different
        assert_ne!(dark.background, light.background);
        assert_ne!(dark.background, violet.background);
        assert_ne!(light.background, violet.background);
    }

    // ─────────────────────────────────────────────────────────────────────────
    // CACHING TESTS
    // ─────────────────────────────────────────────────────────────────────────

    #[test]
    fn test_as_theme_caches_result() {
        let cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);

        // First call populates cache
        let theme1 = cosmic.as_theme();

        // Second call returns cached value (same result)
        let theme2 = cosmic.as_theme();

        assert_eq!(theme1.background, theme2.background);
        assert_eq!(theme1.text_primary, theme2.text_primary);
    }

    #[test]
    fn test_cycle_invalidates_cache() {
        let mut cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);

        // Get cached dark theme
        let dark_theme = cosmic.as_theme();
        assert_eq!(dark_theme.background, Color::Rgb(15, 23, 42)); // Slate-900

        // Cycle to light
        cosmic.cycle();

        // Cache should be invalidated, new theme returned
        let light_theme = cosmic.as_theme();
        assert_eq!(light_theme.background, Color::Rgb(248, 250, 252)); // Slate-50
        assert_ne!(dark_theme.background, light_theme.background);
    }

    #[test]
    fn test_set_variant_invalidates_cache() {
        let mut cosmic = CosmicTheme::new(CosmicVariant::CosmicDark);

        // Get cached dark theme
        let dark_theme = cosmic.as_theme();

        // Set to violet
        cosmic.set_variant(CosmicVariant::CosmicViolet);

        // Cache should be invalidated
        let violet_theme = cosmic.as_theme();
        assert_eq!(violet_theme.background, Color::Rgb(46, 16, 101)); // Violet-950
        assert_ne!(dark_theme.background, violet_theme.background);
    }

    #[test]
    fn test_clone_gets_fresh_cache() {
        let cosmic1 = CosmicTheme::new(CosmicVariant::CosmicDark);

        // Populate cache on original
        let _ = cosmic1.as_theme();

        // Clone gets fresh cache (not shared)
        let cosmic2 = cosmic1.clone();

        // Both should return same values
        let theme1 = cosmic1.as_theme();
        let theme2 = cosmic2.as_theme();
        assert_eq!(theme1.background, theme2.background);
    }
}