|
| 1 | +#!/usr/bin/env node |
| 2 | +import { indexedDB } from 'fake-indexeddb'; |
| 3 | +import 'fake-indexeddb/auto'; |
| 4 | +import { readFileSync } from 'fs'; |
| 5 | +import { JSDOM } from 'jsdom'; |
| 6 | + |
| 7 | +const loadJson = (path) => JSON.parse(readFileSync(path, 'utf-8')); |
| 8 | + |
| 9 | +const SHOW_VERBOSE = process.env.VERBOSE === 'true'; |
| 10 | + |
| 11 | +const setupEnvironment = () => { |
| 12 | + const { window } = new JSDOM('<!DOCTYPE html><html><body></body></html>', { |
| 13 | + url: 'https://test.example.com', |
| 14 | + pretendToBeVisual: true, |
| 15 | + runScripts: 'dangerously', |
| 16 | + }); |
| 17 | + |
| 18 | + window.OneSignalDeferred = []; |
| 19 | + window.indexedDB = indexedDB; |
| 20 | + window.IDBRequest = global.IDBRequest; |
| 21 | + window.IDBTransaction = global.IDBTransaction; |
| 22 | + window.IDBDatabase = global.IDBDatabase; |
| 23 | + window.IDBObjectStore = global.IDBObjectStore; |
| 24 | + window.IDBIndex = global.IDBIndex; |
| 25 | + window.IDBCursor = global.IDBCursor; |
| 26 | + window.navigator.serviceWorker = {}; |
| 27 | + |
| 28 | + return window; |
| 29 | +}; |
| 30 | + |
| 31 | +const validateNamespace = (obj, spec, apiSpec, path = 'OneSignal') => { |
| 32 | + const errors = []; |
| 33 | + |
| 34 | + spec.functions?.forEach(({ name }) => { |
| 35 | + if (typeof obj[name] === 'function') { |
| 36 | + if (SHOW_VERBOSE) console.log(`✓ ${path}.${name}()`); |
| 37 | + } else { |
| 38 | + errors.push(`${path}.${name}() - function missing`); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + spec.properties?.forEach(({ name }) => { |
| 43 | + const exists = |
| 44 | + name in obj || |
| 45 | + Object.getOwnPropertyDescriptor(Object.getPrototypeOf(obj), name); |
| 46 | + if (exists) { |
| 47 | + if (SHOW_VERBOSE) console.log(`✓ ${path}.${name}`); |
| 48 | + } else { |
| 49 | + errors.push(`${path}.${name} - property missing`); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + spec.namespaces?.forEach((name) => { |
| 54 | + if (obj[name] && typeof obj[name] === 'object') { |
| 55 | + if (SHOW_VERBOSE) console.log(`✓ ${path}.${name} namespace`); |
| 56 | + |
| 57 | + const nestedSpec = apiSpec[name]; |
| 58 | + if (nestedSpec) { |
| 59 | + const nestedErrors = validateNamespace( |
| 60 | + obj[name], |
| 61 | + nestedSpec, |
| 62 | + apiSpec, |
| 63 | + `${path}.${name}`, |
| 64 | + ); |
| 65 | + errors.push(...nestedErrors); |
| 66 | + } |
| 67 | + } else { |
| 68 | + errors.push(`${path}.${name} - namespace missing`); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + return errors; |
| 73 | +}; |
| 74 | + |
| 75 | +const validateBundle = async () => { |
| 76 | + console.log('🔍 Validating OneSignal bundle...\n'); |
| 77 | + |
| 78 | + const apiSpec = loadJson('api.json'); |
| 79 | + const bundle = readFileSync( |
| 80 | + 'build/releases/OneSignalSDK.page.es6.js', |
| 81 | + 'utf-8', |
| 82 | + ); |
| 83 | + const window = setupEnvironment(); |
| 84 | + |
| 85 | + const script = window.document.createElement('script'); |
| 86 | + script.textContent = bundle; |
| 87 | + window.document.head.appendChild(script); |
| 88 | + |
| 89 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 90 | + |
| 91 | + if (!window.OneSignal) throw new Error('OneSignal not found'); |
| 92 | + |
| 93 | + const errors = validateNamespace( |
| 94 | + window.OneSignal, |
| 95 | + apiSpec.OneSignal, |
| 96 | + apiSpec, |
| 97 | + ); |
| 98 | + |
| 99 | + if (errors.length > 0) { |
| 100 | + console.log('❌ Validation failures:'); |
| 101 | + errors.forEach((error) => console.log(` ${error}`)); |
| 102 | + process.exit(1); |
| 103 | + } |
| 104 | + |
| 105 | + console.log('✅ All API validations passed!'); |
| 106 | +}; |
| 107 | + |
| 108 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 109 | + validateBundle().catch((error) => { |
| 110 | + console.error('💥 Validation failed:', error.message); |
| 111 | + process.exit(1); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +export { validateBundle }; |
0 commit comments