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::{bail, Context, Result};
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                    if req.matches(&ver) {
545                        match &best {
546                            Some(b) if ver > *b => best = Some(ver),
547                            None => best = Some(ver),
548                            _ => {}
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                                if latest != lock_entry.version {
1682                                    updates.push(PackageUpdateInfo {
1683                                        source: lock_entry.source.clone(),
1684                                        display_name: pkg_name.clone(),
1685                                        source_type: "npm".to_string(),
1686                                        scope: lock_entry.scope,
1687                                    });
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() {
1751            Some(dir)
1752        } else {
1753            None
1754        }
1755    }
1756
1757    /// Get the installed path for a source at a given scope
1758    pub fn get_installed_path_for_source(
1759        &self,
1760        source: &str,
1761        scope: SourceScope,
1762    ) -> Option<PathBuf> {
1763        let parsed = ParsedSource::parse(source);
1764        match &parsed {
1765            ParsedSource::Npm { name, .. } => {
1766                let path = self.npm_install_path(name, scope);
1767                if path.exists() {
1768                    Some(path)
1769                } else {
1770                    None
1771                }
1772            }
1773            ParsedSource::Git { host, path, .. } => {
1774                let path = self.git_install_path(host, path, scope);
1775                if path.exists() {
1776                    Some(path)
1777                } else {
1778                    None
1779                }
1780            }
1781            ParsedSource::Local { path } => {
1782                let p = PathBuf::from(path);
1783                if p.exists() {
1784                    Some(p)
1785                } else {
1786                    None
1787                }
1788            }
1789            ParsedSource::Url { .. } => None,
1790        }
1791    }
1792
1793    // ── Resource discovery ────────────────────────────────────────────
1794
1795    /// Discover all resources from an installed package.
1796    pub fn discover_resources(&self, name: &str) -> Result<Vec<DiscoveredResource>> {
1797        let manifest = self
1798            .installed
1799            .get(name)
1800            .with_context(|| format!("Package '{}' not found", name))?;
1801
1802        let install_dir = self.pkg_install_dir(name);
1803        if !install_dir.exists() {
1804            bail!("Install directory for '{}' does not exist", name);
1805        }
1806
1807        let mut resources = Vec::new();
1808
1809        let has_explicit = !manifest.extensions.is_empty()
1810            || !manifest.skills.is_empty()
1811            || !manifest.prompts.is_empty()
1812            || !manifest.themes.is_empty();
1813
1814        if has_explicit {
1815            for ext in &manifest.extensions {
1816                let path = install_dir.join(ext);
1817                if path.exists() {
1818                    resources.push(DiscoveredResource {
1819                        kind: ResourceKind::Extension,
1820                        path,
1821                        relative_path: ext.clone(),
1822                    });
1823                }
1824            }
1825            for skill in &manifest.skills {
1826                let path = install_dir.join(skill);
1827                if path.exists() {
1828                    resources.push(DiscoveredResource {
1829                        kind: ResourceKind::Skill,
1830                        path,
1831                        relative_path: skill.clone(),
1832                    });
1833                }
1834            }
1835            for prompt in &manifest.prompts {
1836                let path = install_dir.join(prompt);
1837                if path.exists() {
1838                    resources.push(DiscoveredResource {
1839                        kind: ResourceKind::Prompt,
1840                        path,
1841                        relative_path: prompt.clone(),
1842                    });
1843                }
1844            }
1845            for theme in &manifest.themes {
1846                let path = install_dir.join(theme);
1847                if path.exists() {
1848                    resources.push(DiscoveredResource {
1849                        kind: ResourceKind::Theme,
1850                        path,
1851                        relative_path: theme.clone(),
1852                    });
1853                }
1854            }
1855        } else {
1856            resources.extend(discover_extensions(&install_dir));
1857            resources.extend(discover_skills(&install_dir));
1858            resources.extend(discover_prompts(&install_dir));
1859            resources.extend(discover_themes(&install_dir));
1860        }
1861
1862        Ok(resources)
1863    }
1864
1865    /// Get resource counts for a package
1866    pub fn resource_counts(&self, name: &str) -> Result<ResourceCounts> {
1867        let resources = self.discover_resources(name)?;
1868        let mut counts = ResourceCounts::default();
1869        for r in &resources {
1870            match r.kind {
1871                ResourceKind::Extension => counts.extensions += 1,
1872                ResourceKind::Skill => counts.skills += 1,
1873                ResourceKind::Prompt => counts.prompts += 1,
1874                ResourceKind::Theme => counts.themes += 1,
1875            }
1876        }
1877        Ok(counts)
1878    }
1879
1880    /// Resolve all resources from all installed packages, producing ResolvedPaths
1881    pub fn resolve(&self) -> ResolvedPaths {
1882        let mut extensions = Vec::new();
1883        let mut skills = Vec::new();
1884        let mut prompts = Vec::new();
1885        let mut themes = Vec::new();
1886
1887        for name in self.installed.keys() {
1888            let install_dir = self.pkg_install_dir(name);
1889            if !install_dir.exists() {
1890                continue;
1891            }
1892
1893            let metadata = PathMetadata {
1894                source: name.clone(),
1895                scope: SourceScope::User,
1896                origin: ResourceOrigin::Package,
1897                base_dir: Some(install_dir.clone()),
1898            };
1899
1900            // Use discover_resources logic
1901            if let Ok(resources) = self.discover_resources(name) {
1902                for r in resources {
1903                    match r.kind {
1904                        ResourceKind::Extension => extensions.push(ResolvedResource {
1905                            path: r.path,
1906                            enabled: true,
1907                            metadata: metadata.clone(),
1908                        }),
1909                        ResourceKind::Skill => skills.push(ResolvedResource {
1910                            path: r.path,
1911                            enabled: true,
1912                            metadata: metadata.clone(),
1913                        }),
1914                        ResourceKind::Prompt => prompts.push(ResolvedResource {
1915                            path: r.path,
1916                            enabled: true,
1917                            metadata: metadata.clone(),
1918                        }),
1919                        ResourceKind::Theme => themes.push(ResolvedResource {
1920                            path: r.path,
1921                            enabled: true,
1922                            metadata: metadata.clone(),
1923                        }),
1924                    }
1925                }
1926            }
1927        }
1928
1929        ResolvedPaths {
1930            extensions,
1931            skills,
1932            prompts,
1933            themes,
1934        }
1935    }
1936
1937    // ── Dependency resolution ─────────────────────────────────────────
1938
1939    /// Resolve dependencies for all installed packages.
1940    /// Returns a list of (package, missing_dependencies) tuples.
1941    pub fn resolve_dependencies(&self) -> Vec<(String, Vec<String>)> {
1942        let mut result = Vec::new();
1943        let installed_names: HashSet<&str> = self.installed.keys().map(|s| s.as_str()).collect();
1944
1945        for (name, manifest) in &self.installed {
1946            let missing: Vec<String> = manifest
1947                .dependencies
1948                .keys()
1949                .filter(|dep| !installed_names.contains(dep.as_str()))
1950                .cloned()
1951                .collect();
1952
1953            if !missing.is_empty() {
1954                result.push((name.clone(), missing));
1955            }
1956        }
1957
1958        result
1959    }
1960
1961    /// Validate a package structure
1962    pub fn validate_package(dir: &Path) -> Result<Vec<String>> {
1963        let mut warnings = Vec::new();
1964
1965        // Check for manifest
1966        if !dir.join(MANIFEST_NAME).exists() && !dir.join(NPM_MANIFEST_NAME).exists() {
1967            warnings.push(format!(
1968                "No {} or {} found",
1969                MANIFEST_NAME, NPM_MANIFEST_NAME
1970            ));
1971        }
1972
1973        // Try to parse manifest
1974        if dir.join(MANIFEST_NAME).exists() {
1975            match Self::read_manifest(&dir.join(MANIFEST_NAME)) {
1976                Ok(m) => {
1977                    if m.name.is_empty() {
1978                        warnings.push("Package name is empty".to_string());
1979                    }
1980                    if m.version.is_empty() {
1981                        warnings.push("Package version is empty".to_string());
1982                    }
1983                    if semver::Version::parse(&m.version).is_err() {
1984                        warnings.push(format!("Version '{}' is not valid semver", m.version));
1985                    }
1986                    let has_resources = !m.extensions.is_empty()
1987                        || !m.skills.is_empty()
1988                        || !m.prompts.is_empty()
1989                        || !m.themes.is_empty();
1990                    if !has_resources {
1991                        // Check if auto-discovery would find anything
1992                        let discovered = discover_extensions(dir)
1993                            .into_iter()
1994                            .chain(discover_skills(dir))
1995                            .chain(discover_prompts(dir))
1996                            .chain(discover_themes(dir))
1997                            .count();
1998                        if discovered == 0 {
1999                            warnings.push(
2000                                "Package has no explicit resources and auto-discovery found nothing"
2001                                    .to_string(),
2002                            );
2003                        }
2004                    }
2005
2006                    // Check that explicit paths exist
2007                    for ext in &m.extensions {
2008                        if !dir.join(ext).exists() {
2009                            warnings.push(format!("Extension path '{}' does not exist", ext));
2010                        }
2011                    }
2012                    for skill in &m.skills {
2013                        if !dir.join(skill).exists() {
2014                            warnings.push(format!("Skill path '{}' does not exist", skill));
2015                        }
2016                    }
2017                    for prompt in &m.prompts {
2018                        if !dir.join(prompt).exists() {
2019                            warnings.push(format!("Prompt path '{}' does not exist", prompt));
2020                        }
2021                    }
2022                    for theme in &m.themes {
2023                        if !dir.join(theme).exists() {
2024                            warnings.push(format!("Theme path '{}' does not exist", theme));
2025                        }
2026                    }
2027                }
2028                Err(e) => {
2029                    warnings.push(format!("Failed to parse {}: {}", MANIFEST_NAME, e));
2030                }
2031            }
2032        }
2033
2034        // Check for .gitignore or .ignore
2035        if !dir.join(".gitignore").exists() && !dir.join(".ignore").exists() {
2036            warnings.push("No .gitignore or .ignore file found".to_string());
2037        }
2038
2039        Ok(warnings)
2040    }
2041
2042    // ── Version queries ───────────────────────────────────────────────
2043
2044    /// Get installed version of a package
2045    pub fn get_installed_version(&self, name: &str) -> Option<&str> {
2046        self.installed.get(name).map(|m| m.version.as_str())
2047    }
2048
2049    /// Check if an installed version satisfies a semver requirement
2050    pub fn version_satisfies(&self, name: &str, requirement: &str) -> bool {
2051        if let Some(version) = self.get_installed_version(name) {
2052            if let Ok(v) = semver::Version::parse(version) {
2053                if let Ok(req) = semver::VersionReq::parse(requirement) {
2054                    return req.matches(&v);
2055                }
2056            }
2057        }
2058        false
2059    }
2060
2061    /// Get the lockfile
2062    pub fn lockfile(&self) -> &Lockfile {
2063        &self.lockfile
2064    }
2065}
2066
2067// ── Auto-discovery helpers ────────────────────────────────────────────
2068
2069/// Discover extension files in a directory.
2070fn discover_extensions(dir: &Path) -> Vec<DiscoveredResource> {
2071    let mut results = Vec::new();
2072    discover_extensions_recursive(dir, dir, &mut results);
2073    results
2074}
2075
2076fn discover_extensions_recursive(
2077    base: &Path,
2078    current: &Path,
2079    results: &mut Vec<DiscoveredResource>,
2080) {
2081    if !current.exists() {
2082        return;
2083    }
2084
2085    let entries = match fs::read_dir(current) {
2086        Ok(e) => e,
2087        Err(_) => return,
2088    };
2089
2090    for entry in entries.flatten() {
2091        let path = entry.path();
2092        let name = entry.file_name();
2093        let name_str = name.to_string_lossy();
2094
2095        if name_str.starts_with('.') || name_str == "node_modules" {
2096            continue;
2097        }
2098
2099        if path.is_dir() {
2100            // Check for index.ts / index.js in subdirectory
2101            for index in &["index.ts", "index.js"] {
2102                let index_path = path.join(index);
2103                if index_path.exists() {
2104                    let rel = path.strip_prefix(base).unwrap_or(&path);
2105                    results.push(DiscoveredResource {
2106                        kind: ResourceKind::Extension,
2107                        path: index_path,
2108                        relative_path: rel.join(index).to_string_lossy().to_string(),
2109                    });
2110                }
2111            }
2112        } else {
2113            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
2114            if matches!(ext, "so" | "dylib" | "dll" | "ts" | "js") {
2115                let rel = path.strip_prefix(base).unwrap_or(&path);
2116                results.push(DiscoveredResource {
2117                    kind: ResourceKind::Extension,
2118                    path: path.clone(),
2119                    relative_path: rel.to_string_lossy().to_string(),
2120                });
2121            }
2122        }
2123    }
2124}
2125
2126/// Discover skill directories containing SKILL.md
2127fn discover_skills(dir: &Path) -> Vec<DiscoveredResource> {
2128    let mut results = Vec::new();
2129    discover_skills_recursive(dir, dir, &mut results);
2130    results
2131}
2132
2133fn discover_skills_recursive(base: &Path, current: &Path, results: &mut Vec<DiscoveredResource>) {
2134    if !current.exists() {
2135        return;
2136    }
2137
2138    let entries = match fs::read_dir(current) {
2139        Ok(e) => e,
2140        Err(_) => return,
2141    };
2142
2143    for entry in entries.flatten() {
2144        let path = entry.path();
2145        let name = entry.file_name();
2146        let name_str = name.to_string_lossy();
2147
2148        if name_str.starts_with('.') || name_str == "node_modules" {
2149            continue;
2150        }
2151
2152        if path.is_dir() {
2153            let skill_file = path.join("SKILL.md");
2154            if skill_file.exists() {
2155                let rel = path.strip_prefix(base).unwrap_or(&path);
2156                results.push(DiscoveredResource {
2157                    kind: ResourceKind::Skill,
2158                    path: skill_file,
2159                    relative_path: rel.join("SKILL.md").to_string_lossy().to_string(),
2160                });
2161            }
2162            discover_skills_recursive(base, &path, results);
2163        }
2164    }
2165}
2166
2167/// Discover prompt template files (.md in prompts/ subdirectory)
2168fn discover_prompts(dir: &Path) -> Vec<DiscoveredResource> {
2169    let prompts_dir = dir.join("prompts");
2170    discover_files_by_ext(
2171        if prompts_dir.exists() {
2172            &prompts_dir
2173        } else {
2174            dir
2175        },
2176        "md",
2177        ResourceKind::Prompt,
2178    )
2179}
2180
2181/// Discover theme files (.json in themes/ subdirectory)
2182fn discover_themes(dir: &Path) -> Vec<DiscoveredResource> {
2183    let themes_dir = dir.join("themes");
2184    discover_files_by_ext(
2185        if themes_dir.exists() {
2186            &themes_dir
2187        } else {
2188            dir
2189        },
2190        "json",
2191        ResourceKind::Theme,
2192    )
2193}
2194
2195/// Recursively find files with a given extension
2196fn discover_files_by_ext(dir: &Path, ext: &str, kind: ResourceKind) -> Vec<DiscoveredResource> {
2197    let mut results = Vec::new();
2198    discover_files_recursive(dir, dir, ext, kind, &mut results);
2199    results
2200}
2201
2202fn discover_files_recursive(
2203    base: &Path,
2204    current: &Path,
2205    ext: &str,
2206    kind: ResourceKind,
2207    results: &mut Vec<DiscoveredResource>,
2208) {
2209    if !current.exists() {
2210        return;
2211    }
2212
2213    let entries = match fs::read_dir(current) {
2214        Ok(e) => e,
2215        Err(_) => return,
2216    };
2217
2218    for entry in entries.flatten() {
2219        let path = entry.path();
2220        let name = entry.file_name();
2221        let name_str = name.to_string_lossy();
2222
2223        if name_str.starts_with('.') || name_str == "node_modules" {
2224            continue;
2225        }
2226
2227        if path.is_dir() {
2228            discover_files_recursive(base, &path, ext, kind, results);
2229        } else if path.extension().and_then(|e| e.to_str()) == Some(ext) {
2230            let rel = path.strip_prefix(base).unwrap_or(&path);
2231            results.push(DiscoveredResource {
2232                kind,
2233                path: path.clone(),
2234                relative_path: rel.to_string_lossy().to_string(),
2235            });
2236        }
2237    }
2238}
2239
2240// ── Utility functions ─────────────────────────────────────────────────
2241
2242/// Recursively copy a directory
2243fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
2244    if !dst.exists() {
2245        fs::create_dir_all(dst)?;
2246    }
2247
2248    for entry in fs::read_dir(src)? {
2249        let entry = entry?;
2250        let src_path = entry.path();
2251        let dst_path = dst.join(entry.file_name());
2252
2253        if src_path.is_dir() {
2254            copy_dir_recursive(&src_path, &dst_path)?;
2255        } else {
2256            fs::copy(&src_path, &dst_path)?;
2257        }
2258    }
2259
2260    Ok(())
2261}
2262
2263/// Compute a SHA-256 hash of a directory's contents for integrity checking
2264fn compute_dir_hash(dir: &Path) -> Option<String> {
2265    let mut hasher = Sha256::new();
2266    let mut files = collect_file_paths(dir);
2267    files.sort();
2268
2269    for file_path in &files {
2270        if let Ok(content) = fs::read(file_path) {
2271            hasher.update(&content);
2272        }
2273    }
2274
2275    let result = hasher.finalize();
2276    Some(format!("sha256-{:x}", result))
2277}
2278
2279/// Collect all file paths in a directory recursively
2280fn collect_file_paths(dir: &Path) -> Vec<PathBuf> {
2281    let mut paths = Vec::new();
2282    if !dir.exists() {
2283        return paths;
2284    }
2285
2286    let entries = match fs::read_dir(dir) {
2287        Ok(e) => e,
2288        Err(_) => return paths,
2289    };
2290
2291    for entry in entries.flatten() {
2292        let path = entry.path();
2293        if path.is_dir() {
2294            paths.extend(collect_file_paths(&path));
2295        } else {
2296            paths.push(path);
2297        }
2298    }
2299
2300    paths
2301}
2302
2303/// Find the single subdirectory inside an extracted archive
2304fn find_single_subdir(dir: &Path) -> Option<PathBuf> {
2305    let entries: Vec<_> = fs::read_dir(dir).ok()?.filter_map(|e| e.ok()).collect();
2306    if entries.len() == 1 && entries[0].path().is_dir() {
2307        Some(entries[0].path())
2308    } else {
2309        None
2310    }
2311}
2312
2313/// Remove empty parent directories up to a root
2314fn prune_empty_parents(target: &Path, root: &Path) {
2315    let mut current = target.parent();
2316    while let Some(dir) = current {
2317        if dir == root || !dir.starts_with(root) {
2318            break;
2319        }
2320        if dir.exists() {
2321            let is_empty = fs::read_dir(dir)
2322                .map(|mut rd| rd.next().is_none())
2323                .unwrap_or(false);
2324            if is_empty {
2325                let _ = fs::remove_dir(dir);
2326            } else {
2327                break;
2328            }
2329        }
2330        current = dir.parent();
2331    }
2332}
2333
2334#[cfg(test)]
2335mod tests {
2336    use super::*;
2337
2338    fn setup_temp_packages_dir() -> (tempfile::TempDir, PathBuf) {
2339        let tmp = tempfile::tempdir().unwrap();
2340        let packages_dir = tmp.path().join("packages");
2341        fs::create_dir_all(&packages_dir).unwrap();
2342        (tmp, packages_dir)
2343    }
2344
2345    fn create_test_package(base: &Path, name: &str, version: &str) -> PathBuf {
2346        let pkg_dir = base.join("source-pkg");
2347        fs::create_dir_all(&pkg_dir).unwrap();
2348
2349        let manifest = PackageManifest {
2350            name: name.to_string(),
2351            version: version.to_string(),
2352            extensions: vec!["ext1.so".to_string()],
2353            skills: vec!["skill-a".to_string()],
2354            prompts: vec![],
2355            themes: vec![],
2356            description: None,
2357            dependencies: BTreeMap::new(),
2358        };
2359
2360        let toml_content = toml::to_string_pretty(&manifest).unwrap();
2361        fs::write(pkg_dir.join(MANIFEST_NAME), toml_content).unwrap();
2362        fs::write(pkg_dir.join("ext1.so"), "fake extension").unwrap();
2363        fs::create_dir_all(pkg_dir.join("skill-a")).unwrap();
2364        fs::write(pkg_dir.join("skill-a").join("SKILL.md"), "# Skill A").unwrap();
2365
2366        pkg_dir
2367    }
2368
2369    fn create_test_package_with_auto_discovery(base: &Path, name: &str, version: &str) -> PathBuf {
2370        let pkg_dir = base.join("source-pkg-auto");
2371        fs::create_dir_all(&pkg_dir).unwrap();
2372
2373        let manifest = PackageManifest {
2374            name: name.to_string(),
2375            version: version.to_string(),
2376            extensions: vec![],
2377            skills: vec![],
2378            prompts: vec![],
2379            themes: vec![],
2380            description: None,
2381            dependencies: BTreeMap::new(),
2382        };
2383        let toml_content = toml::to_string_pretty(&manifest).unwrap();
2384        fs::write(pkg_dir.join(MANIFEST_NAME), toml_content).unwrap();
2385
2386        fs::write(pkg_dir.join("myext.so"), "extension").unwrap();
2387        fs::create_dir_all(pkg_dir.join("my-skill")).unwrap();
2388        fs::write(pkg_dir.join("my-skill").join("SKILL.md"), "# My Skill").unwrap();
2389        fs::create_dir_all(pkg_dir.join("prompts")).unwrap();
2390        fs::write(pkg_dir.join("prompts").join("review.md"), "# Review").unwrap();
2391        fs::create_dir_all(pkg_dir.join("themes")).unwrap();
2392        fs::write(pkg_dir.join("themes").join("dark.json"), "{}").unwrap();
2393
2394        pkg_dir
2395    }
2396
2397    #[test]
2398    fn test_install_and_list() {
2399        let (tmp, packages_dir) = setup_temp_packages_dir();
2400
2401        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2402        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2403
2404        let manifest = mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2405        assert_eq!(manifest.name, "test-pkg");
2406        assert_eq!(manifest.version, "1.0.0");
2407
2408        let installed = mgr.list();
2409        assert_eq!(installed.len(), 1);
2410        assert_eq!(installed[0].name, "test-pkg");
2411    }
2412
2413    #[test]
2414    fn test_uninstall() {
2415        let (tmp, packages_dir) = setup_temp_packages_dir();
2416
2417        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2418        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2419
2420        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2421        assert!(mgr.is_installed("test-pkg"));
2422
2423        mgr.uninstall("test-pkg").unwrap();
2424        assert!(!mgr.is_installed("test-pkg"));
2425        assert!(mgr.list().is_empty());
2426    }
2427
2428    #[test]
2429    fn test_uninstall_not_installed() {
2430        let (_tmp, packages_dir) = setup_temp_packages_dir();
2431        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2432
2433        let result = mgr.uninstall("nonexistent");
2434        assert!(result.is_err());
2435    }
2436
2437    #[test]
2438    fn test_install_scoped_package() {
2439        let (tmp, packages_dir) = setup_temp_packages_dir();
2440
2441        let pkg_dir = create_test_package(tmp.path(), "@foo/oxi-tools", "2.0.0");
2442        let mut mgr = PackageManager::with_dir(packages_dir.clone()).unwrap();
2443
2444        let manifest = mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2445        assert_eq!(manifest.name, "@foo/oxi-tools");
2446
2447        let expected_dir = packages_dir.join("foo-oxi-tools");
2448        assert!(expected_dir.exists());
2449    }
2450
2451    #[test]
2452    fn test_reinstall_overwrites() {
2453        let (tmp, packages_dir) = setup_temp_packages_dir();
2454
2455        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2456        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2457
2458        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2459
2460        let pkg_dir_v2 = tmp.path().join("source-pkg-v2");
2461        fs::create_dir_all(&pkg_dir_v2).unwrap();
2462        let manifest_v2 = PackageManifest {
2463            name: "test-pkg".to_string(),
2464            version: "2.0.0".to_string(),
2465            extensions: vec![],
2466            skills: vec![],
2467            prompts: vec![],
2468            themes: vec![],
2469            description: None,
2470            dependencies: BTreeMap::new(),
2471        };
2472        fs::write(
2473            pkg_dir_v2.join(MANIFEST_NAME),
2474            toml::to_string_pretty(&manifest_v2).unwrap(),
2475        )
2476        .unwrap();
2477
2478        mgr.install(pkg_dir_v2.to_str().unwrap()).unwrap();
2479
2480        let installed = mgr.list();
2481        assert_eq!(installed.len(), 1);
2482        assert_eq!(installed[0].version, "2.0.0");
2483    }
2484
2485    #[test]
2486    fn test_empty_packages_dir() {
2487        let (_tmp, packages_dir) = setup_temp_packages_dir();
2488        let mgr = PackageManager::with_dir(packages_dir).unwrap();
2489        assert!(mgr.list().is_empty());
2490    }
2491
2492    #[test]
2493    fn test_packages_dir_not_exists() {
2494        let tmp = tempfile::tempdir().unwrap();
2495        let nonexistent = tmp.path().join("does-not-exist");
2496        let mgr = PackageManager::with_dir(nonexistent).unwrap();
2497        assert!(mgr.list().is_empty());
2498    }
2499
2500    #[test]
2501    fn test_discover_resources_explicit() {
2502        let (tmp, packages_dir) = setup_temp_packages_dir();
2503
2504        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2505        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2506        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2507
2508        let resources = mgr.discover_resources("test-pkg").unwrap();
2509        assert_eq!(resources.len(), 2);
2510
2511        let extensions: Vec<_> = resources
2512            .iter()
2513            .filter(|r| r.kind == ResourceKind::Extension)
2514            .collect();
2515        let skills: Vec<_> = resources
2516            .iter()
2517            .filter(|r| r.kind == ResourceKind::Skill)
2518            .collect();
2519        assert_eq!(extensions.len(), 1);
2520        assert_eq!(skills.len(), 1);
2521    }
2522
2523    #[test]
2524    fn test_discover_resources_auto() {
2525        let (tmp, packages_dir) = setup_temp_packages_dir();
2526
2527        let pkg_dir = create_test_package_with_auto_discovery(tmp.path(), "auto-pkg", "1.0.0");
2528        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2529        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2530
2531        let resources = mgr.discover_resources("auto-pkg").unwrap();
2532
2533        let ext_count = resources
2534            .iter()
2535            .filter(|r| r.kind == ResourceKind::Extension)
2536            .count();
2537        let skill_count = resources
2538            .iter()
2539            .filter(|r| r.kind == ResourceKind::Skill)
2540            .count();
2541        let prompt_count = resources
2542            .iter()
2543            .filter(|r| r.kind == ResourceKind::Prompt)
2544            .count();
2545        let theme_count = resources
2546            .iter()
2547            .filter(|r| r.kind == ResourceKind::Theme)
2548            .count();
2549
2550        assert!(
2551            ext_count >= 1,
2552            "Expected at least 1 extension, got {}",
2553            ext_count
2554        );
2555        assert!(
2556            skill_count >= 1,
2557            "Expected at least 1 skill, got {}",
2558            skill_count
2559        );
2560        assert!(
2561            prompt_count >= 1,
2562            "Expected at least 1 prompt, got {}",
2563            prompt_count
2564        );
2565        assert!(
2566            theme_count >= 1,
2567            "Expected at least 1 theme, got {}",
2568            theme_count
2569        );
2570    }
2571
2572    #[test]
2573    fn test_resource_counts() {
2574        let (tmp, packages_dir) = setup_temp_packages_dir();
2575
2576        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2577        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2578        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2579
2580        let counts = mgr.resource_counts("test-pkg").unwrap();
2581        assert_eq!(counts.extensions, 1);
2582        assert_eq!(counts.skills, 1);
2583        assert_eq!(counts.prompts, 0);
2584        assert_eq!(counts.themes, 0);
2585    }
2586
2587    #[test]
2588    fn test_resource_counts_display() {
2589        let counts = ResourceCounts {
2590            extensions: 2,
2591            skills: 1,
2592            prompts: 0,
2593            themes: 3,
2594        };
2595        assert_eq!(counts.to_string(), "2 ext, 1 skill, 3 theme");
2596
2597        let empty = ResourceCounts::default();
2598        assert_eq!(empty.to_string(), "-");
2599    }
2600
2601    #[test]
2602    fn test_resource_kind_display() {
2603        assert_eq!(ResourceKind::Extension.to_string(), "extension");
2604        assert_eq!(ResourceKind::Skill.to_string(), "skill");
2605        assert_eq!(ResourceKind::Prompt.to_string(), "prompt");
2606        assert_eq!(ResourceKind::Theme.to_string(), "theme");
2607    }
2608
2609    #[test]
2610    fn test_get_install_dir() {
2611        let (tmp, packages_dir) = setup_temp_packages_dir();
2612
2613        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2614        let mut mgr = PackageManager::with_dir(packages_dir.clone()).unwrap();
2615        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2616
2617        let dir = mgr.get_install_dir("test-pkg").unwrap();
2618        assert!(dir.exists());
2619        assert!(dir.join(MANIFEST_NAME).exists());
2620
2621        assert!(mgr.get_install_dir("nonexistent").is_none());
2622    }
2623
2624    #[test]
2625    fn test_discover_resources_not_installed() {
2626        let (_tmp, packages_dir) = setup_temp_packages_dir();
2627        let mgr = PackageManager::with_dir(packages_dir).unwrap();
2628
2629        let result = mgr.discover_resources("nonexistent");
2630        assert!(result.is_err());
2631    }
2632
2633    #[test]
2634    fn test_update_not_installed() {
2635        let (_tmp, packages_dir) = setup_temp_packages_dir();
2636        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2637
2638        let result = mgr.update("nonexistent");
2639        assert!(result.is_err());
2640    }
2641
2642    // ── Source parsing tests ──────────────────────────────────────────
2643
2644    #[test]
2645    fn test_parse_npm_source() {
2646        let parsed = ParsedSource::parse("npm:express@4.18.0");
2647        match parsed {
2648            ParsedSource::Npm { spec, name, pinned } => {
2649                assert_eq!(spec, "express@4.18.0");
2650                assert_eq!(name, "express");
2651                assert!(pinned);
2652            }
2653            _ => panic!("Expected Npm source"),
2654        }
2655
2656        let parsed = ParsedSource::parse("npm:lodash");
2657        match parsed {
2658            ParsedSource::Npm { name, pinned, .. } => {
2659                assert_eq!(name, "lodash");
2660                assert!(!pinned);
2661            }
2662            _ => panic!("Expected Npm source"),
2663        }
2664    }
2665
2666    #[test]
2667    fn test_parse_git_source() {
2668        let parsed = ParsedSource::parse("https://github.com/org/repo.git");
2669        match parsed {
2670            ParsedSource::Git {
2671                host, path, ref_, ..
2672            } => {
2673                assert_eq!(host, "github.com");
2674                assert_eq!(path, "org/repo");
2675                assert!(ref_.is_none());
2676            }
2677            _ => panic!("Expected Git source"),
2678        }
2679
2680        let parsed = ParsedSource::parse("https://github.com/org/repo.git@v1.0.0");
2681        match parsed {
2682            ParsedSource::Git { path, ref_, .. } => {
2683                assert_eq!(path, "org/repo");
2684                assert_eq!(ref_.as_deref(), Some("v1.0.0"));
2685            }
2686            _ => panic!("Expected Git source"),
2687        }
2688    }
2689
2690    #[test]
2691    fn test_parse_github_shorthand() {
2692        let parsed = ParsedSource::parse("github:org/repo@main");
2693        match parsed {
2694            ParsedSource::Git {
2695                host, path, ref_, ..
2696            } => {
2697                assert_eq!(host, "github.com");
2698                assert_eq!(path, "org/repo");
2699                assert_eq!(ref_.as_deref(), Some("main"));
2700            }
2701            _ => panic!("Expected Git source"),
2702        }
2703    }
2704
2705    #[test]
2706    fn test_parse_local_source() {
2707        let parsed = ParsedSource::parse("/path/to/package");
2708        match parsed {
2709            ParsedSource::Local { path } => {
2710                assert_eq!(path, "/path/to/package");
2711            }
2712            _ => panic!("Expected Local source"),
2713        }
2714
2715        let parsed = ParsedSource::parse("./relative/path");
2716        match parsed {
2717            ParsedSource::Local { path } => {
2718                assert_eq!(path, "./relative/path");
2719            }
2720            _ => panic!("Expected Local source"),
2721        }
2722    }
2723
2724    #[test]
2725    fn test_parse_url_source() {
2726        let parsed = ParsedSource::parse("https://example.com/pkg.tar.gz");
2727        match parsed {
2728            ParsedSource::Url { url } => {
2729                assert_eq!(url, "https://example.com/pkg.tar.gz");
2730            }
2731            _ => panic!("Expected Url source"),
2732        }
2733    }
2734
2735    #[test]
2736    fn test_source_identity() {
2737        let npm = ParsedSource::parse("npm:express@4.18.0");
2738        assert_eq!(npm.identity(), "npm:express");
2739
2740        let git = ParsedSource::parse("https://github.com/org/repo.git");
2741        assert_eq!(git.identity(), "git:github.com/org/repo");
2742
2743        let local = ParsedSource::parse("/path/to/pkg");
2744        assert_eq!(local.identity(), "local:/path/to/pkg");
2745    }
2746
2747    #[test]
2748    fn test_parse_npm_spec() {
2749        let (name, pinned) = parse_npm_spec("express@4.18.0");
2750        assert_eq!(name, "express");
2751        assert!(pinned);
2752
2753        let (name, pinned) = parse_npm_spec("express");
2754        assert_eq!(name, "express");
2755        assert!(!pinned);
2756
2757        let (name, pinned) = parse_npm_spec("@scope/pkg@1.0.0");
2758        assert_eq!(name, "@scope/pkg");
2759        assert!(pinned);
2760    }
2761
2762    // ── Lockfile tests ────────────────────────────────────────────────
2763
2764    #[test]
2765    fn test_lockfile_roundtrip() {
2766        let (tmp, _) = setup_temp_packages_dir();
2767        let lock_path = tmp.path().join(LOCKFILE_NAME);
2768
2769        let mut lock = Lockfile::new();
2770        lock.insert(LockEntry {
2771            source: "npm:express@4.18.0".to_string(),
2772            name: "express".to_string(),
2773            version: "4.18.0".to_string(),
2774            integrity: Some("sha256-abc123".to_string()),
2775            scope: SourceScope::User,
2776            source_type: "npm".to_string(),
2777            dependencies: BTreeMap::new(),
2778        });
2779
2780        lock.write(&lock_path).unwrap();
2781
2782        let loaded = Lockfile::read(&lock_path).unwrap().unwrap();
2783        assert_eq!(loaded.packages.len(), 1);
2784        assert_eq!(loaded.packages["express"].version, "4.18.0");
2785        assert_eq!(
2786            loaded.packages["express"].integrity.as_deref(),
2787            Some("sha256-abc123")
2788        );
2789    }
2790
2791    #[test]
2792    fn test_lockfile_install_roundtrip() {
2793        let (tmp, packages_dir) = setup_temp_packages_dir();
2794        let pkg_dir = create_test_package(tmp.path(), "locked-pkg", "1.0.0");
2795
2796        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2797        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2798
2799        // Lockfile should have been written
2800        let lock_path = mgr.packages_dir().join(LOCKFILE_NAME);
2801        assert!(lock_path.exists());
2802
2803        let lock = Lockfile::read(&lock_path).unwrap().unwrap();
2804        assert!(lock.contains("locked-pkg"));
2805        let entry = lock.get("locked-pkg").unwrap();
2806        assert_eq!(entry.version, "1.0.0");
2807    }
2808
2809    // ── Validation tests ──────────────────────────────────────────────
2810
2811    #[test]
2812    fn test_validate_valid_package() {
2813        let (tmp, _) = setup_temp_packages_dir();
2814        let pkg_dir = create_test_package(tmp.path(), "valid-pkg", "1.0.0");
2815        let warnings = PackageManager::validate_package(&pkg_dir).unwrap();
2816        // Should have minimal warnings (maybe just about .gitignore)
2817        assert!(
2818            warnings.len() <= 1,
2819            "Expected <= 1 warning, got {:?}",
2820            warnings
2821        );
2822    }
2823
2824    #[test]
2825    fn test_validate_empty_dir() {
2826        let tmp = tempfile::tempdir().unwrap();
2827        let empty_dir = tmp.path().join("empty-pkg");
2828        fs::create_dir_all(&empty_dir).unwrap();
2829        let warnings = PackageManager::validate_package(&empty_dir).unwrap();
2830        assert!(!warnings.is_empty());
2831    }
2832
2833    // ── Dependency tests ──────────────────────────────────────────────
2834
2835    #[test]
2836    fn test_resolve_dependencies() {
2837        let (tmp, packages_dir) = setup_temp_packages_dir();
2838
2839        // Create a package with dependencies
2840        let pkg_dir = tmp.path().join("dep-pkg");
2841        fs::create_dir_all(&pkg_dir).unwrap();
2842        let mut deps = BTreeMap::new();
2843        deps.insert("lodash".to_string(), "^4.0.0".to_string());
2844        deps.insert("nonexistent-pkg".to_string(), "^1.0.0".to_string());
2845
2846        let manifest = PackageManifest {
2847            name: "dep-pkg".to_string(),
2848            version: "1.0.0".to_string(),
2849            extensions: vec![],
2850            skills: vec![],
2851            prompts: vec![],
2852            themes: vec![],
2853            description: None,
2854            dependencies: deps,
2855        };
2856        fs::write(
2857            pkg_dir.join(MANIFEST_NAME),
2858            toml::to_string_pretty(&manifest).unwrap(),
2859        )
2860        .unwrap();
2861
2862        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2863        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2864
2865        let missing = mgr.resolve_dependencies();
2866        assert_eq!(missing.len(), 1);
2867        assert_eq!(missing[0].0, "dep-pkg");
2868        assert!(
2869            missing[0].1.contains(&"lodash".to_string())
2870                || missing[0].1.contains(&"nonexistent-pkg".to_string())
2871        );
2872    }
2873
2874    // ── Version tests ─────────────────────────────────────────────────
2875
2876    #[test]
2877    fn test_version_satisfies() {
2878        let (tmp, packages_dir) = setup_temp_packages_dir();
2879        let pkg_dir = create_test_package(tmp.path(), "ver-pkg", "1.2.3");
2880        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2881        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2882
2883        assert!(mgr.version_satisfies("ver-pkg", "^1.0.0"));
2884        assert!(mgr.version_satisfies("ver-pkg", ">=1.0.0"));
2885        assert!(!mgr.version_satisfies("ver-pkg", "^2.0.0"));
2886        assert!(!mgr.version_satisfies("ver-pkg", "<1.0.0"));
2887    }
2888
2889    #[test]
2890    fn test_get_installed_version() {
2891        let (tmp, packages_dir) = setup_temp_packages_dir();
2892        let pkg_dir = create_test_package(tmp.path(), "ver-pkg", "3.1.4");
2893        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2894        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2895
2896        assert_eq!(mgr.get_installed_version("ver-pkg"), Some("3.1.4"));
2897        assert_eq!(mgr.get_installed_version("nonexistent"), None);
2898    }
2899
2900    // ── Resolve tests ─────────────────────────────────────────────────
2901
2902    #[test]
2903    fn test_resolve() {
2904        let (tmp, packages_dir) = setup_temp_packages_dir();
2905        let pkg_dir = create_test_package(tmp.path(), "resolve-pkg", "1.0.0");
2906        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2907        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2908
2909        let resolved = mgr.resolve();
2910        assert!(!resolved.extensions.is_empty() || !resolved.skills.is_empty());
2911    }
2912
2913    // ── Progress callback tests ───────────────────────────────────────
2914
2915    #[test]
2916    fn test_progress_callback() {
2917        use std::sync::{Arc, Mutex};
2918
2919        let events: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
2920        let events_clone = events.clone();
2921
2922        let (tmp, packages_dir) = setup_temp_packages_dir();
2923        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2924
2925        mgr.set_progress_callback(Box::new(move |event| {
2926            let mut e = events_clone.lock().unwrap();
2927            e.push(format!("{:?}:{:?}", event.event_type, event.action));
2928        }));
2929
2930        let pkg_dir = create_test_package(tmp.path(), "progress-pkg", "1.0.0");
2931        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2932
2933        // install_local doesn't use with_progress, so no events expected from install()
2934        // Just verify the progress event mechanism exists and doesn't panic
2935        let _event_count = events.lock().unwrap().len();
2936    }
2937
2938    #[test]
2939    fn test_list_configured() {
2940        let (tmp, packages_dir) = setup_temp_packages_dir();
2941        let pkg_dir = create_test_package(tmp.path(), "cfg-pkg", "1.0.0");
2942        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2943        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2944
2945        let configured = mgr.list_configured();
2946        assert_eq!(configured.len(), 1);
2947        assert!(configured[0].source.contains("source-pkg"));
2948        // source comes from lockfile, might be the local path
2949    }
2950}