Files
wehub-resource-sync e30e75b5d4
Changesets / Create Version PR (push) Has been cancelled
Deploy Shadcn Registry / Deploy Production (push) Has been cancelled
Template Metrics / LOC + Bundle Size (push) Has been cancelled
Code Quality / Oxlint + Oxfmt (push) Has been cancelled
Code Quality / Template Sync (push) Has been cancelled
Code Quality / Build Changed Packages (push) Has been cancelled
Code Quality / Test Changed Packages (push) Has been cancelled
Deploy Expo Example / Deploy Production (push) Has been cancelled
Deploy Ink Example / Deploy Production (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.12) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:40:13 +08:00

110 lines
3.0 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import * as fs from "node:fs";
import * as path from "node:path";
import * as os from "node:os";
import {
isPackageInstalled,
getInstallCommand,
} from "../../src/lib/utils/package-manager";
describe("package-manager utilities", () => {
let testDir: string;
beforeEach(() => {
testDir = fs.mkdtempSync(path.join(os.tmpdir(), "cli-test-"));
});
afterEach(() => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
describe("isPackageInstalled", () => {
it("should return false when package.json does not exist", () => {
expect(isPackageInstalled("test-package", testDir)).toBe(false);
});
it("should return false when package is not in dependencies", () => {
const packageJson = {
dependencies: {
"other-package": "^1.0.0",
},
};
fs.writeFileSync(
path.join(testDir, "package.json"),
JSON.stringify(packageJson),
);
expect(isPackageInstalled("test-package", testDir)).toBe(false);
});
it("should return true when package is in dependencies", () => {
const packageJson = {
dependencies: {
"test-package": "^1.0.0",
},
};
fs.writeFileSync(
path.join(testDir, "package.json"),
JSON.stringify(packageJson),
);
expect(isPackageInstalled("test-package", testDir)).toBe(true);
});
it("should return true when package is in devDependencies", () => {
const packageJson = {
devDependencies: {
"test-package": "^1.0.0",
},
};
fs.writeFileSync(
path.join(testDir, "package.json"),
JSON.stringify(packageJson),
);
expect(isPackageInstalled("test-package", testDir)).toBe(true);
});
it("should return true when package exists in node_modules", () => {
const nodeModulesPath = path.join(
testDir,
"node_modules",
"test-package",
);
fs.mkdirSync(nodeModulesPath, { recursive: true });
expect(isPackageInstalled("test-package", testDir)).toBe(true);
});
it("should handle scoped packages correctly", () => {
const packageJson = {
dependencies: {
"@scope/test-package": "^1.0.0",
},
};
fs.writeFileSync(
path.join(testDir, "package.json"),
JSON.stringify(packageJson),
);
expect(isPackageInstalled("@scope/test-package", testDir)).toBe(true);
});
});
describe("getInstallCommand", () => {
it("should return an install command", async () => {
const cmd = await getInstallCommand("test-package", testDir);
expect(cmd.args).toContain("test-package");
expect(["npm", "pnpm", "yarn", "bun"]).toContain(cmd.command);
});
it("should include package name in command", async () => {
const cmd = await getInstallCommand("my-awesome-package", testDir);
expect(cmd.args).toContain("my-awesome-package");
});
});
});