428 lines
12 KiB
JavaScript
428 lines
12 KiB
JavaScript
const fs = require('fs');
|
||
const path = require('path');
|
||
const mysql = require('mysql2/promise');
|
||
|
||
const ROOT = process.cwd();
|
||
const OUTPUT_DIR = path.join(ROOT, 'temp', 'method-output');
|
||
const LEGACY_RUN_DIR = path.join(ROOT, 'run');
|
||
const BASELINE_FILE = path.join(OUTPUT_DIR, 'method-output-baseline.json');
|
||
const BASELINE_SUMMARY = path.join(OUTPUT_DIR, 'method-output-baseline.md');
|
||
const COMPARE_FILE = path.join(OUTPUT_DIR, 'method-output-compare.json');
|
||
const LEGACY_BASELINE_FILE = path.join(LEGACY_RUN_DIR, 'method-output-baseline.json');
|
||
|
||
const MODE = (process.argv[2] || 'baseline').toLowerCase();
|
||
const LIMIT = Number(process.argv[3] || 8);
|
||
const TEST_USERNAME = process.env.WMS_TEST_USER || 'liufumingren';
|
||
const TEST_PASSWORD = process.env.WMS_TEST_PASS || '111111';
|
||
|
||
const READONLY_HINT = /(get|list|query|detail|info|stats|tree|search|load)/i;
|
||
const MUTATION_HINT = /(create|update|delete|remove|audit|confirm|complete|add|sync|synchronize|upload|assign|change|logout|login)/i;
|
||
const VOLATILE_KEYS = new Set([
|
||
'timestamp',
|
||
'token',
|
||
'last_login_at',
|
||
'updated_at',
|
||
'update_at',
|
||
'created_at',
|
||
'create_at',
|
||
'login_time',
|
||
'iat',
|
||
'exp',
|
||
]);
|
||
|
||
function ensureOutputDir() {
|
||
if (!fs.existsSync(OUTPUT_DIR)) {
|
||
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
||
}
|
||
}
|
||
|
||
function resolveBaselineFile() {
|
||
if (fs.existsSync(BASELINE_FILE)) return BASELINE_FILE;
|
||
if (fs.existsSync(LEGACY_BASELINE_FILE)) return LEGACY_BASELINE_FILE;
|
||
return BASELINE_FILE;
|
||
}
|
||
|
||
function readLocalConfig() {
|
||
const localCfgFactory = require(path.join(ROOT, 'config', 'config.local.js'));
|
||
const cfg = localCfgFactory({ name: 'wms' });
|
||
return cfg;
|
||
}
|
||
|
||
function buildBaseUrl(cfg) {
|
||
const host = cfg.cluster?.listen?.hostname && cfg.cluster.listen.hostname !== '0.0.0.0'
|
||
? cfg.cluster.listen.hostname
|
||
: '127.0.0.1';
|
||
const port = cfg.cluster?.listen?.port || 6060;
|
||
return `http://${host}:${port}`;
|
||
}
|
||
|
||
function loadSystemMethodDocs() {
|
||
const file = path.join(ROOT, 'methods', 'system.json');
|
||
const raw = fs.readFileSync(file, 'utf8');
|
||
const json = JSON.parse(raw);
|
||
const map = new Map();
|
||
const groups = json?.methods?.groups || [];
|
||
for (const group of groups) {
|
||
for (const item of group.list || []) {
|
||
map.set(item.method, {
|
||
groupName: group.groupName,
|
||
module: group.module,
|
||
description: item.description,
|
||
isNeedAuth: !!item.isNeedAuth,
|
||
isNeedLogin: !!item.isNeedLogin,
|
||
params: (item.params || []).map(p => ({
|
||
name: p.name,
|
||
type: p.type,
|
||
required: !!p.required,
|
||
source: p.source,
|
||
description: p.description,
|
||
})),
|
||
});
|
||
}
|
||
}
|
||
return map;
|
||
}
|
||
|
||
function safeJsonParse(text, fallback = null) {
|
||
if (!text || typeof text !== 'string') return fallback;
|
||
try {
|
||
return JSON.parse(text);
|
||
} catch {
|
||
return fallback;
|
||
}
|
||
}
|
||
|
||
function normalizeValue(value) {
|
||
if (value === null || value === undefined) return value;
|
||
if (Array.isArray(value)) return value.map(item => normalizeValue(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] = normalizeValue(value[key]);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
function stableStringify(obj) {
|
||
return JSON.stringify(obj);
|
||
}
|
||
|
||
function pickPlatform(headers, rowDevice) {
|
||
const fromHeaders = String(headers?.platform || '').trim().toLowerCase();
|
||
if (fromHeaders) return fromHeaders;
|
||
const fromDevice = String(rowDevice || '').trim().toLowerCase();
|
||
if (fromDevice === 'desktop') return 'web';
|
||
if (fromDevice) return fromDevice;
|
||
return 'web';
|
||
}
|
||
|
||
function pickProject(headers) {
|
||
const project = String(headers?.project || '').trim().toLowerCase();
|
||
if (!project) return 'wms';
|
||
if (project === 'all') return 'wms';
|
||
return project;
|
||
}
|
||
|
||
async function createDbConnection(cfg) {
|
||
const db = cfg.mysql.clients.wms;
|
||
return mysql.createConnection({
|
||
host: db.host,
|
||
port: Number(db.port),
|
||
user: db.user,
|
||
password: db.password,
|
||
database: db.database,
|
||
});
|
||
}
|
||
|
||
async function loadRecentSystemLogCases(conn, methodDocs, limit, username) {
|
||
const [rows] = await conn.query(`
|
||
SELECT id, method, body, headers, auth, device, result, created_at
|
||
FROM wms_system_log
|
||
WHERE method IS NOT NULL
|
||
AND method <> ''
|
||
AND body IS NOT NULL
|
||
AND body <> ''
|
||
ORDER BY id DESC
|
||
LIMIT 1200
|
||
`);
|
||
|
||
const preferred = [];
|
||
const fallback = [];
|
||
const seen = new Set();
|
||
|
||
for (const row of rows) {
|
||
if (!methodDocs.has(row.method)) continue;
|
||
if (row.method === 'user.login' || row.method === 'user.logout' || row.method === 'user.changeRole') continue;
|
||
|
||
const body = safeJsonParse(row.body, null);
|
||
if (!body || typeof body !== 'object') continue;
|
||
|
||
const signature = `${row.method}::${JSON.stringify(body)}`;
|
||
if (seen.has(signature)) continue;
|
||
seen.add(signature);
|
||
|
||
const headers = safeJsonParse(row.headers, {});
|
||
const auth = safeJsonParse(row.auth, {});
|
||
const fromUser = String(auth?.username || '').trim().toLowerCase() === String(username || '').trim().toLowerCase();
|
||
const methodLooksReadOnly = READONLY_HINT.test(row.method) && !MUTATION_HINT.test(row.method);
|
||
const historySuccess = typeof row.result === 'string' && row.result.includes('"status":200');
|
||
|
||
const item = {
|
||
source: 'wms_system_log',
|
||
sourceId: row.id,
|
||
sourceCreatedAt: row.created_at,
|
||
method: row.method,
|
||
body,
|
||
headers: {
|
||
platform: pickPlatform(headers, row.device),
|
||
project: pickProject(headers),
|
||
},
|
||
fromUser,
|
||
readOnlyHint: methodLooksReadOnly,
|
||
historySuccess,
|
||
};
|
||
|
||
if (fromUser && methodLooksReadOnly && historySuccess) preferred.push(item);
|
||
else if (fromUser && methodLooksReadOnly) preferred.push(item);
|
||
else if (methodLooksReadOnly) fallback.push(item);
|
||
|
||
if (preferred.length >= limit * 2) break;
|
||
}
|
||
|
||
const result = [];
|
||
const methodSeen = new Set();
|
||
|
||
for (const item of preferred) {
|
||
if (result.length >= limit) break;
|
||
if (methodSeen.has(item.method)) continue;
|
||
result.push(item);
|
||
methodSeen.add(item.method);
|
||
}
|
||
|
||
for (const item of preferred) {
|
||
if (result.length >= limit) break;
|
||
result.push(item);
|
||
}
|
||
|
||
for (const item of fallback) {
|
||
if (result.length >= limit) break;
|
||
if (methodSeen.has(item.method)) continue;
|
||
result.push(item);
|
||
methodSeen.add(item.method);
|
||
}
|
||
|
||
for (const item of fallback) {
|
||
if (result.length >= limit) break;
|
||
result.push(item);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
async function callSystemMethod(baseUrl, method, body, headers, token) {
|
||
const ts = Math.floor(Date.now() / 1000);
|
||
const url = `${baseUrl}/system?method=${encodeURIComponent(method)}&v=1×tamp=${ts}`;
|
||
|
||
const reqHeaders = {
|
||
platform: headers.platform || 'web',
|
||
project: headers.project || 'wms',
|
||
'content-type': 'application/json',
|
||
};
|
||
if (token) {
|
||
reqHeaders.authorization = `Bearer ${token}`;
|
||
}
|
||
|
||
const resp = await fetch(url, {
|
||
method: 'POST',
|
||
headers: reqHeaders,
|
||
body: JSON.stringify(body || {}),
|
||
});
|
||
|
||
const text = await resp.text();
|
||
const data = safeJsonParse(text, { __raw_text__: text });
|
||
return {
|
||
httpStatus: resp.status,
|
||
response: data,
|
||
normalized: normalizeValue(data),
|
||
};
|
||
}
|
||
|
||
function extractToken(loginResponse) {
|
||
const direct = loginResponse?.response?.data?.token;
|
||
if (typeof direct === 'string' && direct.length > 10) return direct;
|
||
return '';
|
||
}
|
||
|
||
function renderBaselineMarkdown(baseline) {
|
||
let md = '';
|
||
md += '# method-output-baseline\n\n';
|
||
md += `GeneratedAt: ${baseline.generatedAt}\n`;
|
||
md += `BaseUrl: ${baseline.baseUrl}\n`;
|
||
md += `User: ${baseline.user}\n`;
|
||
md += `CaseCount: ${baseline.cases.length}\n\n`;
|
||
md += '| method | source | sourceId | status | group | note |\n';
|
||
md += '| --- | --- | --- | --- | --- | --- |\n';
|
||
for (const c of baseline.cases) {
|
||
md += `| ${c.method} | ${c.source} | ${c.sourceId} | ${c.httpStatus} | ${c.doc?.groupName || ''} | ${c.doc?.description || ''} |\n`;
|
||
}
|
||
md += '\n';
|
||
return md;
|
||
}
|
||
|
||
async function runBaseline() {
|
||
const cfg = readLocalConfig();
|
||
const baseUrl = buildBaseUrl(cfg);
|
||
const methodDocs = loadSystemMethodDocs();
|
||
const conn = await createDbConnection(cfg);
|
||
|
||
try {
|
||
const selected = await loadRecentSystemLogCases(conn, methodDocs, LIMIT, TEST_USERNAME);
|
||
if (!selected.length) {
|
||
throw new Error('未从 wms_system_log 找到可回放的只读 method 样本');
|
||
}
|
||
|
||
const login = await callSystemMethod(
|
||
baseUrl,
|
||
'user.login',
|
||
{ username: TEST_USERNAME, password: TEST_PASSWORD },
|
||
{ platform: 'web', project: 'wms' },
|
||
''
|
||
);
|
||
const token = extractToken(login);
|
||
if (!token) {
|
||
throw new Error('登录成功但未拿到 token,请检查 user.login 返回结构');
|
||
}
|
||
|
||
const cases = [];
|
||
for (const item of selected) {
|
||
const call = await callSystemMethod(baseUrl, item.method, item.body, item.headers, token);
|
||
cases.push({
|
||
method: item.method,
|
||
source: item.source,
|
||
sourceId: item.sourceId,
|
||
sourceCreatedAt: item.sourceCreatedAt,
|
||
headers: item.headers,
|
||
requestBody: item.body,
|
||
doc: methodDocs.get(item.method) || null,
|
||
httpStatus: call.httpStatus,
|
||
response: call.response,
|
||
normalized: call.normalized,
|
||
});
|
||
}
|
||
|
||
const baseline = {
|
||
generatedAt: new Date().toISOString(),
|
||
baseUrl,
|
||
user: TEST_USERNAME,
|
||
note: 'Before-change baseline generated from recent system logs and methods/system.json',
|
||
login: {
|
||
httpStatus: login.httpStatus,
|
||
normalized: login.normalized,
|
||
},
|
||
cases,
|
||
};
|
||
|
||
ensureOutputDir();
|
||
fs.writeFileSync(BASELINE_FILE, JSON.stringify(baseline, null, 2), 'utf8');
|
||
fs.writeFileSync(BASELINE_SUMMARY, renderBaselineMarkdown(baseline), 'utf8');
|
||
|
||
console.log(`BASELINE_OK cases=${cases.length}`);
|
||
console.log(`BASELINE_FILE ${BASELINE_FILE}`);
|
||
console.log(`BASELINE_SUMMARY ${BASELINE_SUMMARY}`);
|
||
} finally {
|
||
await conn.end();
|
||
}
|
||
}
|
||
|
||
async function runCompare() {
|
||
const baselineFile = resolveBaselineFile();
|
||
if (!fs.existsSync(baselineFile)) {
|
||
throw new Error(`baseline 文件不存在: ${BASELINE_FILE}`);
|
||
}
|
||
|
||
const baseline = JSON.parse(fs.readFileSync(baselineFile, 'utf8'));
|
||
const cfg = readLocalConfig();
|
||
const baseUrl = buildBaseUrl(cfg);
|
||
|
||
const login = await callSystemMethod(
|
||
baseUrl,
|
||
'user.login',
|
||
{ username: TEST_USERNAME, password: TEST_PASSWORD },
|
||
{ platform: 'web', project: 'wms' },
|
||
''
|
||
);
|
||
const token = extractToken(login);
|
||
if (!token) {
|
||
throw new Error('compare 阶段登录后未拿到 token');
|
||
}
|
||
|
||
const compared = [];
|
||
let mismatchCount = 0;
|
||
|
||
for (const item of baseline.cases || []) {
|
||
const call = await callSystemMethod(
|
||
baseUrl,
|
||
item.method,
|
||
item.requestBody,
|
||
item.headers || { platform: 'web', project: 'wms' },
|
||
token
|
||
);
|
||
|
||
const beforeNorm = item.normalized;
|
||
const afterNorm = call.normalized;
|
||
const same = stableStringify(beforeNorm) === stableStringify(afterNorm);
|
||
if (!same) mismatchCount++;
|
||
|
||
compared.push({
|
||
method: item.method,
|
||
sourceId: item.sourceId,
|
||
httpStatusBefore: item.httpStatus,
|
||
httpStatusAfter: call.httpStatus,
|
||
outputEqual: same,
|
||
beforeNormalized: beforeNorm,
|
||
afterNormalized: afterNorm,
|
||
});
|
||
}
|
||
|
||
const report = {
|
||
comparedAt: new Date().toISOString(),
|
||
baseUrl,
|
||
user: TEST_USERNAME,
|
||
baselineFile,
|
||
total: compared.length,
|
||
mismatchCount,
|
||
pass: mismatchCount === 0,
|
||
items: compared,
|
||
};
|
||
|
||
ensureOutputDir();
|
||
fs.writeFileSync(COMPARE_FILE, JSON.stringify(report, null, 2), 'utf8');
|
||
|
||
console.log(`COMPARE_DONE total=${report.total} mismatch=${report.mismatchCount}`);
|
||
console.log(`COMPARE_FILE ${COMPARE_FILE}`);
|
||
if (report.mismatchCount > 0) {
|
||
process.exitCode = 1;
|
||
}
|
||
}
|
||
|
||
async function main() {
|
||
if (MODE !== 'baseline' && MODE !== 'compare') {
|
||
throw new Error('用法: node script/method-output-regression.cjs <baseline|compare> [limit]');
|
||
}
|
||
if (MODE === 'baseline') {
|
||
await runBaseline();
|
||
} else {
|
||
await runCompare();
|
||
}
|
||
}
|
||
|
||
main().catch(err => {
|
||
console.error(err);
|
||
process.exit(1);
|
||
});
|