import { afterEach, describe, expect, it, vi } from 'vitest' import { writeFormattedClipboard } from './qaWeeklyClipboard' afterEach(() => vi.unstubAllGlobals()) describe('rendered weekly clipboard', () => { it.each(['missing', 'denied'])('uses the native rich copy path when the clipboard API is %s', async (kind) => { vi.stubGlobal( 'navigator', kind === 'missing' ? {} : { clipboard: { write: vi.fn().mockRejectedValue(Error('denied')) } } ) vi.stubGlobal( 'ClipboardItem', class { constructor(readonly data: Record) {} } ) const fallback = vi.fn(() => true) await writeFormattedClipboard({ html: '

正文

', text: '正文' }, fallback) expect(fallback).toHaveBeenCalledOnce() }) it('reports a failure instead of claiming success when neither copy path succeeds', async () => { vi.stubGlobal('navigator', {}) await expect(writeFormattedClipboard({ html: '

正文

', text: '正文' }, () => false)).rejects.toThrow( '复制未成功' ) }) it('writes rendered HTML plus readable text, never Markdown source', async () => { const write = vi.fn().mockResolvedValue(undefined) vi.stubGlobal('navigator', { clipboard: { write } }) vi.stubGlobal( 'ClipboardItem', class { constructor(readonly data: Record) {} } ) const fallback = vi.fn(() => true) await writeFormattedClipboard( { html: '

Weekly report

重点

', text: 'Weekly report\n\n重点' }, fallback ) expect(write).toHaveBeenCalledTimes(1) const data = write.mock.calls[0][0][0].data expect(await data['text/html'].text()).toContain('

Weekly report

') expect(await data['text/plain'].text()).toBe('Weekly report\n\n重点') expect(fallback).not.toHaveBeenCalled() }) })