skrills-intelligence 0.8.2

Intelligent skill recommendations based on usage patterns and project context.
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
//! Parse Claude Code session files from ~/.claude/

use super::{CommandEntry, SkillUsageEvent};
use anyhow::Result;
use serde_json::Value;
use std::fs;
use std::path::Path;
use tracing::debug;

/// Parse Claude Code session files from ~/.claude/projects/
pub fn parse_claude_sessions(projects_dir: &Path) -> Result<Vec<SkillUsageEvent>> {
    let mut events = Vec::new();

    if !projects_dir.exists() {
        debug!(
            "Claude projects directory does not exist: {:?}",
            projects_dir
        );
        return Ok(events);
    }

    for project_entry in fs::read_dir(projects_dir)? {
        let project = project_entry?;
        if !project.file_type()?.is_dir() {
            continue;
        }

        for session_entry in fs::read_dir(project.path())? {
            let session = session_entry?;
            let path = session.path();
            if path.extension().map(|e| e == "jsonl").unwrap_or(false) {
                match parse_claude_session_file(&path) {
                    Ok(session_events) => events.extend(session_events),
                    Err(e) => debug!("Failed to parse session file {:?}: {}", path, e),
                }
            }
        }
    }

    Ok(events)
}

/// Parse Claude Code command history from ~/.claude/history.jsonl
pub fn parse_claude_command_history(history_path: &Path) -> Result<Vec<CommandEntry>> {
    let mut entries = Vec::new();

    if !history_path.exists() {
        debug!("Claude history file does not exist: {:?}", history_path);
        return Ok(entries);
    }

    let content = fs::read_to_string(history_path)?;

    for line in content.lines() {
        if line.trim().is_empty() {
            continue;
        }
        if let Ok(entry) = serde_json::from_str::<Value>(line) {
            let text = entry
                .get("display")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string();

            let timestamp = entry.get("timestamp").and_then(|v| v.as_u64()).unwrap_or(0) / 1000; // Convert from ms to seconds

            let session_id = entry
                .get("sessionId")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown")
                .to_string();

            let project = entry
                .get("project")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            if !text.is_empty() {
                entries.push(CommandEntry {
                    text,
                    timestamp,
                    session_id,
                    project,
                });
            }
        }
    }

    Ok(entries)
}

