|
| 1 | +import {ExtensionBuildOptions} from './extension.js' |
| 2 | +import {executeStep, BuildContext} from './client-steps.js' |
| 3 | +import {ExtensionInstance} from '../../models/extensions/extension-instance.js' |
| 4 | +import {describe, expect, test} from 'vitest' |
| 5 | +import {inTemporaryDirectory, writeFile, readFile, mkdir, fileExists} from '@shopify/cli-kit/node/fs' |
| 6 | +import {joinPath} from '@shopify/cli-kit/node/path' |
| 7 | +import {Writable} from 'stream' |
| 8 | + |
| 9 | +function buildOptions(): ExtensionBuildOptions { |
| 10 | + return { |
| 11 | + stdout: new Writable({ |
| 12 | + write(chunk, encoding, callback) { |
| 13 | + callback() |
| 14 | + }, |
| 15 | + }), |
| 16 | + stderr: new Writable({ |
| 17 | + write(chunk, encoding, callback) { |
| 18 | + callback() |
| 19 | + }, |
| 20 | + }), |
| 21 | + app: {} as any, |
| 22 | + environment: 'production', |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +describe('client_steps integration', () => { |
| 27 | + test('executes include_assets step and copies files to output', async () => { |
| 28 | + await inTemporaryDirectory(async (tmpDir) => { |
| 29 | + // Setup: Create extension directory with assets |
| 30 | + const extensionDir = joinPath(tmpDir, 'extension') |
| 31 | + const assetsDir = joinPath(extensionDir, 'assets') |
| 32 | + const outputDir = joinPath(tmpDir, 'output') |
| 33 | + |
| 34 | + await mkdir(extensionDir) |
| 35 | + await mkdir(assetsDir) |
| 36 | + await mkdir(outputDir) |
| 37 | + |
| 38 | + // Create test files |
| 39 | + await writeFile(joinPath(assetsDir, 'logo.png'), 'fake-png-data') |
| 40 | + await writeFile(joinPath(assetsDir, 'style.css'), 'body { color: red; }') |
| 41 | + |
| 42 | + const mockExtension = { |
| 43 | + directory: extensionDir, |
| 44 | + outputPath: joinPath(outputDir, 'extension.js'), |
| 45 | + } as ExtensionInstance |
| 46 | + |
| 47 | + const context: BuildContext = {extension: mockExtension, options: buildOptions(), stepResults: new Map()} |
| 48 | + |
| 49 | + await executeStep( |
| 50 | + { |
| 51 | + id: 'copy-assets', |
| 52 | + name: 'Copy Assets', |
| 53 | + type: 'include_assets', |
| 54 | + config: { |
| 55 | + inclusions: [{type: 'pattern', baseDir: 'assets', include: ['**/*']}], |
| 56 | + }, |
| 57 | + }, |
| 58 | + context, |
| 59 | + ) |
| 60 | + |
| 61 | + // Verify: Files were copied to output directory |
| 62 | + const logoExists = await fileExists(joinPath(outputDir, 'logo.png')) |
| 63 | + const styleExists = await fileExists(joinPath(outputDir, 'style.css')) |
| 64 | + |
| 65 | + expect(logoExists).toBe(true) |
| 66 | + expect(styleExists).toBe(true) |
| 67 | + |
| 68 | + const logoContent = await readFile(joinPath(outputDir, 'logo.png')) |
| 69 | + const styleContent = await readFile(joinPath(outputDir, 'style.css')) |
| 70 | + |
| 71 | + expect(logoContent).toBe('fake-png-data') |
| 72 | + expect(styleContent).toBe('body { color: red; }') |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + test('executes multiple steps in sequence', async () => { |
| 77 | + await inTemporaryDirectory(async (tmpDir) => { |
| 78 | + // Setup: Create extension with two asset directories |
| 79 | + const extensionDir = joinPath(tmpDir, 'extension') |
| 80 | + const imagesDir = joinPath(extensionDir, 'images') |
| 81 | + const stylesDir = joinPath(extensionDir, 'styles') |
| 82 | + const outputDir = joinPath(tmpDir, 'output') |
| 83 | + |
| 84 | + await mkdir(extensionDir) |
| 85 | + await mkdir(imagesDir) |
| 86 | + await mkdir(stylesDir) |
| 87 | + await mkdir(outputDir) |
| 88 | + |
| 89 | + await writeFile(joinPath(imagesDir, 'logo.png'), 'logo-data') |
| 90 | + await writeFile(joinPath(stylesDir, 'main.css'), 'css-data') |
| 91 | + |
| 92 | + const mockExtension = { |
| 93 | + directory: extensionDir, |
| 94 | + outputPath: joinPath(outputDir, 'extension.js'), |
| 95 | + } as ExtensionInstance |
| 96 | + |
| 97 | + const context: BuildContext = {extension: mockExtension, options: buildOptions(), stepResults: new Map()} |
| 98 | + |
| 99 | + await executeStep( |
| 100 | + { |
| 101 | + id: 'copy-images', |
| 102 | + name: 'Copy Images', |
| 103 | + type: 'include_assets', |
| 104 | + config: { |
| 105 | + inclusions: [{type: 'pattern', baseDir: 'images', include: ['**/*'], destination: 'assets/images'}], |
| 106 | + }, |
| 107 | + }, |
| 108 | + context, |
| 109 | + ) |
| 110 | + await executeStep( |
| 111 | + { |
| 112 | + id: 'copy-styles', |
| 113 | + name: 'Copy Styles', |
| 114 | + type: 'include_assets', |
| 115 | + config: { |
| 116 | + inclusions: [{type: 'pattern', baseDir: 'styles', include: ['**/*'], destination: 'assets/styles'}], |
| 117 | + }, |
| 118 | + }, |
| 119 | + context, |
| 120 | + ) |
| 121 | + |
| 122 | + // Verify: Files from both steps were copied to correct destinations |
| 123 | + const logoExists = await fileExists(joinPath(outputDir, 'assets/images/logo.png')) |
| 124 | + const styleExists = await fileExists(joinPath(outputDir, 'assets/styles/main.css')) |
| 125 | + |
| 126 | + expect(logoExists).toBe(true) |
| 127 | + expect(styleExists).toBe(true) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + test('silently skips configKey step when config key is absent from extension config', async () => { |
| 132 | + await inTemporaryDirectory(async (tmpDir) => { |
| 133 | + const extensionDir = joinPath(tmpDir, 'extension') |
| 134 | + const outputDir = joinPath(tmpDir, 'output') |
| 135 | + |
| 136 | + await mkdir(extensionDir) |
| 137 | + await mkdir(outputDir) |
| 138 | + |
| 139 | + // Extension has no configuration — static_root key is absent |
| 140 | + const mockExtension = { |
| 141 | + directory: extensionDir, |
| 142 | + outputPath: joinPath(outputDir, 'extension.js'), |
| 143 | + configuration: {}, |
| 144 | + } as unknown as ExtensionInstance |
| 145 | + |
| 146 | + const context: BuildContext = {extension: mockExtension, options: buildOptions(), stepResults: new Map()} |
| 147 | + |
| 148 | + // Should not throw — absent configKey values are silently skipped |
| 149 | + await expect( |
| 150 | + executeStep( |
| 151 | + { |
| 152 | + id: 'copy-static-assets', |
| 153 | + name: 'Copy Static Assets', |
| 154 | + type: 'include_assets', |
| 155 | + config: {inclusions: [{type: 'configKey', key: 'static_root'}]}, |
| 156 | + }, |
| 157 | + context, |
| 158 | + ), |
| 159 | + ).resolves.not.toThrow() |
| 160 | + }) |
| 161 | + }) |
| 162 | +}) |
0 commit comments