import test from 'node:test'
import assert from 'node:assert/strict'
import {
  mapCodexJsonEvent,
  mapQoderCloudEvent,
  normalizeArtifact,
  parseJsonLine,
} from './runtime-events.js'

test('parseJsonLine accepts JSONL and ignores non-JSON diagnostics', () => {
  assert.deepEqual(parseJsonLine('{"type":"turn.started"}'), { type: 'turn.started' })
  assert.equal(parseJsonLine('2026-07-19 WARN diagnostic'), null)
  assert.equal(parseJsonLine(''), null)
})

test('maps Codex agent messages and provider session events', () => {
  assert.deepEqual(mapCodexJsonEvent({
    type: 'thread.started',
    thread_id: 'thread_123',
  }), [{ t: 'provider_session', provider: 'local-codex', providerSessionId: 'thread_123' }])

  assert.deepEqual(mapCodexJsonEvent({
    type: 'item.completed',
    item: { type: 'agent_message', text: '正在分析' },
  }), [{ t: 'delta', v: '正在分析' }])
})

test('maps only provider-visible Codex reasoning to reasoning summaries', () => {
  assert.deepEqual(mapCodexJsonEvent({
    type: 'item.completed',
    item: { type: 'reasoning', text: '先检查输入材料' },
  }), [{ t: 'reasoning_summary', v: '先检查输入材料' }])

  assert.deepEqual(mapCodexJsonEvent({ type: 'unknown', hidden_chain: '不可见' }), [])
})

test('maps Codex command lifecycle to tool events', () => {
  assert.deepEqual(mapCodexJsonEvent({
    type: 'item.started',
    item: { id: 'item_7', type: 'command_execution', command: 'pwd' },
  }), [{ t: 'tool_call', name: 'shell', args: { command: 'pwd' }, providerItemId: 'item_7' }])

  assert.deepEqual(mapCodexJsonEvent({
    type: 'item.completed',
    item: { id: 'item_7', type: 'command_execution', command: 'pwd', aggregated_output: '/tmp\n', exit_code: 0 },
  }), [{ t: 'tool_result', name: 'shell', result: '/tmp\n', providerItemId: 'item_7', exitCode: 0 }])
})

test('maps QODER status, reasoning, tool and agent message events', () => {
  assert.deepEqual(mapQoderCloudEvent({
    event: 'session.status_running',
    id: 'evt_1',
    data: { turn_id: 'turn_1' },
  }), [{
    t: 'status',
    v: 'QODER Cloud Agent 正在执行',
    providerEvent: 'session.status_running',
    providerEventId: 'evt_1',
    providerTurnId: 'turn_1',
  }])

  assert.deepEqual(mapQoderCloudEvent({
    event: 'agent.thinking',
    id: 'evt_2',
    data: { turn_id: 'turn_1', content: [{ type: 'text', text: '先检查流程文件' }] },
  }), [{
    t: 'reasoning_summary',
    v: '先检查流程文件',
    providerEvent: 'agent.thinking',
    providerEventId: 'evt_2',
    providerTurnId: 'turn_1',
  }])

  assert.deepEqual(mapQoderCloudEvent({
    event: 'tool.started',
    id: 'evt_3',
    data: { turn_id: 'turn_1', tool_name: 'read', input: { path: 'SOP.md' } },
  }), [{
    t: 'tool_call',
    name: 'read',
    args: { path: 'SOP.md' },
    providerEvent: 'tool.started',
    providerEventId: 'evt_3',
    providerTurnId: 'turn_1',
  }])

  assert.deepEqual(mapQoderCloudEvent({
    event: 'agent.message',
    id: 'evt_4',
    data: { turn_id: 'turn_1', content: [{ type: 'text', text: '已完成' }] },
  }), [{
    t: 'delta',
    v: '已完成',
    providerEvent: 'agent.message',
    providerEventId: 'evt_4',
    providerTurnId: 'turn_1',
  }])
})

test('normalizes provider file metadata without embedding bytes', () => {
  assert.deepEqual(normalizeArtifact({
    path: 'reports/review.md',
    size: 321,
    content: 'do not embed',
  }, { sessionId: 'session_1', provider: 'qoder-cloud-agent' }), {
    name: 'reports/review.md',
    path: 'reports/review.md',
    size: 321,
    mime: '',
    sessionId: 'session_1',
    provider: 'qoder-cloud-agent',
    url: '',
  })
})
