import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { beforeAll, describe, expect, it } from 'vitest' import { resolveSkills } from '../src/providers/claude/skills' import { AuditService } from '../src/services/catalog' import { PluginService } from '../src/services/plugins' import { SettingsService } from '../src/services/settings' import { createDb, migrateDb } from '../src/db' describe('resolveSkills', () => { const plugins = [ { name: 'agent-skills', path: '/a', skills: ['agent-skills:report-conventions'] }, { name: 'document-skills', path: '/b', skills: ['document-skills:xlsx', 'document-skills:docx'] }, ] it('all 选中全部技能并加载全部插件', () => { const r = resolveSkills('all', plugins) expect(r.skills).toHaveLength(3) expect(r.plugins.map(p => p.path)).toEqual(['/a', '/b']) }) it('不带前缀的名称指本地技能;只加载被用到的插件', () => { const r = resolveSkills(['report-conventions'], plugins) expect(r.skills).toEqual(['agent-skills:report-conventions']) expect(r.plugins.map(p => p.path)).toEqual(['/a']) }) it('忽略不存在的技能;空列表不加载任何技能', () => { expect(resolveSkills(['document-skills:pptx'], plugins).skills).toEqual([]) expect(resolveSkills([], plugins)).toEqual({ plugins: [], skills: [] }) }) }) // 用本地测试市场驱动真实的 `claude plugin` 命令(不联网、不涉及官方技能文件) describe('PluginService(真实 CLI)', () => { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'plugins-')) const mkt = path.join(tmp, 'mkt') let svc: PluginService beforeAll(() => { process.env.CLAUDE_CONFIG_DIR = path.join(tmp, 'cfg') fs.mkdirSync(path.join(mkt, '.claude-plugin'), { recursive: true }) fs.mkdirSync(path.join(mkt, 'demo/.claude-plugin'), { recursive: true }) fs.mkdirSync(path.join(mkt, 'demo/skills/hello'), { recursive: true }) fs.writeFileSync(path.join(mkt, '.claude-plugin/marketplace.json'), JSON.stringify({ name: 'test-mkt', owner: { name: 't' }, plugins: [{ name: 'demo', source: './demo', description: '测试插件' }] })) fs.writeFileSync(path.join(mkt, 'demo/.claude-plugin/plugin.json'), JSON.stringify({ name: 'demo', version: '1.0.0' })) fs.writeFileSync(path.join(mkt, 'demo/skills/hello/SKILL.md'), '---\nname: hello\ndescription: "打个招呼"\n---\nhi\n') fs.writeFileSync(path.join(mkt, 'demo/skills/hello/LICENSE.txt'), '© 2025 Anthropic, PBC. All rights reserved.\nUse is governed by your agreement with Anthropic.') const db = createDb(path.join(tmp, 'db.sqlite')) migrateDb(db) svc = new PluginService(new SettingsService(db), new AuditService(db)) }) it('添加市场 → 安装 → 列出技能与许可 → 停用 → 卸载', async () => { await svc.act({ action: 'marketplace-add', source: mkt }) let s = (await svc.act({ action: 'install', plugin: 'demo@test-mkt' })).state const p = s.installed.find(x => x.id === 'demo@test-mkt')! expect(p.enabled).toBe(true) expect(p.license.kind).toBe('restricted') expect(p.skills).toEqual([expect.objectContaining({ id: 'demo:hello', description: '打个招呼', license: 'restricted' })]) expect((await svc.loadable()).map(l => l.name)).toEqual(['demo']) s = (await svc.act({ action: 'disable', plugin: 'demo@test-mkt' })).state expect(s.installed.find(x => x.id === 'demo@test-mkt')!.enabled).toBe(false) expect(await svc.loadable()).toEqual([]) s = (await svc.act({ action: 'uninstall', plugin: 'demo@test-mkt' })).state expect(s.installed).toEqual([]) }, 120_000) it('失败的操作返回错误信息', async () => { await expect(svc.act({ action: 'install', plugin: 'nope@nowhere' })).rejects.toThrow(/not found/) }, 60_000) it('拒绝可疑参数', async () => { await expect(svc.act({ action: 'install', plugin: 'x; rm -rf /' })).rejects.toThrow(/格式不正确/) }) })