perl-lsp 0.1.0

A Perl LSP server built on tree-sitter-perl and tower-lsp
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//! SQLite persistence for the module cache.
//!
//! Stores resolved module exports to disk so they survive LSP restarts.
//! Validates entries against mtime + file size to detect stale data.
//! Invalidates the entire cache when `@INC` changes.

use std::collections::HashMap;
use std::path::PathBuf;
use std::time::SystemTime;

use dashmap::DashMap;
use rusqlite::{params, Connection};

use crate::file_analysis::{inferred_type_from_tag, inferred_type_to_tag};
use crate::module_index::{ExportedOverload, ExportedParam, ExportedSub, ModuleExports};

const SCHEMA_VERSION: &str = "8";

/// Bumped when extraction logic changes (new fields, better parsing).
/// Unlike SCHEMA_VERSION, this doesn't drop the table — stale entries
/// are re-resolved lazily with priority.
pub const EXTRACT_VERSION: i64 = 4;

pub fn cache_base_dir() -> Option<PathBuf> {
    if let Ok(xdg) = std::env::var("XDG_CACHE_HOME") {
        if !xdg.is_empty() {
            return Some(PathBuf::from(xdg).join("perl-lsp"));
        }
    }
    if let Ok(home) = std::env::var("HOME") {
        return Some(PathBuf::from(home).join(".cache").join("perl-lsp"));
    }
    None
}

pub fn cache_dir_for_workspace(workspace_root: Option<&str>) -> Option<PathBuf> {
    let base = cache_base_dir()?;
    match workspace_root {
        Some(root) => {
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};
            let mut hasher = DefaultHasher::new();
            root.hash(&mut hasher);
            Some(base.join(format!("{:016x}", hasher.finish())))
        }
        None => Some(base),
    }
}

#[cfg(not(test))]
pub fn open_cache_db(workspace_root: Option<&str>) -> Option<Connection> {
    let dir = cache_dir_for_workspace(workspace_root)?;
    std::fs::create_dir_all(&dir).ok()?;
    let db_path = dir.join("modules.db");
    log::info!("Module cache: {:?}", db_path);

    match Connection::open(&db_path) {
        Ok(conn) => {
            let _ = conn.execute_batch("PRAGMA journal_mode=WAL;");
            match init_schema(&conn) {
                Ok(()) => Some(conn),
                Err(e) => {
                    log::warn!("Cache DB schema init failed: {}. Recreating.", e);
                    drop(conn);
                    let _ = std::fs::remove_file(&db_path);
                    let conn = Connection::open(&db_path).ok()?;
                    let _ = conn.execute_batch("PRAGMA journal_mode=WAL;");
                    init_schema(&conn).ok()?;
                    Some(conn)
                }
            }
        }
        Err(e) => {
            log::warn!("Failed to open cache DB: {}", e);
            None
        }
    }
}

#[cfg(test)]
pub fn open_cache_db(_workspace_root: Option<&str>) -> Option<Connection> {
    None
}

pub fn init_schema(conn: &Connection) -> rusqlite::Result<()> {
    conn.execute_batch(
        "CREATE TABLE IF NOT EXISTS meta (
            key   TEXT PRIMARY KEY,
            value TEXT NOT NULL
        );
        CREATE TABLE IF NOT EXISTS modules (
            module_name  TEXT PRIMARY KEY,
            path         TEXT NOT NULL,
            mtime_secs   INTEGER NOT NULL,
            file_size    INTEGER NOT NULL,
            export       TEXT NOT NULL,
            export_ok    TEXT NOT NULL,
            source       TEXT NOT NULL DEFAULT 'import',
            subs         TEXT NOT NULL DEFAULT '{}',
            parents      TEXT NOT NULL DEFAULT '[]',
            extract_version INTEGER NOT NULL DEFAULT 0
        );",
    )?;

    let version: Option<String> = conn
        .query_row(
            "SELECT value FROM meta WHERE key = 'schema_version'",
            [],
            |row| row.get(0),
        )
        .ok();

    match version.as_deref() {
        Some(SCHEMA_VERSION) => Ok(()),
        Some(_) => {
            conn.execute_batch("DROP TABLE IF EXISTS modules;")?;
            conn.execute_batch(
                "CREATE TABLE modules (
                    module_name  TEXT PRIMARY KEY,
                    path         TEXT NOT NULL,
                    mtime_secs   INTEGER NOT NULL,
                    file_size    INTEGER NOT NULL,
                    export       TEXT NOT NULL,
                    export_ok    TEXT NOT NULL,
                    source       TEXT NOT NULL DEFAULT 'import',
                    subs         TEXT NOT NULL DEFAULT '{}',
                    parents      TEXT NOT NULL DEFAULT '[]',
                    extract_version INTEGER NOT NULL DEFAULT 0
                );",
            )?;
            conn.execute(
                "INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?1)",
                params![SCHEMA_VERSION],
            )?;
            Ok(())
        }
        None => {
            conn.execute(
                "INSERT INTO meta (key, value) VALUES ('schema_version', ?1)",
                params![SCHEMA_VERSION],
            )?;
            Ok(())
        }
    }
}

