smoke-cost-calibration.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. const assert = require('node:assert/strict');
  3. const fs = require('fs');
  4. const os = require('os');
  5. const path = require('path');
  6. const { buildLegacySkillPlans } = require('../mcp/src/features/voc-cost/skill-planner-adapter');
  7. const {
  8. compareEstimateToActual,
  9. appendCalibrationSample,
  10. readHistory,
  11. summarizeCalibration
  12. } = require('../mcp/src/features/voc-cost/cost-calibration');
  13. function main() {
  14. const root = path.resolve(__dirname, '..', '..', '..');
  15. const xhsConfig = path.join(root, 'openclaw-skills', 'xiaohongshu', 'xiaohongshu-mini-voc', 'api-config.json');
  16. const plans = buildLegacySkillPlans({
  17. skillConfigPath: xhsConfig,
  18. keywords: ['甲', '乙', '丙'],
  19. notesPerKeyword: 2,
  20. maxCommentPages: 2,
  21. includeUserInfo: true
  22. });
  23. assert.equal(plans.length, 3);
  24. assert.equal(plans[0].operations.find(operation => operation.path.includes('search-notes')).count, 3);
  25. assert.equal(plans[1].operations.find(operation => operation.path.includes('note-detail')).count, 6);
  26. assert.equal(plans[1].operations.find(operation => operation.path.includes('note-comments')).count, 12);
  27. assert.equal(plans[0].operations.some(operation => operation.path.includes('user-info')), false);
  28. assert.equal(plans[1].operations.some(operation => operation.path.includes('user-info')), true);
  29. const ecomPlans = buildLegacySkillPlans({
  30. skillConfig: {
  31. name: 'legacy-ecom-fixture',
  32. category: 'ecommerce',
  33. pipeline: [{ step: 1, name: '商品详情', forEach: 'products', api: { channel: 'ecommerce', endpoint: 'product.detail', method: 'GET' } }]
  34. },
  35. productCount: 2,
  36. mode: 'standard'
  37. });
  38. assert.equal(ecomPlans[0].operations[0].model, 'voc-ecom');
  39. const estimate = {
  40. workflowType: 'legacy_skill:xiaohongshu-mini-voc',
  41. selectedMode: 'standard',
  42. plans: [{
  43. mode: 'standard',
  44. expectedCny: 10,
  45. operations: [{ path: 'xhs.search', costCny: 4 }, { path: 'xhs.detail', costCny: 6 }]
  46. }]
  47. };
  48. const actual = {
  49. workflowType: estimate.workflowType,
  50. totals: { costCny: 12 },
  51. rows: [{ path: 'xhs.search', costCny: 4 }, { path: 'xhs.detail', costCny: 8 }]
  52. };
  53. const sample = compareEstimateToActual(estimate, actual);
  54. assert.equal(sample.status, 'calibrated');
  55. assert.equal(sample.deltaCny, 2);
  56. assert.equal(sample.deltaPct, 20);
  57. const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'voc-cost-calibration-'));
  58. const historyPath = path.join(tmp, 'calibration.json');
  59. appendCalibrationSample(historyPath, sample);
  60. const summary = summarizeCalibration(readHistory(historyPath));
  61. assert.equal(summary.sampleCount, 1);
  62. assert.equal(summary.actualCny, 12);
  63. console.log('legacy skill planner and cost calibration smoke passed');
  64. }
  65. try {
  66. main();
  67. } catch (error) {
  68. console.error(error.stack || error);
  69. process.exitCode = 1;
  70. }