import { describe, it, expect } from 'vitest' import { artifactKindOf, artifactSrcDoc, extractArtifacts, ARTIFACT_SANDBOX } from './artifacts' const HTML = '\nHello page

Hi

' describe('artifactKindOf', () => { it('recognises html and svg fences, and untagged fences by their first tag', () => { expect(artifactKindOf('html', '
x
')).toBe('html') expect(artifactKindOf('HTML', '

x

')).toBe('html') expect(artifactKindOf('svg', '')).toBe('svg') expect(artifactKindOf('', HTML)).toBe('html') expect(artifactKindOf('xml', '')).toBe('svg') expect(artifactKindOf('html', '')).toBe('svg') }) it('leaves ordinary code alone', () => { expect(artifactKindOf('js', 'console.log(1)')).toBeNull() expect(artifactKindOf('python', 'print(1)')).toBeNull() expect(artifactKindOf('', 'const x = 1')).toBeNull() expect(artifactKindOf('jsx', '
')).toBeNull() }) }) describe('extractArtifacts', () => { it('finds runnable fences in document order with titles and stable ids', () => { const md = `Here you go:\n\n\`\`\`html\n${HTML}\n\`\`\`\n\nAnd a script:\n\n\`\`\`js\nalert(1)\n\`\`\`\n\n\`\`\`svg\n\n\`\`\`\n` const found = extractArtifacts(md, 3) expect(found.map(a => [a.id, a.kind, a.title, a.filename])).toEqual([ ['3:1', 'html', 'Hello page', 'artifact-1.html'], ['3:3', 'svg', null, 'artifact-3.svg'], ]) expect(found[0].code).toBe(HTML) }) it('accepts tilde fences and indented fences, and ignores an unterminated one while streaming', () => { expect(extractArtifacts('~~~html\n

a

\n~~~')).toHaveLength(1) expect(extractArtifacts(' ```html\n

a

\n ```')).toHaveLength(1) expect(extractArtifacts('```html\n

still typing')).toHaveLength(0) }) it('does not treat a fence inside inline text as a block', () => { expect(extractArtifacts('Use ```html``` fences.')).toHaveLength(0) }) }) describe('artifactSrcDoc', () => { it('passes html through and wraps svg in a centring page', () => { const [html] = extractArtifacts(`\`\`\`html\n${HTML}\n\`\`\``) expect(artifactSrcDoc(html)).toBe(HTML) const [svg] = extractArtifacts('```svg\n\n```') const doc = artifactSrcDoc(svg) expect(doc.startsWith('')).toBe(true) expect(doc).toContain('') }) }) describe('sandbox', () => { it('never grants the preview the dashboard origin', () => { expect(ARTIFACT_SANDBOX).toContain('allow-scripts') expect(ARTIFACT_SANDBOX).not.toContain('allow-same-origin') expect(ARTIFACT_SANDBOX).not.toContain('allow-top-navigation') }) })