|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { vol } from 'memfs'; |
| 3 | +import path from 'node:path'; |
| 4 | +// Import the main components module first to ensure proper initialization |
| 5 | +import '../index'; |
| 6 | +import { logsCommand } from '../command'; |
| 7 | +import { getLogsPath } from '../../../utils/filesystem'; |
| 8 | + |
| 9 | +vi.mock('node:fs'); |
| 10 | +vi.mock('node:fs/promises'); |
| 11 | + |
| 12 | +vi.spyOn(console, 'info'); |
| 13 | +vi.spyOn(console, 'log'); |
| 14 | + |
| 15 | +const LOGS_FILE_DIR = getLogsPath('logs', '12345'); |
| 16 | + |
| 17 | +const preconditions = { |
| 18 | + hasLogFiles() { |
| 19 | + vol.fromJSON({ |
| 20 | + [path.join(LOGS_FILE_DIR, 'storyblok-migrations-run-1234567890.jsonl')]: 'foo', |
| 21 | + [path.join(LOGS_FILE_DIR, 'storyblok-migrations-run-1234567891.jsonl')]: 'foo', |
| 22 | + [path.join(LOGS_FILE_DIR, 'storyblok-components-push-1234567892.jsonl')]: 'foo', |
| 23 | + }); |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +describe('logs prune command', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.resetAllMocks(); |
| 30 | + vi.clearAllMocks(); |
| 31 | + vol.reset(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should delete all logs when keep is 0 (default)', async () => { |
| 35 | + preconditions.hasLogFiles(); |
| 36 | + |
| 37 | + await logsCommand.parseAsync(['node', 'test', 'prune', '--space', '12345']); |
| 38 | + |
| 39 | + expect(console.info).toHaveBeenCalledWith( |
| 40 | + expect.stringContaining('Deleted 3 log files'), |
| 41 | + ); |
| 42 | + const remainingFiles = Object.keys(vol.toJSON()) |
| 43 | + .filter(path => path.includes('.jsonl')); |
| 44 | + expect(remainingFiles).toHaveLength(0); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should keep specified number of recent logs', async () => { |
| 48 | + preconditions.hasLogFiles(); |
| 49 | + |
| 50 | + await logsCommand.parseAsync(['node', 'test', 'prune', '--space', '12345', '--keep', '2']); |
| 51 | + |
| 52 | + expect(console.info).toHaveBeenCalledWith( |
| 53 | + expect.stringContaining('Deleted 1 log file'), |
| 54 | + ); |
| 55 | + const remainingFiles = Object.keys(vol.toJSON()) |
| 56 | + .filter(path => path.includes('.jsonl')); |
| 57 | + expect(remainingFiles).toHaveLength(2); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should not delete logs when keep count equals total', async () => { |
| 61 | + preconditions.hasLogFiles(); |
| 62 | + |
| 63 | + await logsCommand.parseAsync(['node', 'test', 'prune', '--space', '12345', '--keep', '3']); |
| 64 | + |
| 65 | + expect(console.info).toHaveBeenCalledWith( |
| 66 | + expect.stringContaining('Deleted 0 log files'), |
| 67 | + ); |
| 68 | + const remainingFiles = Object.keys(vol.toJSON()) |
| 69 | + .filter(path => path.includes('.jsonl')); |
| 70 | + expect(remainingFiles).toHaveLength(3); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should not delete logs when keep count exceeds total', async () => { |
| 74 | + preconditions.hasLogFiles(); |
| 75 | + |
| 76 | + await logsCommand.parseAsync(['node', 'test', 'prune', '--space', '12345', '--keep', '10']); |
| 77 | + |
| 78 | + expect(console.info).toHaveBeenCalledWith( |
| 79 | + expect.stringContaining('Deleted 0 log files'), |
| 80 | + ); |
| 81 | + const remainingFiles = Object.keys(vol.toJSON()) |
| 82 | + .filter(path => path.includes('.jsonl')); |
| 83 | + expect(remainingFiles).toHaveLength(3); |
| 84 | + }); |
| 85 | +}); |
0 commit comments