rab-agent 0.1.7

rab is a lightweight, extensible, Rust-based coding agent.
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
// ── Pi-compatible /export and /import commands ─────────────────────
//
// Full parity with pi's export functionality:
//
//   /export [path]      — Export session to HTML (default) or JSONL (.jsonl)
//   /import <path>      — Import and resume a session from a JSONL file
//
// JSONL export writes the session header + all branch entries as JSONL,
// re-chaining parentId to form a linear sequence (pi-compatible).
//
// HTML export generates a self-contained HTML file using pi's template
// assets (template.html, template.css, template.js, marked.min.js,
// highlight.min.js), with session data embedded as base64 JSON.

use crate::agent::extension::{CommandHandler, CommandResult};
use crate::agent::session::model::{CURRENT_SESSION_VERSION, Session, SessionHeader};
use std::path::{Path, PathBuf};

// ── Template assets ────────────────────────────────────────────────
// Embedded from pi's export-html/ directory. Using include_bytes! to
// avoid escaping issues with JS/CSS content.

mod templates {
    pub const TEMPLATE_HTML: &[u8] = include_bytes!("export/templates/template.html");
    pub const TEMPLATE_CSS: &[u8] = include_bytes!("export/templates/template.css");
    pub const TEMPLATE_JS: &[u8] = include_bytes!("export/templates/template.js");
    pub const MARKED_JS: &[u8] = include_bytes!("export/templates/vendor/marked.min.js");
    pub const HIGHLIGHT_JS: &[u8] = include_bytes!("export/templates/vendor/highlight.min.js");
}

// ── Path argument parsing (pi-compatible) ────────────────────────
//
// Matches pi's `getPathCommandArgument()` exactly:
//   /export            → None
//   /export path.html  → Some("path.html")
//   /export "path with spaces.html" → Some("path with spaces.html")
//   /export 'path with spaces.html' → Some("path with spaces.html")

/// Parse the path argument from a command text like `/export path` or `/import path`.
/// Returns `None` if no argument is given (command used bare).
pub fn get_path_command_argument(text: &str, command: &str) -> Option<String> {
    if text == command {
        return None;
    }
    let prefix = format!("{} ", command);
    if !text.starts_with(&prefix) {
        return None;
    }

    let args_string = text[prefix.len()..].trim_start();
    if args_string.is_empty() {
        return None;
    }

    let first_char = args_string.chars().next().unwrap();
    if first_char == '"' || first_char == '\'' {
        let closing = args_string[1..].find(first_char)?;
        return Some(args_string[1..=closing].to_string());
    }

    let first_whitespace = args_string.find(char::is_whitespace);
    match first_whitespace {
        Some(idx) => Some(args_string[..idx].to_string()),
        None => Some(args_string.to_string()),
    }
}

// ── Export error type ──────────────────────────────────────────────

#[derive(Debug)]
pub enum ExportError {
    NoSession,
    InMemorySession,
    IoError(std::io::Error),
    JsonError(serde_json::Error),
    TemplateError(String),
}

impl std::fmt::Display for ExportError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExportError::NoSession => write!(f, "No active session"),
            ExportError::InMemorySession => {
                write!(
                    f,
                    "Cannot export an in-memory session without a session file"
                )
            }
            ExportError::IoError(e) => write!(f, "IO error: {}", e),
            ExportError::JsonError(e) => write!(f, "JSON error: {}", e),
            ExportError::TemplateError(e) => write!(f, "Template error: {}", e),
        }
    }
}

impl std::error::Error for ExportError {}

impl From<std::io::Error> for ExportError {
    fn from(e: std::io::Error) -> Self {
        ExportError::IoError(e)
    }
}

impl From<serde_json::Error> for ExportError {
    fn from(e: serde_json::Error) -> Self {
        ExportError::JsonError(e)
    }
}

// ── JSONL export ───────────────────────────────────────────────────
//
// Pi-compatible: writes session header + branch entries as JSONL,
// re-chaining parentId to form a linear sequence.

