|
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs-extra'; |
| 3 | +import { loadCustomTemplates, mergeTemplates } from '../template-loader.js'; |
| 4 | +import templates from '../templates.js'; |
| 5 | + |
| 6 | +const fixturesDir = path.join(process.cwd(), 'src', '__tests__', 'fixtures'); |
| 7 | + |
| 8 | +describe('loadCustomTemplates', () => { |
| 9 | + const exitSpy = jest.spyOn(process, 'exit').mockImplementation(((code?: number) => { |
| 10 | + throw new Error(`process.exit(${code})`); |
| 11 | + }) as () => never); |
| 12 | + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + consoleErrorSpy.mockClear(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterAll(() => { |
| 19 | + exitSpy.mockRestore(); |
| 20 | + consoleErrorSpy.mockRestore(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('loads and parses a valid template file', () => { |
| 24 | + const filePath = path.join(fixturesDir, 'valid-templates.json'); |
| 25 | + const result = loadCustomTemplates(filePath); |
| 26 | + |
| 27 | + expect(result).toHaveLength(2); |
| 28 | + expect(result[0]).toEqual({ |
| 29 | + name: 'custom-one', |
| 30 | + description: 'A custom template', |
| 31 | + repo: 'https://github.com/example/custom-one.git', |
| 32 | + }); |
| 33 | + expect(result[1]).toEqual({ |
| 34 | + name: 'custom-with-options', |
| 35 | + description: 'Custom with clone options', |
| 36 | + repo: 'https://github.com/example/custom.git', |
| 37 | + options: ['--depth', '1'], |
| 38 | + packageManager: 'pnpm', |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('exits when file does not exist', () => { |
| 43 | + const filePath = path.join(fixturesDir, 'nonexistent.json'); |
| 44 | + |
| 45 | + expect(() => loadCustomTemplates(filePath)).toThrow('process.exit(1)'); |
| 46 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 47 | + expect.stringContaining('Template file not found'), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('exits when file contains invalid JSON', async () => { |
| 52 | + const invalidPath = path.join(fixturesDir, 'invalid-json.txt'); |
| 53 | + await fs.writeFile(invalidPath, 'not valid json {'); |
| 54 | + |
| 55 | + try { |
| 56 | + expect(() => loadCustomTemplates(invalidPath)).toThrow('process.exit(1)'); |
| 57 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 58 | + expect.stringContaining('Invalid JSON'), |
| 59 | + ); |
| 60 | + } finally { |
| 61 | + await fs.remove(invalidPath); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('exits when JSON is not an array', () => { |
| 66 | + const filePath = path.join(fixturesDir, 'not-array.json'); |
| 67 | + |
| 68 | + expect(() => loadCustomTemplates(filePath)).toThrow('process.exit(1)'); |
| 69 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 70 | + expect.stringContaining('must be a JSON array'), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('exits when template is missing required name', () => { |
| 75 | + const filePath = path.join(fixturesDir, 'invalid-template-missing-name.json'); |
| 76 | + |
| 77 | + expect(() => loadCustomTemplates(filePath)).toThrow('process.exit(1)'); |
| 78 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 79 | + expect.stringContaining('"name" must be'), |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('exits when template has invalid options (non-string array)', () => { |
| 84 | + const filePath = path.join(fixturesDir, 'invalid-template-bad-options.json'); |
| 85 | + |
| 86 | + expect(() => loadCustomTemplates(filePath)).toThrow('process.exit(1)'); |
| 87 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 88 | + expect.stringContaining('"options" must be'), |
| 89 | + ); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('mergeTemplates', () => { |
| 94 | + const exitSpy = jest.spyOn(process, 'exit').mockImplementation(((code?: number) => { |
| 95 | + throw new Error(`process.exit(${code})`); |
| 96 | + }) as () => never); |
| 97 | + |
| 98 | + afterAll(() => { |
| 99 | + exitSpy.mockRestore(); |
| 100 | + }); |
| 101 | + it('returns built-in templates when no custom file path is provided', () => { |
| 102 | + const result = mergeTemplates(templates); |
| 103 | + |
| 104 | + expect(result).toEqual(templates); |
| 105 | + expect(result).toHaveLength(templates.length); |
| 106 | + }); |
| 107 | + |
| 108 | + it('returns built-in templates when custom file path is undefined', () => { |
| 109 | + const result = mergeTemplates(templates, undefined); |
| 110 | + |
| 111 | + expect(result).toEqual(templates); |
| 112 | + }); |
| 113 | + |
| 114 | + it('merges custom templates with built-in, custom overrides by name', () => { |
| 115 | + const customPath = path.join(fixturesDir, 'valid-templates.json'); |
| 116 | + const result = mergeTemplates(templates, customPath); |
| 117 | + |
| 118 | + const names = result.map((t) => t.name); |
| 119 | + expect(names).toContain('custom-one'); |
| 120 | + expect(names).toContain('custom-with-options'); |
| 121 | + |
| 122 | + const customOne = result.find((t) => t.name === 'custom-one'); |
| 123 | + expect(customOne?.repo).toBe('https://github.com/example/custom-one.git'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('overrides built-in template when custom has same name', async () => { |
| 127 | + const builtInStarter = templates.find((t) => t.name === 'starter'); |
| 128 | + expect(builtInStarter).toBeDefined(); |
| 129 | + |
| 130 | + const customPath = path.join(fixturesDir, 'override-starter.json'); |
| 131 | + await fs.writeJson(customPath, [ |
| 132 | + { |
| 133 | + name: 'starter', |
| 134 | + description: 'Overridden starter', |
| 135 | + repo: 'https://github.com/custom/overridden-starter.git', |
| 136 | + }, |
| 137 | + ]); |
| 138 | + |
| 139 | + try { |
| 140 | + const result = mergeTemplates(templates, customPath); |
| 141 | + const starter = result.find((t) => t.name === 'starter'); |
| 142 | + expect(starter?.description).toBe('Overridden starter'); |
| 143 | + expect(starter?.repo).toBe('https://github.com/custom/overridden-starter.git'); |
| 144 | + } finally { |
| 145 | + await fs.remove(customPath); |
| 146 | + } |
| 147 | + }); |
| 148 | +}); |
0 commit comments