Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { resolve } from 'node:path'
3
4import { createApp } from '../src/create-app.js'
5
6import { createMemoryEnvironment } from '../src/environment.js'
7import type { AddOn, Options } from '../src/types.js'
8
9const simpleOptions = {
10 projectName: 'test',
11 targetDir: '/',
12 framework: {
13 id: 'test',
14 name: 'Test',
15 basePackageJSON: {
16 scripts: {
17 dev: 'react-scripts start',
18 },
19 },
20 optionalPackages: {
21 typescript: {
22 devDependencies: {
23 typescript: '^5.0.0',
24 },
25 },
26 tailwindcss: {
27 dependencies: {
28 tailwindcss: '^3.0.0',
29 },
30 },
31 'file-router': {
32 dependencies: {
33 'file-router': '^1.0.0',
34 },
35 },
36 },
37 getFiles: () => ['src/test.txt'],
38 getFileContents: () => 'Hello',
39 getDeletedFiles: () => [],
40 },
41 chosenAddOns: [],
42 packageManager: 'pnpm',
43 typescript: true,
44 tailwind: true,
45 mode: 'file-router',
46 variableValues: {},
47} as unknown as Options
48
49describe('createApp', () => {
50 it('should create an app', async () => {
51 const { environment, output } = createMemoryEnvironment()
52 await createApp(environment, simpleOptions)
53
54 expect(output.files['/src/test.txt']).toEqual('Hello')
55 })
56
57 it('should create an app - not silent', async () => {
58 const { environment, output } = createMemoryEnvironment()
59 await createApp(environment, {
60 ...simpleOptions,
61 targetDir: '/foo/bar/baz',
62 })
63
64 const cwd = process.cwd()
65
66 expect(output.files[resolve(cwd, '/foo/bar/baz/src/test.txt')]).toEqual(
67 'Hello',
68 )
69 })
70
71 it('should create an app - with a starter', async () => {
72 const { environment, output } = createMemoryEnvironment()
73 await createApp(environment, {
74 ...simpleOptions,
75 starter: {
76 command: {
77 command: 'echo',
78 args: ['Hello'],
79 },
80 getFiles: () => ['src/test2.txt'],
81 getFileContents: () => 'Hello-2',
82 getDeletedFiles: () => [],
83 } as unknown as AddOn,
84 })
85
86 expect(output.files['/src/test2.txt']).toEqual('Hello-2')
87 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
88 })
89
90 it.each(['react', 'solid'])(
91 'generates routes for %s file-router apps after install',
92 async (frameworkId) => {
93 const { environment, output } = createMemoryEnvironment()
94 await createApp(environment, {
95 ...simpleOptions,
96 framework: {
97 ...simpleOptions.framework,
98 id: frameworkId,
99 },
100 install: true,
101 } as Options)
102
103 expect(output.commands).toContainEqual({
104 command: 'pnpm',
105 args: ['generate-routes'],
106 })
107 },
108 )
109
110 it('skips route generation when dependency install is skipped', async () => {
111 const { environment, output } = createMemoryEnvironment()
112 await createApp(environment, {
113 ...simpleOptions,
114 framework: {
115 ...simpleOptions.framework,
116 id: 'react',
117 },
118 install: false,
119 })
120
121 expect(output.commands).not.toContainEqual({
122 command: 'pnpm',
123 args: ['generate-routes'],
124 })
125 })
126
127 it('should create an app - with a add-on', async () => {
128 const { environment, output } = createMemoryEnvironment()
129 await createApp(environment, {
130 ...simpleOptions,
131 git: true,
132 chosenAddOns: [
133 {
134 type: 'add-on',
135 phase: 'add-on',
136 warning: 'This is a warning',
137 command: {
138 command: 'echo',
139 args: ['Hello'],
140 },
141 packageAdditions: {
142 dependencies: {},
143 devDependencies: {},
144 },
145 getFiles: () => ['src/test2.txt', 'public/foo.jpg'],
146 getFileContents: () => 'base64::aGVsbG8=',
147 getDeletedFiles: () => [],
148 } as unknown as AddOn,
149 ],
150 })
151
152 // This is ok, we convert to binary right at the end of the process
153 expect(output.files['/src/test2.txt']).toEqual('base64::aGVsbG8=')
154 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
155 })
156
157 it('should strip demo files from add-ons when includeExamples is false', async () => {
158 const { environment, output } = createMemoryEnvironment()
159
160 const demoFiles = [
161 'src/routes/demo/form.simple.tsx',
162 'src/routes/demo.form.tsx',
163 'src/routes/example.chat.tsx',
164 'src/components/demo-AIAssistant.tsx',
165 'src/components/demo.FormComponents.tsx',
166 'src/hooks/demo-useAudioRecorder.ts',
167 'src/hooks/demo.form.ts',
168 'src/lib/demo-store.ts',
169 'src/lib/demo.ai-hook.ts',
170 'src/data/demo-guitars.ts',
171 'src/store/demo.hooks.ts',
172 'src/store/demo.store.ts',
173 'src/demo.index.css',
174 'public/demo-neon.svg',
175 'public/example-guitar-flowers.jpg',
176 ]
177 const keepFiles = [
178 'src/routes/index.tsx',
179 'src/components/Header.tsx',
180 'src/lib/utils.ts',
181 ]
182 const allFiles = [...demoFiles, ...keepFiles]
183
184 await createApp(environment, {
185 ...simpleOptions,
186 includeExamples: false,
187 chosenAddOns: [
188 {
189 id: 'test-addon',
190 type: 'add-on',
191 phase: 'add-on',
192 packageAdditions: { dependencies: {}, devDependencies: {} },
193 routes: [],
194 integrations: [],
195 getFiles: () => allFiles,
196 getFileContents: () => 'content',
197 getDeletedFiles: () => [],
198 } as unknown as AddOn,
199 ],
200 } as Options)
201
202 for (const file of demoFiles) {
203 expect(output.files[`/${file}`]).toBeUndefined()
204 }
205 for (const file of keepFiles) {
206 expect(output.files[`/${file}`]).toBeDefined()
207 }
208 })
209
210 it('should keep demo files from add-ons when includeExamples is true', async () => {
211 const { environment, output } = createMemoryEnvironment()
212
213 await createApp(environment, {
214 ...simpleOptions,
215 includeExamples: true,
216 chosenAddOns: [
217 {
218 id: 'test-addon',
219 type: 'add-on',
220 phase: 'add-on',
221 packageAdditions: { dependencies: {}, devDependencies: {} },
222 routes: [],
223 integrations: [],
224 getFiles: () => ['src/components/demo-AIAssistant.tsx'],
225 getFileContents: () => 'content',
226 getDeletedFiles: () => [],
227 } as unknown as AddOn,
228 ],
229 } as Options)
230
231 expect(output.files['/src/components/demo-AIAssistant.tsx']).toBeDefined()
232 })
233
234 it('writes .env.example from add-on envVars metadata', async () => {
235 const { environment, output } = createMemoryEnvironment()
236
237 await createApp(environment, {
238 ...simpleOptions,
239 chosenAddOns: [
240 {
241 id: 'fake-auth',
242 name: 'Fake Auth',
243 type: 'add-on',
244 phase: 'add-on',
245 packageAdditions: { dependencies: {}, devDependencies: {} },
246 routes: [],
247 integrations: [],
248 envVars: [
249 {
250 name: 'AUTH_SECRET',
251 description: 'Random secret used to sign sessions',
252 required: true,
253 },
254 {
255 name: 'AUTH_URL',
256 description: 'Base URL for auth callbacks',
257 required: false,
258 },
259 ],
260 getFiles: () => [],
261 getFileContents: () => '',
262 getDeletedFiles: () => [],
263 } as unknown as AddOn,
264 ],
265 } as Options)
266
267 const envExample = output.files['/.env.example']
268 expect(envExample).toBeDefined()
269 expect(envExample).toContain('# Fake Auth')
270 expect(envExample).toContain(
271 '# Random secret used to sign sessions (required)',
272 )
273 expect(envExample).toContain('AUTH_SECRET=')
274 expect(envExample).toContain('# Base URL for auth callbacks')
275 expect(envExample).toContain('AUTH_URL=')
276 })
277
278 it('does not write .env.example when no add-on declares envVars', async () => {
279 const { environment, output } = createMemoryEnvironment()
280
281 await createApp(environment, {
282 ...simpleOptions,
283 chosenAddOns: [],
284 } as Options)
285
286 expect(output.files['/.env.example']).toBeUndefined()
287 })
288
289 it('dedupes env vars across add-ons in .env.example', async () => {
290 const { environment, output } = createMemoryEnvironment()
291
292 await createApp(environment, {
293 ...simpleOptions,
294 chosenAddOns: [
295 {
296 id: 'addon-a',
297 name: 'Addon A',
298 type: 'add-on',
299 phase: 'add-on',
300 packageAdditions: { dependencies: {}, devDependencies: {} },
301 routes: [],
302 integrations: [],
303 envVars: [{ name: 'SHARED_KEY', required: true }],
304 getFiles: () => [],
305 getFileContents: () => '',
306 getDeletedFiles: () => [],
307 } as unknown as AddOn,
308 {
309 id: 'addon-b',
310 name: 'Addon B',
311 type: 'add-on',
312 phase: 'add-on',
313 packageAdditions: { dependencies: {}, devDependencies: {} },
314 routes: [],
315 integrations: [],
316 envVars: [{ name: 'SHARED_KEY', required: true }],
317 getFiles: () => [],
318 getFileContents: () => '',
319 getDeletedFiles: () => [],
320 } as unknown as AddOn,
321 ],
322 } as Options)
323
324 const envExample = output.files['/.env.example'] || ''
325 const occurrences = envExample.match(/^SHARED_KEY=/gm) || []
326 expect(occurrences.length).toBe(1)
327 })
328})