forked from longtao.wu/droboticflow
91 lines
4.9 KiB
JavaScript
91 lines
4.9 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { userErrorMessage } from "../../src/lib/user-error-message.js";
|
|
import { getActionErrorMessage } from "../../src/lib/action-errors.js";
|
|
|
|
test("common API errors explain the problem without implementation details", () => {
|
|
const cases = [
|
|
["invalid phone or password", "账号或密码不正确"],
|
|
["API token is required", "登录信息缺失或已失效"],
|
|
["csrf token mismatch", "当前登录状态无法完成这次操作"],
|
|
["permission denied", "当前账号没有操作权限"],
|
|
["E11000 duplicate key users phone", "相同的记录已存在"],
|
|
["File not found: /workspace/private", "所需内容不存在或已被移除"],
|
|
["Validation failed (sliceSegments.1.name: invalid)", "填写的内容不符合要求"],
|
|
["已保存草稿已有新版本,请核对后再保存", "这条审核意见已被修改,本次保存未成功"],
|
|
["提交前校验未通过;草稿已保留,请修正后重新确认", "审核内容有不符合要求的地方,未能提交"],
|
|
["worker lost heartbeat", "任务处理已中断"],
|
|
["no space left on device", "系统存储空间不足"],
|
|
["OOMKilled", "系统处理资源不足"],
|
|
];
|
|
for (const [input, expected] of cases) assert.equal(userErrorMessage(input), expected);
|
|
});
|
|
|
|
test("unknown network and submission outcomes must never become confirmed failures", () => {
|
|
for (const text of ["Failed to fetch", "request timed out", "deadline exceeded"]) {
|
|
assert.match(userErrorMessage(text), /暂时无法确认操作结果/);
|
|
assert.doesNotMatch(userErrorMessage(text), /未成功|保存失败|提交失败/);
|
|
}
|
|
assert.equal(userErrorMessage("暂时无法确认是否保存成功,文字仍保留。"), "暂时无法确认是否保存成功,文字仍保留。");
|
|
});
|
|
|
|
test("rendering does not mutate errors, status, codes, or diagnostics", () => {
|
|
const error = Object.assign(new Error("unmapped server internals"), {
|
|
code: "unknown",
|
|
status: 503,
|
|
requestId: "abc",
|
|
details: { cause: "database" },
|
|
});
|
|
const before = { message: error.message, stack: error.stack, ...error };
|
|
assert.equal(getActionErrorMessage(error), "系统暂时无法完成操作");
|
|
assert.deepEqual({ message: error.message, stack: error.stack, ...error }, before);
|
|
});
|
|
|
|
test("Chinese business facts and empty states survive display formatting", () => {
|
|
assert.equal(userErrorMessage("手机号格式不正确,请重新填写。"), "手机号格式不正确。");
|
|
assert.equal(userErrorMessage("删除失败;已成功删除的项目不受影响。"), "删除失败;已成功删除的项目不受影响。");
|
|
assert.equal(userErrorMessage("已保存"), "已保存");
|
|
assert.equal(userErrorMessage(null), "");
|
|
assert.equal(userErrorMessage(""), "");
|
|
assert.equal(getActionErrorMessage(new Error("opaque_exception"), "视频加载失败"), "视频加载失败");
|
|
});
|
|
|
|
test("formatting remains stable when an API error passes through a page formatter", () => {
|
|
for (const error of [
|
|
new Error("forbidden"),
|
|
{ code: "version_conflict", message: "conflict" },
|
|
new Error("Failed to fetch"),
|
|
]) {
|
|
const once = getActionErrorMessage(error);
|
|
assert.equal(userErrorMessage(once), once);
|
|
}
|
|
});
|
|
|
|
test("missing form fields still identify what is missing", () => {
|
|
assert.equal(userErrorMessage("请填写连接名称。"), "连接名称尚未填写");
|
|
assert.equal(userErrorMessage("请先选择 bucket。"), "存储空间尚未选择");
|
|
assert.equal(userErrorMessage("请输入对象 key。"), "文件路径尚未填写");
|
|
assert.equal(userErrorMessage("请选择供应商,无外部供应商请选择默认供应商。"), "供应商尚未选择");
|
|
});
|
|
|
|
test("pure-instruction copy stays visible instead of masking as 操作暂时未完成 (retro F6)", () => {
|
|
// Regression: E-panel guard copy and acceptance blockers start with 请;
|
|
// the instruction-stripper emptied them into the generic fallback, which is
|
|
// exactly the string customers reported as "点了很多遍都没有反应".
|
|
assert.equal(
|
|
userErrorMessage("请先保存或清空动作问题备注,再切换切片或提交。"),
|
|
"请先保存或清空动作问题备注,再切换切片或提交。",
|
|
);
|
|
assert.equal(userErrorMessage("请先处理已保存的问题,再通过验收。"), "请先处理已保存的问题,再通过验收。");
|
|
// Fact + appended instruction keeps today's behaviour (fact only).
|
|
assert.equal(userErrorMessage("手机号格式不正确,请重新填写。"), "手机号格式不正确。");
|
|
});
|
|
|
|
test("phone uniqueness errors identify the conflicting account field", () => {
|
|
const error = Object.assign(new Error("Validation failed (phone: phone must be unique)"), {
|
|
status: 400,
|
|
issues: [{ field: "phone", message: "phone must be unique" }],
|
|
});
|
|
assert.equal(getActionErrorMessage(error, "保存用户失败"), "该手机号已被其他账号使用");
|
|
});
|