pub fn compute_inc_hash(inc_paths: &[PathBuf]) -> String {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    let mut hasher = DefaultHasher::new();
    for p in inc_paths {
        p.hash(&mut hasher);
    }
    format!("{:016x}", hasher.finish())
}

pub fn validate_inc_paths(conn: &Connection, inc_paths: &[PathBuf]) -> rusqlite::Result<()> {
    let current_hash = compute_inc_hash(inc_paths);
    let stored: Option<String> = conn
        .query_row(
            "SELECT value FROM meta WHERE key = 'inc_hash'",
            [],
            |row| row.get(0),
        )
        .ok();

    if stored.as_deref() != Some(&current_hash) {
        log::info!(
            "@INC changed (was {:?}, now {}), clearing module cache",
            stored,
            current_hash
        );
        conn.execute("DELETE FROM modules", [])?;
        conn.execute(
            "INSERT OR REPLACE INTO meta (key, value) VALUES ('inc_hash', ?1)",
            params![current_hash],
        )?;
    }
    Ok(())
}

fn mtime_as_secs(path: &std::path::Path) -> Option<(i64, i64)> {
    let meta = std::fs::metadata(path).ok()?;
    let mtime = meta.modified().ok()?;
    let secs = mtime.duration_since(SystemTime::UNIX_EPOCH).ok()?.as_secs() as i64;
    let size = meta.len() as i64;
    Some((secs, size))
}

pub fn warm_cache(
    conn: &Connection,
    cache: &DashMap<String, Option<ModuleExports>>,
) -> (usize, Vec<String>) {
    let mut stmt = match conn.prepare(
        "SELECT module_name, path, mtime_secs, file_size, export, export_ok, subs, extract_version, parents FROM modules",
    ) {
        Ok(s) => s,
        Err(_) => return (0, Vec::new()),
    };

    let rows = match stmt.query_map([], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, i64>(2)?,
            row.get::<_, i64>(3)?,
            row.get::<_, String>(4)?,
            row.get::<_, String>(5)?,
            row.get::<_, String>(6)?,
            row.get::<_, i64>(7)?,
            row.get::<_, String>(8)?,
        ))
    }) {
        Ok(r) => r,
        Err(_) => return (0, Vec::new()),
    };

    let mut count = 0usize;
    let mut stale_names = Vec::new();
    for row in rows.flatten() {
        let (module_name, path_str, cached_mtime, cached_size, export_json, export_ok_json, subs_json, row_extract_version, parents_json) = row;

        // Negative sentinel
        if path_str.is_empty() {
            cache.insert(module_name, None);
            count += 1;
            continue;
        }

        let path = PathBuf::from(&path_str);

        // Validate mtime — skip entries where the file changed on disk
        if let Some((disk_mtime, disk_size)) = mtime_as_secs(&path) {
            if disk_mtime != cached_mtime || disk_size != cached_size {
                continue;
            }
        } else {
            continue; // file deleted
        }

        let export: Vec<String> = serde_json::from_str(&export_json).unwrap_or_default();
        let export_ok: Vec<String> = serde_json::from_str(&export_ok_json).unwrap_or_default();

        // Deserialize subs: JSON map of name → { def_line, params, is_method, return_type, hash_keys }
        let subs = deserialize_subs_json(&subs_json);
        let parents: Vec<String> = serde_json::from_str(&parents_json).unwrap_or_default();

        if export.is_empty() && export_ok.is_empty() {
            cache.insert(module_name, None);
        } else {
            // Check if this entry was produced by an older extraction version
            if row_extract_version < EXTRACT_VERSION {
                stale_names.push(module_name.clone());
            }
            cache.insert(
                module_name,
                Some(ModuleExports {
                    path,
                    export,
                    export_ok,
                    subs,
                    parents,
                }),
            );
        }
        count += 1;
    }

    (count, stale_names)
}

