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