safety-eval-service-frontend/docs/test-reports/测试用例/test-enterprise-info-granul...

372 lines
15 KiB
JavaScript
Raw Permalink Normal View History

2026-06-23 18:07:30 +08:00
/**
* 企业信息管理 - 细粒度接口联调测试
*
* 运行:
* source scripts/setup-test-env.sh
* node scripts/test-enterprise-info-granular.mjs
*/
const API_BASE = process.env.API_BASE || "http://127.0.0.1:8095/safety-eval";
const TS = Date.now();
const results = [];
let passed = 0;
let failed = 0;
const createdIds = {};
/** 保持 ID 为字符串,避免雪花 ID 超出 JS 安全整数精度 */
function normalizeId(id) {
if (id === undefined || id === null || id === "") return undefined;
return String(id);
}
function record(id, name, condition, detail = "") {
const status = condition ? "PASS" : "FAIL";
if (condition) passed += 1;
else failed += 1;
results.push({ id, name, status, detail });
}
async function request(method, path, body) {
const url = `${API_BASE}${path}`;
const options = {
method,
headers: { "Content-Type": "application/json" },
};
if (body !== undefined) options.body = JSON.stringify(body);
const res = await fetch(url, options);
const text = await res.text();
try {
return { httpStatus: res.status, ...JSON.parse(text) };
}
catch {
return { httpStatus: res.status, success: false, raw: text };
}
}
function assertSuccess(id, name, res, extra = "") {
const ok = res.success === true && res.code === "0";
record(id, name, ok, ok ? extra : (res.message || res.raw || `http=${res.httpStatus}`));
return ok;
}
function assertHasData(id, name, res, extra = "") {
const ok = res.success === true && res.code === "0" && res.data != null;
record(id, name, ok, ok ? extra : (res.message || "data 为空"));
return ok;
}
async function testPagination(id, name, path, minTotal = 1) {
const sep = path.includes("?") ? "&" : "?";
const page1 = await request("GET", `${path}${sep}current=1&size=2`);
const ok1 = assertSuccess(`${id}-01`, `${name}-分页第1页`, page1, `total=${page1.total ?? page1.totalCount ?? "?"}`);
const page2 = await request("GET", `${path}${sep}current=2&size=2`);
assertSuccess(`${id}-02`, `${name}-分页第2页`, page2);
if (ok1 && (page1.total ?? page1.totalCount ?? 0) >= minTotal) {
record(`${id}-03`, `${name}-分页total有效`, true, `total=${page1.total ?? page1.totalCount}`);
}
return page1;
}
async function run() {
console.log(`\n=== 企业信息管理 细粒度接口测试 ===`);
console.log(`API Base: ${API_BASE}`);
console.log(`Timestamp: ${TS}\n`);
const orgPage = await testPagination("EI-API-ORG", "机构信息", "/org-info/page", 1);
let orgId = orgPage.data?.[0]?.id;
if (orgId) {
const orgGet = await request("GET", `/org-info/get?id=${normalizeId(orgId)}`);
assertHasData("EI-API-ORG-04", "机构信息-详情", orgGet, `id=${orgId}`);
const orgMod = await request("POST", "/org-info/modify", {
...orgGet.data,
principalName: orgGet.data.principalName,
});
assertSuccess("EI-API-ORG-05", "机构信息-修改", orgMod);
}
await testPagination("EI-API-DEPT", "部门", "/org-department/page", 1);
const deptName = `联调部门-${TS}`;
const deptSave = await request("POST", "/org-department/save", {
deptName,
managerName: "测试负责人",
deptLevelName: "一级",
});
if (assertSuccess("EI-API-DEPT-04", "部门-新增", deptSave)) {
createdIds.dept = normalizeId(deptSave.data?.id);
const deptGet = await request("GET", `/org-department/get?id=${createdIds.dept}`);
if (assertHasData("EI-API-DEPT-05", "部门-详情", deptGet)) {
const deptMod = await request("POST", "/org-department/modify", {
id: createdIds.dept,
deptName: `${deptName}-改`,
managerName: "测试负责人",
deptLevelName: "一级",
});
assertSuccess("EI-API-DEPT-06", "部门-修改", deptMod);
const deptSearch = await request("GET", `/org-department/page?current=1&size=10&deptName=${encodeURIComponent(deptName)}`);
assertSuccess("EI-API-DEPT-07", "部门-名称搜索", deptSearch, `count=${deptSearch.data?.length ?? 0}`);
}
}
const posSave = await request("POST", "/org-position/save", {
positionName: `联调岗位-${TS}`,
dutyDesc: "测试职责",
deptId: createdIds.dept || "102",
});
if (assertSuccess("EI-API-POS-01", "岗位-新增", posSave)) {
createdIds.pos = normalizeId(posSave.data?.id);
}
await testPagination("EI-API-POS-PG", "岗位", "/org-position/page", 1);
if (createdIds.pos) {
const posGet = await request("GET", `/org-position/get?id=${createdIds.pos}`);
if (assertHasData("EI-API-POS-04", "岗位-详情", posGet)) {
const posMod = await request("POST", "/org-position/modify", {
id: createdIds.pos,
positionName: `联调岗位-${TS}-改`,
dutyDesc: "已修改",
deptId: createdIds.dept || posGet.data?.deptId,
});
assertSuccess("EI-API-POS-05", "岗位-修改", posMod);
}
}
const staffSave = await request("POST", "/org-personnel/save", {
userName: `联调人员-${TS}`,
account: `139${String(TS).slice(-8)}`,
genderCode: 1,
genderName: "男",
employmentStatusCode: 1,
employmentStatusName: "在职",
deptId: createdIds.dept || "102",
postId: createdIds.pos || "205",
});
if (assertSuccess("EI-API-STAFF-01", "人员-新增", staffSave)) {
createdIds.staff = normalizeId(staffSave.data?.id);
const staffGet = await request("GET", `/org-personnel/get?id=${createdIds.staff}`);
if (assertHasData("EI-API-STAFF-02", "人员-详情", staffGet)) {
record(
"EI-API-STAFF-05",
"人员-部门名称组装",
!!staffGet.data.deptName,
`deptName=${staffGet.data.deptName ?? "(空)"}`,
);
record(
"EI-API-STAFF-06",
"人员-岗位名称组装",
!!staffGet.data.postName,
`postName=${staffGet.data.postName ?? "(空)"}`,
);
const staffPage = await request("GET", `/org-personnel/page?current=1&size=10&userName=${encodeURIComponent("联调人员")}`);
assertSuccess("EI-API-STAFF-03", "人员-姓名搜索", staffPage);
const pageRow = staffPage.data?.[0];
if (pageRow) {
record("EI-API-STAFF-07", "人员列表-部门字段", !!pageRow.deptName, `deptName=${pageRow.deptName ?? "(空)"}`);
record("EI-API-STAFF-08", "人员列表-岗位字段", !!pageRow.postName, `postName=${pageRow.postName ?? "(空)"}`);
}
const staffMod = await request("POST", "/org-personnel/modify", {
id: createdIds.staff,
userName: `联调人员-${TS}-改`,
account: staffGet.data.account,
genderCode: staffGet.data.genderCode,
genderName: staffGet.data.genderName,
employmentStatusCode: staffGet.data.employmentStatusCode,
employmentStatusName: staffGet.data.employmentStatusName,
deptId: staffGet.data.deptId,
postId: staffGet.data.postId,
});
assertSuccess("EI-API-STAFF-04", "人员-修改", staffMod);
if (staffMod.success) {
const staffAfterMod = await request("GET", `/org-personnel/get?id=${createdIds.staff}`);
record(
"EI-API-STAFF-10",
"人员-修改后变更次数",
(staffAfterMod.data?.changeCount ?? 0) >= 1,
`changeCount=${staffAfterMod.data?.changeCount ?? 0}`,
);
}
const dupSave = await request("POST", "/org-personnel/save", {
userName: `重复账号-${TS}`,
account: staffGet.data.account,
genderCode: 1,
genderName: "男",
employmentStatusCode: 1,
employmentStatusName: "在职",
deptId: staffGet.data.deptId,
postId: staffGet.data.postId,
});
record(
"EI-API-STAFF-11",
"人员-账号重复校验",
dupSave.success === false && String(dupSave.message || "").includes("账号"),
dupSave.message || `code=${dupSave.code}`,
);
const resetPwd = await request("POST", `/org-personnel/reset-password?id=${normalizeId(createdIds.staff)}`);
assertSuccess("EI-API-STAFF-09", "人员-重置密码", resetPwd);
}
}
if (createdIds.staff) {
const certSave = await request("POST", "/org-personnel-cert/save", {
personnelId: createdIds.staff,
certName: `联调证书-${TS}`,
certNo: `CERT-${TS}`,
issueOrg: "测试机关",
validStartDate: "2026-01-01",
validEndDate: "2027-01-01",
});
if (assertSuccess("EI-API-CERT-01", "人员证书-新增", certSave)) {
createdIds.cert = normalizeId(certSave.data?.id);
await testPagination("EI-API-CERT", "人员证书", `/org-personnel-cert/page?personnelId=${createdIds.staff}`, 0);
}
}
else {
record("EI-API-CERT-01", "人员证书-新增", false, "跳过:无人员 id");
}
await testPagination("EI-API-CHANGE", "人员变更", "/org-personnel-change/page", 0);
// 联调数据可能被审核操作污染,测试前重置种子离职单为「未审核」
await request("POST", "/org-resign-apply/modify", {
id: "701",
personnelId: "403",
auditStatusCode: 0,
auditStatusName: "未审核",
});
const equipSave = await request("POST", "/org-equipment/save", {
deviceName: `联调设备-${TS}`,
deviceModel: "T-001",
manufacturer: "测试厂家",
enableFlag: 1,
});
if (assertSuccess("EI-API-EQUIP-01", "装备-新增", equipSave)) {
createdIds.equip = normalizeId(equipSave.data?.id);
const equipGet = await request("GET", `/org-equipment/get?id=${createdIds.equip}`);
if (assertHasData("EI-API-EQUIP-02", "装备-详情", equipGet)) {
const toggle = equipGet.data?.enableFlag === 1 ? 0 : 1;
const equipMod = await request("POST", "/org-equipment/modify", { ...equipGet.data, enableFlag: toggle });
assertSuccess("EI-API-EQUIP-03", "装备-启停", equipMod);
}
}
await testPagination("EI-API-EQUIP-PG", "装备", "/org-equipment/page", 1);
const equipByInstrument = await request("GET", "/org-equipment/page?current=1&size=10&instrumentType=检测仪器");
record(
"EI-API-EQUIP-04",
"装备-仪器类型搜索",
equipByInstrument.success === true && (equipByInstrument.total ?? equipByInstrument.totalCount ?? 0) >= 1 && (equipByInstrument.total ?? equipByInstrument.totalCount ?? 0) < 6,
`total=${equipByInstrument.total ?? equipByInstrument.totalCount ?? "?"}`,
);
const equipByDeviceType = await request("GET", "/org-equipment/page?current=1&size=10&deviceType=便携式");
record(
"EI-API-EQUIP-05",
"装备-设备类型搜索",
equipByDeviceType.success === true && (equipByDeviceType.total ?? equipByDeviceType.totalCount ?? 0) >= 1,
`total=${equipByDeviceType.total ?? equipByDeviceType.totalCount ?? "?"}`,
);
const personnelSeed = await request("GET", "/org-personnel/page?current=1&size=10&userName=钱离职");
const seedRow = personnelSeed.data?.[0];
if (seedRow) {
record("EI-API-CHANGE-04", "人员-变更次数组装", (seedRow.changeCount ?? 0) >= 3, `changeCount=${seedRow.changeCount ?? 0}`);
record("EI-API-CHANGE-05", "人员-就职状态组装", seedRow.employmentStatusCode === 2, `employmentStatusCode=${seedRow.employmentStatusCode}`);
}
const personnelResign = await request("GET", "/org-personnel/page?current=1&size=10&userName=钱离职");
const resignRow = personnelResign.data?.[0];
if (resignRow) {
record("EI-API-CHANGE-06", "人员-离职审核状态", resignRow.resignAuditStatus === 0, `resignAuditStatus=${resignRow.resignAuditStatus ?? "(空)"}`);
}
const resignFilter = await request("GET", "/org-personnel/page?current=1&size=10&resignAuditStatus=0&userName=钱离职");
record(
"EI-API-CHANGE-07",
"人员-离职审核状态筛选",
resignFilter.success === true && (resignFilter.data?.length ?? 0) >= 1,
`count=${resignFilter.data?.length ?? 0}`,
);
const qualSave = await request("POST", "/org-qualification/save", {
licenseTypeName: "测试证照",
certName: `联调资质-${TS}`,
certNo: `QUAL-${TS}`,
issueOrg: "测试机关",
issueDate: "2026-01-01",
validStartDate: "2026-01-01",
validEndDate: "2027-01-01",
enableFlag: 1,
});
if (assertSuccess("EI-API-QUAL-01", "资质-新增", qualSave)) {
createdIds.qual = normalizeId(qualSave.data?.id);
}
await testPagination("EI-API-QUAL-PG", "资质", "/org-qualification/page", 1);
if (createdIds.staff) {
const resignSave = await request("POST", "/org-resign-apply/save", {
personnelId: createdIds.staff,
applicantName: `联调离职-${TS}`,
applyTime: "2026-06-20T10:00:00",
expectedResignDate: "2026-07-31",
resignReason: "联调测试",
auditStatusCode: 0,
auditStatusName: "未审核",
});
if (assertSuccess("EI-API-RESIGN-01", "离职申请-新增", resignSave)) {
createdIds.resign = normalizeId(resignSave.data?.id);
const resignGet = await request("GET", `/org-resign-apply/get?id=${createdIds.resign}`);
if (assertHasData("EI-API-RESIGN-03", "离职申请-详情", resignGet)) {
record(
"EI-API-RESIGN-04",
"离职-审核状态字段",
resignGet.data.auditStatusCode === 0,
`auditStatusCode=${resignGet.data.auditStatusCode}`,
);
record(
"EI-API-RESIGN-05",
"离职-用户名字段组装",
!!resignGet.data.account,
`account=${resignGet.data.account ?? "(空)"}`,
);
}
const audit = await request("POST", "/org-resign-apply/modify", {
id: createdIds.resign,
personnelId: createdIds.staff,
auditStatusCode: 1,
auditStatusName: "已审核",
});
assertSuccess("EI-API-RESIGN-02", "离职申请-审核", audit);
}
}
else {
record("EI-API-RESIGN-01", "离职申请-新增", false, "跳过:无人员 id");
}
await testPagination("EI-API-RESIGN-PG", "离职申请", "/org-resign-apply/page", 0);
const cleanup = [
["cert", "/org-personnel-cert/delete"],
["resign", "/org-resign-apply/delete"],
["staff", "/org-personnel/delete"],
["pos", "/org-position/delete"],
["dept", "/org-department/delete"],
["equip", "/org-equipment/delete"],
["qual", "/org-qualification/delete"],
];
for (const [key, path] of cleanup) {
if (createdIds[key]) {
const del = await request("POST", `${path}?id=${createdIds[key]}`);
assertSuccess(`EI-API-CLEAN-${key}`, `清理-${key}`, del);
}
}
console.log("\n========== 测试结果 ==========\n");
results.forEach((r) => {
const icon = r.status === "PASS" ? "✓" : "✗";
console.log(`${icon} [${r.status}] ${r.id} ${r.name}${r.detail ? `${r.detail}` : ""}`);
});
console.log(`\n合计: ${passed + failed} 通过: ${passed} 失败: ${failed}\n`);
process.exit(failed > 0 ? 1 : 0);
}
run().catch((err) => {
console.error("测试执行失败:", err.message);
process.exit(1);
});