Skip to main content

oxi/storage/
packages.rs

1//! Package system for oxi CLI
2//!
3//! Packages bundle extensions, skills, prompts, and themes for sharing.
4//! Supports local directories, npm packages, git repositories, GitHub
5//! shorthand, and URL-based archives.
6//!
7//! ## Package sources
8//!
9//! - **Local path**: a directory with `oxi-package.toml` or auto-discoverable resources
10//! - **npm**: `npm:<package>[@<version>]` — resolved from the npm registry
11//! - **git**: `https://github.com/org/repo.git[@ref]`, `git://…`, `git+ssh://…`
12//! - **GitHub shorthand**: `github:org/repo[@ref]`
13//! - **URL**: direct `.tar.gz` / `.zip` archive
14//!
15//! ## Package manifest
16//!
17//! A package is a directory containing an `oxi-package.toml` file:
18//!
19//! ```toml
20//! name = "@foo/oxi-tools"
21//! version = "1.0.0"
22//! extensions = ["ext/index.ts"]
23//! skills = ["skills/code-review/SKILL.md"]
24//! prompts = ["prompts/review.md"]
25//! themes = ["themes/dark-pro.json"]
26//! ```
27//!
28//! ## Resource discovery
29//!
30//! When a package lacks explicit resource lists, resources are discovered
31//! automatically by scanning the package directory:
32//! - **Extensions**: `.so`, `.dylib`, `.dll` files, or `index.ts`/`index.js` entries
33//! - **Skills**: Directories containing `SKILL.md`
34//! - **Prompts**: `.md` files in `prompts/` subdirectory
35//! - **Themes**: `.json` files in `themes/` subdirectory
36//!
37//! ## Lockfile
38//!
39//! An `oxi-lock.json` file records exact versions/refs for reproducibility.
40
41use crate::util::http_client::shared_http_client;
42use anyhow::{Context, Result, bail};
43use serde::{Deserialize, Serialize};
44use sha2::{Digest, Sha256};
45use std::collections::{BTreeMap, HashMap, HashSet};
46use std::fs;
47use std::path::{Path, PathBuf};
48use std::sync::LazyLock;
49
50/// Run an async future on a fresh tokio runtime created on a dedicated OS thread.
51///
52/// This avoids the "Cannot start a runtime from within a runtime" panic that
53/// `Runtime::new()?.block_on(future)` causes when called from inside an
54/// existing tokio context (e.g., from an agent tool callback or TUI handler).
55fn run_on_fresh_runtime<F, T>(future: F) -> Result<T>
56where
57    F: std::future::Future<Output = Result<T>> + Send,
58    T: Send,
59{
60    std::thread::scope(|s| {
61        s.spawn(|| {
62            let rt = tokio::runtime::Builder::new_current_thread()
63                .enable_all()
64                .build()
65                .context("failed to build temp runtime")?;
66            rt.block_on(future)
67        })
68        .join()
69        .map_err(|_| anyhow::anyhow!("runtime thread panicked"))?
70    })
71}
72
73/// Cached regex for parsing npm package specs
74static NPM_SPEC_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
75    regex::Regex::new(r"^(@?[^@]+(?:/[^@]+)?)(?:@(.+))?$").expect("valid static regex")
76});
77
78// ── Constants ─────────────────────────────────────────────────────────
79
80const LOCKFILE_NAME: &str = "oxi-lock.json";
81const MANIFEST_NAME: &str = "oxi-package.toml";
82const NPM_MANIFEST_NAME: &str = "package.json";
83
84// ── Types ─────────────────────────────────────────────────────────────
85
86/// Types of resources a package can contribute
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
88#[serde(rename_all = "snake_case")]
89pub enum ResourceKind {
90    /// extension variant.
91    Extension,
92    /// skill variant.
93    Skill,
94    /// prompt variant.
95    Prompt,
96    /// theme variant.
97    Theme,
98}
99
100impl std::fmt::Display for ResourceKind {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        match self {
103            ResourceKind::Extension => write!(f, "extension"),
104            ResourceKind::Skill => write!(f, "skill"),
105            ResourceKind::Prompt => write!(f, "prompt"),
106            ResourceKind::Theme => write!(f, "theme"),
107        }
108    }
109}
110
111// All resource kinds for iteration
112
113/// Package manifest describing bundled resources
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct PackageManifest {
116    /// Package name (e.g. "@foo/oxi-tools")
117    pub name: String,
118    /// Semantic version (e.g. "1.0.0")
119    pub version: String,
120    /// Extension paths relative to the package root
121    #[serde(default)]
122    pub extensions: Vec<String>,
123    /// Skill names/paths
124    #[serde(default)]
125    pub skills: Vec<String>,
126    /// Prompt template paths
127    #[serde(default)]
128    pub prompts: Vec<String>,
129    /// Theme paths
130    #[serde(default)]
131    pub themes: Vec<String>,
132    /// Optional description
133    #[serde(default)]
134    pub description: Option<String>,
135    /// Package dependencies (name -> version constraint)
136    #[serde(default)]
137    pub dependencies: BTreeMap<String, String>,
138}
139
140/// A discovered resource within a package
141#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct DiscoveredResource {
143    /// Resource type
144    pub kind: ResourceKind,
145    /// Absolute path to the resource
146    pub path: PathBuf,
147    /// Relative path within the package
148    pub relative_path: String,
149}
150
151/// Metadata about a resolved resource path
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct PathMetadata {
154    /// Source specifier
155    pub source: String,
156    /// Scope (user / project)
157    pub scope: SourceScope,
158    /// Whether this is a package resource or top-level
159    pub origin: ResourceOrigin,
160    /// Base directory for resolving relative paths
161    pub base_dir: Option<PathBuf>,
162}
163
164/// Origin of a resource
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
166#[serde(rename_all = "snake_case")]
167pub enum ResourceOrigin {
168    /// package variant.
169    Package,
170    /// top level variant.
171    TopLevel,
172}
173
174/// Scope for package sources
175#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
176#[serde(rename_all = "snake_case")]
177pub enum SourceScope {
178    /// user variant.
179    User,
180    /// project variant.
181    Project,
182}
183
184impl std::fmt::Display for SourceScope {
185    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
186        match self {
187            SourceScope::User => write!(f, "user"),
188            SourceScope::Project => write!(f, "project"),
189        }
190    }
191}
192
193/// A resolved resource with metadata
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct ResolvedResource {
196    /// Absolute path to the resource
197    pub path: PathBuf,
198    /// Whether this resource is enabled
199    pub enabled: bool,
200    /// Metadata about the resource
201    pub metadata: PathMetadata,
202}
203
204/// Resolved paths for all resource types
205#[derive(Debug, Clone, Default, Serialize, Deserialize)]
206pub struct ResolvedPaths {
207    /// pub.
208    pub extensions: Vec<ResolvedResource>,
209    /// pub.
210    pub skills: Vec<ResolvedResource>,
211    /// pub.
212    pub prompts: Vec<ResolvedResource>,
213    /// pub.
214    pub themes: Vec<ResolvedResource>,
215}
216
217/// Progress events for package operations
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct ProgressEvent {
220    /// pub.
221    pub event_type: ProgressEventType,
222    /// pub.
223    pub action: ProgressAction,
224    /// pub.
225    pub source: String,
226    /// pub.
227    pub message: Option<String>,
228}
229
230/// Progress event type
231#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
232#[serde(rename_all = "snake_case")]
233pub enum ProgressEventType {
234    /// start variant.
235    Start,
236    /// progress variant.
237    Progress,
238    /// complete variant.
239    Complete,
240    /// error variant.
241    Error,
242}
243
244/// Action being performed
245#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
246#[serde(rename_all = "snake_case")]
247pub enum ProgressAction {
248    /// install variant.
249    Install,
250    /// remove variant.
251    Remove,
252    /// update variant.
253    Update,
254    /// clone variant.
255    Clone,
256    /// pull variant.
257    Pull,
258}
259
260impl std::fmt::Display for ProgressAction {
261    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
262        match self {
263            ProgressAction::Install => write!(f, "install"),
264            ProgressAction::Remove => write!(f, "remove"),
265            ProgressAction::Update => write!(f, "update"),
266            ProgressAction::Clone => write!(f, "clone"),
267            ProgressAction::Pull => write!(f, "pull"),
268        }
269    }
270}
271
272/// Callback for progress events
273pub type ProgressCallback = Box<dyn Fn(ProgressEvent) + Send + Sync>;
274
275// ── Source parsing ────────────────────────────────────────────────────
276
277/// Parsed package source
278#[derive(Debug, Clone, Serialize, Deserialize)]
279#[serde(tag = "type", rename_all = "snake_case")]
280pub enum ParsedSource {
281    /// Variant.
282    Npm {
283        /// Full spec (e.g. "express@4.18.0")
284        spec: String,
285        /// Package name without version
286        name: String,
287        /// Whether a version was pinned
288        pinned: bool,
289    },
290    /// Variant.
291    Git {
292        /// Full repository URL
293        repo: String,
294        /// Host (e.g. "github.com")
295        host: String,
296        /// Path on host (e.g. "org/repo")
297        path: String,
298        /// Optional ref (branch / tag / commit)
299        ref_: Option<String>,
300    },
301    /// Variant.
302    Local {
303        /// Local path
304        path: String,
305    },
306    /// Variant.
307    Url {
308        /// URL to archive
309        url: String,
310    },
311}
312
313impl ParsedSource {
314    /// Parse a source string into a ParsedSource
315    pub fn parse(source: &str) -> Self {
316        if let Some(rest) = source.strip_prefix("npm:") {
317            let spec = rest.trim();
318            let (name, pinned) = parse_npm_spec(spec);
319            return ParsedSource::Npm {
320                spec: spec.to_string(),
321                name,
322                pinned,
323            };
324        }
325
326        if let Some(rest) = source.strip_prefix("github:") {
327            let parts: Vec<&str> = rest.splitn(2, '/').collect();
328            if parts.len() == 2 {
329                let (path, ref_) = split_git_path_ref(rest);
330                return ParsedSource::Git {
331                    repo: format!("https://github.com/{}.git", path),
332                    host: "github.com".to_string(),
333                    path,
334                    ref_,
335                };
336            }
337        }
338
339        if source.starts_with("git+") || source.starts_with("git://") || source.starts_with("git@")
340        {
341            return parse_git_source(source);
342        }
343
344        if source.starts_with("https://") || source.starts_with("http://") {
345            // Distinguish git URLs from plain archive URLs
346            if source.ends_with(".git")
347                || source.contains("github.com")
348                || source.contains("gitlab.com")
349            {
350                return parse_git_source(source);
351            }
352            // Archive URL (.tar.gz, .zip, .tgz)
353            if source.ends_with(".tar.gz")
354                || source.ends_with(".tgz")
355                || source.ends_with(".zip")
356                || source.ends_with(".tar.bz2")
357            {
358                return ParsedSource::Url {
359                    url: source.to_string(),
360                };
361            }
362            // Default to git for http(s) URLs that look like repos
363            return parse_git_source(source);
364        }
365
366        // Local path
367        ParsedSource::Local {
368            path: source.to_string(),
369        }
370    }
371
372    /// Get a unique identity key for this source (ignoring version/ref)
373    pub fn identity(&self) -> String {
374        match self {
375            ParsedSource::Npm { name, .. } => format!("npm:{}", name),
376            ParsedSource::Git { host, path, .. } => format!("git:{}/{}", host, path),
377            ParsedSource::Local { path } => format!("local:{}", path),
378            ParsedSource::Url { url } => format!("url:{}", url),
379        }
380    }
381
382    /// Get a display-friendly name
383    pub fn display_name(&self) -> String {
384        match self {
385            ParsedSource::Npm { name, .. } => name.clone(),
386            ParsedSource::Git { host, path, .. } => format!("{}/{}", host, path),
387            ParsedSource::Local { path } => path.clone(),
388            ParsedSource::Url { url } => url.clone(),
389        }
390    }
391}
392
393/// Parse an npm spec into (name, pinned)
394fn parse_npm_spec(spec: &str) -> (String, bool) {
395    // Handle scoped packages like @scope/name@version
396    if let Some(caps) = NPM_SPEC_RE.captures(spec) {
397        let name = caps.get(1).map(|m| m.as_str()).unwrap_or(spec);
398        let has_version = caps.get(2).is_some();
399        return (name.to_string(), has_version);
400    }
401    (spec.to_string(), false)
402}
403
404/// Split a git path like "org/repo@ref" into ("org/repo", Some("ref"))
405fn split_git_path_ref(input: &str) -> (String, Option<String>) {
406    if let Some(at_pos) = input.rfind('@') {
407        // Make sure it's not part of an email (don't split if there's no / before @)
408        if input[..at_pos].contains('/') {
409            return (
410                input[..at_pos].to_string(),
411                Some(input[at_pos + 1..].to_string()),
412            );
413        }
414    }
415    (input.to_string(), None)
416}
417
418/// Parse a git source string
419fn parse_git_source(source: &str) -> ParsedSource {
420    // Handle git@host:path format (SSH)
421    if let Some(rest) = source.strip_prefix("git@") {
422        let colon_pos = rest.find(':').unwrap_or(rest.len());
423        let host = &rest[..colon_pos];
424        let path_part = rest.get(colon_pos + 1..).unwrap_or("");
425        let (path, ref_) = if let Some(hash_pos) = path_part.rfind('#') {
426            (
427                path_part[..hash_pos].to_string(),
428                Some(path_part[hash_pos + 1..].to_string()),
429            )
430        } else {
431            split_git_path_ref(path_part)
432        };
433        let repo = format!("git@{}:{}", host, path_part);
434        let host = host.to_string();
435        return ParsedSource::Git {
436            repo,
437            host,
438            path: path.trim_end_matches(".git").to_string(),
439            ref_,
440        };
441    }
442
443    // Handle git+ssh://, git+https://, git:// prefixes
444    let url_str = source
445        .strip_prefix("git+")
446        .unwrap_or(source)
447        .strip_prefix("git://")
448        .map(|s| format!("https://{}", s))
449        .unwrap_or_else(|| source.strip_prefix("git+").unwrap_or(source).to_string());
450
451    // Parse URL to extract host and path
452    let url = match url::Url::parse(&url_str) {
453        Ok(u) => u,
454        Err(_) => {
455            return ParsedSource::Local {
456                path: source.to_string(),
457            };
458        }
459    };
460
461    let host = url.host_str().unwrap_or("unknown").to_string();
462    let full_path = url.path().trim_start_matches('/').to_string();
463
464    // Check for #ref fragment
465    let fragment = url.fragment().map(|f| f.to_string());
466
467    let (path, ref_) = if let Some(frag) = fragment {
468        (full_path.trim_end_matches(".git").to_string(), Some(frag))
469    } else {
470        let (p, r) = split_git_path_ref(&full_path);
471        (p.trim_end_matches(".git").to_string(), r)
472    };
473
474    let repo = url_str.clone();
475
476    ParsedSource::Git {
477        repo,
478        host,
479        path,
480        ref_,
481    }
482}
483
484// ── NPM Registry ──────────────────────────────────────────────────────
485
486/// Information fetched from the npm registry
487#[derive(Debug, Clone, Serialize, Deserialize)]
488pub struct NpmPackageInfo {
489    /// pub.
490    pub name: String,
491    /// pub.
492    pub versions: BTreeMap<String, serde_json::Value>,
493    /// pub.
494    #[serde(rename = "dist-tags")]
495    pub dist_tags: BTreeMap<String, String>,
496}
497
498impl NpmPackageInfo {
499    /// Fetch package info from the npm registry
500    pub async fn fetch(name: &str) -> Result<Self> {
501        let url = format!("https://registry.npmjs.org/{}", name);
502        let client = shared_http_client();
503
504        let resp = client
505            .get(&url)
506            .header("Accept", "application/json")
507            .send()
508            .await
509            .with_context(|| format!("Failed to fetch npm info for '{}'", name))?;
510
511        if !resp.status().is_success() {
512            bail!("npm registry returned {} for '{}'", resp.status(), name);
513        }
514
515        let info: NpmPackageInfo = resp
516            .json()
517            .await
518            .with_context(|| format!("Failed to parse npm registry response for '{}'", name))?;
519
520        Ok(info)
521    }
522
523    /// Get the latest version from dist-tags
524    pub fn latest_version(&self) -> Option<&str> {
525        self.dist_tags.get("latest").map(|s| s.as_str())
526    }
527
528    /// Find the best matching version for a constraint
529    pub fn resolve_version(&self, constraint: &str) -> Option<String> {
530        if constraint == "latest" || constraint.is_empty() {
531            return self.latest_version().map(|s| s.to_string());
532        }
533
534        // Try exact match first
535        if self.versions.contains_key(constraint) {
536            return Some(constraint.to_string());
537        }
538
539        // Try semver range matching
540        if let Ok(req) = semver::VersionReq::parse(constraint) {
541            let mut best: Option<semver::Version> = None;
542            for ver_str in self.versions.keys() {
543                if let Ok(ver) = semver::Version::parse(ver_str)
544                    && req.matches(&ver)
545                {
546                    match &best {
547                        Some(b) if ver > *b => best = Some(ver),
548                        None => best = Some(ver),
549                        _ => {}
550                    }
551                }
552            }
553            if let Some(v) = best {
554                return Some(v.to_string());
555            }
556        }
557
558        None
559    }
560}
561
562/// Get the latest version of an npm package
563pub async fn get_latest_npm_version(name: &str) -> Result<String> {
564    let info = NpmPackageInfo::fetch(name).await?;
565    info.latest_version()
566        .map(|s| s.to_string())
567        .context(format!("No latest version found for '{}'", name))
568}
569
570// ── Git Operations ────────────────────────────────────────────────────
571
572/// Run a git command and capture stdout
573fn git_command(args: &[&str], cwd: Option<&Path>) -> Result<String> {
574    let mut cmd = std::process::Command::new("git");
575    cmd.args(args)
576        .env("GIT_TERMINAL_PROMPT", "0")
577        .stdout(std::process::Stdio::piped())
578        .stderr(std::process::Stdio::piped());
579
580    if let Some(dir) = cwd {
581        cmd.current_dir(dir);
582    }
583
584    let output = cmd.output().context("Failed to execute git")?;
585
586    if !output.status.success() {
587        let stderr = String::from_utf8_lossy(&output.stderr);
588        bail!(
589            "git {} failed ({}): {}",
590            args.join(" "),
591            output.status,
592            stderr.trim()
593        );
594    }
595
596    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
597}
598
599/// Run a git command (no capture)
600fn git_command_silent(args: &[&str], cwd: Option<&Path>) -> Result<()> {
601    let mut cmd = std::process::Command::new("git");
602    cmd.args(args)
603        .env("GIT_TERMINAL_PROMPT", "0")
604        .stdout(std::process::Stdio::null())
605        .stderr(std::process::Stdio::null());
606
607    if let Some(dir) = cwd {
608        cmd.current_dir(dir);
609    }
610
611    let status = cmd.status().context("Failed to execute git")?;
612    if !status.success() {
613        bail!("git {} failed ({})", args.join(" "), status);
614    }
615    Ok(())
616}
617
618/// Clone a git repository
619pub fn git_clone(repo_url: &str, target_dir: &Path, ref_: Option<&str>) -> Result<()> {
620    if target_dir.exists() {
621        bail!("Target directory already exists: {}", target_dir.display());
622    }
623    fs::create_dir_all(target_dir)
624        .with_context(|| format!("Failed to create {}", target_dir.display()))?;
625
626    let target_str = target_dir.to_string_lossy().to_string();
627    let args = vec!["clone", repo_url, &target_str];
628
629    git_command_silent(&args, None)?;
630
631    if let Some(r) = ref_ {
632        git_command_silent(&["checkout", r], Some(target_dir))?;
633    }
634
635    Ok(())
636}
637
638/// Pull/update a git repository in place
639pub fn git_update(repo_dir: &Path, ref_: Option<&str>) -> Result<bool> {
640    if !repo_dir.exists() {
641        bail!(
642            "Repository directory does not exist: {}",
643            repo_dir.display()
644        );
645    }
646
647    // Get current HEAD
648    let local_head = git_command(&["rev-parse", "HEAD"], Some(repo_dir))?;
649
650    // Determine what to fetch
651    let fetch_ref = if let Some(r) = ref_ {
652        r.to_string()
653    } else {
654        // Try to get upstream ref
655        match git_command(
656            &["rev-parse", "--abbrev-ref", "@{upstream}"],
657            Some(repo_dir),
658        ) {
659            Ok(upstream) => {
660                if let Some(branch) = upstream.strip_prefix("origin/") {
661                    format!("+refs/heads/{branch}:refs/remotes/origin/{branch}")
662                } else {
663                    "+HEAD:refs/remotes/origin/HEAD".to_string()
664                }
665            }
666            Err(_) => "+HEAD:refs/remotes/origin/HEAD".to_string(),
667        }
668    };
669
670    git_command_silent(
671        &["fetch", "--prune", "--no-tags", "origin", &fetch_ref],
672        Some(repo_dir),
673    )?;
674
675    // Determine what to reset to
676    let target_ref = ref_.unwrap_or("origin/HEAD");
677    let remote_head = git_command(&["rev-parse", target_ref], Some(repo_dir))?;
678
679    if local_head == remote_head {
680        return Ok(false); // No update needed
681    }
682
683    git_command_silent(&["reset", "--hard", target_ref], Some(repo_dir))?;
684    git_command_silent(&["clean", "-fdx"], Some(repo_dir))?;
685
686    Ok(true) // Updated
687}
688
689/// Check if a git repo has remote updates available
690pub fn git_has_update(repo_dir: &Path) -> Result<bool> {
691    let local_head = git_command(&["rev-parse", "HEAD"], Some(repo_dir))?;
692
693    // Try to get upstream
694    let upstream_ref = match git_command(
695        &["rev-parse", "--abbrev-ref", "@{upstream}"],
696        Some(repo_dir),
697    ) {
698        Ok(u) if u.starts_with("origin/") => {
699            let branch = &u["origin/".len()..];
700            format!("refs/heads/{branch}")
701        }
702        _ => "HEAD".to_string(),
703    };
704
705    // Fetch quietly and check remote
706    let _ = git_command_silent(&["fetch", "--prune", "--no-tags", "origin"], Some(repo_dir));
707
708    let remote_head = git_command(&["ls-remote", "origin", &upstream_ref], None)?;
709
710    // Parse first hash from ls-remote output
711    let remote_hash = remote_head
712        .lines()
713        .next()
714        .and_then(|line| line.split_whitespace().next())
715        .unwrap_or("");
716
717    Ok(local_head != remote_hash)
718}
719
720// ── Lockfile ──────────────────────────────────────────────────────────
721
722/// Lockfile entry for an installed package
723#[derive(Debug, Clone, Serialize, Deserialize)]
724pub struct LockEntry {
725    /// Source specifier
726    pub source: String,
727    /// Package name
728    pub name: String,
729    /// Resolved version or ref
730    pub version: String,
731    /// Integrity hash (sha256)
732    pub integrity: Option<String>,
733    /// Scope
734    pub scope: SourceScope,
735    /// Type of source
736    pub source_type: String,
737    /// Dependencies
738    #[serde(default)]
739    pub dependencies: BTreeMap<String, String>,
740}
741
742/// The lockfile structure
743#[derive(Debug, Clone, Serialize, Deserialize)]
744pub struct Lockfile {
745    /// Lockfile version
746    pub version: u32,
747    /// Locked packages
748    pub packages: BTreeMap<String, LockEntry>,
749}
750
751impl Lockfile {
752    /// Create a new empty lockfile
753    pub fn new() -> Self {
754        Self {
755            version: 1,
756            packages: BTreeMap::new(),
757        }
758    }
759
760    /// Read lockfile from disk
761    pub fn read(path: &Path) -> Result<Option<Self>> {
762        if !path.exists() {
763            return Ok(None);
764        }
765        let content = fs::read_to_string(path)
766            .with_context(|| format!("Failed to read lockfile {}", path.display()))?;
767        let lock: Lockfile = serde_json::from_str(&content)
768            .with_context(|| format!("Failed to parse lockfile {}", path.display()))?;
769        Ok(Some(lock))
770    }
771
772    /// Write lockfile to disk
773    pub fn write(&self, path: &Path) -> Result<()> {
774        let content = serde_json::to_string_pretty(self).context("Failed to serialize lockfile")?;
775        fs::write(path, content)
776            .with_context(|| format!("Failed to write lockfile {}", path.display()))?;
777        Ok(())
778    }
779
780    /// Add or update an entry
781    pub fn insert(&mut self, entry: LockEntry) {
782        self.packages.insert(entry.name.clone(), entry);
783    }
784
785    /// Remove an entry
786    pub fn remove(&mut self, name: &str) -> Option<LockEntry> {
787        self.packages.remove(name)
788    }
789
790    /// Check if a package is locked
791    pub fn contains(&self, name: &str) -> bool {
792        self.packages.contains_key(name)
793    }
794
795    /// Get an entry
796    pub fn get(&self, name: &str) -> Option<&LockEntry> {
797        self.packages.get(name)
798    }
799}
800
801impl Default for Lockfile {
802    fn default() -> Self {
803        Self::new()
804    }
805}
806
807// ── Counts ────────────────────────────────────────────────────────────
808
809/// Counts of each resource type in a package
810#[derive(Debug, Clone, Default, Serialize, Deserialize)]
811pub struct ResourceCounts {
812    /// pub.
813    pub extensions: usize,
814    /// pub.
815    pub skills: usize,
816    /// pub.
817    pub prompts: usize,
818    /// pub.
819    pub themes: usize,
820}
821
822impl std::fmt::Display for ResourceCounts {
823    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
824        let mut parts = Vec::new();
825        if self.extensions > 0 {
826            parts.push(format!("{} ext", self.extensions));
827        }
828        if self.skills > 0 {
829            parts.push(format!("{} skill", self.skills));
830        }
831        if self.prompts > 0 {
832            parts.push(format!("{} prompt", self.prompts));
833        }
834        if self.themes > 0 {
835            parts.push(format!("{} theme", self.themes));
836        }
837        if parts.is_empty() {
838            write!(f, "-")?;
839        } else {
840            write!(f, "{}", parts.join(", "))?;
841        }
842        Ok(())
843    }
844}
845
846// ── Package Manager ───────────────────────────────────────────────────
847
848/// Information about an available package update
849#[derive(Debug, Clone, Serialize, Deserialize)]
850pub struct PackageUpdateInfo {
851    /// pub.
852    pub source: String,
853    /// pub.
854    pub display_name: String,
855    /// pub.
856    pub source_type: String, // "npm" or "git"
857    /// pub.
858    pub scope: SourceScope,
859}
860
861/// A configured package
862#[derive(Debug, Clone, Serialize, Deserialize)]
863pub struct ConfiguredPackage {
864    /// pub.
865    pub source: String,
866    /// pub.
867    pub scope: SourceScope,
868    /// pub.
869    pub filtered: bool,
870    /// pub.
871    pub installed_path: Option<PathBuf>,
872}
873
874/// Manages installation, removal, and listing of packages
875pub struct PackageManager {
876    packages_dir: PathBuf,
877    /// Base directory for project-scoped packages
878    project_dir: PathBuf,
879    installed: HashMap<String, PackageManifest>,
880    lockfile: Lockfile,
881    progress_callback: Option<Box<dyn Fn(ProgressEvent) + Send + Sync>>,
882}
883
884impl PackageManager {
885    /// Create a new PackageManager using the default packages directory
886    pub fn new() -> Result<Self> {
887        let base = dirs::home_dir().context("Cannot determine home directory")?;
888        let packages_dir = base.join(".oxi").join("packages");
889        let project_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
890        let mut mgr = Self {
891            packages_dir,
892            project_dir,
893            installed: HashMap::new(),
894            lockfile: Lockfile::new(),
895            progress_callback: None,
896        };
897        mgr.load_installed()?;
898        mgr.load_lockfile()?;
899        Ok(mgr)
900    }
901
902    /// Create a PackageManager with a custom packages directory (for testing)
903    pub fn with_dir(packages_dir: PathBuf) -> Result<Self> {
904        let project_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
905        let mut mgr = Self {
906            packages_dir,
907            project_dir,
908            installed: HashMap::new(),
909            lockfile: Lockfile::new(),
910            progress_callback: None,
911        };
912        mgr.load_installed()?;
913        mgr.load_lockfile()?;
914        Ok(mgr)
915    }
916
917    /// Set the project directory for project-scoped packages
918    pub fn set_project_dir(&mut self, dir: PathBuf) {
919        self.project_dir = dir;
920    }
921
922    /// Set a progress callback
923    pub fn set_progress_callback(&mut self, callback: Box<dyn Fn(ProgressEvent) + Send + Sync>) {
924        self.progress_callback = Some(callback);
925    }
926
927    fn emit_progress(&self, event: ProgressEvent) {
928        if let Some(ref cb) = self.progress_callback {
929            cb(event);
930        }
931    }
932
933    // ── Loading ───────────────────────────────────────────────────────
934
935    /// Load all installed package manifests from disk
936    fn load_installed(&mut self) -> Result<()> {
937        if !self.packages_dir.exists() {
938            return Ok(());
939        }
940        for entry in fs::read_dir(&self.packages_dir)? {
941            let entry = entry?;
942            let manifest_path = entry.path().join(MANIFEST_NAME);
943            if manifest_path.exists() {
944                match Self::read_manifest(&manifest_path) {
945                    Ok(manifest) => {
946                        self.installed.insert(manifest.name.clone(), manifest);
947                    }
948                    Err(e) => {
949                        tracing::warn!(
950                            "Failed to load manifest {}: {}",
951                            manifest_path.display(),
952                            e
953                        );
954                    }
955                }
956            }
957        }
958        Ok(())
959    }
960
961    /// Load lockfile from disk
962    fn load_lockfile(&mut self) -> Result<()> {
963        let lock_path = self.packages_dir.join(LOCKFILE_NAME);
964        if let Some(lock) = Lockfile::read(&lock_path)? {
965            self.lockfile = lock;
966        }
967        Ok(())
968    }
969
970    /// Save lockfile to disk
971    fn save_lockfile(&self) -> Result<()> {
972        let lock_path = self.packages_dir.join(LOCKFILE_NAME);
973        self.lockfile.write(&lock_path)
974    }
975
976    // ── Manifest ──────────────────────────────────────────────────────
977
978    /// Read and parse a package manifest from disk
979    fn read_manifest(path: &Path) -> Result<PackageManifest> {
980        let content = fs::read_to_string(path)
981            .with_context(|| format!("Failed to read manifest {}", path.display()))?;
982        let manifest: PackageManifest = toml::from_str(&content)
983            .with_context(|| format!("Failed to parse manifest {}", path.display()))?;
984        Ok(manifest)
985    }
986
987    /// Try to read a `package.json` manifest (for npm packages)
988    fn read_package_json(dir: &Path) -> Option<serde_json::Value> {
989        let path = dir.join(NPM_MANIFEST_NAME);
990        let content = fs::read_to_string(path).ok()?;
991        serde_json::from_str(&content).ok()
992    }
993
994    // ── Path helpers ──────────────────────────────────────────────────
995
996    /// Get the installation directory for a package
997    fn pkg_install_dir(&self, name: &str) -> PathBuf {
998        let safe_name = name.replace('@', "").replace('/', "-");
999        self.packages_dir.join(safe_name)
1000    }
1001
1002    /// Get the packages directory path
1003    pub fn packages_dir(&self) -> &Path {
1004        &self.packages_dir
1005    }
1006
1007    /// Get install dir for a git source
1008    fn git_install_path(&self, host: &str, path: &str, scope: SourceScope) -> PathBuf {
1009        match scope {
1010            SourceScope::Project => self
1011                .project_dir
1012                .join(".oxi")
1013                .join("git")
1014                .join(host)
1015                .join(path),
1016            SourceScope::User => self.packages_dir.join("git").join(host).join(path),
1017        }
1018    }
1019
1020    /// Get install dir for an npm source
1021    fn npm_install_path(&self, name: &str, scope: SourceScope) -> PathBuf {
1022        let safe_name = name.replace('@', "").replace('/', "-");
1023        match scope {
1024            SourceScope::Project => self.project_dir.join(".oxi").join("npm").join(safe_name),
1025            SourceScope::User => self.packages_dir.join("npm").join(safe_name),
1026        }
1027    }
1028
1029    // ── Install ───────────────────────────────────────────────────────
1030
1031    /// Ensure packages directory exists
1032    fn ensure_packages_dir(&self) -> Result<()> {
1033        fs::create_dir_all(&self.packages_dir).with_context(|| {
1034            format!(
1035                "Failed to create packages directory {}",
1036                self.packages_dir.display()
1037            )
1038        })
1039    }
1040
1041    /// Install a package from a local directory path
1042    pub fn install(&mut self, source: &str) -> Result<PackageManifest> {
1043        let parsed = ParsedSource::parse(source);
1044        match parsed {
1045            ParsedSource::Local { path } => self.install_local(&path),
1046            _ => bail!("Use install_from_source() for non-local packages"),
1047        }
1048    }
1049
1050    /// Install a package from a local directory path
1051    fn install_local(&mut self, path: &str) -> Result<PackageManifest> {
1052        let source_path = Path::new(path);
1053        let manifest_path = source_path.join(MANIFEST_NAME);
1054
1055        let manifest = if manifest_path.exists() {
1056            Self::read_manifest(&manifest_path)
1057                .with_context(|| format!("No valid {} found in {}", MANIFEST_NAME, path))?
1058        } else {
1059            // Synthesise a minimal manifest
1060            let name = source_path
1061                .file_name()
1062                .map(|n| n.to_string_lossy().to_string())
1063                .unwrap_or_else(|| "unknown".to_string());
1064            PackageManifest {
1065                name,
1066                version: "0.0.0".to_string(),
1067                extensions: Vec::new(),
1068                skills: Vec::new(),
1069                prompts: Vec::new(),
1070                themes: Vec::new(),
1071                description: None,
1072                dependencies: BTreeMap::new(),
1073            }
1074        };
1075
1076        let dest = self.pkg_install_dir(&manifest.name);
1077        self.ensure_packages_dir()?;
1078
1079        if dest.exists() {
1080            fs::remove_dir_all(&dest).with_context(|| {
1081                format!("Failed to remove existing package at {}", dest.display())
1082            })?;
1083        }
1084
1085        copy_dir_recursive(source_path, &dest).with_context(|| {
1086            format!("Failed to copy package from {} to {}", path, dest.display())
1087        })?;
1088
1089        let integrity = compute_dir_hash(&dest);
1090
1091        self.lockfile.insert(LockEntry {
1092            source: path.to_string(),
1093            name: manifest.name.clone(),
1094            version: manifest.version.clone(),
1095            integrity,
1096            scope: SourceScope::User,
1097            source_type: "local".to_string(),
1098            dependencies: manifest.dependencies.clone(),
1099        });
1100
1101        self.installed
1102            .insert(manifest.name.clone(), manifest.clone());
1103        let _ = self.save_lockfile();
1104        Ok(manifest)
1105    }
1106
1107    /// Install from any source
1108    pub fn install_from_source(
1109        &mut self,
1110        source: &str,
1111        scope: SourceScope,
1112    ) -> Result<PackageManifest> {
1113        let parsed = ParsedSource::parse(source);
1114        self.emit_progress(ProgressEvent {
1115            event_type: ProgressEventType::Start,
1116            action: ProgressAction::Install,
1117            source: source.to_string(),
1118            message: Some(format!("Installing {}...", source)),
1119        });
1120        let result = match &parsed {
1121            ParsedSource::Npm { .. } => run_on_fresh_runtime(self.install_npm_async(source, scope)),
1122            ParsedSource::Git { repo, ref_, .. } => {
1123                self.install_git_sync(source, repo, ref_.as_deref(), scope)
1124            }
1125            ParsedSource::Local { path } => self.install_local(path),
1126            ParsedSource::Url { url } => run_on_fresh_runtime(self.install_url(url, scope)),
1127        };
1128        match &result {
1129            Ok(_) => self.emit_progress(ProgressEvent {
1130                event_type: ProgressEventType::Complete,
1131                action: ProgressAction::Install,
1132                source: source.to_string(),
1133                message: None,
1134            }),
1135            Err(e) => self.emit_progress(ProgressEvent {
1136                event_type: ProgressEventType::Error,
1137                action: ProgressAction::Install,
1138                source: source.to_string(),
1139                message: Some(e.to_string()),
1140            }),
1141        }
1142        result
1143    }
1144
1145    /// Async install from npm using registry
1146    async fn install_npm_async(
1147        &mut self,
1148        source: &str,
1149        scope: SourceScope,
1150    ) -> Result<PackageManifest> {
1151        let parsed = ParsedSource::parse(source);
1152        let (spec, name, pinned) = match &parsed {
1153            ParsedSource::Npm { spec, name, pinned } => (spec.clone(), name.clone(), *pinned),
1154            _ => bail!("Expected npm source"),
1155        };
1156
1157        // Resolve version
1158        let _version = if pinned {
1159            // Extract version from spec
1160            let (_, ver) = parse_npm_spec(&spec);
1161            if ver {
1162                spec.rsplit('@').next().unwrap_or("latest").to_string()
1163            } else {
1164                "latest".to_string()
1165            }
1166        } else {
1167            get_latest_npm_version(&name)
1168                .await
1169                .unwrap_or_else(|_| "latest".to_string())
1170        };
1171
1172        // Use npm pack approach
1173        self.install_npm_pack(&spec, scope)
1174    }
1175
1176    /// Install npm package using `npm pack`
1177    fn install_npm_pack(&mut self, spec: &str, scope: SourceScope) -> Result<PackageManifest> {
1178        let tmp_dir =
1179            tempfile::tempdir().context("Failed to create temp directory for npm install")?;
1180
1181        let output = std::process::Command::new("npm")
1182            .args(["pack", spec, "--pack-destination"])
1183            .arg(tmp_dir.path())
1184            .current_dir(tmp_dir.path())
1185            .output()
1186            .context("Failed to run npm pack")?;
1187
1188        if !output.status.success() {
1189            let stderr = String::from_utf8_lossy(&output.stderr);
1190            bail!("npm pack failed for '{}': {}", spec, stderr);
1191        }
1192
1193        // Find the tarball
1194        let tarball = fs::read_dir(tmp_dir.path())?
1195            .filter_map(|e| e.ok())
1196            .find(|e| {
1197                e.path()
1198                    .extension()
1199                    .map(|ext| ext == "tgz")
1200                    .unwrap_or(false)
1201            })
1202            .map(|e| e.path())
1203            .context("No .tgz file found after npm pack")?;
1204
1205        // Extract tarball
1206        let extract_dir = tmp_dir.path().join("extracted");
1207        fs::create_dir_all(&extract_dir)?;
1208
1209        let tar_status = std::process::Command::new("tar")
1210            .args(["-xzf", &tarball.to_string_lossy(), "-C"])
1211            .arg(&extract_dir)
1212            .output()
1213            .context("Failed to run tar")?;
1214
1215        if !tar_status.status.success() {
1216            let stderr = String::from_utf8_lossy(&tar_status.stderr);
1217            bail!("tar extraction failed: {}", stderr);
1218        }
1219
1220        // npm pack extracts into a "package" subdirectory
1221        let pkg_source = extract_dir.join("package");
1222        let source_for_copy = if pkg_source.exists() {
1223            &pkg_source
1224        } else {
1225            // Might be just the extracted dir
1226            extract_dir.as_path()
1227        };
1228
1229        self.ensure_packages_dir()?;
1230
1231        // Determine package name from manifest or spec
1232        let manifest = if source_for_copy.join(MANIFEST_NAME).exists() {
1233            Self::read_manifest(&source_for_copy.join(MANIFEST_NAME))?
1234        } else if source_for_copy.join(NPM_MANIFEST_NAME).exists() {
1235            let pj = Self::read_package_json(source_for_copy);
1236            let (pkg_name, pkg_version) = pj
1237                .as_ref()
1238                .map(|v| {
1239                    (
1240                        v.get("name")
1241                            .and_then(|n| n.as_str())
1242                            .unwrap_or(spec)
1243                            .to_string(),
1244                        v.get("version")
1245                            .and_then(|v| v.as_str())
1246                            .unwrap_or("0.0.0")
1247                            .to_string(),
1248                    )
1249                })
1250                .unwrap_or((spec.to_string(), "0.0.0".to_string()));
1251
1252            PackageManifest {
1253                name: pkg_name,
1254                version: pkg_version,
1255                extensions: Vec::new(),
1256                skills: Vec::new(),
1257                prompts: Vec::new(),
1258                themes: Vec::new(),
1259                description: None,
1260                dependencies: BTreeMap::new(),
1261            }
1262        } else {
1263            PackageManifest {
1264                name: spec.to_string(),
1265                version: "0.0.0".to_string(),
1266                extensions: Vec::new(),
1267                skills: Vec::new(),
1268                prompts: Vec::new(),
1269                themes: Vec::new(),
1270                description: None,
1271                dependencies: BTreeMap::new(),
1272            }
1273        };
1274
1275        let dest = self.pkg_install_dir(&manifest.name);
1276        if dest.exists() {
1277            fs::remove_dir_all(&dest).with_context(|| {
1278                format!("Failed to remove existing package at {}", dest.display())
1279            })?;
1280        }
1281
1282        copy_dir_recursive(source_for_copy, &dest)
1283            .with_context(|| format!("Failed to copy npm package for '{}'", spec))?;
1284
1285        let integrity = compute_dir_hash(&dest);
1286
1287        self.lockfile.insert(LockEntry {
1288            source: format!("npm:{}", spec),
1289            name: manifest.name.clone(),
1290            version: manifest.version.clone(),
1291            integrity,
1292            scope,
1293            source_type: "npm".to_string(),
1294            dependencies: manifest.dependencies.clone(),
1295        });
1296
1297        self.installed
1298            .insert(manifest.name.clone(), manifest.clone());
1299        let _ = self.save_lockfile();
1300        Ok(manifest)
1301    }
1302
1303    /// Install from git
1304    fn install_git_sync(
1305        &mut self,
1306        source: &str,
1307        repo: &str,
1308        ref_: Option<&str>,
1309        scope: SourceScope,
1310    ) -> Result<PackageManifest> {
1311        let parsed = ParsedSource::parse(source);
1312        let (host, path) = match &parsed {
1313            ParsedSource::Git { host, path, .. } => (host.clone(), path.clone()),
1314            _ => bail!("Expected git source"),
1315        };
1316
1317        let target_dir = self.git_install_path(&host, &path, scope);
1318
1319        if target_dir.exists() {
1320            // Already installed
1321            return self.load_manifest_from_dir(&target_dir, source, scope);
1322        }
1323
1324        let Some(parent) = target_dir.parent() else {
1325            bail!(
1326                "Invalid install path: no parent directory for {}",
1327                target_dir.display()
1328            );
1329        };
1330        fs::create_dir_all(parent)
1331            .with_context(|| format!("Failed to create parent dir for {}", target_dir.display()))?;
1332
1333        git_clone(repo, &target_dir, ref_)?;
1334
1335        // Install npm dependencies if package.json exists
1336        if target_dir.join(NPM_MANIFEST_NAME).exists() {
1337            let _ = std::process::Command::new("npm")
1338                .args(["install", "--omit=dev"])
1339                .current_dir(&target_dir)
1340                .output();
1341        }
1342
1343        self.load_manifest_from_dir(&target_dir, source, scope)
1344    }
1345
1346    /// Load manifest from a directory and register it
1347    fn load_manifest_from_dir(
1348        &mut self,
1349        dir: &Path,
1350        source: &str,
1351        scope: SourceScope,
1352    ) -> Result<PackageManifest> {
1353        let manifest = if dir.join(MANIFEST_NAME).exists() {
1354            Self::read_manifest(&dir.join(MANIFEST_NAME))?
1355        } else {
1356            let name = dir
1357                .file_name()
1358                .map(|n| n.to_string_lossy().to_string())
1359                .unwrap_or_else(|| "unknown".to_string());
1360            PackageManifest {
1361                name,
1362                version: "0.0.0".to_string(),
1363                extensions: Vec::new(),
1364                skills: Vec::new(),
1365                prompts: Vec::new(),
1366                themes: Vec::new(),
1367                description: None,
1368                dependencies: BTreeMap::new(),
1369            }
1370        };
1371
1372        let integrity = compute_dir_hash(dir);
1373
1374        self.lockfile.insert(LockEntry {
1375            source: source.to_string(),
1376            name: manifest.name.clone(),
1377            version: manifest.version.clone(),
1378            integrity,
1379            scope,
1380            source_type: "git".to_string(),
1381            dependencies: manifest.dependencies.clone(),
1382        });
1383
1384        self.installed
1385            .insert(manifest.name.clone(), manifest.clone());
1386        let _ = self.save_lockfile();
1387        Ok(manifest)
1388    }
1389
1390    /// Install from a URL (archive)
1391    async fn install_url(&mut self, url: &str, scope: SourceScope) -> Result<PackageManifest> {
1392        let client = shared_http_client();
1393
1394        let resp = client.get(url).send().await?;
1395        if !resp.status().is_success() {
1396            bail!("Failed to download {}: {}", url, resp.status());
1397        }
1398
1399        let bytes = resp.bytes().await?;
1400
1401        let tmp_dir = tempfile::tempdir()?;
1402        let archive_name = url.split('/').next_back().unwrap_or("archive");
1403        let archive_path = tmp_dir.path().join(archive_name);
1404        fs::write(&archive_path, &bytes)?;
1405
1406        let extract_dir = tmp_dir.path().join("extracted");
1407        fs::create_dir_all(&extract_dir)?;
1408
1409        if archive_name.ends_with(".tar.gz") || archive_name.ends_with(".tgz") {
1410            let status = std::process::Command::new("tar")
1411                .args(["-xzf", &archive_path.to_string_lossy(), "-C"])
1412                .arg(&extract_dir)
1413                .output()?;
1414            if !status.status.success() {
1415                bail!("Failed to extract archive");
1416            }
1417        } else if archive_name.ends_with(".zip") {
1418            // Use unzip if available
1419            let status = std::process::Command::new("unzip")
1420                .arg("-o")
1421                .arg(&archive_path)
1422                .arg("-d")
1423                .arg(&extract_dir)
1424                .output()?;
1425            if !status.status.success() {
1426                bail!("Failed to extract zip archive");
1427            }
1428        } else {
1429            bail!("Unsupported archive format: {}", archive_name);
1430        }
1431
1432        // Find the extracted package directory
1433        let pkg_dir = find_single_subdir(&extract_dir).unwrap_or_else(|| extract_dir.to_path_buf());
1434
1435        self.ensure_packages_dir()?;
1436
1437        let manifest = if pkg_dir.join(MANIFEST_NAME).exists() {
1438            Self::read_manifest(&pkg_dir.join(MANIFEST_NAME))?
1439        } else {
1440            let name = url
1441                .split('/')
1442                .next_back()
1443                .unwrap_or("url-package")
1444                .trim_end_matches(".tar.gz")
1445                .trim_end_matches(".tgz")
1446                .trim_end_matches(".zip")
1447                .to_string();
1448            PackageManifest {
1449                name,
1450                version: "0.0.0".to_string(),
1451                extensions: Vec::new(),
1452                skills: Vec::new(),
1453                prompts: Vec::new(),
1454                themes: Vec::new(),
1455                description: None,
1456                dependencies: BTreeMap::new(),
1457            }
1458        };
1459
1460        let dest = self.pkg_install_dir(&manifest.name);
1461        if dest.exists() {
1462            fs::remove_dir_all(&dest)?;
1463        }
1464
1465        copy_dir_recursive(&pkg_dir, &dest)?;
1466
1467        let integrity = compute_dir_hash(&dest);
1468
1469        self.lockfile.insert(LockEntry {
1470            source: url.to_string(),
1471            name: manifest.name.clone(),
1472            version: manifest.version.clone(),
1473            integrity,
1474            scope,
1475            source_type: "url".to_string(),
1476            dependencies: manifest.dependencies.clone(),
1477        });
1478
1479        self.installed
1480            .insert(manifest.name.clone(), manifest.clone());
1481        let _ = self.save_lockfile();
1482        Ok(manifest)
1483    }
1484
1485    /// Install from npm using `npm pack` (legacy sync method)
1486    pub fn install_npm(&mut self, name: &str) -> Result<PackageManifest> {
1487        self.install_npm_pack(name, SourceScope::User)
1488    }
1489
1490    // ── Uninstall ─────────────────────────────────────────────────────
1491
1492    /// Uninstall a package by name
1493    pub fn uninstall(&mut self, name: &str) -> Result<()> {
1494        if !self.installed.contains_key(name) {
1495            bail!("Package '{}' is not installed", name);
1496        }
1497
1498        let dest = self.pkg_install_dir(name);
1499        if dest.exists() {
1500            fs::remove_dir_all(&dest).with_context(|| {
1501                format!("Failed to remove package directory {}", dest.display())
1502            })?;
1503        }
1504
1505        // Also try to clean up git/npm scoped dirs
1506        // (best effort)
1507        let _ = self.lockfile.remove(name);
1508        let _ = self.save_lockfile();
1509
1510        self.installed.remove(name);
1511        Ok(())
1512    }
1513
1514    /// Uninstall a package from a specific source
1515    pub fn uninstall_from_source(&mut self, source: &str, scope: SourceScope) -> Result<()> {
1516        let parsed = ParsedSource::parse(source);
1517        self.emit_progress(ProgressEvent {
1518            event_type: ProgressEventType::Start,
1519            action: ProgressAction::Remove,
1520            source: source.to_string(),
1521            message: Some(format!("Removing {}...", source)),
1522        });
1523        let result = self.do_uninstall_from_source(&parsed, scope);
1524        match &result {
1525            Ok(_) => self.emit_progress(ProgressEvent {
1526                event_type: ProgressEventType::Complete,
1527                action: ProgressAction::Remove,
1528                source: source.to_string(),
1529                message: None,
1530            }),
1531            Err(e) => self.emit_progress(ProgressEvent {
1532                event_type: ProgressEventType::Error,
1533                action: ProgressAction::Remove,
1534                source: source.to_string(),
1535                message: Some(e.to_string()),
1536            }),
1537        }
1538        result
1539    }
1540
1541    fn do_uninstall_from_source(
1542        &mut self,
1543        parsed: &ParsedSource,
1544        scope: SourceScope,
1545    ) -> Result<()> {
1546        match parsed {
1547            ParsedSource::Npm { name, .. } => {
1548                let dest = self.npm_install_path(name, scope);
1549                if dest.exists() {
1550                    fs::remove_dir_all(&dest)?;
1551                }
1552                self.installed.remove(name);
1553                self.lockfile.remove(name);
1554                let _ = self.save_lockfile();
1555                Ok(())
1556            }
1557            ParsedSource::Git { host, path, .. } => {
1558                let dest = self.git_install_path(host, path, scope);
1559                if dest.exists() {
1560                    fs::remove_dir_all(&dest)?;
1561                    prune_empty_parents(&dest, &self.packages_dir);
1562                }
1563                self.installed.retain(|_, m| {
1564                    let parsed_m = ParsedSource::parse(m.name.as_str());
1565                    parsed_m.identity() != parsed.identity()
1566                });
1567                self.lockfile.packages.retain(|_, entry| {
1568                    let parsed_e = ParsedSource::parse(&entry.source);
1569                    parsed_e.identity() != parsed.identity()
1570                });
1571                let _ = self.save_lockfile();
1572                Ok(())
1573            }
1574            ParsedSource::Local { .. } => Ok(()),
1575            ParsedSource::Url { .. } => {
1576                let identity = parsed.identity();
1577                self.lockfile
1578                    .packages
1579                    .retain(|_, e| ParsedSource::parse(&e.source).identity() != identity);
1580                let _ = self.save_lockfile();
1581                Ok(())
1582            }
1583        }
1584    }
1585
1586    // ── Update ────────────────────────────────────────────────────────
1587
1588    /// Update a package (re-install from the same source).
1589    /// For npm packages, re-runs `npm pack` to get the latest version.
1590    /// For local packages, re-copies from the source path (if available).
1591    /// For git packages, does a git pull.
1592    pub fn update(&mut self, name: &str) -> Result<PackageManifest> {
1593        let lock_entry = self.lockfile.get(name).cloned();
1594
1595        if let Some(entry) = lock_entry {
1596            let parsed = ParsedSource::parse(&entry.source);
1597            return match &parsed {
1598                ParsedSource::Npm { spec, .. } => {
1599                    self.emit_progress(ProgressEvent {
1600                        event_type: ProgressEventType::Start,
1601                        action: ProgressAction::Update,
1602                        source: entry.source.clone(),
1603                        message: Some(format!("Updating {}...", name)),
1604                    });
1605                    let result = self.install_npm_pack(spec, entry.scope);
1606                    match &result {
1607                        Ok(_) => self.emit_progress(ProgressEvent {
1608                            event_type: ProgressEventType::Complete,
1609                            action: ProgressAction::Update,
1610                            source: entry.source.clone(),
1611                            message: None,
1612                        }),
1613                        Err(e) => self.emit_progress(ProgressEvent {
1614                            event_type: ProgressEventType::Error,
1615                            action: ProgressAction::Update,
1616                            source: entry.source.clone(),
1617                            message: Some(e.to_string()),
1618                        }),
1619                    }
1620                    result
1621                }
1622                ParsedSource::Git { repo, ref_, .. } => {
1623                    let target_dir = match &parsed {
1624                        ParsedSource::Git { host, path, .. } => {
1625                            self.git_install_path(host, path, entry.scope)
1626                        }
1627                        _ => unreachable!(),
1628                    };
1629                    if target_dir.exists() {
1630                        let updated = git_update(&target_dir, ref_.as_deref())?;
1631                        if updated && target_dir.join(NPM_MANIFEST_NAME).exists() {
1632                            let _ = std::process::Command::new("npm")
1633                                .args(["install", "--omit=dev"])
1634                                .current_dir(&target_dir)
1635                                .output();
1636                        }
1637                        self.load_manifest_from_dir(&target_dir, &entry.source, entry.scope)
1638                    } else {
1639                        self.install_git_sync(&entry.source, repo, ref_.as_deref(), entry.scope)
1640                    }
1641                }
1642                ParsedSource::Local { path } => self.install_local(path),
1643                ParsedSource::Url { url } => {
1644                    run_on_fresh_runtime(self.install_url(url, entry.scope))
1645                }
1646            };
1647        }
1648
1649        // Fallback: try npm re-install
1650        if self.installed.contains_key(name) {
1651            self.install_npm_pack(name, SourceScope::User)
1652        } else {
1653            bail!("Package '{}' is not installed", name);
1654        }
1655    }
1656
1657    /// Update all installed packages
1658    pub fn update_all(&mut self) -> Vec<(String, Result<PackageManifest>)> {
1659        let names: Vec<String> = self.installed.keys().cloned().collect();
1660        let mut results = Vec::new();
1661        for name in names {
1662            let result = self.update(&name);
1663            results.push((name, result));
1664        }
1665        results
1666    }
1667
1668    /// Check for available updates across all packages
1669    pub async fn check_for_updates(&self) -> Vec<PackageUpdateInfo> {
1670        let mut updates = Vec::new();
1671
1672        for lock_entry in self.lockfile.packages.values() {
1673            let parsed = ParsedSource::parse(&lock_entry.source);
1674
1675            match &parsed {
1676                ParsedSource::Npm { name: pkg_name, .. } => {
1677                    // Check npm for newer version
1678                    match NpmPackageInfo::fetch(pkg_name).await {
1679                        Ok(info) => {
1680                            if let Some(latest) = info.latest_version()
1681                                && latest != lock_entry.version
1682                            {
1683                                updates.push(PackageUpdateInfo {
1684                                    source: lock_entry.source.clone(),
1685                                    display_name: pkg_name.clone(),
1686                                    source_type: "npm".to_string(),
1687                                    scope: lock_entry.scope,
1688                                });
1689                            }
1690                        }
1691                        Err(_) => continue,
1692                    }
1693                }
1694                ParsedSource::Git { host, path, .. } => {
1695                    let install_path = self.git_install_path(host, path, lock_entry.scope);
1696                    if install_path.exists() {
1697                        match git_has_update(&install_path) {
1698                            Ok(true) => {
1699                                updates.push(PackageUpdateInfo {
1700                                    source: lock_entry.source.clone(),
1701                                    display_name: format!("{}/{}", host, path),
1702                                    source_type: "git".to_string(),
1703                                    scope: lock_entry.scope,
1704                                });
1705                            }
1706                            _ => continue,
1707                        }
1708                    }
1709                }
1710                _ => continue,
1711            }
1712        }
1713
1714        updates
1715    }
1716
1717    // ── List / query ──────────────────────────────────────────────────
1718
1719    /// List all installed packages
1720    pub fn list(&self) -> Vec<&PackageManifest> {
1721        self.installed.values().collect()
1722    }
1723
1724    /// List configured packages with metadata
1725    pub fn list_configured(&self) -> Vec<ConfiguredPackage> {
1726        let mut result = Vec::new();
1727        for name in self.installed.keys() {
1728            let installed_path = self.get_install_dir(name);
1729            let lock_entry = self.lockfile.get(name);
1730            result.push(ConfiguredPackage {
1731                source: lock_entry
1732                    .map(|e| e.source.clone())
1733                    .unwrap_or_else(|| name.clone()),
1734                scope: lock_entry.map(|e| e.scope).unwrap_or(SourceScope::User),
1735                filtered: false,
1736                installed_path,
1737            });
1738        }
1739        result
1740    }
1741
1742    /// Check whether a package is installed
1743    pub fn is_installed(&self, name: &str) -> bool {
1744        self.installed.contains_key(name)
1745    }
1746
1747    /// Get the install directory for a package (if it exists on disk)
1748    pub fn get_install_dir(&self, name: &str) -> Option<PathBuf> {
1749        let dir = self.pkg_install_dir(name);
1750        if dir.exists() { Some(dir) } else { None }
1751    }
1752
1753    /// Get the installed path for a source at a given scope
1754    pub fn get_installed_path_for_source(
1755        &self,
1756        source: &str,
1757        scope: SourceScope,
1758    ) -> Option<PathBuf> {
1759        let parsed = ParsedSource::parse(source);
1760        match &parsed {
1761            ParsedSource::Npm { name, .. } => {
1762                let path = self.npm_install_path(name, scope);
1763                if path.exists() { Some(path) } else { None }
1764            }
1765            ParsedSource::Git { host, path, .. } => {
1766                let path = self.git_install_path(host, path, scope);
1767                if path.exists() { Some(path) } else { None }
1768            }
1769            ParsedSource::Local { path } => {
1770                let p = PathBuf::from(path);
1771                if p.exists() { Some(p) } else { None }
1772            }
1773            ParsedSource::Url { .. } => None,
1774        }
1775    }
1776
1777    // ── Resource discovery ────────────────────────────────────────────
1778
1779    /// Discover all resources from an installed package.
1780    pub fn discover_resources(&self, name: &str) -> Result<Vec<DiscoveredResource>> {
1781        let manifest = self
1782            .installed
1783            .get(name)
1784            .with_context(|| format!("Package '{}' not found", name))?;
1785
1786        let install_dir = self.pkg_install_dir(name);
1787        if !install_dir.exists() {
1788            bail!("Install directory for '{}' does not exist", name);
1789        }
1790
1791        let mut resources = Vec::new();
1792
1793        let has_explicit = !manifest.extensions.is_empty()
1794            || !manifest.skills.is_empty()
1795            || !manifest.prompts.is_empty()
1796            || !manifest.themes.is_empty();
1797
1798        if has_explicit {
1799            for ext in &manifest.extensions {
1800                let path = install_dir.join(ext);
1801                if path.exists() {
1802                    resources.push(DiscoveredResource {
1803                        kind: ResourceKind::Extension,
1804                        path,
1805                        relative_path: ext.clone(),
1806                    });
1807                }
1808            }
1809            for skill in &manifest.skills {
1810                let path = install_dir.join(skill);
1811                if path.exists() {
1812                    resources.push(DiscoveredResource {
1813                        kind: ResourceKind::Skill,
1814                        path,
1815                        relative_path: skill.clone(),
1816                    });
1817                }
1818            }
1819            for prompt in &manifest.prompts {
1820                let path = install_dir.join(prompt);
1821                if path.exists() {
1822                    resources.push(DiscoveredResource {
1823                        kind: ResourceKind::Prompt,
1824                        path,
1825                        relative_path: prompt.clone(),
1826                    });
1827                }
1828            }
1829            for theme in &manifest.themes {
1830                let path = install_dir.join(theme);
1831                if path.exists() {
1832                    resources.push(DiscoveredResource {
1833                        kind: ResourceKind::Theme,
1834                        path,
1835                        relative_path: theme.clone(),
1836                    });
1837                }
1838            }
1839        } else {
1840            resources.extend(discover_extensions(&install_dir));
1841            resources.extend(discover_skills(&install_dir));
1842            resources.extend(discover_prompts(&install_dir));
1843            resources.extend(discover_themes(&install_dir));
1844        }
1845
1846        Ok(resources)
1847    }
1848
1849    /// Get resource counts for a package
1850    pub fn resource_counts(&self, name: &str) -> Result<ResourceCounts> {
1851        let resources = self.discover_resources(name)?;
1852        let mut counts = ResourceCounts::default();
1853        for r in &resources {
1854            match r.kind {
1855                ResourceKind::Extension => counts.extensions += 1,
1856                ResourceKind::Skill => counts.skills += 1,
1857                ResourceKind::Prompt => counts.prompts += 1,
1858                ResourceKind::Theme => counts.themes += 1,
1859            }
1860        }
1861        Ok(counts)
1862    }
1863
1864    /// Resolve all resources from all installed packages, producing ResolvedPaths
1865    pub fn resolve(&self) -> ResolvedPaths {
1866        let mut extensions = Vec::new();
1867        let mut skills = Vec::new();
1868        let mut prompts = Vec::new();
1869        let mut themes = Vec::new();
1870
1871        for name in self.installed.keys() {
1872            let install_dir = self.pkg_install_dir(name);
1873            if !install_dir.exists() {
1874                continue;
1875            }
1876
1877            let metadata = PathMetadata {
1878                source: name.clone(),
1879                scope: SourceScope::User,
1880                origin: ResourceOrigin::Package,
1881                base_dir: Some(install_dir.clone()),
1882            };
1883
1884            // Use discover_resources logic
1885            if let Ok(resources) = self.discover_resources(name) {
1886                for r in resources {
1887                    match r.kind {
1888                        ResourceKind::Extension => extensions.push(ResolvedResource {
1889                            path: r.path,
1890                            enabled: true,
1891                            metadata: metadata.clone(),
1892                        }),
1893                        ResourceKind::Skill => skills.push(ResolvedResource {
1894                            path: r.path,
1895                            enabled: true,
1896                            metadata: metadata.clone(),
1897                        }),
1898                        ResourceKind::Prompt => prompts.push(ResolvedResource {
1899                            path: r.path,
1900                            enabled: true,
1901                            metadata: metadata.clone(),
1902                        }),
1903                        ResourceKind::Theme => themes.push(ResolvedResource {
1904                            path: r.path,
1905                            enabled: true,
1906                            metadata: metadata.clone(),
1907                        }),
1908                    }
1909                }
1910            }
1911        }
1912
1913        ResolvedPaths {
1914            extensions,
1915            skills,
1916            prompts,
1917            themes,
1918        }
1919    }
1920
1921    // ── Dependency resolution ─────────────────────────────────────────
1922
1923    /// Resolve dependencies for all installed packages.
1924    /// Returns a list of (package, missing_dependencies) tuples.
1925    pub fn resolve_dependencies(&self) -> Vec<(String, Vec<String>)> {
1926        let mut result = Vec::new();
1927        let installed_names: HashSet<&str> = self.installed.keys().map(|s| s.as_str()).collect();
1928
1929        for (name, manifest) in &self.installed {
1930            let missing: Vec<String> = manifest
1931                .dependencies
1932                .keys()
1933                .filter(|dep| !installed_names.contains(dep.as_str()))
1934                .cloned()
1935                .collect();
1936
1937            if !missing.is_empty() {
1938                result.push((name.clone(), missing));
1939            }
1940        }
1941
1942        result
1943    }
1944
1945    /// Validate a package structure
1946    pub fn validate_package(dir: &Path) -> Result<Vec<String>> {
1947        let mut warnings = Vec::new();
1948
1949        // Check for manifest
1950        if !dir.join(MANIFEST_NAME).exists() && !dir.join(NPM_MANIFEST_NAME).exists() {
1951            warnings.push(format!(
1952                "No {} or {} found",
1953                MANIFEST_NAME, NPM_MANIFEST_NAME
1954            ));
1955        }
1956
1957        // Try to parse manifest
1958        if dir.join(MANIFEST_NAME).exists() {
1959            match Self::read_manifest(&dir.join(MANIFEST_NAME)) {
1960                Ok(m) => {
1961                    if m.name.is_empty() {
1962                        warnings.push("Package name is empty".to_string());
1963                    }
1964                    if m.version.is_empty() {
1965                        warnings.push("Package version is empty".to_string());
1966                    }
1967                    if semver::Version::parse(&m.version).is_err() {
1968                        warnings.push(format!("Version '{}' is not valid semver", m.version));
1969                    }
1970                    let has_resources = !m.extensions.is_empty()
1971                        || !m.skills.is_empty()
1972                        || !m.prompts.is_empty()
1973                        || !m.themes.is_empty();
1974                    if !has_resources {
1975                        // Check if auto-discovery would find anything
1976                        let discovered = discover_extensions(dir)
1977                            .into_iter()
1978                            .chain(discover_skills(dir))
1979                            .chain(discover_prompts(dir))
1980                            .chain(discover_themes(dir))
1981                            .count();
1982                        if discovered == 0 {
1983                            warnings.push(
1984                                "Package has no explicit resources and auto-discovery found nothing"
1985                                    .to_string(),
1986                            );
1987                        }
1988                    }
1989
1990                    // Check that explicit paths exist
1991                    for ext in &m.extensions {
1992                        if !dir.join(ext).exists() {
1993                            warnings.push(format!("Extension path '{}' does not exist", ext));
1994                        }
1995                    }
1996                    for skill in &m.skills {
1997                        if !dir.join(skill).exists() {
1998                            warnings.push(format!("Skill path '{}' does not exist", skill));
1999                        }
2000                    }
2001                    for prompt in &m.prompts {
2002                        if !dir.join(prompt).exists() {
2003                            warnings.push(format!("Prompt path '{}' does not exist", prompt));
2004                        }
2005                    }
2006                    for theme in &m.themes {
2007                        if !dir.join(theme).exists() {
2008                            warnings.push(format!("Theme path '{}' does not exist", theme));
2009                        }
2010                    }
2011                }
2012                Err(e) => {
2013                    warnings.push(format!("Failed to parse {}: {}", MANIFEST_NAME, e));
2014                }
2015            }
2016        }
2017
2018        // Check for .gitignore or .ignore
2019        if !dir.join(".gitignore").exists() && !dir.join(".ignore").exists() {
2020            warnings.push("No .gitignore or .ignore file found".to_string());
2021        }
2022
2023        Ok(warnings)
2024    }
2025
2026    // ── Version queries ───────────────────────────────────────────────
2027
2028    /// Get installed version of a package
2029    pub fn get_installed_version(&self, name: &str) -> Option<&str> {
2030        self.installed.get(name).map(|m| m.version.as_str())
2031    }
2032
2033    /// Check if an installed version satisfies a semver requirement
2034    pub fn version_satisfies(&self, name: &str, requirement: &str) -> bool {
2035        if let Some(version) = self.get_installed_version(name)
2036            && let Ok(v) = semver::Version::parse(version)
2037            && let Ok(req) = semver::VersionReq::parse(requirement)
2038        {
2039            return req.matches(&v);
2040        }
2041        false
2042    }
2043
2044    /// Get the lockfile
2045    pub fn lockfile(&self) -> &Lockfile {
2046        &self.lockfile
2047    }
2048}
2049
2050// ── Auto-discovery helpers ────────────────────────────────────────────
2051
2052/// Discover extension files in a directory.
2053fn discover_extensions(dir: &Path) -> Vec<DiscoveredResource> {
2054    let mut results = Vec::new();
2055    discover_extensions_recursive(dir, dir, &mut results);
2056    results
2057}
2058
2059fn discover_extensions_recursive(
2060    base: &Path,
2061    current: &Path,
2062    results: &mut Vec<DiscoveredResource>,
2063) {
2064    if !current.exists() {
2065        return;
2066    }
2067
2068    let entries = match fs::read_dir(current) {
2069        Ok(e) => e,
2070        Err(_) => return,
2071    };
2072
2073    for entry in entries.flatten() {
2074        let path = entry.path();
2075        let name = entry.file_name();
2076        let name_str = name.to_string_lossy();
2077
2078        if name_str.starts_with('.') || name_str == "node_modules" {
2079            continue;
2080        }
2081
2082        if path.is_dir() {
2083            // Check for index.ts / index.js in subdirectory
2084            for index in &["index.ts", "index.js"] {
2085                let index_path = path.join(index);
2086                if index_path.exists() {
2087                    let rel = path.strip_prefix(base).unwrap_or(&path);
2088                    results.push(DiscoveredResource {
2089                        kind: ResourceKind::Extension,
2090                        path: index_path,
2091                        relative_path: rel.join(index).to_string_lossy().to_string(),
2092                    });
2093                }
2094            }
2095        } else {
2096            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
2097            if matches!(ext, "so" | "dylib" | "dll" | "ts" | "js") {
2098                let rel = path.strip_prefix(base).unwrap_or(&path);
2099                results.push(DiscoveredResource {
2100                    kind: ResourceKind::Extension,
2101                    path: path.clone(),
2102                    relative_path: rel.to_string_lossy().to_string(),
2103                });
2104            }
2105        }
2106    }
2107}
2108
2109/// Discover skill directories containing SKILL.md
2110fn discover_skills(dir: &Path) -> Vec<DiscoveredResource> {
2111    let mut results = Vec::new();
2112    discover_skills_recursive(dir, dir, &mut results);
2113    results
2114}
2115
2116fn discover_skills_recursive(base: &Path, current: &Path, results: &mut Vec<DiscoveredResource>) {
2117    if !current.exists() {
2118        return;
2119    }
2120
2121    let entries = match fs::read_dir(current) {
2122        Ok(e) => e,
2123        Err(_) => return,
2124    };
2125
2126    for entry in entries.flatten() {
2127        let path = entry.path();
2128        let name = entry.file_name();
2129        let name_str = name.to_string_lossy();
2130
2131        if name_str.starts_with('.') || name_str == "node_modules" {
2132            continue;
2133        }
2134
2135        if path.is_dir() {
2136            let skill_file = path.join("SKILL.md");
2137            if skill_file.exists() {
2138                let rel = path.strip_prefix(base).unwrap_or(&path);
2139                results.push(DiscoveredResource {
2140                    kind: ResourceKind::Skill,
2141                    path: skill_file,
2142                    relative_path: rel.join("SKILL.md").to_string_lossy().to_string(),
2143                });
2144            }
2145            discover_skills_recursive(base, &path, results);
2146        }
2147    }
2148}
2149
2150/// Discover prompt template files (.md in prompts/ subdirectory)
2151fn discover_prompts(dir: &Path) -> Vec<DiscoveredResource> {
2152    let prompts_dir = dir.join("prompts");
2153    discover_files_by_ext(
2154        if prompts_dir.exists() {
2155            &prompts_dir
2156        } else {
2157            dir
2158        },
2159        "md",
2160        ResourceKind::Prompt,
2161    )
2162}
2163
2164/// Discover theme files (.json in themes/ subdirectory)
2165fn discover_themes(dir: &Path) -> Vec<DiscoveredResource> {
2166    let themes_dir = dir.join("themes");
2167    discover_files_by_ext(
2168        if themes_dir.exists() {
2169            &themes_dir
2170        } else {
2171            dir
2172        },
2173        "json",
2174        ResourceKind::Theme,
2175    )
2176}
2177
2178/// Recursively find files with a given extension
2179fn discover_files_by_ext(dir: &Path, ext: &str, kind: ResourceKind) -> Vec<DiscoveredResource> {
2180    let mut results = Vec::new();
2181    discover_files_recursive(dir, dir, ext, kind, &mut results);
2182    results
2183}
2184
2185fn discover_files_recursive(
2186    base: &Path,
2187    current: &Path,
2188    ext: &str,
2189    kind: ResourceKind,
2190    results: &mut Vec<DiscoveredResource>,
2191) {
2192    if !current.exists() {
2193        return;
2194    }
2195
2196    let entries = match fs::read_dir(current) {
2197        Ok(e) => e,
2198        Err(_) => return,
2199    };
2200
2201    for entry in entries.flatten() {
2202        let path = entry.path();
2203        let name = entry.file_name();
2204        let name_str = name.to_string_lossy();
2205
2206        if name_str.starts_with('.') || name_str == "node_modules" {
2207            continue;
2208        }
2209
2210        if path.is_dir() {
2211            discover_files_recursive(base, &path, ext, kind, results);
2212        } else if path.extension().and_then(|e| e.to_str()) == Some(ext) {
2213            let rel = path.strip_prefix(base).unwrap_or(&path);
2214            results.push(DiscoveredResource {
2215                kind,
2216                path: path.clone(),
2217                relative_path: rel.to_string_lossy().to_string(),
2218            });
2219        }
2220    }
2221}
2222
2223// ── Utility functions ─────────────────────────────────────────────────
2224
2225/// Recursively copy a directory
2226fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
2227    if !dst.exists() {
2228        fs::create_dir_all(dst)?;
2229    }
2230
2231    for entry in fs::read_dir(src)? {
2232        let entry = entry?;
2233        let src_path = entry.path();
2234        let dst_path = dst.join(entry.file_name());
2235
2236        if src_path.is_dir() {
2237            copy_dir_recursive(&src_path, &dst_path)?;
2238        } else {
2239            fs::copy(&src_path, &dst_path)?;
2240        }
2241    }
2242
2243    Ok(())
2244}
2245
2246/// Compute a SHA-256 hash of a directory's contents for integrity checking
2247fn compute_dir_hash(dir: &Path) -> Option<String> {
2248    let mut hasher = Sha256::new();
2249    let mut files = collect_file_paths(dir);
2250    files.sort();
2251
2252    for file_path in &files {
2253        if let Ok(content) = fs::read(file_path) {
2254            hasher.update(&content);
2255        }
2256    }
2257
2258    let result = hasher.finalize();
2259    Some(format!("sha256-{:x}", result))
2260}
2261
2262/// Collect all file paths in a directory recursively
2263fn collect_file_paths(dir: &Path) -> Vec<PathBuf> {
2264    let mut paths = Vec::new();
2265    if !dir.exists() {
2266        return paths;
2267    }
2268
2269    let entries = match fs::read_dir(dir) {
2270        Ok(e) => e,
2271        Err(_) => return paths,
2272    };
2273
2274    for entry in entries.flatten() {
2275        let path = entry.path();
2276        if path.is_dir() {
2277            paths.extend(collect_file_paths(&path));
2278        } else {
2279            paths.push(path);
2280        }
2281    }
2282
2283    paths
2284}
2285
2286/// Find the single subdirectory inside an extracted archive
2287fn find_single_subdir(dir: &Path) -> Option<PathBuf> {
2288    let entries: Vec<_> = fs::read_dir(dir).ok()?.filter_map(|e| e.ok()).collect();
2289    if entries.len() == 1 && entries[0].path().is_dir() {
2290        Some(entries[0].path())
2291    } else {
2292        None
2293    }
2294}
2295
2296/// Remove empty parent directories up to a root
2297fn prune_empty_parents(target: &Path, root: &Path) {
2298    let mut current = target.parent();
2299    while let Some(dir) = current {
2300        if dir == root || !dir.starts_with(root) {
2301            break;
2302        }
2303        if dir.exists() {
2304            let is_empty = fs::read_dir(dir)
2305                .map(|mut rd| rd.next().is_none())
2306                .unwrap_or(false);
2307            if is_empty {
2308                let _ = fs::remove_dir(dir);
2309            } else {
2310                break;
2311            }
2312        }
2313        current = dir.parent();
2314    }
2315}
2316
2317#[cfg(test)]
2318mod tests {
2319    use super::*;
2320
2321    fn setup_temp_packages_dir() -> (tempfile::TempDir, PathBuf) {
2322        let tmp = tempfile::tempdir().unwrap();
2323        let packages_dir = tmp.path().join("packages");
2324        fs::create_dir_all(&packages_dir).unwrap();
2325        (tmp, packages_dir)
2326    }
2327
2328    fn create_test_package(base: &Path, name: &str, version: &str) -> PathBuf {
2329        let pkg_dir = base.join("source-pkg");
2330        fs::create_dir_all(&pkg_dir).unwrap();
2331
2332        let manifest = PackageManifest {
2333            name: name.to_string(),
2334            version: version.to_string(),
2335            extensions: vec!["ext1.so".to_string()],
2336            skills: vec!["skill-a".to_string()],
2337            prompts: vec![],
2338            themes: vec![],
2339            description: None,
2340            dependencies: BTreeMap::new(),
2341        };
2342
2343        let toml_content = toml::to_string_pretty(&manifest).unwrap();
2344        fs::write(pkg_dir.join(MANIFEST_NAME), toml_content).unwrap();
2345        fs::write(pkg_dir.join("ext1.so"), "fake extension").unwrap();
2346        fs::create_dir_all(pkg_dir.join("skill-a")).unwrap();
2347        fs::write(pkg_dir.join("skill-a").join("SKILL.md"), "# Skill A").unwrap();
2348
2349        pkg_dir
2350    }
2351
2352    fn create_test_package_with_auto_discovery(base: &Path, name: &str, version: &str) -> PathBuf {
2353        let pkg_dir = base.join("source-pkg-auto");
2354        fs::create_dir_all(&pkg_dir).unwrap();
2355
2356        let manifest = PackageManifest {
2357            name: name.to_string(),
2358            version: version.to_string(),
2359            extensions: vec![],
2360            skills: vec![],
2361            prompts: vec![],
2362            themes: vec![],
2363            description: None,
2364            dependencies: BTreeMap::new(),
2365        };
2366        let toml_content = toml::to_string_pretty(&manifest).unwrap();
2367        fs::write(pkg_dir.join(MANIFEST_NAME), toml_content).unwrap();
2368
2369        fs::write(pkg_dir.join("myext.so"), "extension").unwrap();
2370        fs::create_dir_all(pkg_dir.join("my-skill")).unwrap();
2371        fs::write(pkg_dir.join("my-skill").join("SKILL.md"), "# My Skill").unwrap();
2372        fs::create_dir_all(pkg_dir.join("prompts")).unwrap();
2373        fs::write(pkg_dir.join("prompts").join("review.md"), "# Review").unwrap();
2374        fs::create_dir_all(pkg_dir.join("themes")).unwrap();
2375        fs::write(pkg_dir.join("themes").join("dark.json"), "{}").unwrap();
2376
2377        pkg_dir
2378    }
2379
2380    #[test]
2381    fn test_install_and_list() {
2382        let (tmp, packages_dir) = setup_temp_packages_dir();
2383
2384        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2385        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2386
2387        let manifest = mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2388        assert_eq!(manifest.name, "test-pkg");
2389        assert_eq!(manifest.version, "1.0.0");
2390
2391        let installed = mgr.list();
2392        assert_eq!(installed.len(), 1);
2393        assert_eq!(installed[0].name, "test-pkg");
2394    }
2395
2396    #[test]
2397    fn test_uninstall() {
2398        let (tmp, packages_dir) = setup_temp_packages_dir();
2399
2400        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2401        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2402
2403        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2404        assert!(mgr.is_installed("test-pkg"));
2405
2406        mgr.uninstall("test-pkg").unwrap();
2407        assert!(!mgr.is_installed("test-pkg"));
2408        assert!(mgr.list().is_empty());
2409    }
2410
2411    #[test]
2412    fn test_uninstall_not_installed() {
2413        let (_tmp, packages_dir) = setup_temp_packages_dir();
2414        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2415
2416        let result = mgr.uninstall("nonexistent");
2417        assert!(result.is_err());
2418    }
2419
2420    #[test]
2421    fn test_install_scoped_package() {
2422        let (tmp, packages_dir) = setup_temp_packages_dir();
2423
2424        let pkg_dir = create_test_package(tmp.path(), "@foo/oxi-tools", "2.0.0");
2425        let mut mgr = PackageManager::with_dir(packages_dir.clone()).unwrap();
2426
2427        let manifest = mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2428        assert_eq!(manifest.name, "@foo/oxi-tools");
2429
2430        let expected_dir = packages_dir.join("foo-oxi-tools");
2431        assert!(expected_dir.exists());
2432    }
2433
2434    #[test]
2435    fn test_reinstall_overwrites() {
2436        let (tmp, packages_dir) = setup_temp_packages_dir();
2437
2438        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2439        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2440
2441        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2442
2443        let pkg_dir_v2 = tmp.path().join("source-pkg-v2");
2444        fs::create_dir_all(&pkg_dir_v2).unwrap();
2445        let manifest_v2 = PackageManifest {
2446            name: "test-pkg".to_string(),
2447            version: "2.0.0".to_string(),
2448            extensions: vec![],
2449            skills: vec![],
2450            prompts: vec![],
2451            themes: vec![],
2452            description: None,
2453            dependencies: BTreeMap::new(),
2454        };
2455        fs::write(
2456            pkg_dir_v2.join(MANIFEST_NAME),
2457            toml::to_string_pretty(&manifest_v2).unwrap(),
2458        )
2459        .unwrap();
2460
2461        mgr.install(pkg_dir_v2.to_str().unwrap()).unwrap();
2462
2463        let installed = mgr.list();
2464        assert_eq!(installed.len(), 1);
2465        assert_eq!(installed[0].version, "2.0.0");
2466    }
2467
2468    #[test]
2469    fn test_empty_packages_dir() {
2470        let (_tmp, packages_dir) = setup_temp_packages_dir();
2471        let mgr = PackageManager::with_dir(packages_dir).unwrap();
2472        assert!(mgr.list().is_empty());
2473    }
2474
2475    #[test]
2476    fn test_packages_dir_not_exists() {
2477        let tmp = tempfile::tempdir().unwrap();
2478        let nonexistent = tmp.path().join("does-not-exist");
2479        let mgr = PackageManager::with_dir(nonexistent).unwrap();
2480        assert!(mgr.list().is_empty());
2481    }
2482
2483    #[test]
2484    fn test_discover_resources_explicit() {
2485        let (tmp, packages_dir) = setup_temp_packages_dir();
2486
2487        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2488        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2489        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2490
2491        let resources = mgr.discover_resources("test-pkg").unwrap();
2492        assert_eq!(resources.len(), 2);
2493
2494        let extensions: Vec<_> = resources
2495            .iter()
2496            .filter(|r| r.kind == ResourceKind::Extension)
2497            .collect();
2498        let skills: Vec<_> = resources
2499            .iter()
2500            .filter(|r| r.kind == ResourceKind::Skill)
2501            .collect();
2502        assert_eq!(extensions.len(), 1);
2503        assert_eq!(skills.len(), 1);
2504    }
2505
2506    #[test]
2507    fn test_discover_resources_auto() {
2508        let (tmp, packages_dir) = setup_temp_packages_dir();
2509
2510        let pkg_dir = create_test_package_with_auto_discovery(tmp.path(), "auto-pkg", "1.0.0");
2511        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2512        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2513
2514        let resources = mgr.discover_resources("auto-pkg").unwrap();
2515
2516        let ext_count = resources
2517            .iter()
2518            .filter(|r| r.kind == ResourceKind::Extension)
2519            .count();
2520        let skill_count = resources
2521            .iter()
2522            .filter(|r| r.kind == ResourceKind::Skill)
2523            .count();
2524        let prompt_count = resources
2525            .iter()
2526            .filter(|r| r.kind == ResourceKind::Prompt)
2527            .count();
2528        let theme_count = resources
2529            .iter()
2530            .filter(|r| r.kind == ResourceKind::Theme)
2531            .count();
2532
2533        assert!(
2534            ext_count >= 1,
2535            "Expected at least 1 extension, got {}",
2536            ext_count
2537        );
2538        assert!(
2539            skill_count >= 1,
2540            "Expected at least 1 skill, got {}",
2541            skill_count
2542        );
2543        assert!(
2544            prompt_count >= 1,
2545            "Expected at least 1 prompt, got {}",
2546            prompt_count
2547        );
2548        assert!(
2549            theme_count >= 1,
2550            "Expected at least 1 theme, got {}",
2551            theme_count
2552        );
2553    }
2554
2555    #[test]
2556    fn test_resource_counts() {
2557        let (tmp, packages_dir) = setup_temp_packages_dir();
2558
2559        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2560        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2561        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2562
2563        let counts = mgr.resource_counts("test-pkg").unwrap();
2564        assert_eq!(counts.extensions, 1);
2565        assert_eq!(counts.skills, 1);
2566        assert_eq!(counts.prompts, 0);
2567        assert_eq!(counts.themes, 0);
2568    }
2569
2570    #[test]
2571    fn test_resource_counts_display() {
2572        let counts = ResourceCounts {
2573            extensions: 2,
2574            skills: 1,
2575            prompts: 0,
2576            themes: 3,
2577        };
2578        assert_eq!(counts.to_string(), "2 ext, 1 skill, 3 theme");
2579
2580        let empty = ResourceCounts::default();
2581        assert_eq!(empty.to_string(), "-");
2582    }
2583
2584    #[test]
2585    fn test_resource_kind_display() {
2586        assert_eq!(ResourceKind::Extension.to_string(), "extension");
2587        assert_eq!(ResourceKind::Skill.to_string(), "skill");
2588        assert_eq!(ResourceKind::Prompt.to_string(), "prompt");
2589        assert_eq!(ResourceKind::Theme.to_string(), "theme");
2590    }
2591
2592    #[test]
2593    fn test_get_install_dir() {
2594        let (tmp, packages_dir) = setup_temp_packages_dir();
2595
2596        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2597        let mut mgr = PackageManager::with_dir(packages_dir.clone()).unwrap();
2598        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2599
2600        let dir = mgr.get_install_dir("test-pkg").unwrap();
2601        assert!(dir.exists());
2602        assert!(dir.join(MANIFEST_NAME).exists());
2603
2604        assert!(mgr.get_install_dir("nonexistent").is_none());
2605    }
2606
2607    #[test]
2608    fn test_discover_resources_not_installed() {
2609        let (_tmp, packages_dir) = setup_temp_packages_dir();
2610        let mgr = PackageManager::with_dir(packages_dir).unwrap();
2611
2612        let result = mgr.discover_resources("nonexistent");
2613        assert!(result.is_err());
2614    }
2615
2616    #[test]
2617    fn test_update_not_installed() {
2618        let (_tmp, packages_dir) = setup_temp_packages_dir();
2619        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2620
2621        let result = mgr.update("nonexistent");
2622        assert!(result.is_err());
2623    }
2624
2625    // ── Source parsing tests ──────────────────────────────────────────
2626
2627    #[test]
2628    fn test_parse_npm_source() {
2629        let parsed = ParsedSource::parse("npm:express@4.18.0");
2630        match parsed {
2631            ParsedSource::Npm { spec, name, pinned } => {
2632                assert_eq!(spec, "express@4.18.0");
2633                assert_eq!(name, "express");
2634                assert!(pinned);
2635            }
2636            _ => panic!("Expected Npm source"),
2637        }
2638
2639        let parsed = ParsedSource::parse("npm:lodash");
2640        match parsed {
2641            ParsedSource::Npm { name, pinned, .. } => {
2642                assert_eq!(name, "lodash");
2643                assert!(!pinned);
2644            }
2645            _ => panic!("Expected Npm source"),
2646        }
2647    }
2648
2649    #[test]
2650    fn test_parse_git_source() {
2651        let parsed = ParsedSource::parse("https://github.com/org/repo.git");
2652        match parsed {
2653            ParsedSource::Git {
2654                host, path, ref_, ..
2655            } => {
2656                assert_eq!(host, "github.com");
2657                assert_eq!(path, "org/repo");
2658                assert!(ref_.is_none());
2659            }
2660            _ => panic!("Expected Git source"),
2661        }
2662
2663        let parsed = ParsedSource::parse("https://github.com/org/repo.git@v1.0.0");
2664        match parsed {
2665            ParsedSource::Git { path, ref_, .. } => {
2666                assert_eq!(path, "org/repo");
2667                assert_eq!(ref_.as_deref(), Some("v1.0.0"));
2668            }
2669            _ => panic!("Expected Git source"),
2670        }
2671    }
2672
2673    #[test]
2674    fn test_parse_github_shorthand() {
2675        let parsed = ParsedSource::parse("github:org/repo@main");
2676        match parsed {
2677            ParsedSource::Git {
2678                host, path, ref_, ..
2679            } => {
2680                assert_eq!(host, "github.com");
2681                assert_eq!(path, "org/repo");
2682                assert_eq!(ref_.as_deref(), Some("main"));
2683            }
2684            _ => panic!("Expected Git source"),
2685        }
2686    }
2687
2688    #[test]
2689    fn test_parse_local_source() {
2690        let parsed = ParsedSource::parse("/path/to/package");
2691        match parsed {
2692            ParsedSource::Local { path } => {
2693                assert_eq!(path, "/path/to/package");
2694            }
2695            _ => panic!("Expected Local source"),
2696        }
2697
2698        let parsed = ParsedSource::parse("./relative/path");
2699        match parsed {
2700            ParsedSource::Local { path } => {
2701                assert_eq!(path, "./relative/path");
2702            }
2703            _ => panic!("Expected Local source"),
2704        }
2705    }
2706
2707    #[test]
2708    fn test_parse_url_source() {
2709        let parsed = ParsedSource::parse("https://example.com/pkg.tar.gz");
2710        match parsed {
2711            ParsedSource::Url { url } => {
2712                assert_eq!(url, "https://example.com/pkg.tar.gz");
2713            }
2714            _ => panic!("Expected Url source"),
2715        }
2716    }
2717
2718    #[test]
2719    fn test_source_identity() {
2720        let npm = ParsedSource::parse("npm:express@4.18.0");
2721        assert_eq!(npm.identity(), "npm:express");
2722
2723        let git = ParsedSource::parse("https://github.com/org/repo.git");
2724        assert_eq!(git.identity(), "git:github.com/org/repo");
2725
2726        let local = ParsedSource::parse("/path/to/pkg");
2727        assert_eq!(local.identity(), "local:/path/to/pkg");
2728    }
2729
2730    #[test]
2731    fn test_parse_npm_spec() {
2732        let (name, pinned) = parse_npm_spec("express@4.18.0");
2733        assert_eq!(name, "express");
2734        assert!(pinned);
2735
2736        let (name, pinned) = parse_npm_spec("express");
2737        assert_eq!(name, "express");
2738        assert!(!pinned);
2739
2740        let (name, pinned) = parse_npm_spec("@scope/pkg@1.0.0");
2741        assert_eq!(name, "@scope/pkg");
2742        assert!(pinned);
2743    }
2744
2745    // ── Lockfile tests ────────────────────────────────────────────────
2746
2747    #[test]
2748    fn test_lockfile_roundtrip() {
2749        let (tmp, _) = setup_temp_packages_dir();
2750        let lock_path = tmp.path().join(LOCKFILE_NAME);
2751
2752        let mut lock = Lockfile::new();
2753        lock.insert(LockEntry {
2754            source: "npm:express@4.18.0".to_string(),
2755            name: "express".to_string(),
2756            version: "4.18.0".to_string(),
2757            integrity: Some("sha256-abc123".to_string()),
2758            scope: SourceScope::User,
2759            source_type: "npm".to_string(),
2760            dependencies: BTreeMap::new(),
2761        });
2762
2763        lock.write(&lock_path).unwrap();
2764
2765        let loaded = Lockfile::read(&lock_path).unwrap().unwrap();
2766        assert_eq!(loaded.packages.len(), 1);
2767        assert_eq!(loaded.packages["express"].version, "4.18.0");
2768        assert_eq!(
2769            loaded.packages["express"].integrity.as_deref(),
2770            Some("sha256-abc123")
2771        );
2772    }
2773
2774    #[test]
2775    fn test_lockfile_install_roundtrip() {
2776        let (tmp, packages_dir) = setup_temp_packages_dir();
2777        let pkg_dir = create_test_package(tmp.path(), "locked-pkg", "1.0.0");
2778
2779        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2780        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2781
2782        // Lockfile should have been written
2783        let lock_path = mgr.packages_dir().join(LOCKFILE_NAME);
2784        assert!(lock_path.exists());
2785
2786        let lock = Lockfile::read(&lock_path).unwrap().unwrap();
2787        assert!(lock.contains("locked-pkg"));
2788        let entry = lock.get("locked-pkg").unwrap();
2789        assert_eq!(entry.version, "1.0.0");
2790    }
2791
2792    // ── Validation tests ──────────────────────────────────────────────
2793
2794    #[test]
2795    fn test_validate_valid_package() {
2796        let (tmp, _) = setup_temp_packages_dir();
2797        let pkg_dir = create_test_package(tmp.path(), "valid-pkg", "1.0.0");
2798        let warnings = PackageManager::validate_package(&pkg_dir).unwrap();
2799        // Should have minimal warnings (maybe just about .gitignore)
2800        assert!(
2801            warnings.len() <= 1,
2802            "Expected <= 1 warning, got {:?}",
2803            warnings
2804        );
2805    }
2806
2807    #[test]
2808    fn test_validate_empty_dir() {
2809        let tmp = tempfile::tempdir().unwrap();
2810        let empty_dir = tmp.path().join("empty-pkg");
2811        fs::create_dir_all(&empty_dir).unwrap();
2812        let warnings = PackageManager::validate_package(&empty_dir).unwrap();
2813        assert!(!warnings.is_empty());
2814    }
2815
2816    // ── Dependency tests ──────────────────────────────────────────────
2817
2818    #[test]
2819    fn test_resolve_dependencies() {
2820        let (tmp, packages_dir) = setup_temp_packages_dir();
2821
2822        // Create a package with dependencies
2823        let pkg_dir = tmp.path().join("dep-pkg");
2824        fs::create_dir_all(&pkg_dir).unwrap();
2825        let mut deps = BTreeMap::new();
2826        deps.insert("lodash".to_string(), "^4.0.0".to_string());
2827        deps.insert("nonexistent-pkg".to_string(), "^1.0.0".to_string());
2828
2829        let manifest = PackageManifest {
2830            name: "dep-pkg".to_string(),
2831            version: "1.0.0".to_string(),
2832            extensions: vec![],
2833            skills: vec![],
2834            prompts: vec![],
2835            themes: vec![],
2836            description: None,
2837            dependencies: deps,
2838        };
2839        fs::write(
2840            pkg_dir.join(MANIFEST_NAME),
2841            toml::to_string_pretty(&manifest).unwrap(),
2842        )
2843        .unwrap();
2844
2845        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2846        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2847
2848        let missing = mgr.resolve_dependencies();
2849        assert_eq!(missing.len(), 1);
2850        assert_eq!(missing[0].0, "dep-pkg");
2851        assert!(
2852            missing[0].1.contains(&"lodash".to_string())
2853                || missing[0].1.contains(&"nonexistent-pkg".to_string())
2854        );
2855    }
2856
2857    // ── Version tests ─────────────────────────────────────────────────
2858
2859    #[test]
2860    fn test_version_satisfies() {
2861        let (tmp, packages_dir) = setup_temp_packages_dir();
2862        let pkg_dir = create_test_package(tmp.path(), "ver-pkg", "1.2.3");
2863        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2864        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2865
2866        assert!(mgr.version_satisfies("ver-pkg", "^1.0.0"));
2867        assert!(mgr.version_satisfies("ver-pkg", ">=1.0.0"));
2868        assert!(!mgr.version_satisfies("ver-pkg", "^2.0.0"));
2869        assert!(!mgr.version_satisfies("ver-pkg", "<1.0.0"));
2870    }
2871
2872    #[test]
2873    fn test_get_installed_version() {
2874        let (tmp, packages_dir) = setup_temp_packages_dir();
2875        let pkg_dir = create_test_package(tmp.path(), "ver-pkg", "3.1.4");
2876        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2877        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2878
2879        assert_eq!(mgr.get_installed_version("ver-pkg"), Some("3.1.4"));
2880        assert_eq!(mgr.get_installed_version("nonexistent"), None);
2881    }
2882
2883    // ── Resolve tests ─────────────────────────────────────────────────
2884
2885    #[test]
2886    fn test_resolve() {
2887        let (tmp, packages_dir) = setup_temp_packages_dir();
2888        let pkg_dir = create_test_package(tmp.path(), "resolve-pkg", "1.0.0");
2889        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2890        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2891
2892        let resolved = mgr.resolve();
2893        assert!(!resolved.extensions.is_empty() || !resolved.skills.is_empty());
2894    }
2895
2896    // ── Progress callback tests ───────────────────────────────────────
2897
2898    #[test]
2899    fn test_progress_callback() {
2900        use std::sync::{Arc, Mutex};
2901
2902        let events: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
2903        let events_clone = events.clone();
2904
2905        let (tmp, packages_dir) = setup_temp_packages_dir();
2906        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2907
2908        mgr.set_progress_callback(Box::new(move |event| {
2909            let mut e = events_clone.lock().unwrap();
2910            e.push(format!("{:?}:{:?}", event.event_type, event.action));
2911        }));
2912
2913        let pkg_dir = create_test_package(tmp.path(), "progress-pkg", "1.0.0");
2914        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2915
2916        // install_local doesn't use with_progress, so no events expected from install()
2917        // Just verify the progress event mechanism exists and doesn't panic
2918        let _event_count = events.lock().unwrap().len();
2919    }
2920
2921    #[test]
2922    fn test_list_configured() {
2923        let (tmp, packages_dir) = setup_temp_packages_dir();
2924        let pkg_dir = create_test_package(tmp.path(), "cfg-pkg", "1.0.0");
2925        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2926        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2927
2928        let configured = mgr.list_configured();
2929        assert_eq!(configured.len(), 1);
2930        assert!(configured[0].source.contains("source-pkg"));
2931        // source comes from lockfile, might be the local path
2932    }
2933}