skill-dir-manager.test.js 2.11 KB
/**
 * Unit Tests — skill-dir-manager.js
 */
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import fs from 'fs';
import path from 'path';

describe('skill-dir-manager', () => {
  let dirManager;
  const TEST_CODE_DIR = '/tmp/test-sdm-code';
  const TEST_DATA_DIR = '/tmp/test-sdm-data';

  beforeAll(() => {
    fs.mkdirSync(TEST_CODE_DIR, { recursive: true });
    fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
    fs.mkdirSync(path.join(TEST_DATA_DIR, 'my-test-skill'), { recursive: true });
    fs.writeFileSync(path.join(TEST_DATA_DIR, 'my-test-skill', 'SKILL.md'), '# Test');

    process.env.SKILLS_CODE_DIR = TEST_CODE_DIR;
    process.env.SKILLS_DATA_DIR = TEST_DATA_DIR;
    delete require.cache[require.resolve('../../config')];
    delete require.cache[require.resolve('../../services/skill-dir-manager')];
    dirManager = require('../../services/skill-dir-manager');
  });

  afterAll(() => {
    try { fs.rmSync(TEST_CODE_DIR, { recursive: true }); } catch {}
    try { fs.rmSync(TEST_DATA_DIR, { recursive: true }); } catch {}
  });

  describe('resolveSkillDir', () => {
    it('should resolve a skill directory that exists in data dir', () => {
      const dir = dirManager.resolveSkillDir('my-test-skill');
      expect(dir).toBeDefined();
      expect(dir).toContain('my-test-skill');
    });

    it('should return null for non-existent skill', () => {
      const dir = dirManager.resolveSkillDir('nonexistent_skill_xyz_abc');
      expect(dir).toBeNull();
    });
  });

  describe('exported functions', () => {
    it('should export resolveSkillDir', () => {
      expect(typeof dirManager.resolveSkillDir).toBe('function');
    });

    it('should export isCodeSkill', () => {
      expect(typeof dirManager.isCodeSkill).toBe('function');
    });

    it('should export getTargetDir', () => {
      expect(typeof dirManager.getTargetDir).toBe('function');
    });

    it('should export initSkillDir', () => {
      expect(typeof dirManager.initSkillDir).toBe('function');
    });

    it('should export ensureDataDirs', () => {
      expect(typeof dirManager.ensureDataDirs).toBe('function');
    });
  });
});