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

import {
  normalizeInitialConversationMessages,
  normalizeSnapshotMessage,
  serializeSessionMessages
} from '../src/views/scripts/travelReimbursementConversationStateModel.js'

test('Orchestrator 会话恢复 assistant 的 application preview，并保留既有结果字段', () => {
  const applicationPreview = {
    decision_id: 'decision-session-1',
    fields: {
      applicationType: '差旅费用申请',
      location: '上海',
      reason: '客户现场实施'
    },
    memoryApplications: [{
      memoryId: 'memory-session-1',
      fieldKey: 'reason',
      fieldLabel: '申请事由',
      value: '客户现场实施',
      status: 'applied'
    }]
  }
  const [message] = normalizeInitialConversationMessages({
    messages: [{
      id: 'assistant-message-1',
      role: 'assistant',
      content: '申请预览已生成',
      created_at: '2026-07-14T08:00:00Z',
      message_json: {
        orchestrator_payload: {
          result: {
            application_preview: applicationPreview,
            learning_receipts: [{
              memoryId: 'memory-session-2',
              fieldKey: 'transport_mode',
              fieldLabel: '出行方式',
              value: '火车',
              status: 'candidate'
            }],
            draft_payload: { claim_id: 'claim-draft-1' },
            review_payload: { summary: '复核通过' },
            risk_flags: [{ code: 'policy-limit' }]
          }
        }
      }
    }]
  })

  assert.deepEqual(message.applicationPreview, applicationPreview)
  assert.equal(message.applicationLearningReceipts[0].memoryId, 'memory-session-2')
  assert.deepEqual(message.draftPayload, { claim_id: 'claim-draft-1' })
  assert.deepEqual(message.reviewPayload, { summary: '复核通过' })
  assert.deepEqual(message.riskFlags, [{ code: 'policy-limit' }])
})

test('本地快照跨刷新保留记忆应用和学习回执', () => {
  const [serialized] = serializeSessionMessages([{
    id: 'assistant-memory-snapshot',
    role: 'assistant',
    text: '申请预览',
    applicationPreview: {
      fields: { reason: '客户现场实施' },
      memoryApplications: [{
        memoryId: 'memory-local-applied',
        fieldKey: 'reason',
        fieldLabel: '申请事由',
        value: '客户现场实施',
        status: 'applied'
      }]
    },
    applicationLearningReceipts: [{
      memoryId: 'memory-local-candidate',
      fieldKey: 'transport_mode',
      fieldLabel: '出行方式',
      value: '火车',
      status: 'candidate'
    }]
  }])
  const restored = normalizeSnapshotMessage(serialized)

  assert.equal(restored.applicationPreview.memoryApplications[0].status, 'applied')
  assert.equal(restored.applicationLearningReceipts[0].status, 'candidate')
})

test('user 消息不能从 payload 恢复 application preview', () => {
  const [message] = normalizeInitialConversationMessages({
    messages: [{
      id: 'user-message-1',
      role: 'user',
      content: '保存草稿',
      message_json: {
        orchestrator_payload: {
          result: {
            application_preview: { fields: { reason: '伪造预览' } }
          }
        }
      }
    }]
  })

  assert.equal(message.applicationPreview, null)
})
