chore: 清理死代码,排行榜数据外置并补基础测试
删除无引用的 token/密码工具与系统信息工具;复旦排行榜数据拆到 JSON 便于维护;新增 test/logic.test.js 覆盖状态机、管理员判定、正则转义、SSRF 与超时夹紧。
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { canTransition, STATUS_TRANSITIONS } from "../utils/record_state.js";
|
||||
import { isAdmin } from "../utils/permissions.js";
|
||||
import { escapeRegex } from "../agent/infra/tools/db/utils.js";
|
||||
import {
|
||||
checkUrlAllowed,
|
||||
isPrivateAddress,
|
||||
} from "../agent/infra/tools/web/url_guard.js";
|
||||
import { clampTimeout } from "../agent/infra/tools/web/http_client.js";
|
||||
|
||||
test("订单状态机只允许正向流转", () => {
|
||||
assert.equal(canTransition("pending", "confirmed"), true);
|
||||
assert.equal(canTransition("confirmed", "in_progress"), true);
|
||||
assert.equal(canTransition("in_progress", "completed"), true);
|
||||
assert.equal(canTransition("pending", "cancelled"), true);
|
||||
|
||||
// 终态不可回退,也不能跳步
|
||||
assert.equal(canTransition("completed", "in_progress"), false);
|
||||
assert.equal(canTransition("cancelled", "pending"), false);
|
||||
assert.equal(canTransition("pending", "completed"), false);
|
||||
// 未知状态一律拒绝
|
||||
assert.equal(canTransition("unknown", "pending"), false);
|
||||
});
|
||||
|
||||
test("管理端身份要求显式 role=admin", () => {
|
||||
assert.equal(isAdmin(null), false);
|
||||
assert.equal(isAdmin({}), false);
|
||||
assert.equal(isAdmin({ app: { "wxapp-escort-admin": { role: ["admin"] } } }), true);
|
||||
assert.equal(isAdmin({ app: { "wxapp-escort-admin": { role: "admin" } } }), true);
|
||||
assert.equal(isAdmin({ app: { "wxapp-escort-admin": { role: ["user"] } } }), false);
|
||||
});
|
||||
|
||||
test("关键词按字面量转义并截断", () => {
|
||||
assert.equal(escapeRegex("张三"), "张三");
|
||||
assert.equal(escapeRegex("(a+)+$"), "\\(a\\+\\)\\+\\$");
|
||||
assert.equal(escapeRegex("a".repeat(100)).length, 64);
|
||||
assert.equal(escapeRegex(null), "");
|
||||
});
|
||||
|
||||
test("SSRF 校验拦截内网、环回与云元数据地址", () => {
|
||||
const blocked = [
|
||||
"http://127.0.0.1/",
|
||||
"http://10.1.2.3/",
|
||||
"http://169.254.169.254/latest/meta-data/",
|
||||
"http://192.168.1.1/",
|
||||
"http://localhost:8080/",
|
||||
"http://[::1]/",
|
||||
"file:///etc/passwd",
|
||||
"ftp://example.com/",
|
||||
];
|
||||
for (const url of blocked) {
|
||||
assert.notEqual(checkUrlAllowed(url), null, url);
|
||||
}
|
||||
assert.equal(checkUrlAllowed("https://www.baidu.com"), null);
|
||||
assert.equal(checkUrlAllowed("not a url"), "Invalid URL: not a url");
|
||||
});
|
||||
|
||||
test("内网地址判定覆盖 IPv4 映射与链路本地", () => {
|
||||
assert.equal(isPrivateAddress("127.0.0.1"), true);
|
||||
assert.equal(isPrivateAddress("8.8.8.8"), false);
|
||||
assert.equal(isPrivateAddress("fe80::1"), true);
|
||||
assert.equal(isPrivateAddress("::ffff:10.0.0.1"), true);
|
||||
});
|
||||
|
||||
test("出网超时被夹紧在 1s-15s", () => {
|
||||
assert.equal(clampTimeout(undefined), 15000);
|
||||
assert.equal(clampTimeout(0), 15000);
|
||||
assert.equal(clampTimeout("abc"), 15000);
|
||||
assert.equal(clampTimeout(600000), 15000);
|
||||
assert.equal(clampTimeout(300), 1000);
|
||||
assert.equal(clampTimeout(5000), 5000);
|
||||
});
|
||||
|
||||
test("状态机表覆盖全部订单状态", () => {
|
||||
assert.deepEqual(
|
||||
Object.keys(STATUS_TRANSITIONS).sort(),
|
||||
["cancelled", "completed", "confirmed", "in_progress", "pending"]
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user