import { beforeEach, describe, expect, it, vi } from 'vitest';

import { copyTextWithFeedback } from '../clipboard-feedback';

const { copyTextToClipboard, error, success } = vi.hoisted(() => ({
  copyTextToClipboard: vi.fn(),
  error: vi.fn(),
  success: vi.fn(),
}));

vi.mock('@easyflow/utils', () => ({
  copyTextToClipboard,
}));

vi.mock('element-plus', () => ({
  ElMessage: {
    error,
    success,
  },
}));

describe('copyTextWithFeedback', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('copies through the shared Chrome 90 compatible helper', async () => {
    copyTextToClipboard.mockResolvedValue('exec-command');

    await expect(
      copyTextWithFeedback('https://example.test/share', '复制成功'),
    ).resolves.toBe(true);

    expect(copyTextToClipboard).toHaveBeenCalledWith(
      'https://example.test/share',
    );
    expect(success).toHaveBeenCalledWith('复制成功');
    expect(error).not.toHaveBeenCalled();
  });

  it('reports a recoverable error when copying fails', async () => {
    copyTextToClipboard.mockRejectedValue(new Error('copy denied'));

    await expect(
      copyTextWithFeedback('content', '复制成功', '复制失败，请手动复制'),
    ).resolves.toBe(false);

    expect(error).toHaveBeenCalledWith('复制失败，请手动复制');
    expect(success).not.toHaveBeenCalled();
  });
});
