import test from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import ts from 'typescript' function evaluate(path, imports = {}) { const compiled = ts.transpileModule(readFileSync(path, 'utf8'), { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText const module = { exports: {} } new Function('require', 'module', 'exports', compiled)( (name) => { assert.ok(imports[name], `Unexpected dependency: ${name}`) return imports[name] }, module, module.exports ) return module.exports } const constants = evaluate('src/config/constants/trafficRecords.ts') const utils = evaluate('src/utils/business/trafficRecords.ts', { '@/config/constants/trafficRecords': constants }) test('时间范围:结束时间不含,跨度不超过 31 天', () => { assert.deepEqual(utils.toTimeQuery(['2026-11-01 00:00:00', '2026-11-02 00:00:00']), { start_time: '2026-11-01 00:00:00', end_time: '2026-11-02 00:00:00' }) assert.doesNotThrow(() => utils.toTimeQuery(['2026-10-01 00:00:00', '2026-11-01 00:00:00'])) assert.throws( () => utils.toTimeQuery(['2026-10-01 00:00:00', '2026-11-06 00:00:00']), /时间跨度不能超过 31 天/ ) assert.throws( () => utils.toTimeQuery(['2026-11-02 00:00:00', '2026-11-01 00:00:00']), /结束时间必须晚于开始时间/ ) assert.throws(() => utils.toTimeQuery(null), /请选择完整的时间范围/) assert.deepEqual(utils.toTimeQuery(null, false), {}) }) test('日期范围:含首尾,最多 31 天', () => { assert.deepEqual(utils.toDateQuery(['2026-10-01', '2026-10-31']), { start_date: '2026-10-01', end_date: '2026-10-31' }) assert.throws(() => utils.toDateQuery(['2026-10-01', '2026-11-01']), /日期跨度不能超过 31 天/) assert.throws(() => utils.toDateQuery(['2026-10-02', '2026-10-01']), /结束日期不得早于开始日期/) }) test('非平台账号必须填资产标识', () => { assert.doesNotThrow(() => utils.requireIdentifierForNonPlatform('', 1)) assert.doesNotThrow(() => utils.requireIdentifierForNonPlatform('', 2)) assert.throws(() => utils.requireIdentifierForNonPlatform(' ', 3), /请先输入资产标识/) assert.doesNotThrow(() => utils.requireIdentifierForNonPlatform('898600', 3)) assert.equal(utils.isEnterpriseAccount(4), true) assert.equal(utils.isPlatformAccount(3), false) }) test('筛选值处理', () => { assert.deepEqual(utils.compactQuery({ a: '', b: 0, c: null, d: undefined, e: false, f: 'x' }), { b: 0, e: false, f: 'x' }) assert.equal(utils.optionalPositiveInt('', 'ID'), undefined) assert.equal(utils.optionalPositiveInt('12', 'ID'), 12) assert.throws(() => utils.optionalPositiveInt('-1', '运营商账户 ID'), /运营商账户 ID须为正整数/) assert.equal(utils.optionalBoolean('true'), true) assert.equal(utils.optionalBoolean('false'), false) assert.equal(utils.optionalBoolean(''), undefined) assert.equal(utils.formatMb(12), '12 MB') assert.equal(utils.formatMb(1.256), '1.26 MB') assert.equal(utils.formatMb(null), '-') }) test('资产同步后的流量入账结果文案', () => { assert.equal(utils.describeTrafficRefresh(undefined), '') assert.equal( utils.describeTrafficRefresh({ result: 'ok', reading_mb: 1200, written: true, decision: 'written', delta_mb: 15 }), '本次读数 1200 MB,已写入,增量 15 MB' ) assert.equal( utils.describeTrafficRefresh({ result: 'ok', reading_mb: 1200, written: false, decision: 'queue_not_full' }), '本次读数 1200 MB,未写入:采集队列未满' ) assert.equal(utils.describeTrafficRefresh({ result: 'rate_limited' }), '流量读数限流,未写入') }) test('退款后处理状态与权限码常量', () => { assert.equal(constants.REFUND_ASSET_POST_STATUS.manualRequired, 3) assert.equal(constants.REFUND_ASSET_POST_STATUS_LABELS[3], '需人工处理') assert.equal(constants.REFUND_ASSET_POST_MAX_ATTEMPTS, 10) assert.equal(constants.TRAFFIC_RECORD_PERMISSIONS.refundAssetPostRetry, 'refund:asset_post_retry') assert.equal(constants.labelOf(constants.TRAFFIC_DECISION_LABELS, 'out_of_order'), '采集乱序') assert.equal(constants.labelOf(constants.TRAFFIC_DECISION_LABELS, 'new_value'), 'new_value') assert.equal(constants.labelOf(constants.TRAFFIC_DECISION_LABELS, ''), '-') }) test('卡 / 设备展示优先用 ICCID、设备号,没有时才显示内部 ID', () => { assert.equal( utils.assetDisplay({ iot_card_id: 367218, iccid: '89860624660030813727' }), '89860624660030813727' ) assert.equal( utils.assetDisplay({ device_id: 49323, device_virtual_no: '60003081372', device_imei: '869555083099518' }), '60003081372(IMEI 869555083099518)' ) assert.equal( utils.assetDisplay({ device_id: 1, device_virtual_no: '862639078983683', device_imei: '862639078983683' }), '862639078983683' ) assert.equal( utils.assetDisplay({ device_id: 1, device_imei: '869555083099518' }), '869555083099518' ) assert.equal(utils.assetDisplay({ iot_card_id: 367218 }), '卡ID 367218') assert.equal(utils.assetDisplay({ device_id: 49323 }), '设备ID 49323') assert.equal(utils.assetDisplay({}), '-') assert.equal(utils.nameOrId('移动账户A', 3), '移动账户A') assert.equal(utils.nameOrId('', 3), 'ID 3') assert.equal(utils.nameOrId('', 7, '账号ID'), '账号ID 7') assert.equal(utils.nameOrId(undefined, 0), '-') })