You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB

import { describe, expect, it } from "vitest";
import { decidePull, hasActiveFilterConfig } from "@/services/imap/pull-filter";
describe("decidePull", () => {
it("empty rules → business fallback for 新增预报", () => {
const d = decidePull({
fromAddr: "a@x.com",
subject: "新增预报 MATU2745683",
body: "请查收",
filenames: ["卡派.xlsx"],
rules: [],
});
expect(d).toEqual({ pull: true, reason: "BUSINESS_FALLBACK" });
});
it("empty rules → skip unrelated", () => {
const d = decidePull({
fromAddr: "noreply@github.com",
subject: "Your security digest",
body: "hello",
filenames: [],
rules: [],
});
expect(d.pull).toBe(false);
expect(d.reason).toBe("SKIP_FILTER");
});
it("sender blacklist wins", () => {
const d = decidePull({
fromAddr: "spam@x.com",
subject: "新增预报",
body: "",
rules: [
{ kind: "sender_blacklist", value: "spam@x.com", enabled: true },
{ kind: "keyword", value: "新增预报", enabled: true },
],
});
expect(d).toEqual({ pull: false, reason: "SKIP_BLACKLIST" });
});
it("keyword blacklist wins over whitelist? no — blacklist first then whitelist", () => {
const d = decidePull({
fromAddr: "vip@x.com",
subject: "广告促销",
body: "广告",
rules: [
{ kind: "keyword_blacklist", value: "广告", enabled: true },
{ kind: "sender_whitelist", value: "vip@x.com", enabled: true },
],
});
expect(d).toEqual({ pull: false, reason: "SKIP_BLACKLIST" });
});
it("whitelist pulls without keyword", () => {
const d = decidePull({
fromAddr: "vip@Partner.COM",
subject: "hello",
body: "world",
rules: [
{ kind: "sender_whitelist", value: "vip@partner.com", enabled: true },
{ kind: "keyword", value: "预报", enabled: true },
],
});
expect(d).toEqual({ pull: true, reason: "WHITELIST" });
});
it("keyword match when configured", () => {
const d = decidePull({
fromAddr: "other@x.com",
subject: "关于卡派资料",
body: "",
rules: [{ kind: "keyword", value: "卡派", enabled: true }],
});
expect(d).toEqual({ pull: true, reason: "KEYWORD" });
});
it("configured but no match → SKIP_FILTER (no business fallback)", () => {
const d = decidePull({
fromAddr: "other@x.com",
subject: "新增预报 MATU2745683",
body: "请查收卡派",
filenames: ["卡派.xlsx"],
rules: [{ kind: "keyword", value: "只有这个才拉", enabled: true }],
});
expect(d).toEqual({ pull: false, reason: "SKIP_FILTER" });
});
it("hasActiveFilterConfig ignores disabled", () => {
expect(
hasActiveFilterConfig([
{ kind: "keyword", value: "x", enabled: false },
]),
).toBe(false);
});
});