import { describe, it, expect, beforeEach, vi } from 'vitest' import { createMarkdownDownloadConfig } from './download' let mdText = '' let revokeSpy: ReturnType beforeEach(() => { mdText = '' vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:mock') revokeSpy = vi .spyOn(URL, 'revokeObjectURL') .mockImplementation(() => undefined) const RealBlob = global.Blob vi.stubGlobal( 'Blob', class extends RealBlob { constructor(parts: BlobPart[], opts?: BlobPropertyBag) { mdText = typeof parts[0] === 'string' ? parts[0] : '' super(parts, opts) } }, ) }) describe('createMarkdownDownloadConfig', () => { it('returns a single .md item by default', () => { const items = createMarkdownDownloadConfig({ filename: 'doc', getData: () => ({ content: '# hi' }), }) expect(items.map((i) => i.id)).toEqual(['md']) }) it('prepends PNG when getCaptureEl is provided', () => { const items = createMarkdownDownloadConfig({ filename: 'doc', getData: () => ({ content: '' }), getCaptureEl: () => document.createElement('div'), }) expect(items.map((i) => i.id)).toEqual(['png', 'md']) }) it('md resolve writes the source verbatim with a .md filename', async () => { const items = createMarkdownDownloadConfig({ filename: 'doc', getData: () => ({ content: '# title\n\nbody' }), }) const handle = await items.find((i) => i.id === 'md')!.resolve() expect(handle.url).toBe('blob:mock') expect(handle.filename).toBe('doc.md') expect(mdText).toBe('# title\n\nbody') handle.revoke?.() expect(revokeSpy).toHaveBeenCalledWith('blob:mock') }) it('md resolve falls back to empty content when data.content is missing', async () => { const items = createMarkdownDownloadConfig({ filename: 'doc', // missing content field — exercise the `?? ''` branch getData: () => ({}) as { content: string }, }) await items.find((i) => i.id === 'md')!.resolve() expect(mdText).toBe('') }) })