Files
claude-mem/tests/smart-install.test.ts
T
Alex Newman 50eeed97e7 MAESTRO: fix cache-based install — resolveRoot, post-install verification, CLI path
Replace hardcoded marketplace path in plugin/scripts/smart-install.js with
resolveRoot() that uses CLAUDE_PLUGIN_ROOT env var (set by Claude Code for
all hooks), with fallback to script location and legacy paths. Fixes #1128,
#1166 where cache installs couldn't find or install node_modules.

Also fixes installCLI() path (ROOT/plugin/scripts/ → ROOT/scripts/) and adds
verifyCriticalModules() post-install check with npm fallback on failure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 17:28:51 -05:00

166 lines
5.7 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import { existsSync, mkdirSync, writeFileSync, rmSync, readFileSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
/**
* Smart Install Script Tests
*
* Tests the resolveRoot() and verifyCriticalModules() logic used by
* plugin/scripts/smart-install.js to find the correct install directory
* for cache-based and marketplace installs.
*
* These are unit tests that exercise the resolution logic in isolation
* using temp directories, without running actual bun/npm install.
*/
const TEST_DIR = join(tmpdir(), `claude-mem-smart-install-test-${process.pid}`);
function createDir(relativePath: string): string {
const fullPath = join(TEST_DIR, relativePath);
mkdirSync(fullPath, { recursive: true });
return fullPath;
}
function createPackageJson(dir: string, version = '10.0.0', deps: Record<string, string> = {}): void {
writeFileSync(join(dir, 'package.json'), JSON.stringify({
name: 'claude-mem-plugin',
version,
dependencies: deps
}));
}
describe('smart-install resolveRoot logic', () => {
beforeEach(() => {
mkdirSync(TEST_DIR, { recursive: true });
});
afterEach(() => {
rmSync(TEST_DIR, { recursive: true, force: true });
});
it('should prefer CLAUDE_PLUGIN_ROOT when it contains package.json', () => {
const cacheDir = createDir('cache/thedotmack/claude-mem/10.0.0');
createPackageJson(cacheDir);
// Simulate what resolveRoot does
const root = cacheDir;
expect(existsSync(join(root, 'package.json'))).toBe(true);
});
it('should detect cache-based install paths', () => {
// Cache installs have paths like ~/.claude/plugins/cache/thedotmack/claude-mem/<version>/
const cacheDir = createDir('plugins/cache/thedotmack/claude-mem/10.3.0');
createPackageJson(cacheDir);
// Marketplace dir does NOT exist (fresh cache install, no marketplace)
const pluginRoot = cacheDir;
expect(existsSync(join(pluginRoot, 'package.json'))).toBe(true);
// The cache dir is valid — resolveRoot should use it, not try to navigate to marketplace
});
it('should fall back to script-relative path when CLAUDE_PLUGIN_ROOT is unset', () => {
// Simulate: scripts/smart-install.js lives in <root>/scripts/
const pluginRoot = createDir('marketplace-plugin');
createPackageJson(pluginRoot);
const scriptsDir = createDir('marketplace-plugin/scripts');
// dirname(scripts/) = marketplace-plugin/ which has package.json
const candidate = join(scriptsDir, '..');
expect(existsSync(join(candidate, 'package.json'))).toBe(true);
});
it('should handle missing package.json in CLAUDE_PLUGIN_ROOT gracefully', () => {
// CLAUDE_PLUGIN_ROOT points to a dir without package.json
const badDir = createDir('empty-cache-dir');
expect(existsSync(join(badDir, 'package.json'))).toBe(false);
// resolveRoot should fall through to next candidate
});
});
describe('smart-install verifyCriticalModules logic', () => {
beforeEach(() => {
mkdirSync(TEST_DIR, { recursive: true });
});
afterEach(() => {
rmSync(TEST_DIR, { recursive: true, force: true });
});
it('should pass when all dependencies exist in node_modules', () => {
const root = createDir('plugin-root');
createPackageJson(root, '10.0.0', {
'@chroma-core/default-embed': '^0.1.9'
});
// Create the module directory
mkdirSync(join(root, 'node_modules', '@chroma-core', 'default-embed'), { recursive: true });
// Simulate verifyCriticalModules
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'));
const dependencies = Object.keys(pkg.dependencies || {});
const missing: string[] = [];
for (const dep of dependencies) {
const modulePath = join(root, 'node_modules', ...dep.split('/'));
if (!existsSync(modulePath)) {
missing.push(dep);
}
}
expect(missing).toEqual([]);
});
it('should detect missing dependencies in node_modules', () => {
const root = createDir('plugin-root-missing');
createPackageJson(root, '10.0.0', {
'@chroma-core/default-embed': '^0.1.9'
});
// Do NOT create node_modules — simulate a failed install
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'));
const dependencies = Object.keys(pkg.dependencies || {});
const missing: string[] = [];
for (const dep of dependencies) {
const modulePath = join(root, 'node_modules', ...dep.split('/'));
if (!existsSync(modulePath)) {
missing.push(dep);
}
}
expect(missing).toEqual(['@chroma-core/default-embed']);
});
it('should handle packages with no dependencies gracefully', () => {
const root = createDir('plugin-root-no-deps');
createPackageJson(root, '10.0.0', {});
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'));
const dependencies = Object.keys(pkg.dependencies || {});
expect(dependencies).toEqual([]);
});
it('should detect partially installed scoped packages', () => {
const root = createDir('plugin-root-partial');
createPackageJson(root, '10.0.0', {
'@chroma-core/default-embed': '^0.1.9',
'@chroma-core/other-pkg': '^1.0.0'
});
// Only install one of the two packages
mkdirSync(join(root, 'node_modules', '@chroma-core', 'default-embed'), { recursive: true });
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'));
const dependencies = Object.keys(pkg.dependencies || {});
const missing: string[] = [];
for (const dep of dependencies) {
const modulePath = join(root, 'node_modules', ...dep.split('/'));
if (!existsSync(modulePath)) {
missing.push(dep);
}
}
expect(missing).toEqual(['@chroma-core/other-pkg']);
});
});