-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-test.js
More file actions
158 lines (131 loc) · 3.49 KB
/
runtime-test.js
File metadata and controls
158 lines (131 loc) · 3.49 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
/**
* Runtime Verification Test
*
* This script tests that the SDK actually runs and works correctly.
* It imports the compiled code and exercises all core features.
*/
import { SDK } from '../../packages/core/dist/index.js';
console.log('🧪 Starting Runtime Verification...\n');
// Track test results
const results = [];
function test(name, fn) {
try {
fn();
results.push({ name, status: '✅ PASS' });
} catch (error) {
results.push({ name, status: '❌ FAIL', error: error.message });
}
}
// Create a simple test plugin
function testPlugin(plugin, instance, config) {
plugin.ns('test');
plugin.defaults({
test: { value: 'default' },
});
plugin.expose({
getValue() {
return config.get('test.value');
},
emitTest(data) {
plugin.emit('test:event', data);
},
});
instance.on('sdk:ready', () => {
console.log(' 📦 Test plugin initialized');
});
}
// Test 1: SDK Creation
test('SDK Creation', () => {
const sdk = new SDK();
if (!(sdk instanceof SDK)) throw new Error('SDK not created');
console.log('✅ SDK instance created');
});
// Test 2: Plugin Registration
test('Plugin Registration', () => {
const sdk = new SDK();
sdk.use(testPlugin);
console.log('✅ Plugin registered');
});
// Test 3: SDK Initialization
let sdk;
test('SDK Initialization', () => {
sdk = new SDK({ test: { value: 'configured' } });
sdk.use(testPlugin);
sdk.init();
console.log('✅ SDK initialized');
});
// Test 4: Configuration Management
test('Configuration Management', () => {
const value = sdk.getValue();
if (value !== 'configured') {
throw new Error(`Expected 'configured', got '${value}'`);
}
console.log(`✅ Config value: ${value}`);
});
// Test 5: Event Handling
test('Event Handling', () => {
let eventFired = false;
sdk.on('test:event', (data) => {
eventFired = true;
console.log(`✅ Event received: ${JSON.stringify(data)}`);
});
sdk.emitTest({ message: 'Hello!' });
if (!eventFired) {
throw new Error('Event not fired');
}
});
// Test 6: Wildcard Events
test('Wildcard Events', () => {
let wildcardFired = false;
sdk.on('test:*', () => {
wildcardFired = true;
});
sdk.emitTest({ message: 'Wildcard test' });
if (!wildcardFired) {
throw new Error('Wildcard event not fired');
}
console.log('✅ Wildcard event matched');
});
// Test 7: Lifecycle Events
test('Lifecycle Events', () => {
let destroyFired = false;
sdk.on('sdk:destroy', () => {
destroyFired = true;
});
sdk.destroy();
if (!destroyFired) {
throw new Error('Destroy event not fired');
}
console.log('✅ Lifecycle destroy event fired');
});
// Test 8: Idempotency
test('Idempotency', () => {
const newSdk = new SDK();
newSdk.use(testPlugin);
newSdk.init();
newSdk.init(); // Should not error
console.log('✅ Init is idempotent');
});
// Print results
console.log(`\n${'='.repeat(50)}`);
console.log('📊 Runtime Verification Results:');
console.log('='.repeat(50));
let passed = 0;
let failed = 0;
for (const result of results) {
console.log(`${result.status} ${result.name}`);
if (result.error) {
console.log(` Error: ${result.error}`);
}
if (result.status.includes('PASS')) passed++;
else failed++;
}
console.log('='.repeat(50));
console.log(`Total: ${results.length} | Passed: ${passed} | Failed: ${failed}`);
if (failed > 0) {
console.log('\n❌ Runtime verification FAILED');
process.exit(1);
} else {
console.log('\n✅ Runtime verification PASSED');
process.exit(0);
}