同质量或体积单位不再要求手填系数,普通 http 页面也能生成幂等操作号。 Co-authored-by: Cursor <cursoragent@cursor.com>
34 lines
987 B
TypeScript
34 lines
987 B
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { randomOperationId } from '../src/utils/random-id'
|
|
|
|
const uuidV4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
|
|
|
|
test('secure context keeps the native randomUUID value', () => {
|
|
assert.equal(
|
|
randomOperationId({ randomUUID: () => 'native-operation-id' }),
|
|
'native-operation-id'
|
|
)
|
|
})
|
|
|
|
test('insecure context still returns a uuid from getRandomValues', () => {
|
|
const insecure = {
|
|
getRandomValues(bytes: Uint8Array) {
|
|
crypto.getRandomValues(bytes)
|
|
return bytes
|
|
}
|
|
}
|
|
const first = randomOperationId(insecure)
|
|
const second = randomOperationId(insecure)
|
|
assert.match(first, uuidV4)
|
|
assert.match(second, uuidV4)
|
|
assert.notEqual(first, second)
|
|
assert.ok(first.length <= 64)
|
|
})
|
|
|
|
test('missing web crypto still returns a bounded operation id', () => {
|
|
const id = randomOperationId({})
|
|
assert.match(id, uuidV4)
|
|
assert.ok(id.length <= 64)
|
|
})
|