|
13 | 13 | package elide.runtime.node |
14 | 14 |
|
15 | 15 | import kotlin.test.Test |
| 16 | +import kotlin.test.assertEquals |
| 17 | +import kotlin.test.assertIs |
| 18 | +import kotlin.test.assertNotEquals |
| 19 | +import kotlin.test.assertTrue |
16 | 20 | import kotlin.test.assertNotNull |
17 | 21 | import elide.annotations.Inject |
18 | 22 | import elide.runtime.node.crypto.NodeCryptoModule |
@@ -99,4 +103,99 @@ import elide.testing.annotations.TestCase |
99 | 103 | @Test override fun testInjectable() { |
100 | 104 | assertNotNull(crypto) |
101 | 105 | } |
| 106 | + |
| 107 | + @Test fun `randomUUID should return a string`() = conforms { |
| 108 | + val uuid = crypto.provide().randomUUID(null) |
| 109 | + assertIs<String>(uuid, "randomUUID should return a String") |
| 110 | + }.guest { |
| 111 | + //language=javascript |
| 112 | + """ |
| 113 | + const crypto = require("crypto") |
| 114 | + const assert = require("assert") |
| 115 | +
|
| 116 | + const uuid = crypto.randomUUID(); |
| 117 | + assert.equal(typeof uuid, "string"); |
| 118 | + """ |
| 119 | + } |
| 120 | + |
| 121 | + @Test fun `randomUUID should return lowercase format`() = conforms { |
| 122 | + val uuid = crypto.provide().randomUUID(null) |
| 123 | + assertEquals(uuid, uuid.lowercase(), "UUID should be in lowercase format") |
| 124 | + }.guest { |
| 125 | + //language=javascript |
| 126 | + """ |
| 127 | + const crypto = require("crypto") |
| 128 | + const assert = require("assert") |
| 129 | +
|
| 130 | + const uuid = crypto.randomUUID(); |
| 131 | + assert.equal(uuid, uuid.toLowerCase(), "UUID should be lowercase"); |
| 132 | + """ |
| 133 | + } |
| 134 | + |
| 135 | + @Test fun `randomUUID should return valid UUID v4 format`() = conforms { |
| 136 | + val uuid = crypto.provide().randomUUID(null) |
| 137 | + |
| 138 | + // UUID v4 format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx |
| 139 | + // where y is one of [8,9,a,b] |
| 140 | + val uuidRegex = Regex("^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$") |
| 141 | + assertTrue( |
| 142 | + uuidRegex.matches(uuid), |
| 143 | + "UUID should match v4 format: $uuid" |
| 144 | + ) |
| 145 | + assertEquals(36, uuid.length, "UUID should be 36 characters long") |
| 146 | + }.guest { |
| 147 | + //language=javascript |
| 148 | + """ |
| 149 | + const crypto = require("crypto") |
| 150 | + const assert = require("assert") |
| 151 | +
|
| 152 | + const uuid = crypto.randomUUID(); |
| 153 | + const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; |
| 154 | +
|
| 155 | + assert.equal(uuid.length, 36, "UUID should be 36 characters"); |
| 156 | + assert.ok(uuidRegex.test(uuid), "UUID should match v4 format: " + uuid); |
| 157 | + """ |
| 158 | + } |
| 159 | + |
| 160 | + @Test fun `randomUUID should generate unique values`() = conforms { |
| 161 | + val uuid1 = crypto.provide().randomUUID(null) |
| 162 | + val uuid2 = crypto.provide().randomUUID(null) |
| 163 | + val uuid3 = crypto.provide().randomUUID(null) |
| 164 | + |
| 165 | + assertNotEquals(uuid1, uuid2, "UUIDS should be unique") |
| 166 | + assertNotEquals(uuid2, uuid3, "UUIDS should be unique") |
| 167 | + assertNotEquals(uuid1, uuid3, "UUIDS should be unique") |
| 168 | + }.guest { |
| 169 | + //language=javascript |
| 170 | + """ |
| 171 | + const crypto = require("crypto") |
| 172 | + const assert = require("assert") |
| 173 | +
|
| 174 | + const uuid1 = crypto.randomUUID(); |
| 175 | + const uuid2 = crypto.randomUUID(); |
| 176 | + const uuid3 = crypto.randomUUID(); |
| 177 | +
|
| 178 | + assert.notEqual(uuid1, uuid2, "UUIDs should be unique"); |
| 179 | + assert.notEqual(uuid2, uuid3, "UUIDs should be unique"); |
| 180 | + assert.notEqual(uuid1, uuid3, "UUIDs should be unique"); |
| 181 | + """ |
| 182 | + } |
| 183 | + |
| 184 | + @Test fun `randomUUID should accept optional options parameter`() = conforms { |
| 185 | + // Options parameter is accepted but currently ignored |
| 186 | + val uuid = crypto.provide().randomUUID(null) |
| 187 | + assertNotNull(uuid) |
| 188 | + }.guest { |
| 189 | + //language=javascript |
| 190 | + """ |
| 191 | + const crypto = require("crypto") |
| 192 | + const assert = require("assert") |
| 193 | +
|
| 194 | + const uuid1 = crypto.randomUUID({ disableEntropyCache: true }); |
| 195 | + const uuid2 = crypto.randomUUID({ disableEntropyCache: false }); |
| 196 | +
|
| 197 | + assert.equal(typeof uuid1, "string"); |
| 198 | + assert.equal(typeof uuid2, "string"); |
| 199 | + """ |
| 200 | + } |
102 | 201 | } |
0 commit comments