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 { FILE_ROUTER } from '../src/constants.js'
8import { AddOn, Options } from '../src/types.js'
9
10const simpleOptions = {
11 projectName: 'test',
12 targetDir: '/',
13 framework: {
14 id: 'test',
15 name: 'Test',
16 basePackageJSON: {
17 scripts: {
18 dev: 'react-scripts start',
19 },
20 },
21 optionalPackages: {
22 typescript: {
23 devDependencies: {
24 typescript: '^5.0.0',
25 },
26 },
27 tailwindcss: {
28 dependencies: {
29 tailwindcss: '^3.0.0',
30 },
31 },
32 'file-router': {
33 dependencies: {
34 'file-router': '^1.0.0',
35 },
36 },
37 },
38 getFiles: () => ['src/test.txt'],
39 getFileContents: () => 'Hello',
40 getDeletedFiles: () => [],
41 },
42 chosenAddOns: [],
43 packageManager: 'pnpm',
44 typescript: true,
45 tailwind: true,
46 mode: FILE_ROUTER,
47 variableValues: {},
48} as unknown as Options
49
50describe('createApp', () => {
51 it('should create an app', async () => {
52 const { environment, output } = createMemoryEnvironment()
53 await createApp(environment, simpleOptions)
54
55 expect(output.files['/src/test.txt']).toEqual('Hello')
56 })
57
58 it('should create an app - not silent', async () => {
59 const { environment, output } = createMemoryEnvironment()
60 await createApp(environment, {
61 ...simpleOptions,
62 targetDir: '/foo/bar/baz',
63 })
64
65 const cwd = process.cwd()
66
67 expect(output.files[resolve(cwd, '/foo/bar/baz/src/test.txt')]).toEqual(
68 'Hello',
69 )
70 })
71
72 it('should create an app - with a starter', async () => {
73 const { environment, output } = createMemoryEnvironment()
74 await createApp(environment, {
75 ...simpleOptions,
76 starter: {
77 command: {
78 command: 'echo',
79 args: ['Hello'],
80 },
81 getFiles: () => ['src/test2.txt'],
82 getFileContents: () => 'Hello-2',
83 getDeletedFiles: () => [],
84 } as unknown as AddOn,
85 })
86
87 expect(output.files['/src/test2.txt']).toEqual('Hello-2')
88 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
89 })
90
91 it('should create an app - with a add-on', async () => {
92 const { environment, output } = createMemoryEnvironment()
93 await createApp(environment, {
94 ...simpleOptions,
95 git: true,
96 chosenAddOns: [
97 {
98 type: 'add-on',
99 phase: 'add-on',
100 warning: 'This is a warning',
101 command: {
102 command: 'echo',
103 args: ['Hello'],
104 },
105 packageAdditions: {
106 dependencies: {},
107 devDependencies: {},
108 },
109 getFiles: () => ['src/test2.txt', 'public/foo.jpg'],
110 getFileContents: () => 'base64::aGVsbG8=',
111 getDeletedFiles: () => [],
112 } as unknown as AddOn,
113 ],
114 })
115
116 // This is ok, we convert to binary right at the end of the process
117 expect(output.files['/src/test2.txt']).toEqual('base64::aGVsbG8=')
118 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
119 })
120
121 it('should strip demo files from add-ons when includeExamples is false', async () => {
122 const { environment, output } = createMemoryEnvironment()
123
124 const demoFiles = [
125 'src/routes/demo/form.simple.tsx',
126 'src/routes/demo.form.tsx',
127 'src/routes/example.chat.tsx',
128 'src/components/demo-AIAssistant.tsx',
129 'src/components/demo.FormComponents.tsx',
130 'src/hooks/demo-useAudioRecorder.ts',
131 'src/hooks/demo.form.ts',
132 'src/lib/demo-store.ts',
133 'src/lib/demo.ai-hook.ts',
134 'src/data/demo-guitars.ts',
135 'src/store/demo.hooks.ts',
136 'src/store/demo.store.ts',
137 'src/demo.index.css',
138 'public/demo-neon.svg',
139 'public/example-guitar-flowers.jpg',
140 ]
141 const keepFiles = [
142 'src/routes/index.tsx',
143 'src/components/Header.tsx',
144 'src/lib/utils.ts',
145 ]
146 const allFiles = [...demoFiles, ...keepFiles]
147
148 await createApp(environment, {
149 ...simpleOptions,
150 includeExamples: false,
151 chosenAddOns: [
152 {
153 id: 'test-addon',
154 type: 'add-on',
155 phase: 'add-on',
156 packageAdditions: { dependencies: {}, devDependencies: {} },
157 routes: [],
158 integrations: [],
159 getFiles: () => allFiles,
160 getFileContents: () => 'content',
161 getDeletedFiles: () => [],
162 } as unknown as AddOn,
163 ],
164 } as Options)
165
166 for (const file of demoFiles) {
167 expect(output.files[`/${file}`]).toBeUndefined()
168 }
169 for (const file of keepFiles) {
170 expect(output.files[`/${file}`]).toBeDefined()
171 }
172 })
173
174 it('should keep demo files from add-ons when includeExamples is true', async () => {
175 const { environment, output } = createMemoryEnvironment()
176
177 await createApp(environment, {
178 ...simpleOptions,
179 includeExamples: true,
180 chosenAddOns: [
181 {
182 id: 'test-addon',
183 type: 'add-on',
184 phase: 'add-on',
185 packageAdditions: { dependencies: {}, devDependencies: {} },
186 routes: [],
187 integrations: [],
188 getFiles: () => ['src/components/demo-AIAssistant.tsx'],
189 getFileContents: () => 'content',
190 getDeletedFiles: () => [],
191 } as unknown as AddOn,
192 ],
193 } as Options)
194
195 expect(output.files['/src/components/demo-AIAssistant.tsx']).toBeDefined()
196 })
197})