forked from longtao.wu/droboticflow
110 lines
5.7 KiB
JavaScript
110 lines
5.7 KiB
JavaScript
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
|
||
import {
|
||
SYSTEM_STATUS_TIMEOUT_MS,
|
||
classifySystemStatusError,
|
||
systemStatusErrorMessage,
|
||
} from "../../src/lib/system-status.js";
|
||
|
||
function httpError(status, message = "") {
|
||
return Object.assign(new Error(message), { status });
|
||
}
|
||
|
||
test("classifies a timed-out check as timeout", () => {
|
||
assert.equal(classifySystemStatusError(new Error("aborted"), { timedOut: true }), "timeout");
|
||
assert.equal(classifySystemStatusError(undefined, { timedOut: true, online: true }), "timeout");
|
||
});
|
||
|
||
test("timeout takes priority over offline and HTTP status", () => {
|
||
assert.equal(
|
||
classifySystemStatusError(new TypeError("Failed to fetch"), { timedOut: true, online: false }),
|
||
"timeout",
|
||
);
|
||
assert.equal(classifySystemStatusError(httpError(502, "bad gateway"), { timedOut: true, online: false }), "timeout");
|
||
assert.equal(classifySystemStatusError(httpError(502, "bad gateway"), { timedOut: true, online: true }), "timeout");
|
||
});
|
||
|
||
test("classifies a browser-offline or unreachable network as offline", () => {
|
||
assert.equal(classifySystemStatusError(new Error("boom"), { online: false }), "offline");
|
||
assert.equal(classifySystemStatusError(undefined, { online: false }), "offline");
|
||
// fetch() rejects with a TypeError when the network is unreachable.
|
||
assert.equal(classifySystemStatusError(new TypeError("Failed to fetch"), { online: true }), "offline");
|
||
assert.equal(classifySystemStatusError(new TypeError("Failed to fetch")), "offline");
|
||
});
|
||
|
||
test("offline takes priority over HTTP status", () => {
|
||
assert.equal(classifySystemStatusError(httpError(503, "unavailable"), { online: false }), "offline");
|
||
assert.equal(classifySystemStatusError(httpError(503), { timedOut: false, online: false }), "offline");
|
||
});
|
||
|
||
test("classifies errors carrying a numeric status as http", () => {
|
||
assert.equal(classifySystemStatusError(httpError(502, "bad gateway"), { online: true }), "http");
|
||
assert.equal(classifySystemStatusError(httpError(404)), "http");
|
||
assert.equal(classifySystemStatusError({ status: 500 }, { online: true }), "http");
|
||
// A numeric status wins over the TypeError network heuristic.
|
||
assert.equal(classifySystemStatusError(Object.assign(new TypeError("x"), { status: 500 })), "http");
|
||
// A non-numeric status is not an HTTP response.
|
||
assert.equal(classifySystemStatusError({ status: "502" }, { online: true }), "error");
|
||
});
|
||
|
||
test("classifies anything else as error", () => {
|
||
assert.equal(classifySystemStatusError(new Error("boom"), { online: true }), "error");
|
||
assert.equal(classifySystemStatusError(new Error("boom")), "error");
|
||
assert.equal(classifySystemStatusError("boom"), "error");
|
||
assert.equal(classifySystemStatusError(null), "error");
|
||
assert.equal(classifySystemStatusError(undefined), "error");
|
||
});
|
||
|
||
test("timeout message reports the timeout in seconds", () => {
|
||
assert.equal(SYSTEM_STATUS_TIMEOUT_MS, 10000);
|
||
assert.equal(systemStatusErrorMessage("timeout", new Error("timeout")), "状态检查超时(超过 10 秒未响应)");
|
||
assert.equal(systemStatusErrorMessage("timeout", undefined, 5000), "状态检查超时(超过 5 秒未响应)");
|
||
assert.equal(systemStatusErrorMessage("timeout", undefined, 2500), "状态检查超时(超过 3 秒未响应)");
|
||
});
|
||
|
||
test("offline message ignores the original error", () => {
|
||
assert.equal(
|
||
systemStatusErrorMessage("offline", new TypeError("Failed to fetch")),
|
||
"网络不可达,无法连接 Drobodata API",
|
||
);
|
||
assert.equal(systemStatusErrorMessage("offline", undefined), "网络不可达,无法连接 Drobodata API");
|
||
});
|
||
|
||
test("http message includes the status code and the error message", () => {
|
||
assert.equal(systemStatusErrorMessage("http", httpError(502, "bad gateway")), "Drobodata API 返回 502:bad gateway");
|
||
assert.equal(systemStatusErrorMessage("http", httpError(503, "not ready")), "Drobodata API 返回 503:not ready");
|
||
});
|
||
|
||
test("http message shows only the status code when there is no error message", () => {
|
||
// An error without a message falls back to String(error), so it must also stringify empty.
|
||
const silent = { status: 500, toString: () => "" };
|
||
assert.equal(systemStatusErrorMessage("http", silent), "Drobodata API 返回 500");
|
||
const unnamed = Object.assign(new Error(""), { name: "", status: 504 });
|
||
assert.equal(systemStatusErrorMessage("http", unnamed), "Drobodata API 返回 504");
|
||
});
|
||
|
||
test("other errors use the error message or fall back to the default text", () => {
|
||
assert.equal(systemStatusErrorMessage("error", new Error("boom")), "boom");
|
||
assert.equal(systemStatusErrorMessage("error", "plain failure"), "plain failure");
|
||
assert.equal(systemStatusErrorMessage("error", ""), "无法连接 Drobodata API");
|
||
assert.equal(systemStatusErrorMessage("error", undefined), "无法连接 Drobodata API");
|
||
assert.equal(systemStatusErrorMessage("error", null), "无法连接 Drobodata API");
|
||
// Unknown kinds are treated like generic errors.
|
||
assert.equal(systemStatusErrorMessage("unknown", new Error("boom")), "boom");
|
||
});
|
||
|
||
test("messages match each classified result end to end", () => {
|
||
const cases = [
|
||
[new Error("aborted"), { timedOut: true, online: false }, "状态检查超时(超过 10 秒未响应)"],
|
||
[httpError(502, "bad gateway"), { online: false }, "网络不可达,无法连接 Drobodata API"],
|
||
[httpError(502, "bad gateway"), { online: true }, "Drobodata API 返回 502:bad gateway"],
|
||
[new Error("boom"), { online: true }, "boom"],
|
||
[null, { online: true }, "无法连接 Drobodata API"],
|
||
];
|
||
for (const [error, options, expected] of cases) {
|
||
const kind = classifySystemStatusError(error, options);
|
||
assert.equal(systemStatusErrorMessage(kind, error), expected);
|
||
}
|
||
});
|