1212 */
1313package elide.runtime.node
1414
15+ import org.graalvm.polyglot.Value
16+ import org.junit.jupiter.api.assertThrows
1517import kotlin.test.Test
1618import kotlin.test.assertEquals
1719import kotlin.test.assertIs
@@ -20,7 +22,9 @@ import kotlin.test.assertTrue
2022import kotlin.test.assertNotNull
2123import kotlin.test.assertNull
2224import elide.annotations.Inject
25+ import elide.runtime.intrinsics.js.err.AbstractJsException
2326import elide.runtime.intrinsics.js.err.RangeError
27+ import elide.runtime.intrinsics.js.err.ValueError
2428import elide.runtime.node.crypto.NodeCryptoModule
2529import elide.testing.annotations.TestCase
2630
@@ -211,76 +215,49 @@ import elide.testing.annotations.TestCase
211215 const crypto = require("crypto")
212216 const assert = require("assert")
213217
214- const int = crypto.randomInt();
215- assert.equal(typeof int, "Number ");
218+ const int = crypto.randomInt(5, 10 );
219+ assert.equal(typeof int, "number ");
216220 """
217221 }
218222
219- @Test fun `randomInt should invoke callback with error when min is greater than or equal to max` () = conforms {
220- var callbackInvoked = false
221- crypto.provide().randomInt(10 , 5 ) { err, value ->
222- callbackInvoked = true
223- assertNotNull(err, " Error should be provided when min >= max" )
224- assertIs<RangeError >(err, " Error should be of type RangeError" )
225- }
226- assertTrue(callbackInvoked, " Callback should have been invoked" )
223+ @Test fun `randomInt should throw a RangeError when min is greater than or equal to max` () = conforms {
224+ assertThrows<RangeError > { crypto.provide().randomInt(10 , 5 ) }
225+ assertThrows<RangeError > { crypto.provide().randomInt(10 , 10 ) }
227226 }.guest {
228227 // language=javascript
229228 """
230229 const crypto = require("crypto")
231230 const assert = require("assert")
232231
233- let callbackInvoked = false;
234- crypto.randomInt(10, 5, (err, value) => {
235- callbackInvoked = true;
236- assert.ok(err, "Error should be provided when min >= max" );
237- assert.equal(err.name, "RangeError", "Error should be of type RangeError") ;
232+ assert.throws(() => {
233+ crypto.randomInt(10, 10);
234+ }, (err) => {
235+ assert.ok(err instanceof RangeError );
236+ return true ;
238237 });
239-
240- assert.ok(callbackInvoked, "Callback should have been invoked");
241- """
242- }
243-
244- @Test fun `randomInt should return Unit when a callback is provided` () = conforms {
245- var callbackInvoked = false
246- val result = crypto.provide().randomInt(5 , 10 ) { err, value ->
247- callbackInvoked = true
248- assertNull(err, " Error should be null for valid range" )
249- assertTrue(value in 5 until 10 , " Value should be within the specified range" )
250- }
251- assertTrue(callbackInvoked, " Callback should have been invoked" )
252- assertEquals(Unit , result, " randomInt should return Unit when a callback is provided" )
253- }.guest {
254- // language=javascript
255- """
256- const crypto = require("crypto")
257- const assert = require("assert")
258-
259- let callbackInvoked = false;
260- const result = crypto.randomInt(5, 10, (err, value) => {
261- callbackInvoked = true;
262- assert.ifError(err);
263- assert.ok(value >= 5 && value < 10, "Value should be within the specified range");
238+
239+ assert.throws(() => {
240+ crypto.randomInt(10, 5);
241+ }, (err) => {
242+ assert.ok(err instanceof RangeError);
243+ return true;
264244 });
265-
266- assert.ok(callbackInvoked, "Callback should have been invoked");
267- assert.equal(result, undefined, "randomInt should return undefined when a callback is provided");
268245 """
269246 }
270247
271248 @Test fun `randomInt should default min to 0 when only max is provided` () = conforms {
272- val randomInt = crypto.provide().randomInt(max = 5 )
249+ val randomInt = crypto.provide().randomInt(max = 1 )
273250 assertIs<Int >(randomInt, " randomInt should return an Int" )
274- assertTrue(randomInt in 0 until 5 , " randomInt should be within the range 0 to max" )
251+ assertEquals( 0 , randomInt , " randomInt should be 0 when max is 1 " )
275252 }.guest {
276253 // language=javascript
277254 """
278255 const crypto = require("crypto")
279256 const assert = require("assert")
280257
281- const int = crypto.randomInt(5 );
282- assert.equal(typeof int, "Number ");
283- assert.ok(int >= 0 && int < 5 , "randomInt should be within the range 0 to max");
258+ const int = crypto.randomInt(1 );
259+ assert.equal(typeof int, "number ");
260+ assert.ok(int >= 0 && int < 1 , "randomInt should be within the range 0 to max");
284261 """
285262 }
286263}
0 commit comments