plugins.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import fs from 'node:fs'
  2. import os from 'node:os'
  3. import path from 'node:path'
  4. import { beforeAll, describe, expect, it } from 'vitest'
  5. import { resolveSkills } from '../src/providers/claude/skills'
  6. import { AuditService } from '../src/services/catalog'
  7. import { PluginService } from '../src/services/plugins'
  8. import { SettingsService } from '../src/services/settings'
  9. import { createDb, migrateDb } from '../src/db'
  10. describe('resolveSkills', () => {
  11. const plugins = [
  12. { name: 'agent-skills', path: '/a', skills: [{ id: 'agent-skills:report-conventions', loadId: 'agent-skills:report-conventions' }] },
  13. // 官方仓库按路径加载时,CLI 以目录名作为前缀
  14. { name: 'dbd4588f9e10', path: '/b', skills: [{ id: 'document-skills:xlsx', loadId: 'dbd4588f9e10:xlsx' }, { id: 'document-skills:docx', loadId: 'dbd4588f9e10:docx' }] },
  15. ]
  16. it('all 选中全部技能并加载全部插件,输出 CLI 使用的 ID', () => {
  17. const r = resolveSkills('all', plugins)
  18. expect(r.skills).toEqual(['agent-skills:report-conventions', 'dbd4588f9e10:xlsx', 'dbd4588f9e10:docx'])
  19. expect(r.plugins.map(p => p.path)).toEqual(['/a', '/b'])
  20. })
  21. it('按展示 ID 选择;不带前缀的名称指本地技能;只加载被用到的插件', () => {
  22. expect(resolveSkills(['report-conventions'], plugins)).toEqual({ plugins: [{ type: 'local', path: '/a' }], skills: ['agent-skills:report-conventions'] })
  23. expect(resolveSkills(['document-skills:xlsx'], plugins)).toEqual({ plugins: [{ type: 'local', path: '/b' }], skills: ['dbd4588f9e10:xlsx'] })
  24. })
  25. it('忽略不存在的技能;空列表不加载任何技能', () => {
  26. expect(resolveSkills(['document-skills:pptx'], plugins).skills).toEqual([])
  27. expect(resolveSkills([], plugins)).toEqual({ plugins: [], skills: [] })
  28. })
  29. })
  30. // 用本地测试市场驱动真实的 `claude plugin` 命令(不联网、不涉及官方技能文件)
  31. describe('PluginService(真实 CLI)', () => {
  32. const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'plugins-'))
  33. const mkt = path.join(tmp, 'mkt')
  34. let svc: PluginService
  35. beforeAll(() => {
  36. process.env.CLAUDE_CONFIG_DIR = path.join(tmp, 'cfg')
  37. fs.mkdirSync(path.join(mkt, '.claude-plugin'), { recursive: true })
  38. fs.mkdirSync(path.join(mkt, 'demo/.claude-plugin'), { recursive: true })
  39. fs.mkdirSync(path.join(mkt, 'demo/skills/hello'), { recursive: true })
  40. fs.writeFileSync(path.join(mkt, '.claude-plugin/marketplace.json'), JSON.stringify({ name: 'test-mkt', owner: { name: 't' }, plugins: [{ name: 'demo', source: './demo', description: '测试插件' }] }))
  41. fs.writeFileSync(path.join(mkt, 'demo/.claude-plugin/plugin.json'), JSON.stringify({ name: 'demo', version: '1.0.0' }))
  42. fs.writeFileSync(path.join(mkt, 'demo/skills/hello/SKILL.md'), '---\nname: hello\ndescription: "打个招呼"\n---\nhi\n')
  43. fs.writeFileSync(path.join(mkt, 'demo/skills/hello/LICENSE.txt'), 'LICENSE: Use is governed by your agreement with\nAnthropic regarding use of the services.')
  44. const db = createDb(path.join(tmp, 'db.sqlite'))
  45. migrateDb(db)
  46. svc = new PluginService(new SettingsService(db), new AuditService(db))
  47. })
  48. it('添加市场 → 安装 → 列出技能与许可 → 停用 → 卸载', async () => {
  49. await svc.act({ action: 'marketplace-add', source: mkt })
  50. let s = (await svc.act({ action: 'install', plugin: 'demo@test-mkt' })).state
  51. const p = s.installed.find(x => x.id === 'demo@test-mkt')!
  52. expect(p.enabled).toBe(true)
  53. expect(p.license.kind).toBe('restricted')
  54. expect(p.skills).toEqual([expect.objectContaining({ id: 'demo:hello', description: '打个招呼', license: 'restricted' })])
  55. expect((await svc.loadable()).map(l => l.name)).toEqual(['demo'])
  56. expect((await svc.loadable())[0]!.skills).toEqual([{ id: 'demo:hello', loadId: 'demo:hello' }])
  57. s = (await svc.act({ action: 'disable', plugin: 'demo@test-mkt' })).state
  58. expect(s.installed.find(x => x.id === 'demo@test-mkt')!.enabled).toBe(false)
  59. expect(await svc.loadable()).toEqual([])
  60. s = (await svc.act({ action: 'uninstall', plugin: 'demo@test-mkt' })).state
  61. expect(s.installed).toEqual([])
  62. }, 120_000)
  63. it('官方仓库式布局:多个插件共用目录、没有 plugin.json、只声明部分技能', async () => {
  64. const repo = path.join(tmp, 'repo')
  65. for (const k of ['xlsx', 'docx', 'art']) {
  66. fs.mkdirSync(path.join(repo, 'skills', k), { recursive: true })
  67. fs.writeFileSync(path.join(repo, 'skills', k, 'SKILL.md'), `---\nname: ${k}\ndescription: ${k}\n---\n`)
  68. }
  69. fs.mkdirSync(path.join(repo, '.claude-plugin'), { recursive: true })
  70. fs.writeFileSync(path.join(repo, '.claude-plugin/marketplace.json'), JSON.stringify({
  71. name: 'repo-mkt', owner: { name: 't' },
  72. plugins: [
  73. { name: 'doc-skills', source: './', strict: false, skills: ['./skills/xlsx', './skills/docx'] },
  74. { name: 'example', source: './', strict: false, skills: ['./skills/art'] },
  75. ],
  76. }))
  77. await svc.act({ action: 'marketplace-add', source: repo })
  78. const s = (await svc.act({ action: 'install', plugin: 'doc-skills@repo-mkt' })).state
  79. const p = s.installed.find(x => x.id === 'doc-skills@repo-mkt')!
  80. expect(p.skills.map(k => k.id)).toEqual(['doc-skills:docx', 'doc-skills:xlsx'])
  81. const l = (await svc.loadable()).find(x => x.path === p.installPath)!
  82. expect(l.name).toBe(path.basename(p.installPath))
  83. expect(l.skills.map(k => k.loadId)).toEqual([`${l.name}:docx`, `${l.name}:xlsx`])
  84. await svc.act({ action: 'uninstall', plugin: 'doc-skills@repo-mkt' })
  85. }, 120_000)
  86. it('失败的操作返回错误信息', async () => {
  87. await expect(svc.act({ action: 'install', plugin: 'nope@nowhere' })).rejects.toThrow(/not found/)
  88. }, 60_000)
  89. it('拒绝可疑参数', async () => {
  90. await expect(svc.act({ action: 'install', plugin: 'x; rm -rf /' })).rejects.toThrow(/格式不正确/)
  91. })
  92. })