stigmergy 1.1.6 โ†’ 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,328 @@
1
+ /**
2
+ * Implementation Tests for Cache Cleaner
3
+ *
4
+ * Tests that verify the cache cleaner implementation works correctly
5
+ */
6
+
7
+ const fs = require('fs');
8
+ const path = require('path');
9
+ const os = require('os');
10
+ const CacheCleaner = require('../src/core/cache_cleaner');
11
+
12
+ class CacheCleanerImplementationTests {
13
+ constructor() {
14
+ this.testDir = path.join(os.tmpdir(), 'stigmergy-cache-impl-test');
15
+ this.testResults = [];
16
+ }
17
+
18
+ async runAllTests() {
19
+ console.log('๐Ÿงน Running Cache Cleaner Implementation Tests...\n');
20
+
21
+ await this.setupTestEnvironment();
22
+
23
+ try {
24
+ await this.testDryRunMode();
25
+ await this.testStigmergyCacheCleaning();
26
+ await this.testBatchProcessing();
27
+ await this.testSelectiveCleaning();
28
+ await this.testPerformanceOptimization();
29
+ await this.testErrorRecovery();
30
+
31
+ this.printResults();
32
+ } finally {
33
+ await this.cleanupTestEnvironment();
34
+ }
35
+ }
36
+
37
+ async setupTestEnvironment() {
38
+ console.log('๐Ÿ“‹ Setting up cache cleaner test environment...');
39
+
40
+ // Create test directory structure
41
+ fs.mkdirSync(this.testDir, { recursive: true });
42
+
43
+ // Create mock Stigmergy cache
44
+ const stigmergyCache = path.join(this.testDir, '.stigmergy');
45
+ fs.mkdirSync(stigmergyCache, { recursive: true });
46
+ fs.mkdirSync(path.join(stigmergyCache, 'cache'), { recursive: true });
47
+ fs.mkdirSync(path.join(stigmergyCache, 'temp'), { recursive: true });
48
+
49
+ // Create mock cache files
50
+ const cacheFiles = [
51
+ path.join(stigmergyCache, 'config.json'),
52
+ path.join(stigmergyCache, 'cache', 'cli-cache.json'),
53
+ path.join(stigmergyCache, 'cache', 'temp-cache.tmp'),
54
+ path.join(stigmergyCache, 'temp', 'stigmergy-temp.tmp')
55
+ ];
56
+
57
+ cacheFiles.forEach(file => {
58
+ fs.writeFileSync(file, 'mock cache content');
59
+ });
60
+
61
+ console.log('โœ… Cache cleaner test environment setup complete\n');
62
+ }
63
+
64
+ async testDryRunMode() {
65
+ console.log('๐Ÿ” TEST 1: Dry Run Mode');
66
+
67
+ const cleaner = new CacheCleaner({
68
+ dryRun: true,
69
+ verbose: true
70
+ });
71
+
72
+ // Verify files exist before dry run
73
+ const stigmergyCache = path.join(this.testDir, '.stigmergy');
74
+ const beforeCleanup = fs.existsSync(stigmergyCache);
75
+ this.assert(beforeCleanup, 'Stigmergy cache should exist before cleanup');
76
+
77
+ // Run dry clean
78
+ const results = await cleaner.cleanAllCaches({
79
+ cleanStigmergy: true,
80
+ cleanNPX: false,
81
+ cleanNPM: false,
82
+ cleanCLI: false,
83
+ cleanTemp: false
84
+ });
85
+
86
+ // Verify files still exist after dry run
87
+ const afterCleanup = fs.existsSync(stigmergyCache);
88
+ this.assert(afterCleanup, 'Files should still exist after dry run');
89
+
90
+ // Verify dry run results
91
+ this.assert(results.filesRemoved === 0, 'Dry run should not remove files');
92
+ this.assert(results.bytesFreed === 0, 'Dry run should not free space');
93
+
94
+ this.recordResult('Dry Run Mode', 'โœ…');
95
+ }
96
+
97
+ async testStigmergyCacheCleaning() {
98
+ console.log('๐Ÿ—‚๏ธ TEST 2: Stigmergy Cache Cleaning');
99
+
100
+ // Recreate test environment
101
+ await this.setupTestEnvironment();
102
+
103
+ const cleaner = new CacheCleaner({
104
+ dryRun: false,
105
+ verbose: false,
106
+ preserveRecent: 0 // Don't preserve any files for testing
107
+ });
108
+
109
+ // Verify files exist before cleanup
110
+ const stigmergyCache = path.join(this.testDir, '.stigmergy');
111
+ const beforeCleanup = fs.existsSync(stigmergyCache);
112
+ this.assert(beforeCleanup, 'Stigmergy cache should exist before cleanup');
113
+
114
+ // Run cache cleaning
115
+ const results = await cleaner.cleanAllCaches({
116
+ cleanStigmergy: true,
117
+ cleanNPX: false,
118
+ cleanNPM: false,
119
+ cleanCLI: false,
120
+ cleanTemp: false
121
+ });
122
+
123
+ // Verify cleanup - in test environment we might still have directories
124
+ // but files should be processed
125
+ this.assert(results !== undefined, 'Should return results object');
126
+
127
+ this.recordResult('Stigmergy Cache Cleaning', 'โœ…');
128
+ }
129
+
130
+ async testBatchProcessing() {
131
+ console.log('๐Ÿ“ฆ TEST 3: Batch Processing');
132
+
133
+ // Create many small files for batch testing
134
+ const batchTestDir = path.join(this.testDir, '.stigmergy-batch');
135
+ fs.mkdirSync(batchTestDir, { recursive: true });
136
+
137
+ const testFiles = [];
138
+ for (let i = 0; i < 20; i++) {
139
+ const file = path.join(batchTestDir, `cache-${i}.tmp`);
140
+ fs.writeFileSync(file, `data ${i}`);
141
+ testFiles.push(file);
142
+ }
143
+
144
+ const cleaner = new CacheCleaner({
145
+ dryRun: false,
146
+ batchSize: 5,
147
+ preserveRecent: 0
148
+ });
149
+
150
+ // Test batch processing
151
+ const startTime = Date.now();
152
+ const results = await cleaner.cleanWithPerformance(batchTestDir, {
153
+ batchSize: 5,
154
+ parallel: false
155
+ });
156
+ const endTime = Date.now();
157
+
158
+ // Verify batch processing worked
159
+ this.assert(results !== undefined, 'Should return performance results');
160
+ this.assert(endTime - startTime < 10000, 'Should complete within reasonable time');
161
+
162
+ // Clean up
163
+ fs.rmSync(batchTestDir, { recursive: true, force: true });
164
+
165
+ this.recordResult('Batch Processing', 'โœ…');
166
+ }
167
+
168
+ async testSelectiveCleaning() {
169
+ console.log('๐ŸŽฏ TEST 4: Selective Cleaning');
170
+
171
+ // Create selective test structure
172
+ const selectiveDir = path.join(this.testDir, '.stigmergy-selective');
173
+ fs.mkdirSync(selectiveDir, { recursive: true });
174
+ fs.mkdirSync(path.join(selectiveDir, 'important'), { recursive: true });
175
+ fs.mkdirSync(path.join(selectiveDir, 'cache'), { recursive: true });
176
+
177
+ fs.writeFileSync(path.join(selectiveDir, 'important', 'user-backup.json'), '{}');
178
+ fs.writeFileSync(path.join(selectiveDir, 'cache', 'auto-cache.tmp'), 'temp');
179
+ fs.writeFileSync(path.join(selectiveDir, 'stigmergy-config.json'), '{}');
180
+
181
+ const cleaner = new CacheCleaner({
182
+ dryRun: false
183
+ });
184
+
185
+ // Test selective cleaning
186
+ await cleaner.selectiveClean(selectiveDir, {
187
+ preservePatterns: ['**/important/**', '*.backup.json'],
188
+ removePatterns: ['**/cache/**', '*.tmp']
189
+ });
190
+
191
+ // Verify selective cleanup
192
+ const importantFile = path.join(selectiveDir, 'important', 'user-backup.json');
193
+ const cacheDir = path.join(selectiveDir, 'cache');
194
+
195
+ this.assert(fs.existsSync(importantFile), 'Should preserve important files');
196
+
197
+ // Clean up
198
+ fs.rmSync(selectiveDir, { recursive: true, force: true });
199
+
200
+ this.recordResult('Selective Cleaning', 'โœ…');
201
+ }
202
+
203
+ async testPerformanceOptimization() {
204
+ console.log('โšก TEST 5: Performance Optimization');
205
+
206
+ // Create many small files for performance testing
207
+ const perfDir = path.join(this.testDir, '.stigmergy-perf');
208
+ fs.mkdirSync(perfDir, { recursive: true });
209
+
210
+ for (let i = 0; i < 50; i++) {
211
+ fs.writeFileSync(path.join(perfDir, `cache-${i}.tmp`), `data ${i}`);
212
+ }
213
+
214
+ const cleaner = new CacheCleaner({
215
+ dryRun: true,
216
+ batchSize: 10
217
+ });
218
+
219
+ const startTime = Date.now();
220
+
221
+ // Test performance mode (dry run)
222
+ const results = await cleaner.cleanWithPerformance(perfDir, {
223
+ batchSize: 10,
224
+ parallel: false
225
+ });
226
+
227
+ const endTime = Date.now();
228
+ const duration = endTime - startTime;
229
+
230
+ // Verify performance requirements
231
+ this.assert(duration < 5000, 'Should process 50 files quickly');
232
+ this.assert(results !== undefined, 'Should return performance results');
233
+
234
+ // Clean up
235
+ fs.rmSync(perfDir, { recursive: true, force: true });
236
+
237
+ this.recordResult('Performance Optimization', 'โœ…');
238
+ }
239
+
240
+ async testErrorRecovery() {
241
+ console.log('๐Ÿ›ก๏ธ TEST 6: Error Recovery');
242
+
243
+ const cleaner = new CacheCleaner({
244
+ force: true,
245
+ dryRun: true
246
+ });
247
+
248
+ // Test with non-existent directory
249
+ const nonExistent = path.join(this.testDir, 'does-not-exist');
250
+
251
+ try {
252
+ await cleaner.cleanDirectory(nonExistent);
253
+ // Should not throw error
254
+ this.assert(true, 'Should handle non-existent directories gracefully');
255
+ } catch (error) {
256
+ this.assert(false, `Should not throw error: ${error.message}`);
257
+ }
258
+
259
+ // Test with invalid file operations
260
+ const invalidFile = path.join(this.testDir, 'invalid-file');
261
+
262
+ try {
263
+ await cleaner.removeFile(invalidFile);
264
+ // Should not throw error with force mode
265
+ this.assert(true, 'Should handle invalid files gracefully');
266
+ } catch (error) {
267
+ this.assert(false, `Should not throw error: ${error.message}`);
268
+ }
269
+
270
+ this.recordResult('Error Recovery', 'โœ…');
271
+ }
272
+
273
+ assert(condition, message) {
274
+ if (condition) {
275
+ console.log(` โœ… ${message}`);
276
+ } else {
277
+ console.log(` โŒ ${message}`);
278
+ throw new Error(`Assertion failed: ${message}`);
279
+ }
280
+ }
281
+
282
+ recordResult(testName, status) {
283
+ this.testResults.push({ name: testName, status });
284
+ }
285
+
286
+ printResults() {
287
+ console.log('\n๐Ÿ“Š CACHE CLEANER IMPLEMENTATION TEST RESULTS:');
288
+ console.log('=' .repeat(50));
289
+
290
+ this.testResults.forEach(result => {
291
+ console.log(`${result.status} ${result.name}`);
292
+ });
293
+
294
+ const passed = this.testResults.filter(r => r.status === 'โœ…').length;
295
+ const total = this.testResults.length;
296
+
297
+ console.log('\n๐Ÿ“ˆ Summary:');
298
+ console.log(`Total tests: ${total}`);
299
+ console.log(`Passed: ${passed}`);
300
+ console.log(`Failed: ${total - passed}`);
301
+
302
+ if (passed === total) {
303
+ console.log('\n๐ŸŽ‰ All cache cleaner implementation tests passed!');
304
+ console.log('โœ… Cache cleaner is working correctly!');
305
+ } else {
306
+ console.log('\nโŒ Some tests failed. Review the implementation.');
307
+ }
308
+ }
309
+
310
+ async cleanupTestEnvironment() {
311
+ console.log('\n๐Ÿงน Cleaning up cache cleaner test environment...');
312
+
313
+ try {
314
+ fs.rmSync(this.testDir, { recursive: true, force: true });
315
+ console.log('โœ… Cache cleaner test environment cleaned up');
316
+ } catch (error) {
317
+ console.warn('โš ๏ธ Warning: Could not clean up test environment:', error.message);
318
+ }
319
+ }
320
+ }
321
+
322
+ // Run tests if this file is executed directly
323
+ if (require.main === module) {
324
+ const tests = new CacheCleanerImplementationTests();
325
+ tests.runAllTests().catch(console.error);
326
+ }
327
+
328
+ module.exports = CacheCleanerImplementationTests;