| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { httpErrorMessage, connectionErrorMessage, nativeFailureMessage } from "./error-message.mjs"; |
| 4 | |
| 5 | test("HTTP failures name the operation and avoid upstream HTML or opaque runtime output", () => { |
| 6 | for (const [status, reason] of [[401, "登录会话"], [409, "操作"], [413, "大小"], [503, "尚未就绪"], [504, "超时"]]) { |
| 7 | const message = httpErrorMessage("/environment/resources", status, "<html>token=private</html>", "text/html"); |
| 8 | assert.match(message, /资源配置/); |
| 9 | assert.ok(message.includes(reason)); |
| 10 | assert.ok(!message.includes("private")); |
| 11 | } |
| 12 | const specific = "上传窗口已关闭,请重新点击网页上传按钮"; |
| 13 | assert.equal(httpErrorMessage("/input/files", 409, specific, "text/plain; charset=utf-8"), specific); |
| 14 | assert.match(connectionErrorMessage("/input/files"), /传递文件.*连接已中断.*核对结果/); |
| 15 | assert.match(httpErrorMessage("/settings", 503, "", "", "POST"), /无法确认设置是否已保存.*核对设置/); |
| 16 | }); |
| 17 | |
| 18 | test("native errors retain precise rules and explain opaque failures in context", () => { |
| 19 | assert.match(nativeFailureMessage("exportDiagnostics", "Native operation timeout"), /导出诊断包.*超时/); |
| 20 | assert.match(nativeFailureMessage("uploadHostFiles", "operation failed token=private"), /上传本机文件失败.*可识别的原因/); |
| 21 | assert.ok(!nativeFailureMessage("uploadHostFiles", "operation failed token=private").includes("private")); |
| 22 | const rule = "请选择本机项目中的文件"; |
| 23 | assert.equal(nativeFailureMessage("uploadHostFiles", rule), rule); |
| 24 | }); |