import { describe, expect, it } from 'vitest'; import { adapterConfigHintForPackage, classifyPackage, filterSearchResults, formatPackageBadge, isZhinEcosystemPackage, mergeSearchResults, resolveInstallRoute, ZHIN_ECOSYSTEM_KEYWORDS, runSearchQueries, zhinSearchQueries, } from '../src/utils/plugin-registry.js'; describe('classifyPackage / formatPackageBadge', () => { it('官方适配器标注 [official] [adapter]', () => { const c = classifyPackage({ name: '@zhin.js/adapter-telegram' }); expect(c).toEqual({ official: true, kind: 'adapter' }); expect(formatPackageBadge(c)).toBe('[official] [adapter]'); }); it('命中 scaffold-wizard ADAPTERS 清单的非标准命名也识别为 adapter', () => { // ADAPTERS 清单中的 package 字段与命名约定一致,这里验证 findAdapterByPackage 通路 const c = classifyPackage({ name: '@zhin.js/adapter-sandbox' }); expect(c.kind).toBe('adapter'); }); it('官方插件标注 [official] [plugin]', () => { const c = classifyPackage({ name: '@zhin.js/plugin-repeat' }); expect(c).toEqual({ official: true, kind: 'plugin' }); }); it('社区插件(zhin.js-plugin-*)标注 [community] [plugin]', () => { const c = classifyPackage({ name: 'zhin.js-plugin-rss' }); expect(c).toEqual({ official: false, kind: 'plugin' }); expect(formatPackageBadge(c)).toBe('[community] [plugin]'); }); it('带 zhin-package keyword 的包标注 skill-pack', () => { const c = classifyPackage({ name: 'some-skills', keywords: ['zhin-package'] }); expect(c).toEqual({ official: false, kind: 'skill-pack' }); }); it('普通包只标注 scope', () => { const c = classifyPackage({ name: 'lodash' }); expect(c).toEqual({ official: false, kind: null }); expect(formatPackageBadge(c)).toBe('[community]'); }); }); describe('filterSearchResults', () => { const results = [ { name: '@zhin.js/adapter-telegram', version: '1.0.0', keywords: ['zhin', 'adapter'], downloads: '300' }, { name: 'zhin.js-plugin-rss', version: '0.1.0', keywords: ['zhin', 'plugin'], downloads: '500' }, { name: 'unrelated-lib', version: '2.0.0', keywords: ['http'], downloads: '9999' }, { name: '@zhin.js/core', version: '4.0.0', keywords: ['zhin'], downloads: '100' }, ]; it('过滤掉与 zhin 无关的包并按下载量排序', () => { const filtered = filterSearchResults(results, {}); expect(filtered.map((p) => p.name)).toEqual([ 'zhin.js-plugin-rss', '@zhin.js/adapter-telegram', '@zhin.js/core', ]); }); it('按关键词过滤', () => { const filtered = filterSearchResults(results, { keyword: 'telegram' }); expect(filtered.map((p) => p.name)).toEqual(['@zhin.js/adapter-telegram']); }); it('按分类过滤', () => { const filtered = filterSearchResults(results, { category: 'adapter' }); expect(filtered.map((p) => p.name)).toEqual(['@zhin.js/adapter-telegram']); }); it('按发布清单声明的分类过滤(接受旧别名 util)', () => { const declared = [ { name: 'zhin.js-plugin-a', keywords: ['zhin'], zhin: { metadata: { category: 'utility' } } }, { name: 'zhin.js-plugin-b', keywords: ['zhin', 'utility'], zhin: { metadata: { category: 'game' } } }, ]; expect(filterSearchResults(declared, { category: 'util' }).map((p) => p.name)).toEqual(['zhin.js-plugin-a']); expect(filterSearchResults(declared, { category: 'game' }).map((p) => p.name)).toEqual(['zhin.js-plugin-b']); }); it('关键词搜索匹配本地化名称/描述,不匹配通用 keywords', () => { const packages = [ { name: 'zhin.js-plugin-a', keywords: ['zhin', 'bot', 'rss'], zhin: { metadata: { descriptionI18n: { 'zh-CN': '订阅推送' } } } }, ]; expect(filterSearchResults(packages, { keyword: '订阅' })).toHaveLength(1); expect(filterSearchResults(packages, { keyword: 'rss' })).toHaveLength(1); expect(filterSearchResults(packages, { keyword: 'bot' })).toHaveLength(0); }); it('接受文档推荐的 zhin.js / zhinjs / zhin-plugin keywords 与带 zhin 清单的包', () => { const packages = [ { name: 'community-rss', keywords: ['zhin.js', 'rss'], downloads: '40' }, { name: 'community-weather', keywords: ['ZhinJS', 'weather'], downloads: '30' }, { name: 'community-dice', keywords: ['zhin-plugin', 'game'], downloads: '20' }, { name: 'community-manifest-only', keywords: ['bot'], zhin: { protocol: 1, type: 'plugin' }, downloads: '10' }, { name: 'unrelated', keywords: ['zhin.jsx', 'react'], downloads: '999' }, { name: 'unrelated-null-manifest', keywords: [], zhin: null, downloads: '998' }, ]; expect(filterSearchResults(packages, {}).map((p) => p.name)).toEqual([ 'community-rss', 'community-weather', 'community-dice', 'community-manifest-only', ]); expect(filterSearchResults(packages, { keyword: 'rss' }).map((p) => p.name)).toEqual(['community-rss']); }); it('通用 plugin 关键词不再让无关包混进结果;官方 @zhin.js/* 按 scope 仍能找到(#1250)', () => { const packages = [ { name: '@maptiler/weather', keywords: ['maptiler', 'plugin', 'weather', 'forecast'], downloads: '9000' }, { name: '@zhin.js/plugin-weather', keywords: ['weather'], downloads: '50' }, { name: '@zhin.js/plugin-60s', keywords: [], downloads: '40' }, { name: 'community-skills', keywords: ['zhin-package', 'weather'], downloads: '10' }, ]; expect(ZHIN_ECOSYSTEM_KEYWORDS).not.toContain('plugin'); expect(isZhinEcosystemPackage(packages[0]!)).toBe(false); expect(filterSearchResults(packages, { keyword: 'weather' }).map((p) => p.name)).toEqual([ '@zhin.js/plugin-weather', 'community-skills', ]); expect(filterSearchResults(packages, {}).map((p) => p.name)).toContain('@zhin.js/plugin-60s'); }); it('搜索同时查官方 scope,并按包名去重合并', () => { expect(zhinSearchQueries('weather')).toEqual(['zhin.js weather', '@zhin.js weather']); expect(zhinSearchQueries(undefined)).toEqual(['zhin.js plugin', '@zhin.js']); expect(zhinSearchQueries('weather', true)).toEqual(['@zhin.js']); expect(mergeSearchResults([ [{ name: 'a', version: '1' }, { name: 'b', version: '1' }], [{ name: 'b', version: '2' }, { name: 'c', version: '1' }], ]).map((p) => `${p.name}@${p.version}`)).toEqual(['a@1', 'b@1', 'c@1']); }); it('一个查询失败时保留另一个查询的结果,全部失败才抛出', () => { const rateLimited = new Error('E429 Too Many Requests'); const search = (query: string) => { if (query.startsWith('@zhin.js')) throw rateLimited; return [{ name: 'zhin.js-weather', version: '1' }]; }; const partial = runSearchQueries(['zhin.js weather', '@zhin.js weather'], search); expect(partial.failed).toEqual(['@zhin.js weather']); expect(mergeSearchResults(partial.pages).map((p) => p.name)).toEqual(['zhin.js-weather']); expect(() => runSearchQueries(['@zhin.js'], search)).toThrow(rateLimited); expect(runSearchQueries(['zhin.js weather'], search).failed).toEqual([]); }); it('limit 截断结果', () => { const filtered = filterSearchResults(results, { limit: 2 }); expect(filtered).toHaveLength(2); expect(filtered[0].name).toBe('zhin.js-plugin-rss'); }); }); describe('resolveInstallRoute(a/b/c 分流)', () => { it('npm: 前缀走 ADR 0010 技能包路径', () => { const r = resolveInstallRoute('npm:@scope/skill-pack@1.0.0'); expect(r.route).toBe('skill-pack'); expect(r.package).toBe('npm:@scope/skill-pack@1.0.0'); }); it('git: 前缀走 ADR 0010 技能包路径', () => { const r = resolveInstallRoute('git:https://github.com/user/skills.git'); expect(r.route).toBe('skill-pack'); }); it('已知适配器命中 scaffold-wizard ADAPTERS(route=adapter)', () => { const r = resolveInstallRoute('@zhin.js/adapter-telegram'); expect(r.route).toBe('adapter'); expect(r.package).toBe('@zhin.js/adapter-telegram'); expect(r.adapter?.value).toBe('telegram'); }); it('带版本号的适配器 spec 也能命中', () => { const r = resolveInstallRoute('@zhin.js/adapter-qq@latest'); expect(r.route).toBe('adapter'); expect(r.package).toBe('@zhin.js/adapter-qq'); }); it('其他 npm 包走 npm 路由(zhin 字段安装后再判定)', () => { const r = resolveInstallRoute('zhin.js-plugin-rss@^1.0.0'); expect(r.route).toBe('npm'); expect(r.package).toBe('zhin.js-plugin-rss'); }); }); describe('adapterConfigHintForPackage', () => { it('已知适配器返回 zhin setup / endpoints 配置提示', () => { const hint = adapterConfigHintForPackage('@zhin.js/adapter-telegram'); expect(hint).toContain('zhin setup'); expect(hint).toContain('telegram.endpoints'); }); it('未知包返回 null', () => { expect(adapterConfigHintForPackage('lodash')).toBeNull(); }); });