fn deserialize_subs_json(json_str: &str) -> HashMap<String, ExportedSub> {
    let obj: HashMap<String, serde_json::Value> = match serde_json::from_str(json_str) {
        Ok(v) => v,
        Err(_) => return HashMap::new(),
    };
    obj.into_iter()
        .filter_map(|(name, val)| {
            let def_line = val.get("def_line").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
            let is_method = val.get("is_method").and_then(|v| v.as_bool()).unwrap_or(false);
            let params = val
                .get("params")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|p| {
                            Some(ExportedParam {
                                name: p.get("name")?.as_str()?.to_string(),
                                is_slurpy: p
                                    .get("is_slurpy")
                                    .and_then(|v| v.as_bool())
                                    .unwrap_or(false),
                                inferred_type: p.get("type").and_then(|v| v.as_str()).map(|s| s.to_string()),
                            })
                        })
                        .collect()
                })
                .unwrap_or_default();
            let return_type = val
                .get("return_type")
                .and_then(|v| v.as_str())
                .and_then(inferred_type_from_tag);
            let hash_keys = val
                .get("hash_keys")
                .and_then(|v| v.as_array())
                .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
                .unwrap_or_default();
            let doc = val.get("doc")
                .and_then(|v| v.as_str())
                .map(String::from);
            let overloads = val.get("overloads")
                .and_then(|v| v.as_array())
                .map(|arr| arr.iter().filter_map(|o| {
                    let ol_params = o.get("params")
                        .and_then(|p| p.as_array())
                        .map(|pa| pa.iter().filter_map(|pv| {
                            Some(ExportedParam {
                                name: pv.get("name")?.as_str()?.to_string(),
                                is_slurpy: pv.get("is_slurpy").and_then(|v| v.as_bool()).unwrap_or(false),
                                inferred_type: pv.get("type").and_then(|v| v.as_str()).map(String::from),
                            })
                        }).collect())
                        .unwrap_or_default();
                    let ol_rt = o.get("return_type")
                        .and_then(|v| v.as_str())
                        .and_then(inferred_type_from_tag);
                    Some(ExportedOverload { params: ol_params, return_type: ol_rt })
                }).collect())
                .unwrap_or_default();
            Some((
                name,
                ExportedSub {
                    def_line,
                    params,
                    is_method,
                    return_type,
                    hash_keys,
                    doc,
                    overloads,
                },
            ))
        })
        .collect()
}

pub fn save_to_db(
    conn: &Connection,
    module_name: &str,
    result: &Option<ModuleExports>,
    source: &str,
) {
    let (path_str, mtime, size, export_json, export_ok_json, subs_json, parents_json) = match result {
        Some(exports) => {
            let (mtime, size) = mtime_as_secs(&exports.path).unwrap_or((0, 0));
            let ej = serde_json::to_string(&exports.export).unwrap_or_default();
            let eoj = serde_json::to_string(&exports.export_ok).unwrap_or_default();
            let sj = serialize_subs_json(&exports.subs);
            let pj = serde_json::to_string(&exports.parents).unwrap_or_else(|_| "[]".to_string());
            (
                exports.path.to_string_lossy().to_string(),
                mtime,
                size,
                ej,
                eoj,
                sj,
                pj,
            )
        }
        None => (
            String::new(),
            0i64,
            0i64,
            "[]".to_string(),
            "[]".to_string(),
            "{}".to_string(),
            "[]".to_string(),
        ),
    };

    let r = conn.execute(
        "INSERT OR REPLACE INTO modules (module_name, path, mtime_secs, file_size, export, export_ok, source, subs, parents, extract_version)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
        params![module_name, path_str, mtime, size, export_json, export_ok_json, source, subs_json, parents_json, EXTRACT_VERSION],
    );
    if let Err(e) = r {
        log::warn!("Failed to save module cache for '{}': {}", module_name, e);
    }
}

