import test from 'node:test'
import assert from 'node:assert/strict'
import {
  buildAgentRunChatCompletionsRequest,
  extractOpenAIChunkText,
  parseAgentRunIntegrationUrl,
} from './agentrun-openai.js'

const INTEGRATION_URL = 'https://1242739972938523.agentrun-data.cn-hangzhou.aliyuncs.com/agent-runtimes/AIPRO-SP/endpoints/Default/invocations/openai/v1/chat/completions'

test('parseAgentRunIntegrationUrl extracts account, region, runtime and endpoint', () => {
  assert.deepEqual(parseAgentRunIntegrationUrl(INTEGRATION_URL), {
    endpointPublicUrl: INTEGRATION_URL,
    accountId: '1242739972938523',
    regionId: 'cn-hangzhou',
    runtimeName: 'AIPRO-SP',
    endpointName: 'Default',
  })
})

test('buildAgentRunChatCompletionsRequest follows the AgentRun integration curl contract', () => {
  const req = buildAgentRunChatCompletionsRequest({
    endpointPublicUrl: INTEGRATION_URL,
    invocationKey: 'ak-secret',
    sessionId: 'ec1ac362-af13-4bd3-b06e-8b5a32e71669',
    messages: [{ role: 'user', content: '写一段代码,查询现在是几点?' }],
    stream: true,
  })

  assert.equal(req.url, INTEGRATION_URL)
  assert.equal(req.options.method, 'POST')
  assert.equal(req.options.headers['Content-Type'], 'application/json')
  assert.equal(req.options.headers.Authorization, 'Bearer ak-secret')
  assert.equal(req.options.headers['x-agentrun-session-id'], 'ec1ac362-af13-4bd3-b06e-8b5a32e71669')
  assert.deepEqual(JSON.parse(req.options.body), {
    messages: [{ role: 'user', content: '写一段代码,查询现在是几点?' }],
    stream: true,
  })
})

test('buildAgentRunChatCompletionsRequest forwards OpenAI-compatible tool fields', () => {
  const req = buildAgentRunChatCompletionsRequest({
    endpointPublicUrl: INTEGRATION_URL,
    invocationKey: 'ak-secret',
    sessionId: 'ec1ac362-af13-4bd3-b06e-8b5a32e71669',
    messages: [{ role: 'user', content: '列出文件' }],
    stream: true,
    tools: [{ type: 'function', function: { name: 'list_files', parameters: { type: 'object' } } }],
    toolChoice: 'auto',
  })

  assert.deepEqual(JSON.parse(req.options.body), {
    messages: [{ role: 'user', content: '列出文件' }],
    stream: true,
    tools: [{ type: 'function', function: { name: 'list_files', parameters: { type: 'object' } } }],
    tool_choice: 'auto',
  })
})

test('buildAgentRunChatCompletionsRequest forwards max token budget when provided', () => {
  const req = buildAgentRunChatCompletionsRequest({
    endpointPublicUrl: INTEGRATION_URL,
    sessionId: 'ec1ac362-af13-4bd3-b06e-8b5a32e71669',
    messages: [{ role: 'user', content: '生成完整 Skill 包' }],
    stream: true,
    maxTokens: 8192,
  })

  assert.equal(JSON.parse(req.options.body).max_tokens, 8192)
})

test('buildAgentRunChatCompletionsRequest rejects non-AgentRun URLs', () => {
  assert.throws(() => buildAgentRunChatCompletionsRequest({
    endpointPublicUrl: 'https://example.com/v1/chat/completions',
    sessionId: 'sid',
    messages: [],
  }), /AgentRun/)
})

test('extractOpenAIChunkText reads streaming and non-streaming content shapes', () => {
  assert.equal(extractOpenAIChunkText({
    choices: [{ delta: { content: '你好' } }],
  }), '你好')

  assert.equal(extractOpenAIChunkText({
    choices: [{ message: { content: '完成' } }],
  }), '完成')

  assert.equal(extractOpenAIChunkText({ choices: [] }), '')
})
