import { expect, test } from '../fixtures/auth';

test.describe('大模型 / 供应商管理', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/llm/providers');
    await page.getByRole('table').waitFor();
  });

  test('展示列表及主要操作', async ({ page }) => {
    await expect(page.getByRole('button', { name: '新增供应商' })).toBeVisible();
    await expect(page.getByRole('table')).toBeVisible();
    await expect(page.getByText('OpenAI', { exact: true })).toBeVisible();
    await expect(page.getByText('https://api.openai.com/v1')).toBeVisible();
  });

  test('可搜索供应商', async ({ page }) => {
    await page.getByPlaceholder('搜索供应商名称').fill('DeepSeek');
    await expect(page.getByText('DeepSeek', { exact: true })).toBeVisible();
    await expect(page.getByText('OpenAI', { exact: true })).toHaveCount(0);
  });

  test('可新增供应商', async ({ page }) => {
    await page.getByRole('button', { name: '新增供应商' }).click();
    const dialog = page.getByRole('dialog', { name: '新增供应商' });
    await expect(dialog).toBeVisible();

    await dialog.getByLabel('供应商名称').fill('测试供应商');
    await dialog.getByLabel('Base URL').fill('https://api.test.com/v1');
    await dialog.getByRole('button', { name: '保 存' }).click();

    await expect(page.getByText('测试供应商', { exact: true })).toBeVisible();
  });

  test('可切换供应商状态并测试连通', async ({ page }) => {
    const statusSwitch = page.getByRole('switch', { name: 'OpenAI状态' });
    await expect(statusSwitch).toBeChecked();
    await statusSwitch.click();
    await expect(statusSwitch).not.toBeChecked();

    const row = page.getByRole('row').filter({ hasText: 'OpenAI' });
    await row.getByRole('button', { name: '测试连通' }).click();
    await expect(page.getByText('OpenAI 连通正常')).toBeVisible();
  });

  test('可编辑凭证', async ({ page }) => {
    const row = page.getByRole('row').filter({ hasText: 'DeepSeek' });
    await row.getByRole('button', { name: '编辑凭证' }).click();
    const dialog = page.getByRole('dialog', { name: '编辑供应商' });
    await expect(dialog).toBeVisible();
    await dialog.getByLabel('API Key').fill('sk-test-key');
    await dialog.getByRole('button', { name: '保 存' }).click();
    await expect(page.getByText('供应商已更新')).toBeVisible();
  });
});
