117 lines
3.5 KiB
JavaScript
117 lines
3.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const root = process.cwd();
|
|
const defaultBaselinePath = path.join(root, 'temp', 'method-output', 'method-output-baseline.json');
|
|
const defaultTargetPath = path.join(root, 'temp', 'generated-tests', 'system.method-output.generated.test.js');
|
|
const legacyBaselinePath = path.join(root, 'run', 'method-output-baseline.json');
|
|
let baselinePath = process.argv[2]
|
|
? path.resolve(process.argv[2])
|
|
: defaultBaselinePath;
|
|
const targetPath = process.argv[3]
|
|
? path.resolve(process.argv[3])
|
|
: defaultTargetPath;
|
|
|
|
if (!process.argv[2] && !fs.existsSync(baselinePath) && fs.existsSync(legacyBaselinePath)) {
|
|
baselinePath = legacyBaselinePath;
|
|
}
|
|
|
|
const supportClientPath = path.relative(
|
|
path.dirname(targetPath),
|
|
path.join(root, 'test', 'support', 'system_method_client')
|
|
).replace(/\\/g, '/');
|
|
const supportRequirePath = supportClientPath.startsWith('.') ? supportClientPath : `./${supportClientPath}`;
|
|
const describePath = path.relative(root, targetPath).replace(/\\/g, '/');
|
|
|
|
if (!fs.existsSync(baselinePath)) {
|
|
console.error(`Baseline not found: ${baselinePath}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const baseline = JSON.parse(fs.readFileSync(baselinePath, 'utf8'));
|
|
const cases = Array.isArray(baseline.cases) ? baseline.cases : [];
|
|
|
|
const header = `// @eggjs-test-generated
|
|
// @purpose 接口返回数据一致性校验(自动生成,请勿手改)
|
|
// @source ${path.relative(root, baselinePath).replace(/\\/g, '/')}
|
|
// @generatedAt ${new Date().toISOString()}
|
|
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
getAuthToken,
|
|
invokeSystemMethod,
|
|
} = require('${supportRequirePath}');
|
|
|
|
const VOLATILE_KEYS = new Set([
|
|
'timestamp',
|
|
'token',
|
|
'last_login_at',
|
|
'updated_at',
|
|
'update_at',
|
|
'created_at',
|
|
'create_at',
|
|
'login_time',
|
|
'iat',
|
|
'exp',
|
|
]);
|
|
|
|
function normalize(value) {
|
|
if (value === null || value === undefined) return value;
|
|
if (Array.isArray(value)) return value.map(item => normalize(item));
|
|
if (typeof value !== 'object') return value;
|
|
const out = {};
|
|
for (const key of Object.keys(value)) {
|
|
if (VOLATILE_KEYS.has(key)) {
|
|
out[key] = '__VOLATILE__';
|
|
continue;
|
|
}
|
|
out[key] = normalize(value[key]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
describe(${JSON.stringify(describePath)}, () => {
|
|
let token = '';
|
|
|
|
before(async () => {
|
|
const auth = await getAuthToken();
|
|
token = auth.token;
|
|
assert.ok(token, 'token should be available');
|
|
});
|
|
`;
|
|
|
|
const body = cases.map((item, idx) => {
|
|
const title = `[${idx + 1}] ${item.method} (sourceId=${item.sourceId})`;
|
|
const requestBody = JSON.stringify(item.requestBody || {}, null, 2);
|
|
const expected = JSON.stringify(item.normalized || {}, null, 2);
|
|
const platform = JSON.stringify((item.headers && item.headers.platform) || 'web');
|
|
const project = JSON.stringify((item.headers && item.headers.project) || 'wms');
|
|
|
|
return `
|
|
it(${JSON.stringify(title)}, async () => {
|
|
const res = await invokeSystemMethod(
|
|
${JSON.stringify(item.method)},
|
|
${requestBody},
|
|
token,
|
|
{ platform: ${platform}, project: ${project} }
|
|
);
|
|
|
|
assert.equal(res.status, ${Number(item.httpStatus || 200)});
|
|
const normalized = normalize(res.body);
|
|
const expected = ${expected};
|
|
assert.deepEqual(normalized, expected);
|
|
});
|
|
`;
|
|
}).join('\n');
|
|
|
|
const footer = `
|
|
});
|
|
`;
|
|
|
|
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
fs.writeFileSync(targetPath, header + body + footer, 'utf8');
|
|
|
|
console.log(`GENERATED ${targetPath}`);
|
|
console.log(`CASE_COUNT ${cases.length}`);
|