#!/usr/bin/env node /** * mock-env - Mock 测试环境搭建脚本 * Agent: evolver-youtube-crawler */ const fs = require('fs'); const path = require('path'); function parseArgs() { const args = process.argv.slice(2); const opts = { outputDir: '../../../../../system/templates/youtube-crawler/workspace/evolve/mock/', cases: 10 }; for (let i = 0; i < args.length; i++) { if (args[i] === '--output-dir') opts.outputDir = args[i + 1]; if (args[i] === '--cases') opts.cases = parseInt(args[i + 1], 10); } return opts; } function ensureDir(dir) { if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); } const MOCKS = { 'trending_us.json': { items: [{ id: 'mock_video_1', snippet: { title: 'Mock Video US 1', channelTitle: 'Mock Channel', thumbnails: { default: { url: 'https://i.ytimg.com/vi/mock1/default.jpg' } }, publishedAt: '2026-04-01T00:00:00Z', categoryId: '10' }, statistics: { viewCount: '1234567' } }], nextPageToken: null, pageInfo: { totalResults: 1 } }, 'trending_jp.json': { items: [{ id: 'mock_video_jp_1', snippet: { title: 'Mock Video JP 1', channelTitle: 'JP Channel', thumbnails: { medium: { url: 'https://i.ytimg.com/vi/mockjp1/medium.jpg' } }, publishedAt: '2026-04-01T01:00:00Z', categoryId: '20' }, statistics: { viewCount: '987654' } }], nextPageToken: null, pageInfo: { totalResults: 1 } }, 'api_quota_exceeded.json': { error: { code: 403, message: 'quotaExceeded', errors: [{ domain: 'youtube.quota', reason: 'quotaExceeded' }] } }, 'api_invalid_key.json': { error: { code: 400, message: 'API key not valid', errors: [{ domain: 'global', reason: 'badRequest' }] } }, 'rate_limit_429.json': { error: { code: 429, message: 'Rate limit exceeded', errors: [{ domain: 'global', reason: 'rateLimitExceeded' }] } }, 'ytdlp_fallback.json': { entries: [{ id: 'ytdlp_mock_1', title: 'YT-DLP Mock Video', uploader: 'YT-DLP Channel', webpage_url: 'https://youtube.com/watch?v=ytdlp_mock_1', view_count: 555555, thumbnail: 'https://i.ytimg.com/vi/ytdlp_mock_1/default.jpg', upload_date: '20260401' }] }, 'empty_results.json': { items: [], nextPageToken: null, pageInfo: { totalResults: 0 } }, 'partial_data.json': { items: [{ id: 'partial_1', snippet: { title: 'Partial Title' }, statistics: {} }] }, 'network_timeout.txt': 'ETIMEDOUT', 'malformed_response.txt': '{"items": [invalid json here' }; function main() { const opts = parseArgs(); const outDir = path.resolve(__dirname, opts.outputDir); ensureDir(outDir); console.log(`Mock environment setup: ${outDir}`); for (const [filename, data] of Object.entries(MOCKS)) { const filepath = path.join(outDir, filename); if (typeof data === 'string') fs.writeFileSync(filepath, data, 'utf-8'); else fs.writeFileSync(filepath, JSON.stringify(data, null, 2), 'utf-8'); console.log(` Created: ${filename}`); } const runnerPath = path.join(outDir, 'mock_runner.js'); const runnerCode = `const fs = require('fs');\nconst path = require('path');\nfunction loadMock(name) {\n const p = path.join(__dirname, name);\n if (!fs.existsSync(p)) return null;\n const raw = fs.readFileSync(p, 'utf-8');\n try { return JSON.parse(raw); } catch { return raw; }\n}\nmodule.exports = { loadMock };\n`; fs.writeFileSync(runnerPath, runnerCode, 'utf-8'); console.log(` Created: mock_runner.js`); console.log('Mock environment setup complete.'); } main();