mock_setup.js
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/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/agents/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();