import test from 'node:test'
import assert from 'node:assert/strict'
import {
  normalizeDeviceRow,
  buildMaintainOrderPayload,
  getDeviceOrderEndpoint
} from '../src/utils/waterSupplyUiModel.js'

test('normalizes alarm and related work-order fields for device pages', () => {
  const row = normalizeDeviceRow({
    equipmentId: 'EQ-1',
    equipmentCode: 'P-001',
    equipmentName: '一号压力计',
    alarmStatus: 1,
    alertLevel: 2,
    latestOrderNo: 'WO202606101703482640',
    latestMaintainStatus: '处理中'
  })

  assert.equal(row.deviceStatusText, '报警')
  assert.equal(row.alarmLevelText, '二级报警')
  assert.equal(row.relatedOrderNo, 'WO202606101703482640')
  assert.equal(row.workOrderStatusText, '处理中')
})

test('builds a repair dispatch payload with the selected responsible user', () => {
  const payload = buildMaintainOrderPayload(
    { equipmentId: 'EQ-1', equipmentCode: 'P-001', equipmentName: '一号压力计' },
    { description: '压力低于阈值', receiveUserId: 18 }
  )

  assert.deepEqual(payload, {
    equipmentId: 'EQ-1',
    equipmentCode: 'P-001',
    equipmentName: '一号压力计',
    maintainType: '故障维修',
    maintainContent: '压力低于阈值',
    receiveUserId: 18
  })
})

test('uses the abnormal-device order endpoint for device repair dispatch', () => {
  assert.equal(getDeviceOrderEndpoint('dispatch'), '/waterSupply/device/abnormal/order')
  assert.equal(getDeviceOrderEndpoint('abnormal'), null)
})