fn serialize_subs_json(subs: &HashMap<String, ExportedSub>) -> String {
    let mut map = serde_json::Map::new();
    for (name, sub_info) in subs {
        let mut obj = serde_json::Map::new();
        obj.insert("def_line".into(), sub_info.def_line.into());
        obj.insert("is_method".into(), sub_info.is_method.into());
        let params: Vec<serde_json::Value> = sub_info
            .params
            .iter()
            .map(|p| {
                serde_json::json!({
                    "name": p.name,
                    "is_slurpy": p.is_slurpy,
                })
            })
            .collect();
        obj.insert("params".into(), params.into());
        if let Some(ref rt) = sub_info.return_type {
            obj.insert(
                "return_type".into(),
                serde_json::Value::String(inferred_type_to_tag(rt)),
            );
        }
        if !sub_info.hash_keys.is_empty() {
            obj.insert("hash_keys".into(), serde_json::json!(sub_info.hash_keys));
        }
        if let Some(ref doc) = sub_info.doc {
            obj.insert("doc".into(), serde_json::Value::String(doc.clone()));
        }
        if !sub_info.overloads.is_empty() {
            let overloads: Vec<serde_json::Value> = sub_info.overloads.iter().map(|ol| {
                let ol_params: Vec<serde_json::Value> = ol.params.iter().map(|p| {
                    let mut pobj = serde_json::json!({ "name": p.name, "is_slurpy": p.is_slurpy });
                    if let Some(ref t) = p.inferred_type {
                        pobj["type"] = serde_json::Value::String(t.clone());
                    }
                    pobj
                }).collect();
                let mut ol_obj = serde_json::json!({ "params": ol_params });
                if let Some(ref rt) = ol.return_type {
                    ol_obj["return_type"] = serde_json::Value::String(inferred_type_to_tag(rt));
                }
                ol_obj
            }).collect();
            obj.insert("overloads".into(), overloads.into());
        }
        map.insert(name.clone(), obj.into());
    }
    serde_json::Value::Object(map).to_string()
}

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

    fn test_db() -> Connection {
        let conn = Connection::open_in_memory().unwrap();
        init_schema(&conn).unwrap();
        conn
    }

    #[test]
    fn test_db_save_and_load_roundtrip() {
        let conn = test_db();

        let dir = std::env::temp_dir();
        let pm = dir.join("TestModule.pm");
        std::fs::write(&pm, "package TestModule; 1;").unwrap();

        let exports = Some(ModuleExports {
            path: pm.clone(),
            export: vec!["foo".into(), "bar".into()],
            export_ok: vec!["baz".into()],
            subs: HashMap::new(),
            parents: vec![],
        });
        save_to_db(&conn, "TestModule", &exports, "import");

        let cache = DashMap::new();
        let (n, stale) = warm_cache(&conn, &cache);
        assert_eq!(n, 1);
        assert!(stale.is_empty());

        let loaded = cache.get("TestModule").unwrap();
        let loaded = loaded.as_ref().unwrap();
        assert_eq!(loaded.export, vec!["foo", "bar"]);
        assert_eq!(loaded.export_ok, vec!["baz"]);
        assert_eq!(loaded.path, pm);

        let _ = std::fs::remove_file(&pm);
    }

    #[test]
    fn test_db_negative_result_roundtrip() {
        let conn = test_db();
        save_to_db(&conn, "Nonexistent::Module", &None, "import");

        let cache = DashMap::new();
        let (n, _) = warm_cache(&conn, &cache);
        assert_eq!(n, 1);

        let entry = cache.get("Nonexistent::Module").unwrap();
        assert!(entry.is_none());
    }

    #[test]
    fn test_db_stale_entry_skipped() {
        let conn = test_db();

        let dir = std::env::temp_dir();
        let pm = dir.join("StaleModule.pm");
        std::fs::write(&pm, "v1").unwrap();

        let exports = Some(ModuleExports {
            path: pm.clone(),
            export: vec!["old".into()],
            export_ok: vec![],
            subs: HashMap::new(),
            parents: vec![],
        });
        save_to_db(&conn, "StaleModule", &exports, "import");

        std::thread::sleep(std::time::Duration::from_secs(1));
        std::fs::write(&pm, "v2 with more content").unwrap();

        let cache = DashMap::new();
        let (n, _) = warm_cache(&conn, &cache);
        assert_eq!(n, 0, "stale entry should not be loaded");
        assert!(!cache.contains_key("StaleModule"));

        let _ = std::fs::remove_file(&pm);
    }

    #[test]
    fn test_db_inc_hash_invalidation() {
        let conn = test_db();
        let paths1 = vec![PathBuf::from("/usr/lib/perl5")];
        let paths2 = vec![
            PathBuf::from("/usr/lib/perl5"),
            PathBuf::from("/home/user/lib"),
        ];

        validate_inc_paths(&conn, &paths1).unwrap();
        save_to_db(&conn, "Foo", &None, "import");

        validate_inc_paths(&conn, &paths2).unwrap();
        let cache = DashMap::new();
        let (n, _) = warm_cache(&conn, &cache);
        assert_eq!(n, 0, "cache should be empty after @INC change");
    }

    #[test]
    fn test_db_schema_version_migration() {
        let conn = test_db();

        conn.execute(
            "UPDATE meta SET value = '0' WHERE key = 'schema_version'",
            [],
        )
        .unwrap();
        save_to_db(&conn, "OldModule", &None, "import");

        init_schema(&conn).unwrap();
        let cache = DashMap::new();
        let (n, _) = warm_cache(&conn, &cache);
        assert_eq!(n, 0, "old data should be gone after migration");
    }

    #[test]
    fn test_db_migration_old_to_v5_supports_subs() {
        // Simulate an old database (different schema version)
        let conn = Connection::open_in_memory().unwrap();
        conn.execute_batch(
            "CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
             INSERT INTO meta (key, value) VALUES ('schema_version', '4');
             CREATE TABLE modules (
                 module_name TEXT PRIMARY KEY,
                 path        TEXT NOT NULL,
                 mtime_secs  INTEGER NOT NULL,
                 file_size   INTEGER NOT NULL,
                 export      TEXT NOT NULL,
                 export_ok   TEXT NOT NULL,
                 source      TEXT NOT NULL DEFAULT 'import',
                 return_types TEXT NOT NULL DEFAULT '{}',
                 hash_keys    TEXT NOT NULL DEFAULT '{}'
             );",
        )
        .unwrap();

        // Run migration
        init_schema(&conn).unwrap();

        // Now save with subs — should not fail
        let dir = std::env::temp_dir();
        let pm = dir.join("MigratedModule.pm");
        std::fs::write(&pm, "package MigratedModule; 1;").unwrap();

        let mut subs = HashMap::new();
        subs.insert(
            "get_data".to_string(),
            ExportedSub {
                def_line: 10,
                params: vec![],
                is_method: false,
                return_type: Some(crate::file_analysis::InferredType::HashRef),
                hash_keys: vec!["host".into()],
                doc: None,
                overloads: vec![],
            },
        );

        let exports = Some(ModuleExports {
            path: pm.clone(),
            export: vec!["get_data".into()],
            export_ok: vec![],
            subs,
            parents: vec![],
        });
        save_to_db(&conn, "MigratedModule", &exports, "import");

        // Verify it round-trips
        let cache = DashMap::new();
        let (n, _) = warm_cache(&conn, &cache);
        assert_eq!(n, 1);
        let loaded = cache.get("MigratedModule").unwrap();
        let loaded = loaded.as_ref().unwrap();
        let sub_info = loaded.subs.get("get_data").expect("should have get_data sub");
        assert_eq!(sub_info.def_line, 10);
        assert_eq!(
            sub_info.return_type,
            Some(crate::file_analysis::InferredType::HashRef),
            "return_type should survive migration + roundtrip"
        );
        assert_eq!(sub_info.hash_keys, vec!["host"]);

        let _ = std::fs::remove_file(&pm);
    }

    #[test]
    fn test_db_source_column() {
        let conn = test_db();
        let dir = std::env::temp_dir();
        let pm = dir.join("SourceTest.pm");
        std::fs::write(&pm, "package SourceTest; 1;").unwrap();

        let exports = Some(ModuleExports {
            path: pm.clone(),
            export: vec!["foo".into()],
            export_ok: vec![],
            subs: HashMap::new(),
            parents: vec![],
        });
        save_to_db(&conn, "SourceTest", &exports, "cpanfile");

        let source: String = conn
            .query_row(
                "SELECT source FROM modules WHERE module_name = 'SourceTest'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(source, "cpanfile");

        let _ = std::fs::remove_file(&pm);
    }

    #[test]
    fn test_workspace_cache_dir_uniqueness() {
        let d1 = cache_dir_for_workspace(Some("file:///home/user/project-a"));
        let d2 = cache_dir_for_workspace(Some("file:///home/user/project-b"));
        let d_none = cache_dir_for_workspace(None);
        assert_ne!(d1, d2, "Different roots should produce different paths");
        assert_ne!(d1, d_none, "Root vs no-root should differ");
        assert_eq!(
            d1,
            cache_dir_for_workspace(Some("file:///home/user/project-a")),
            "Same root should produce same path"
        );
    }

    #[test]
    fn test_db_subs_roundtrip() {
        use crate::file_analysis::InferredType;

        let conn = test_db();

        let dir = std::env::temp_dir();
        let pm = dir.join("SubsTest.pm");
        std::fs::write(&pm, "package SubsTest; 1;").unwrap();

        let mut subs = HashMap::new();
        subs.insert(
            "get_config".to_string(),
            ExportedSub {
                def_line: 5,
                params: vec![
                    ExportedParam { name: "$path".into(), is_slurpy: false, inferred_type: None },
                ],
                is_method: false,
                return_type: Some(InferredType::HashRef),
                hash_keys: vec!["host".into(), "port".into()],
                doc: None,
                overloads: vec![],
            },
        );
        subs.insert(
            "make_items".to_string(),
            ExportedSub {
                def_line: 20,
                params: vec![],
                is_method: false,
                return_type: Some(InferredType::ArrayRef),
                hash_keys: vec![],
                doc: None,
                overloads: vec![],
            },
        );
        subs.insert(
            "new_obj".to_string(),
            ExportedSub {
                def_line: 30,
                params: vec![ExportedParam { name: "$class".into(), is_slurpy: false, inferred_type: None }],
                is_method: true,
                return_type: Some(InferredType::ClassName("MyObj".into())),
                hash_keys: vec![],
                doc: None,
                overloads: vec![],
            },
        );

        let exports = Some(ModuleExports {
            path: pm.clone(),
            export: vec!["get_config".into()],
            export_ok: vec!["make_items".into(), "new_obj".into()],
            subs,
            parents: vec![],
        });
        save_to_db(&conn, "SubsTest", &exports, "import");

        let cache = DashMap::new();
        let (n, _) = warm_cache(&conn, &cache);
        assert_eq!(n, 1);

        let loaded = cache.get("SubsTest").unwrap();
        let loaded = loaded.as_ref().unwrap();

        let gc = loaded.subs.get("get_config").expect("get_config");
        assert_eq!(gc.def_line, 5);
        assert_eq!(gc.params.len(), 1);
        assert_eq!(gc.params[0].name, "$path");
        assert_eq!(gc.return_type, Some(InferredType::HashRef));
        assert_eq!(gc.hash_keys, vec!["host", "port"]);

        let mi = loaded.subs.get("make_items").expect("make_items");
        assert_eq!(mi.def_line, 20);
        assert_eq!(mi.return_type, Some(InferredType::ArrayRef));

        let no = loaded.subs.get("new_obj").expect("new_obj");
        assert_eq!(no.def_line, 30);
        assert!(no.is_method);
        assert_eq!(no.return_type, Some(InferredType::ClassName("MyObj".into())));

        let _ = std::fs::remove_file(&pm);
    }

    #[test]
    fn test_exported_sub_overloads_roundtrip() {
        use crate::file_analysis::InferredType;

        let mut subs = HashMap::new();
        subs.insert(
            "name".to_string(),
            ExportedSub {
                def_line: 5,
                params: vec![], // getter: no params
                is_method: true,
                return_type: None,
                hash_keys: vec![],
                doc: None,
                overloads: vec![ExportedOverload {
                    params: vec![ExportedParam {
                        name: "$val".into(),
                        is_slurpy: false,
                        inferred_type: None,
                    }],
                    return_type: Some(InferredType::ClassName("Foo".into())),
                }],
            },
        );

        let json = serialize_subs_json(&subs);
        let roundtripped = deserialize_subs_json(&json);
        let rt = roundtripped.get("name").unwrap();
        assert_eq!(rt.overloads.len(), 1);
        assert_eq!(rt.overloads[0].params.len(), 1);
        assert_eq!(rt.overloads[0].params[0].name, "$val");
        assert_eq!(
            rt.overloads[0].return_type,
            Some(InferredType::ClassName("Foo".into()))
        );
        // Primary should be preserved
        assert!(rt.params.is_empty());
        assert!(rt.return_type.is_none());
    }

    #[test]
    fn test_warm_reports_stale_entries() {
        let conn = test_db();
        let dir = std::env::temp_dir();
        let pm = dir.join("StaleExtract.pm");
        std::fs::write(&pm, "package StaleExtract; 1;").unwrap();

        let (mtime, size) = mtime_as_secs(&pm).unwrap();

        // Save with old extract version (0, below EXTRACT_VERSION)
        conn.execute(
            "INSERT INTO modules (module_name, path, mtime_secs, file_size, export, export_ok, subs, extract_version)
             VALUES ('StaleExtract', ?1, ?2, ?3, '[\"foo\"]', '[]', '{}', 0)",
            params![pm.to_string_lossy(), mtime, size],
        ).unwrap();

        let cache = DashMap::new();
        let (loaded, stale) = warm_cache(&conn, &cache);
        assert_eq!(loaded, 1, "stale entry should still be loaded");
        assert!(cache.contains_key("StaleExtract"), "stale entry should be in cache");
        assert_eq!(stale, vec!["StaleExtract"], "should report as stale");

        let _ = std::fs::remove_file(&pm);
    }

    #[test]
    fn test_warm_fresh_not_stale() {
        let conn = test_db();
        let dir = std::env::temp_dir();
        let pm = dir.join("FreshExtract.pm");
        std::fs::write(&pm, "package FreshExtract; 1;").unwrap();

        let exports = Some(ModuleExports {
            path: pm.clone(),
            export: vec!["foo".into()],
            export_ok: vec![],
            subs: HashMap::new(),
            parents: vec![],
        });
        save_to_db(&conn, "FreshExtract", &exports, "import");

        let cache = DashMap::new();
        let (loaded, stale) = warm_cache(&conn, &cache);
        assert_eq!(loaded, 1);
        assert!(stale.is_empty(), "current-version entry should not be stale");

        let _ = std::fs::remove_file(&pm);
    }

    #[test]
    fn test_save_writes_extract_version() {
        let conn = test_db();
        let dir = std::env::temp_dir();
        let pm = dir.join("VersionedSave.pm");
        std::fs::write(&pm, "package VersionedSave; 1;").unwrap();

        let exports = Some(ModuleExports {
            path: pm.clone(),
            export: vec!["foo".into()],
            export_ok: vec![],
            subs: HashMap::new(),
            parents: vec![],
        });
        save_to_db(&conn, "VersionedSave", &exports, "import");

        let ver: i64 = conn.query_row(
            "SELECT extract_version FROM modules WHERE module_name = 'VersionedSave'",
            [], |row| row.get(0),
        ).unwrap();
        assert_eq!(ver, EXTRACT_VERSION);

        let _ = std::fs::remove_file(&pm);
    }
}