smoke-catalog-search.js 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env node
  2. 'use strict';
  3. // 接口目录检索的离线冒烟:不发任何网络请求。
  4. // 覆盖 Agent 的常见问法(中文无空格、中文平台名、通道名当平台)、返回体积上限、
  5. // 下架接口的替代指向,以及平台索引文档是否与目录同步。
  6. const assert = require('assert');
  7. const fs = require('fs');
  8. const { searchVocApis, getVocApiDoc, callVocApi } = require('../mcp/src/tools/voc-api-catalog-run');
  9. const { listEndpoints, findEndpoint } = require('../mcp/src/core/api-catalog');
  10. const { build, OUTPUT } = require('./build-platform-index');
  11. const kb = value => Buffer.byteLength(JSON.stringify(value), 'utf8') / 1024;
  12. async function topIds(input, count = 5) {
  13. const result = await searchVocApis(input);
  14. assert.equal(result.status, 'ok');
  15. return result.data.endpoints.slice(0, count).map(item => item.id);
  16. }
  17. (async () => {
  18. // 1. 返回体积:任何检索/报错都不能把整份目录塞进上下文。
  19. const overview = await searchVocApis({});
  20. assert.equal(overview.summary.mode, 'overview');
  21. assert.ok(kb(overview) < 12, `overview too large: ${kb(overview).toFixed(1)}KB`);
  22. assert.ok(overview.assistantMessage.includes('`xiaohongshu`') && overview.assistantMessage.includes('channel=overseas'));
  23. assert.ok(kb(await searchVocApis({ platform: 'douyin', limit: 100 })) < 60);
  24. assert.ok(kb(await getVocApiDoc({})) < 2);
  25. assert.ok(kb(await getVocApiDoc({ id: 'not.exist' })) < 6);
  26. assert.ok(kb(await callVocApi({})) < 2);
  27. assert.ok(kb(await callVocApi({ id: 'douyin.serch' })) < 6);
  28. // 2. 常见问法都要能命中正确接口。
  29. assert.ok((await topIds({ query: '小红书评论' })).includes('xiaohongshu.note_comments'));
  30. assert.equal((await topIds({ query: '淘宝商品详情' }))[0], 'taobao.get_item_detail_v7');
  31. assert.ok((await topIds({ query: '抖音视频评论' })).includes('douyin.video_comments'));
  32. assert.ok((await topIds({ platform: '视频号', query: '详情' })).includes('wechat_channels.v2.fetch_video_detail'));
  33. assert.ok((await topIds({ platform: '小红书', query: '搜索' })).some(id => id.startsWith('xiaohongshu.')));
  34. assert.ok((await topIds({ platform: 'overseas' })).every(id => id.startsWith('overseas-ecom.')));
  35. assert.ok((await topIds({ platform: '亚马逊', query: '评论' })).length > 0);
  36. assert.ok((await topIds({ channel: 'ecommerce', query: '京东 评论' })).includes('jd.get_item_comments_v1'));
  37. // 3. 查不到时给出路,而不是空手而归。
  38. const pdd = await searchVocApis({ query: '拼多多' });
  39. assert.equal(pdd.summary.total, 0);
  40. assert.ok(pdd.assistantMessage.includes('京东'));
  41. const unknown = await searchVocApis({ platform: '火星' });
  42. assert.equal(unknown.summary.unknownPlatform, true);
  43. assert.ok(unknown.assistantMessage.includes('`douyin`'));
  44. const typo = await getVocApiDoc({ id: 'xiaohongshu.search' });
  45. assert.ok(typo.data.suggestions.some(item => item.id === 'xiaohongshu.app_v2.search_notes'));
  46. // 4. 下架接口:检索不出现;每条替代指向都必须是存在且在售的接口。
  47. const active = new Set(listEndpoints().filter(item => !item.deprecated).map(item => item.id));
  48. for (const endpoint of listEndpoints().filter(item => item.deprecated)) {
  49. if (endpoint.replacedBy) assert.ok(active.has(endpoint.replacedBy), `${endpoint.id} → ${endpoint.replacedBy} is not an active endpoint`);
  50. }
  51. const searched = await searchVocApis({ platform: 'weibo', limit: 100 });
  52. assert.ok(searched.data.endpoints.every(item => !item.deprecated));
  53. const retired = await getVocApiDoc({ id: 'weibo.web.fetch_search' });
  54. assert.equal(retired.summary.replacedBy, 'weibo.app.fetch_search_all');
  55. const blocked = await callVocApi({ id: 'weibo.web.fetch_search', params: { keyword: 'x' } });
  56. assert.equal(blocked.status, 'not_supported');
  57. assert.equal(blocked.summary.errorKind, 'deprecated_endpoint');
  58. // 5. 技能文档里点名的接口 id 必须真实存在且在售。
  59. const skillDoc = fs.readFileSync(require('path').resolve(__dirname, '..', 'skills', 'voc-api-catalog', 'SKILL.md'), 'utf8');
  60. const named = [...new Set([...skillDoc.matchAll(/`((?:[a-z0-9_-]+\.){1,4}[a-z0-9_]+)`/g)].map(match => match[1]))]
  61. .filter(id => /^(douyin|xiaohongshu|taobao|jd|wechat_channels|weibo|bilibili|kuaishou|zhihu|amazon|overseas-ecom|1688|youtube|tiktok)\./.test(id));
  62. assert.ok(named.length >= 20, `expected the skill doc to name concrete interface ids, got ${named.length}`);
  63. for (const id of named) {
  64. const endpoint = findEndpoint(id);
  65. assert.ok(endpoint, `skill doc names an unknown interface: ${id}`);
  66. assert.ok(!endpoint.deprecated, `skill doc names a retired interface: ${id}`);
  67. }
  68. // 6. 平台索引文档与目录同步。
  69. assert.equal(fs.readFileSync(OUTPUT, 'utf8'), build(), 'references/platforms.md is stale; run npm run catalog:index');
  70. console.log(`catalog search smoke passed (${named.length} documented ids checked)`);
  71. })().catch(error => {
  72. console.error(error);
  73. process.exit(1);
  74. });