Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { createMemoryEnvironment } from '../src/environment.js'
4import { createTemplateFile } from '../src/template-file.js'
5
6import type { AddOn, Options } from '../src/types.js'
7
8const simpleOptions = {
9 projectName: 'test',
10 targetDir: '/test',
11 framework: {
12 id: 'test',
13 name: 'Test',
14 },
15 chosenAddOns: [],
16 packageManager: 'pnpm',
17 typescript: true,
18 tailwind: true,
19 mode: 'file-router',
20 addOnOptions: {},
21} as unknown as Options
22
23describe('Template Context - Add-on Options', () => {
24 it('should provide addOnOption context variable', async () => {
25 const { environment, output } = createMemoryEnvironment()
26 const templateFile = createTemplateFile(environment, {
27 ...simpleOptions,
28 addOnOptions: {
29 testAddon: {
30 database: 'postgres'
31 }
32 }
33 })
34 environment.startRun()
35 await templateFile('test.txt.ejs', 'Database: <%= addOnOption.testAddon.database %>')
36 environment.finishRun()
37
38 expect(output.files['/test/test.txt']).toEqual('Database: postgres')
39 })
40
41 it('should handle multiple add-on options', async () => {
42 const { environment, output } = createMemoryEnvironment()
43 const templateFile = createTemplateFile(environment, {
44 ...simpleOptions,
45 addOnOptions: {
46 testAddon: {
47 database: 'mysql'
48 },
49 shadcn: {
50 theme: 'slate'
51 }
52 }
53 })
54 environment.startRun()
55 await templateFile(
56 'test.txt.ejs',
57 'Drizzle: <%= addOnOption.testAddon.database %>, shadcn: <%= addOnOption.shadcn.theme %>'
58 )
59 environment.finishRun()
60
61 expect(output.files['/test/test.txt']).toEqual('Drizzle: mysql, shadcn: slate')
62 })
63
64 it('should handle multiple options per add-on', async () => {
65 const { environment, output } = createMemoryEnvironment()
66 const templateFile = createTemplateFile(environment, {
67 ...simpleOptions,
68 addOnOptions: {
69 'complex-addon': {
70 database: 'postgres',
71 theme: 'dark',
72 port: 5432
73 }
74 }
75 })
76 environment.startRun()
77 await templateFile(
78 'test.txt.ejs',
79 'DB: <%= addOnOption["complex-addon"].database %>, Theme: <%= addOnOption["complex-addon"].theme %>, Port: <%= addOnOption["complex-addon"].port %>'
80 )
81 environment.finishRun()
82
83 expect(output.files['/test/test.txt']).toEqual('DB: postgres, Theme: dark, Port: 5432')
84 })
85
86 it('should handle conditional logic with addOnOption', async () => {
87 const { environment, output } = createMemoryEnvironment()
88 const templateFile = createTemplateFile(environment, {
89 ...simpleOptions,
90 addOnOptions: {
91 testAddon: {
92 database: 'postgres'
93 }
94 }
95 })
96 environment.startRun()
97 await templateFile(
98 'test.txt.ejs',
99 `<% if (addOnOption.testAddon.database === 'postgres') { %>
100PostgreSQL configuration
101<% } else if (addOnOption.testAddon.database === 'mysql') { %>
102MySQL configuration
103<% } else { %>
104SQLite configuration
105<% } %>`
106 )
107 environment.finishRun()
108
109 expect(output.files['/test/test.txt'].trim()).toEqual('PostgreSQL configuration')
110 })
111
112 it('should handle ignoreFile() with option conditions', async () => {
113 const { environment, output } = createMemoryEnvironment()
114 const templateFile = createTemplateFile(environment, {
115 ...simpleOptions,
116 addOnOptions: {
117 testAddon: {
118 database: 'postgres'
119 }
120 }
121 })
122 environment.startRun()
123 await templateFile(
124 'postgres-config.ts.ejs',
125 '<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\n// PostgreSQL configuration\nexport const config = "postgres"'
126 )
127 await templateFile(
128 'mysql-config.ts.ejs',
129 '<% if (addOnOption.testAddon.database !== "mysql") { ignoreFile() } %>\n// MySQL configuration\nexport const config = "mysql"'
130 )
131 environment.finishRun()
132
133 expect(output.files['/test/postgres-config.ts']).toBeDefined()
134 expect(output.files['/test/postgres-config.ts'].trim()).toEqual('// PostgreSQL configuration\nexport const config = \'postgres\'')
135 expect(output.files['/test/mysql-config.ts']).toBeUndefined()
136 })
137
138 it('should handle empty addOnOptions', async () => {
139 const { environment, output } = createMemoryEnvironment()
140 const templateFile = createTemplateFile(environment, {
141 ...simpleOptions,
142 addOnOptions: {}
143 })
144 environment.startRun()
145 await templateFile(
146 'test.txt.ejs',
147 'Options: <%= JSON.stringify(addOnOption) %>'
148 )
149 environment.finishRun()
150
151 expect(output.files['/test/test.txt']).toEqual('Options: {}')
152 })
153
154 it('should handle undefined option values', async () => {
155 const { environment, output } = createMemoryEnvironment()
156 const templateFile = createTemplateFile(environment, {
157 ...simpleOptions,
158 addOnOptions: {
159 testAddon: {
160 database: undefined
161 }
162 }
163 })
164 environment.startRun()
165 await templateFile(
166 'test.txt.ejs',
167 'Database: <%= addOnOption.testAddon.database || "not set" %>'
168 )
169 environment.finishRun()
170
171 expect(output.files['/test/test.txt']).toEqual('Database: not set')
172 })
173
174 it('should work alongside existing template variables', async () => {
175 const { environment, output } = createMemoryEnvironment()
176 const templateFile = createTemplateFile(environment, {
177 ...simpleOptions,
178 projectName: 'my-app',
179 chosenAddOns: [
180 {
181 id: 'testAddon',
182 name: 'Drizzle ORM',
183 } as AddOn
184 ],
185 addOnOptions: {
186 testAddon: {
187 database: 'postgres'
188 }
189 }
190 })
191 environment.startRun()
192 await templateFile(
193 'test.txt.ejs',
194 'Project: <%= projectName %>, Add-ons: <%= Object.keys(addOnEnabled).join(", ") %>, Database: <%= addOnOption.testAddon.database %>'
195 )
196 environment.finishRun()
197
198 expect(output.files['/test/test.txt']).toEqual('Project: my-app, Add-ons: testAddon, Database: postgres')
199 })
200
201 it('should handle nested object access safely', async () => {
202 const { environment, output } = createMemoryEnvironment()
203 const templateFile = createTemplateFile(environment, {
204 ...simpleOptions,
205 addOnOptions: {
206 testAddon: {
207 database: 'postgres'
208 }
209 }
210 })
211 environment.startRun()
212 await templateFile(
213 'test.txt.ejs',
214 'Exists: <%= addOnOption.testAddon ? "yes" : "no" %>, Non-existent: <%= addOnOption.nonexistent ? "yes" : "no" %>'
215 )
216 environment.finishRun()
217
218 expect(output.files['/test/test.txt']).toEqual('Exists: yes, Non-existent: no')
219 })
220
221 it('should handle option-based conditional imports', async () => {
222 const { environment, output } = createMemoryEnvironment()
223 const templateFile = createTemplateFile(environment, {
224 ...simpleOptions,
225 addOnOptions: {
226 testAddon: {
227 database: 'postgres'
228 }
229 }
230 })
231 environment.startRun()
232 await templateFile(
233 'db-config.ts.ejs',
234 `<% if (addOnOption.testAddon.database === 'postgres') { %>
235import { testAddon } from 'testAddon-orm/postgres-js'
236import postgres from 'postgres'
237<% } else if (addOnOption.testAddon.database === 'mysql') { %>
238import { testAddon } from 'testAddon-orm/mysql2'
239import mysql from 'mysql2/promise'
240<% } else if (addOnOption.testAddon.database === 'sqlite') { %>
241import { testAddon } from 'testAddon-orm/better-sqlite3'
242import Database from 'better-sqlite3'
243<% } %>
244
245export const db = testAddon(/* connection */)`
246 )
247 environment.finishRun()
248
249 expect(output.files['/test/db-config.ts']).toContain("import { testAddon } from 'testAddon-orm/postgres-js'")
250 expect(output.files['/test/db-config.ts']).toContain("import postgres from 'postgres'")
251 expect(output.files['/test/db-config.ts']).not.toContain("import mysql from 'mysql2/promise'")
252 expect(output.files['/test/db-config.ts']).not.toContain("import Database from 'better-sqlite3'")
253 })
254
255 it('should handle filename prefix stripping', async () => {
256 const { environment, output } = createMemoryEnvironment()
257 const templateFile = createTemplateFile(environment, {
258 ...simpleOptions,
259 addOnOptions: {
260 testAddon: {
261 database: 'postgres'
262 }
263 }
264 })
265 environment.startRun()
266 await templateFile(
267 '__postgres__testAddon.config.ts.ejs',
268 '<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\n// PostgreSQL Drizzle config\nexport default { driver: "postgres" }'
269 )
270 await templateFile(
271 '__mysql__testAddon.config.ts.ejs',
272 '<% if (addOnOption.testAddon.database !== "mysql") { ignoreFile() } %>\n// MySQL Drizzle config\nexport default { driver: "mysql" }'
273 )
274 environment.finishRun()
275
276 // File should be created with prefix stripped
277 expect(output.files['/test/testAddon.config.ts']).toBeDefined()
278 expect(output.files['/test/testAddon.config.ts'].trim()).toEqual('// PostgreSQL Drizzle config\nexport default { driver: \'postgres\' }')
280 // Prefixed filename should not exist
281 expect(output.files['/test/__postgres__testAddon.config.ts']).toBeUndefined()
282 expect(output.files['/test/__mysql__testAddon.config.ts']).toBeUndefined()
283 })
284
285 it('should handle nested directory with prefixed files', async () => {
286 const { environment, output } = createMemoryEnvironment()
287 const templateFile = createTemplateFile(environment, {
288 ...simpleOptions,
289 addOnOptions: {
290 testAddon: {
291 database: 'sqlite'
292 }
293 }
294 })
295 environment.startRun()
296 await templateFile(
297 'src/db/__sqlite__index.ts.ejs',
298 '<% if (addOnOption.testAddon.database !== "sqlite") { ignoreFile() } %>\n// SQLite database connection\nexport const db = "sqlite"'
299 )
300 await templateFile(
301 'src/db/__postgres__index.ts.ejs',
302 '<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\n// PostgreSQL database connection\nexport const db = "postgres"'
303 )
304 environment.finishRun()
305
306 // SQLite file should be created with prefix stripped
307 expect(output.files['/test/src/db/index.ts']).toBeDefined()
308 expect(output.files['/test/src/db/index.ts'].trim()).toEqual('// SQLite database connection\nexport const db = \'sqlite\'')
310 // Prefixed filenames should not exist
311 expect(output.files['/test/src/db/__sqlite__index.ts']).toBeUndefined()
312 expect(output.files['/test/src/db/__postgres__index.ts']).toBeUndefined()
313 })
314})
315
316describe('Template Context - includeExamples', () => {
317 it('should default includeExamples to true when undefined', async () => {
318 const { environment, output } = createMemoryEnvironment()
319 const templateFile = createTemplateFile(environment, simpleOptions)
320 environment.startRun()
321 await templateFile(
322 'test.txt.ejs',
323 '<%= includeExamples ? "with-examples" : "no-examples" %>',
324 )
325 environment.finishRun()
326
327 expect(output.files['/test/test.txt']).toEqual('with-examples')
328 })
329
330 it('should set includeExamples to false when options.includeExamples is false', async () => {
331 const { environment, output } = createMemoryEnvironment()
332 const templateFile = createTemplateFile(environment, {
333 ...simpleOptions,
334 includeExamples: false,
335 })
336 environment.startRun()
337 await templateFile(
338 'test.txt.ejs',
339 '<%= includeExamples ? "with-examples" : "no-examples" %>',
340 )
341 environment.finishRun()
342
343 expect(output.files['/test/test.txt']).toEqual('no-examples')
344 })
345
346 it('should ignore files when includeExamples is false', async () => {
347 const { environment, output } = createMemoryEnvironment()
348 const templateFile = createTemplateFile(environment, {
349 ...simpleOptions,
350 includeExamples: false,
351 })
352 environment.startRun()
353 await templateFile(
354 'about.ts.ejs',
355 '<% if (!includeExamples) { ignoreFile(); return; } %>\nexport const about = true',
356 )
357 environment.finishRun()
358
359 expect(output.files['/test/about.ts']).toBeUndefined()
360 })
361
362 it('should include files when includeExamples is true', async () => {
363 const { environment, output } = createMemoryEnvironment()
364 const templateFile = createTemplateFile(environment, {
365 ...simpleOptions,
366 includeExamples: true,
367 })
368 environment.startRun()
369 await templateFile(
370 'about.ts.ejs',
371 '<% if (!includeExamples) { ignoreFile(); return; } %>\nexport const about = true',
372 )
373 environment.finishRun()
374
375 expect(output.files['/test/about.ts']).toBeDefined()
376 })
377})