📄 test-skill.js

#!/usr/bin/env node

/**
 * QQ文件发送Skill - 测试脚本
 * 测试skill的基本功能是否正常工作
 */

import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

console.log('🧪 QQ Bot文件发送Skill测试开始\n');

// 测试1: 检查skill文件是否存在
console.log('测试1: 检查skill文件完整性');
try {
  const skillPath = '/root/.openclaw/workspace/skills/qqbot-file-sender';
  const fs = await import('fs');
  
  const requiredFiles = ['index.ts', 'package.json', 'SKILL.md', 'README.md'];
  let allExist = true;
  
  for (const file of requiredFiles) {
    const exists = fs.existsSync(join(skillPath, file));
    console.log(`  ${exists ? '✅' : '❌'} ${file}`);
    if (!exists) allExist = false;
  }
  
  if (allExist) {
    console.log('✅ 所有必需文件都存在\n');
  } else {
    console.log('❌ 部分文件缺失\n');
    process.exit(1);
  }
} catch (error) {
  console.error('❌ 检查失败:', error.message, '\n');
  process.exit(1);
}

// 测试2: 尝试加载skill模块
console.log('测试2: 加载skill模块');
try {
  const skill = await import('/root/.openclaw/workspace/skills/qqbot-file-sender/index.ts');
  
  const requiredExports = ['sendQQFile', 'sendQQImageToUser', 'sendQQImageToGroup'];
  let allExported = true;
  
  for (const exp of requiredExports) {
    const hasExport = exp in skill;
    console.log(`  ${hasExport ? '✅' : '❌'} ${exp}`);
    if (!hasExport) allExported = false;
  }
  
  if (allExported) {
    console.log('✅ 所有必需函数都已导出\n');
  } else {
    console.log('❌ 部分函数未导出\n');
    process.exit(1);
  }
} catch (error) {
  console.error('❌ 加载失败:', error.message, '\n');
  console.error('这可能是由于QQ Bot扩展未安装或配置不正确\n');
  process.exit(1);
}

// 测试3: 测试配置文件读取
console.log('测试3: 检查QQ Bot配置');
try {
  const fs = await import('fs');
  const path = await import('path');
  const os = await import('os');
  
  const configPath = path.join(os.homedir(), '.openclaw', 'openclaw.json');
  
  if (!fs.existsSync(configPath)) {
    console.log('  ⚠️  配置文件不存在:', configPath);
    console.log('  ℹ️  请先配置QQ Bot:');
    console.log('     openclaw channels add --channel qqbot --token "AppID:AppSecret"\n');
  } else {
    const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
    const qqbotConfig = config.channels?.qqbot;
    
    if (qqbotConfig?.appId && qqbotConfig?.clientSecret) {
      console.log(`  ✅ QQ Bot已配置 (AppID: ${qqbotConfig.appId})\n`);
    } else {
      console.log('  ⚠️  QQ Bot配置不完整\n');
    }
  }
} catch (error) {
  console.log('  ⚠️  无法读取配置:', error.message, '\n');
}

// 测试4: 测试工具函数
console.log('测试4: 测试工具函数');
try {
  const skill = await import('/root/.openclaw/workspace/skills/qqbot-file-sender/index.ts');
  
  // 测试文件检查函数
  const testFile = '/etc/passwd';  // 使用系统中肯定存在的文件
  const exists = skill.checkFileExists(testFile);
  console.log(`  ${exists ? '✅' : '❌'} checkFileExists() - 文件存在性检查`);
  
  if (exists) {
    const size = skill.getFileSize(testFile);
    const formatted = skill.formatFileSize(size || 0);
    console.log(`  ✅ getFileSize() / formatFileSize() - 文件大小: ${formatted}`);
  }
  
  console.log('✅ 工具函数测试通过\n');
} catch (error) {
  console.error('❌ 工具函数测试失败:', error.message, '\n');
  process.exit(1);
}

console.log('═════════════════════════════════════');
console.log('🎉 所有测试通过!Skill已正确安装');
console.log('═════════════════════════════════════\n');

console.log('下一步操作:');
console.log('1. 配置QQ Bot(如未配置):');
console.log('   openclaw channels add --channel qqbot --token "AppID:AppSecret"');
console.log('\n2. 使用示例:');
console.log('   - 查看示例: cat /root/.openclaw/workspace/skills/qqbot-file-sender/examples.ts');
console.log('   - 快速API: sendQQFile(), sendQQImageToUser(), sendQQImageToGroup()');
console.log('\n3. 发送测试图片:');
console.log('   准备一张图片,然后调用sendQQImageToUser()函数\n');