import { describe, it, expect, vi } from 'vitest'
import { useAiSummary } from '../useAiSummary'

vi.mock('../../api/client', () => ({
  api: { post: vi.fn() },
}))

describe('useAiSummary', () => {
  it('initial state is empty', () => {
    const { aiLoading, aiSummary, aiMode } = useAiSummary(12345)
    expect(aiLoading.value).toBe(false)
    expect(aiSummary.value).toBe('')
    expect(aiMode.value).toBe('one_liner')
  })

  it('generateAI sets loading and returns summary', async () => {
    const { api } = await import('../../api/client')
    vi.mocked(api.post).mockResolvedValue({ data: { summary: 'AI summary text' } })

    const { aiLoading, aiSummary, generateAI } = useAiSummary(12345)
    const promise = generateAI('one_liner')
    expect(aiLoading.value).toBe(true)
    await promise
    expect(aiSummary.value).toBe('AI summary text')
    expect(aiLoading.value).toBe(false)
  })

  it('handles 503 error with service unavailable message', async () => {
    const { api } = await import('../../api/client')
    vi.mocked(api.post).mockRejectedValue({ response: { status: 503 } })

    const { aiSummary, generateAI } = useAiSummary(12345)
    await generateAI('structured')
    expect(aiSummary.value).toContain('AI 服务暂不可用')
  })

  it('handles generic error with retry message', async () => {
    const { api } = await import('../../api/client')
    vi.mocked(api.post).mockRejectedValue(new Error('network error'))

    const { aiSummary, generateAI } = useAiSummary(12345)
    await generateAI('implication')
    expect(aiSummary.value).toBe('生成失败，请重试。')
  })
})
