96 lines
2.6 KiB
JavaScript
96 lines
2.6 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
const {
|
|
validateAndNormalizeReportInput,
|
|
toDatabaseRecord,
|
|
} = require('../src/report-service');
|
|
|
|
function createManualPayload() {
|
|
return {
|
|
reportId: 'report-001',
|
|
sourceType: 'MANUAL_CAPTURE',
|
|
sourceReportId: null,
|
|
aadvid: '1648829117232140',
|
|
name: '测试报告',
|
|
price: ['1,100', '101,100000'],
|
|
rules: [{ keywords: ['奶粉'], op: 'INCLUDE' }],
|
|
analysisDims: ['MARKETOVERVIEW'],
|
|
categories: [{ id: '20028', name: '奶粉类目' }],
|
|
channels: ['ALL'],
|
|
startTime: '2025-03-01',
|
|
endTime: '2026-02-28',
|
|
periodType: 'MONTH',
|
|
userName: 'tester@example.com',
|
|
payload: {
|
|
name: '测试报告',
|
|
startTime: '2025-03-01',
|
|
endTime: '2026-02-28',
|
|
},
|
|
};
|
|
}
|
|
|
|
test('validateAndNormalizeReportInput accepts a valid manual report payload', () => {
|
|
const input = createManualPayload();
|
|
|
|
const result = validateAndNormalizeReportInput(input);
|
|
|
|
assert.equal(result.reportId, 'report-001');
|
|
assert.equal(result.sourceType, 'MANUAL_CAPTURE');
|
|
assert.equal(result.sourceReportId, null);
|
|
assert.equal(result.aadvid, '1648829117232140');
|
|
assert.equal(result.startTime, '2025-03-01');
|
|
assert.equal(result.endTime, '2026-02-28');
|
|
});
|
|
|
|
test('validateAndNormalizeReportInput rejects AUTO_COPY without sourceReportId', () => {
|
|
const input = {
|
|
...createManualPayload(),
|
|
reportId: 'report-002',
|
|
sourceType: 'AUTO_COPY',
|
|
sourceReportId: '',
|
|
};
|
|
|
|
assert.throws(
|
|
() => validateAndNormalizeReportInput(input),
|
|
(error) => {
|
|
assert.equal(error.code, 'VALIDATION_ERROR');
|
|
assert.match(error.message, /sourceReportId/i);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test('toDatabaseRecord maps API payload fields into database-ready values', () => {
|
|
const normalized = validateAndNormalizeReportInput({
|
|
...createManualPayload(),
|
|
reportId: 'report-003',
|
|
sourceType: 'AUTO_COPY',
|
|
sourceReportId: 'report-001',
|
|
});
|
|
|
|
const record = toDatabaseRecord(normalized);
|
|
|
|
assert.deepEqual(record, {
|
|
report_id: 'report-003',
|
|
source_type: 'AUTO_COPY',
|
|
source_report_id: 'report-001',
|
|
aadvid: '1648829117232140',
|
|
name: '测试报告',
|
|
price: ['1,100', '101,100000'],
|
|
rules: [{ keywords: ['奶粉'], op: 'INCLUDE' }],
|
|
analysis_dims: ['MARKETOVERVIEW'],
|
|
categories: [{ id: '20028', name: '奶粉类目' }],
|
|
transaction_channels: ['ALL'],
|
|
start_time: '2025-03-01',
|
|
end_time: '2026-02-28',
|
|
period_type: 'MONTH',
|
|
user_name: 'tester@example.com',
|
|
payload: {
|
|
name: '测试报告',
|
|
startTime: '2025-03-01',
|
|
endTime: '2026-02-28',
|
|
},
|
|
});
|
|
});
|