import test from 'node:test'
import assert from 'node:assert/strict'

import {
  buildChatCompletionResponse,
  buildSmokeEnv,
} from './fake_deepseek_server.mjs'

test('buildChatCompletionResponse returns Baidu tool calls for the first round', () => {
  const response = buildChatCompletionResponse({
    messages: [
      {role: 'system', content: 'You are sgClaw'},
      {role: 'user', content: '打开百度搜索天气'},
    ],
  })

  const message = response.choices[0].message
  assert.equal(message.content, '')
  assert.equal(message.tool_calls.length, 3)
  assert.equal(message.tool_calls[0].function.name, 'browser_action')
  assert.match(message.tool_calls[0].function.arguments, /"action":"navigate"/)
  assert.match(message.tool_calls[1].function.arguments, /"action":"type"/)
  assert.match(message.tool_calls[2].function.arguments, /"action":"click"/)
})

test('buildChatCompletionResponse returns Zhihu navigate tool call for the first round', () => {
  const response = buildChatCompletionResponse({
    messages: [
      {role: 'system', content: 'You are sgClaw'},
      {role: 'user', content: '打开知乎搜索天气'},
    ],
  })

  const message = response.choices[0].message
  assert.equal(message.content, '')
  assert.equal(message.tool_calls.length, 1)
  assert.match(
    message.tool_calls[0].function.arguments,
    /https:\/\/www\.zhihu\.com\/search\?type=content&q=%E5%A4%A9%E6%B0%94/,
  )
})

test('buildChatCompletionResponse returns final summaries expected by the existing smoke script', () => {
  const baiduResponse = buildChatCompletionResponse({
    messages: [
      {role: 'system', content: 'You are sgClaw'},
      {role: 'user', content: '打开百度搜索天气'},
      {role: 'assistant', content: '', tool_calls: [{id: 'call_baidu_1'}]},
      {role: 'tool', tool_call_id: 'call_baidu_1', content: '{"ok":true}'},
    ],
  })
  assert.equal(baiduResponse.choices[0].message.content, '已在百度搜索天气')

  const zhihuResponse = buildChatCompletionResponse({
    messages: [
      {role: 'system', content: 'You are sgClaw'},
      {role: 'user', content: '打开知乎搜索天气'},
      {role: 'assistant', content: '', tool_calls: [{id: 'call_zhihu_1'}]},
      {role: 'tool', tool_call_id: 'call_zhihu_1', content: '{"ok":true}'},
    ],
  })
  assert.equal(zhihuResponse.choices[0].message.content, '已在知乎搜索天气')
})

test('buildChatCompletionResponse rejects unsupported instructions clearly', () => {
  assert.throws(
    () =>
      buildChatCompletionResponse({
        messages: [{role: 'user', content: '打开豆瓣搜索天气'}],
      }),
    /unsupported smoke instruction: 打开豆瓣搜索天气/,
  )
})

test('buildChatCompletionResponse accepts ZeroClaw timestamp-prefixed instructions', () => {
  const response = buildChatCompletionResponse({
    messages: [
      {role: 'user', content: '[2026-03-26 16:11:26 +08:00] 打开百度搜索天气'},
    ],
  })

  const message = response.choices[0].message
  assert.equal(message.tool_calls.length, 3)
  assert.match(message.tool_calls[0].function.arguments, /https:\/\/www\.baidu\.com/)
})

test('buildSmokeEnv injects DeepSeek environment variables for the local fake server', () => {
  const env = buildSmokeEnv('http://127.0.0.1:32123', {
    PATH: '/usr/bin',
    HOME: '/tmp/home',
  })

  assert.equal(env.PATH, '/usr/bin')
  assert.equal(env.HOME, '/tmp/home')
  assert.equal(env.DEEPSEEK_API_KEY, 'sgclaw-smoke-test-key')
  assert.equal(env.DEEPSEEK_BASE_URL, 'http://127.0.0.1:32123')
  assert.equal(env.DEEPSEEK_MODEL, 'deepseek-chat')
})
