|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | +const path = require('path'); |
| 5 | +const { listBakedInTemplates, isCoreTemplate, getTemplate } = require('../lib/templates/bakedInTemplates'); |
| 6 | +const templates = require('../lib/templates/BakedInTemplatesList.json'); |
| 7 | + |
| 8 | +describe('bakedInTemplates', () => { |
| 9 | + describe('listBakedInTemplates', () => { |
| 10 | + it('returns all templates when no filter is provided', () => { |
| 11 | + const result = listBakedInTemplates(); |
| 12 | + expect(result).toHaveLength(templates.length); |
| 13 | + expect(result).toEqual(templates); |
| 14 | + }); |
| 15 | + |
| 16 | + it('returns all templates when filter is undefined', () => { |
| 17 | + const result = listBakedInTemplates(undefined); |
| 18 | + expect(result).toHaveLength(templates.length); |
| 19 | + expect(result).toEqual(templates); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns all templates when filter is empty object', () => { |
| 23 | + const result = listBakedInTemplates({}); |
| 24 | + expect(result).toHaveLength(templates.length); |
| 25 | + expect(result).toEqual(templates); |
| 26 | + }); |
| 27 | + |
| 28 | + it('filters by type correctly', () => { |
| 29 | + const result = listBakedInTemplates({ type: 'client' }); |
| 30 | + expect(result.length).toBeGreaterThan(0); |
| 31 | + expect(result.every(t => t.type === 'client')).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns empty array when type filter matches nothing', () => { |
| 35 | + const result = listBakedInTemplates({ type: 'nonexistent' }); |
| 36 | + expect(result).toEqual([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('filters by stack correctly', () => { |
| 40 | + const result = listBakedInTemplates({ stack: 'quarkus' }); |
| 41 | + expect(result.length).toBeGreaterThan(0); |
| 42 | + expect(result.every(t => t.stack === 'quarkus')).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns empty array when stack filter matches nothing', () => { |
| 46 | + const result = listBakedInTemplates({ stack: 'nonexistent' }); |
| 47 | + expect(result).toEqual([]); |
| 48 | + }); |
| 49 | + |
| 50 | + it('filters by protocol correctly', () => { |
| 51 | + const result = listBakedInTemplates({ protocol: 'websocket' }); |
| 52 | + expect(result.length).toBeGreaterThan(0); |
| 53 | + expect(result.every(t => t.protocol === 'websocket')).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns empty array when protocol filter matches nothing', () => { |
| 57 | + const result = listBakedInTemplates({ protocol: 'nonexistent' }); |
| 58 | + expect(result).toEqual([]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('filters by target correctly', () => { |
| 62 | + const result = listBakedInTemplates({ target: 'javascript' }); |
| 63 | + expect(result.length).toBeGreaterThan(0); |
| 64 | + expect(result.every(t => t.target === 'javascript')).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns empty array when target filter matches nothing', () => { |
| 68 | + const result = listBakedInTemplates({ target: 'nonexistent' }); |
| 69 | + expect(result).toEqual([]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('filters by combined type and protocol', () => { |
| 73 | + const result = listBakedInTemplates({ type: 'client', protocol: 'websocket' }); |
| 74 | + expect(result.length).toBeGreaterThan(0); |
| 75 | + expect(result.every(t => t.type === 'client' && t.protocol === 'websocket')).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('filters by combined type, protocol, and target', () => { |
| 79 | + const result = listBakedInTemplates({ type: 'client', protocol: 'websocket', target: 'javascript' }); |
| 80 | + expect(result.length).toBeGreaterThan(0); |
| 81 | + expect(result.every(t => |
| 82 | + t.type === 'client' && |
| 83 | + t.protocol === 'websocket' && |
| 84 | + t.target === 'javascript' |
| 85 | + )).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('filters by combined type, protocol, target, and stack', () => { |
| 89 | + const result = listBakedInTemplates({ |
| 90 | + type: 'client', |
| 91 | + protocol: 'websocket', |
| 92 | + target: 'java', |
| 93 | + stack: 'quarkus' |
| 94 | + }); |
| 95 | + expect(result.length).toBeGreaterThan(0); |
| 96 | + expect(result.every(t => |
| 97 | + t.type === 'client' && |
| 98 | + t.protocol === 'websocket' && |
| 99 | + t.target === 'java' && |
| 100 | + t.stack === 'quarkus' |
| 101 | + )).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('returns empty array when combined filters match nothing', () => { |
| 105 | + const result = listBakedInTemplates({ |
| 106 | + type: 'client', |
| 107 | + protocol: 'nonexistent' |
| 108 | + }); |
| 109 | + expect(result).toEqual([]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('handles templates without optional stack property', () => { |
| 113 | + const templatesWithoutStack = templates.filter(t => !t.stack); |
| 114 | + if (templatesWithoutStack.length > 0) { |
| 115 | + const result = listBakedInTemplates({ stack: 'quarkus' }); |
| 116 | + // Should not include templates without stack property |
| 117 | + expect(result.every(t => t.stack === 'quarkus')).toBe(true); |
| 118 | + } |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('isCoreTemplate', () => { |
| 123 | + it('returns true for existing template name', () => { |
| 124 | + const existingTemplate = templates[0]; |
| 125 | + expect(isCoreTemplate(existingTemplate.name)).toBe(true); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns true for all templates in the list', () => { |
| 129 | + templates.forEach(template => { |
| 130 | + expect(isCoreTemplate(template.name)).toBe(true); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('returns false for non-existing template name', () => { |
| 135 | + expect(isCoreTemplate('nonexistent-template')).toBe(false); |
| 136 | + }); |
| 137 | + |
| 138 | + it('returns false for empty string', () => { |
| 139 | + expect(isCoreTemplate('')).toBe(false); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns false for null', () => { |
| 143 | + expect(isCoreTemplate(null)).toBe(false); |
| 144 | + }); |
| 145 | + |
| 146 | + it('returns false for undefined', () => { |
| 147 | + expect(isCoreTemplate(undefined)).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + it('is case-sensitive', () => { |
| 151 | + const existingTemplate = templates[0]; |
| 152 | + const caseVariation = existingTemplate.name.toUpperCase(); |
| 153 | + if (caseVariation !== existingTemplate.name) { |
| 154 | + expect(isCoreTemplate(caseVariation)).toBe(false); |
| 155 | + } |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('getTemplate', () => { |
| 160 | + it('returns template object with name and path for existing template', async () => { |
| 161 | + const existingTemplate = templates[0]; |
| 162 | + const result = await getTemplate(existingTemplate.name); |
| 163 | + |
| 164 | + expect(result).toBeDefined(); |
| 165 | + expect(result.name).toBe(existingTemplate.name); |
| 166 | + expect(result.path).toBeDefined(); |
| 167 | + expect(typeof result.path).toBe('string'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('uses template path when available', async () => { |
| 171 | + const templateWithPath = templates.find(t => t.path); |
| 172 | + if (templateWithPath) { |
| 173 | + const result = await getTemplate(templateWithPath.name); |
| 174 | + expect(result.path).toBe(templateWithPath.path); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + it('falls back to bakedInTemplates directory when path is not provided', async () => { |
| 179 | + const templateWithoutPath = templates.find(t => !t.path); |
| 180 | + if (templateWithoutPath) { |
| 181 | + const result = await getTemplate(templateWithoutPath.name); |
| 182 | + const expectedPath = path.resolve( |
| 183 | + __dirname, |
| 184 | + '../lib/templates/bakedInTemplates', |
| 185 | + templateWithoutPath.name |
| 186 | + ); |
| 187 | + expect(result.path).toBe(expectedPath); |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + it('handles all templates in the list', async () => { |
| 192 | + for (const template of templates) { |
| 193 | + const result = await getTemplate(template.name); |
| 194 | + expect(result).toBeDefined(); |
| 195 | + expect(result.name).toBe(template.name); |
| 196 | + expect(result.path).toBeDefined(); |
| 197 | + expect(typeof result.path).toBe('string'); |
| 198 | + } |
| 199 | + }); |
| 200 | + |
| 201 | + it('throws error for non-existing template', async () => { |
| 202 | + // Current implementation throws when template.name is accessed on undefined |
| 203 | + await expect(getTemplate('nonexistent-template')).rejects.toThrow(); |
| 204 | + }); |
| 205 | + |
| 206 | + it('throws error for empty string template name', async () => { |
| 207 | + // Current implementation will throw when template is not found |
| 208 | + await expect(getTemplate('')).rejects.toThrow(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('throws error for null template name', async () => { |
| 212 | + // Current implementation will throw when template is not found |
| 213 | + await expect(getTemplate(null)).rejects.toThrow(); |
| 214 | + }); |
| 215 | + |
| 216 | + it('throws error for undefined template name', async () => { |
| 217 | + // Current implementation will throw when template is not found |
| 218 | + await expect(getTemplate(undefined)).rejects.toThrow(); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments