import { describe, expect, it } from 'vitest'

import {
  extractAssistantStreamDelta,
  normalizeAssistantSourceChunks,
  parseServerSentEvents
} from '../src/ai-assistant/stream.js'

describe('assistant stream helpers', () => {
  it('parses SSE events with chunks and streamed deltas', () => {
    const events = parseServerSentEvents([
      'event: chunks',
      'data: [{"id":"1","score":0.8,"text":"Source","item":{"key":"/guide.md","metadata":{"title":"Guide"}}}]',
      '',
      'data: {"choices":[{"delta":{"content":"Hello"}}]}',
      '',
      'data: [DONE]',
      ''
    ].join('\n'))

    expect(events).toHaveLength(3)
    expect(extractAssistantStreamDelta(events[0])).toMatchObject({
      done: false,
      content: '',
      chunks: [{ id: '1', key: '/guide.md', title: 'Guide', score: 0.8 }]
    })
    expect(extractAssistantStreamDelta(events[1])).toMatchObject({ content: 'Hello' })
    expect(extractAssistantStreamDelta(events[2])).toEqual({ done: true, content: '', chunks: [] })
  })

  it('normalizes source chunks from AI Search response shape', () => {
    expect(normalizeAssistantSourceChunks([
      {
        id: 'chunk-a',
        score: 0.72,
        text: '  Use MCP.  ',
        item: {
          key: 'manual/basic/agent-skills/overview.md',
          metadata: { title: 'Agent Skills' }
        }
      }
    ])).toEqual([
      {
        id: 'chunk-a',
        key: 'manual/basic/agent-skills/overview.md',
        title: 'Agent Skills',
        meta: 'manual/basic/agent-skills/overview',
        text: 'Use MCP.',
        score: 0.72
      }
    ])
  })

  it('derives readable labels from URL source keys', () => {
    expect(normalizeAssistantSourceChunks([
      {
        key: 'https://docsector.com/manual/content/blocks/quick-links/showcase',
        score: 0.4
      }
    ])).toEqual([
      {
        id: 'https://docsector.com/manual/content/blocks/quick-links/showcase',
        key: 'https://docsector.com/manual/content/blocks/quick-links/showcase',
        title: 'Quick Links / Showcase',
        meta: 'docsector.com/manual/content/blocks/quick-links/showcase',
        text: '',
        score: 0.4
      }
    ])
  })

  it('deduplicates source chunks that point to the same link', () => {
    expect(normalizeAssistantSourceChunks([
      {
        id: 'chunk-a',
        score: 0.2,
        text: 'First match',
        item: {
          key: '/manual/basic/agent-skills/overview.md',
          metadata: { title: 'Agent Skills' }
        }
      },
      {
        id: 'chunk-b',
        score: 0.91,
        text: 'Second match',
        item: {
          key: 'manual/basic/agent-skills/overview/',
          metadata: { title: 'Agent Skills Duplicate' }
        }
      }
    ])).toEqual([
      {
        id: 'chunk-a',
        key: '/manual/basic/agent-skills/overview.md',
        title: 'Agent Skills',
        meta: 'manual/basic/agent-skills/overview',
        text: 'First match',
        score: 0.91
      }
    ])
  })
})
