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 { 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('Filename Processing - Prefix Stripping', () => {
24 it('should strip single prefix from filename', async () => {
25 const { environment, output } = createMemoryEnvironment()
26 const templateFile = createTemplateFile(environment, {
27 ...simpleOptions,
28 addOnOptions: {
29 testAddon: { database: 'postgres' }
30 }
31 })
32 environment.startRun()
33 await templateFile(
34 './__postgres__testAddon.config.ts.ejs',
35 '<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\n// PostgreSQL config\nexport default { driver: "postgres" }'
36 )
37 environment.finishRun()
38
39 // File should be created with prefix stripped
40 expect(output.files['/test/testAddon.config.ts']).toBeDefined()
41 expect(output.files['/test/testAddon.config.ts'].trim()).toEqual('// PostgreSQL config\nexport default { driver: \'postgres\' }')
43 // Original prefixed filename should not exist
44 expect(output.files['/test/__postgres__testAddon.config.ts']).toBeUndefined()
45 })
46
47 it('should strip prefix from nested directory paths', async () => {
48 const { environment, output } = createMemoryEnvironment()
49 const templateFile = createTemplateFile(environment, {
50 ...simpleOptions,
51 addOnOptions: {
52 testAddon: { database: 'mysql' }
53 }
54 })
55 environment.startRun()
56 await templateFile(
57 './src/db/__mysql__connection.ts.ejs',
58 '<% if (addOnOption.testAddon.database !== "mysql") { ignoreFile() } %>\n// MySQL connection\nexport const connection = "mysql"'
59 )
60 environment.finishRun()
61
62 // File should be created with prefix stripped, preserving directory structure
63 expect(output.files['/test/src/db/connection.ts']).toBeDefined()
64 expect(output.files['/test/src/db/connection.ts'].trim()).toEqual('// MySQL connection\nexport const connection = \'mysql\'')
66 // Original prefixed path should not exist
67 expect(output.files['/test/src/db/__mysql__connection.ts']).toBeUndefined()
68 })
69
70 it('should handle multiple prefixed files in same directory', async () => {
71 const { environment, output } = createMemoryEnvironment()
72 const templateFile = createTemplateFile(environment, {
73 ...simpleOptions,
74 addOnOptions: {
75 testAddon: { database: 'sqlite' }
76 }
77 })
78 environment.startRun()
79 await templateFile(
80 './__postgres__testAddon.config.ts.ejs',
81 '<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\n// PostgreSQL config'
82 )
83 await templateFile(
84 './__mysql__testAddon.config.ts.ejs',
85 '<% if (addOnOption.testAddon.database !== "mysql") { ignoreFile() } %>\n// MySQL config'
86 )
87 await templateFile(
88 './__sqlite__testAddon.config.ts.ejs',
89 '<% if (addOnOption.testAddon.database !== "sqlite") { ignoreFile() } %>\n// SQLite config'
90 )
91 environment.finishRun()
92
93 // Only SQLite file should exist (others ignored via ignoreFile())
94 expect(output.files['/test/testAddon.config.ts']).toBeDefined()
95 expect(output.files['/test/testAddon.config.ts'].trim()).toEqual('// SQLite config')
97 // Prefixed filenames should not exist
98 expect(output.files['/test/__postgres__testAddon.config.ts']).toBeUndefined()
99 expect(output.files['/test/__mysql__testAddon.config.ts']).toBeUndefined()
100 expect(output.files['/test/__sqlite__testAddon.config.ts']).toBeUndefined()
101 })
102
103 it('should handle complex filename patterns', async () => {
104 const { environment, output } = createMemoryEnvironment()
105 const templateFile = createTemplateFile(environment, {
106 ...simpleOptions,
107 addOnOptions: {
108 auth: { provider: 'auth0' }
109 }
110 })
111 environment.startRun()
112 await templateFile(
113 './__auth0__auth.config.js.ejs',
114 '<% if (addOnOption.auth.provider !== "auth0") { ignoreFile() } %>\n// Auth0 configuration\nmodule.exports = { provider: "auth0" }'
115 )
116 environment.finishRun()
117
118 // File should be created with prefix stripped
119 expect(output.files['/test/auth.config.js']).toBeDefined()
120 expect(output.files['/test/auth.config.js'].trim()).toEqual('// Auth0 configuration\nmodule.exports = { provider: "auth0" }')
121 })
122
123 it('should handle prefixed files with .append.ejs extension', async () => {
124 const { environment, output } = createMemoryEnvironment()
125 const templateFile = createTemplateFile(environment, {
126 ...simpleOptions,
127 addOnOptions: {
128 testAddon: { database: 'postgres' }
129 }
130 })
131 environment.startRun()
132 // Create base file first
133 await templateFile(
134 './.env.ejs',
135 'BASE_VAR=value\n'
136 )
137 // Then append with prefixed filename
138 await templateFile(
139 './__postgres__.env.append.ejs',
140 '<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\nDATABASE_URL=postgresql://localhost:5432/mydb\n'
141 )
142 environment.finishRun()
143
144 // File should be created with prefix stripped and content appended
145 expect(output.files['/test/.env']).toBeDefined()
146 expect(output.files['/test/.env']).toEqual('BASE_VAR=value\n\nDATABASE_URL=postgresql://localhost:5432/mydb\n')
147 })
148
149 it('should handle files without prefixes normally', async () => {
150 const { environment, output } = createMemoryEnvironment()
151 const templateFile = createTemplateFile(environment, simpleOptions)
152 environment.startRun()
153 await templateFile(
154 './regular-file.ts.ejs',
155 'export const config = "normal"'
156 )
157 environment.finishRun()
158
159 // File should be created with normal filename processing
160 expect(output.files['/test/regular-file.ts']).toBeDefined()
161 expect(output.files['/test/regular-file.ts']).toEqual('export const config = \'normal\'\n')
162 })
163
164 it('should handle malformed prefixes gracefully', async () => {
165 const { environment, output } = createMemoryEnvironment()
166 const templateFile = createTemplateFile(environment, simpleOptions)
167 environment.startRun()
168 await templateFile(
169 './__malformed_prefix.ts.ejs',
170 'export const config = "malformed"'
171 )
172 await templateFile(
173 './__only_one_underscore.ts.ejs',
174 'export const config = "malformed2"'
175 )
176 await templateFile(
177 './____.ts.ejs',
178 'export const config = "empty"'
179 )
180 environment.finishRun()
181
182 // Files with malformed prefixes should be created as-is (minus .ejs extension)
183 expect(output.files['/test/__malformed_prefix.ts']).toBeDefined()
184 expect(output.files['/test/__only_one_underscore.ts']).toBeDefined()
185 expect(output.files['/test/____.ts']).toBeDefined()
186 })
187
188 it('should handle deeply nested prefixed files', async () => {
189 const { environment, output } = createMemoryEnvironment()
190 const templateFile = createTemplateFile(environment, {
191 ...simpleOptions,
192 addOnOptions: {
193 styling: { framework: 'tailwind' }
194 }
195 })
196 environment.startRun()
197 await templateFile(
198 './src/styles/components/__tailwind__button.css.ejs',
199 '<% if (addOnOption.styling.framework !== "tailwind") { ignoreFile() } %>\n@tailwind base;\n@tailwind components;\n@tailwind utilities;'
200 )
201 environment.finishRun()
202
203 // File should be created with prefix stripped, preserving deep directory structure
204 expect(output.files['/test/src/styles/components/button.css']).toBeDefined()
205 expect(output.files['/test/src/styles/components/button.css'].trim()).toEqual('@tailwind base;\n@tailwind components;\n@tailwind utilities;')
206 })
207
208 it('should handle prefix stripping with different option values', async () => {
209 const { environment, output } = createMemoryEnvironment()
210 const templateFile = createTemplateFile(environment, {
211 ...simpleOptions,
212 addOnOptions: {
213 ui: { library: 'chakra' }
214 }
215 })
216 environment.startRun()
217 await templateFile(
218 './__chakra__theme.ts.ejs',
219 '<% if (addOnOption.ui.library !== "chakra") { ignoreFile() } %>\n// Chakra UI theme\nexport const theme = { colors: {} }'
220 )
221 await templateFile(
222 './__mui__theme.ts.ejs',
223 '<% if (addOnOption.ui.library !== "mui") { ignoreFile() } %>\n// Material-UI theme\nexport const theme = { palette: {} }'
224 )
225 environment.finishRun()
226
227 // Only Chakra file should exist (MUI ignored via ignoreFile())
228 expect(output.files['/test/theme.ts']).toBeDefined()
229 expect(output.files['/test/theme.ts'].trim()).toEqual('// Chakra UI theme\nexport const theme = { colors: {} }')
231 // Prefixed filenames should not exist
232 expect(output.files['/test/__chakra__theme.ts']).toBeUndefined()
233 expect(output.files['/test/__mui__theme.ts']).toBeUndefined()
234 })
235
236 it('should handle complex prefix with special characters', async () => {
237 const { environment, output } = createMemoryEnvironment()
238 const templateFile = createTemplateFile(environment, {
239 ...simpleOptions,
240 addOnOptions: {
241 deployment: { platform: 'vercel-edge' }
242 }
243 })
244 environment.startRun()
245 await templateFile(
246 './__vercel-edge__api.ts.ejs',
247 '<% if (addOnOption.deployment.platform !== "vercel-edge") { ignoreFile() } %>\n// Vercel Edge API\nexport const runtime = "edge"'
248 )
249 environment.finishRun()
250
251 // File should be created with prefix stripped
252 expect(output.files['/test/api.ts']).toBeDefined()
253 expect(output.files['/test/api.ts'].trim()).toEqual('// Vercel Edge API\nexport const runtime = \'edge\'')
254 })
255
256 it('should handle multiple prefixes in same filename (edge case)', async () => {
257 const { environment, output } = createMemoryEnvironment()
258 const templateFile = createTemplateFile(environment, {
259 ...simpleOptions,
260 addOnOptions: {
261 test: { value: 'postgres' }
262 }
263 })
264 environment.startRun()
265 await templateFile(
266 './__postgres__file__with__underscores.ts.ejs',
267 '<% if (addOnOption.test.value !== "postgres") { ignoreFile() } %>\n// File with underscores\nexport const value = "test"'
268 )
269 environment.finishRun()
270
271 // Should only strip the first valid prefix pattern
272 expect(output.files['/test/file__with__underscores.ts']).toBeDefined()
273 expect(output.files['/test/file__with__underscores.ts'].trim()).toEqual('// File with underscores\nexport const value = \'test\'')
274 })
275})