Files
dify/web/app/components/base/markdown-blocks/__tests__/utils.spec.ts
+42 f50e44b24a test: improve coverage for some test files (#32916)
Signed-off-by: edvatar <[email protected]>
Signed-off-by: -LAN- <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: majiayu000 <[email protected]>
Co-authored-by: Poojan <[email protected]>
Co-authored-by: sahil-infocusp <[email protected]>
Co-authored-by: 非法操作 <[email protected]>
Co-authored-by: Pandaaaa906 <[email protected]>
Co-authored-by: Asuka Minato <[email protected]>
Co-authored-by: heyszt <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Ijas <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: 木之本澪 <[email protected]>
Co-authored-by: KinomotoMio <[email protected]>
Co-authored-by: 不做了睡大觉 <[email protected]>
Co-authored-by: User <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: edvatar <[email protected]>
Co-authored-by: -LAN- <[email protected]>
Co-authored-by: Leilei <[email protected]>
Co-authored-by: HaKu <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: wangxiaolei <[email protected]>
Co-authored-by: Varun Chawla <[email protected]>
Co-authored-by: Stephen Zhou <[email protected]>
Co-authored-by: yyh <[email protected]>
Co-authored-by: yyh <[email protected]>
Co-authored-by: tda <[email protected]>
Co-authored-by: root <root@DESKTOP-KQLO90N>
Co-authored-by: Sisyphus <[email protected]>
Co-authored-by: Niels Kaspers <[email protected]>
Co-authored-by: hj24 <[email protected]>
Co-authored-by: Tyson Cung <[email protected]>
Co-authored-by: Stephen Zhou <[email protected]>
Co-authored-by: FFXN <[email protected]>
Co-authored-by: slegarraga <[email protected]>
Co-authored-by: 99 <[email protected]>
Co-authored-by: Br1an <[email protected]>
Co-authored-by: L1nSn0w <[email protected]>
Co-authored-by: Yunlu Wen <[email protected]>
Co-authored-by: akkoaya <[email protected]>
Co-authored-by: 盐粒 Yanli <[email protected]>
Co-authored-by: lif <[email protected]>
Co-authored-by: weiguang li <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: crazywoola <[email protected]>
Co-authored-by: HanWenbo <[email protected]>
Co-authored-by: Coding On Star <[email protected]>
Co-authored-by: CodingOnStar <[email protected]>
Co-authored-by: Stable Genius <[email protected]>
Co-authored-by: Stable Genius <[email protected]>
Co-authored-by: ふるい <[email protected]>
Co-authored-by: Xiyuan Chen <[email protected]>
2026-03-06 18:59:16 +08:00

122 lines
4.1 KiB
TypeScript

import { getMarkdownImageURL, isValidUrl } from '../utils'
vi.mock('@/config', () => ({
ALLOW_UNSAFE_DATA_SCHEME: false,
MARKETPLACE_API_PREFIX: '/api/marketplace',
}))
describe('utils', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('isValidUrl', () => {
it('should return true for http: URLs', () => {
expect(isValidUrl('http://example.com')).toBe(true)
})
it('should return true for https: URLs', () => {
expect(isValidUrl('https://example.com')).toBe(true)
})
it('should return true for protocol-relative URLs', () => {
expect(isValidUrl('//cdn.example.com/image.png')).toBe(true)
})
it('should return true for mailto: URLs', () => {
expect(isValidUrl('mailto:[email protected]')).toBe(true)
})
it('should return false for data: URLs when ALLOW_UNSAFE_DATA_SCHEME is false', () => {
expect(isValidUrl('data:image/png;base64,abc123')).toBe(false)
})
it('should return false for javascript: URLs', () => {
expect(isValidUrl('javascript:alert(1)')).toBe(false)
})
it('should return false for ftp: URLs', () => {
expect(isValidUrl('ftp://files.example.com')).toBe(false)
})
it('should return false for relative paths', () => {
expect(isValidUrl('/images/photo.png')).toBe(false)
})
it('should return false for empty string', () => {
expect(isValidUrl('')).toBe(false)
})
it('should return false for plain text', () => {
expect(isValidUrl('not a url')).toBe(false)
})
})
describe('isValidUrl with ALLOW_UNSAFE_DATA_SCHEME enabled', () => {
beforeEach(() => {
vi.resetModules()
vi.doMock('@/config', () => ({
ALLOW_UNSAFE_DATA_SCHEME: true,
MARKETPLACE_API_PREFIX: '/api/marketplace',
}))
})
it('should return true for data: URLs when ALLOW_UNSAFE_DATA_SCHEME is true', async () => {
const { isValidUrl: isValidUrlWithData } = await import('../utils')
expect(isValidUrlWithData('data:image/png;base64,abc123')).toBe(true)
})
})
describe('getMarkdownImageURL', () => {
it('should return the original URL when it does not match the asset regex', () => {
expect(getMarkdownImageURL('https://example.com/image.png')).toBe('https://example.com/image.png')
})
it('should transform ./_assets URL without pathname', () => {
const result = getMarkdownImageURL('./_assets/icon.png')
expect(result).toBe('/api/marketplace/plugins//_assets/icon.png')
})
it('should transform ./_assets URL with pathname', () => {
const result = getMarkdownImageURL('./_assets/icon.png', 'my-plugin/')
expect(result).toBe('/api/marketplace/plugins/my-plugin//_assets/icon.png')
})
it('should transform _assets URL without leading dot-slash', () => {
const result = getMarkdownImageURL('_assets/logo.svg')
expect(result).toBe('/api/marketplace/plugins//_assets/logo.svg')
})
it('should transform _assets URL with pathname', () => {
const result = getMarkdownImageURL('_assets/logo.svg', 'org/plugin/')
expect(result).toBe('/api/marketplace/plugins/org/plugin//_assets/logo.svg')
})
it('should not transform URLs that contain _assets in the middle', () => {
expect(getMarkdownImageURL('https://cdn.example.com/_assets/image.png'))
.toBe('https://cdn.example.com/_assets/image.png')
})
it('should use empty string for pathname when undefined', () => {
const result = getMarkdownImageURL('./_assets/test.png')
expect(result).toBe('/api/marketplace/plugins//_assets/test.png')
})
})
describe('getMarkdownImageURL with trailing slash prefix', () => {
beforeEach(() => {
vi.resetModules()
vi.doMock('@/config', () => ({
ALLOW_UNSAFE_DATA_SCHEME: false,
MARKETPLACE_API_PREFIX: '/api/marketplace/',
}))
})
it('should not add extra slash when prefix ends with slash', async () => {
const { getMarkdownImageURL: getURL } = await import('../utils')
const result = getURL('./_assets/icon.png', 'my-plugin/')
expect(result).toBe('/api/marketplace/plugins/my-plugin//_assets/icon.png')
})
})
})