test(embeds): add Go race tests, Blog Editor Vitest suite, and README docs
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
// Embeds, the URL rule, uploads and labels of the Blog Editor (Phase 16.2
|
||||
// D-04, D-05, D-13, D-18).
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { EMBED_PROVIDERS, isEmbedProvider, parseEmbedUrl } from '../../assets/src/blogeditor/embed'
|
||||
import {
|
||||
DEFAULT_EMBED_PROVIDERS,
|
||||
EMBED_PROVIDERS,
|
||||
isEmbedProvider,
|
||||
isValidAspect,
|
||||
isValidHeight,
|
||||
parseEmbedUrl,
|
||||
resolveEmbedProviders,
|
||||
validateEmbedId,
|
||||
} from '../../assets/src/blogeditor/embed'
|
||||
import { MEDIA_PICK_EVENT, MEDIA_PICK_RESULT_EVENT } from '../../assets/src/blogeditor/element'
|
||||
import { isPolicyUrl } from '../../assets/src/blogeditor/extensions'
|
||||
import { format, labelsFor } from '../../assets/src/blogeditor/labels'
|
||||
import { previewBody, youtubeThumbnail } from '../../assets/src/blogeditor/preview'
|
||||
import { button, chooseImage, click, fill, form, mount, status } from './helpers'
|
||||
|
||||
afterEach(() => {
|
||||
@@ -278,3 +289,155 @@ describe('disabled and invalid', () => {
|
||||
expect(el.value).toBe('<p>a</p>')
|
||||
})
|
||||
})
|
||||
|
||||
describe('dynamic embed providers (Phase 38)', () => {
|
||||
const customOptions = JSON.stringify({
|
||||
embedProviders: [
|
||||
{ key: 'youtube', name: 'YouTube', idPattern: '^[A-Za-z0-9_-]{11}$', mode: 'url' },
|
||||
{
|
||||
key: 'viz',
|
||||
name: 'Interactive Chart',
|
||||
idPattern: '^[a-z0-9][a-z0-9-]{0,63}$',
|
||||
mode: 'id',
|
||||
placeholder: 'chart-slug',
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
it('resolveEmbedProviders defaults to youtube and ignores invalid keys, regexes, and duplicates', () => {
|
||||
expect(resolveEmbedProviders(undefined)).toEqual(DEFAULT_EMBED_PROVIDERS)
|
||||
expect(resolveEmbedProviders([])).toEqual(DEFAULT_EMBED_PROVIDERS)
|
||||
expect(resolveEmbedProviders('invalid')).toEqual(DEFAULT_EMBED_PROVIDERS)
|
||||
|
||||
const resolved = resolveEmbedProviders([
|
||||
{ key: 'youtube', name: 'YouTube', idPattern: '^[A-Za-z0-9_-]{11}$', mode: 'url' },
|
||||
{ key: 'viz', name: 'Chart', idPattern: '[a-z0-9-]+', mode: 'id', placeholder: 'demo-1' },
|
||||
{ key: 'viz', name: 'Duplicate', idPattern: '^[0-9]+$' },
|
||||
{ key: '../bad', name: 'Bad Key', idPattern: '^[a-z]+$' },
|
||||
{ key: 'BadCase', name: 'Upper', idPattern: '^[a-z]+$' },
|
||||
{ key: 'broken', name: 'Bad Regex', idPattern: '[unclosed' },
|
||||
{ key: 'empty', name: 'No Pattern', idPattern: '' },
|
||||
])
|
||||
expect(resolved.map((p) => p.key)).toEqual(['youtube', 'viz'])
|
||||
expect(validateEmbedId(resolved, 'viz', 'demo-1')).toBe(true)
|
||||
expect(validateEmbedId(resolved, 'viz', 'demo-1!')).toBe(false)
|
||||
expect(validateEmbedId(resolved, 'youtube', 'demo-1')).toBe(false)
|
||||
expect(validateEmbedId(resolved, 'youtube', ID)).toBe(true)
|
||||
|
||||
expect(isValidAspect('16/9')).toBe(true)
|
||||
expect(isValidAspect('4 / 3')).toBe(true)
|
||||
expect(isValidAspect('1.5')).toBe(true)
|
||||
expect(isValidAspect('16/9; background:red')).toBe(false)
|
||||
expect(isValidHeight('420')).toBe(true)
|
||||
expect(isValidHeight('420px')).toBe(true)
|
||||
expect(isValidHeight('-100px')).toBe(false)
|
||||
})
|
||||
|
||||
it('keeps the single URL input form when only youtube is registered', () => {
|
||||
const { el } = mount('<p>a</p>')
|
||||
click(button(el, 'embed'))
|
||||
const embedForm = form(el, 'embed')!
|
||||
expect(embedForm.querySelector('select[name="provider"]')).toBeNull()
|
||||
expect(embedForm.querySelector('input[name="url"]')).not.toBeNull()
|
||||
expect(embedForm.querySelector('input[name="embedId"]')).toBeNull()
|
||||
})
|
||||
|
||||
it('shows adaptive provider selector, ID fields, and Media Library fallback picker for custom providers', () => {
|
||||
const { el, changes } = mount('<p>Intro</p>', { options: customOptions })
|
||||
const picks: Array<{ id: string; mode: string; defaultFolder: string }> = []
|
||||
el.addEventListener(MEDIA_PICK_EVENT, (e) => {
|
||||
picks.push((e as CustomEvent<{ id: string; mode: string; defaultFolder: string }>).detail)
|
||||
})
|
||||
|
||||
click(button(el, 'embed'))
|
||||
const embedForm = form(el, 'embed')!
|
||||
const select = embedForm.querySelector<HTMLSelectElement>('select[name="provider"]')!
|
||||
expect(Array.from(select.options).map((o) => o.value)).toEqual(['youtube', 'viz'])
|
||||
|
||||
// Switch to custom ID-mode provider
|
||||
select.value = 'viz'
|
||||
select.dispatchEvent(new Event('change'))
|
||||
|
||||
// Pick fallback image from Media Library
|
||||
click(embedForm.querySelector('[data-action="pick-fallback"]'))
|
||||
expect(picks).toHaveLength(1)
|
||||
expect(picks[0]!.mode).toBe('image')
|
||||
el.dispatchEvent(
|
||||
new CustomEvent(MEDIA_PICK_RESULT_EVENT, {
|
||||
detail: {
|
||||
id: picks[0]!.id,
|
||||
item: {
|
||||
url: '/media/abc123/fallback.webp',
|
||||
alt: 'Chart preview',
|
||||
caption: 'Figure 1',
|
||||
width: 800,
|
||||
height: 450,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
fill(embedForm.querySelector('input[name="embedId"]'), 'demo-1')
|
||||
fill(embedForm.querySelector('input[name="aspect"]'), '4/3')
|
||||
click(embedForm.querySelector('[data-action="insert"]'))
|
||||
|
||||
expect(form(el, 'embed')).toBeNull()
|
||||
expect(changes).toHaveLength(1)
|
||||
expect(el.value).toContain(
|
||||
'<figure data-embed="viz" data-id="demo-1" data-aspect="4/3">' +
|
||||
'<img src="/media/abc123/fallback.webp" alt="Chart preview" width="800" height="450">' +
|
||||
'<figcaption>Figure 1</figcaption></figure>',
|
||||
)
|
||||
})
|
||||
|
||||
it('TipTap Embed node parses and round-trips custom embed with fallback without RawHtml', () => {
|
||||
const customHtml =
|
||||
'<figure data-embed="viz" data-id="demo-1" data-aspect="4/3">' +
|
||||
'<img src="/media/abc/fallback.webp" alt="Fallback">' +
|
||||
'<figcaption>Figure 1</figcaption></figure>'
|
||||
const docHtml = `<p>Before</p>${customHtml}${PLACEHOLDER}<p>After</p>`
|
||||
const { el, editor } = mount(docHtml, { options: customOptions })
|
||||
|
||||
expect(editor.state.doc.child(1).type.name).toBe('embed')
|
||||
expect(editor.state.doc.child(1).attrs).toMatchObject({
|
||||
provider: 'viz',
|
||||
id: 'demo-1',
|
||||
aspect: '4/3',
|
||||
fallbackSrc: '/media/abc/fallback.webp',
|
||||
fallbackAlt: 'Fallback',
|
||||
caption: 'Figure 1',
|
||||
})
|
||||
expect(editor.state.doc.child(2).type.name).toBe('embed')
|
||||
expect(editor.state.doc.child(2).attrs).toMatchObject({ provider: 'youtube', id: ID })
|
||||
|
||||
// Edit an unrelated paragraph and verify both embeds round-trip cleanly.
|
||||
editor.view.dispatch(editor.state.tr.insertText('X', 1))
|
||||
expect(el.value).toBe(`<p>XBefore</p>${customHtml}${PLACEHOLDER}<p>After</p>`)
|
||||
})
|
||||
|
||||
it('previewBody renders fallback img and figcaption for custom embeds and youtubeThumbnail for YouTube', () => {
|
||||
const providers = resolveEmbedProviders([
|
||||
{ key: 'youtube', name: 'YouTube', idPattern: '^[A-Za-z0-9_-]{11}$', mode: 'url' },
|
||||
{ key: 'viz', name: 'Interactive Chart', idPattern: '^[a-z0-9-]+$', mode: 'id' },
|
||||
])
|
||||
const labels = labelsFor('en')
|
||||
|
||||
const customWithFallback =
|
||||
'<figure data-embed="viz" data-id="demo-1" data-aspect="16/9">' +
|
||||
'<img src="/media/abc/fallback.webp" alt="Fallback" width="800" height="450">' +
|
||||
'<figcaption>Figure 1</figcaption></figure>'
|
||||
const customPreview = previewBody(customWithFallback, labels, providers)
|
||||
expect(customPreview).toContain('class="embed-card"')
|
||||
expect(customPreview).toContain('src="/media/abc/fallback.webp"')
|
||||
expect(customPreview).toContain('alt="Fallback"')
|
||||
expect(customPreview).toContain('<figcaption>Figure 1</figcaption>')
|
||||
expect(customPreview).not.toContain('img.youtube.com')
|
||||
|
||||
const ytPreview = previewBody(PLACEHOLDER, labels, providers)
|
||||
expect(ytPreview).toContain(`src="${youtubeThumbnail(ID)}"`)
|
||||
expect(ytPreview).toContain(`${labels.embedCard}: YouTube ${ID}`)
|
||||
|
||||
const customNoFallback = previewBody('<figure data-embed="viz" data-id="demo-2" data-height="420px"></figure>', labels, providers)
|
||||
expect(customNoFallback).toContain('Interactive Chart: demo-2 (420px)')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user