dify/web/features/new-rag/__tests__/request-id.spec.ts
Stephen Zhou eac9f69ad1
feat(dataset): add crawl source selection (#39325)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-24 07:11:48 +00:00

42 lines
1.2 KiB
TypeScript

import { createRequestId } from '../request-id'
const uuidV4Mock = vi.hoisted(() => vi.fn())
vi.mock('uuid', () => ({
v4: uuidV4Mock,
}))
describe('createRequestId', () => {
beforeEach(() => {
vi.clearAllMocks()
uuidV4Mock.mockReturnValue('fallback-request-id')
})
it('uses the native random UUID when it is available', () => {
const randomUUID = vi
.spyOn(globalThis.crypto, 'randomUUID')
.mockReturnValue('11111111-1111-4111-8111-111111111111')
expect(createRequestId()).toBe('11111111-1111-4111-8111-111111111111')
expect(uuidV4Mock).not.toHaveBeenCalled()
randomUUID.mockRestore()
})
it('falls back when randomUUID is unavailable', () => {
const descriptor = Object.getOwnPropertyDescriptor(globalThis.crypto, 'randomUUID')
Object.defineProperty(globalThis.crypto, 'randomUUID', {
configurable: true,
value: undefined,
})
try {
expect(createRequestId()).toBe('fallback-request-id')
expect(uuidV4Mock).toHaveBeenCalledOnce()
} finally {
if (descriptor) Object.defineProperty(globalThis.crypto, 'randomUUID', descriptor)
else Reflect.deleteProperty(globalThis.crypto, 'randomUUID')
}
})
})