fn parse_claude_session_file(path: &Path) -> Result<Vec<SkillUsageEvent>> {
    let mut events = Vec::new();
    let content = fs::read_to_string(path)?;
    let session_id = path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown")
        .to_string();

    let mut last_user_prompt: Option<String> = None;

    for line in content.lines() {
        if line.trim().is_empty() {
            continue;
        }
        if let Ok(entry) = serde_json::from_str::<Value>(line) {
            // Track user prompts for context
            if let Some(message) = entry.get("message") {
                if message.get("role").and_then(|r| r.as_str()) == Some("user") {
                    if let Some(contents) = message.get("content").and_then(|c| c.as_array()) {
                        for content_block in contents {
                            if content_block.get("type").and_then(|t| t.as_str()) == Some("text") {
                                if let Some(text) =
                                    content_block.get("text").and_then(|t| t.as_str())
                                {
                                    last_user_prompt =
                                        Some(text.chars().take(200).collect::<String>());
                                }
                            }
                        }
                    }
                }

                // Look for tool_use content blocks
                if let Some(contents) = message.get("content").and_then(|c| c.as_array()) {
                    for content_block in contents {
                        if content_block.get("type").and_then(|t| t.as_str()) == Some("tool_use") {
                            if let Some(name) = content_block.get("name").and_then(|n| n.as_str()) {
                                // Track skill-loading related tools
                                if name.contains("skill") || name == "Skill" {
                                    if let Some(input) = content_block.get("input") {
                                        if let Some(skill_path) = extract_skill_path(input) {
                                            let timestamp = parse_timestamp(
                                                entry.get("timestamp").and_then(|t| t.as_str()),
                                            );

                                            events.push(SkillUsageEvent {
                                                timestamp,
                                                skill_path,
                                                session_id: session_id.clone(),
                                                prompt_context: last_user_prompt.clone(),
                                            });
                                        }
                                    }
                                }
                                // Also track Read tool for SKILL.md files
                                if name == "Read" {
                                    if let Some(input) = content_block.get("input") {
                                        if let Some(file_path) =
                                            input.get("file_path").and_then(|p| p.as_str())
                                        {
                                            if file_path.contains("SKILL.md")
                                                || file_path.contains("/skills/")
                                            {
                                                let timestamp = parse_timestamp(
                                                    entry.get("timestamp").and_then(|t| t.as_str()),
                                                );

                                                events.push(SkillUsageEvent {
                                                    timestamp,
                                                    skill_path: file_path.to_string(),
                                                    session_id: session_id.clone(),
                                                    prompt_context: last_user_prompt.clone(),
                                                });
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(events)
}

fn extract_skill_path(input: &Value) -> Option<String> {
    // Check for skill parameter in Skill tool
    if let Some(skill) = input.get("skill").and_then(|s| s.as_str()) {
        return Some(skill.to_string());
    }
    // Check for uri in skill tools
    if let Some(uri) = input.get("uri").and_then(|u| u.as_str()) {
        if uri.starts_with("skill://") {
            return Some(uri.to_string());
        }
    }
    // Check for file_path
    if let Some(path) = input.get("file_path").and_then(|p| p.as_str()) {
        if path.contains("SKILL.md") {
            return Some(path.to_string());
        }
    }
    None
}

fn parse_timestamp(s: Option<&str>) -> u64 {
    s.and_then(|ts| {
        chrono::DateTime::parse_from_rfc3339(ts)
            .ok()
            .map(|dt| dt.timestamp() as u64)
    })
    .unwrap_or(0)
}

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

    // =========================================================================
    // Directory and Path Tests
    // =========================================================================

    #[test]
    fn test_parse_empty_directory() {
        let tmp = tempdir().unwrap();
        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert!(events.is_empty());
    }

    #[test]
    fn test_parse_nonexistent_directory() {
        let events = parse_claude_sessions(Path::new("/nonexistent/path")).unwrap();
        assert!(events.is_empty());
    }

    #[test]
    fn test_parse_nonexistent_history_file() {
        let entries =
            parse_claude_command_history(Path::new("/nonexistent/history.jsonl")).unwrap();
        assert!(entries.is_empty());
    }

    // =========================================================================
    // Command History Parsing Tests
    // =========================================================================

    #[test]
    fn test_parse_command_history() {
        let tmp = tempdir().unwrap();
        let history_path = tmp.path().join("history.jsonl");

        let content = r#"{"display":"test command","timestamp":1703001000000,"sessionId":"abc123","project":"/home/user/project"}
{"display":"another command","timestamp":1703002000000,"sessionId":"abc123"}"#;

        fs::write(&history_path, content).unwrap();

        let entries = parse_claude_command_history(&history_path).unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].text, "test command");
        assert_eq!(entries[0].timestamp, 1703001000);
        assert_eq!(entries[0].project, Some("/home/user/project".to_string()));
        assert_eq!(entries[1].text, "another command");
        assert!(entries[1].project.is_none());
    }

    #[test]
    fn test_parse_command_history_empty_file() {
        let tmp = tempdir().unwrap();
        let history_path = tmp.path().join("history.jsonl");
        fs::write(&history_path, "").unwrap();

        let entries = parse_claude_command_history(&history_path).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn test_parse_command_history_blank_lines() {
        let tmp = tempdir().unwrap();
        let history_path = tmp.path().join("history.jsonl");

        let content = r#"{"display":"command1","timestamp":1703001000000,"sessionId":"s1"}

{"display":"command2","timestamp":1703002000000,"sessionId":"s1"}

{"display":"command3","timestamp":1703003000000,"sessionId":"s1"}"#;

        fs::write(&history_path, content).unwrap();

        let entries = parse_claude_command_history(&history_path).unwrap();
        assert_eq!(entries.len(), 3);
    }

    #[test]
    fn test_parse_command_history_malformed_json() {
        let tmp = tempdir().unwrap();
        let history_path = tmp.path().join("history.jsonl");

        let content = r#"{"display":"valid","timestamp":1703001000000,"sessionId":"s1"}
{invalid json here}
{"display":"also valid","timestamp":1703002000000,"sessionId":"s2"}"#;

        fs::write(&history_path, content).unwrap();

        let entries = parse_claude_command_history(&history_path).unwrap();
        // Should skip malformed line and continue
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].text, "valid");
        assert_eq!(entries[1].text, "also valid");
    }

    #[test]
    fn test_parse_command_history_missing_fields() {
        let tmp = tempdir().unwrap();
        let history_path = tmp.path().join("history.jsonl");

        let content = r#"{"display":"has all","timestamp":1703001000000,"sessionId":"s1","project":"/proj"}
{"timestamp":1703002000000,"sessionId":"s2"}
{"display":"no timestamp","sessionId":"s3"}
{"display":"no session","timestamp":1703003000000}"#;

        fs::write(&history_path, content).unwrap();

        let entries = parse_claude_command_history(&history_path).unwrap();
        // Entry without display text is skipped (empty text filtered)
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].text, "has all");
        assert_eq!(entries[0].session_id, "s1");
        assert_eq!(entries[1].text, "no timestamp");
        assert_eq!(entries[1].timestamp, 0);
        assert_eq!(entries[2].text, "no session");
        assert_eq!(entries[2].session_id, "unknown");
    }

    #[test]
    fn test_parse_command_history_empty_display() {
        let tmp = tempdir().unwrap();
        let history_path = tmp.path().join("history.jsonl");

        let content = r#"{"display":"","timestamp":1703001000000,"sessionId":"s1"}
{"display":"non-empty","timestamp":1703002000000,"sessionId":"s2"}"#;

        fs::write(&history_path, content).unwrap();

        let entries = parse_claude_command_history(&history_path).unwrap();
        // Empty display text is filtered out
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].text, "non-empty");
    }

    #[test]
    fn test_parse_command_history_timestamp_conversion() {
        let tmp = tempdir().unwrap();
        let history_path = tmp.path().join("history.jsonl");

        // Timestamp is in milliseconds, should be converted to seconds
        let content = r#"{"display":"test","timestamp":1703001000123,"sessionId":"s1"}"#;

        fs::write(&history_path, content).unwrap();

        let entries = parse_claude_command_history(&history_path).unwrap();
        assert_eq!(entries[0].timestamp, 1703001000); // ms / 1000 = seconds
    }

    // =========================================================================
    // Session File Parsing Tests
    // =========================================================================

    #[test]
    fn test_parse_session_file_skill_tool() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("my-project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("session123.jsonl");

        let content = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"skill":"my-skill"}}]}}"#;

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].skill_path, "my-skill");
        assert_eq!(events[0].session_id, "session123");
    }

    #[test]
    fn test_parse_session_file_read_skill_md() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("sess.jsonl");

        let content = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Read","input":{"file_path":"/home/user/skills/test/SKILL.md"}}]}}"#;

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].skill_path, "/home/user/skills/test/SKILL.md");
    }

    #[test]
    fn test_parse_session_file_read_skills_directory() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("sess.jsonl");

        let content = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Read","input":{"file_path":"/path/to/skills/something.md"}}]}}"#;

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
        assert!(events[0].skill_path.contains("/skills/"));
    }

    #[test]
    fn test_parse_session_file_with_prompt_context() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("sess.jsonl");

        let content = r#"{"timestamp":"2024-01-01T11:59:00Z","message":{"role":"user","content":[{"type":"text","text":"Please run the test skill"}]}}
{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"skill":"test-skill"}}]}}"#;

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(
            events[0].prompt_context,
            Some("Please run the test skill".to_string())
        );
    }

    #[test]
    fn test_parse_session_file_skill_uri() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("sess.jsonl");

        let content = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"uri":"skill://my-plugin/my-skill"}}]}}"#;

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].skill_path, "skill://my-plugin/my-skill");
    }

    #[test]
    fn test_parse_session_file_empty() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("empty.jsonl");
        fs::write(&session_path, "").unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert!(events.is_empty());
    }

    #[test]
    fn test_parse_session_file_malformed_entries() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("sess.jsonl");

        let content = r#"not valid json
{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"skill":"valid-skill"}}]}}
{"incomplete": true"#;

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        // Should recover and parse valid entry
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].skill_path, "valid-skill");
    }

    #[test]
    fn test_parse_session_file_no_skill_events() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("sess.jsonl");

        let content = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Read","input":{"file_path":"/some/regular/file.txt"}}]}}
{"timestamp":"2024-01-01T12:01:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Just some text"}]}}"#;

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert!(events.is_empty());
    }

    #[test]
    fn test_parse_session_file_multiple_projects() {
        let tmp = tempdir().unwrap();

        // Create two project directories
        let project1 = tmp.path().join("project1");
        let project2 = tmp.path().join("project2");
        fs::create_dir_all(&project1).unwrap();
        fs::create_dir_all(&project2).unwrap();

        // Add session files to each
        let content1 = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"skill":"skill-from-project1"}}]}}"#;
        let content2 = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"skill":"skill-from-project2"}}]}}"#;

        fs::write(project1.join("sess1.jsonl"), content1).unwrap();
        fs::write(project2.join("sess2.jsonl"), content2).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 2);

        let skill_paths: Vec<&str> = events.iter().map(|e| e.skill_path.as_str()).collect();
        assert!(skill_paths.contains(&"skill-from-project1"));
        assert!(skill_paths.contains(&"skill-from-project2"));
    }

    #[test]
    fn test_parse_session_ignores_non_jsonl_files() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();

        // Create various file types
        let jsonl_content = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"skill":"real-skill"}}]}}"#;
        fs::write(project_dir.join("session.jsonl"), jsonl_content).unwrap();
        fs::write(project_dir.join("notes.txt"), "just notes").unwrap();
        fs::write(project_dir.join("data.json"), "{}").unwrap();
        fs::write(project_dir.join("README.md"), "# Readme").unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].skill_path, "real-skill");
    }

    #[test]
    fn test_parse_session_ignores_non_directory_entries() {
        let tmp = tempdir().unwrap();

        // Create a file at the top level (not a project directory)
        fs::write(tmp.path().join("random-file.jsonl"), "{}").unwrap();

        // Create actual project directory
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let content = r#"{"timestamp":"2024-01-01T12:00:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Skill","input":{"skill":"skill"}}]}}"#;
        fs::write(project_dir.join("sess.jsonl"), content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
    }

    // =========================================================================
    // Timestamp Parsing Tests
    // =========================================================================

    #[test]
    fn test_parse_timestamp_valid_rfc3339() {
        let ts = parse_timestamp(Some("2024-01-15T10:30:00Z"));
        assert!(ts > 0);
        // 2024-01-15 10:30:00 UTC = 1705314600
        assert_eq!(ts, 1705314600);
    }

    #[test]
    fn test_parse_timestamp_with_timezone() {
        let ts = parse_timestamp(Some("2024-01-15T10:30:00+05:00"));
        assert!(ts > 0);
    }

    #[test]
    fn test_parse_timestamp_invalid() {
        assert_eq!(parse_timestamp(Some("not a date")), 0);
        assert_eq!(parse_timestamp(Some("")), 0);
        assert_eq!(parse_timestamp(None), 0);
    }

    // =========================================================================
    // Extract Skill Path Tests
    // =========================================================================

    #[test]
    fn test_extract_skill_path_from_skill_param() {
        let input = serde_json::json!({"skill": "my-skill-name"});
        assert_eq!(
            extract_skill_path(&input),
            Some("my-skill-name".to_string())
        );
    }

    #[test]
    fn test_extract_skill_path_from_uri() {
        let input = serde_json::json!({"uri": "skill://plugin/skill"});
        assert_eq!(
            extract_skill_path(&input),
            Some("skill://plugin/skill".to_string())
        );
    }

    #[test]
    fn test_extract_skill_path_from_uri_non_skill() {
        let input = serde_json::json!({"uri": "file:///some/path"});
        assert_eq!(extract_skill_path(&input), None);
    }

    #[test]
    fn test_extract_skill_path_from_file_path() {
        let input = serde_json::json!({"file_path": "/path/to/SKILL.md"});
        assert_eq!(
            extract_skill_path(&input),
            Some("/path/to/SKILL.md".to_string())
        );
    }

    #[test]
    fn test_extract_skill_path_from_file_path_non_skill() {
        let input = serde_json::json!({"file_path": "/path/to/README.md"});
        assert_eq!(extract_skill_path(&input), None);
    }

    #[test]
    fn test_extract_skill_path_empty_input() {
        let input = serde_json::json!({});
        assert_eq!(extract_skill_path(&input), None);
    }

    // =========================================================================
    // Prompt Context Truncation Tests
    // =========================================================================

    #[test]
    fn test_prompt_context_truncation() {
        let tmp = tempdir().unwrap();
        let project_dir = tmp.path().join("project");
        fs::create_dir_all(&project_dir).unwrap();
        let session_path = project_dir.join("sess.jsonl");

        // Create a very long prompt
        let long_prompt = "a".repeat(500);
        let content = format!(
            r#"{{"timestamp":"2024-01-01T11:59:00Z","message":{{"role":"user","content":[{{"type":"text","text":"{}"}}]}}}}
{{"timestamp":"2024-01-01T12:00:00Z","message":{{"role":"assistant","content":[{{"type":"tool_use","name":"Skill","input":{{"skill":"test"}}}}]}}}}"#,
            long_prompt
        );

        fs::write(&session_path, content).unwrap();

        let events = parse_claude_sessions(tmp.path()).unwrap();
        assert_eq!(events.len(), 1);
        // Prompt should be truncated to 200 chars
        assert_eq!(events[0].prompt_context.as_ref().unwrap().len(), 200);
    }
}