/**
 * L2 回归测试：TTS语音合成
 * 覆盖：音色列表、提供商列表、音频生成、状态查询、预览、下载
 */
import { test, expect } from '@playwright/test';
import { apiGet, apiPost, initApiClient, disposeApiClient } from '../helpers/api-client';
import { trackResource } from '../helpers/test-data';

let testAudioId: string;

test.describe('TTS语音合成 (TTS)', () => {
  test.beforeAll(async () => {
    await initApiClient();
  });

  test.afterAll(async () => {
    await disposeApiClient();
  });

  test('T01: 获取音色列表 GET /api/tts/voices', async () => {
    const res = await apiGet('/api/tts/voices');
    expect(res.code).toBe(0);
    expect(res.data?.voices).toBeDefined();
    expect(Array.isArray(res.data.voices)).toBe(true);
  });

  test('T02: 获取提供商列表 GET /api/tts/providers', async () => {
    const res = await apiGet('/api/tts/providers');
    expect(res.code).toBe(0);
    expect(res.data?.providers).toBeDefined();
  });

  test('T03: 生成音频任务 POST /api/tts/generate', async () => {
    const res = await apiPost('/api/tts/generate', {
      text: '这是一段自动化测试文本，用于验证TTS音频生成功能。',
      voiceId: 'Cherry',
      voiceParams: { speed: 1.0, pitch: 0, volume: 50 },
    });
    // 异步任务应该返回 0
    expect(res.code).toBe(0);
    testAudioId = res.data?.audioId;
    expect(testAudioId).toBeDefined();
  });

  test('T04: 查询音频状态 GET /api/tts/status/:audioId', async () => {
    if (!testAudioId) return test.skip();
    const res = await apiGet(`/api/tts/status/${testAudioId}`);
    expect([0, 404]).toContain(res.code);
  });

  test('T05: 获取下载信息 GET /api/tts/download/:audioId', async () => {
    // 使用一个可能存在的音频ID测试路由可达性
    const res = await apiGet('/api/tts/download/1');
    expect([0, 404, 500]).toContain(res.code);
  });
});
