119 lines
2.9 KiB
JavaScript
119 lines
2.9 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
const {
|
|
validateAndNormalizeReportInput,
|
|
toDatabaseRecord,
|
|
} = require('../src/report-service');
|
|
|
|
function createResponseInfo() {
|
|
return {
|
|
code: 0,
|
|
message: 'success',
|
|
data: {
|
|
reportId: 'report-001',
|
|
status: 'SUCCESS',
|
|
name: '测试报告',
|
|
},
|
|
};
|
|
}
|
|
|
|
function createPersistRequest() {
|
|
return {
|
|
reportId: 'report-001',
|
|
sourceType: 'MANUAL_CAPTURE',
|
|
sourceReportId: null,
|
|
aadvid: '1648829117232140',
|
|
payload: {
|
|
name: '测试报告',
|
|
startTime: '2025-03-01',
|
|
endTime: '2026-02-28',
|
|
categories: [],
|
|
analysisDims: ['MARKETOVERVIEW'],
|
|
},
|
|
};
|
|
}
|
|
|
|
test('validateAndNormalizeReportInput accepts persist request with payload', () => {
|
|
const input = createPersistRequest();
|
|
|
|
const result = validateAndNormalizeReportInput(input);
|
|
|
|
assert.equal(result.reportId, 'report-001');
|
|
assert.deepEqual(result.reportInfo, input.payload);
|
|
});
|
|
|
|
test('validateAndNormalizeReportInput accepts response info with data.reportId', () => {
|
|
const input = createResponseInfo();
|
|
|
|
const result = validateAndNormalizeReportInput(input);
|
|
|
|
assert.equal(result.reportId, 'report-001');
|
|
assert.deepEqual(result.reportInfo, input);
|
|
});
|
|
|
|
test('validateAndNormalizeReportInput accepts response info with data.report_id', () => {
|
|
const input = {
|
|
code: 0,
|
|
data: {
|
|
report_id: 'report-002',
|
|
},
|
|
};
|
|
|
|
const result = validateAndNormalizeReportInput(input);
|
|
|
|
assert.equal(result.reportId, 'report-002');
|
|
assert.deepEqual(result.reportInfo, input);
|
|
});
|
|
|
|
test('validateAndNormalizeReportInput rejects response info without a report id', () => {
|
|
assert.throws(
|
|
() =>
|
|
validateAndNormalizeReportInput({
|
|
code: 0,
|
|
data: {},
|
|
}),
|
|
(error) => {
|
|
assert.equal(error.code, 'VALIDATION_ERROR');
|
|
assert.match(error.message, /report[_ ]?id/i);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test('toDatabaseRecord maps response info into segmented_market_reports columns', () => {
|
|
const normalized = validateAndNormalizeReportInput(createResponseInfo());
|
|
|
|
const record = toDatabaseRecord(normalized);
|
|
|
|
assert.deepEqual(record, {
|
|
report_id: 'report-001',
|
|
report_info: {
|
|
code: 0,
|
|
message: 'success',
|
|
data: {
|
|
reportId: 'report-001',
|
|
status: 'SUCCESS',
|
|
name: '测试报告',
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
test('toDatabaseRecord maps persist request payload into segmented_market_reports report_info', () => {
|
|
const normalized = validateAndNormalizeReportInput(createPersistRequest());
|
|
|
|
const record = toDatabaseRecord(normalized);
|
|
|
|
assert.deepEqual(record, {
|
|
report_id: 'report-001',
|
|
report_info: {
|
|
name: '测试报告',
|
|
startTime: '2025-03-01',
|
|
endTime: '2026-02-28',
|
|
categories: [],
|
|
analysisDims: ['MARKETOVERVIEW'],
|
|
},
|
|
});
|
|
});
|