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

import { buildTaskListFilters } from './task-repo.js'

test('任务列表综合关键词覆盖任务订单商品与角色字段', () => {
  const result = buildTaskListFilters({
    keyword: '指挥官密钥',
    executorKey: 'kuaishou_ct_assisted',
    status: 'manual_review',
    dateFrom: '2026-09-01T00:00:00.000Z',
    dateTo: '2026-09-02T23:59:59.999Z',
  })

  assert.match(result.whereClause, /ft\.task_no.*ft\.platform_order_id.*ILIKE \$1/s)
  assert.match(result.whereClause, /oi\.sku_code ILIKE \$1/)
  assert.match(result.whereClause, /oi\.sku_name ILIKE \$1/)
  assert.match(result.whereClause, /kuaishouSendCode'.*itemTitle.*ILIKE \$1/s)
  assert.match(result.whereClause, /kuaishouSendCode'.*skuNick.*ILIKE \$1/s)
  assert.match(result.whereClause, /kcts\.role_id.*ctx\.role_id.*ILIKE \$1/s)
  assert.match(result.whereClause, /kcts\.role_name.*ctx\.role_name.*ctx\.nickname.*ILIKE \$1/s)
  assert.match(result.whereClause, /ft\.executor_key = \$2/)
  assert.match(result.whereClause, /ft\.task_status = \$3/)
  assert.match(result.whereClause, /ft\.created_at >= \$4/)
  assert.match(result.whereClause, /ft\.created_at <= \$5/)
  assert.deepEqual(result.params, [
    '%指挥官密钥%',
    'kuaishou_ct_assisted',
    'manual_review',
    '2026-09-01T00:00:00.000Z',
    '2026-09-02T23:59:59.999Z',
  ])
})

test('任务列表保留旧版分字段筛选参数', () => {
  const result = buildTaskListFilters({
    platformOrderId: '26245',
    taskNo: 'DT-1',
    skuCode: 'SKU-1',
    roleId: '10001',
  })

  assert.match(result.whereClause, /ft\.platform_order_id ILIKE \$1/)
  assert.match(result.whereClause, /ft\.task_no ILIKE \$2/)
  assert.match(result.whereClause, /oi\.sku_code = \$3/)
  assert.match(result.whereClause, /kcts\.role_id.*ctx\.role_id.*ILIKE \$4/s)
  assert.deepEqual(result.params, ['%26245%', '%DT-1%', 'SKU-1', '%10001%'])
})
