75 lines
3.3 KiB
TypeScript
75 lines
3.3 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { cloudflareLine, cloudflareTechItems, healthLine, healthMessage, healthTechItems } from "./site-fields.ts";
|
|
|
|
describe("健康检测文案(HC-1)", () => {
|
|
it("人机验证单独成类;失败原因一句,原始判定进技术信息", () => {
|
|
const h = {
|
|
ok: false,
|
|
status: "auth_expired",
|
|
latencyMs: 120,
|
|
message: '健康检查:首页里找不到登录标志(选择器 a[href*="logout"])',
|
|
detail: { reason: "marker_missing", selector: 'a[href*="logout"]', path: "index.php", title: "Site :: 首页" },
|
|
};
|
|
expect(healthMessage(h)).toContain("找不到登录标志");
|
|
expect(healthTechItems(h)).toEqual([
|
|
{ label: "判定", value: "页面里找不到登录标志(marker_missing)" },
|
|
{ label: "登录标志选择器", value: 'a[href*="logout"]', mono: true },
|
|
{ label: "检测页面", value: "index.php", mono: true },
|
|
{ label: "页面标题", value: "Site :: 首页" },
|
|
]);
|
|
expect(healthLine({ ok: false, status: "challenge", latencyMs: null, message: null }, null).text).toContain(
|
|
"需要人机验证",
|
|
);
|
|
// 旧记录没有 detail
|
|
expect(healthTechItems({ ok: true, status: "ok", latencyMs: 1, message: null })).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("cloudflareLine / cloudflareTechItems(CF-2)", () => {
|
|
const now = new Date("2026-09-30T12:00:00+08:00");
|
|
const cfg = (cloudflare: Record<string, unknown>) => ({ cloudflare });
|
|
it("没遇到过挑战不显示", () => {
|
|
expect(cloudflareLine({}, now)).toBeNull();
|
|
expect(cloudflareLine(cfg({ lastSolvedBy: "auto" }), now)).toBeNull();
|
|
expect(cloudflareTechItems({})).toEqual([]);
|
|
});
|
|
it("需要你验证 > 浏览器代理中 > 自动通过", () => {
|
|
const at = "2026-09-30T11:00:00+08:00";
|
|
expect(cloudflareLine(cfg({ lastChallengeAt: at, pending: "human", lastSolvedBy: "auto" }), now)).toEqual({
|
|
text: "Cloudflare:需要你验证",
|
|
tone: "warning",
|
|
takeover: true,
|
|
});
|
|
const until = new Date("2026-09-30T17:30:00+08:00");
|
|
const line = cloudflareLine(cfg({ lastChallengeAt: at, browserUntil: until.toISOString() }), now);
|
|
const hm = `${String(until.getHours()).padStart(2, "0")}:${String(until.getMinutes()).padStart(2, "0")}`;
|
|
expect(line?.text).toBe(`Cloudflare:浏览器代理中,至 ${hm}`);
|
|
expect(cloudflareLine(cfg({ lastChallengeAt: at, lastSolvedBy: "vision" }), now)?.text).toBe(
|
|
"Cloudflare:自动通过",
|
|
);
|
|
// 代理期已过、没通过过、有错误
|
|
expect(
|
|
cloudflareLine(cfg({ lastChallengeAt: at, browserUntil: at, lastError: "浏览器服务不可用" }), now)?.text,
|
|
).toBe("Cloudflare:没能通过");
|
|
});
|
|
it("原始记录进技术信息", () => {
|
|
const items = cloudflareTechItems(
|
|
cfg({
|
|
lastChallengeAt: "2026-09-30T11:00:00+08:00",
|
|
lastRule: "header cf-mitigated=challenge",
|
|
lastResult: "reuse_fails",
|
|
lastSolvedBy: "auto",
|
|
cfReuse: "fails",
|
|
}),
|
|
);
|
|
expect(items.map((i) => i.label)).toEqual([
|
|
"最近遇到挑战",
|
|
"挑战规则",
|
|
"处理结果",
|
|
"最近通过方式",
|
|
"cf_clearance 复用",
|
|
]);
|
|
expect(items.find((i) => i.label === "处理结果")?.value).toContain("reuse_fails");
|
|
});
|
|
});
|