// bot/src/__tests__/channelKey.test.ts
// Testa extractProjectKey com mocks do WebClient — CA-02

import { extractProjectKey, ChannelKeyMissingError } from '../channelKey'
import type { WebClient } from '@slack/web-api'

function makeClient(topic: string, purpose: string): WebClient {
  return {
    conversations: {
      info: jest.fn().mockResolvedValue({
        channel: {
          topic:   { value: topic },
          purpose: { value: purpose },
        },
      }),
    },
  } as unknown as WebClient
}

describe('extractProjectKey', () => {
  test('extrai key do tópico do canal (CA-02)', async () => {
    const client = makeClient('Projeto SDD — key: PROJ', '')
    const key = await extractProjectKey(client, 'C001')
    expect(key).toBe('PROJ')
  })

  test('key em purpose é usada quando topic não tem (CA-02)', async () => {
    const client = makeClient('Sem chave aqui', 'Canal do time — key: TEAM')
    const key = await extractProjectKey(client, 'C002')
    expect(key).toBe('TEAM')
  })

  test('lança ChannelKeyMissingError se key ausente (CA-02)', async () => {
    const client = makeClient('Sem nada', 'Também sem nada')
    await expect(extractProjectKey(client, 'C003')).rejects.toThrow(ChannelKeyMissingError)
  })

  test('key é retornada em maiúsculas', async () => {
    const client = makeClient('key: abc123', '')
    const key = await extractProjectKey(client, 'C004')
    expect(key).toBe('ABC123')
  })
})
