-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathproject-integration.test.ts
More file actions
414 lines (342 loc) · 13.8 KB
/
project-integration.test.ts
File metadata and controls
414 lines (342 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import {Project} from './project.js'
import {resolveDotEnv, resolveHiddenConfig, extensionFilesForConfig, webFilesForConfig} from './config-selection.js'
import {requireProjectPackageManagerForOperations} from '../../utilities/project-package-manager.js'
import {loadApp, reloadApp} from '../app/loader.js'
import {AppLinkedInterface} from '../app/app.js'
import {loadLocalExtensionsSpecifications} from '../extensions/load-specifications.js'
import {handleWatcherEvents} from '../../services/dev/app-events/app-event-watcher-handler.js'
import {EventType} from '../../services/dev/app-events/app-event-watcher.js'
import {describe, expect, test} from 'vitest'
import {inTemporaryDirectory, writeFile, mkdir, removeFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {AbortController} from '@shopify/cli-kit/node/abort'
/**
* Integration tests verifying that Project + config-selection produce
* the same results as the old loader for real app configurations.
*/
async function setupRealApp(dir: string) {
// App config
await writeFile(
joinPath(dir, 'shopify.app.toml'),
`
client_id = "test-client-id"
name = "Integration Test App"
application_url = "https://example.com"
embedded = true
[build]
dev_store_url = "test.myshopify.com"
[access_scopes]
scopes = "read_products,write_products"
[auth]
redirect_urls = ["https://example.com/callback"]
[webhooks]
api_version = "2024-01"
`.trim(),
)
// Extension
await mkdir(joinPath(dir, 'extensions', 'my-function'))
await writeFile(
joinPath(dir, 'extensions', 'my-function', 'shopify.extension.toml'),
`
type = "product_discounts"
name = "My Discount"
handle = "my-discount"
api_version = "2024-01"
[build]
command = "cargo build"
`.trim(),
)
// Web
await mkdir(joinPath(dir, 'web', 'backend'))
await writeFile(
joinPath(dir, 'web', 'backend', 'shopify.web.toml'),
`
name = "backend"
roles = ["backend"]
[commands]
dev = "npm run dev"
`.trim(),
)
// Dotenv
await writeFile(joinPath(dir, '.env'), 'SHOPIFY_API_KEY=test-key\nSHOPIFY_API_SECRET=test-secret')
// Hidden config
await mkdir(joinPath(dir, '.shopify'))
await writeFile(
joinPath(dir, '.shopify', 'project.json'),
JSON.stringify({'test-client-id': {dev_store_url: 'hidden.myshopify.com'}}),
)
// package.json (needed by the loader)
await writeFile(joinPath(dir, 'package.json'), JSON.stringify({name: 'test-app', dependencies: {}}))
// Pin npm: getPackageManager walks up to ancestors if no lockfile is found
await writeFile(joinPath(dir, 'package-lock.json'), '')
}
// Load specifications once — this is expensive (loads all extension specs from disk)
const specifications = await loadLocalExtensionsSpecifications()
describe('Project integration', () => {
test('Project discovers the same directory as the old loader', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
const project = await Project.load(dir)
const app = await loadApp({
directory: dir,
userProvidedConfigName: undefined,
specifications,
})
expect(project.directory).toBe(app.directory)
})
})
test('Project discovers the same extension files as the old loader', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
const project = await Project.load(dir)
const app = await loadApp({
directory: dir,
userProvidedConfigName: undefined,
specifications,
})
// The app's non-config extensions should match what the project discovered
const appExtensionPaths = app.realExtensions
.filter((ext) => !ext.isAppConfigExtension)
.map((ext) => ext.configurationPath)
.sort()
const activeConfig = project.appConfigByName('shopify.app.toml')!
const projectExtensionPaths = extensionFilesForConfig(project, activeConfig)
.map((file) => file.path)
.sort()
expect(projectExtensionPaths).toStrictEqual(appExtensionPaths)
})
})
test('Project discovers the same web files as the old loader', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
const project = await Project.load(dir)
const app = await loadApp({
directory: dir,
userProvidedConfigName: undefined,
specifications,
})
const appWebDirs = app.webs.map((web) => web.directory).sort()
const activeConfig = project.appConfigByName('shopify.app.toml')!
const projectWebDirs = webFilesForConfig(project, activeConfig)
.map((file) => joinPath(file.path, '..'))
.sort()
// Both should find the same web directories
expect(projectWebDirs.length).toBe(appWebDirs.length)
})
})
test('resolveDotEnv matches the old loader dotenv', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
const project = await Project.load(dir)
const app = await loadApp({
directory: dir,
userProvidedConfigName: undefined,
specifications,
})
const configPath = joinPath(dir, 'shopify.app.toml')
const projectDotenv = resolveDotEnv(project, configPath)
// Both should find the same .env file with the same variables
expect(projectDotenv?.path).toBe(app.dotenv?.path)
expect(projectDotenv?.variables).toStrictEqual(app.dotenv?.variables)
})
})
test('resolveHiddenConfig matches the old loader hidden config', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
const project = await Project.load(dir)
const app = await loadApp({
directory: dir,
userProvidedConfigName: undefined,
specifications,
})
const projectHiddenConfig = await resolveHiddenConfig(project, 'test-client-id')
expect(projectHiddenConfig).toStrictEqual(app.hiddenConfig)
})
})
test('Project loads correct metadata from filesystem', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
const project = await Project.load(dir)
expect(project.packageManager).toBe('npm')
expect(project.nodeDependencies).toStrictEqual({})
expect(project.usesWorkspaces).toBe(false)
})
})
test('Project uses unknown package manager metadata when the app root has no package.json', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
await removeFile(joinPath(dir, 'package.json'))
const project = await Project.load(dir)
expect(project.packageManager).toBe('unknown')
expect(project.nodeDependencies).toStrictEqual({})
expect(project.usesWorkspaces).toBe(false)
})
})
test('requireProjectPackageManagerForOperations errors when the app root has no package.json', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
await removeFile(joinPath(dir, 'package.json'))
const project = await Project.load(dir)
expect(() => requireProjectPackageManagerForOperations(project)).toThrow(
/Could not determine the project package manager/,
)
})
})
test('multi-config project discovers all configs', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
// Add a staging config with different extension dirs
await writeFile(
joinPath(dir, 'shopify.app.staging.toml'),
`
client_id = "staging-client-id"
name = "Staging App"
application_url = "https://staging.example.com"
embedded = true
extension_directories = ["staging-ext/*"]
`.trim(),
)
await mkdir(joinPath(dir, 'staging-ext', 'staging-func'))
await writeFile(
joinPath(dir, 'staging-ext', 'staging-func', 'shopify.extension.toml'),
'type = "function"\nname = "staging-func"\nhandle = "staging-func"',
)
const project = await Project.load(dir)
// Should discover both configs
expect(project.appConfigFiles).toHaveLength(2)
expect(project.appConfigByClientId('test-client-id')).toBeDefined()
expect(project.appConfigByClientId('staging-client-id')).toBeDefined()
// Should discover extensions from both configs' directories
expect(project.extensionConfigFiles.length).toBeGreaterThanOrEqual(2)
// Filtering to default config should only get extensions/*
const defaultConfig = project.appConfigByName('shopify.app.toml')!
const defaultExts = extensionFilesForConfig(project, defaultConfig)
expect(defaultExts).toHaveLength(1)
// Filtering to staging config should only get staging-ext/*
const stagingConfig = project.appConfigByName('shopify.app.staging.toml')!
const stagingExts = extensionFilesForConfig(project, stagingConfig)
expect(stagingExts).toHaveLength(1)
expect(stagingExts[0]!.content.name).toBe('staging-func')
})
})
test('config-specific dotenv resolution works', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
await writeFile(joinPath(dir, '.env.staging'), 'STAGING_VAR=staging-value')
await writeFile(joinPath(dir, 'shopify.app.staging.toml'), 'client_id = "staging"')
const project = await Project.load(dir)
// Default config gets .env
const defaultDotenv = resolveDotEnv(project, joinPath(dir, 'shopify.app.toml'))
expect(defaultDotenv?.variables.SHOPIFY_API_KEY).toBe('test-key')
// Staging config gets .env.staging
const stagingDotenv = resolveDotEnv(project, joinPath(dir, 'shopify.app.staging.toml'))
expect(stagingDotenv?.variables.STAGING_VAR).toBe('staging-value')
})
})
test('Project.load re-scans filesystem and finds extensions added after initial load', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
// Initial load — should find 1 extension file
const project1 = await Project.load(dir)
expect(project1.extensionConfigFiles).toHaveLength(1)
// Add a new extension to disk (simulates `shopify generate extension` mid-dev)
await mkdir(joinPath(dir, 'extensions', 'another-function'))
await writeFile(
joinPath(dir, 'extensions', 'another-function', 'shopify.extension.toml'),
`
type = "product_discounts"
name = "Another Discount"
handle = "another-discount"
api_version = "2024-01"
[build]
command = "cargo build"
`.trim(),
)
// Fresh Project.load should find the new file
const project2 = await Project.load(dir)
expect(project2.extensionConfigFiles).toHaveLength(2)
// extensionFilesForConfig should also include it
const activeConfig = (await import('./active-config.js')).selectActiveConfig
const config = await activeConfig(project2)
const extFiles = extensionFilesForConfig(project2, config.file)
expect(extFiles).toHaveLength(2)
})
})
test('reloadApp finds extensions added after initial load', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
// Initial load with report mode (matches dev behavior)
const app = await loadApp({
directory: dir,
userProvidedConfigName: undefined,
specifications,
})
const initialRealExtensions = app.realExtensions
const initialCount = initialRealExtensions.length
// Add a new extension to disk (simulates `shopify generate extension` mid-dev)
await mkdir(joinPath(dir, 'extensions', 'another-function'))
await writeFile(
joinPath(dir, 'extensions', 'another-function', 'shopify.extension.toml'),
`
type = "product_discounts"
name = "Another Discount"
handle = "another-discount"
api_version = "2024-01"
[build]
command = "cargo build"
`.trim(),
)
// Reload should find the new extension
const reloadedApp = await reloadApp(app as AppLinkedInterface)
const reloadedRealExtensions = reloadedApp.realExtensions
expect(reloadedRealExtensions.length).toBe(initialCount + 1)
})
})
test('handleWatcherEvents produces Created event for extension added mid-dev', async () => {
await inTemporaryDirectory(async (dir) => {
await setupRealApp(dir)
// Initial load
const app = await loadApp({
directory: dir,
userProvidedConfigName: undefined,
specifications,
})
// Add a new extension to disk
const newExtDir = joinPath(dir, 'extensions', 'another-function')
await mkdir(newExtDir)
await writeFile(
joinPath(newExtDir, 'shopify.extension.toml'),
`
type = "product_discounts"
name = "Another Discount"
handle = "another-discount"
api_version = "2024-01"
[build]
command = "cargo build"
`.trim(),
)
// Simulate the file watcher event that would fire
const appEvent = await handleWatcherEvents(
[
{
type: 'extension_folder_created',
path: newExtDir,
extensionPath: newExtDir,
startTime: [0, 0] as [number, number],
},
],
app as AppLinkedInterface,
{stdout: process.stdout, stderr: process.stderr, signal: new AbortController().signal},
)
// The event should indicate the app was reloaded and the new extension was created
expect(appEvent).toBeDefined()
expect(appEvent!.appWasReloaded).toBe(true)
expect(appEvent!.app.realExtensions.length).toBeGreaterThan(app.realExtensions.length)
const createdEvents = appEvent!.extensionEvents.filter((ev) => ev.type === EventType.Created)
expect(createdEvents.length).toBeGreaterThanOrEqual(1)
// The reloaded app should include the new extension
const handles = appEvent!.app.realExtensions.map((ext) => ext.configuration.handle ?? ext.configuration.name)
expect(handles).toContain('another-discount')
})
})
})