|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { ensureFileFrom } from './ensure-file-from.js'; |
| 6 | + |
| 7 | +vi.mock('node:fs/promises', () => ({ |
| 8 | + readFile: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +describe('ensureFileFrom', () => { |
| 12 | + it('should read template file from config directory and write to package', async () => { |
| 13 | + const mockReadFile = vi.mocked(readFile); |
| 14 | + const mockWriteFile = vi.fn(); |
| 15 | + const templateContent = 'This is template content'; |
| 16 | + |
| 17 | + mockReadFile.mockResolvedValue(templateContent); |
| 18 | + |
| 19 | + const plugin = ensureFileFrom('.eslintrc.json', 'templates/eslintrc.json'); |
| 20 | + |
| 21 | + const ctx = { |
| 22 | + workspacePath: '/workspace', |
| 23 | + packagePath: '/workspace/packages/test', |
| 24 | + packageName: 'test', |
| 25 | + packageJson: { name: 'test' }, |
| 26 | + configDirectory: '/workspace/config', |
| 27 | + isCheckMode: false, |
| 28 | + readFile: vi.fn(), |
| 29 | + writeFile: mockWriteFile, |
| 30 | + }; |
| 31 | + |
| 32 | + await plugin(ctx); |
| 33 | + |
| 34 | + expect(mockReadFile).toHaveBeenCalledWith( |
| 35 | + path.resolve('/workspace/config', 'templates/eslintrc.json'), |
| 36 | + 'utf8', |
| 37 | + ); |
| 38 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 39 | + '.eslintrc.json', |
| 40 | + templateContent, |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should handle absolute template paths correctly', async () => { |
| 45 | + const mockReadFile = vi.mocked(readFile); |
| 46 | + const mockWriteFile = vi.fn(); |
| 47 | + const templateContent = 'Template content'; |
| 48 | + |
| 49 | + mockReadFile.mockResolvedValue(templateContent); |
| 50 | + |
| 51 | + const plugin = ensureFileFrom('config.json', '../shared/config.json'); |
| 52 | + |
| 53 | + const ctx = { |
| 54 | + workspacePath: '/workspace', |
| 55 | + packagePath: '/workspace/packages/test', |
| 56 | + packageName: 'test', |
| 57 | + packageJson: { name: 'test' }, |
| 58 | + configDirectory: '/workspace/.config', |
| 59 | + isCheckMode: false, |
| 60 | + readFile: vi.fn(), |
| 61 | + writeFile: mockWriteFile, |
| 62 | + }; |
| 63 | + |
| 64 | + await plugin(ctx); |
| 65 | + |
| 66 | + expect(mockReadFile).toHaveBeenCalledWith( |
| 67 | + path.resolve('/workspace/.config', '../shared/config.json'), |
| 68 | + 'utf8', |
| 69 | + ); |
| 70 | + expect(mockWriteFile).toHaveBeenCalledWith('config.json', templateContent); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should throw error if template file cannot be read', async () => { |
| 74 | + const mockReadFile = vi.mocked(readFile); |
| 75 | + const mockWriteFile = vi.fn(); |
| 76 | + |
| 77 | + mockReadFile.mockRejectedValue(new Error('File not found')); |
| 78 | + |
| 79 | + const plugin = ensureFileFrom('output.txt', 'templates/missing.txt'); |
| 80 | + |
| 81 | + const ctx = { |
| 82 | + workspacePath: '/workspace', |
| 83 | + packagePath: '/workspace/packages/test', |
| 84 | + packageName: 'test', |
| 85 | + packageJson: { name: 'test' }, |
| 86 | + configDirectory: '/workspace/config', |
| 87 | + isCheckMode: false, |
| 88 | + readFile: vi.fn(), |
| 89 | + writeFile: mockWriteFile, |
| 90 | + }; |
| 91 | + |
| 92 | + await expect(plugin(ctx)).rejects.toThrow( |
| 93 | + 'Failed to read template file "templates/missing.txt" from config directory: File not found', |
| 94 | + ); |
| 95 | + |
| 96 | + expect(mockWriteFile).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should handle non-Error exceptions when reading template', async () => { |
| 100 | + const mockReadFile = vi.mocked(readFile); |
| 101 | + const mockWriteFile = vi.fn(); |
| 102 | + |
| 103 | + mockReadFile.mockRejectedValue('Unknown error'); |
| 104 | + |
| 105 | + const plugin = ensureFileFrom('output.txt', 'template.txt'); |
| 106 | + |
| 107 | + const ctx = { |
| 108 | + workspacePath: '/workspace', |
| 109 | + packagePath: '/workspace/packages/test', |
| 110 | + packageName: 'test', |
| 111 | + packageJson: { name: 'test' }, |
| 112 | + configDirectory: '/workspace/config', |
| 113 | + isCheckMode: false, |
| 114 | + readFile: vi.fn(), |
| 115 | + writeFile: mockWriteFile, |
| 116 | + }; |
| 117 | + |
| 118 | + await expect(plugin(ctx)).rejects.toThrow( |
| 119 | + 'Failed to read template file "template.txt" from config directory: Unknown error', |
| 120 | + ); |
| 121 | + }); |
| 122 | +}); |
0 commit comments