|
| 1 | +const { execSync, spawnSync } = require('child_process'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const TEST_PROJECT_PATH = path.resolve(__dirname, '../dev-test-npm'); |
| 6 | +const rootPath = path.resolve(__dirname, '..'); |
| 7 | + |
| 8 | +function run(command, options = {}) { |
| 9 | + console.log(`Running: ${command}`); |
| 10 | + const result = spawnSync(command, { shell: true, stdio: 'inherit', ...options }); |
| 11 | + if (result.status !== 0) { |
| 12 | + throw new Error(`Command failed: ${command}`); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +try { |
| 17 | + // 0. Ensure monorepo dependencies are installed |
| 18 | + console.log('\n=== Installing monorepo dependencies ==='); |
| 19 | + run('npm install', { cwd: rootPath }); |
| 20 | + |
| 21 | + // 1. Build all packages in the monorepo (this builds dependencies in correct order) |
| 22 | + console.log('\n=== Building all packages ==='); |
| 23 | + run('npm run build', { cwd: rootPath }); |
| 24 | + |
| 25 | + // 2. Force build decap-cms-app specifically (bypass nx cache) |
| 26 | + console.log('\n=== Force building decap-cms-app ==='); |
| 27 | + const appPackagePath = path.join(rootPath, 'packages', 'decap-cms-app'); |
| 28 | + run('npx nx reset', { cwd: rootPath }); // Clear nx cache |
| 29 | + run('npx nx run decap-cms-app:build --skip-nx-cache', { cwd: rootPath }); |
| 30 | + |
| 31 | + // 3. Verify decap-cms-app build outputs exist |
| 32 | + const mainFile = path.join(appPackagePath, 'dist', 'decap-cms-app.js'); |
| 33 | + const esmFile = path.join(appPackagePath, 'dist', 'esm', 'index.js'); |
| 34 | + |
| 35 | + console.log('\n=== Checking build outputs ==='); |
| 36 | + console.log('Main file exists:', fs.existsSync(mainFile)); |
| 37 | + console.log('ESM file exists:', fs.existsSync(esmFile)); |
| 38 | + |
| 39 | + if (!fs.existsSync(mainFile)) { |
| 40 | + throw new Error(`Build failed: ${mainFile} was not created`); |
| 41 | + } |
| 42 | + |
| 43 | + // 4. Create a fresh test project |
| 44 | + if (fs.existsSync(TEST_PROJECT_PATH)) { |
| 45 | + fs.rmSync(TEST_PROJECT_PATH, { recursive: true, force: true }); |
| 46 | + } |
| 47 | + fs.mkdirSync(TEST_PROJECT_PATH, { recursive: true }); |
| 48 | + |
| 49 | + // 5. Initialize package.json |
| 50 | + const packageJson = { |
| 51 | + name: 'decap-cms-integrity-test', |
| 52 | + version: '1.0.0', |
| 53 | + type: 'module', |
| 54 | + private: true |
| 55 | + }; |
| 56 | + fs.writeFileSync( |
| 57 | + path.join(TEST_PROJECT_PATH, 'package.json'), |
| 58 | + JSON.stringify(packageJson, null, 2) |
| 59 | + ); |
| 60 | + |
| 61 | + // 6. Install dependencies including Playwright |
| 62 | + console.log('\n=== Installing test dependencies ==='); |
| 63 | + run('npm install react@^19.1.0 react-dom@^19.1.0 playwright --save-dev', { cwd: TEST_PROJECT_PATH }); |
| 64 | + |
| 65 | + // 6.1. Install Playwright browsers |
| 66 | + console.log('\n=== Installing Playwright browsers ==='); |
| 67 | + run('npx playwright install chromium', { cwd: TEST_PROJECT_PATH }); |
| 68 | + |
| 69 | + // 7. Link decap-cms-app |
| 70 | + run('npm link', { cwd: appPackagePath }); |
| 71 | + run('npm link decap-cms-app', { cwd: TEST_PROJECT_PATH }); |
| 72 | + |
| 73 | + // 8. Create HTML test page |
| 74 | + const testHtmlPath = path.join(TEST_PROJECT_PATH, 'test.html'); |
| 75 | + fs.writeFileSync( |
| 76 | + testHtmlPath, |
| 77 | + `<!DOCTYPE html> |
| 78 | + <html> |
| 79 | + <head> |
| 80 | + <meta charset="UTF-8"> |
| 81 | + <title>Decap CMS Smoke Test</title> |
| 82 | + </head> |
| 83 | + <body> |
| 84 | + <div id="nc-root"></div> |
| 85 | + <script type="module"> |
| 86 | + import CMS from './node_modules/decap-cms-app/dist/esm/index.js'; |
| 87 | +
|
| 88 | + window.testResult = { success: false, error: null }; |
| 89 | +
|
| 90 | + try { |
| 91 | + console.log('CMS loaded:', typeof CMS); |
| 92 | + console.log('CMS.init:', typeof CMS?.init); |
| 93 | + console.log('CMS.registerPreviewTemplate:', typeof CMS?.registerPreviewTemplate); |
| 94 | +
|
| 95 | + if (CMS && typeof CMS.init === 'function' && typeof CMS.registerPreviewTemplate === 'function') { |
| 96 | + window.testResult.success = true; |
| 97 | + window.testResult.message = 'CMS API is available'; |
| 98 | + } else { |
| 99 | + throw new Error('CMS API is incomplete'); |
| 100 | + } |
| 101 | + } catch (e) { |
| 102 | + window.testResult.error = e.message; |
| 103 | + console.error('Test failed:', e); |
| 104 | + } |
| 105 | + </script> |
| 106 | + </body> |
| 107 | + </html> |
| 108 | + ` |
| 109 | + ); |
| 110 | + |
| 111 | + // 9. Create Playwright test |
| 112 | + const playwrightTestPath = path.join(TEST_PROJECT_PATH, 'smoke-test.mjs'); |
| 113 | + fs.writeFileSync( |
| 114 | + playwrightTestPath, |
| 115 | + `import { chromium } from 'playwright'; |
| 116 | + import { createServer } from 'http'; |
| 117 | + import { readFileSync } from 'fs'; |
| 118 | + import { join, extname } from 'path'; |
| 119 | + import { fileURLToPath } from 'url'; |
| 120 | + import { dirname } from 'path'; |
| 121 | +
|
| 122 | + const __filename = fileURLToPath(import.meta.url); |
| 123 | + const __dirname = dirname(__filename); |
| 124 | +
|
| 125 | + // Simple HTTP server to serve files |
| 126 | + const server = createServer((req, res) => { |
| 127 | + const filePath = join(__dirname, req.url === '/' ? 'test.html' : req.url.slice(1)); |
| 128 | +
|
| 129 | + const mimeTypes = { |
| 130 | + '.html': 'text/html', |
| 131 | + '.js': 'application/javascript', |
| 132 | + '.mjs': 'application/javascript' |
| 133 | + }; |
| 134 | +
|
| 135 | + const ext = extname(filePath); |
| 136 | + const contentType = mimeTypes[ext] || 'text/plain'; |
| 137 | +
|
| 138 | + try { |
| 139 | + const content = readFileSync(filePath); |
| 140 | + res.writeHead(200, { 'Content-Type': contentType }); |
| 141 | + res.end(content); |
| 142 | + } catch (err) { |
| 143 | + res.writeHead(404); |
| 144 | + res.end('Not found'); |
| 145 | + } |
| 146 | + }); |
| 147 | +
|
| 148 | + server.listen(0, async () => { |
| 149 | + const port = server.address().port; |
| 150 | + console.log(\`Test server running on http://localhost:\${port}\`); |
| 151 | +
|
| 152 | + const browser = await chromium.launch(); |
| 153 | + const page = await browser.newPage(); |
| 154 | +
|
| 155 | + // Listen for console messages |
| 156 | + page.on('console', msg => console.log('Browser:', msg.text())); |
| 157 | +
|
| 158 | + // Load the test page |
| 159 | + await page.goto(\`http://localhost:\${port}/test.html\`); |
| 160 | +
|
| 161 | + // Wait a bit for the module to load |
| 162 | + await page.waitForTimeout(2000); |
| 163 | +
|
| 164 | + // Check test result |
| 165 | + const result = await page.evaluate(() => window.testResult); |
| 166 | +
|
| 167 | + await browser.close(); |
| 168 | + server.close(); |
| 169 | +
|
| 170 | + if (result && result.success) { |
| 171 | + console.log('✓ Integrity check: decap-cms-app loaded successfully'); |
| 172 | + console.log('✓', result.message); |
| 173 | + console.log('✓ Smoke test passed'); |
| 174 | + process.exit(0); |
| 175 | + } else { |
| 176 | + console.error('✗ Smoke test failed:', result?.error || 'Unknown error'); |
| 177 | + console.error(result); |
| 178 | + process.exit(1); |
| 179 | + } |
| 180 | + }); |
| 181 | + ` |
| 182 | + ); |
| 183 | + |
| 184 | + // 10. Run Playwright test |
| 185 | + console.log('\n=== Running smoke test in browser ==='); |
| 186 | + run('node smoke-test.mjs', { cwd: TEST_PROJECT_PATH }); |
| 187 | + |
| 188 | + console.log('\n✓ Integrity check completed successfully!'); |
| 189 | + process.exit(0); |
| 190 | +} catch (err) { |
| 191 | + console.error('\n✗ Integrity check failed:', err.message); |
| 192 | + process.exit(1); |
| 193 | +} |
0 commit comments