Skip to content

Commit 6bc32b2

Browse files
committed
test(randomInt): write more tests, address issues with method.
1 parent 933bac1 commit 6bc32b2

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

packages/graalvm/src/main/kotlin/elide/runtime/node/crypto/NodeCrypto.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ internal class NodeCrypto private constructor () : ReadOnlyProxyObject, CryptoAP
8484

8585
val randomInt = cryptoRandomGenerator.nextInt(min, max)
8686

87-
callback?.invoke(error, randomInt)
88-
89-
return randomInt
87+
if (callback != null) {
88+
callback.invoke(error, randomInt)
89+
return Unit
90+
} else {
91+
return randomInt
92+
}
9093
}
9194

9295
@Polyglot override fun randomInt(min: Value, max: Value, callback: Value?): Any {
@@ -98,7 +101,7 @@ internal class NodeCrypto private constructor () : ReadOnlyProxyObject, CryptoAP
98101
}
99102

100103
@Polyglot override fun randomInt(max: Value): Any {
101-
return randomInt(0, max = max.asInt())
104+
return randomInt(0, max.asInt())
102105
}
103106

104107
// ProxyObject implementation

packages/graalvm/src/test/kotlin/elide/runtime/node/NodeCryptoTest.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.graalvm.polyglot.Value
1616
import org.junit.jupiter.api.assertThrows
1717
import kotlin.test.Test
1818
import kotlin.test.assertEquals
19+
import kotlin.test.assertFalse
1920
import kotlin.test.assertIs
2021
import kotlin.test.assertNotEquals
2122
import kotlin.test.assertTrue
@@ -260,4 +261,41 @@ import elide.testing.annotations.TestCase
260261
assert.ok(int >= 0 && int < 1, "randomInt should be within the range 0 to max");
261262
"""
262263
}
264+
265+
@Test fun `randomInt should invoke callback when callback is provided`() = conforms {
266+
var callbackInvoked = false
267+
val genInt = crypto.provide().randomInt(10, 20) { error, result ->
268+
callbackInvoked = true
269+
assertNull(error, "Callback error should be null")
270+
assertIs<Int>(result, "Callback result should be an Int")
271+
assertTrue(result in 10 until 20, "Callback result should be within the specified range")
272+
}
273+
assertTrue(callbackInvoked, "Callback should have been invoked")
274+
assertIs<Unit>(genInt, "randomInt should return Unit when callback is provided")
275+
}.guest {
276+
//language=javascript
277+
"""
278+
const crypto = require("crypto")
279+
const assert = require("assert")
280+
281+
function randomIntPromise(min, max) {
282+
return new Promise((resolve, reject) => {
283+
crypto.randomInt(min, max, (err, int) => {
284+
callbackInvoked = true;
285+
assert.equal(err, null, "Callback error should be null");
286+
resolve(int);
287+
});
288+
});
289+
}
290+
291+
let callbackInvoked = false;
292+
293+
randomIntPromise(10, 20)
294+
.then((int) => {
295+
assert.equal(typeof int, "number");
296+
assert.ok(int >= 10 && int < 20, "randomInt should be within the range");
297+
assert.ok(callbackInvoked, "Callback should have been invoked");
298+
})
299+
"""
300+
}
263301
}

0 commit comments

Comments
 (0)