/// Export the current session branch to a JSONL file.
///
/// * `session` — The session to export
/// * `cwd` — Current working directory (for resolving relative paths)
/// * `output_path` — Target file path. If omitted, generates a timestamped name in cwd
///
/// Returns the resolved output file path.
pub fn export_to_jsonl(
    session: &Session,
    cwd: &Path,
    output_path: Option<&str>,
) -> Result<PathBuf, ExportError> {
    let file_path = match output_path {
        Some(p) => crate::builtin::resolve_path(p, cwd),
        None => {
            let ts = chrono::Utc::now()
                .format("session-%Y-%m-%dT%H-%M-%S")
                .to_string();
            cwd.join(format!("{}.jsonl", ts))
        }
    };

    // Create parent directory
    if let Some(parent) = file_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Build header (pi-compatible: uses fresh timestamp, not original createdAt)
    let meta = session.metadata();
    let header = SessionHeader {
        type_: "session".to_string(),
        version: Some(CURRENT_SESSION_VERSION),
        id: meta.id.clone(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        cwd: meta.cwd.clone(),
        parent_session: meta.parent_session_path.clone(),
    };

    // Get branch entries (pi-compatible: linearized path from root to leaf)
    let branch_entries = session
        .get_branch(None)
        .map_err(|e| ExportError::TemplateError(format!("Failed to get branch: {}", e)))?;

    // Build JSONL content
    let mut lines = Vec::with_capacity(branch_entries.len() + 1);
    lines.push(serde_json::to_string(&header)?);

    // Re-chain parentIds to form a linear sequence (pi-compatible)
    let mut prev_id: Option<String> = None;
    for entry in &branch_entries {
        let mut value = serde_json::to_value(entry)?;
        if let Some(obj) = value.as_object_mut() {
            match prev_id {
                Some(ref pid) => {
                    obj.insert(
                        "parentId".to_string(),
                        serde_json::Value::String(pid.clone()),
                    );
                }
                None => {
                    obj.insert("parentId".to_string(), serde_json::Value::Null);
                }
            }
        }
        prev_id = Some(entry.id().to_string());
        lines.push(serde_json::to_string(&value)?);
    }

    let content = lines.join("\n") + "\n";
    std::fs::write(&file_path, content)?;

    Ok(file_path)
}

// ── HTML export ────────────────────────────────────────────────────
//
// Pi-compatible: generates a self-contained HTML file using pi's
// template assets with session data embedded as base64 JSON.

/// Theme color mapping for export CSS variables.
struct ExportThemeColors {
    /// CSS custom property declarations
    theme_vars: String,
    /// Body background color
    body_bg: String,
    /// Card/container background color
    container_bg: String,
    /// Info block background color
    info_bg: String,
}

/// Load theme colors for export from the current theme.
/// Mirrors pi's `generateThemeVars()` and `deriveExportColors()`.
fn load_export_theme_colors(theme_name: Option<&str>) -> ExportThemeColors {
    // Try to load the theme config and resolve hex colors
    let colors = resolve_theme_hex_colors(theme_name.unwrap_or("dark"));

    // Build CSS custom property declarations (pi-compatible)
    let mut lines: Vec<String> = Vec::new();
    // Explicitly list the keys we want in the export (matching pi's approach)
    let export_keys = [
        "text",
        "dim",
        "muted",
        "accent",
        "success",
        "error",
        "warning",
        "border",
        "borderAccent",
        "selectedBg",
        "hover",
        "userMessageBg",
        "userMessageText",
        "thinkingText",
        "customMessageBg",
        "customMessageLabel",
        "customMessageText",
        "toolPendingBg",
        "toolSuccessBg",
        "toolErrorBg",
        "toolOutput",
        "toolTitle",
        "toolDiffAdded",
        "toolDiffRemoved",
        "toolDiffContext",
        "mdHeading",
        "mdLink",
        "mdLinkUrl",
        "mdCode",
        "mdCodeBlock",
        "mdCodeBlockBorder",
        "mdQuote",
        "mdQuoteBorder",
        "mdHr",
        "mdListBullet",
        "syntaxComment",
        "syntaxKeyword",
        "syntaxNumber",
        "syntaxString",
        "syntaxFunction",
        "syntaxType",
        "syntaxVariable",
        "syntaxOperator",
        "syntaxPunctuation",
    ];

    for key in &export_keys {
        if let Some(value) = colors.get(*key) {
            lines.push(format!("--{}: {};", key, value));
        }
    }

    // For any remaining colors from the config that aren't in export_keys, include them too
    for (key, value) in &colors {
        if !export_keys.contains(&key.as_str()) {
            lines.push(format!("--{}: {};", key, value));
        }
    }

    let theme_vars = lines.join("\n      ");

    // Derive export background colors from userMessageBg (pi-compatible)
    let user_message_bg = colors
        .get("userMessageBg")
        .map(|s| s.as_str())
        .unwrap_or("#343541");

    let derived = derive_export_colors(user_message_bg);

    ExportThemeColors {
        theme_vars,
        body_bg: derived.page_bg,
        container_bg: derived.card_bg,
        info_bg: derived.info_bg,
    }
}

/// Parse a hex color string to RGB components.
fn parse_hex_color(hex: &str) -> Option<(u8, u8, u8)> {
    let hex = hex.trim_start_matches('#');
    if hex.len() != 6 {
        return None;
    }
    let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
    let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
    let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
    Some((r, g, b))
}

/// Calculate relative luminance of an sRGB color.
fn relative_luminance(r: u8, g: u8, b: u8) -> f64 {
    let to_linear = |c: u8| {
        let s = c as f64 / 255.0;
        if s <= 0.03928 {
            s / 12.92
        } else {
            ((s + 0.055) / 1.055).powf(2.4)
        }
    };
    0.2126 * to_linear(r) + 0.7152 * to_linear(g) + 0.0722 * to_linear(b)
}

/// Adjust color brightness — factor > 1 lightens, < 1 darkens.
fn adjust_brightness(hex: &str, factor: f64) -> String {
    let (r, g, b) = match parse_hex_color(hex) {
        Some(c) => c,
        None => return hex.to_string(),
    };
    let adj = |c: u8| (c as f64 * factor).clamp(0.0, 255.0).round() as u8;
    format!("#{:02x}{:02x}{:02x}", adj(r), adj(g), adj(b))
}

/// Derive export background colors from a base color (pi-compatible).
struct DerivedExportColors {
    page_bg: String,
    card_bg: String,
    info_bg: String,
}

fn derive_export_colors(base_color: &str) -> DerivedExportColors {
    let rgb = match parse_hex_color(base_color) {
        Some(c) => c,
        None => {
            return DerivedExportColors {
                page_bg: "#18181e".to_string(),
                card_bg: "#1e1e24".to_string(),
                info_bg: "#3c3728".to_string(),
            };
        }
    };

    let luminance = relative_luminance(rgb.0, rgb.1, rgb.2);
    let is_light = luminance > 0.5;

    if is_light {
        DerivedExportColors {
            page_bg: adjust_brightness(base_color, 0.96),
            card_bg: base_color.to_string(),
            info_bg: format!(
                "#{:02x}{:02x}{:02x}",
                (rgb.0 as u16 + 10).min(255) as u8,
                (rgb.1 as u16 + 5).min(255) as u8,
                (rgb.2 as u16).max(20).saturating_sub(20) as u8,
            ),
        }
    } else {
        DerivedExportColors {
            page_bg: adjust_brightness(base_color, 0.7),
            card_bg: adjust_brightness(base_color, 0.85),
            info_bg: format!(
                "#{:02x}{:02x}{:02x}",
                (rgb.0 as u16 + 20).min(255) as u8,
                (rgb.1 as u16 + 15).min(255) as u8,
                rgb.2,
            ),
        }
    }
}

/// Resolve theme hex colors by name.
/// Falls back to dark theme if the requested theme can't be loaded.
fn resolve_theme_hex_colors(theme_name: &str) -> std::collections::HashMap<String, String> {
    // Use the theme system's own resolution
    let config = crate::agent::ui::theme::load_theme_config(theme_name)
        .or_else(|_| crate::agent::ui::theme::load_theme_config("dark"))
        .unwrap_or_else(|_| {
            // Ultimate fallback: construct minimal dark theme config
            use crate::agent::ui::theme::{ColorValue, ThemeConfig};
            let mut colors = std::collections::HashMap::new();
            let entries: Vec<(&str, &str)> = vec![
                ("text", "#d4d4d4"),
                ("dim", "#666666"),
                ("muted", "#808080"),
                ("accent", "#8abeb7"),
                ("success", "#b5bd68"),
                ("error", "#cc6666"),
                ("warning", "#e8a838"),
                ("border", "#333"),
                ("borderAccent", "#8abeb7"),
                ("selectedBg", "#2a2a2a"),
                ("hover", "#333"),
                ("userMessageBg", "#343541"),
                ("userMessageText", "#d4d4d4"),
                ("thinkingText", "#808080"),
                ("customMessageBg", "#1e1e24"),
                ("customMessageLabel", "#8abeb7"),
                ("customMessageText", "#d4d4d4"),
                ("toolPendingBg", "#282832"),
                ("toolSuccessBg", "#283228"),
                ("toolErrorBg", "#3c2828"),
                ("toolOutput", "#808080"),
                ("toolTitle", "#d4d4d4"),
                ("toolDiffAdded", "#22c55e"),
                ("toolDiffRemoved", "#ef4444"),
                ("toolDiffContext", "#808080"),
                ("mdHeading", "#e8a838"),
                ("mdLink", "#8abeb7"),
                ("mdLinkUrl", "#5f87af"),
                ("mdCode", "#e8a838"),
                ("mdCodeBlock", "#808080"),
                ("mdCodeBlockBorder", "#444"),
                ("mdQuote", "#808080"),
                ("mdQuoteBorder", "#555"),
                ("mdHr", "#555"),
                ("mdListBullet", "#8abeb7"),
                ("syntaxComment", "#6a9955"),
                ("syntaxKeyword", "#569cd6"),
                ("syntaxNumber", "#b5cea8"),
                ("syntaxString", "#ce9178"),
                ("syntaxFunction", "#dcdcaa"),
                ("syntaxType", "#4ec9b0"),
                ("syntaxVariable", "#9cdcfe"),
                ("syntaxOperator", "#d4d4d4"),
                ("syntaxPunctuation", "#d4d4d4"),
            ];
            for (k, v) in entries {
                colors.insert(k.to_string(), ColorValue::HexOrVar(v.to_string()));
            }
            ThemeConfig {
                name: "dark".to_string(),
                vars: std::collections::HashMap::new(),
                colors,
            }
        });

    crate::agent::ui::theme::RabTheme::resolve_colors(&config)
}

/// Serialize session data to the JSON format expected by pi's template.
///
/// Returns a serde_json::Value matching pi's SessionData interface:
/// ```typescript
/// { header, entries, leafId, systemPrompt?, tools?, renderedTools? }
/// ```
fn build_session_data_json(
    session: &Session,
    system_prompt: Option<&str>,
) -> Result<serde_json::Value, ExportError> {
    let meta = session.metadata();

    let mut header = serde_json::json!({
        "type": "session",
        "version": CURRENT_SESSION_VERSION,
        "id": meta.id,
        "timestamp": meta.created_at,
        "cwd": meta.cwd,
    });

    // Pi-compatible: only include parentSession if it exists (omit when None)
    if let Some(ref ps) = meta.parent_session_path
        && let Some(obj) = header.as_object_mut()
    {
        obj.insert(
            "parentSession".to_string(),
            serde_json::Value::String(ps.clone()),
        );
    }

    let leaf_id = session.get_leaf_id();

    let mut data = serde_json::json!({
        "header": header,
        "entries": session.get_entries(),
        "leafId": leaf_id,
    });

    // Add optional systemPrompt
    if let Some(sp) = system_prompt
        && let Some(obj) = data.as_object_mut()
    {
        obj.insert(
            "systemPrompt".to_string(),
            serde_json::Value::String(sp.to_string()),
        );
    }

    // Note: `tools` and `renderedTools` are omitted for now.
    // The template JS handles their absence gracefully.
    // Tools can be added later when we have a tool renderer system.

    Ok(data)
}

/// Export the session to a self-contained HTML file.
///
/// * `session` — The session to export
/// * `system_prompt` — Optional system prompt text
/// * `cwd` — Current working directory (for resolving relative paths)
/// * `output_path` — Target file path. If omitted, generates a name in cwd
/// * `theme_name` — Theme name for colors (defaults to current theme)
///
/// Returns the resolved output file path.
pub fn export_to_html(
    session: &Session,
    system_prompt: Option<&str>,
    cwd: &Path,
    output_path: Option<&str>,
    theme_name: Option<&str>,
) -> Result<PathBuf, ExportError> {
    // Determine output path (pi-compatible default naming)
    let file_path = match output_path {
        Some(p) => crate::builtin::resolve_path(p, cwd),
        None => {
            let session_id = &session.metadata().id;
            let short_id = if session_id.len() > 8 {
                &session_id[..8]
            } else {
                session_id.as_str()
            };
            cwd.join(format!("rab-session-{}.html", short_id))
        }
    };

    // Create parent directory
    if let Some(parent) = file_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Resolve theme name (default to current theme)
    let theme_name = theme_name
        .map(|s| s.to_string())
        .unwrap_or_else(|| crate::agent::ui::theme::current_theme().name.clone());

    // Build session data JSON
    let session_data = build_session_data_json(session, system_prompt)?;
    let session_data_json = serde_json::to_string(&session_data)?;

    // Base64 encode session data (pi-compatible)
    use base64::Engine as _;
    let session_data_base64 =
        base64::engine::general_purpose::STANDARD.encode(session_data_json.as_bytes());

    // Load theme colors
    let export_colors = load_export_theme_colors(Some(&theme_name));

    // Read template files
    let template_html = String::from_utf8(templates::TEMPLATE_HTML.to_vec()).map_err(|e| {
        ExportError::TemplateError(format!("Invalid UTF-8 in template.html: {}", e))
    })?;
    let template_css = String::from_utf8(templates::TEMPLATE_CSS.to_vec())
        .map_err(|e| ExportError::TemplateError(format!("Invalid UTF-8 in template.css: {}", e)))?;
    let template_js = String::from_utf8(templates::TEMPLATE_JS.to_vec())
        .map_err(|e| ExportError::TemplateError(format!("Invalid UTF-8 in template.js: {}", e)))?;
    let marked_js = String::from_utf8(templates::MARKED_JS.to_vec()).map_err(|e| {
        ExportError::TemplateError(format!("Invalid UTF-8 in marked.min.js: {}", e))
    })?;
    let highlight_js = String::from_utf8(templates::HIGHLIGHT_JS.to_vec()).map_err(|e| {
        ExportError::TemplateError(format!("Invalid UTF-8 in highlight.min.js: {}", e))
    })?;

    // Build CSS with theme variables injected (pi-compatible)
    let css = template_css
        .replace("{{THEME_VARS}}", &export_colors.theme_vars)
        .replace("{{BODY_BG}}", &export_colors.body_bg)
        .replace("{{CONTAINER_BG}}", &export_colors.container_bg)
        .replace("{{INFO_BG}}", &export_colors.info_bg);

    // Build final HTML (pi-compatible template injection)
    let html = template_html
        .replace("{{CSS}}", &css)
        .replace("{{JS}}", &template_js)
        .replace("{{SESSION_DATA}}", &session_data_base64)
        .replace("{{MARKED_JS}}", &marked_js)
        .replace("{{HIGHLIGHT_JS}}", &highlight_js);

    std::fs::write(&file_path, html)?;

    Ok(file_path)
}

// ── /export command handler ────────────────────────────────────────

/// Handler for `/export` command.
/// Parses the path argument (pi-compatible) and returns ExportSession result.
pub struct ExportCommand;

impl CommandHandler for ExportCommand {
    fn execute(&self, args: &str) -> anyhow::Result<CommandResult> {
        let text = if args.is_empty() {
            "/export".to_string()
        } else {
            format!("/export {}", args)
        };

        let path = get_path_command_argument(&text, "/export");
        Ok(CommandResult::ExportSession { path })
    }
}

// ── /import command handler ────────────────────────────────────────

/// Handler for `/import` command.
/// Parses the path argument (pi-compatible) and returns ImportSession result.
pub struct ImportCommand;

impl CommandHandler for ImportCommand {
    fn execute(&self, args: &str) -> anyhow::Result<CommandResult> {
        let text = if args.is_empty() {
            "/import".to_string()
        } else {
            format!("/import {}", args)
        };

        let path = get_path_command_argument(&text, "/import");
        match path {
            Some(p) => Ok(CommandResult::ImportSession { path: p }),
            None => Ok(CommandResult::Info(
                "Usage: /import <path.jsonl>".to_string(),
            )),
        }
    }
}

// ── Tests ──────────────────────────────────────────────────────────

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

    #[test]
    fn test_get_path_no_arg() {
        assert_eq!(get_path_command_argument("/export", "/export"), None);
        assert_eq!(get_path_command_argument("/import", "/import"), None);
    }

    #[test]
    fn test_get_path_simple() {
        assert_eq!(
            get_path_command_argument("/export output.html", "/export"),
            Some("output.html".to_string())
        );
    }

    #[test]
    fn test_get_path_quoted_double() {
        assert_eq!(
            get_path_command_argument("/export \"my session.html\"", "/export"),
            Some("my session.html".to_string())
        );
    }

    #[test]
    fn test_get_path_quoted_single() {
        assert_eq!(
            get_path_command_argument("/export 'my session.html'", "/export"),
            Some("my session.html".to_string())
        );
    }

    #[test]
    fn test_get_path_no_close_quote() {
        assert_eq!(
            get_path_command_argument("/export \"no close", "/export"),
            None
        );
    }

    #[test]
    fn test_get_path_command_prefix_mismatch() {
        assert_eq!(
            get_path_command_argument("/exporter out.html", "/export"),
            None
        );
    }

    #[test]
    fn test_get_path_only_whitespace() {
        assert_eq!(get_path_command_argument("/export  ", "/export"), None);
        assert_eq!(get_path_command_argument("/import  ", "/import"), None);
    }

    #[test]
    fn test_export_command_no_args() {
        let cmd = ExportCommand;
        let result = cmd.execute("").unwrap();
        match result {
            CommandResult::ExportSession { path } => assert_eq!(path, None),
            _ => panic!("Expected ExportSession with None"),
        }
    }

    #[test]
    fn test_export_command_with_path() {
        let cmd = ExportCommand;
        let result = cmd.execute("test.html").unwrap();
        match result {
            CommandResult::ExportSession { path } => {
                assert_eq!(path, Some("test.html".to_string()));
            }
            _ => panic!("Expected ExportSession with path"),
        }
    }

    #[test]
    fn test_import_command_no_args() {
        let cmd = ImportCommand;
        let result = cmd.execute("").unwrap();
        match result {
            CommandResult::Info(msg) => assert!(msg.contains("Usage:")),
            _ => panic!("Expected Info message"),
        }
    }

    #[test]
    fn test_import_command_with_path() {
        let cmd = ImportCommand;
        let result = cmd.execute("session.jsonl").unwrap();
        match result {
            CommandResult::ImportSession { path } => {
                assert_eq!(path, "session.jsonl");
            }
            _ => panic!("Expected ImportSession"),
        }
    }

    #[test]
    fn test_parse_hex_color() {
        assert_eq!(parse_hex_color("#ff0000"), Some((255, 0, 0)));
        assert_eq!(parse_hex_color("00ff00"), Some((0, 255, 0)));
        assert_eq!(parse_hex_color("#fff"), None);
        assert_eq!(parse_hex_color("invalid"), None);
    }

    #[test]
    fn test_relative_luminance() {
        let black = relative_luminance(0, 0, 0);
        let white = relative_luminance(255, 255, 255);
        assert!(black < 0.1);
        assert!(white > 0.9);
    }

    #[test]
    fn test_derive_export_colors_dark() {
        let derived = derive_export_colors("#343541");
        // Dark theme: pageBg should be darker than cardBg
        let page_rgb = parse_hex_color(&derived.page_bg).unwrap();
        let card_rgb = parse_hex_color(&derived.card_bg).unwrap();
        assert!(page_rgb.0 < card_rgb.0); // Darker page background
    }

    #[test]
    fn test_derive_export_colors_light() {
        let derived = derive_export_colors("#ffffff");
        // Light theme: pageBg should be slightly darker than white
        let page_rgb = parse_hex_color(&derived.page_bg).unwrap();
        assert!(page_rgb.0 < 255);
    }

    #[test]
    fn test_adjust_brightness() {
        let result = adjust_brightness("#808080", 0.5);
        let rgb = parse_hex_color(&result).unwrap();
        // 128 * 0.5 = 64
        assert!(rgb.0 <= 70 && rgb.0 >= 60);
    }
}