#!/usr/bin/env node const assert = require('assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); async function main() { const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-official-cli-test-')); const runtimeRoot = path.join(tempRoot, 'runtime'); const configDir = path.join(tempRoot, 'config'); process.env.QIWEI_OFFICIAL_CLI_RUNTIME_DIR = runtimeRoot; process.env.WECOM_CLI_CONFIG_DIR = configDir; const runtime = require('../mcp/src/core/wecom-cli-runtime'); const provider = require('../mcp/src/providers/wecom-official-cli'); try { const emptyStatus = await runtime.getOfficialCliStatus(); assert.strictEqual(emptyStatus.installed, false); assert.strictEqual(emptyStatus.ready, false); const packageDir = path.join(runtime.getRuntimeDir(), 'node_modules', '@wecom', 'cli'); fs.mkdirSync(path.join(packageDir, 'bin'), { recursive: true }); fs.writeFileSync( path.join(packageDir, 'package.json'), JSON.stringify({ name: runtime.runtimeManifest.package, version: runtime.runtimeManifest.version }) ); fs.writeFileSync( path.join(packageDir, 'bin', 'wecom.js'), [ "const args = process.argv.slice(2);", "if (args.join(' ') === 'auth show --auth-status') { console.log('authorized'); process.exit(0); }", "if (args.at(-1) === '--help') { console.log(`help:${args.slice(0, -1).join('.') || 'root'}`); process.exit(0); }", "const jsonIndex = args.indexOf('--json');", "const input = jsonIndex >= 0 ? JSON.parse(args[jsonIndex + 1]) : {};", "console.log(JSON.stringify({ errcode: 0, category: args[0], method: args[1], input, bot_secret: 'server-only-secret', accessToken: 'server-only-token', providerName: 'private-provider', doc_url: 'https://docs.example.test/document/1' }));" ].join('\n') ); fs.mkdirSync(configDir, { recursive: true }); fs.writeFileSync(path.join(configDir, 'bot.enc'), 'test'); fs.writeFileSync(path.join(configDir, 'mcp_config.enc'), 'test'); const readyStatus = await runtime.getOfficialCliStatus(); assert.strictEqual(readyStatus.valid, true); assert.strictEqual(readyStatus.authorized, true); assert.strictEqual(readyStatus.ready, true); const help = await provider.qiweiOfficialHelp({ category: 'meeting', method: 'create_meeting' }); assert.strictEqual(help.status, 'ok'); assert.strictEqual(help.data.help, 'help:meeting.create_meeting'); const call = await provider.qiweiOfficialCall({ category: 'doc', method: 'create_doc', args: { doc_type: 3, doc_name: '测试文档; echo unsafe' } }); assert.strictEqual(call.status, 'ok'); assert.strictEqual(call.data.response.category, 'doc'); assert.strictEqual(call.data.response.method, 'create_doc'); assert.strictEqual(call.data.response.input.doc_name, '测试文档; echo unsafe'); assert(!Object.hasOwn(call.data.response, 'bot_secret')); assert(!Object.hasOwn(call.data.response, 'accessToken')); assert(!Object.hasOwn(call.data.response, 'providerName')); assert.strictEqual(call.data.response.doc_url, 'https://docs.example.test/document/1'); const invalidMethod = await provider.qiweiOfficialCall({ category: 'doc', method: 'create_doc; echo unsafe', args: {} }); assert.strictEqual(invalidMethod.status, 'error'); console.log('[ok] official CLI runtime status, structured help and shell-safe calls'); } finally { fs.rmSync(tempRoot, { recursive: true, force: true }); } } main().catch(error => { console.error('official CLI smoke test failed:', error); process.exit(1); });