Files
taiheEhu/scripts/tests/repair-utils.test.cjs
2026-05-08 15:09:41 +08:00

86 lines
2.3 KiB
JavaScript

const assert = require("node:assert/strict");
const {
appendRepairAttachments,
clampDescription,
isValidPhone,
buildDeviceMap,
normalizeChosenFiles,
validateRepairForm
} = require("../../tmp/report-tests/repair/repair-utils.js");
function run(name, fn) {
try {
fn();
console.log(`PASS ${name}`);
} catch (error) {
console.error(`FAIL ${name}`);
throw error;
}
}
run("clampDescription trims to 60 chars", () => {
assert.equal(clampDescription("a".repeat(61)).length, 60);
});
run("isValidPhone accepts mainland mobile number", () => {
assert.equal(isValidPhone("13689569989"), true);
});
run("validateRepairForm rejects empty problem description", () => {
assert.equal(
validateRepairForm({
selectedDeviceId: "A1",
description: "",
contactName: "张小龙",
phone: "13689569989"
}),
"请填写问题描述"
);
});
run("buildDeviceMap groups devices by type", () => {
const map = buildDeviceMap([
{ id: "A1", type: "monitor", label: "设备A", params: "P1" },
{ id: "B1", type: "camera", label: "设备B", params: "P2" }
]);
assert.equal(map.monitor.length, 1);
assert.equal(map.camera[0].id, "B1");
});
run("normalizeChosenFiles tags images and videos", () => {
const files = normalizeChosenFiles([
{ tempFilePath: "x/a.png", fileType: "image", size: 10 },
{ tempFilePath: "x/b.mp4", fileType: "video", size: 20, thumbTempFilePath: "x/b.jpg", duration: 5 }
]);
assert.equal(files[0].kind, "image");
assert.equal(files[1].kind, "video");
assert.equal(files[1].thumbPath, "x/b.jpg");
});
run("appendRepairAttachments blocks second video", () => {
const result = appendRepairAttachments(
[{ id: "v1", kind: "video", path: "x/1.mp4" }],
[{ id: "v2", kind: "video", path: "x/2.mp4" }]
);
assert.equal(result.errorMessage, "最多上传1个视频");
assert.equal(result.attachments.length, 1);
});
run("appendRepairAttachments limits images to 9", () => {
const existing = Array.from({ length: 8 }, (_, index) => ({
id: `img-${index}`,
kind: "image",
path: `x/${index}.png`
}));
const result = appendRepairAttachments(existing, [
{ id: "img-8", kind: "image", path: "x/8.png" },
{ id: "img-9", kind: "image", path: "x/9.png" }
]);
assert.equal(result.errorMessage, "最多上传9张图片");
assert.equal(result.attachments.length, 9);
});