Branchwell

Become a Pro / website · becomeapro.ai / scripts / prevent-test-news.ts

Raw 2.4 KB on 5069836c1e4c4e369ebe86b13dc7d299c5bed8c0
1import * as fs from 'fs';
2import * as path from 'path';
3
4const testNewsApiPath = path.join(process.cwd(), 'scripts', 'tests', 'test-news-api.js');
5
6function main() {
7 console.log(`Modifying ${testNewsApiPath} to prevent it from creating test news items...`);
8
9 if (!fs.existsSync(testNewsApiPath)) {
10 console.error(`File not found: ${testNewsApiPath}`);
11 process.exit(1);
12 }
13
14 // Read the file
15 let content = fs.readFileSync(testNewsApiPath, 'utf8');
16
17 // Create a backup
18 const backupPath = `${testNewsApiPath}.bak`;
19 fs.writeFileSync(backupPath, content);
20 console.log(`Created backup at ${backupPath}`);
21
22 // Modify the file to disable the test functions that create, update, and delete news items
23 content = content.replace(
24 /async function testCreateNews\(\) {[\s\S]*?return data;\n } catch[\s\S]*?return null;\n }/,
25 `async function testCreateNews() {
26 console.log('\\n🧪 DISABLED: Test for creating news is disabled to prevent test data in production');
27 return null; // Disabled to prevent test data in production
28}`
29 );
30
31 content = content.replace(
32 /async function testUpdateNews\(id\) {[\s\S]*?return data;\n } catch[\s\S]*?return null;\n }/,
33 `async function testUpdateNews(id) {
34 console.log('\\n🧪 DISABLED: Test for updating news is disabled to prevent test data in production');
35 return null; // Disabled to prevent test data in production
36}`
37 );
38
39 content = content.replace(
40 /async function testDeleteNews\(id\) {[\s\S]*?return true;\n } catch[\s\S]*?return false;\n }/,
41 `async function testDeleteNews(id) {
42 console.log('\\n🧪 DISABLED: Test for deleting news is disabled to prevent test data in production');
43 return false; // Disabled to prevent test data in production
44}`
45 );
46
47 // Modify the runTests function to only test GET
48 content = content.replace(
49 /async function runTests\(\) {[\s\S]*?console\.log\('\\n🎯 All tests completed!'\);/,
50 `async function runTests() {
51 console.log('📝 Starting News API tests (READ ONLY)...');
52
53 // Test GET all news
54 await testGetAllNews();
55
56 // Skip tests that modify data
57 console.log('\\n⚠️ Tests for creating, updating, and deleting news are disabled to prevent test data in production');
58
59 console.log('\\n🎯 All tests completed!');`
60 );
61
62 // Write the modified content back to the file
63 fs.writeFileSync(testNewsApiPath, content);
64 console.log(`Modified ${testNewsApiPath} to prevent it from creating test news items`);
65}
66
67main();