import assert from 'node:assert/strict' import test from 'node:test' import { resolveIndustryVoucherForTask } from './consume-on-deliver.js' import type { KuaishouIndustryVoucherRow } from '../../types/repository/rows.js' function voucher(partial: Partial & { voucher_code: string }): KuaishouIndustryVoucherRow { return { id: partial.id || 1, oid: partial.oid || 'OID-1', seller_id: partial.seller_id || 'seller', order_id: partial.order_id ?? null, task_id: partial.task_id ?? null, unit_index: partial.unit_index ?? 0, voucher_code: partial.voucher_code, token: partial.token || 'token', status: partial.status || 'UNUSED', send_callback_status: partial.send_callback_status || 'success', send_callback_attempt_count: partial.send_callback_attempt_count || 1, send_callback_last_error: partial.send_callback_last_error || '', send_callback_sent_at: partial.send_callback_sent_at || null, valid_start_time: partial.valid_start_time || 0, valid_end_time: partial.valid_end_time || 0, consume_serial_num: partial.consume_serial_num || '', consume_details_json: partial.consume_details_json || [], consumed_at: partial.consumed_at || null, destroyed_at: partial.destroyed_at || null, created_at: partial.created_at || '2026-07-01T00:00:00.000Z', updated_at: partial.updated_at || '2026-07-01T00:00:00.000Z', } as KuaishouIndustryVoucherRow } test('resolveIndustryVoucherForTask prefers task_id over unit_index', () => { const vouchers = [ voucher({ id: 1, voucher_code: 'A', task_id: 10, unit_index: 1 }), voucher({ id: 2, voucher_code: 'B', task_id: 11, unit_index: 2 }), ] const matched = resolveIndustryVoucherForTask({ id: 11, unit_index: 1 }, vouchers) assert.equal(matched?.voucher_code, 'B') }) test('resolveIndustryVoucherForTask falls back to unit_index when task_id missing', () => { const vouchers = [ voucher({ id: 1, voucher_code: 'A', task_id: null, unit_index: 2 }), ] const matched = resolveIndustryVoucherForTask({ id: 99, unit_index: 2 }, vouchers) assert.equal(matched?.voucher_code, 'A') }) test('resolveIndustryVoucherForTask returns null when no match', () => { const vouchers = [ voucher({ id: 1, voucher_code: 'A', task_id: 1, unit_index: 1 }), ] assert.equal(resolveIndustryVoucherForTask({ id: 2, unit_index: 3 }, vouchers), null) })