Skip to content

Commit 9dc4e4e

Browse files
Merge pull request #139 from godaddy/bun-async-tests
Add async API coverage to Bun compatibility tests
2 parents 80e681d + 1be091e commit 9dc4e4e

1 file changed

Lines changed: 137 additions & 31 deletions

File tree

test/bun-test.js

Lines changed: 137 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
/**
44
* Simple Bun test runner for CI/CD integration
5-
*
5+
*
66
* This script runs the Bun compatibility test and exits with
77
* appropriate status codes for CI/CD pipelines.
8-
*
8+
*
99
* Usage: bun test/bun-test.js
1010
*/
1111

@@ -27,41 +27,59 @@ const config = {
2727
CheckInterval: 1
2828
};
2929

30-
let success = true;
30+
async function runTests() {
31+
let testNum = 0;
3132

32-
try {
33-
// Test 1: Setup
34-
console.log('1. Testing setup...');
33+
// Test 1: Setup (sync)
34+
console.log(`${++testNum}. Testing setup (sync)...`);
3535
asherah.setup(config);
3636
console.log(' ✅ Setup successful');
37-
38-
// Test 2: Encrypt
39-
console.log('2. Testing encryption...');
37+
38+
// Test 2: Encrypt (sync)
39+
console.log(`${++testNum}. Testing encrypt (sync)...`);
4040
const partitionId = 'bun-test-' + Date.now();
4141
const testData = 'Bun runtime test: Hello from Bun! 🚀';
4242
const encrypted = asherah.encrypt(partitionId, Buffer.from(testData, 'utf8'));
43-
43+
4444
if (!encrypted || encrypted.length === 0) {
4545
throw new Error('Encryption failed - no data returned');
4646
}
4747
console.log(` ✅ Encrypted ${testData.length} bytes -> ${encrypted.length} chars`);
48-
49-
// Test 3: Decrypt
50-
console.log('3. Testing decryption...');
48+
49+
// Test 3: Decrypt (sync)
50+
console.log(`${++testNum}. Testing decrypt (sync)...`);
5151
const decrypted = asherah.decrypt(partitionId, encrypted);
52-
52+
5353
if (!decrypted) {
5454
throw new Error('Decryption failed - no data returned');
5555
}
56-
56+
5757
const decryptedText = decrypted.toString('utf8');
5858
if (decryptedText !== testData) {
5959
throw new Error(`Decryption mismatch: expected "${testData}", got "${decryptedText}"`);
6060
}
6161
console.log(' ✅ Decrypted successfully, data matches');
62-
63-
// Test 4: Multiple cycles
64-
console.log('4. Testing multiple encrypt/decrypt cycles...');
62+
63+
// Test 4: Encrypt string (sync)
64+
console.log(`${++testNum}. Testing encrypt_string (sync)...`);
65+
const encryptedStr = asherah.encrypt_string(partitionId, testData);
66+
67+
if (!encryptedStr || encryptedStr.length === 0) {
68+
throw new Error('String encryption failed - no data returned');
69+
}
70+
console.log(` ✅ Encrypted string ${testData.length} chars -> ${encryptedStr.length} chars`);
71+
72+
// Test 5: Decrypt string (sync)
73+
console.log(`${++testNum}. Testing decrypt_string (sync)...`);
74+
const decryptedStr = asherah.decrypt_string(partitionId, encryptedStr);
75+
76+
if (decryptedStr !== testData) {
77+
throw new Error(`String decryption mismatch: expected "${testData}", got "${decryptedStr}"`);
78+
}
79+
console.log(' ✅ Decrypted string successfully, data matches');
80+
81+
// Test 6: Multiple sync cycles
82+
console.log(`${++testNum}. Testing multiple sync encrypt/decrypt cycles...`);
6583
for (let i = 0; i < 5; i++) {
6684
const data = `Test cycle ${i}: ${Math.random()}`;
6785
const enc = asherah.encrypt(partitionId, Buffer.from(data, 'utf8'));
@@ -70,27 +88,115 @@ try {
7088
throw new Error(`Cycle ${i} failed: data mismatch`);
7189
}
7290
}
73-
console.log(' ✅ 5 cycles completed successfully');
74-
75-
// Test 5: Shutdown
76-
console.log('5. Testing shutdown...');
91+
console.log(' ✅ 5 sync cycles completed successfully');
92+
93+
// Test 7: Encrypt async
94+
console.log(`${++testNum}. Testing encrypt_async...`);
95+
const encryptedAsync = await asherah.encrypt_async(partitionId, Buffer.from(testData, 'utf8'));
96+
97+
if (!encryptedAsync || encryptedAsync.length === 0) {
98+
throw new Error('Async encryption failed - no data returned');
99+
}
100+
console.log(` ✅ Async encrypted ${testData.length} bytes -> ${encryptedAsync.length} chars`);
101+
102+
// Test 8: Decrypt async
103+
console.log(`${++testNum}. Testing decrypt_async...`);
104+
const decryptedAsync = await asherah.decrypt_async(partitionId, encryptedAsync);
105+
106+
if (!decryptedAsync) {
107+
throw new Error('Async decryption failed - no data returned');
108+
}
109+
110+
const decryptedAsyncText = decryptedAsync.toString('utf8');
111+
if (decryptedAsyncText !== testData) {
112+
throw new Error(`Async decryption mismatch: expected "${testData}", got "${decryptedAsyncText}"`);
113+
}
114+
console.log(' ✅ Async decrypted successfully, data matches');
115+
116+
// Test 9: Encrypt string async
117+
console.log(`${++testNum}. Testing encrypt_string_async...`);
118+
const encryptedStrAsync = await asherah.encrypt_string_async(partitionId, testData);
119+
120+
if (!encryptedStrAsync || encryptedStrAsync.length === 0) {
121+
throw new Error('Async string encryption failed - no data returned');
122+
}
123+
console.log(` ✅ Async encrypted string ${testData.length} chars -> ${encryptedStrAsync.length} chars`);
124+
125+
// Test 10: Decrypt string async
126+
console.log(`${++testNum}. Testing decrypt_string_async...`);
127+
const decryptedStrAsync = await asherah.decrypt_string_async(partitionId, encryptedStrAsync);
128+
129+
if (decryptedStrAsync !== testData) {
130+
throw new Error(`Async string decryption mismatch: expected "${testData}", got "${decryptedStrAsync}"`);
131+
}
132+
console.log(' ✅ Async decrypted string successfully, data matches');
133+
134+
// Test 11: Multiple async cycles
135+
console.log(`${++testNum}. Testing multiple async encrypt/decrypt cycles...`);
136+
for (let i = 0; i < 5; i++) {
137+
const data = `Async test cycle ${i}: ${Math.random()}`;
138+
const enc = await asherah.encrypt_async(partitionId, Buffer.from(data, 'utf8'));
139+
const dec = await asherah.decrypt_async(partitionId, enc);
140+
if (dec.toString('utf8') !== data) {
141+
throw new Error(`Async cycle ${i} failed: data mismatch`);
142+
}
143+
}
144+
console.log(' ✅ 5 async cycles completed successfully');
145+
146+
// Test 12: Cross sync/async - encrypt sync, decrypt async
147+
console.log(`${++testNum}. Testing cross sync->async round trip...`);
148+
const crossEncSync = asherah.encrypt(partitionId, Buffer.from(testData, 'utf8'));
149+
const crossDecAsync = await asherah.decrypt_async(partitionId, crossEncSync);
150+
if (crossDecAsync.toString('utf8') !== testData) {
151+
throw new Error('Cross sync->async round trip failed: data mismatch');
152+
}
153+
console.log(' ✅ Sync encrypt -> async decrypt succeeded');
154+
155+
// Test 13: Cross async/sync - encrypt async, decrypt sync
156+
console.log(`${++testNum}. Testing cross async->sync round trip...`);
157+
const crossEncAsync = await asherah.encrypt_async(partitionId, Buffer.from(testData, 'utf8'));
158+
const crossDecSync = asherah.decrypt(partitionId, crossEncAsync);
159+
if (crossDecSync.toString('utf8') !== testData) {
160+
throw new Error('Cross async->sync round trip failed: data mismatch');
161+
}
162+
console.log(' ✅ Async encrypt -> sync decrypt succeeded');
163+
164+
// Test 14: Shutdown (sync)
165+
console.log(`${++testNum}. Testing shutdown (sync)...`);
77166
asherah.shutdown();
78167
console.log(' ✅ Shutdown successful');
79-
168+
169+
// Test 15: Setup async + shutdown async
170+
console.log(`${++testNum}. Testing setup_async...`);
171+
await asherah.setup_async(config);
172+
console.log(' ✅ Async setup successful');
173+
174+
console.log(`${++testNum}. Testing encrypt/decrypt after async setup...`);
175+
const asyncSetupEnc = asherah.encrypt(partitionId, Buffer.from(testData, 'utf8'));
176+
const asyncSetupDec = asherah.decrypt(partitionId, asyncSetupEnc);
177+
if (asyncSetupDec.toString('utf8') !== testData) {
178+
throw new Error('Round trip after async setup failed: data mismatch');
179+
}
180+
console.log(' ✅ Round trip after async setup succeeded');
181+
182+
console.log(`${++testNum}. Testing shutdown_async...`);
183+
await asherah.shutdown_async();
184+
console.log(' ✅ Async shutdown successful');
185+
80186
console.log('');
81-
console.log('🎉 All tests PASSED!');
187+
console.log(`🎉 All ${testNum} tests PASSED!`);
82188
console.log('Bun compatibility confirmed ✅');
83-
84-
} catch (error) {
189+
}
190+
191+
runTests().then(() => {
192+
process.exit(0);
193+
}).catch((error) => {
85194
console.error('');
86195
console.error('❌ Test FAILED:');
87196
console.error(error.message);
88197
if (error.stack) {
89198
console.error('\nStack trace:');
90199
console.error(error.stack);
91200
}
92-
success = false;
93-
}
94-
95-
// Exit with appropriate code
96-
process.exit(success ? 0 : 1);
201+
process.exit(1);
202+
});

0 commit comments

Comments
 (0)