UNPKG

super-pancake-automation

Version:

A lightweight DOM-based UI automation framework using Chrome DevTools Protocol

232 lines (193 loc) โ€ข 6.09 kB
#!/usr/bin/env node import { spawn } from 'child_process'; import { fileURLToPath } from 'url'; import path from 'path'; import fs from 'fs'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); console.log('๐Ÿงช Testing NPM Package Functionality...\n'); // Test 1: Check if CLI commands work async function testCLICommands() { console.log('1๏ธโƒฃ Testing CLI Commands...'); const commands = [ { cmd: 'node', args: ['bin/cli.js', '--version'], desc: 'Version command' }, { cmd: 'node', args: ['bin/cli.js', '--help'], desc: 'Help command' }, { cmd: 'node', args: ['bin/cli.js', 'browsers'], desc: 'Browser detection' } ]; for (const command of commands) { try { const result = await runCommand(command.cmd, command.args); console.log(`โœ… ${command.desc}: PASSED`); } catch (error) { console.log(`โŒ ${command.desc}: FAILED - ${error.message}`); } } } // Test 2: Check if package.json exports are correct function testPackageExports() { console.log('\n2๏ธโƒฃ Testing Package Exports...'); const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); // Check main entry point if (packageJson.main === 'index.js') { console.log('โœ… Main entry point: PASSED'); } else { console.log('โŒ Main entry point: FAILED'); } // Check bin commands const binCommands = Object.keys(packageJson.bin || {}); if (binCommands.length >= 4) { console.log(`โœ… Bin commands (${binCommands.length}): PASSED`); binCommands.forEach(cmd => console.log(` - ${cmd}`)); } else { console.log('โŒ Bin commands: FAILED'); } // Check exports const exports = Object.keys(packageJson.exports || {}); if (exports.length >= 10) { console.log(`โœ… Exports (${exports.length}): PASSED`); } else { console.log('โŒ Exports: FAILED'); } } // Test 3: Check if template files exist function testTemplateFiles() { console.log('\n3๏ธโƒฃ Testing Template Files...'); const requiredFiles = [ 'templates/tests/sample.test.js', 'templates/tests/ui-website.test.js', 'templates/tests/api.test.js', 'bin/init.js', 'bin/cli.js', 'bin/ui-runner.js' ]; for (const file of requiredFiles) { if (fs.existsSync(file)) { console.log(`โœ… ${file}: EXISTS`); } else { console.log(`โŒ ${file}: MISSING`); } } } // Test 4: Check if init script works (dry run) async function testInitScript() { console.log('\n4๏ธโƒฃ Testing Init Script (Dry Run)...'); try { // Test init script without creating actual project const result = await runCommand('node', ['bin/init.js', '--dry-run']); console.log('โœ… Init script: PASSED'); } catch (error) { console.log(`โŒ Init script: FAILED - ${error.message}`); } } // Test 5: Check if main exports work async function testMainExports() { console.log('\n5๏ธโƒฃ Testing Main Exports...'); try { // Test importing main module const { createTestEnvironment, navigateTo, getText } = await import('../index.js'); if (typeof createTestEnvironment === 'function') { console.log('โœ… createTestEnvironment: PASSED'); } else { console.log('โŒ createTestEnvironment: FAILED'); } if (typeof navigateTo === 'function') { console.log('โœ… navigateTo: PASSED'); } else { console.log('โŒ navigateTo: FAILED'); } if (typeof getText === 'function') { console.log('โœ… getText: PASSED'); } else { console.log('โŒ getText: FAILED'); } } catch (error) { console.log(`โŒ Main exports: FAILED - ${error.message}`); } } // Helper function to run commands function runCommand(command, args) { return new Promise((resolve, reject) => { const child = spawn(command, args, { stdio: 'pipe', cwd: path.dirname(__dirname) }); let stdout = ''; let stderr = ''; child.stdout.on('data', (data) => { stdout += data.toString(); }); child.stderr.on('data', (data) => { stderr += data.toString(); }); child.on('close', (code) => { if (code === 0) { resolve(stdout); } else { reject(new Error(`Command failed with code ${code}: ${stderr}`)); } }); child.on('error', (error) => { reject(error); }); }); } // Test 6: Check npm package structure function testNpmStructure() { console.log('\n6๏ธโƒฃ Testing NPM Package Structure...'); const requiredDirs = [ 'bin', 'core', 'utils', 'templates', 'reporter', 'helpers' ]; for (const dir of requiredDirs) { if (fs.existsSync(dir)) { console.log(`โœ… ${dir}/: EXISTS`); } else { console.log(`โŒ ${dir}/: MISSING`); } } // Check if files array in package.json includes all necessary files const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); const files = packageJson.files || []; const requiredFiles = [ 'bin/', 'core/', 'utils/', 'templates/', 'index.js', 'index.d.ts' ]; for (const file of requiredFiles) { if (files.includes(file)) { console.log(`โœ… ${file} in files array: INCLUDED`); } else { console.log(`โŒ ${file} in files array: MISSING`); } } } // Run all tests async function runAllTests() { try { await testCLICommands(); testPackageExports(); testTemplateFiles(); await testInitScript(); await testMainExports(); testNpmStructure(); console.log('\n๐ŸŽ‰ NPM Package Test Summary:'); console.log('โœ… All core functionality tests completed'); console.log('๐Ÿ“ฆ Package is ready for npm deployment'); console.log('๐Ÿš€ End users can use: npx super-pancake-automation init <project>'); } catch (error) { console.error('\nโŒ Test failed:', error.message); process.exit(1); } } // Run tests if this file is executed directly if (import.meta.url === `file://${process.argv[1]}`) { runAllTests(); } export { runAllTests };