import assert from "node:assert";
import { describe, it } from "node:test";
import { getPackageManagerUsageProperties } from "./package-manager.js";
describe("getPackageManagerUsageProperties", () => {
    it("buckets npm create under npm", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 darwin arm64 workspaces/false",
            npm_command: "init",
            npm_execpath: "/usr/local/lib/node_modules/npm/bin/npm-cli.js",
            _: "/usr/local/bin/npm",
        });
        assert.strictEqual(info.packageManager, "npm");
        assert.strictEqual(info.runner, "npm-create");
        assert.strictEqual(info.packageManagerVersion, "10.9.0");
        assert.strictEqual(info.installSource, "system");
    });
    it("buckets npx under npm when npm exposes the npx runner", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 darwin arm64 workspaces/false",
            npm_command: "exec",
            npm_execpath: "/usr/local/lib/node_modules/npm/bin/npx-cli.js",
            _: "/usr/local/bin/npx",
        });
        assert.strictEqual(info.packageManager, "npm");
        assert.strictEqual(info.runner, "npx");
    });
    it("buckets pnpm create under pnpm", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "pnpm/10.7.0 npm/? node/v22.14.0 darwin arm64",
            npm_command: "create",
            npm_execpath: "/Users/me/Library/pnpm/pnpm.cjs",
            _: "/Users/me/Library/pnpm/pnpm",
        });
        assert.strictEqual(info.packageManager, "pnpm");
        assert.strictEqual(info.runner, "pnpm-create");
        assert.strictEqual(info.packageManagerVersion, "10.7.0");
    });
    it("buckets pnpx under pnpm", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "pnpm/10.7.0 npm/? node/v22.14.0 darwin arm64",
            npm_command: "exec",
            npm_execpath: "/Users/me/Library/pnpm/pnpm.cjs",
            _: "/Users/me/Library/pnpm/pnpx",
        });
        assert.strictEqual(info.packageManager, "pnpm");
        assert.strictEqual(info.runner, "pnpx");
    });
    it("buckets yarn create under yarn", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "yarn/4.5.1 npm/? node/v22.14.0 darwin arm64",
            npm_command: "create",
            npm_execpath: "/usr/local/bin/yarn.js",
            _: "/usr/local/bin/yarn",
        });
        assert.strictEqual(info.packageManager, "yarn");
        assert.strictEqual(info.runner, "yarn-create");
        assert.strictEqual(info.packageManagerVersion, "4.5.1");
    });
    it("buckets yarn dlx under yarn", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "yarn/4.5.1 npm/? node/v22.14.0 darwin arm64",
            npm_command: "dlx",
            npm_execpath: "/usr/local/bin/yarn.js",
            _: "/usr/local/bin/yarn",
        });
        assert.strictEqual(info.packageManager, "yarn");
        assert.strictEqual(info.runner, "yarn-dlx");
    });
    it("buckets bun create under bun", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "bun/1.2.0 npm/? node/v22.14.0 darwin arm64",
            npm_command: "create",
            npm_execpath: "/usr/local/bin/bun",
            _: "/usr/local/bin/bun",
        });
        assert.strictEqual(info.packageManager, "bun");
        assert.strictEqual(info.runner, "bun-create");
        assert.strictEqual(info.packageManagerVersion, "1.2.0");
    });
    it("buckets bunx under bun", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "bun/1.2.0 npm/? node/v22.14.0 darwin arm64",
            npm_command: "x",
            npm_execpath: "/usr/local/bin/bun",
            _: "/usr/local/bin/bunx",
        });
        assert.strictEqual(info.packageManager, "bun");
        assert.strictEqual(info.runner, "bunx");
    });
    it("reports unknown for missing environment", () => {
        const info = getPackageManagerUsageProperties({});
        assert.strictEqual(info.packageManager, "unknown");
        assert.strictEqual(info.runner, "unknown");
        assert.strictEqual(info.installSource, "unknown");
    });
});
describe("installSource detection", () => {
    it("detects project-local node_modules/.bin", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 darwin arm64",
            npm_execpath: "/Users/me/work/proj/node_modules/.bin/npm",
            _: "/Users/me/work/proj/node_modules/.bin/npm",
        });
        assert.strictEqual(info.installSource, "project-local");
    });
    it("detects corepack", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "pnpm/10.7.0 npm/? node/v22.14.0 darwin arm64",
            npm_execpath: "/Users/me/.nvm/versions/node/v22.14.0/lib/node_modules/corepack/dist/pnpm.js",
            _: "/Users/me/.nvm/versions/node/v22.14.0/bin/pnpm",
        });
        assert.strictEqual(info.installSource, "corepack");
    });
    it("detects volta", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 darwin arm64",
            npm_execpath: "/Users/me/.volta/tools/image/npm/10.9.0/bin/npm-cli.js",
            _: "/Users/me/.volta/bin/npm",
        });
        assert.strictEqual(info.installSource, "volta");
    });
    it("detects nvm", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 darwin arm64",
            npm_execpath: "/Users/me/.nvm/versions/node/v22.14.0/lib/node_modules/npm/bin/npm-cli.js",
            _: "/Users/me/.nvm/versions/node/v22.14.0/bin/npm",
        });
        assert.strictEqual(info.installSource, "nvm");
    });
    it("detects asdf", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 linux x64",
            npm_execpath: "/Users/me/.asdf/installs/nodejs/22.14.0/lib/node_modules/npm/bin/npm-cli.js",
            _: "/Users/me/.asdf/shims/npm",
        });
        assert.strictEqual(info.installSource, "asdf");
    });
    it("detects fnm", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 darwin arm64",
            npm_execpath: "/var/folders/xx/fnm_multishells/12345_0000000000000/lib/node_modules/npm/bin/npm-cli.js",
            _: "/Users/me/.fnm/aliases/default/bin/npm",
        });
        assert.strictEqual(info.installSource, "fnm");
    });
    it("detects homebrew (apple silicon path)", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 darwin arm64",
            npm_execpath: "/opt/homebrew/Cellar/node/22.14.0/lib/node_modules/npm/bin/npm-cli.js",
            _: "/opt/homebrew/bin/npm",
        });
        assert.strictEqual(info.installSource, "homebrew");
    });
    it("detects system install", () => {
        const info = getPackageManagerUsageProperties({
            npm_config_user_agent: "npm/10.9.0 node/v22.14.0 linux x64",
            npm_execpath: "/usr/local/lib/node_modules/npm/bin/npm-cli.js",
            _: "/usr/local/bin/npm",
        });
        assert.strictEqual(info.installSource, "system");
    });
});
//# sourceMappingURL=package-manager.test.js.map